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" 45aaaa20b6SVladimir Sementsov-Ogievskiy#include "block/block_int.h"\ 46aaaa20b6SVladimir Sementsov-Ogievskiy""" 47aaaa20b6SVladimir Sementsov-Ogievskiy 48aaaa20b6SVladimir Sementsov-Ogievskiy 49aaaa20b6SVladimir Sementsov-Ogievskiyclass ParamDecl: 50aaaa20b6SVladimir Sementsov-Ogievskiy param_re = re.compile(r'(?P<decl>' 51aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<type>.*[ *])' 52aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<name>[a-z][a-z0-9_]*)' 53aaaa20b6SVladimir Sementsov-Ogievskiy r')') 54aaaa20b6SVladimir Sementsov-Ogievskiy 55aaaa20b6SVladimir Sementsov-Ogievskiy def __init__(self, param_decl: str) -> None: 56aaaa20b6SVladimir Sementsov-Ogievskiy m = self.param_re.match(param_decl.strip()) 57aaaa20b6SVladimir Sementsov-Ogievskiy if m is None: 58aaaa20b6SVladimir Sementsov-Ogievskiy raise ValueError(f'Wrong parameter declaration: "{param_decl}"') 59aaaa20b6SVladimir Sementsov-Ogievskiy self.decl = m.group('decl') 60aaaa20b6SVladimir Sementsov-Ogievskiy self.type = m.group('type') 61aaaa20b6SVladimir Sementsov-Ogievskiy self.name = m.group('name') 62aaaa20b6SVladimir Sementsov-Ogievskiy 63aaaa20b6SVladimir Sementsov-Ogievskiy 64aaaa20b6SVladimir Sementsov-Ogievskiyclass FuncDecl: 6576a2f554SEmanuele Giuseppe Esposito def __init__(self, return_type: str, name: str, args: str, 6676a2f554SEmanuele Giuseppe Esposito variant: str) -> None: 67aaaa20b6SVladimir Sementsov-Ogievskiy self.return_type = return_type.strip() 68aaaa20b6SVladimir Sementsov-Ogievskiy self.name = name.strip() 6976a2f554SEmanuele Giuseppe Esposito self.struct_name = snake_to_camel(self.name) 70aaaa20b6SVladimir Sementsov-Ogievskiy self.args = [ParamDecl(arg.strip()) for arg in args.split(',')] 7176a2f554SEmanuele Giuseppe Esposito self.create_only_co = 'mixed' not in variant 7276a2f554SEmanuele Giuseppe Esposito 7376a2f554SEmanuele Giuseppe Esposito subsystem, subname = self.name.split('_', 1) 7476a2f554SEmanuele Giuseppe Esposito self.co_name = f'{subsystem}_co_{subname}' 7576a2f554SEmanuele Giuseppe Esposito 7676a2f554SEmanuele Giuseppe Esposito t = self.args[0].type 7776a2f554SEmanuele Giuseppe Esposito if t == 'BlockDriverState *': 78*0582fb82SEmanuele Giuseppe Esposito ctx = 'bdrv_get_aio_context(bs)' 7976a2f554SEmanuele Giuseppe Esposito elif t == 'BdrvChild *': 80*0582fb82SEmanuele Giuseppe Esposito ctx = 'bdrv_get_aio_context(child->bs)' 81*0582fb82SEmanuele Giuseppe Esposito elif t == 'BlockBackend *': 82*0582fb82SEmanuele Giuseppe Esposito ctx = 'blk_get_aio_context(blk)' 8376a2f554SEmanuele Giuseppe Esposito else: 84*0582fb82SEmanuele Giuseppe Esposito ctx = 'qemu_get_aio_context()' 85*0582fb82SEmanuele Giuseppe Esposito self.ctx = ctx 86aaaa20b6SVladimir Sementsov-Ogievskiy 87aaaa20b6SVladimir Sementsov-Ogievskiy def gen_list(self, format: str) -> str: 88aaaa20b6SVladimir Sementsov-Ogievskiy return ', '.join(format.format_map(arg.__dict__) for arg in self.args) 89aaaa20b6SVladimir Sementsov-Ogievskiy 90aaaa20b6SVladimir Sementsov-Ogievskiy def gen_block(self, format: str) -> str: 91aaaa20b6SVladimir Sementsov-Ogievskiy return '\n'.join(format.format_map(arg.__dict__) for arg in self.args) 92aaaa20b6SVladimir Sementsov-Ogievskiy 93aaaa20b6SVladimir Sementsov-Ogievskiy 9476a2f554SEmanuele Giuseppe Esposito# Match wrappers declared with a co_wrapper mark 9576a2f554SEmanuele Giuseppe Espositofunc_decl_re = re.compile(r'^int\s*co_wrapper' 9676a2f554SEmanuele Giuseppe Esposito r'(?P<variant>(_[a-z][a-z0-9_]*)?)\s*' 97aaaa20b6SVladimir Sementsov-Ogievskiy r'(?P<wrapper_name>[a-z][a-z0-9_]*)' 98aaaa20b6SVladimir Sementsov-Ogievskiy r'\((?P<args>[^)]*)\);$', re.MULTILINE) 99aaaa20b6SVladimir Sementsov-Ogievskiy 100aaaa20b6SVladimir Sementsov-Ogievskiy 101aaaa20b6SVladimir Sementsov-Ogievskiydef func_decl_iter(text: str) -> Iterator: 102aaaa20b6SVladimir Sementsov-Ogievskiy for m in func_decl_re.finditer(text): 103aaaa20b6SVladimir Sementsov-Ogievskiy yield FuncDecl(return_type='int', 104aaaa20b6SVladimir Sementsov-Ogievskiy name=m.group('wrapper_name'), 10576a2f554SEmanuele Giuseppe Esposito args=m.group('args'), 10676a2f554SEmanuele Giuseppe Esposito variant=m.group('variant')) 107aaaa20b6SVladimir Sementsov-Ogievskiy 108aaaa20b6SVladimir Sementsov-Ogievskiy 109aaaa20b6SVladimir Sementsov-Ogievskiydef snake_to_camel(func_name: str) -> str: 110aaaa20b6SVladimir Sementsov-Ogievskiy """ 111aaaa20b6SVladimir Sementsov-Ogievskiy Convert underscore names like 'some_function_name' to camel-case like 112aaaa20b6SVladimir Sementsov-Ogievskiy 'SomeFunctionName' 113aaaa20b6SVladimir Sementsov-Ogievskiy """ 114aaaa20b6SVladimir Sementsov-Ogievskiy words = func_name.split('_') 115aaaa20b6SVladimir Sementsov-Ogievskiy words = [w[0].upper() + w[1:] for w in words] 116aaaa20b6SVladimir Sementsov-Ogievskiy return ''.join(words) 117aaaa20b6SVladimir Sementsov-Ogievskiy 118aaaa20b6SVladimir Sementsov-Ogievskiy 11976a2f554SEmanuele Giuseppe Espositodef create_mixed_wrapper(func: FuncDecl) -> str: 12076a2f554SEmanuele Giuseppe Esposito """ 12176a2f554SEmanuele Giuseppe Esposito Checks if we are already in coroutine 12276a2f554SEmanuele Giuseppe Esposito """ 12376a2f554SEmanuele Giuseppe Esposito name = func.co_name 12476a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 12576a2f554SEmanuele Giuseppe Esposito return f"""\ 12676a2f554SEmanuele Giuseppe Espositoint {func.name}({ func.gen_list('{decl}') }) 12776a2f554SEmanuele Giuseppe Esposito{{ 12876a2f554SEmanuele Giuseppe Esposito if (qemu_in_coroutine()) {{ 12976a2f554SEmanuele Giuseppe Esposito return {name}({ func.gen_list('{name}') }); 13076a2f554SEmanuele Giuseppe Esposito }} else {{ 13176a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 132*0582fb82SEmanuele Giuseppe Esposito .poll_state.ctx = {func.ctx}, 13376a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 13476a2f554SEmanuele Giuseppe Esposito 13576a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 13676a2f554SEmanuele Giuseppe Esposito }}; 13776a2f554SEmanuele Giuseppe Esposito 13876a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 13976a2f554SEmanuele Giuseppe Esposito 14076a2f554SEmanuele Giuseppe Esposito return bdrv_poll_co(&s.poll_state); 14176a2f554SEmanuele Giuseppe Esposito }} 14276a2f554SEmanuele Giuseppe Esposito}}""" 14376a2f554SEmanuele Giuseppe Esposito 14476a2f554SEmanuele Giuseppe Esposito 14576a2f554SEmanuele Giuseppe Espositodef create_co_wrapper(func: FuncDecl) -> str: 14676a2f554SEmanuele Giuseppe Esposito """ 14776a2f554SEmanuele Giuseppe Esposito Assumes we are not in coroutine, and creates one 14876a2f554SEmanuele Giuseppe Esposito """ 14976a2f554SEmanuele Giuseppe Esposito name = func.co_name 15076a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 15176a2f554SEmanuele Giuseppe Esposito return f"""\ 15276a2f554SEmanuele Giuseppe Espositoint {func.name}({ func.gen_list('{decl}') }) 15376a2f554SEmanuele Giuseppe Esposito{{ 15476a2f554SEmanuele Giuseppe Esposito {struct_name} s = {{ 155*0582fb82SEmanuele Giuseppe Esposito .poll_state.ctx = {func.ctx}, 15676a2f554SEmanuele Giuseppe Esposito .poll_state.in_progress = true, 15776a2f554SEmanuele Giuseppe Esposito 15876a2f554SEmanuele Giuseppe Esposito{ func.gen_block(' .{name} = {name},') } 15976a2f554SEmanuele Giuseppe Esposito }}; 16076a2f554SEmanuele Giuseppe Esposito assert(!qemu_in_coroutine()); 16176a2f554SEmanuele Giuseppe Esposito 16276a2f554SEmanuele Giuseppe Esposito s.poll_state.co = qemu_coroutine_create({name}_entry, &s); 16376a2f554SEmanuele Giuseppe Esposito 16476a2f554SEmanuele Giuseppe Esposito return bdrv_poll_co(&s.poll_state); 16576a2f554SEmanuele Giuseppe Esposito}}""" 16676a2f554SEmanuele Giuseppe Esposito 16776a2f554SEmanuele Giuseppe Esposito 168aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrapper(func: FuncDecl) -> str: 169bb436948SVladimir Sementsov-Ogievskiy assert not '_co_' in func.name 170aaaa20b6SVladimir Sementsov-Ogievskiy assert func.return_type == 'int' 171aaaa20b6SVladimir Sementsov-Ogievskiy 17276a2f554SEmanuele Giuseppe Esposito name = func.co_name 17376a2f554SEmanuele Giuseppe Esposito struct_name = func.struct_name 174bb436948SVladimir Sementsov-Ogievskiy 17576a2f554SEmanuele Giuseppe Esposito creation_function = create_mixed_wrapper 17676a2f554SEmanuele Giuseppe Esposito if func.create_only_co: 17776a2f554SEmanuele Giuseppe Esposito creation_function = create_co_wrapper 178aaaa20b6SVladimir Sementsov-Ogievskiy 179aaaa20b6SVladimir Sementsov-Ogievskiy return f"""\ 180aaaa20b6SVladimir Sementsov-Ogievskiy/* 181aaaa20b6SVladimir Sementsov-Ogievskiy * Wrappers for {name} 182aaaa20b6SVladimir Sementsov-Ogievskiy */ 183aaaa20b6SVladimir Sementsov-Ogievskiy 184aaaa20b6SVladimir Sementsov-Ogievskiytypedef struct {struct_name} {{ 185aaaa20b6SVladimir Sementsov-Ogievskiy BdrvPollCo poll_state; 186aaaa20b6SVladimir Sementsov-Ogievskiy{ func.gen_block(' {decl};') } 187aaaa20b6SVladimir Sementsov-Ogievskiy}} {struct_name}; 188aaaa20b6SVladimir Sementsov-Ogievskiy 189aaaa20b6SVladimir Sementsov-Ogievskiystatic void coroutine_fn {name}_entry(void *opaque) 190aaaa20b6SVladimir Sementsov-Ogievskiy{{ 191aaaa20b6SVladimir Sementsov-Ogievskiy {struct_name} *s = opaque; 192aaaa20b6SVladimir Sementsov-Ogievskiy 193aaaa20b6SVladimir Sementsov-Ogievskiy s->poll_state.ret = {name}({ func.gen_list('s->{name}') }); 194aaaa20b6SVladimir Sementsov-Ogievskiy s->poll_state.in_progress = false; 195aaaa20b6SVladimir Sementsov-Ogievskiy 196aaaa20b6SVladimir Sementsov-Ogievskiy aio_wait_kick(); 197aaaa20b6SVladimir Sementsov-Ogievskiy}} 198aaaa20b6SVladimir Sementsov-Ogievskiy 19976a2f554SEmanuele Giuseppe Esposito{creation_function(func)}""" 200aaaa20b6SVladimir Sementsov-Ogievskiy 201aaaa20b6SVladimir Sementsov-Ogievskiy 202aaaa20b6SVladimir Sementsov-Ogievskiydef gen_wrappers(input_code: str) -> str: 203aaaa20b6SVladimir Sementsov-Ogievskiy res = '' 204aaaa20b6SVladimir Sementsov-Ogievskiy for func in func_decl_iter(input_code): 205aaaa20b6SVladimir Sementsov-Ogievskiy res += '\n\n\n' 206aaaa20b6SVladimir Sementsov-Ogievskiy res += gen_wrapper(func) 207aaaa20b6SVladimir Sementsov-Ogievskiy 208aaaa20b6SVladimir Sementsov-Ogievskiy return res 209aaaa20b6SVladimir Sementsov-Ogievskiy 210aaaa20b6SVladimir Sementsov-Ogievskiy 211aaaa20b6SVladimir Sementsov-Ogievskiyif __name__ == '__main__': 212aaaa20b6SVladimir Sementsov-Ogievskiy if len(sys.argv) < 3: 213aaaa20b6SVladimir Sementsov-Ogievskiy exit(f'Usage: {sys.argv[0]} OUT_FILE.c IN_FILE.[ch]...') 214aaaa20b6SVladimir Sementsov-Ogievskiy 215aaaa20b6SVladimir Sementsov-Ogievskiy with open(sys.argv[1], 'w', encoding='utf-8') as f_out: 216aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_header()) 217aaaa20b6SVladimir Sementsov-Ogievskiy for fname in sys.argv[2:]: 218aaaa20b6SVladimir Sementsov-Ogievskiy with open(fname, encoding='utf-8') as f_in: 219aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write(gen_wrappers(f_in.read())) 220aaaa20b6SVladimir Sementsov-Ogievskiy f_out.write('\n') 221