def module_internal_static_name(self, module_name: str, emitter: Emitter) -> str: return emitter.static_name(module_name + '_internal', None, prefix=MODULE_PREFIX)
def generate_dealloc_for_class(cl: ClassIR, dealloc_func_name: str, clear_func_name: str, emitter: Emitter) -> None: emitter.emit_line('static void') emitter.emit_line('{}({} *self)'.format(dealloc_func_name, cl.struct_name(emitter.names))) emitter.emit_line('{') emitter.emit_line('PyObject_GC_UnTrack(self);') # The trashcan is needed to handle deep recursive deallocations emitter.emit_line('CPy_TRASHCAN_BEGIN(self, {})'.format(dealloc_func_name)) emitter.emit_line('{}(self);'.format(clear_func_name)) emitter.emit_line('Py_TYPE(self)->tp_free((PyObject *)self);') emitter.emit_line('CPy_TRASHCAN_END(self)') emitter.emit_line('}')
def generate_getseter_declarations(cl: ClassIR, emitter: Emitter) -> None: if not cl.is_trait: for attr in cl.attributes: emitter.emit_line('static PyObject *') emitter.emit_line('{}({} *self, void *closure);'.format( getter_name(cl, attr, emitter.names), cl.struct_name(emitter.names))) emitter.emit_line('static int') emitter.emit_line('{}({} *self, PyObject *value, void *closure);'.format( setter_name(cl, attr, emitter.names), cl.struct_name(emitter.names))) for prop in cl.properties: # Generate getter declaration emitter.emit_line('static PyObject *') emitter.emit_line('{}({} *self, void *closure);'.format( getter_name(cl, prop, emitter.names), cl.struct_name(emitter.names))) # Generate property setter declaration if a setter exists if cl.properties[prop][1]: emitter.emit_line('static int') emitter.emit_line('{}({} *self, PyObject *value, void *closure);'.format( setter_name(cl, prop, emitter.names), cl.struct_name(emitter.names)))
def generate_new_for_class(cl: ClassIR, func_name: str, vtable_name: str, setup_name: str, emitter: Emitter) -> None: emitter.emit_line('static PyObject *') emitter.emit_line( '{}(PyTypeObject *type, PyObject *args, PyObject *kwds)'.format(func_name)) emitter.emit_line('{') # TODO: Check and unbox arguments if not cl.allow_interpreted_subclasses: emitter.emit_line('if (type != {}) {{'.format(emitter.type_struct_name(cl))) emitter.emit_line( 'PyErr_SetString(PyExc_TypeError, "interpreted classes cannot inherit from compiled");' ) emitter.emit_line('return NULL;') emitter.emit_line('}') emitter.emit_line('return {}(type);'.format(setup_name)) emitter.emit_line('}')
def generate_traverse_for_class(cl: ClassIR, func_name: str, emitter: Emitter) -> None: """Emit function that performs cycle GC traversal of an instance.""" emitter.emit_line('static int') emitter.emit_line('{}({} *self, visitproc visit, void *arg)'.format( func_name, cl.struct_name(emitter.names))) emitter.emit_line('{') for base in reversed(cl.base_mro): for attr, rtype in base.attributes.items(): emitter.emit_gc_visit('self->{}'.format(emitter.attr(attr)), rtype) if cl.has_dict: struct_name = cl.struct_name(emitter.names) # __dict__ lives right after the struct and __weakref__ lives right after that emitter.emit_gc_visit('*((PyObject **)((char *)self + sizeof({})))'.format( struct_name), object_rprimitive) emitter.emit_gc_visit( '*((PyObject **)((char *)self + sizeof(PyObject *) + sizeof({})))'.format( struct_name), object_rprimitive) emitter.emit_line('return 0;') emitter.emit_line('}')
def generate_offset_table(trait_offset_table_name: str, emitter: Emitter, trait: ClassIR, cl: ClassIR) -> None: """Generate attribute offset row of a trait vtable.""" emitter.emit_line('size_t {}_scratch[] = {{'.format(trait_offset_table_name)) for attr in trait.attributes: emitter.emit_line('offsetof({}, {}),'.format( cl.struct_name(emitter.names), emitter.attr(attr) )) if not trait.attributes: # This is for msvc. emitter.emit_line('0') emitter.emit_line('};') emitter.emit_line('memcpy({name}, {name}_scratch, sizeof({name}));'.format( name=trait_offset_table_name) )
def generate_setup_for_class(cl: ClassIR, func_name: str, defaults_fn: Optional[FuncIR], vtable_name: str, shadow_vtable_name: Optional[str], emitter: Emitter) -> None: """Generate a native function that allocates an instance of a class.""" emitter.emit_line('static PyObject *') emitter.emit_line('{}(PyTypeObject *type)'.format(func_name)) emitter.emit_line('{') emitter.emit_line('{} *self;'.format(cl.struct_name(emitter.names))) emitter.emit_line('self = ({struct} *)type->tp_alloc(type, 0);'.format( struct=cl.struct_name(emitter.names))) emitter.emit_line('if (self == NULL)') emitter.emit_line(' return NULL;') if shadow_vtable_name: emitter.emit_line('if (type != {}) {{'.format(emitter.type_struct_name(cl))) emitter.emit_line('self->vtable = {};'.format(shadow_vtable_name)) emitter.emit_line('} else {') emitter.emit_line('self->vtable = {};'.format(vtable_name)) emitter.emit_line('}') else: emitter.emit_line('self->vtable = {};'.format(vtable_name)) for base in reversed(cl.base_mro): for attr, rtype in base.attributes.items(): emitter.emit_line('self->{} = {};'.format( emitter.attr(attr), emitter.c_undefined_value(rtype))) # Initialize attributes to default values, if necessary if defaults_fn is not None: emitter.emit_lines( 'if ({}{}((PyObject *)self) == 0) {{'.format( NATIVE_PREFIX, defaults_fn.cname(emitter.names)), 'Py_DECREF(self);', 'return NULL;', '}') emitter.emit_line('return (PyObject *)self;') emitter.emit_line('}')
def generate_module_def(self, emitter: Emitter, module_name: str, module: ModuleIR) -> None: """Emit the PyModuleDef struct for a module and the module init function.""" # Emit module methods module_prefix = emitter.names.private_name(module_name) emitter.emit_line( 'static PyMethodDef {}module_methods[] = {{'.format(module_prefix)) for fn in module.functions: if fn.class_name is not None or fn.name == TOP_LEVEL_NAME: continue emitter.emit_line(( '{{"{name}", (PyCFunction){prefix}{cname}, METH_VARARGS | METH_KEYWORDS, ' 'NULL /* docstring */}},').format(name=fn.name, cname=fn.cname( emitter.names), prefix=PREFIX)) emitter.emit_line('{NULL, NULL, 0, NULL}') emitter.emit_line('};') emitter.emit_line() # Emit module definition struct emitter.emit_lines( 'static struct PyModuleDef {}module = {{'.format(module_prefix), 'PyModuleDef_HEAD_INIT,', '"{}",'.format(module_name), 'NULL, /* docstring */', '-1, /* size of per-interpreter state of the module,', ' or -1 if the module keeps state in global variables. */', '{}module_methods'.format(module_prefix), '};') emitter.emit_line() # Emit module init function. If we are compiling just one module, this # will be the C API init function. If we are compiling 2+ modules, we # generate a shared library for the modules and shims that call into # the shared library, and in this case we use an internal module # initialized function that will be called by the shim. if not self.use_shared_lib: declaration = 'PyMODINIT_FUNC PyInit_{}(void)'.format(module_name) else: declaration = 'PyObject *CPyInit_{}(void)'.format( exported_name(module_name)) emitter.emit_lines(declaration, '{') # Store the module reference in a static and return it when necessary. # This is separate from the *global* reference to the module that will # be populated when it is imported by a compiled module. We want that # reference to only be populated when the module has been successfully # imported, whereas this we want to have to stop a circular import. module_static = self.module_internal_static_name(module_name, emitter) emitter.emit_lines('if ({}) {{'.format(module_static), 'Py_INCREF({});'.format(module_static), 'return {};'.format(module_static), '}') emitter.emit_lines( '{} = PyModule_Create(&{}module);'.format(module_static, module_prefix), 'if (unlikely({} == NULL))'.format(module_static), ' return NULL;') emitter.emit_line( 'PyObject *modname = PyObject_GetAttrString((PyObject *){}, "__name__");' .format(module_static)) module_globals = emitter.static_name('globals', module_name) emitter.emit_lines( '{} = PyModule_GetDict({});'.format(module_globals, module_static), 'if (unlikely({} == NULL))'.format(module_globals), ' return NULL;') # HACK: Manually instantiate generated classes here for cl in module.classes: if cl.is_generated: type_struct = emitter.type_struct_name(cl) emitter.emit_lines( '{t} = (PyTypeObject *)CPyType_FromTemplate({t}_template, NULL, modname);' .format(t=type_struct)) emitter.emit_lines('if (unlikely(!{}))'.format(type_struct), ' return NULL;') emitter.emit_lines('if (CPyGlobalsInit() < 0)', ' return NULL;') self.generate_top_level_call(module, emitter) emitter.emit_lines('Py_DECREF(modname);') emitter.emit_line('return {};'.format(module_static)) emitter.emit_line('}')
def generate_init_for_class(cl: ClassIR, init_fn: FuncIR, emitter: Emitter) -> str: """Generate an init function suitable for use as tp_init. tp_init needs to be a function that returns an int, and our __init__ methods return a PyObject. Translate NULL to -1, everything else to 0. """ func_name = f'{cl.name_prefix(emitter.names)}_init' emitter.emit_line('static int') emitter.emit_line( f'{func_name}(PyObject *self, PyObject *args, PyObject *kwds)') emitter.emit_line('{') if cl.allow_interpreted_subclasses or cl.builtin_base: emitter.emit_line('return {}{}(self, args, kwds) != NULL ? 0 : -1;'.format( PREFIX, init_fn.cname(emitter.names))) else: emitter.emit_line('return 0;') emitter.emit_line('}') return func_name
def generate_shared_lib_init(self, emitter: Emitter) -> None: """Generate the init function for a shared library. A shared library contains all of the actual code for a compilation group. The init function is responsible for creating Capsules that wrap pointers to the initialization function of all the real init functions for modules in this shared library as well as the export table containing all of the exported functions and values from all the modules. These capsules are stored in attributes of the shared library. """ assert self.group_name is not None emitter.emit_line() emitter.emit_lines( 'PyMODINIT_FUNC PyInit_{}(void)'.format( shared_lib_name(self.group_name).split('.')[-1]), '{', ('static PyModuleDef def = {{ PyModuleDef_HEAD_INIT, "{}", NULL, -1, NULL, NULL }};' .format(shared_lib_name(self.group_name))), 'int res;', 'PyObject *capsule;', 'PyObject *tmp;', 'static PyObject *module;', 'if (module) {', 'Py_INCREF(module);', 'return module;', '}', 'module = PyModule_Create(&def);', 'if (!module) {', 'goto fail;', '}', '', ) emitter.emit_lines( 'capsule = PyCapsule_New(&exports, "{}.exports", NULL);'.format( shared_lib_name(self.group_name)), 'if (!capsule) {', 'goto fail;', '}', 'res = PyObject_SetAttrString(module, "exports", capsule);', 'Py_DECREF(capsule);', 'if (res < 0) {', 'goto fail;', '}', '', ) for mod, _ in self.modules: name = exported_name(mod) emitter.emit_lines( 'extern PyObject *CPyInit_{}(void);'.format(name), 'capsule = PyCapsule_New((void *)CPyInit_{}, "{}.init_{}", NULL);' .format(name, shared_lib_name(self.group_name), name), 'if (!capsule) {', 'goto fail;', '}', 'res = PyObject_SetAttrString(module, "init_{}", capsule);'. format(name), 'Py_DECREF(capsule);', 'if (res < 0) {', 'goto fail;', '}', '', ) for group in sorted(self.context.group_deps): egroup = exported_name(group) emitter.emit_lines( 'tmp = PyImport_ImportModule("{}"); if (!tmp) goto fail; Py_DECREF(tmp);' .format(shared_lib_name(group)), 'struct export_table_{} *pexports_{} = PyCapsule_Import("{}.exports", 0);' .format(egroup, egroup, shared_lib_name(group)), 'if (!pexports_{}) {{'.format(egroup), 'goto fail;', '}', 'memcpy(&exports_{group}, pexports_{group}, sizeof(exports_{group}));' .format(group=egroup), '', ) emitter.emit_lines( 'return module;', 'fail:', 'Py_XDECREF(module);', 'return NULL;', '}', )
def generate_globals_init(self, emitter: Emitter) -> None: emitter.emit_lines('', 'int CPyGlobalsInit(void)', '{', 'static int is_initialized = 0;', 'if (is_initialized) return 0;', '') emitter.emit_line('CPy_Init();') for symbol, fixup in self.simple_inits: emitter.emit_line('{} = {};'.format(symbol, fixup)) for (_, literal), identifier in self.literals.items(): symbol = emitter.static_name(identifier, None) if isinstance(literal, int): actual_symbol = symbol symbol = INT_PREFIX + symbol emitter.emit_line( 'PyObject * {} = PyLong_FromString(\"{}\", NULL, 10);'. format(symbol, str(literal))) elif isinstance(literal, float): emitter.emit_line('{} = PyFloat_FromDouble({});'.format( symbol, str(literal))) elif isinstance(literal, complex): emitter.emit_line('{} = PyComplex_FromDoubles({}, {});'.format( symbol, str(literal.real), str(literal.imag))) elif isinstance(literal, str): emitter.emit_line( '{} = PyUnicode_FromStringAndSize({}, {});'.format( symbol, *encode_as_c_string(literal))) elif isinstance(literal, bytes): emitter.emit_line( '{} = PyBytes_FromStringAndSize({}, {});'.format( symbol, *encode_bytes_as_c_string(literal))) else: assert False, ( 'Literals must be integers, floating point numbers, or strings,', 'but the provided literal is of type {}'.format( type(literal))) emitter.emit_lines('if (unlikely({} == NULL))'.format(symbol), ' return -1;') # Ints have an unboxed representation. if isinstance(literal, int): emitter.emit_line('{} = CPyTagged_FromObject({});'.format( actual_symbol, symbol)) emitter.emit_lines( 'is_initialized = 1;', 'return 0;', '}', )
def generate_export_table(self, decl_emitter: Emitter, code_emitter: Emitter) -> None: """Generate the declaration and definition of the group's export struct. To avoid needing to deal with deeply platform specific issues involving dynamic library linking (and some possibly insurmountable issues involving cyclic dependencies), compiled code accesses functions and data in other compilation groups via an explicit "export struct". Each group declares a struct type that contains a pointer to every function and static variable it exports. It then populates this struct and stores a pointer to it in a capsule stored as an attribute named 'exports' on the group's shared library's python module. On load, a group's init function will import all of its dependencies' exports tables using the capsule mechanism and copy the contents into a local copy of the table (to eliminate the need for a pointer indirection when accessing it). Then, all calls to functions in another group and accesses to statics from another group are done indirectly via the export table. For example, a group containing a module b, where b contains a class B and a function bar, would declare an export table like: struct export_table_b { PyTypeObject **CPyType_B; PyObject *(*CPyDef_B)(CPyTagged cpy_r_x); CPyTagged (*CPyDef_B___foo)(PyObject *cpy_r_self, CPyTagged cpy_r_y); tuple_T2OI (*CPyDef_bar)(PyObject *cpy_r_x); char (*CPyDef___top_level__)(void); }; that would be initialized with: static struct export_table_b exports = { &CPyType_B, &CPyDef_B, &CPyDef_B___foo, &CPyDef_bar, &CPyDef___top_level__, }; To call `b.foo`, then, a function in another group would do `exports_b.CPyDef_bar(...)`. """ decls = decl_emitter.context.declarations decl_emitter.emit_lines( '', 'struct export_table{} {{'.format(self.group_suffix), ) for name, decl in decls.items(): if decl.needs_export: decl_emitter.emit_line(pointerize('\n'.join(decl.decl), name)) decl_emitter.emit_line('};') code_emitter.emit_lines( '', 'static struct export_table{} exports = {{'.format( self.group_suffix), ) for name, decl in decls.items(): if decl.needs_export: code_emitter.emit_line('&{},'.format(name)) code_emitter.emit_line('};')
def generate_c_for_modules(self) -> List[Tuple[str, str]]: file_contents = [] multi_file = self.use_shared_lib and self.multi_file base_emitter = Emitter(self.context) # Optionally just include the runtime library c files to # reduce the number of compiler invocations needed if self.compiler_options.include_runtime_files: base_emitter.emit_line('#include "CPy.c"') base_emitter.emit_line('#include "getargs.c"') base_emitter.emit_line('#include "__native{}.h"'.format( self.short_group_suffix)) base_emitter.emit_line('#include "__native_internal{}.h"'.format( self.short_group_suffix)) emitter = base_emitter for (_, literal), identifier in self.literals.items(): if isinstance(literal, int): symbol = emitter.static_name(identifier, None) self.declare_global('CPyTagged ', symbol) else: self.declare_static_pyobject(identifier, emitter) for module_name, module in self.modules: if multi_file: emitter = Emitter(self.context) emitter.emit_line('#include "__native{}.h"'.format( self.short_group_suffix)) emitter.emit_line('#include "__native_internal{}.h"'.format( self.short_group_suffix)) self.declare_module(module_name, emitter) self.declare_internal_globals(module_name, emitter) self.declare_imports(module.imports, emitter) for cl in module.classes: if cl.is_ext_class: generate_class(cl, module_name, emitter) # Generate Python extension module definitions and module initialization functions. self.generate_module_def(emitter, module_name, module) for fn in module.functions: emitter.emit_line() generate_native_function(fn, emitter, self.source_paths[module_name], module_name) if fn.name != TOP_LEVEL_NAME: emitter.emit_line() generate_wrapper_function(fn, emitter, self.source_paths[module_name], module_name) if multi_file: name = ('__native_{}.c'.format( emitter.names.private_name(module_name))) file_contents.append((name, ''.join(emitter.fragments))) # The external header file contains type declarations while # the internal contains declarations of functions and objects # (which are shared between shared libraries via dynamic # exports tables and not accessed directly.) ext_declarations = Emitter(self.context) ext_declarations.emit_line('#ifndef MYPYC_NATIVE{}_H'.format( self.group_suffix)) ext_declarations.emit_line('#define MYPYC_NATIVE{}_H'.format( self.group_suffix)) ext_declarations.emit_line('#include <Python.h>') ext_declarations.emit_line('#include <CPy.h>') declarations = Emitter(self.context) declarations.emit_line('#ifndef MYPYC_NATIVE_INTERNAL{}_H'.format( self.group_suffix)) declarations.emit_line('#define MYPYC_NATIVE_INTERNAL{}_H'.format( self.group_suffix)) declarations.emit_line('#include <Python.h>') declarations.emit_line('#include <CPy.h>') declarations.emit_line('#include "__native{}.h"'.format( self.short_group_suffix)) declarations.emit_line() declarations.emit_line('int CPyGlobalsInit(void);') declarations.emit_line() for module_name, module in self.modules: self.declare_finals(module_name, module.final_names, declarations) for cl in module.classes: generate_class_type_decl(cl, emitter, ext_declarations, declarations) for fn in module.functions: generate_function_declaration(fn, declarations) for lib in sorted(self.context.group_deps): elib = exported_name(lib) short_lib = exported_name(lib.split('.')[-1]) declarations.emit_lines( '#include <{}>'.format( os.path.join(group_dir(lib), "__native_{}.h".format(short_lib))), 'struct export_table_{} exports_{};'.format(elib, elib)) sorted_decls = self.toposort_declarations() emitter = base_emitter self.generate_globals_init(emitter) emitter.emit_line() for declaration in sorted_decls: decls = ext_declarations if declaration.is_type else declarations if not declaration.is_type: decls.emit_lines('extern {}'.format(declaration.decl[0]), *declaration.decl[1:]) # If there is a definition, emit it. Otherwise repeat the declaration # (without an extern). if declaration.defn: emitter.emit_lines(*declaration.defn) else: emitter.emit_lines(*declaration.decl) else: decls.emit_lines(*declaration.decl) if self.group_name: self.generate_export_table(ext_declarations, emitter) self.generate_shared_lib_init(emitter) ext_declarations.emit_line('#endif') declarations.emit_line('#endif') output_dir = group_dir(self.group_name) if self.group_name else '' return file_contents + [ (os.path.join(output_dir, '__native{}.c'.format( self.short_group_suffix)), ''.join(emitter.fragments)), (os.path.join( output_dir, '__native_internal{}.h'.format( self.short_group_suffix)), ''.join( declarations.fragments)), (os.path.join(output_dir, '__native{}.h'.format( self.short_group_suffix)), ''.join( ext_declarations.fragments)), ]
def declare_static_pyobject(self, identifier: str, emitter: Emitter) -> None: symbol = emitter.static_name(identifier, None) self.declare_global('PyObject *', symbol)
def generate_class(cl: ClassIR, module: str, emitter: Emitter) -> None: """Generate C code for a class. This is the main entry point to the module. """ name = cl.name name_prefix = cl.name_prefix(emitter.names) setup_name = '{}_setup'.format(name_prefix) new_name = '{}_new'.format(name_prefix) members_name = '{}_members'.format(name_prefix) getseters_name = '{}_getseters'.format(name_prefix) vtable_name = '{}_vtable'.format(name_prefix) traverse_name = '{}_traverse'.format(name_prefix) clear_name = '{}_clear'.format(name_prefix) dealloc_name = '{}_dealloc'.format(name_prefix) methods_name = '{}_methods'.format(name_prefix) vtable_setup_name = '{}_trait_vtable_setup'.format(name_prefix) fields = OrderedDict() # type: Dict[str, str] fields['tp_name'] = '"{}"'.format(name) generate_full = not cl.is_trait and not cl.builtin_base needs_getseters = not cl.is_generated if not cl.builtin_base: fields['tp_new'] = new_name if generate_full: fields['tp_dealloc'] = '(destructor){}_dealloc'.format(name_prefix) fields['tp_traverse'] = '(traverseproc){}_traverse'.format(name_prefix) fields['tp_clear'] = '(inquiry){}_clear'.format(name_prefix) if needs_getseters: fields['tp_getset'] = getseters_name fields['tp_methods'] = methods_name def emit_line() -> None: emitter.emit_line() emit_line() # If the class has a method to initialize default attribute # values, we need to call it during initialization. defaults_fn = cl.get_method('__mypyc_defaults_setup') # If there is a __init__ method, we'll use it in the native constructor. init_fn = cl.get_method('__init__') # Fill out slots in the type object from dunder methods. fields.update(generate_slots(cl, SLOT_DEFS, emitter)) # Fill out dunder methods that live in tables hanging off the side. for table_name, type, slot_defs in SIDE_TABLES: slots = generate_slots(cl, slot_defs, emitter) if slots: table_struct_name = generate_side_table_for_class(cl, table_name, type, slots, emitter) fields['tp_{}'.format(table_name)] = '&{}'.format(table_struct_name) richcompare_name = generate_richcompare_wrapper(cl, emitter) if richcompare_name: fields['tp_richcompare'] = richcompare_name # If the class inherits from python, make space for a __dict__ struct_name = cl.struct_name(emitter.names) if cl.builtin_base: base_size = 'sizeof({})'.format(cl.builtin_base) elif cl.is_trait: base_size = 'sizeof(PyObject)' else: base_size = 'sizeof({})'.format(struct_name) # Since our types aren't allocated using type() we need to # populate these fields ourselves if we want them to have correct # values. PyType_Ready will inherit the offsets from tp_base but # that isn't what we want. # XXX: there is no reason for the __weakref__ stuff to be mixed up with __dict__ if cl.has_dict: # __dict__ lives right after the struct and __weakref__ lives right after that # TODO: They should get members in the struct instead of doing this nonsense. weak_offset = '{} + sizeof(PyObject *)'.format(base_size) emitter.emit_lines( 'PyMemberDef {}[] = {{'.format(members_name), '{{"__dict__", T_OBJECT_EX, {}, 0, NULL}},'.format(base_size), '{{"__weakref__", T_OBJECT_EX, {}, 0, NULL}},'.format(weak_offset), '{0}', '};', ) fields['tp_members'] = members_name fields['tp_basicsize'] = '{} + 2*sizeof(PyObject *)'.format(base_size) fields['tp_dictoffset'] = base_size fields['tp_weaklistoffset'] = weak_offset else: fields['tp_basicsize'] = base_size if generate_full: # Declare setup method that allocates and initializes an object. type is the # type of the class being initialized, which could be another class if there # is an interpreted subclass. emitter.emit_line('static PyObject *{}(PyTypeObject *type);'.format(setup_name)) assert cl.ctor is not None emitter.emit_line(native_function_header(cl.ctor, emitter) + ';') emit_line() generate_new_for_class(cl, new_name, vtable_name, setup_name, emitter) emit_line() generate_traverse_for_class(cl, traverse_name, emitter) emit_line() generate_clear_for_class(cl, clear_name, emitter) emit_line() generate_dealloc_for_class(cl, dealloc_name, clear_name, emitter) emit_line() if cl.allow_interpreted_subclasses: shadow_vtable_name = generate_vtables( cl, vtable_setup_name + "_shadow", vtable_name + "_shadow", emitter, shadow=True ) # type: Optional[str] emit_line() else: shadow_vtable_name = None vtable_name = generate_vtables(cl, vtable_setup_name, vtable_name, emitter, shadow=False) emit_line() if needs_getseters: generate_getseter_declarations(cl, emitter) emit_line() generate_getseters_table(cl, getseters_name, emitter) emit_line() if cl.is_trait: generate_new_for_trait(cl, new_name, emitter) generate_methods_table(cl, methods_name, emitter) emit_line() flags = ['Py_TPFLAGS_DEFAULT', 'Py_TPFLAGS_HEAPTYPE', 'Py_TPFLAGS_BASETYPE'] if generate_full: flags.append('Py_TPFLAGS_HAVE_GC') fields['tp_flags'] = ' | '.join(flags) emitter.emit_line("static PyTypeObject {}_template_ = {{".format(emitter.type_struct_name(cl))) emitter.emit_line("PyVarObject_HEAD_INIT(NULL, 0)") for field, value in fields.items(): emitter.emit_line(".{} = {},".format(field, value)) emitter.emit_line("};") emitter.emit_line("static PyTypeObject *{t}_template = &{t}_template_;".format( t=emitter.type_struct_name(cl))) emitter.emit_line() if generate_full: generate_setup_for_class( cl, setup_name, defaults_fn, vtable_name, shadow_vtable_name, emitter) emitter.emit_line() generate_constructor_for_class( cl, cl.ctor, init_fn, setup_name, vtable_name, emitter) emitter.emit_line() if needs_getseters: generate_getseters(cl, emitter)
def generate_new_for_class(cl: ClassIR, func_name: str, vtable_name: str, setup_name: str, init_fn: Optional[FuncIR], emitter: Emitter) -> None: emitter.emit_line('static PyObject *') emitter.emit_line( f'{func_name}(PyTypeObject *type, PyObject *args, PyObject *kwds)') emitter.emit_line('{') # TODO: Check and unbox arguments if not cl.allow_interpreted_subclasses: emitter.emit_line(f'if (type != {emitter.type_struct_name(cl)}) {{') emitter.emit_line( 'PyErr_SetString(PyExc_TypeError, "interpreted classes cannot inherit from compiled");' ) emitter.emit_line('return NULL;') emitter.emit_line('}') if (not init_fn or cl.allow_interpreted_subclasses or cl.builtin_base or cl.is_serializable()): # Match Python semantics -- __new__ doesn't call __init__. emitter.emit_line(f'return {setup_name}(type);') else: # __new__ of a native class implicitly calls __init__ so that we # can enforce that instances are always properly initialized. This # is needed to support always defined attributes. emitter.emit_line(f'PyObject *self = {setup_name}(type);') emitter.emit_lines('if (self == NULL)', ' return NULL;') emitter.emit_line( f'PyObject *ret = {PREFIX}{init_fn.cname(emitter.names)}(self, args, kwds);') emitter.emit_lines('if (ret == NULL)', ' return NULL;') emitter.emit_line('return self;') emitter.emit_line('}')
def generate_vtables(base: ClassIR, vtable_setup_name: str, vtable_name: str, emitter: Emitter, shadow: bool) -> str: """Emit the vtables and vtable setup functions for a class. This includes both the primary vtable and any trait implementation vtables. The trait vtables go before the main vtable, and have the following layout: { CPyType_T1, // pointer to type object C_T1_trait_vtable, // pointer to array of method pointers C_T1_offset_table, // pointer to array of attribute offsets CPyType_T2, C_T2_trait_vtable, C_T2_offset_table, ... } The method implementations are calculated at the end of IR pass, attribute offsets are {offsetof(native__C, _x1), offsetof(native__C, _y1), ...}. To account for both dynamic loading and dynamic class creation, vtables are populated dynamically at class creation time, so we emit empty array definitions to store the vtables and a function to populate them. If shadow is True, generate "shadow vtables" that point to the shadow glue methods (which should dispatch via the Python C-API). Returns the expression to use to refer to the vtable, which might be different than the name, if there are trait vtables. """ def trait_vtable_name(trait: ClassIR) -> str: return '{}_{}_trait_vtable{}'.format( base.name_prefix(emitter.names), trait.name_prefix(emitter.names), '_shadow' if shadow else '') def trait_offset_table_name(trait: ClassIR) -> str: return '{}_{}_offset_table'.format( base.name_prefix(emitter.names), trait.name_prefix(emitter.names) ) # Emit array definitions with enough space for all the entries emitter.emit_line('static CPyVTableItem {}[{}];'.format( vtable_name, max(1, len(base.vtable_entries) + 3 * len(base.trait_vtables)))) for trait, vtable in base.trait_vtables.items(): # Trait methods entry (vtable index -> method implementation). emitter.emit_line('static CPyVTableItem {}[{}];'.format( trait_vtable_name(trait), max(1, len(vtable)))) # Trait attributes entry (attribute number in trait -> offset in actual struct). emitter.emit_line('static size_t {}[{}];'.format( trait_offset_table_name(trait), max(1, len(trait.attributes))) ) # Emit vtable setup function emitter.emit_line('static bool') emitter.emit_line('{}{}(void)'.format(NATIVE_PREFIX, vtable_setup_name)) emitter.emit_line('{') if base.allow_interpreted_subclasses and not shadow: emitter.emit_line('{}{}_shadow();'.format(NATIVE_PREFIX, vtable_setup_name)) subtables = [] for trait, vtable in base.trait_vtables.items(): name = trait_vtable_name(trait) offset_name = trait_offset_table_name(trait) generate_vtable(vtable, name, emitter, [], shadow) generate_offset_table(offset_name, emitter, trait, base) subtables.append((trait, name, offset_name)) generate_vtable(base.vtable_entries, vtable_name, emitter, subtables, shadow) emitter.emit_line('return 1;') emitter.emit_line('}') return vtable_name if not subtables else "{} + {}".format(vtable_name, len(subtables) * 3)
def generate_getter(cl: ClassIR, attr: str, rtype: RType, emitter: Emitter) -> None: attr_field = emitter.attr(attr) emitter.emit_line('static PyObject *') emitter.emit_line('{}({} *self, void *closure)'.format(getter_name(cl, attr, emitter.names), cl.struct_name(emitter.names))) emitter.emit_line('{') attr_expr = f'self->{attr_field}' # HACK: Don't consider refcounted values as always defined, since it's possible to # access uninitialized values via 'gc.get_objects()'. Accessing non-refcounted # values is benign. always_defined = cl.is_always_defined(attr) and not rtype.is_refcounted if not always_defined: emitter.emit_undefined_attr_check(rtype, attr_expr, '==', unlikely=True) emitter.emit_line('PyErr_SetString(PyExc_AttributeError,') emitter.emit_line(' "attribute {} of {} undefined");'.format(repr(attr), repr(cl.name))) emitter.emit_line('return NULL;') emitter.emit_line('}') emitter.emit_inc_ref(f'self->{attr_field}', rtype) emitter.emit_box(f'self->{attr_field}', 'retval', rtype, declare_dest=True) emitter.emit_line('return retval;') emitter.emit_line('}')
def generate_vtable(entries: VTableEntries, vtable_name: str, emitter: Emitter, subtables: List[Tuple[ClassIR, str, str]], shadow: bool) -> None: emitter.emit_line('CPyVTableItem {}_scratch[] = {{'.format(vtable_name)) if subtables: emitter.emit_line('/* Array of trait vtables */') for trait, table, offset_table in subtables: emitter.emit_line( '(CPyVTableItem){}, (CPyVTableItem){}, (CPyVTableItem){},'.format( emitter.type_struct_name(trait), table, offset_table)) emitter.emit_line('/* Start of real vtable */') for entry in entries: method = entry.shadow_method if shadow and entry.shadow_method else entry.method emitter.emit_line('(CPyVTableItem){}{}{},'.format( emitter.get_group_prefix(entry.method.decl), NATIVE_PREFIX, method.cname(emitter.names))) # msvc doesn't allow empty arrays; maybe allowing them at all is an extension? if not entries: emitter.emit_line('NULL') emitter.emit_line('};') emitter.emit_line('memcpy({name}, {name}_scratch, sizeof({name}));'.format(name=vtable_name))
def generate_setter(cl: ClassIR, attr: str, rtype: RType, emitter: Emitter) -> None: attr_field = emitter.attr(attr) emitter.emit_line('static int') emitter.emit_line('{}({} *self, PyObject *value, void *closure)'.format( setter_name(cl, attr, emitter.names), cl.struct_name(emitter.names))) emitter.emit_line('{') deletable = cl.is_deletable(attr) if not deletable: emitter.emit_line('if (value == NULL) {') emitter.emit_line('PyErr_SetString(PyExc_AttributeError,') emitter.emit_line(' "{} object attribute {} cannot be deleted");'.format(repr(cl.name), repr(attr))) emitter.emit_line('return -1;') emitter.emit_line('}') # HACK: Don't consider refcounted values as always defined, since it's possible to # access uninitialized values via 'gc.get_objects()'. Accessing non-refcounted # values is benign. always_defined = cl.is_always_defined(attr) and not rtype.is_refcounted if rtype.is_refcounted: attr_expr = f'self->{attr_field}' if not always_defined: emitter.emit_undefined_attr_check(rtype, attr_expr, '!=') emitter.emit_dec_ref('self->{}'.format(attr_field), rtype) if not always_defined: emitter.emit_line('}') if deletable: emitter.emit_line('if (value != NULL) {') if rtype.is_unboxed: emitter.emit_unbox('value', 'tmp', rtype, error=ReturnHandler('-1'), declare_dest=True) elif is_same_type(rtype, object_rprimitive): emitter.emit_line('PyObject *tmp = value;') else: emitter.emit_cast('value', 'tmp', rtype, declare_dest=True) emitter.emit_lines('if (!tmp)', ' return -1;') emitter.emit_inc_ref('tmp', rtype) emitter.emit_line(f'self->{attr_field} = tmp;') if deletable: emitter.emit_line('} else') emitter.emit_line(' self->{} = {};'.format(attr_field, emitter.c_undefined_value(rtype))) emitter.emit_line('return 0;') emitter.emit_line('}')
def generate_constructor_for_class(cl: ClassIR, fn: FuncDecl, init_fn: Optional[FuncIR], setup_name: str, vtable_name: str, emitter: Emitter) -> None: """Generate a native function that allocates and initializes an instance of a class.""" emitter.emit_line('{}'.format(native_function_header(fn, emitter))) emitter.emit_line('{') emitter.emit_line('PyObject *self = {}({});'.format(setup_name, emitter.type_struct_name(cl))) emitter.emit_line('if (self == NULL)') emitter.emit_line(' return NULL;') args = ', '.join(['self'] + [REG_PREFIX + arg.name for arg in fn.sig.args]) if init_fn is not None: emitter.emit_line('char res = {}{}{}({});'.format( emitter.get_group_prefix(init_fn.decl), NATIVE_PREFIX, init_fn.cname(emitter.names), args)) emitter.emit_line('if (res == 2) {') emitter.emit_line('Py_DECREF(self);') emitter.emit_line('return NULL;') emitter.emit_line('}') # If there is a nontrivial ctor that we didn't define, invoke it via tp_init elif len(fn.sig.args) > 1: emitter.emit_line( 'int res = {}->tp_init({});'.format( emitter.type_struct_name(cl), args)) emitter.emit_line('if (res < 0) {') emitter.emit_line('Py_DECREF(self);') emitter.emit_line('return NULL;') emitter.emit_line('}') emitter.emit_line('return self;') emitter.emit_line('}')
def generate_readonly_getter(cl: ClassIR, attr: str, rtype: RType, func_ir: FuncIR, emitter: Emitter) -> None: emitter.emit_line('static PyObject *') emitter.emit_line('{}({} *self, void *closure)'.format(getter_name(cl, attr, emitter.names), cl.struct_name(emitter.names))) emitter.emit_line('{') if rtype.is_unboxed: emitter.emit_line('{}retval = {}{}((PyObject *) self);'.format( emitter.ctype_spaced(rtype), NATIVE_PREFIX, func_ir.cname(emitter.names))) emitter.emit_box('retval', 'retbox', rtype, declare_dest=True) emitter.emit_line('return retbox;') else: emitter.emit_line('return {}{}((PyObject *) self);'.format(NATIVE_PREFIX, func_ir.cname(emitter.names))) emitter.emit_line('}')
def generate_new_for_trait(cl: ClassIR, func_name: str, emitter: Emitter) -> None: emitter.emit_line('static PyObject *') emitter.emit_line( '{}(PyTypeObject *type, PyObject *args, PyObject *kwds)'.format(func_name)) emitter.emit_line('{') emitter.emit_line('if (type != {}) {{'.format(emitter.type_struct_name(cl))) emitter.emit_line( 'PyErr_SetString(PyExc_TypeError, ' '"interpreted classes cannot inherit from compiled traits");' ) emitter.emit_line('} else {') emitter.emit_line( 'PyErr_SetString(PyExc_TypeError, "traits may not be directly created");' ) emitter.emit_line('}') emitter.emit_line('return NULL;') emitter.emit_line('}')
def generate_property_setter(cl: ClassIR, attr: str, arg_type: RType, func_ir: FuncIR, emitter: Emitter) -> None: emitter.emit_line('static int') emitter.emit_line('{}({} *self, PyObject *value, void *closure)'.format( setter_name(cl, attr, emitter.names), cl.struct_name(emitter.names))) emitter.emit_line('{') if arg_type.is_unboxed: emitter.emit_unbox('value', 'tmp', arg_type, error=ReturnHandler('-1'), declare_dest=True) emitter.emit_line('{}{}((PyObject *) self, tmp);'.format( NATIVE_PREFIX, func_ir.cname(emitter.names))) else: emitter.emit_line('{}{}((PyObject *) self, value);'.format( NATIVE_PREFIX, func_ir.cname(emitter.names))) emitter.emit_line('return 0;') emitter.emit_line('}')
def generate_clear_for_class(cl: ClassIR, func_name: str, emitter: Emitter) -> None: emitter.emit_line('static int') emitter.emit_line('{}({} *self)'.format(func_name, cl.struct_name(emitter.names))) emitter.emit_line('{') for base in reversed(cl.base_mro): for attr, rtype in base.attributes.items(): emitter.emit_gc_clear('self->{}'.format(emitter.attr(attr)), rtype) if cl.has_dict: struct_name = cl.struct_name(emitter.names) # __dict__ lives right after the struct and __weakref__ lives right after that emitter.emit_gc_clear('*((PyObject **)((char *)self + sizeof({})))'.format( struct_name), object_rprimitive) emitter.emit_gc_clear( '*((PyObject **)((char *)self + sizeof(PyObject *) + sizeof({})))'.format( struct_name), object_rprimitive) emitter.emit_line('return 0;') emitter.emit_line('}')
def native_function_type(fn: FuncIR, emitter: Emitter) -> str: args = ', '.join(emitter.ctype(arg.type) for arg in fn.args) or 'void' ret = emitter.ctype(fn.ret_type) return '{} (*)({})'.format(ret, args)
def generate_methods_table(cl: ClassIR, name: str, emitter: Emitter) -> None: emitter.emit_line('static PyMethodDef {}[] = {{'.format(name)) for fn in cl.methods.values(): if fn.decl.is_prop_setter or fn.decl.is_prop_getter: continue emitter.emit_line('{{"{}",'.format(fn.name)) emitter.emit_line(' (PyCFunction){}{},'.format(PREFIX, fn.cname(emitter.names))) flags = ['METH_VARARGS', 'METH_KEYWORDS'] if fn.decl.kind == FUNC_STATICMETHOD: flags.append('METH_STATIC') elif fn.decl.kind == FUNC_CLASSMETHOD: flags.append('METH_CLASS') emitter.emit_line(' {}, NULL}},'.format(' | '.join(flags))) # Provide a default __getstate__ and __setstate__ if not cl.has_method('__setstate__') and not cl.has_method('__getstate__'): emitter.emit_lines( '{"__setstate__", (PyCFunction)CPyPickle_SetState, METH_O, NULL},', '{"__getstate__", (PyCFunction)CPyPickle_GetState, METH_NOARGS, NULL},', ) emitter.emit_line('{NULL} /* Sentinel */') emitter.emit_line('};')
def generate_native_function(fn: FuncIR, emitter: Emitter, source_path: str, module_name: str, optimize_int: bool = True) -> None: if optimize_int: const_int_regs = find_constant_integer_registers(fn.blocks) else: const_int_regs = {} declarations = Emitter(emitter.context, fn.env) body = Emitter(emitter.context, fn.env) visitor = FunctionEmitterVisitor(body, declarations, source_path, module_name, const_int_regs) declarations.emit_line('{} {{'.format( native_function_header(fn.decl, emitter))) body.indent() for r, i in fn.env.indexes.items(): if isinstance(r.type, RTuple): emitter.declare_tuple_struct(r.type) if i < len(fn.args): continue # skip the arguments ctype = emitter.ctype_spaced(r.type) init = '' if r in fn.env.vars_needing_init: init = ' = {}'.format(declarations.c_error_value(r.type)) if r.name not in const_int_regs: declarations.emit_line('{ctype}{prefix}{name}{init};'.format( ctype=ctype, prefix=REG_PREFIX, name=r.name, init=init)) # Before we emit the blocks, give them all labels for i, block in enumerate(fn.blocks): block.label = i for block in fn.blocks: body.emit_label(block) for op in block.ops: op.accept(visitor) body.emit_line('}') emitter.emit_from_emitter(declarations) emitter.emit_from_emitter(body)
def generate_getseters_table(cl: ClassIR, name: str, emitter: Emitter) -> None: emitter.emit_line('static PyGetSetDef {}[] = {{'.format(name)) if not cl.is_trait: for attr in cl.attributes: emitter.emit_line('{{"{}",'.format(attr)) emitter.emit_line(' (getter){}, (setter){},'.format( getter_name(cl, attr, emitter.names), setter_name(cl, attr, emitter.names))) emitter.emit_line(' NULL, NULL},') for prop in cl.properties: emitter.emit_line('{{"{}",'.format(prop)) emitter.emit_line(' (getter){},'.format(getter_name(cl, prop, emitter.names))) setter = cl.properties[prop][1] if setter: emitter.emit_line(' (setter){},'.format(setter_name(cl, prop, emitter.names))) emitter.emit_line('NULL, NULL},') else: emitter.emit_line('NULL, NULL, NULL},') emitter.emit_line('{NULL} /* Sentinel */') emitter.emit_line('};')
def declare_internal_globals(self, module_name: str, emitter: Emitter) -> None: static_name = emitter.static_name('globals', module_name) self.declare_global('PyObject *', static_name)