Exemplo n.º 1
0
    def setUp(self) -> None:
        self.env = Environment()
        self.n = self.env.add_local(Var('n'), int_rprimitive)
        self.m = self.env.add_local(Var('m'), int_rprimitive)
        self.k = self.env.add_local(Var('k'), int_rprimitive)
        self.l = self.env.add_local(Var('l'), list_rprimitive)  # noqa
        self.ll = self.env.add_local(Var('ll'), list_rprimitive)
        self.o = self.env.add_local(Var('o'), object_rprimitive)
        self.o2 = self.env.add_local(Var('o2'), object_rprimitive)
        self.d = self.env.add_local(Var('d'), dict_rprimitive)
        self.b = self.env.add_local(Var('b'), bool_rprimitive)
        self.t = self.env.add_local(Var('t'),
                                    RTuple([int_rprimitive, bool_rprimitive]))
        self.tt = self.env.add_local(
            Var('tt'),
            RTuple(
                [RTuple([int_rprimitive, bool_rprimitive]), bool_rprimitive]))
        ir = ClassIR('A', 'mod')
        ir.attributes = OrderedDict([('x', bool_rprimitive),
                                     ('y', int_rprimitive)])
        compute_vtable(ir)
        ir.mro = [ir]
        self.r = self.env.add_local(Var('r'), RInstance(ir))

        self.context = EmitterContext(NameGenerator([['mod']]))
        self.emitter = Emitter(self.context, self.env)
        self.declarations = Emitter(self.context, self.env)
        self.visitor = FunctionEmitterVisitor(self.emitter, self.declarations,
                                              'prog.py', 'prog')
Exemplo n.º 2
0
    def __init__(self, literals: LiteralsMap, modules: List[Tuple[str,
                                                                  ModuleIR]],
                 source_paths: Dict[str, str], group_name: Optional[str],
                 group_map: Dict[str, Optional[str]], names: NameGenerator,
                 multi_file: bool) -> None:
        """Generator for C source for a compilation group.

        The code for a compilation group contains an internal and an
        external .h file, and then one .c if not in multi_file mode or
        one .c file per module if in multi_file mode.)

        Arguments:
            literals: The literals declared in this group
            modules: (name, ir) pairs for each module in the group
            source_paths: Map from module names to source file paths
            group_name: The name of the group (or None if this is single-module compilation)
            group_map: A map of modules to their group names
            names: The name generator for the compilation
            multi_file: Whether to put each module in its own source file regardless
                        of group structure.
        """
        self.literals = literals
        self.modules = modules
        self.source_paths = source_paths
        self.context = EmitterContext(names, group_name, group_map)
        self.names = names
        # Initializations of globals to simple values that we can't
        # do statically because the windows loader is bad.
        self.simple_inits = []  # type: List[Tuple[str, str]]
        self.group_name = group_name
        self.use_shared_lib = group_name is not None
        self.multi_file = multi_file
Exemplo n.º 3
0
 def __init__(self,
              modules: List[Tuple[str, ModuleIR]],
              source_paths: Dict[str, str]) -> None:
     self.modules = modules
     self.source_paths = source_paths
     self.context = EmitterContext([name for name, _ in modules])
     self.names = self.context.names
Exemplo n.º 4
0
 def __init__(self, literals: LiteralsMap, modules: List[Tuple[str,
                                                               ModuleIR]],
              source_paths: Dict[str, str], use_shared_lib: bool) -> None:
     self.literals = literals
     self.modules = modules
     self.source_paths = source_paths
     self.context = EmitterContext([name for name, _ in modules])
     self.names = self.context.names
     self.use_shared_lib = use_shared_lib
Exemplo n.º 5
0
 def test_simple(self) -> None:
     self.block.ops.append(Return(self.reg))
     fn = FuncIR('myfunc', [self.arg], IntRType(), [self.block], self.env)
     emitter = Emitter(EmitterContext())
     generate_native_function(fn, emitter)
     result = emitter.fragments
     assert_string_arrays_equal([
         'static CPyTagged CPyDef_myfunc(CPyTagged cpy_r_arg) {\n',
         'CPyL0: ;\n',
         '    return cpy_r_arg;\n',
         '}\n',
     ],
                                result,
                                msg='Generated code invalid')
