Skip to content
This repository was archived by the owner on Nov 11, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions porth.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Intrinsic(Enum):
MEM=auto()
LOAD=auto()
STORE=auto()
LOAD32=auto()
STORE32=auto()
LOAD64=auto()
STORE64=auto()
ARGC=auto()
Expand Down Expand Up @@ -181,7 +183,7 @@ def simulate_little_endian_linux(program: Program, argv: List[str]):
else:
ip += 1
elif op.typ == OpType.INTRINSIC:
assert len(Intrinsic) == 33, "Exhaustive handling of intrinsic in simulate_little_endian_linux()"
assert len(Intrinsic) == 35, "Exhaustive handling of intrinsic in simulate_little_endian_linux()"
if op.operand == Intrinsic.PLUS:
a = stack.pop()
b = stack.pop()
Expand Down Expand Up @@ -292,6 +294,19 @@ def simulate_little_endian_linux(program: Program, argv: List[str]):
store_addr = stack.pop()
mem[store_addr] = store_value & 0xFF
ip += 1
elif op.operand == Intrinsic.LOAD32:
addr = stack.pop()
_bytes = bytearray(4)
for offset in range(0,4):
_bytes[offset] = mem[addr + offset]
stack.append(int.from_bytes(_bytes, byteorder="little"))
ip += 1
elif op.operand == Intrinsic.STORE32:
store_value32 = stack.pop().to_bytes(length=8, byteorder="little")
store_addr32 = stack.pop()
for index in range(0, 4):
mem[store_addr32 + index] = store_value32[index]
ip += 1
elif op.operand == Intrinsic.LOAD64:
addr = stack.pop()
_bytes = bytearray(8)
Expand Down Expand Up @@ -624,7 +639,7 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
assert isinstance(op.operand, int), "This could be a bug in the compilation step"
out.write(" jz addr_%d\n" % op.operand)
elif op.typ == OpType.INTRINSIC:
assert len(Intrinsic) == 33, "Exhaustive intrinsic handling in generate_nasm_linux_x86_64()"
assert len(Intrinsic) == 35, "Exhaustive intrinsic handling in generate_nasm_linux_x86_64()"
if op.operand == Intrinsic.PLUS:
out.write(" ;; -- plus --\n")
out.write(" pop rax\n")
Expand Down Expand Up @@ -778,6 +793,17 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
out.write(" mov rax, [args_ptr]\n")
out.write(" add rax, 8\n")
out.write(" push rax\n")
elif op.operand == Intrinsic.LOAD32:
out.write(" ;; -- load --\n")
out.write(" pop rax\n")
out.write(" xor rbx, rbx\n")
out.write(" mov ebx, [rax]\n")
out.write(" push rbx\n")
elif op.operand == Intrinsic.STORE32:
out.write(" ;; -- store --\n")
out.write(" pop rbx\n");
out.write(" pop rax\n");
out.write(" mov [rax], ebx\n");
elif op.operand == Intrinsic.LOAD64:
out.write(" ;; -- load --\n")
out.write(" pop rax\n")
Expand Down Expand Up @@ -872,7 +898,7 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
'include': Keyword.INCLUDE,
}

assert len(Intrinsic) == 33, "Exhaustive INTRINSIC_NAMES definition"
assert len(Intrinsic) == 35, "Exhaustive INTRINSIC_NAMES definition"
INTRINSIC_NAMES = {
'+': Intrinsic.PLUS,
'-': Intrinsic.MINUS,
Expand All @@ -896,6 +922,8 @@ def generate_nasm_linux_x86_64(program: Program, out_file_path: str):
'mem': Intrinsic.MEM,
'.': Intrinsic.STORE,
',': Intrinsic.LOAD,
'.32': Intrinsic.STORE32,
',32': Intrinsic.LOAD32,
'.64': Intrinsic.STORE64,
',64': Intrinsic.LOAD64,
'argc': Intrinsic.ARGC,
Expand Down