def do(self, src, expected_output, lang="c3"): base_filename = make_filename(self.id()) bsp_c3 = relpath("..", "examples", "microblaze", "bsp.c3") crt0 = relpath("..", "examples", "microblaze", "crt0.asm") mmap = relpath("..", "examples", "microblaze", "layout.mmp") build( base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format="bin", code_image="flash", ) bin_filename = base_filename + ".bin" if has_qemu(): output = qemu([ "qemu-system-microblaze", "-nographic", "-kernel", bin_filename, ]) self.assertEqual(expected_output, output)
def do(self, src, expected_output, lang="c3"): base_filename = make_filename(self.id()) bsp_c3 = relpath("..", "examples", "xtensa", "bsp.c3") crt0 = relpath("..", "examples", "xtensa", "glue.asm") mmap = relpath("..", "examples", "xtensa", "layout.mmp") build( base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format="elf", code_image="flash", ) elf_filename = base_filename + ".elf" if has_qemu(): output = qemu([ "qemu-system-xtensa", "-nographic", "-M", "lx60", "-m", "16", "-kernel", elf_filename, ]) self.assertEqual(expected_output, output)
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) bsp_c3 = relpath('..', 'examples', 'or1k', 'bsp.c3') crt0 = relpath('..', 'examples', 'or1k', 'crt0.asm') mmap = relpath('..', 'examples', 'or1k', 'layout.mmp') build(base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format='bin', code_image='flash') binfile = base_filename + '.bin' # Create a uboot application file: with open(binfile, 'rb') as f: bindata = f.read() img_filename = base_filename + '.img' with open(img_filename, 'wb') as f: uboot_image.write_uboot_image( f, bindata, arch=uboot_image.Architecture.OPENRISC) if has_qemu(): output = qemu([ 'qemu-system-or1k', '-nographic', '-M', 'or1k-sim', '-m', '16', '-kernel', img_filename ]) self.assertEqual(expected_output, output)
def partial_build(src, lang, bsp_c3, opt_level, march, reporter): """ Compile source and return an object """ if lang == 'c3': srcs = [relpath('..', 'librt', 'io.c3'), bsp_c3, io.StringIO(src)] o2 = c3c(srcs, [], march, opt_level=opt_level, reporter=reporter, debug=True) objs = [o2] elif lang == 'bf': o3 = bfcompile(src, march, reporter=reporter) o2 = c3c([bsp_c3], [], march, reporter=reporter) objs = [o2, o3] elif lang == 'c': o2 = c3c([bsp_c3], [], march, reporter=reporter) coptions = COptions() include_path1 = relpath('..', 'librt', 'libc') coptions.add_include_path(include_path1) with open(relpath('..', 'librt', 'libc', 'lib.c'), 'r') as f: o3 = cc(f, march, coptions=coptions, debug=True, reporter=reporter) o4 = cc(io.StringIO(src), march, coptions=coptions, debug=True, reporter=reporter) objs = [o2, o3, o4] else: raise NotImplementedError('language not implemented') obj = link(objs, partial_link=True, use_runtime=True, reporter=reporter, debug=True) return obj
def build_sample_to_ir(src, lang, bsp_c3, march, reporter): """ Compile the given sample into ir-modules """ if lang == "c3": ir_modules = [ api.c3_to_ir( [bsp_c3, relpath("..", "librt", "io.c3"), io.StringIO(src)], [], march, reporter=reporter, ) ] elif lang == "bf": ir_modules = [api.bf_to_ir(src, march)] elif lang == "c": coptions = COptions() include_path1 = relpath("..", "librt", "libc") lib = relpath("..", "librt", "libc", "lib.c") coptions.add_include_path(include_path1) with open(lib, "r") as f: mod1 = api.c_to_ir(f, march, coptions=coptions, reporter=reporter) mod2 = api.c_to_ir( io.StringIO(src), march, coptions=coptions, reporter=reporter ) ir_modules = [mod1, mod2] elif lang == "pas": pascal_ir_modules = api.pascal_to_ir( [io.StringIO(src)], api.get_arch(march) ) ir_modules = pascal_ir_modules else: # pragma: no cover raise NotImplementedError("Language {} not implemented".format(lang)) return ir_modules
def test_a9_bare(self): """ Build vexpress cortex-A9 binary and emulate it """ recipe = relpath('..', 'examples', 'realview-pb-a8', 'build.xml') construct(recipe) if has_qemu(): bin_file = relpath('..', 'examples', 'realview-pb-a8', 'hello.bin') data = run_qemu(bin_file, machine='realview-pb-a8') self.assertEqual('Hello worle', data)
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) sample_filename = base_filename + '.py' list_filename = base_filename + '.html' bsp = io.StringIO(""" module bsp; public function void putc(byte c); """) march = 'arm' with HtmlReportGenerator(open(list_filename, 'w')) as reporter: if lang == 'c3': ir_modules = [ c3_to_ir([ relpath('..', 'librt', 'io.c3'), bsp, io.StringIO(src) ], [], march, reporter=reporter) ] elif lang == 'bf': ir_modules = [bf_to_ir(src, march)] elif lang == 'c': coptions = COptions() include_path1 = relpath('..', 'librt', 'libc') lib = relpath('..', 'librt', 'libc', 'lib.c') coptions.add_include_path(include_path1) with open(lib, 'r') as f: mod1 = c_to_ir(f, march, coptions=coptions, reporter=reporter) mod2 = c_to_ir(io.StringIO(src), march, coptions=coptions, reporter=reporter) ir_modules = [mod1, mod2] else: # pragma: no cover raise NotImplementedError( 'Language {} not implemented'.format(lang)) # Test roundtrip of ir_modules for ir_module in ir_modules: serialization_roundtrip(ir_module) optimize(ir_module, level=self.opt_level, reporter=reporter) with open(sample_filename, 'w') as f: ir_to_python(ir_modules, f, reporter=reporter) # Add glue: print('', file=f) print('def bsp_putc(c):', file=f) print(' print(chr(c), end="")', file=f) print('main_main()', file=f) res = run_python(sample_filename) self.assertEqual(expected_output, res)
def test_m3_bare(self): """ Build bare m3 binary and emulate it """ recipe = relpath('..', 'examples', 'lm3s6965evb', 'bare', 'build.xml') construct(recipe) if has_qemu(): bin_file = relpath('..', 'examples', 'lm3s6965evb', 'bare', 'bare.bin') data = run_qemu(bin_file) self.assertEqual('Hello worle', data)
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) bsp_c3 = relpath('..', 'examples', 'stm8', 'bsp.c3') crt0 = relpath('..', 'examples', 'stm8', 'start.asm') mmap = relpath('..', 'examples', 'avr', 'avr.mmap') build(base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format='hex', code_image='flash')
def do(self, src, expected_output, lang="c3"): # Construct binary file from snippet: bsp_c3 = relpath("..", "examples", "realview-pb-a8", "arch.c3") startercode = io.StringIO(self.startercode) base_filename = make_filename(self.id()) obj = build( base_filename, src, bsp_c3, startercode, self.march, self.opt_level, io.StringIO(self.arch_mmap), lang=lang, bin_format="bin", ) sample_filename = base_filename + ".bin" # Run bin file in emulator: if has_qemu(): res = qemu([ "qemu-system-arm", "--machine", "realview-pb-a8", "-m", "16M", "-nographic", "-kernel", sample_filename, ]) self.assertEqual(expected_output, res)
def do(self, src, expected_output, lang="c3"): base_filename = make_filename(self.id()) bsp_c3 = relpath("..", "examples", "msp430", "bsp.c3") startercode = io.StringIO(self.startercode) obj = build( base_filename, src, bsp_c3, startercode, "msp430", self.opt_level, io.StringIO(self.arch_mmap), lang=lang, ) flash = obj.get_image("flash") ivect = obj.get_image("vector16") rom = merge_memories(flash, ivect, "rom") rom_data = rom.data assert len(rom_data) % 2 == 0 with open(base_filename + ".bin", "wb") as f: f.write(rom_data) mem_file = base_filename + ".mem" with open(mem_file, "w") as f: for i in range(len(rom_data) // 2): w = rom_data[2 * i:2 * i + 2] print("%02x%02x" % (w[1], w[0]), file=f) if has_iverilog() and do_iverilog(): res = run_msp430(mem_file) self.assertEqual(expected_output, res)
def test_hexutil_address_format(self, mock_stderr): file1 = new_temp_file('.hex') datafile = relpath('..', 'examples', 'build.xml') with self.assertRaises(SystemExit) as cm: hexutil(['new', file1, '10000000', datafile]) self.assertEqual(2, cm.exception.code) self.assertIn('argument address', mock_stderr.getvalue())
def test_command(self, mock_stderr): obj_file = new_temp_file('.obj') src = relpath('..', 'examples', 'avr', 'arduino-blinky', 'boot.asm') asm(['-m', 'avr', '-o', obj_file, src]) with patch('sys.stdout', new_callable=io.StringIO) as mock_stdout: objdump([obj_file]) self.assertIn('SECTION', mock_stdout.getvalue())
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) bsp_c3 = relpath('..', 'examples', 'msp430', 'bsp.c3') startercode = io.StringIO(self.startercode) obj = build(base_filename, src, bsp_c3, startercode, "msp430", self.opt_level, io.StringIO(self.arch_mmap), lang=lang) flash = obj.get_image('flash') ivect = obj.get_image('vector16') rom = merge_memories(flash, ivect, 'rom') rom_data = rom.data assert len(rom_data) % 2 == 0 with open(base_filename + '.bin', 'wb') as f: f.write(rom_data) mem_file = base_filename + '.mem' with open(mem_file, 'w') as f: for i in range(len(rom_data) // 2): w = rom_data[2 * i:2 * i + 2] print('%02x%02x' % (w[1], w[0]), file=f) if has_iverilog() and do_iverilog(): res = run_msp430(mem_file) self.assertEqual(expected_output, res)
def build_sample_to_code(src, lang, bsp_c3, opt_level, march, debug, reporter): """ Turn example sample into code objects. """ if lang == "c3": srcs = [relpath("..", "librt", "io.c3"), bsp_c3, io.StringIO(src)] o2 = api.c3c( srcs, [], march, opt_level=opt_level, reporter=reporter, debug=debug, ) objs = [o2] elif lang == "bf": o3 = api.bfcompile(src, march, reporter=reporter) o2 = api.c3c([bsp_c3], [], march, reporter=reporter) objs = [o2, o3] elif lang == "c": o2 = api.c3c([bsp_c3], [], march, reporter=reporter) coptions = COptions() libc_path = relpath("..", "librt", "libc") include_path1 = os.path.join(libc_path, "include") coptions.add_include_path(include_path1) with open(relpath("..", "librt", "libc", "lib.c"), "r") as f: o3 = api.cc(f, march, coptions=coptions, debug=debug, reporter=reporter) o4 = api.cc( io.StringIO(src), march, coptions=coptions, debug=debug, reporter=reporter, ) objs = [o2, o3, o4] elif lang == "pas": o3 = api.pascal([io.StringIO(src)], march, reporter=reporter, debug=debug) o2 = api.c3c([bsp_c3], [], march, reporter=reporter) objs = [o2, o3] else: raise NotImplementedError("language not implemented") return objs
def do(self, src, expected_output, lang="c3"): base_filename = make_filename(self.id()) bsp_c3 = relpath("..", "examples", "stm8", "bsp.c3") crt0 = relpath("..", "examples", "stm8", "start.asm") mmap = relpath("..", "examples", "avr", "avr.mmap") build( base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format="hex", code_image="flash", )
def test_normal_use(self, mock_stdout, mock_stderr): """ Test normal yacc use """ grammar_file = relpath('..', 'ppci', 'codegen', 'burg.grammar') file1 = new_temp_file('.py') yacc([grammar_file, '-o', file1]) with open(file1, 'r') as f: content = f.read() self.assertIn('Automatically generated', content)
def deco(cls): for folder in folders: for source in source_files(relpath('data', folder), '.ll'): tf = create_test_function(source) basename = os.path.basename(source) func_name = 'test_' + os.path.splitext(basename)[0] assert not hasattr(cls, func_name) setattr(cls, func_name, tf) return cls
def do(self, src, expected_output, lang="c3"): base_filename = make_filename(self.id()) bsp_c3 = relpath("..", "examples", "or1k", "bsp.c3") crt0 = relpath("..", "examples", "or1k", "crt0.asm") mmap = relpath("..", "examples", "or1k", "layout.mmp") build( base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format="bin", code_image="flash", ) binfile = base_filename + ".bin" # Create a uboot application file: with open(binfile, "rb") as f: bindata = f.read() img_filename = base_filename + ".img" with open(img_filename, "wb") as f: uboot_image.write_uboot_image( f, bindata, arch=uboot_image.Architecture.OPENRISC) qemu_cmd = [ "qemu-system-or1k", "-nographic", "-M", "or1k-sim", "-m", "16", "-kernel", img_filename, ] create_qemu_launch_script(base_filename + ".sh", qemu_cmd) if has_qemu(): output = qemu(qemu_cmd) self.assertEqual(expected_output, output)
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) bsp_c3 = relpath('..', 'examples', 'avr', 'bsp.c3') crt0 = relpath('..', 'examples', 'avr', 'glue.asm') mmap = relpath('..', 'examples', 'avr', 'avr.mmap') build(base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format='hex', code_image='flash') hexfile = base_filename + '.hex' print(hexfile) if has_avr_emulator() and do_iverilog(): res = run_avr(hexfile) self.assertEqual(expected_output, res)
def test_hexutil_merge(self, mock_stdout): """ Create three hexfiles and manipulate those """ file1 = new_temp_file('file1.hex') file2 = new_temp_file('file2.hex') file3 = new_temp_file('file3.hex') datafile = relpath('..', 'docs', 'logo', 'logo.png') hexutil(['new', file1, '0x10000000', datafile]) hexutil(['new', file2, '0x20000000', datafile]) hexutil(['merge', file1, file2, file3]) hexutil(['info', file3]) self.assertIn("Hexfile containing 2832 bytes", mock_stdout.getvalue())
def deco(cls): for folder in folders: for source in source_files(relpath("samples", folder), extensions): output = os.path.splitext(source)[0] + ".out" basename = os.path.basename(source) name, lang = os.path.splitext(basename) lang = lang[1:] func_name = "test_" + name tf = create_test_function(source, output, lang) assert not hasattr(cls, func_name) setattr(cls, func_name, tf) return cls
def test_error_reporting(self, mock_stdout): """ Simulate some errors into the diagnostics system """ filename = relpath('..', 'examples', 'src', 'snake', 'game.c3') diag = DiagnosticsManager() with open(filename, 'r') as f: src = f.read() diag.add_source(filename, src) diag.error('Test1', SourceLocation(filename, 1, 2, 1)) diag.error('Test2', SourceLocation(filename, 1000, 2, 1)) diag.error('Test2', SourceLocation("other.c", 1000, 2, 1)) diag.error('Test3', None) diag.print_errors()
def do(self, src, expected_output, lang="c3"): base_filename = make_filename(self.id()) bsp_c3 = relpath("..", "examples", "avr", "bsp.c3") crt0 = relpath("..", "examples", "avr", "glue.asm") mmap = relpath("..", "examples", "avr", "avr.mmap") build( base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format="hex", code_image="flash", ) hexfile = base_filename + ".hex" print(hexfile) if has_avr_emulator() and do_iverilog(): res = run_avr(hexfile) self.assertEqual(expected_output, res)
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) bsp_c3 = relpath('..', 'examples', 'microblaze', 'bsp.c3') crt0 = relpath('..', 'examples', 'microblaze', 'crt0.asm') mmap = relpath('..', 'examples', 'microblaze', 'layout.mmp') build(base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format='bin', code_image='flash') bin_filename = base_filename + '.bin' if has_qemu(): output = qemu([ 'qemu-system-microblaze', '-nographic', '-kernel', bin_filename ]) self.assertEqual(expected_output, output)
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) bsp_c3 = relpath('..', 'examples', 'xtensa', 'bsp.c3') crt0 = relpath('..', 'examples', 'xtensa', 'glue.asm') mmap = relpath('..', 'examples', 'xtensa', 'layout.mmp') build(base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format='elf', code_image='flash') elf_filename = base_filename + '.elf' if has_qemu(): output = qemu([ 'qemu-system-xtensa', '-nographic', '-M', 'lx60', '-m', '16', '-kernel', elf_filename ]) self.assertEqual(expected_output, output)
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) bsp_c3 = relpath('..', 'examples', 'xtensa', 'bsp.c3') crt0 = relpath('..', 'examples', 'xtensa', 'glue.asm') mmap = relpath('..', 'examples', 'xtensa', 'layout.mmp') build(base_filename, src, bsp_c3, crt0, self.march, self.opt_level, mmap, lang=lang, bin_format='bin', code_image='flash') binfile = base_filename + '.bin' img_filename = base_filename + '.img' self.make_image(binfile, img_filename) if has_qemu(): output = qemu([ 'qemu-system-xtensa', '-nographic', '-M', 'lx60', '-m', '16', '-drive', 'if=pflash,format=raw,file={}'.format(img_filename) ]) self.assertEqual(expected_output, output)
def test_command(self, mock_stdout, mock_stderr): obj1 = new_temp_file('.obj') obj2 = new_temp_file('.obj') obj3 = new_temp_file('.obj') asm_src = relpath('..', 'examples', 'lm3s6965evb', 'startup.asm') mmap = relpath('..', 'examples', 'lm3s6965evb', 'memlayout.mmap') c3_srcs = [ relpath('..', 'examples', 'src', 'snake', 'main.c3'), relpath('..', 'examples', 'src', 'snake', 'game.c3'), relpath('..', 'librt', 'io.c3'), relpath('..', 'examples', 'lm3s6965evb', 'bsp.c3'), ] asm(['-m', 'arm', '--mtune', 'thumb', '-o', obj1, asm_src]) c3c(['-m', 'arm', '--mtune', 'thumb', '-o', obj2] + c3_srcs) link(['-o', obj3, '-L', mmap, obj1, obj2])
class CcTestCase(unittest.TestCase): """ Test the cc command-line utility """ c_file = relpath('..', 'examples', 'c', 'hello', 'std.c') @patch('sys.stdout', new_callable=io.StringIO) @patch('sys.stderr', new_callable=io.StringIO) def test_cc_command(self, mock_stdout, mock_stderr): """ Capture stdout. Important because it is closed by the command! """ oj_file = new_temp_file('.oj') cc(['-m', 'arm', self.c_file, '-o', oj_file]) @patch('sys.stdout', new_callable=io.StringIO) @patch('sys.stderr', new_callable=io.StringIO) def test_cc_command_s(self, mock_stdout, mock_stderr): """ Capture stdout. Important because it is closed by the command! """ oj_file = new_temp_file('.oj') cc(['-m', 'arm', '-S', self.c_file, '-o', oj_file]) @patch('sys.stdout', new_callable=io.StringIO) @patch('sys.stderr', new_callable=io.StringIO) def test_cc_command_e(self, mock_stdout, mock_stderr): """ Capture stdout. Important because it is closed by the command! """ oj_file = new_temp_file('.oj') cc(['-m', 'arm', '-E', self.c_file, '-o', oj_file]) @patch('sys.stdout', new_callable=io.StringIO) @patch('sys.stderr', new_callable=io.StringIO) def test_cc_command_ir(self, mock_stdout, mock_stderr): """ Capture stdout. Important because it is closed by the command! """ oj_file = new_temp_file('.oj') cc(['-m', 'arm', '--ir', self.c_file, '-o', oj_file]) @patch('sys.stdout', new_callable=io.StringIO) def test_cc_command_help(self, mock_stdout): with self.assertRaises(SystemExit) as cm: cc(['-h']) self.assertEqual(0, cm.exception.code) self.assertIn('compiler', mock_stdout.getvalue())
def do(self, src, expected_output, lang="c3"): # Construct binary file from snippet: bsp_c3 = relpath('..', 'examples', 'realview-pb-a8', 'arch.c3') startercode = io.StringIO(self.startercode) base_filename = make_filename(self.id()) obj = build(base_filename, src, bsp_c3, startercode, self.march, self.opt_level, io.StringIO(self.arch_mmap), lang=lang, bin_format='bin') sample_filename = base_filename + '.bin' # Run bin file in emulator: if has_qemu(): res = qemu([ 'qemu-system-arm', '--machine', 'realview-pb-a8', '-m', '16M', '-nographic', '-kernel', sample_filename ]) self.assertEqual(expected_output, res)