source/projects/shoggoth/test/ichor/test_assembler.py

63 lines
1.3 KiB
Python
Raw Normal View History

2022-07-02 06:35:03 +00:00
#!/usr/bin/env python3
2022-08-09 15:39:33 +00:00
from ichor import isa
from ichor.assembler import FuncBuilder
2022-07-02 06:35:03 +00:00
import pytest
2022-07-16 01:37:34 +00:00
2022-07-02 06:35:03 +00:00
@pytest.fixture
def builder() -> FuncBuilder:
return FuncBuilder()
def test_forwards_label(builder: FuncBuilder):
l = builder.make_label()
2022-07-16 01:33:32 +00:00
builder.write(isa.GOTO(l))
builder.write(isa.DROP(0)) # no-op
builder.write(l)
builder.write(isa.DROP(0)) # no-op
2022-07-02 06:35:03 +00:00
instrs = builder.build()
assert instrs == [
2022-07-16 01:33:32 +00:00
isa.GOTO(2),
isa.DROP(0),
isa.DROP(0),
2022-07-02 06:35:03 +00:00
]
def test_backwards_label(builder: FuncBuilder):
l = builder.make_label()
2022-07-16 01:33:32 +00:00
builder.write(l)
builder.write(isa.DROP(0)) # no-op
builder.write(isa.GOTO(l))
2022-07-02 06:35:03 +00:00
instrs = builder.build()
assert instrs == [
2022-07-16 01:33:32 +00:00
isa.DROP(0),
isa.GOTO(0),
2022-07-02 06:35:03 +00:00
]
def test_self_label(builder: FuncBuilder):
l = builder.make_label()
2022-07-16 01:33:32 +00:00
builder.write(isa.DROP(0)) # no-op
builder.write(l)
builder.write(isa.GOTO(l))
2022-07-02 06:35:03 +00:00
instrs = builder.build()
assert instrs == [
2022-07-16 01:33:32 +00:00
isa.DROP(0),
isa.GOTO(1),
2022-07-02 06:35:03 +00:00
]
2022-08-09 16:05:26 +00:00
def test_local_label(builder: FuncBuilder):
2022-08-19 06:43:42 +00:00
s0 = builder.mark_slot(target=0)
2022-08-09 16:05:26 +00:00
builder.write(isa.SLOT(s0))
s999 = builder.mark_slot(target=999)
builder.write(isa.SLOT(s999))
instrs = builder.build()
assert instrs == [
isa.SLOT(0),
isa.SLOT(999),
]