コード例 #1
0
 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)
コード例 #2
0
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
コード例 #3
0
; 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))
コード例 #4
0
ファイル: mkfwc.py プロジェクト: ttwj/ppci
            obj.append(
                cc(f,
                   "riscv",
                   coptions=coptions,
                   debug=True,
                   reporter=reporter))
    obj = link([o1, o2] + obj,
               "firmware.mmap",
               use_runtime=True,
               reporter=reporter,
               debug=True)

    with open("firmware.oj", "w") as of:
        obj.save(of)

    objcopy(obj, "flash", "elf", "firmware.elf")
    objcopy(obj, "flash", "bin", "code.bin")
    objcopy(obj, "ram", "bin", "data.bin")
    size = 0x8000
    cimg = obj.get_image('flash')
    dimg = obj.get_image('ram')
    img = merge_memories(cimg, dimg, 'img')
    imgdata = img.data

with open('Murax.bin', 'wb') as f:
    f.write(imgdata)

assert len(imgdata) % 4 == 0

f0 = open("Murax.v_toplevel_system_ram_ram_symbol0" + ".bin", "w")
f1 = open("Murax.v_toplevel_system_ram_ram_symbol1" + ".bin", "w")
コード例 #5
0
            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')
コード例 #6
0
  (func $fac-f32 (export "fac-f32") (type $over-f32)
    (if (result f32) (f32.eq (local.get 0) (f32.const 0.0))
      (then (f32.const 1.0))
      (else
        (f32.mul
          (local.get 0)

          (call $fac-f32
            (f32.sub (local.get 0) (f32.const 1.0))
          )
        )
      )
    )
  )
)
"""
m = Module(src)

obj = api.wasmcompile(src, 'x86_64', opt_level=0)
api.objcopy(obj, None, 'elf', 'fac_f32.o')

print(m.to_string())

inst = instantiate(m, {})
inst2 = instantiate(m, {}, target='python')

for number in [1.0, 5.0, 10.0]:
    print('number', number, 'fac', inst.exports['fac-f32'](number),
          'fac_python', inst2.exports['fac-f32'](number))
コード例 #7
0
ファイル: test_api.py プロジェクト: vpoulailleau/ppci
 def test_wrong_format(self):
     with self.assertRaises(TaskError):
         objcopy(None, None, 'invalid_format', None)
コード例 #8
0
ファイル: compile_mandelbrot.py プロジェクト: michg/ppci
        "mov rdi, %1 \n"
        "mov rsi, %2 \n"
        "mov rdx, %3 \n"
        "syscall \n"
        :
        : "r" (nr), "r" (a), "r" (b), "r" (c) // input registers, patched into %0 .. %3
        : "rax", "rdi", "rsi", "rdx"  // clobber registers, handled by the register allocator
    );
}

"""

layout = """
ENTRY(main)

MEMORY code LOCATION=0x40000 SIZE=0x10000 {
    SECTION(code)
}

MEMORY ram LOCATION=0x20000000 SIZE=0xA000 {
    SECTION(data)
}
"""

obj_glue = cc(io.StringIO(c_glue), 'x86_64')

obj = link([obj_py, obj_glue], layout=io.StringIO(layout))
print(obj)

objcopy(obj, None, "elf", "mandelbrot")
コード例 #9
0
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')
コード例 #10
0
                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
    f = open("firmware.hex", "w")
    size = 0x8000
    for i in range(size):
        if i < len(imgdata) // 4:
            w = imgdata[4 * i:4 * i + 4]
            print("%02x%02x%02x%02x" % (w[3], w[2], w[1], w[0]), file=f)
        else:
            print("00000000", file=f)
    f.close()
コード例 #11
0
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