def test_function_tailcall(self): """ Test if a tailcall in a function works out nicely """ # Prepare an optimizable module: builder = irutils.Builder() module = ir.Module('test') builder.set_module(module) function = builder.new_function('x', ir.Binding.GLOBAL, ir.i8) builder.set_function(function) entry = builder.new_block() function.entry = entry a = ir.Parameter('a', ir.i8) function.add_parameter(a) b = ir.Parameter('b', ir.i8) function.add_parameter(b) builder.set_block(entry) one = builder.emit(ir.Const(1, 'const', ir.i8)) a2 = builder.emit(ir.add(a, one, 'a2', ir.i8)) b2 = builder.emit(ir.add(b, one, 'b2', ir.i8)) result = builder.emit(ir.FunctionCall(function, [a2, b2], 'rv', ir.i8)) builder.emit(ir.Return(result)) # Verify first version: module.display() verify_module(module) self.assertFalse(function.is_leaf()) # Run optimizer: self.opt.run(module) # Verify again: module.display() verify_module(module) self.assertTrue(function.is_leaf())
def test_bug2(self): """ Check that if blocks are in function in strange order, the dag builder works """ module = ir.Module('dut') function = ir.Procedure('tst', ir.Binding.GLOBAL) module.add_function(function) block1 = ir.Block('b1') block2 = ir.Block('b2') function.add_block(block1) function.add_block(block2) function.entry = block2 con = ir.Const(2, 'con', ir.i32) block2.add_instruction(con) block2.add_instruction(ir.Jump(block1)) block1.add_instruction(ir.Cast(con, 'con_cast', ir.i8)) block1.add_instruction(ir.Exit()) # Target generation target = get_arch('arm') frame = target.new_frame('a', function) function_info = FunctionInfo(frame) debug_db = DebugDb() prepare_function_info(target, function_info, function) dag_builder = SelectionGraphBuilder(target) sgraph = dag_builder.build(function, function_info, debug_db) dag_splitter = DagSplitter(target)
def generate(self, wasm_module, debug_db=None): assert isinstance(wasm_module, wasmfun.Module) self.builder.module = ir.Module('mainmodule') for wasm_function in wasm_module.sections[-1].functiondefs: self.generate_function(wasm_function, debug_db) return self.builder.module
def setUp(self): self.debug_db = DebugDb() self.builder = irutils.Builder() self.module = ir.Module('test', debug_db=self.debug_db) self.builder.set_module(self.module) self.function = self.builder.new_procedure('testfunction', ir.Binding.GLOBAL) self.builder.set_function(self.function) entry = self.builder.new_block() self.function.entry = entry self.builder.set_block(entry)
def gen_module(self): module = ir.Module('fuzz') self.builder.module = module # Generate some functions for i in range(random.randrange(6, 20)): self.gen_function('fzfnc{}'.format(i)) f = io.StringIO() self.verifier.verify(module) writer = Writer(f) writer.write(module) print(f.getvalue()) return module
def test_write(self): module = ir.Module("mod1") function = ir.Procedure("func1", ir.Binding.GLOBAL) module.add_function(function) entry = ir.Block("entry") function.add_block(entry) function.entry = entry entry.add_instruction(ir.Exit()) f = io.StringIO() writer = irutils.Writer(file=f) writer.write(module) # print(f.getvalue()) f2 = io.StringIO(f.getvalue()) reader = irutils.Reader() module2 = reader.read(f2) f3 = io.StringIO() writer = irutils.Writer(file=f3) writer.write(module2) self.assertEqual(f3.getvalue(), f.getvalue())
def parse(): woot = metamodel_from_file('say.tx') model = woot.model_from_file('test.say') module = ir.Module('main') func = ir.Procedure('main') module.add_function(func) block = ir.Block('entry') func.add_block(block) func.entry = block for statement in model.statements: text = statement.text data = struct.pack('<i', len(text)) + text.encode('ascii') text_val = ir.LiteralData(data, 'text') block.add_instruction(text_val) block.add_instruction(ir.ProcedureCall('crt_print', [text_val])) block.add_instruction(ir.Exit()) return module
def compile(self, filename): self.variables = {} # Prepare the module ir_module = ir.Module('toy') self.io_print2 = ir.ExternalProcedure('io_print2', [ir.ptr, self.int_type]) ir_module.add_external(self.io_print2) ir_function = ir.Procedure('toy_toy', ir.Binding.GLOBAL) ir_module.add_function(ir_function) self.ir_block = ir.Block('entry') ir_function.entry = self.ir_block ir_function.add_block(self.ir_block) # Load the program: self.toy_mm.model_from_file(filename) # close the procedure self.emit(ir.Exit()) verify_module(ir_module) return ir_module
def test_bug1(self): """ This is the bug: function XXX sleep(i32 ms) entry: JUMP block12 block12: i32 loaded = load global_tick i32 binop = loaded + ms JUMP block14 block14: i32 loaded_2 = load global_tick IF binop > loaded_2 THEN block14 ELSE epilog epilog: exit Selection graph for function XXX sleep(i32 ms) entry: MOVI32[vreg0ms](REGI32[R1]) JMP[bsp_sleep_block12:] block12: MOVI32[vreg1loaded](LDRI32(LABEL[bsp_global_tick])) JMP[bsp_sleep_block14:] block14: MOVI32[vreg4loaded_2](LDRI32(LABEL[bsp_global_tick])) CJMP[('>', bsp_sleep_block14:, bsp_sleep_epilog:)] (REGI32[vreg5binop], REGI32[vreg4loaded_2]) epilog: """ builder = Builder() module = ir.Module('fuzz') global_tick = ir.Variable('global_tick', ir.Binding.GLOBAL, 4, 4) module.add_variable(global_tick) builder.module = module function = builder.new_procedure('sleep', ir.Binding.GLOBAL) ms = ir.Parameter('ms', ir.i32) function.add_parameter(ms) builder.set_function(function) entry = builder.new_block() function.entry = entry builder.set_block(entry) block12 = builder.new_block() builder.emit(ir.Jump(block12)) builder.set_block(block12) loaded = builder.emit(ir.Load(global_tick, 'loaded', ir.i32)) binop = builder.emit(ir.Binop(loaded, '+', ms, 'binop', ir.i32)) block14 = builder.new_block() builder.emit(ir.Jump(block14)) builder.set_block(block14) loaded2 = builder.emit(ir.Load(global_tick, 'loaded2', ir.i32)) epilog = builder.new_block() builder.emit(ir.CJump(binop, '>', loaded2, block14, epilog)) builder.set_block(epilog) builder.emit(ir.Exit()) # print('module:') # print_module(module) # Target generation target = ExampleArch() frame = target.new_frame('a', function) function_info = FunctionInfo(frame) debug_db = DebugDb() prepare_function_info(target, function_info, function) dag_builder = SelectionGraphBuilder(target) sgraph = dag_builder.build(function, function_info, debug_db) dag_splitter = DagSplitter(target) # print(function_info.value_map) for b in function: # root = function_info.block_roots[b] #print('root=', root) #for tree in dag_splitter.split_into_trees(root, frame): # print('tree=', tree) pass
def setUp(self): self.b = irutils.Builder() debug_db = DebugDb() self.cf = ConstantFolder() self.m = ir.Module("test", debug_db=debug_db) self.b.set_module(self.m)
def setUp(self): self.b = irutils.Builder() self.m = ir.Module("test") self.b.set_module(self.m)