Пример #1
0
def test_assembler_align():
    source = """
    addi zero zero 0
    pack <B 42
    align 4
    addi zero zero 0
    """
    binary = asm.assemble(source)
    target = b''.join([
        asm.ADDI(0, 0, 0),
        b'\x2a\x00\x00\x00',
        asm.ADDI(0, 0, 0),
    ])
    assert binary == target
Пример #2
0
def test_assembler_basic_uppercase():
    source = """
    ADDI t0 zero 1
    ADDI t1, zero, 2
    ADDI(t2, zero, 3)
    """
    binary = asm.assemble(source)
    target = b''.join([
        # can use nums OR names for registers
        asm.ADDI(5, 0, 1),
        asm.ADDI('t1', 'zero', 2),
        asm.ADDI('t2', 'zero', 3),
    ])
    assert binary == target
Пример #3
0
def test_assemble_align():
    source = r"""
    addi zero zero 0
    pack <B 42
    align 4
    addi zero zero 0
    """
    binary = asm.assemble(source)
    target = b''.join([
        struct.pack('<I', asm.ADDI(0, 0, 0)),
        b'\x2a\x00\x00\x00',
        struct.pack('<I', asm.ADDI(0, 0, 0)),
    ])
    assert binary == target
Пример #4
0
def test_assembler_constants():
    source = """
    FOO = 42
    BAR = FOO * 2
    BAZ = BAR >> 1 & 0b11111
    W = 's0'
    IP = gp
    addi zero zero BAR
    addi W IP BAZ
    """
    binary = asm.assemble(source)
    target = b''.join([
        asm.ADDI(0, 0, 84),
        asm.ADDI('s0', 'gp', 10),
    ])
    assert binary == target
Пример #5
0
def test_assemble_constants():
    source = r"""
    FOO = 42
    BAR = FOO * 2
    BAZ = BAR >> 1 & 0b11111
    W = s0
    IP = gp
    addi zero zero BAR
    addi W IP BAZ

    QMARK = '?'
    addi x0 x0 QMARK
    """
    binary = asm.assemble(source)
    target = b''.join(
        struct.pack('<I', inst) for inst in [
            asm.ADDI(0, 0, 84),
            asm.ADDI('s0', 'gp', 10),
            asm.ADDI(0, 0, ord('?')),  # ord('?') == 63
        ])
    assert binary == target
Пример #6
0
def test_assembler_modifiers():
    source = """
    ADDR = 0x20000000

    addi zero zero 0
    addi zero zero 0
    addi zero zero 0

    main:
        # without nestable exprs under hi / lo
        lui t0 %hi ADDR
        addi t0 t0 %lo(ADDR)
        addi t0 t0 main
    
        # with nestable exprs under hi / lo
        lui t0 %hi %position main ADDR
        addi t0 t0 %lo(%position(main, ADDR))
    """
    binary = asm.assemble(source)
    target = b''.join([
        asm.ADDI(0, 0, 0),
        asm.ADDI(0, 0, 0),
        asm.ADDI(0, 0, 0),
        asm.LUI('t0', asm.relocate_hi(0x20000000)),
        asm.ADDI('t0', 't0', asm.relocate_lo(0x20000000)),
        asm.ADDI('t0', 't0', 12),
        asm.LUI('t0', asm.relocate_hi(0x20000000 + 12)),
        asm.ADDI('t0', 't0', asm.relocate_lo(0x20000000 + 12)),
    ])
    assert binary == target
Пример #7
0
def test_assembler_labels_and_jumps():
    source = """
    start:
        addi t0 zero 42
        jal zero end
    middle:
        beq t0 zero main
        addi t0 t0 -1
    end:
        jal zero %offset middle
    main:
        addi zero zero 0
    """
    binary = asm.assemble(source)
    target = b''.join([
        asm.ADDI('t0', 'zero', 42),
        asm.JAL('zero', 12),
        asm.BEQ('t0', 'zero', 12),
        asm.ADDI('t0', 't0', -1),
        asm.JAL('zero', -8),
        asm.ADDI(0, 0, 0),
    ])
    assert binary == target
Пример #8
0
def test_assemble_labels_and_jumps():
    source = r"""
    start:
        addi t0 zero 42
        jal zero end
    middle:
        beq t0 zero main
        addi t0 t0 -1
    end:
        jal zero middle
    main:
        addi zero zero 0
    """
    binary = asm.assemble(source)
    target = b''.join(
        struct.pack('<I', inst) for inst in [
            asm.ADDI('t0', 'zero', 42),
            asm.JAL('zero', 12),
            asm.BEQ('t0', 'zero', 12),
            asm.ADDI('t0', 't0', -1),
            asm.JAL('zero', -8),
            asm.ADDI(0, 0, 0),
        ])
    assert binary == target
Пример #9
0
def test_addi(rd, rs1, imm, code):
    assert asm.ADDI(rd, rs1, imm) == struct.pack('<I', code)