def test_print(self): """ See if the ast can be printed using the visitor pattern """ snippet = """ module tstwhile; type int A; function void t() { var A i; i = 0; while (i < 1054) { i = i + 3; } } """ diag = DiagnosticsManager() lexer = Lexer(diag) parser = Parser(diag) arch = ExampleArch() context = Context(arch.info) tokens = lexer.lex(io.StringIO(snippet)) ast = parser.parse_source(tokens, context) printer = AstPrinter() f = io.StringIO() printer.print_ast(ast, f) self.assertTrue(f.getvalue()) str(ast.inner_scope)
def test_memory_layout(self): spec = """ MEMORY flash LOCATION=0x08000000 SIZE=0x3000 { DEFINESYMBOL(codestart) SECTION(code) DEFINESYMBOL(codeend) } MEMORY flash LOCATION=0x20000000 SIZE=0x3000 { SECTION(data) } """ memory_layout = layout.Layout.load(io.StringIO(spec)) arch = ExampleArch() object1 = ObjectFile(arch) object1.get_section('code', create=True).add_data(bytes([0] * 108)) object1.add_symbol(0, 'b', 'global', 24, 'code', 'object', 0) object2 = ObjectFile(arch) object2.get_section('code', create=True).add_data(bytes([0] * 100)) object2.get_section('data', create=True).add_data(bytes([0] * 100)) object2.add_symbol(0, 'a', 'global', 2, 'data', 'object', 0) object2.add_symbol(1, 'c', 'global', 2, 'code', 'object', 0) object3 = link([object1, object2], memory_layout) self.assertEqual(0x20000000 + 2, object3.get_symbol_value('a')) self.assertEqual(0x08000000 + 24, object3.get_symbol_value('b')) self.assertEqual(0x08000000 + 110, object3.get_symbol_value('c')) self.assertEqual(208, object3.get_section('code').size) self.assertEqual(100, object3.get_section('data').size) self.assertEqual(0x08000000, object3.get_symbol_value('codestart')) self.assertEqual(0x08000000 + 208, object3.get_symbol_value('codeend'))
def setUp(self): self.diag = DiagnosticsManager() arch = ExampleArch() self.builder = C3Builder(self.diag, arch.info) self.diag.clear() # Add a null logging handler to disable warning log messages: null_handler = logging.NullHandler() logging.getLogger().addHandler(null_handler)
def test_common_type2(self): arch = ExampleArch() context = Context(arch.info) uint16_type = self.context.get_type('uint16_t') int16_type = self.context.get_type('int16_t') # TODO: tbd, what is the result of uint16_t and int16_t? # int32_type = self.context.get_type('int32_t') common_type = self.common_type(uint16_type, int16_type) self.assertIs(int16_type, common_type)
def test_code_exceeds_memory(self): """ Check the error that is given when code exceeds memory size """ arch = ExampleArch() layout2 = layout.Layout() m = layout.Memory('flash') m.location = 0x0 m.size = 0x10 m.add_input(layout.Section('code')) layout2.add_memory(m) object1 = ObjectFile(arch) object1.get_section('code', create=True).add_data(bytes([0] * 22)) with self.assertRaisesRegex(CompilerError, 'exceeds'): link([object1], layout2)
def test_hello(self): """ Convert C to Ir, and then this IR to C """ src = r""" void printf(char*); void main(int b) { printf("Hello" "world\n"); } """ arch = ExampleArch() builder = CBuilder(arch.info, COptions()) f = io.StringIO(src) ir_module = builder.build(f, None) assert isinstance(ir_module, ir.Module) verify_module(ir_module) synthesizer = CSynthesizer() synthesizer.syn_module(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): arch = ExampleArch() self.builder = CBuilder(arch.info, COptions())
def test_binary_and_logstream(self): arch = ExampleArch() object1 = ObjectFile(arch) stream = binary_and_logging_stream(object1) stream.select_section('code') stream.emit(Mov(R1, R0))
def test_text_stream(self, mock_stdout): """ Test output to stdout """ arch = ExampleArch() stream = TextOutputStream() stream.select_section('code') stream.emit(Mov(R1, R0))
def test_dummy_stream(self): arch = ExampleArch() stream = DummyOutputStream() stream.select_section('code') stream.emit(Mov(R1, R0))
def setUp(self): self.diag = DiagnosticsManager() self.builder = PascalBuilder(self.diag, ExampleArch()) self.diag.clear()
def setUp(self): arch = ExampleArch() self.context = Context(arch.info)