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

48 lines
1,010 B
Python
Raw Normal View History

2022-07-02 06:35:03 +00:00
#!/usr/bin/env python3
2022-07-16 01:33:32 +00:00
from ichor import FuncBuilder, isa
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
]