1#! /usr/bin/env python3
2"""Generate coroutine wrappers for block subsystem.
3
4The program parses one or several concatenated c files from stdin,
5searches for functions with the 'co_wrapper' specifier
6and generates corresponding wrappers on stdout.
7
8Usage: block-coroutine-wrapper.py generated-file.c FILE.[ch]...
9
10Copyright (c) 2020 Virtuozzo International GmbH.
11
12This program is free software; you can redistribute it and/or modify
13it under the terms of the GNU General Public License as published by
14the Free Software Foundation; either version 2 of the License, or
15(at your option) any later version.
16
17This program is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20GNU General Public License for more details.
21
22You should have received a copy of the GNU General Public License
23along with this program.  If not, see <http://www.gnu.org/licenses/>.
24"""
25
26import sys
27import re
28from typing import Iterator
29
30
31def gen_header():
32    copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL)
33    copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE)
34    copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE)
35    return f"""\
36/*
37 * File is generated by scripts/block-coroutine-wrapper.py
38 *
39{copyright}
40 */
41
42#include "qemu/osdep.h"
43#include "block/coroutines.h"
44#include "block/block-gen.h"
45#include "block/block_int.h"\
46"""
47
48
49class ParamDecl:
50    param_re = re.compile(r'(?P<decl>'
51                          r'(?P<type>.*[ *])'
52                          r'(?P<name>[a-z][a-z0-9_]*)'
53                          r')')
54
55    def __init__(self, param_decl: str) -> None:
56        m = self.param_re.match(param_decl.strip())
57        if m is None:
58            raise ValueError(f'Wrong parameter declaration: "{param_decl}"')
59        self.decl = m.group('decl')
60        self.type = m.group('type')
61        self.name = m.group('name')
62
63
64class FuncDecl:
65    def __init__(self, return_type: str, name: str, args: str,
66                 variant: str) -> None:
67        self.return_type = return_type.strip()
68        self.name = name.strip()
69        self.struct_name = snake_to_camel(self.name)
70        self.args = [ParamDecl(arg.strip()) for arg in args.split(',')]
71        self.create_only_co = 'mixed' not in variant
72        self.graph_rdlock = 'bdrv_rdlock' in variant
73
74        subsystem, subname = self.name.split('_', 1)
75        self.co_name = f'{subsystem}_co_{subname}'
76
77        t = self.args[0].type
78        if t == 'BlockDriverState *':
79            ctx = 'bdrv_get_aio_context(bs)'
80        elif t == 'BdrvChild *':
81            ctx = 'bdrv_get_aio_context(child->bs)'
82        elif t == 'BlockBackend *':
83            ctx = 'blk_get_aio_context(blk)'
84        else:
85            ctx = 'qemu_get_aio_context()'
86        self.ctx = ctx
87
88    def gen_list(self, format: str) -> str:
89        return ', '.join(format.format_map(arg.__dict__) for arg in self.args)
90
91    def gen_block(self, format: str) -> str:
92        return '\n'.join(format.format_map(arg.__dict__) for arg in self.args)
93
94
95# Match wrappers declared with a co_wrapper mark
96func_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)'
97                          r'\s*co_wrapper'
98                          r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*'
99                          r'(?P<wrapper_name>[a-z][a-z0-9_]*)'
100                          r'\((?P<args>[^)]*)\);$', re.MULTILINE)
101
102
103def func_decl_iter(text: str) -> Iterator:
104    for m in func_decl_re.finditer(text):
105        yield FuncDecl(return_type=m.group('return_type'),
106                       name=m.group('wrapper_name'),
107                       args=m.group('args'),
108                       variant=m.group('variant'))
109
110
111def snake_to_camel(func_name: str) -> str:
112    """
113    Convert underscore names like 'some_function_name' to camel-case like
114    'SomeFunctionName'
115    """
116    words = func_name.split('_')
117    words = [w[0].upper() + w[1:] for w in words]
118    return ''.join(words)
119
120
121def create_mixed_wrapper(func: FuncDecl) -> str:
122    """
123    Checks if we are already in coroutine
124    """
125    name = func.co_name
126    struct_name = func.struct_name
127    graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else ''
128
129    return f"""\
130{func.return_type} {func.name}({ func.gen_list('{decl}') })
131{{
132    if (qemu_in_coroutine()) {{
133        {graph_assume_lock}
134        return {name}({ func.gen_list('{name}') });
135    }} else {{
136        {struct_name} s = {{
137            .poll_state.ctx = {func.ctx},
138            .poll_state.in_progress = true,
139
140{ func.gen_block('            .{name} = {name},') }
141        }};
142
143        s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
144
145        bdrv_poll_co(&s.poll_state);
146        return s.ret;
147    }}
148}}"""
149
150
151def create_co_wrapper(func: FuncDecl) -> str:
152    """
153    Assumes we are not in coroutine, and creates one
154    """
155    name = func.co_name
156    struct_name = func.struct_name
157    return f"""\
158{func.return_type} {func.name}({ func.gen_list('{decl}') })
159{{
160    {struct_name} s = {{
161        .poll_state.ctx = {func.ctx},
162        .poll_state.in_progress = true,
163
164{ func.gen_block('        .{name} = {name},') }
165    }};
166    assert(!qemu_in_coroutine());
167
168    s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
169
170    bdrv_poll_co(&s.poll_state);
171    return s.ret;
172}}"""
173
174
175def gen_wrapper(func: FuncDecl) -> str:
176    assert not '_co_' in func.name
177
178    name = func.co_name
179    struct_name = func.struct_name
180
181    graph_lock=''
182    graph_unlock=''
183    if func.graph_rdlock:
184        graph_lock='    bdrv_graph_co_rdlock();'
185        graph_unlock='    bdrv_graph_co_rdunlock();'
186
187    creation_function = create_mixed_wrapper
188    if func.create_only_co:
189        creation_function = create_co_wrapper
190
191    return f"""\
192/*
193 * Wrappers for {name}
194 */
195
196typedef struct {struct_name} {{
197    BdrvPollCo poll_state;
198    {func.return_type} ret;
199{ func.gen_block('    {decl};') }
200}} {struct_name};
201
202static void coroutine_fn {name}_entry(void *opaque)
203{{
204    {struct_name} *s = opaque;
205
206{graph_lock}
207    s->ret = {name}({ func.gen_list('s->{name}') });
208{graph_unlock}
209    s->poll_state.in_progress = false;
210
211    aio_wait_kick();
212}}
213
214{creation_function(func)}"""
215
216
217def gen_wrappers(input_code: str) -> str:
218    res = ''
219    for func in func_decl_iter(input_code):
220        res += '\n\n\n'
221        res += gen_wrapper(func)
222
223    return res
224
225
226if __name__ == '__main__':
227    if len(sys.argv) < 3:
228        exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...')
229
230    with open(sys.argv[1], 'w', encoding='utf-8') as f_out:
231        f_out.write(gen_header())
232        for fname in sys.argv[2:]:
233            with open(fname, encoding='utf-8') as f_in:
234                f_out.write(gen_wrappers(f_in.read()))
235                f_out.write('\n')
236