xref: /openbmc/qemu/scripts/block-coroutine-wrapper.py (revision e84c07bc73f63cd0251d9fd2c582ad051e27fb39)
1aaaa20b6SVladimir Sementsov-Ogievskiy#! /usr/bin/env python3
2aaaa20b6SVladimir Sementsov-Ogievskiy"""Generate coroutine wrappers for block subsystem.
3aaaa20b6SVladimir Sementsov-Ogievskiy
4aaaa20b6SVladimir Sementsov-OgievskiyThe program parses one or several concatenated c files from stdin,
576a2f554SEmanuele Giuseppe Espositosearches for functions with the 'co_wrapper' specifier
6aaaa20b6SVladimir Sementsov-Ogievskiyand generates corresponding wrappers on stdout.
7aaaa20b6SVladimir Sementsov-Ogievskiy
8aaaa20b6SVladimir Sementsov-OgievskiyUsage: block-coroutine-wrapper.py generated-file.c FILE.[ch]...
9aaaa20b6SVladimir Sementsov-Ogievskiy
10aaaa20b6SVladimir Sementsov-OgievskiyCopyright (c) 2020 Virtuozzo International GmbH.
11aaaa20b6SVladimir Sementsov-Ogievskiy
12aaaa20b6SVladimir Sementsov-OgievskiyThis program is free software; you can redistribute it and/or modify
13aaaa20b6SVladimir Sementsov-Ogievskiyit under the terms of the GNU General Public License as published by
14aaaa20b6SVladimir Sementsov-Ogievskiythe Free Software Foundation; either version 2 of the License, or
15aaaa20b6SVladimir Sementsov-Ogievskiy(at your option) any later version.
16aaaa20b6SVladimir Sementsov-Ogievskiy
17aaaa20b6SVladimir Sementsov-OgievskiyThis program is distributed in the hope that it will be useful,
18aaaa20b6SVladimir Sementsov-Ogievskiybut WITHOUT ANY WARRANTY; without even the implied warranty of
19aaaa20b6SVladimir Sementsov-OgievskiyMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20aaaa20b6SVladimir Sementsov-OgievskiyGNU General Public License for more details.
21aaaa20b6SVladimir Sementsov-Ogievskiy
22aaaa20b6SVladimir Sementsov-OgievskiyYou should have received a copy of the GNU General Public License
23aaaa20b6SVladimir Sementsov-Ogievskiyalong with this program.  If not, see <http://www.gnu.org/licenses/>.
24aaaa20b6SVladimir Sementsov-Ogievskiy"""
25aaaa20b6SVladimir Sementsov-Ogievskiy
26aaaa20b6SVladimir Sementsov-Ogievskiyimport sys
27aaaa20b6SVladimir Sementsov-Ogievskiyimport re
28aaaa20b6SVladimir Sementsov-Ogievskiyfrom typing import Iterator
29aaaa20b6SVladimir Sementsov-Ogievskiy
30aaaa20b6SVladimir Sementsov-Ogievskiy
31aaaa20b6SVladimir Sementsov-Ogievskiydef gen_header():
32aaaa20b6SVladimir Sementsov-Ogievskiy    copyright = re.sub('^.*Copyright', 'Copyright', __doc__, flags=re.DOTALL)
33aaaa20b6SVladimir Sementsov-Ogievskiy    copyright = re.sub('^(?=.)', ' * ', copyright.strip(), flags=re.MULTILINE)
34aaaa20b6SVladimir Sementsov-Ogievskiy    copyright = re.sub('^$', ' *', copyright, flags=re.MULTILINE)
35aaaa20b6SVladimir Sementsov-Ogievskiy    return f"""\
36aaaa20b6SVladimir Sementsov-Ogievskiy/*
37aaaa20b6SVladimir Sementsov-Ogievskiy * File is generated by scripts/block-coroutine-wrapper.py
38aaaa20b6SVladimir Sementsov-Ogievskiy *
39aaaa20b6SVladimir Sementsov-Ogievskiy{copyright}
40aaaa20b6SVladimir Sementsov-Ogievskiy */
41aaaa20b6SVladimir Sementsov-Ogievskiy
42aaaa20b6SVladimir Sementsov-Ogievskiy#include "qemu/osdep.h"
43aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/coroutines.h"
44aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/block-gen.h"
45e2c1c34fSMarkus Armbruster#include "block/block_int.h"
46e2c1c34fSMarkus Armbruster#include "block/dirty-bitmap.h"
47aaaa20b6SVladimir Sementsov-Ogievskiy"""
48aaaa20b6SVladimir Sementsov-Ogievskiy
49aaaa20b6SVladimir Sementsov-Ogievskiy
50aaaa20b6SVladimir Sementsov-Ogievskiyclass ParamDecl:
51aaaa20b6SVladimir Sementsov-Ogievskiy    param_re = re.compile(r'(?P<decl>'
52aaaa20b6SVladimir Sementsov-Ogievskiy                          r'(?P<type>.*[ *])'
53aaaa20b6SVladimir Sementsov-Ogievskiy                          r'(?P<name>[a-z][a-z0-9_]*)'
54aaaa20b6SVladimir Sementsov-Ogievskiy                          r')')
55aaaa20b6SVladimir Sementsov-Ogievskiy
56aaaa20b6SVladimir Sementsov-Ogievskiy    def __init__(self, param_decl: str) -> None:
57aaaa20b6SVladimir Sementsov-Ogievskiy        m = self.param_re.match(param_decl.strip())
58aaaa20b6SVladimir Sementsov-Ogievskiy        if m is None:
59aaaa20b6SVladimir Sementsov-Ogievskiy            raise ValueError(f'Wrong parameter declaration: "{param_decl}"')
60aaaa20b6SVladimir Sementsov-Ogievskiy        self.decl = m.group('decl')
61aaaa20b6SVladimir Sementsov-Ogievskiy        self.type = m.group('type')
62aaaa20b6SVladimir Sementsov-Ogievskiy        self.name = m.group('name')
63aaaa20b6SVladimir Sementsov-Ogievskiy
64aaaa20b6SVladimir Sementsov-Ogievskiy
65aaaa20b6SVladimir Sementsov-Ogievskiyclass FuncDecl:
66d6ee2e32SKevin Wolf    def __init__(self, wrapper_type: str, return_type: str, name: str,
67d6ee2e32SKevin Wolf                 args: str, variant: str) -> None:
68aaaa20b6SVladimir Sementsov-Ogievskiy        self.return_type = return_type.strip()
69aaaa20b6SVladimir Sementsov-Ogievskiy        self.name = name.strip()
7076a2f554SEmanuele Giuseppe Esposito        self.struct_name = snake_to_camel(self.name)
71aaaa20b6SVladimir Sementsov-Ogievskiy        self.args = [ParamDecl(arg.strip()) for arg in args.split(',')]
7276a2f554SEmanuele Giuseppe Esposito        self.create_only_co = 'mixed' not in variant
73e6d3f7a6SEmanuele Giuseppe Esposito        self.graph_rdlock = 'bdrv_rdlock' in variant
74de903298SKevin Wolf        self.graph_wrlock = 'bdrv_wrlock' in variant
7576a2f554SEmanuele Giuseppe Esposito
76d6ee2e32SKevin Wolf        self.wrapper_type = wrapper_type
77d6ee2e32SKevin Wolf
78d6ee2e32SKevin Wolf        if wrapper_type == 'co':
79de903298SKevin Wolf            if self.graph_wrlock:
80de903298SKevin Wolf                raise ValueError(f"co function can't be wrlock: {self.name}")
8176a2f554SEmanuele Giuseppe Esposito            subsystem, subname = self.name.split('_', 1)
82d6ee2e32SKevin Wolf            self.target_name = f'{subsystem}_co_{subname}'
83d6ee2e32SKevin Wolf        else:
84d6ee2e32SKevin Wolf            assert wrapper_type == 'no_co'
85d6ee2e32SKevin Wolf            subsystem, co_infix, subname = self.name.split('_', 2)
86d6ee2e32SKevin Wolf            if co_infix != 'co':
87d6ee2e32SKevin Wolf                raise ValueError(f"Invalid no_co function name: {self.name}")
88d6ee2e32SKevin Wolf            if not self.create_only_co:
89d6ee2e32SKevin Wolf                raise ValueError(f"no_co function can't be mixed: {self.name}")
90*e84c07bcSKevin Wolf            if self.graph_rdlock and self.graph_wrlock:
91*e84c07bcSKevin Wolf                raise ValueError("function can't be both rdlock and wrlock: "
92*e84c07bcSKevin Wolf                                 f"{self.name}")
93d6ee2e32SKevin Wolf            self.target_name = f'{subsystem}_{subname}'
9476a2f554SEmanuele Giuseppe Esposito
95dea97c1fSKevin Wolf        self.ctx = self.gen_ctx()
96aaaa20b6SVladimir Sementsov-Ogievskiy
975b317b8dSEmanuele Giuseppe Esposito        self.get_result = 's->ret = '
985b317b8dSEmanuele Giuseppe Esposito        self.ret = 'return s.ret;'
995b317b8dSEmanuele Giuseppe Esposito        self.co_ret = 'return '
1005b317b8dSEmanuele Giuseppe Esposito        self.return_field = self.return_type + " ret;"
1015b317b8dSEmanuele Giuseppe Esposito        if self.return_type == 'void':
1025b317b8dSEmanuele Giuseppe Esposito            self.get_result = ''
1035b317b8dSEmanuele Giuseppe Esposito            self.ret = ''
1045b317b8dSEmanuele Giuseppe Esposito            self.co_ret = ''
1055b317b8dSEmanuele Giuseppe Esposito            self.return_field = ''
1065b317b8dSEmanuele Giuseppe Esposito
107dea97c1fSKevin Wolf    def gen_ctx(self, prefix: str = '') -> str:
108dea97c1fSKevin Wolf        t = self.args[0].type
109d2184349SKevin Wolf        name = self.args[0].name
110dea97c1fSKevin Wolf        if t == 'BlockDriverState *':
111d2184349SKevin Wolf            return f'bdrv_get_aio_context({prefix}{name})'
112dea97c1fSKevin Wolf        elif t == 'BdrvChild *':
113d2184349SKevin Wolf            return f'bdrv_get_aio_context({prefix}{name}->bs)'
114dea97c1fSKevin Wolf        elif t == 'BlockBackend *':
115d2184349SKevin Wolf            return f'blk_get_aio_context({prefix}{name})'
116dea97c1fSKevin Wolf        else:
117dea97c1fSKevin Wolf            return 'qemu_get_aio_context()'
118dea97c1fSKevin Wolf
119aaaa20b6SVladimir Sementsov-Ogievskiy    def gen_list(self, format: str) -> str:
120aaaa20b6SVladimir Sementsov-Ogievskiy        return ', '.join(format.format_map(arg.__dict__) for arg in self.args)
121aaaa20b6SVladimir Sementsov-Ogievskiy
122aaaa20b6SVladimir Sementsov-Ogievskiy    def gen_block(self, format: str) -> str:
123aaaa20b6SVladimir Sementsov-Ogievskiy        return '\n'.join(format.format_map(arg.__dict__) for arg in self.args)
124aaaa20b6SVladimir Sementsov-Ogievskiy
125aaaa20b6SVladimir Sementsov-Ogievskiy
12676a2f554SEmanuele Giuseppe Esposito# Match wrappers declared with a co_wrapper mark
1276700dfb1SEmanuele Giuseppe Espositofunc_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)'
128d6ee2e32SKevin Wolf                          r'(\s*coroutine_fn)?'
129d6ee2e32SKevin Wolf                          r'\s*(?P<wrapper_type>(no_)?co)_wrapper'
13076a2f554SEmanuele Giuseppe Esposito                          r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*'
131aaaa20b6SVladimir Sementsov-Ogievskiy                          r'(?P<wrapper_name>[a-z][a-z0-9_]*)'
132aaaa20b6SVladimir Sementsov-Ogievskiy                          r'\((?P<args>[^)]*)\);$', re.MULTILINE)
133aaaa20b6SVladimir Sementsov-Ogievskiy
134aaaa20b6SVladimir Sementsov-Ogievskiy
135aaaa20b6SVladimir Sementsov-Ogievskiydef func_decl_iter(text: str) -> Iterator:
136aaaa20b6SVladimir Sementsov-Ogievskiy    for m in func_decl_re.finditer(text):
137d6ee2e32SKevin Wolf        yield FuncDecl(wrapper_type=m.group('wrapper_type'),
138d6ee2e32SKevin Wolf                       return_type=m.group('return_type'),
139aaaa20b6SVladimir Sementsov-Ogievskiy                       name=m.group('wrapper_name'),
14076a2f554SEmanuele Giuseppe Esposito                       args=m.group('args'),
14176a2f554SEmanuele Giuseppe Esposito                       variant=m.group('variant'))
142aaaa20b6SVladimir Sementsov-Ogievskiy
143aaaa20b6SVladimir Sementsov-Ogievskiy
144aaaa20b6SVladimir Sementsov-Ogievskiydef snake_to_camel(func_name: str) -> str:
145aaaa20b6SVladimir Sementsov-Ogievskiy    """
146aaaa20b6SVladimir Sementsov-Ogievskiy    Convert underscore names like 'some_function_name' to camel-case like
147aaaa20b6SVladimir Sementsov-Ogievskiy    'SomeFunctionName'
148aaaa20b6SVladimir Sementsov-Ogievskiy    """
149aaaa20b6SVladimir Sementsov-Ogievskiy    words = func_name.split('_')
150aaaa20b6SVladimir Sementsov-Ogievskiy    words = [w[0].upper() + w[1:] for w in words]
151aaaa20b6SVladimir Sementsov-Ogievskiy    return ''.join(words)
152aaaa20b6SVladimir Sementsov-Ogievskiy
153aaaa20b6SVladimir Sementsov-Ogievskiy
15476a2f554SEmanuele Giuseppe Espositodef create_mixed_wrapper(func: FuncDecl) -> str:
15576a2f554SEmanuele Giuseppe Esposito    """
15676a2f554SEmanuele Giuseppe Esposito    Checks if we are already in coroutine
15776a2f554SEmanuele Giuseppe Esposito    """
158d6ee2e32SKevin Wolf    name = func.target_name
15976a2f554SEmanuele Giuseppe Esposito    struct_name = func.struct_name
160e6d3f7a6SEmanuele Giuseppe Esposito    graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else ''
161e6d3f7a6SEmanuele Giuseppe Esposito
16276a2f554SEmanuele Giuseppe Esposito    return f"""\
1636700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') })
16476a2f554SEmanuele Giuseppe Esposito{{
16576a2f554SEmanuele Giuseppe Esposito    if (qemu_in_coroutine()) {{
166e6d3f7a6SEmanuele Giuseppe Esposito        {graph_assume_lock}
1675b317b8dSEmanuele Giuseppe Esposito        {func.co_ret}{name}({ func.gen_list('{name}') });
16876a2f554SEmanuele Giuseppe Esposito    }} else {{
16976a2f554SEmanuele Giuseppe Esposito        {struct_name} s = {{
1700582fb82SEmanuele Giuseppe Esposito            .poll_state.ctx = {func.ctx},
17176a2f554SEmanuele Giuseppe Esposito            .poll_state.in_progress = true,
17276a2f554SEmanuele Giuseppe Esposito
17376a2f554SEmanuele Giuseppe Esposito{ func.gen_block('            .{name} = {name},') }
17476a2f554SEmanuele Giuseppe Esposito        }};
17576a2f554SEmanuele Giuseppe Esposito
17676a2f554SEmanuele Giuseppe Esposito        s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
17776a2f554SEmanuele Giuseppe Esposito
1786700dfb1SEmanuele Giuseppe Esposito        bdrv_poll_co(&s.poll_state);
1795b317b8dSEmanuele Giuseppe Esposito        {func.ret}
18076a2f554SEmanuele Giuseppe Esposito    }}
18176a2f554SEmanuele Giuseppe Esposito}}"""
18276a2f554SEmanuele Giuseppe Esposito
18376a2f554SEmanuele Giuseppe Esposito
18476a2f554SEmanuele Giuseppe Espositodef create_co_wrapper(func: FuncDecl) -> str:
18576a2f554SEmanuele Giuseppe Esposito    """
18676a2f554SEmanuele Giuseppe Esposito    Assumes we are not in coroutine, and creates one
18776a2f554SEmanuele Giuseppe Esposito    """
188d6ee2e32SKevin Wolf    name = func.target_name
18976a2f554SEmanuele Giuseppe Esposito    struct_name = func.struct_name
19076a2f554SEmanuele Giuseppe Esposito    return f"""\
1916700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') })
19276a2f554SEmanuele Giuseppe Esposito{{
19376a2f554SEmanuele Giuseppe Esposito    {struct_name} s = {{
1940582fb82SEmanuele Giuseppe Esposito        .poll_state.ctx = {func.ctx},
19576a2f554SEmanuele Giuseppe Esposito        .poll_state.in_progress = true,
19676a2f554SEmanuele Giuseppe Esposito
19776a2f554SEmanuele Giuseppe Esposito{ func.gen_block('        .{name} = {name},') }
19876a2f554SEmanuele Giuseppe Esposito    }};
19976a2f554SEmanuele Giuseppe Esposito    assert(!qemu_in_coroutine());
20076a2f554SEmanuele Giuseppe Esposito
20176a2f554SEmanuele Giuseppe Esposito    s.poll_state.co = qemu_coroutine_create({name}_entry, &s);
20276a2f554SEmanuele Giuseppe Esposito
2036700dfb1SEmanuele Giuseppe Esposito    bdrv_poll_co(&s.poll_state);
2045b317b8dSEmanuele Giuseppe Esposito    {func.ret}
20576a2f554SEmanuele Giuseppe Esposito}}"""
20676a2f554SEmanuele Giuseppe Esposito
20776a2f554SEmanuele Giuseppe Esposito
208d6ee2e32SKevin Wolfdef gen_co_wrapper(func: FuncDecl) -> str:
209bb436948SVladimir Sementsov-Ogievskiy    assert not '_co_' in func.name
210d6ee2e32SKevin Wolf    assert func.wrapper_type == 'co'
211aaaa20b6SVladimir Sementsov-Ogievskiy
212d6ee2e32SKevin Wolf    name = func.target_name
21376a2f554SEmanuele Giuseppe Esposito    struct_name = func.struct_name
214bb436948SVladimir Sementsov-Ogievskiy
215e6d3f7a6SEmanuele Giuseppe Esposito    graph_lock=''
216e6d3f7a6SEmanuele Giuseppe Esposito    graph_unlock=''
217e6d3f7a6SEmanuele Giuseppe Esposito    if func.graph_rdlock:
218e6d3f7a6SEmanuele Giuseppe Esposito        graph_lock='    bdrv_graph_co_rdlock();'
219e6d3f7a6SEmanuele Giuseppe Esposito        graph_unlock='    bdrv_graph_co_rdunlock();'
220e6d3f7a6SEmanuele Giuseppe Esposito
22176a2f554SEmanuele Giuseppe Esposito    creation_function = create_mixed_wrapper
22276a2f554SEmanuele Giuseppe Esposito    if func.create_only_co:
22376a2f554SEmanuele Giuseppe Esposito        creation_function = create_co_wrapper
224aaaa20b6SVladimir Sementsov-Ogievskiy
225aaaa20b6SVladimir Sementsov-Ogievskiy    return f"""\
226aaaa20b6SVladimir Sementsov-Ogievskiy/*
227aaaa20b6SVladimir Sementsov-Ogievskiy * Wrappers for {name}
228aaaa20b6SVladimir Sementsov-Ogievskiy */
229aaaa20b6SVladimir Sementsov-Ogievskiy
230aaaa20b6SVladimir Sementsov-Ogievskiytypedef struct {struct_name} {{
231aaaa20b6SVladimir Sementsov-Ogievskiy    BdrvPollCo poll_state;
2325b317b8dSEmanuele Giuseppe Esposito    {func.return_field}
233aaaa20b6SVladimir Sementsov-Ogievskiy{ func.gen_block('    {decl};') }
234aaaa20b6SVladimir Sementsov-Ogievskiy}} {struct_name};
235aaaa20b6SVladimir Sementsov-Ogievskiy
236aaaa20b6SVladimir Sementsov-Ogievskiystatic void coroutine_fn {name}_entry(void *opaque)
237aaaa20b6SVladimir Sementsov-Ogievskiy{{
238aaaa20b6SVladimir Sementsov-Ogievskiy    {struct_name} *s = opaque;
239aaaa20b6SVladimir Sementsov-Ogievskiy
240e6d3f7a6SEmanuele Giuseppe Esposito{graph_lock}
2415b317b8dSEmanuele Giuseppe Esposito    {func.get_result}{name}({ func.gen_list('s->{name}') });
242e6d3f7a6SEmanuele Giuseppe Esposito{graph_unlock}
243aaaa20b6SVladimir Sementsov-Ogievskiy    s->poll_state.in_progress = false;
244aaaa20b6SVladimir Sementsov-Ogievskiy
245aaaa20b6SVladimir Sementsov-Ogievskiy    aio_wait_kick();
246aaaa20b6SVladimir Sementsov-Ogievskiy}}
247aaaa20b6SVladimir Sementsov-Ogievskiy
24876a2f554SEmanuele Giuseppe Esposito{creation_function(func)}"""
249aaaa20b6SVladimir Sementsov-Ogievskiy
250aaaa20b6SVladimir Sementsov-Ogievskiy
251d6ee2e32SKevin Wolfdef gen_no_co_wrapper(func: FuncDecl) -> str:
252d6ee2e32SKevin Wolf    assert '_co_' in func.name
253d6ee2e32SKevin Wolf    assert func.wrapper_type == 'no_co'
254d6ee2e32SKevin Wolf
255d6ee2e32SKevin Wolf    name = func.target_name
256d6ee2e32SKevin Wolf    struct_name = func.struct_name
257d6ee2e32SKevin Wolf
258de903298SKevin Wolf    graph_lock=''
259de903298SKevin Wolf    graph_unlock=''
260*e84c07bcSKevin Wolf    if func.graph_rdlock:
261*e84c07bcSKevin Wolf        graph_lock='    bdrv_graph_rdlock_main_loop();'
262*e84c07bcSKevin Wolf        graph_unlock='    bdrv_graph_rdunlock_main_loop();'
263*e84c07bcSKevin Wolf    elif func.graph_wrlock:
264de903298SKevin Wolf        graph_lock='    bdrv_graph_wrlock(NULL);'
265de903298SKevin Wolf        graph_unlock='    bdrv_graph_wrunlock();'
266de903298SKevin Wolf
267d6ee2e32SKevin Wolf    return f"""\
268d6ee2e32SKevin Wolf/*
269d6ee2e32SKevin Wolf * Wrappers for {name}
270d6ee2e32SKevin Wolf */
271d6ee2e32SKevin Wolf
272d6ee2e32SKevin Wolftypedef struct {struct_name} {{
273d6ee2e32SKevin Wolf    Coroutine *co;
274d6ee2e32SKevin Wolf    {func.return_field}
275d6ee2e32SKevin Wolf{ func.gen_block('    {decl};') }
276d6ee2e32SKevin Wolf}} {struct_name};
277d6ee2e32SKevin Wolf
278d6ee2e32SKevin Wolfstatic void {name}_bh(void *opaque)
279d6ee2e32SKevin Wolf{{
280d6ee2e32SKevin Wolf    {struct_name} *s = opaque;
281dea97c1fSKevin Wolf    AioContext *ctx = {func.gen_ctx('s->')};
282d6ee2e32SKevin Wolf
283de903298SKevin Wolf{graph_lock}
284dea97c1fSKevin Wolf    aio_context_acquire(ctx);
285d6ee2e32SKevin Wolf    {func.get_result}{name}({ func.gen_list('s->{name}') });
286dea97c1fSKevin Wolf    aio_context_release(ctx);
287de903298SKevin Wolf{graph_unlock}
288d6ee2e32SKevin Wolf
289d6ee2e32SKevin Wolf    aio_co_wake(s->co);
290d6ee2e32SKevin Wolf}}
291d6ee2e32SKevin Wolf
292d6ee2e32SKevin Wolf{func.return_type} coroutine_fn {func.name}({ func.gen_list('{decl}') })
293d6ee2e32SKevin Wolf{{
294d6ee2e32SKevin Wolf    {struct_name} s = {{
295d6ee2e32SKevin Wolf        .co = qemu_coroutine_self(),
296d6ee2e32SKevin Wolf{ func.gen_block('        .{name} = {name},') }
297d6ee2e32SKevin Wolf    }};
298d6ee2e32SKevin Wolf    assert(qemu_in_coroutine());
299d6ee2e32SKevin Wolf
300d6ee2e32SKevin Wolf    aio_bh_schedule_oneshot(qemu_get_aio_context(), {name}_bh, &s);
301d6ee2e32SKevin Wolf    qemu_coroutine_yield();
302d6ee2e32SKevin Wolf
303d6ee2e32SKevin Wolf    {func.ret}
304d6ee2e32SKevin Wolf}}"""
305d6ee2e32SKevin Wolf
306d6ee2e32SKevin Wolf
307aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrappers(input_code: str) -> str:
308aaaa20b6SVladimir Sementsov-Ogievskiy    res = ''
309aaaa20b6SVladimir Sementsov-Ogievskiy    for func in func_decl_iter(input_code):
310aaaa20b6SVladimir Sementsov-Ogievskiy        res += '\n\n\n'
311d6ee2e32SKevin Wolf        if func.wrapper_type == 'co':
312d6ee2e32SKevin Wolf            res += gen_co_wrapper(func)
313d6ee2e32SKevin Wolf        else:
314d6ee2e32SKevin Wolf            res += gen_no_co_wrapper(func)
315aaaa20b6SVladimir Sementsov-Ogievskiy
316aaaa20b6SVladimir Sementsov-Ogievskiy    return res
317aaaa20b6SVladimir Sementsov-Ogievskiy
318aaaa20b6SVladimir Sementsov-Ogievskiy
319aaaa20b6SVladimir Sementsov-Ogievskiyif __name__ == '__main__':
320aaaa20b6SVladimir Sementsov-Ogievskiy    if len(sys.argv) < 3:
321aaaa20b6SVladimir Sementsov-Ogievskiy        exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...')
322aaaa20b6SVladimir Sementsov-Ogievskiy
323aaaa20b6SVladimir Sementsov-Ogievskiy    with open(sys.argv[1], 'w', encoding='utf-8') as f_out:
324aaaa20b6SVladimir Sementsov-Ogievskiy        f_out.write(gen_header())
325aaaa20b6SVladimir Sementsov-Ogievskiy        for fname in sys.argv[2:]:
326aaaa20b6SVladimir Sementsov-Ogievskiy            with open(fname, encoding='utf-8') as f_in:
327aaaa20b6SVladimir Sementsov-Ogievskiy                f_out.write(gen_wrappers(f_in.read()))
328aaaa20b6SVladimir Sementsov-Ogievskiy                f_out.write('\n')
329