Exemplo n.º 6
0
 def __init__(self, literals: LiteralsMap, modules: List[Tuple[str,
                                                               ModuleIR]],
              source_paths: Dict[str, str], shared_lib_name: Optional[str],
              multi_file: bool) -> None:
     self.literals = literals
     self.modules = modules
     self.source_paths = source_paths
     self.context = EmitterContext([name for name, _ in modules])
     self.names = self.context.names
     # Initializations of globals to simple values that we can't
     # do statically because the windows loader is bad.
     self.simple_inits = []  # type: List[Tuple[str, str]]
     self.shared_lib_name = shared_lib_name
     self.use_shared_lib = shared_lib_name is not None
     self.multi_file = multi_file
Exemplo n.º 7
0
 def setUp(self) -> None:
     self.env = Environment()
     self.n = self.env.add_local(Var('n'), IntRType())
     self.m = self.env.add_local(Var('m'), IntRType())
     self.k = self.env.add_local(Var('k'), IntRType())
     self.l = self.env.add_local(Var('l'), ListRType())
     self.ll = self.env.add_local(Var('ll'), ListRType())
     self.o = self.env.add_local(Var('o'), ObjectRType())
     self.o2 = self.env.add_local(Var('o2'), ObjectRType())
     self.d = self.env.add_local(Var('d'), DictRType())
     self.b = self.env.add_local(Var('b'), BoolRType())
     self.context = EmitterContext()
     self.emitter = Emitter(self.context, self.env)
     self.declarations = Emitter(self.context, self.env)
     self.visitor = FunctionEmitterVisitor(self.emitter, self.declarations)
Exemplo n.º 8
0
 def test_simple(self) -> None:
     self.block.ops.append(Return(self.reg))
     fn = FuncIR('myfunc', None, 'mod', FuncSignature([self.arg], int_rprimitive),
                 [self.block], self.env)
     emitter = Emitter(EmitterContext(['mod']))
     generate_native_function(fn, emitter, 'prog.py', 'prog')
     result = emitter.fragments
     assert_string_arrays_equal(
         [
             'static CPyTagged CPyDef_myfunc(CPyTagged cpy_r_arg) {\n',
             'CPyL0: ;\n',
             '    return cpy_r_arg;\n',
             '}\n',
         ],
         result, msg='Generated code invalid')
Exemplo n.º 9
0
 def test_register(self) -> None:
     self.temp = self.env.add_temp(IntRType())
     self.block.ops.append(LoadInt(self.temp, 5))
     fn = FuncIR('myfunc', [self.arg], ListRType(), [self.block], self.env)
     emitter = Emitter(EmitterContext())
     generate_native_function(fn, emitter)
     result = emitter.fragments
     assert_string_arrays_equal([
         'static PyObject *CPyDef_myfunc(CPyTagged cpy_r_arg) {\n',
         '    CPyTagged cpy_r_r0;\n',
         'CPyL0: ;\n',
         '    cpy_r_r0 = 10;\n',
         '}\n',
     ],
                                result,
                                msg='Generated code invalid')
Exemplo n.º 10
0
 def test_register(self) -> None:
     self.env.temp_index = 0
     op = LoadInt(5)
     self.block.ops.append(op)
     self.env.add_op(op)
     fn = FuncIR('myfunc', None, 'mod', FuncSignature([self.arg], list_rprimitive),
                 [self.block], self.env)
     emitter = Emitter(EmitterContext(['mod']))
     generate_native_function(fn, emitter, 'prog.py', 'prog')
     result = emitter.fragments
     assert_string_arrays_equal(
         [
             'static PyObject *CPyDef_myfunc(CPyTagged cpy_r_arg) {\n',
             '    CPyTagged cpy_r_r0;\n',
             'CPyL0: ;\n',
             '    cpy_r_r0 = 10;\n',
             '}\n',
         ],
         result, msg='Generated code invalid')
Exemplo n.º 11
0
 def setUp(self) -> None:
     self.env = Environment()
     self.n = self.env.add_local(Var('n'), int_rprimitive)
     self.context = EmitterContext(NameGenerator([['mod']]))
     self.emitter = Emitter(self.context, self.env)
Exemplo n.º 12
0
 def setUp(self) -> None:
     self.context = EmitterContext(['mod'])
Exemplo n.º 13
0
 def setUp(self) -> None:
     self.context = EmitterContext(NameGenerator([['mod']]))
Exemplo n.º 14
0
 def setUp(self) -> None:
     self.env = Environment()
     self.n = self.env.add_local(Var('n'), IntRType())
     self.context = EmitterContext()
     self.emitter = Emitter(self.context, self.env)
Exemplo n.º 15
0
 def __init__(self, module_name: str, module: ModuleIR) -> None:
     self.module_name = module_name
     self.module = module
     self.context = EmitterContext()