Exemple #1
0
    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)
Exemple #2
0
 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())
Exemple #3
0
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