def compile_nos_for_riscv(): """ Compile nOS for riscv architecture. """ logging.basicConfig(level=logging.INFO) murax_folder = os.path.join(this_dir, "..", "examples", "riscvmurax") arch = api.get_arch("riscv") # Gather sources: path = os.path.join(murax_folder, "csrc", "nos") folders, srcs = get_sources(path, "*.c") folders += [os.path.join(murax_folder, "csrc")] print(srcs) coptions = COptions() for folder in folders: coptions.add_include_path(folder) # Build code: o1 = api.asm(os.path.join(murax_folder, "start.s"), arch) o2 = api.asm(os.path.join(murax_folder, "nOSPortasm.s"), arch) objs = [o1, o2] for src in srcs: with open(src) as f: objs.append(api.cc(f, "riscv", coptions=coptions, debug=True)) # Link code: api.link( objs, os.path.join(murax_folder, "firmware.mmap"), use_runtime=True, debug=True, )
def test_exit42(self): """ ; exit with code 42: ; syscall 60 = exit, rax is first argument, rdi second """ src = io.StringIO(""" section code mov rax, 60 mov rdi, 42 syscall """) mmap = """ MEMORY code LOCATION=0x40000 SIZE=0x10000 { SECTION(code) } """ obj = asm(src, 'x86_64') handle, exe = mkstemp() os.close(handle) obj2 = link([obj], layout=io.StringIO(mmap)) objcopy(obj2, 'prog', 'elf', exe) if hasattr(subprocess, 'TimeoutExpired'): returncode = subprocess.call(exe, timeout=10) else: returncode = subprocess.call(exe) self.assertEqual(42, returncode)
def generate_code(module): """ Generate code """ march = 'x86_64' obj1 = api.ir_to_object([module], march) obj2 = api.asm('crt0.asm', march) obj3 = api.c3c(['crt0.c3'], [], march) obj = api.link([obj1, obj2, obj3]) with open('test.oj', 'w') as f: obj.save(f)
def dump_asm( code_input: str, verbose: bool = False, ) -> List[int]: obj = asm(io.StringIO(code_input), "riscv") # NOQA obj = link([obj]) code = obj.get_section("code").data code = bytes_to_u32_arr(code) dump_instrs(code) dump_asm_to_S_file(code, verbose=verbose) return code
def perform_test(filename): """ Try to compile the given snippet. """ logger.info("Step 1: Compile %s!", filename) march = "x86_64" html_report = os.path.splitext(filename)[0] + "_report.html" coptions = COptions() root_folder = os.path.join(this_dir, "..", "..", "..") libc_folder = os.path.join(root_folder, "librt", "libc") libc_include = os.path.join(libc_folder, "include") coptions.add_include_path(libc_include) # TODO: this should be injected elsewhere? coptions.add_define('__LP64__', '1') # coptions.enable('freestanding') with html_reporter(html_report) as reporter: with open(filename, "r") as f: try: obj1 = api.cc(f, march, coptions=coptions, reporter=reporter) except CompilerError as ex: ex.print() raise logger.info("Compilation complete, %s", obj1) obj0 = api.asm(io.StringIO(STARTERCODE), march) obj2 = api.c3c([io.StringIO(BSP_C3_SRC)], [], march) with open(os.path.join(libc_include, "lib.c"), "r") as f: obj3 = api.cc(f, march, coptions=coptions) obj = api.link([obj0, obj1, obj2, obj3], layout=io.StringIO(ARCH_MMAP)) logger.info("Step 2: Run it!") exe_filename = os.path.splitext(filename)[0] + "_executable.elf" with open(exe_filename, "wb") as f: write_elf(obj, f, type="executable") api.chmod_x(exe_filename) logger.info("Running %s", exe_filename) test_prog = subprocess.Popen(exe_filename, stdout=subprocess.PIPE) exit_code = test_prog.wait() assert exit_code == 0 captured_stdout = test_prog.stdout.read().decode("ascii") with open(filename + ".expected", "r") as f: expected_stdout = f.read() # Compare stdout: assert captured_stdout == expected_stdout
def dump_asm(string, verbose=False): # from ppci.common import CompilerError obj = asm(string, 'riscv') # try: # obj = asm(source_file, 'riscv') # print("ASM succeeded...") # except CompilerError: # print("ASM didn't work, trying CC...") # obj = cc(source_file, 'riscv') obj = link([obj]) # print(obj.sections) code = obj.get_section('code').data code = bytes_to_u32_arr(code) dump_instrs(code) dump_asm_to_S_file(code, verbose=verbose) return code
def build( base_filename, src, bsp_c3, crt0_asm, march, opt_level, mmap, lang="c3", bin_format=None, elf_format=None, code_image="code", ): """ Construct object file from source snippet """ list_filename = base_filename + ".html" with html_reporter(list_filename) as reporter: objs = build_sample_to_code(src, lang, bsp_c3, opt_level, march, True, reporter) o1 = api.asm(crt0_asm, march) objs.append(o1) obj = api.link(objs, layout=mmap, use_runtime=True, reporter=reporter, debug=True) # Save object: obj_file = base_filename + ".oj" with open(obj_file, "w") as f: obj.save(f) if elf_format: elf_filename = base_filename + "." + elf_format api.objcopy(obj, code_image, elf_format, elf_filename) # Export code image to some format: if bin_format: sample_filename = base_filename + "." + bin_format api.objcopy(obj, code_image, bin_format, sample_filename) return obj
coptions.add_include_path(libc_folder) coptions.add_define('COMPILER_VERSION', '"ppci {}"'.format(ppci_version)) coptions.add_define('FLAGS_STR', '"-O{}"'.format(opt_level)) # Prevent malloc / free usage: coptions.add_define('MEM_METHOD', 'MEM_STATIC') # TODO: Hack to enable %f formatting: coptions.add_define('__x86_64__', '1') objs = [] crt0_asm = os.path.join(linux64_folder, 'glue.asm') crt0_c3 = os.path.join(linux64_folder, 'bsp.c3') linker_script = os.path.join(linux64_folder, 'linux64.mmap') objs.append(api.asm(crt0_asm, march)) objs.append(api.c3c([crt0_c3], [], march)) objs.append(api.cc(io.StringIO(hacked_libc_extras), march, coptions=coptions)) sources = list(glob.glob(os.path.join(core_mark_folder, '*.c'))) sources.extend(glob.glob(os.path.join(port_folder, '*.c'))) sources.extend(glob.glob(os.path.join(libc_folder, '*.c'))) for source_file in sources: print(source_file) try: with open(source_file, 'r') as f: obj = api.cc(f, march, coptions=coptions, opt_level=opt_level) except CompilerError as ex: print('ERROR!') print(ex)
""" Takes a web assembly module and turns it into riscv code """ import logging from ppci.api import asm, c3c, link, get_arch, wasmcompile from ppci.binutils.objectfile import merge_memories from ppci.utils.reporting import html_reporter logging.basicConfig(level=logging.INFO) arch = get_arch('riscv') obj1 = asm("start.s", arch) with html_reporter('report.html') as reporter: srcs = ['../src/wasm_fac/main.c3', '../../librt/io.c3', 'c3src/bsp.c3'] obj2 = c3c(srcs, [], arch, reporter=reporter) with open('../src/wasm_fac/fact.wasm', 'rb') as f2: obj3 = wasmcompile(f2, arch) obj = link([obj1, obj2, obj3], "firmware.mmap", use_runtime=True, reporter=reporter, debug=True) size = 0x2000 cimg = obj.get_image('flash') dimg = obj.get_image('ram') img = merge_memories(cimg, dimg, 'img') imgdata = img.data with open("firmware.hex", "w") as f: for i in range(size): if i < len(imgdata) // 4:
def build(base_filename, src, bsp_c3, crt0_asm, march, opt_level, mmap, lang='c3', bin_format=None, elf_format=None, code_image='code'): """ Construct object file from source snippet """ list_filename = base_filename + '.html' with HtmlReportGenerator(open(list_filename, 'w')) as reporter: o1 = asm(crt0_asm, march) 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 = [o1, o2] elif lang == 'bf': o3 = bfcompile(src, march, reporter=reporter) o2 = c3c([bsp_c3], [], march, reporter=reporter) objs = [o1, 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, reporter=reporter) o4 = cc(io.StringIO(src), march, coptions=coptions, reporter=reporter) objs = [o1, o2, o3, o4] else: raise NotImplementedError('language not implemented') obj = link(objs, layout=mmap, use_runtime=True, reporter=reporter, debug=True) # Save object: obj_file = base_filename + '.oj' with open(obj_file, 'w') as f: obj.save(f) if elf_format: elf_filename = base_filename + '.' + elf_format objcopy(obj, code_image, elf_format, elf_filename) # Export code image to some format: if bin_format: sample_filename = base_filename + '.' + bin_format objcopy(obj, code_image, bin_format, sample_filename) return obj
def cc(filename): logging.info('Compiling %s', filename) with open(os.path.join(this_dir, filename)) as f: try: obj = api.cc(f, arch, reporter=reporter, coptions=coptions) logging.info('Compiled %s into %s bytes', filename, obj.byte_size) except CompilerError as e: print(e) e.print() obj = None return obj file_list = ['xprintf.c', 'loader.c', 'ff.c', 'sdmm.c'] objs = [cc(f) for f in file_list] objs = [api.asm("start.s", arch)] + objs print(objs) obj = api.link(objs, "firmware.mmap", use_runtime=True, reporter=reporter, debug=True) of = open("firmware.tlf", "w") obj.save(of) of.close() api.objcopy(obj, "flash", "bin", "code.bin") api.objcopy(obj, "ram", "bin", "data.bin") cimg = obj.get_image('flash') dimg = obj.get_image('ram') img = merge_memories(cimg, dimg, 'img') imgdata = img.data
parser.add_argument("example", help="example name from the c3src directory") parser.add_argument( "-g", "--debug", action="store_true", default=False, help="Enable debug code", ) args = parser.parse_args() loglevel = logging.DEBUG if args.v else logging.INFO logging.basicConfig(level=loglevel, filename="debug.log") with html_reporter("report.html") as reporter: arch = get_arch("riscv") if args.debug: obj1 = asm("startdbg.s", arch) else: obj1 = asm("start.s", arch) c3_sources = [ os.path.join("c3src", "bsp.c3"), os.path.join("c3src", "io.c3"), os.path.join("c3src", args.example, "main.c3"), ] if args.debug: c3_sources.append(os.path.join("c3src", "gdbstub.c3")) c3_sources.append(os.path.join("c3src", "irq.c3")) obj2 = c3c( c3_sources, [], "riscv", reporter=reporter, debug=True, opt_level=2 )
lhs = self.emit(ir.Binop(lhs, '*', rhs, 'prod', self.int_type)) product.ir_value = lhs def get_value(self, value): if isinstance(value, int): ir_value = self.emit(ir.Const(value, 'constant', self.int_type)) elif isinstance(value, str): ir_value = self.load_var(value) else: ir_value = value.ir_value return ir_value def load_var(self, var_name): mem_loc = self.variables[var_name] return self.emit(ir.Load(mem_loc, var_name, self.int_type)) tcfCompiler = TcfCompiler() ir_module = tcfCompiler.compile('example.tcf') obj1 = api.ir_to_object([ir_module], 'x86_64') obj2 = api.c3c(['bsp.c3', 'io.c3'], [], 'x86_64') obj3 = api.asm('linux.asm', 'x86_64') obj = api.link([obj1, obj2, obj3], layout='layout.mmap') print(obj) with open('example.oj', 'w') as f: obj.save(f) api.objcopy(obj, 'code', 'elf', 'example')
from ppci import api march = api.get_arch('riscv') with open('boot0.asm', 'r') as f: obj1 = api.asm(f, march) with open('sifive_u.mmap', 'r') as f: obj = api.link([obj1], layout=f) print(obj) with open('kernel.elf', 'wb') as f: api.write_elf(obj, f)
import logging from ppci import api logging.basicConfig(level=logging.INFO) arch = api.get_arch('x86_64:wincc') obj1 = api.c3c( ['../src/hello/hello.c3', '../../librt/io.c3', 'bsp.c3', 'kernel32.c3'], [], arch) with open('kernel32.s', 'r') as f: obj2 = api.asm(f, arch) obj = api.link([obj1, obj2], partial_link=True) print(obj) api.objcopy(obj, 'code', 'exe', 'hello.exe')
from ppci.utils.reporting import html_reporter def get_sources(folder, extension): resfiles = [] resdirs = [] for x in os.walk(folder): for y in glob(os.path.join(x[0], extension)): resfiles.append(y) resdirs.append(x[0]) return ((resdirs, resfiles)) with html_reporter('report.html') as reporter: arch = get_arch('riscv') o1 = asm("start.s", arch) o2 = asm("nOSPortasm.s", arch) path = os.path.join('.', 'csrc', argv[1]) dirs, srcs = get_sources(path, '*.c') #srcs += [os.path.join('.','csrc','bsp.c')] + [os.path.join('.','csrc','lib.c')] dirs += [os.path.join('.', 'csrc')] obj = [] coptions = COptions() for dir in dirs: coptions.add_include_path(dir) for src in srcs: with open(src) as f: obj.append( cc(f, "riscv", coptions=coptions,
} """ layout = """ MEMORY flash LOCATION=0x100 SIZE=0x800 { SECTION(reset) SECTION(code) } MEMORY ram LOCATION=0x001000 SIZE=0x800 { SECTION(data) } """ obj1 = api.asm(io.StringIO(boot_src), 'or1k') with open('report.html', 'w') as f: obj2 = api.c3c([io.StringIO(src)], [], march='or1k', opt_level=2, reporter=HtmlReportGenerator(f)) print('Bootcode:', obj1) print('Program code:', obj2) obj = api.link([obj1, obj2], layout=io.StringIO(layout)) print('Total:', obj) # api.objcopy(obj, 'flash', 'bin', 'baremetal.bin') # Create uboot image: data = obj.get_image('flash').data with open('baremetal.bin', 'wb') as f:
inc 0xd005 inc 0xd007 inc 0xd009 inc 0xd00b inc 0xd00d inc 0xd00f rts """ # sprite = bytes(range(63)) sprite = make_sprite() print(sprite) f = io.StringIO(asm_src) oj = api.asm(f, march) print(oj) layout = Layout() ram = Memory('ram') ram.location = 0x890 ram.size = 0x1000 ram.add_input(Section('code')) layout.add_memory(ram) oj = api.link([oj], layout=layout) asm_bin = oj.get_image('ram').data print(oj, asm_bin) if len(sys.argv) > 1:
obj = cc(io.StringIO(source), 'm68k') obj = link([obj]) print(obj) print(""" Please now open the online disassembler: https://onlinedisassembler.com/odaweb/ Select m68k:68000 and paste: {} """.format(obj.get_section('code').data.hex())) # TODO: with open('demo.srec', 'w') as f: write_srecord(obj, f) obj = asm('amiga_hello_world.asm', 'm68k') obj = link([obj]) print(obj) print(obj.get_section('code').data.hex()) write_hunk('demo', obj.get_section('code').data)
if len(sys.argv) > 1: with open(sys.argv[1], 'r') as f: text_message = f.read() else: text_message = 'you can provide a text file to customize this message' with html_reporter('report.html') reporter: with open('add.c') as f: oj = api.cc(f, arch, reporter=reporter) print(oj) with open('hello.s') as f: oj = api.asm(f, arch) oj = api.link([oj], layout='layout.mmp') print(oj) with open(os.path.join('c64disk', 'hello.prg'), 'wb') as f: # Generate tokenized basic: load_address = 0x801 basic_program = bytes([ 0x01, 0x08, # Load address 0x09, 0x08, # start of the next line 0x0a, 0x00, # line number 10 word in little endianness # 0x9e, 0x20, 0x34, 0x30, 0x39, 0x36, 0x00, # SYS 4096 0x99, # Print token! 0x20, 0x31, # Space and number 1 0x0, # End of line 0x1a, 0x00, # 0x1a line number in little endianness
from ppci.lang.c import COptions from ppci.utils.reporting import html_reporter def get_sources(folder, extension): resfiles = [] resdirs = [] for x in os.walk(folder): for y in glob(os.path.join(x[0], extension)): resfiles.append(y) resdirs.append(x[0]) return((resdirs, resfiles)) with html_reporter('report.html', 'w') as reporter: arch = get_arch('riscv') o1 = asm("src/crt1.s", arch) path = os.path.join('.','src',argv[1]) dirs, srcs = get_sources(path, '*.c') dirs += [os.path.join('.','src')] obj = [] coptions = COptions() for dir in dirs: coptions.add_include_path(dir) for src in srcs: with open(src) as f: obj.append(cc(f, "riscv", coptions=coptions, debug=True, reporter=reporter)) obj = link([o1] + obj, "firmware.mmap", use_runtime=True, reporter=reporter, debug=True) with open("firmware.oj", "w") as of: obj.save(of)
def get_sources(folder, extension): resfiles = [] resdirs = [] for x in os.walk(folder): for y in glob(os.path.join(x[0], extension)): resfiles.append(y) resdirs.append(x[0]) return((resdirs, resfiles)) with open('report.html', 'w') as f: archname = 'riscv:rva' if (len(argv)==4): archname +=':' + argv[3] arch = get_arch(archname) o1 = asm("sw/" + argv[1] + "/src/crt1.s", arch) reporter = HtmlReportGenerator(f) path = os.path.join('.','sw',argv[1],'src',argv[2]) dirs, srcs = get_sources(path, '*.c') dirs += [os.path.join('.','sw',argv[1],'src')] srcs += [os.path.join('.','sw',argv[1],'src','bsp.c')] + [os.path.join('.','sw',argv[1],'src','lib.c')] obj = [] coptions = COptions() for dir in dirs: coptions.add_include_path(dir) for src in srcs: with open(src) as f: obj.append(cc(f, archname, coptions=coptions, debug=True, reporter=reporter)) obj = link([o1] + obj, "./sw/"+argv[1]+"/firmware.mmap", use_runtime=True, reporter=reporter, debug=True) cimg = obj.get_image('flash')
; imm prefix to load 0x8400 0004 into r6: imm 0x8400 addik r6, r0, 4 ; Write word (mem[r6+r0] = r5 sw r5, r6, r0 end_label: bri end_label ; Endless loop! section data """ layout = get_layout('../layout.mmp') obj = asm(io.StringIO(src), arch) obj = link([obj], layout=layout) print(obj) objcopy(obj, 'flash', 'bin', 'baremetal.bin') addik = instructions.Addik(registers.R5, registers.R0, 0x41) print(addik.encode().hex()) add = instructions.Add(registers.R2, registers.R5, registers.R7) print(add.encode().hex()) ts = add.get_tokens() print(ts, ts[0].bit_value) add.set_all_patterns(ts) print(ts, hex(ts[0].bit_value))
from ppci.lang.c import COptions from ppci.utils.reporting import html_reporter def get_sources(folder, extension): resfiles = [] resdirs = [] for x in os.walk(folder): for y in glob(os.path.join(x[0], extension)): resfiles.append(y) resdirs.append(x[0]) return((resdirs, resfiles)) with html_reporter('report.html') as reporter: arch = get_arch('riscv') o1 = asm("startdbg.s", arch) path = os.path.join('.','csrc',argv[1]) dirs, srcs = get_sources(path, '*.c') srcs += [os.path.join('.','csrc','bsp.c')] + [os.path.join('.','csrc','lib.c')] + [os.path.join('.','csrc','gdbstub.c')] dirs += [os.path.join('.','csrc')] obj = [] coptions = COptions() for dir in dirs: coptions.add_include_path(dir) for src in srcs: with open(src) as f: obj.append(cc(f, "riscv", coptions=coptions, debug=True, reporter=reporter)) obj = link([o1] + obj, "firmware.mmap", use_runtime=True, reporter=reporter, debug=True) with open("firmware.oj", "w") as of: obj.save(of)