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}") 90e84c07bcSKevin Wolf if self.graph_rdlock and self.graph_wrlock: 91e84c07bcSKevin Wolf raise ValueError("function can't be both rdlock and wrlock: " 92e84c07bcSKevin Wolf f"{self.name}") 93d6ee2e32SKevin Wolf self.target_name = f'{subsystem}_{subname}' 9476a2f554SEmanuele Giuseppe Esposito 955b317b8dSEmanuele Giuseppe Esposito self.get_result = 's->ret = ' 965b317b8dSEmanuele Giuseppe Esposito self.ret = 'return s.ret;' 975b317b8dSEmanuele Giuseppe Esposito self.co_ret = 'return ' 985b317b8dSEmanuele Giuseppe Esposito self.return_field = self.return_type + " ret;" 995b317b8dSEmanuele Giuseppe Esposito if self.return_type == 'void': 1005b317b8dSEmanuele Giuseppe Esposito self.get_result = '' 1015b317b8dSEmanuele Giuseppe Esposito self.ret = '' 1025b317b8dSEmanuele Giuseppe Esposito self.co_ret = '' 1035b317b8dSEmanuele Giuseppe Esposito self.return_field = '' 1045b317b8dSEmanuele Giuseppe Esposito 105dea97c1fSKevin Wolf def gen_ctx(self, prefix: str = '') -> str: 106dea97c1fSKevin Wolf t = self.args[0].type 107d2184349SKevin Wolf name = self.args[0].name 108dea97c1fSKevin Wolf if t == 'BlockDriverState *': 109d2184349SKevin Wolf return f'bdrv_get_aio_context({prefix}{name})' 110dea97c1fSKevin Wolf elif t == 'BdrvChild *': 111d2184349SKevin Wolf return f'bdrv_get_aio_context({prefix}{name}->bs)' 112dea97c1fSKevin Wolf elif t == 'BlockBackend *': 113d2184349SKevin Wolf return f'blk_get_aio_context({prefix}{name})' 114dea97c1fSKevin Wolf else: 115dea97c1fSKevin Wolf return 'qemu_get_aio_context()' 116dea97c1fSKevin Wolf 117aaaa20b6SVladimir Sementsov-Ogievskiy def gen_list(self, format: str) -> str: 118aaaa20b6SVladimir Sementsov-Ogievskiy return ', '.join(format.format_map(arg.__dict__) for arg in self.args) 119aaaa20b6SVladimir Sementsov-Ogievskiy 120aaaa20b6SVladimir Sementsov-Ogievskiy def gen_block(self, format: str) -> str: 121aaaa20b6SVladimir Sementsov-Ogievskiy return '\n'.join(format.format_map(arg.__dict__) for arg in self.args) 122aaaa20b6SVladimir Sementsov-Ogievskiy 123aaaa20b6SVladimir Sementsov-Ogievskiy 12476a2f554SEmanuele Giuseppe Esposito# Match wrappers declared with a co_wrapper mark 1256700dfb1SEmanuele Giuseppe Espositofunc_decl_re = re.compile(r'^(?P<return_type>[a-zA-Z][a-zA-Z0-9_]* [\*]?)' 126d6ee2e32SKevin Wolf r'(\s*coroutine_fn)?' 127d6ee2e32SKevin Wolf r'\s*(?P<wrapper_type>(no_)?co)_wrapper' 12876a2f554SEmanuele Giuseppe Esposito r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*' 129aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<wrapper_name>[a-z][a-z0-9_]*)' 130aaaa20b6SVladimir Sementsov-Ogievskiy r'\((?P<args>[^)]*)\);$', re.MULTILINE) 131aaaa20b6SVladimir Sementsov-Ogievskiy 132aaaa20b6SVladimir Sementsov-Ogievskiy 133aaaa20b6SVladimir Sementsov-Ogievskiydef func_decl_iter(text: str) -> Iterator: 134aaaa20b6SVladimir Sementsov-Ogievskiy for m in func_decl_re.finditer(text): 135d6ee2e32SKevin Wolf yield FuncDecl(wrapper_type=m.group('wrapper_type'), 136d6ee2e32SKevin Wolf return_type=m.group('return_type'), 137aaaa20b6SVladimir Sementsov-Ogievskiy name=m.group('wrapper_name'), 13876a2f554SEmanuele Giuseppe Esposito args=m.group('args'), 13976a2f554SEmanuele Giuseppe Esposito variant=m.group('variant')) 140aaaa20b6SVladimir Sementsov-Ogievskiy 141aaaa20b6SVladimir Sementsov-Ogievskiy 142aaaa20b6SVladimir Sementsov-Ogievskiydef snake_to_camel(func_name: str) -> str: 143aaaa20b6SVladimir Sementsov-Ogievskiy """ 144aaaa20b6SVladimir Sementsov-Ogievskiy Convert underscore names like 'some_function_name' to camel-case like 145aaaa20b6SVladimir Sementsov-Ogievskiy 'SomeFunctionName' 146aaaa20b6SVladimir Sementsov-Ogievskiy """ 147aaaa20b6SVladimir Sementsov-Ogievskiy words = func_name.split('_') 148aaaa20b6SVladimir Sementsov-Ogievskiy words = [w[0].upper() + w[1:] for w in words] 149aaaa20b6SVladimir Sementsov-Ogievskiy return ''.join(words) 150aaaa20b6SVladimir Sementsov-Ogievskiy 151aaaa20b6SVladimir Sementsov-Ogievskiy 15276a2f554SEmanuele Giuseppe Espositodef create_mixed_wrapper(func: FuncDecl) -> str: 15376a2f554SEmanuele Giuseppe Esposito """ 15476a2f554SEmanuele Giuseppe Esposito Checks if we are already in coroutine 15576a2f554SEmanuele Giuseppe Esposito """ 156d6ee2e32SKevin Wolf name = func.target_name 15776a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 158e6d3f7a6SEmanuele Giuseppe Esposito graph_assume_lock = 'assume_graph_lock();' if func.graph_rdlock else '' 159e6d3f7a6SEmanuele Giuseppe Esposito 16076a2f554SEmanuele Giuseppe Esposito return f"""\ 1616700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') }) 16276a2f554SEmanuele Giuseppe Esposito{{ 16376a2f554SEmanuele Giuseppe Esposito if (qemu_in_coroutine()) {{ 164e6d3f7a6SEmanuele Giuseppe Esposito {graph_assume_lock} 1655b317b8dSEmanuele Giuseppe Esposito {func.co_ret}{name}({ func.gen_list('{name}') }); 16676a2f554SEmanuele Giuseppe Esposito }} else {{ 16776a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 168*c12887e1SStefan Hajnoczi .poll_state.ctx = qemu_get_current_aio_context(), 16976a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 17076a2f554SEmanuele Giuseppe Esposito 17176a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 17276a2f554SEmanuele Giuseppe Esposito }}; 17376a2f554SEmanuele Giuseppe Esposito 17476a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 17576a2f554SEmanuele Giuseppe Esposito 1766700dfb1SEmanuele Giuseppe Esposito bdrv_poll_co(&s.poll_state); 1775b317b8dSEmanuele Giuseppe Esposito {func.ret} 17876a2f554SEmanuele Giuseppe Esposito }} 17976a2f554SEmanuele Giuseppe Esposito}}""" 18076a2f554SEmanuele Giuseppe Esposito 18176a2f554SEmanuele Giuseppe Esposito 18276a2f554SEmanuele Giuseppe Espositodef create_co_wrapper(func: FuncDecl) -> str: 18376a2f554SEmanuele Giuseppe Esposito """ 18476a2f554SEmanuele Giuseppe Esposito Assumes we are not in coroutine, and creates one 18576a2f554SEmanuele Giuseppe Esposito """ 186d6ee2e32SKevin Wolf name = func.target_name 18776a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 18876a2f554SEmanuele Giuseppe Esposito return f"""\ 1896700dfb1SEmanuele Giuseppe Esposito{func.return_type} {func.name}({ func.gen_list('{decl}') }) 19076a2f554SEmanuele Giuseppe Esposito{{ 19176a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 192*c12887e1SStefan Hajnoczi .poll_state.ctx = qemu_get_current_aio_context(), 19376a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 19476a2f554SEmanuele Giuseppe Esposito 19576a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 19676a2f554SEmanuele Giuseppe Esposito }}; 19776a2f554SEmanuele Giuseppe Esposito assert(!qemu_in_coroutine()); 19876a2f554SEmanuele Giuseppe Esposito 19976a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 20076a2f554SEmanuele Giuseppe Esposito 2016700dfb1SEmanuele Giuseppe Esposito bdrv_poll_co(&s.poll_state); 2025b317b8dSEmanuele Giuseppe Esposito {func.ret} 20376a2f554SEmanuele Giuseppe Esposito}}""" 20476a2f554SEmanuele Giuseppe Esposito 20576a2f554SEmanuele Giuseppe Esposito 206d6ee2e32SKevin Wolfdef gen_co_wrapper(func: FuncDecl) -> str: 207bb436948SVladimir Sementsov-Ogievskiy assert not '_co_' in func.name 208d6ee2e32SKevin Wolf assert func.wrapper_type == 'co' 209aaaa20b6SVladimir Sementsov-Ogievskiy 210d6ee2e32SKevin Wolf name = func.target_name 21176a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 212bb436948SVladimir Sementsov-Ogievskiy 213e6d3f7a6SEmanuele Giuseppe Esposito graph_lock='' 214e6d3f7a6SEmanuele Giuseppe Esposito graph_unlock='' 215e6d3f7a6SEmanuele Giuseppe Esposito if func.graph_rdlock: 216e6d3f7a6SEmanuele Giuseppe Esposito graph_lock=' bdrv_graph_co_rdlock();' 217e6d3f7a6SEmanuele Giuseppe Esposito graph_unlock=' bdrv_graph_co_rdunlock();' 218e6d3f7a6SEmanuele Giuseppe Esposito 21976a2f554SEmanuele Giuseppe Esposito creation_function = create_mixed_wrapper 22076a2f554SEmanuele Giuseppe Esposito if func.create_only_co: 22176a2f554SEmanuele Giuseppe Esposito creation_function = create_co_wrapper 222aaaa20b6SVladimir Sementsov-Ogievskiy 223aaaa20b6SVladimir Sementsov-Ogievskiy return f"""\ 224aaaa20b6SVladimir Sementsov-Ogievskiy/* 225aaaa20b6SVladimir Sementsov-Ogievskiy * Wrappers for {name} 226aaaa20b6SVladimir Sementsov-Ogievskiy */ 227aaaa20b6SVladimir Sementsov-Ogievskiy 228aaaa20b6SVladimir Sementsov-Ogievskiytypedef struct {struct_name} {{ 229aaaa20b6SVladimir Sementsov-Ogievskiy BdrvPollCo poll_state; 2305b317b8dSEmanuele Giuseppe Esposito {func.return_field} 231aaaa20b6SVladimir Sementsov-Ogievskiy{ func.gen_block(' {decl};') } 232aaaa20b6SVladimir Sementsov-Ogievskiy}} {struct_name}; 233aaaa20b6SVladimir Sementsov-Ogievskiy 234aaaa20b6SVladimir Sementsov-Ogievskiystatic void coroutine_fn {name}_entry(void *opaque) 235aaaa20b6SVladimir Sementsov-Ogievskiy{{ 236aaaa20b6SVladimir Sementsov-Ogievskiy {struct_name} *s = opaque; 237aaaa20b6SVladimir Sementsov-Ogievskiy 238e6d3f7a6SEmanuele Giuseppe Esposito{graph_lock} 2395b317b8dSEmanuele Giuseppe Esposito {func.get_result}{name}({ func.gen_list('s->{name}') }); 240e6d3f7a6SEmanuele Giuseppe Esposito{graph_unlock} 241aaaa20b6SVladimir Sementsov-Ogievskiy s->poll_state.in_progress = false; 242aaaa20b6SVladimir Sementsov-Ogievskiy 243aaaa20b6SVladimir Sementsov-Ogievskiy aio_wait_kick(); 244aaaa20b6SVladimir Sementsov-Ogievskiy}} 245aaaa20b6SVladimir Sementsov-Ogievskiy 24676a2f554SEmanuele Giuseppe Esposito{creation_function(func)}""" 247aaaa20b6SVladimir Sementsov-Ogievskiy 248aaaa20b6SVladimir Sementsov-Ogievskiy 249d6ee2e32SKevin Wolfdef gen_no_co_wrapper(func: FuncDecl) -> str: 250d6ee2e32SKevin Wolf assert '_co_' in func.name 251d6ee2e32SKevin Wolf assert func.wrapper_type == 'no_co' 252d6ee2e32SKevin Wolf 253d6ee2e32SKevin Wolf name = func.target_name 254d6ee2e32SKevin Wolf struct_name = func.struct_name 255d6ee2e32SKevin Wolf 256de903298SKevin Wolf graph_lock='' 257de903298SKevin Wolf graph_unlock='' 258e84c07bcSKevin Wolf if func.graph_rdlock: 259e84c07bcSKevin Wolf graph_lock=' bdrv_graph_rdlock_main_loop();' 260e84c07bcSKevin Wolf graph_unlock=' bdrv_graph_rdunlock_main_loop();' 261e84c07bcSKevin Wolf elif func.graph_wrlock: 2626bc30f19SStefan Hajnoczi graph_lock=' bdrv_graph_wrlock();' 2636bc30f19SStefan Hajnoczi graph_unlock=' bdrv_graph_wrunlock();' 264de903298SKevin Wolf 265d6ee2e32SKevin Wolf return f"""\ 266d6ee2e32SKevin Wolf/* 267d6ee2e32SKevin Wolf * Wrappers for {name} 268d6ee2e32SKevin Wolf */ 269d6ee2e32SKevin Wolf 270d6ee2e32SKevin Wolftypedef struct {struct_name} {{ 271d6ee2e32SKevin Wolf Coroutine *co; 272d6ee2e32SKevin Wolf {func.return_field} 273d6ee2e32SKevin Wolf{ func.gen_block(' {decl};') } 274d6ee2e32SKevin Wolf}} {struct_name}; 275d6ee2e32SKevin Wolf 276d6ee2e32SKevin Wolfstatic void {name}_bh(void *opaque) 277d6ee2e32SKevin Wolf{{ 278d6ee2e32SKevin Wolf {struct_name} *s = opaque; 279d6ee2e32SKevin Wolf 280de903298SKevin Wolf{graph_lock} 281d6ee2e32SKevin Wolf {func.get_result}{name}({ func.gen_list('s->{name}') }); 282de903298SKevin Wolf{graph_unlock} 283d6ee2e32SKevin Wolf 284d6ee2e32SKevin Wolf aio_co_wake(s->co); 285d6ee2e32SKevin Wolf}} 286d6ee2e32SKevin Wolf 287d6ee2e32SKevin Wolf{func.return_type} coroutine_fn {func.name}({ func.gen_list('{decl}') }) 288d6ee2e32SKevin Wolf{{ 289d6ee2e32SKevin Wolf {struct_name} s = {{ 290d6ee2e32SKevin Wolf .co = qemu_coroutine_self(), 291d6ee2e32SKevin Wolf{ func.gen_block(' .{name} = {name},') } 292d6ee2e32SKevin Wolf }}; 293d6ee2e32SKevin Wolf assert(qemu_in_coroutine()); 294d6ee2e32SKevin Wolf 295d6ee2e32SKevin Wolf aio_bh_schedule_oneshot(qemu_get_aio_context(), {name}_bh, &s); 296d6ee2e32SKevin Wolf qemu_coroutine_yield(); 297d6ee2e32SKevin Wolf 298d6ee2e32SKevin Wolf {func.ret} 299d6ee2e32SKevin Wolf}}""" 300d6ee2e32SKevin Wolf 301d6ee2e32SKevin Wolf 302aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrappers(input_code: str) -> str: 303aaaa20b6SVladimir Sementsov-Ogievskiy res = '' 304aaaa20b6SVladimir Sementsov-Ogievskiy for func in func_decl_iter(input_code): 305aaaa20b6SVladimir Sementsov-Ogievskiy res += '\n\n\n' 306d6ee2e32SKevin Wolf if func.wrapper_type == 'co': 307d6ee2e32SKevin Wolf res += gen_co_wrapper(func) 308d6ee2e32SKevin Wolf else: 309d6ee2e32SKevin Wolf res += gen_no_co_wrapper(func) 310aaaa20b6SVladimir Sementsov-Ogievskiy 311aaaa20b6SVladimir Sementsov-Ogievskiy return res 312aaaa20b6SVladimir Sementsov-Ogievskiy 313aaaa20b6SVladimir Sementsov-Ogievskiy 314aaaa20b6SVladimir Sementsov-Ogievskiyif __name__ == '__main__': 315aaaa20b6SVladimir Sementsov-Ogievskiy if len(sys.argv) < 3: 316aaaa20b6SVladimir Sementsov-Ogievskiy exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') 317aaaa20b6SVladimir Sementsov-Ogievskiy 318aaaa20b6SVladimir Sementsov-Ogievskiy with open(sys.argv[1], 'w', encoding='utf-8') as f_out: 319aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_header()) 320aaaa20b6SVladimir Sementsov-Ogievskiy for fname in sys.argv[2:]: 321aaaa20b6SVladimir Sementsov-Ogievskiy with open(fname, encoding='utf-8') as f_in: 322aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_wrappers(f_in.read())) 323aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write('\n') 324