From 451a8e915870e24451cd2bcc1c564ac993f32236 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Tue, 9 Aug 2022 20:42:02 +0200 Subject: [PATCH] Removes the old ___new_reference___ allocator --- TODO.md | 1 - phasm/compiler.py | 28 ++-------------------------- phasm/wasm.py | 2 +- tests/integration/helpers.py | 7 ++----- 4 files changed, 5 insertions(+), 33 deletions(-) diff --git a/TODO.md b/TODO.md index 2775e07..28a862b 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,4 @@ # TODO - Implement foldl for bytes -- Replace ___new_reference___ by stdlib.alloc.__alloc__ - Implement a trace() builtin for debugging diff --git a/phasm/compiler.py b/phasm/compiler.py index 58186de..13cc851 100644 --- a/phasm/compiler.py +++ b/phasm/compiler.py @@ -376,7 +376,6 @@ def module(inp: ourlang.Module) -> wasm.Module: ] result.functions = [ - _generate____new_reference___(inp), # Old allocator stdlib_alloc.__init__, stdlib_alloc.__find_free_block__, stdlib_alloc.__alloc__, @@ -390,32 +389,9 @@ def module(inp: ourlang.Module) -> wasm.Module: return result -def _generate____new_reference___(mod: ourlang.Module) -> wasm.Function: - return wasm.Function( - '___new_reference___', - '___new_reference___', - [ - ('alloc_size', type_(mod.types['i32']), ), - ], - [ - ('result', type_(mod.types['i32']), ), - ], - type_(mod.types['i32']), - [ - i32.const(0), - i32.const(0), - i32.load(), - wasm.Statement('local.tee', '$result', comment='Address for this call'), - wasm.Statement('local.get', '$alloc_size'), - i32.add(), - i32.store(comment='Address for the next call'), - wasm.Statement('local.get', '$result'), - ], - ) - def _generate_tuple_constructor(inp: ourlang.TupleConstructor) -> Statements: yield wasm.Statement('i32.const', str(inp.tuple.alloc_size())) - yield wasm.Statement('call', '$___new_reference___') + yield wasm.Statement('call', '$stdlib.alloc.__alloc__') yield wasm.Statement('local.set', '$___new_reference___addr') @@ -434,7 +410,7 @@ def _generate_tuple_constructor(inp: ourlang.TupleConstructor) -> Statements: def _generate_struct_constructor(inp: ourlang.StructConstructor) -> Statements: yield wasm.Statement('i32.const', str(inp.struct.alloc_size())) - yield wasm.Statement('call', '$___new_reference___') + yield wasm.Statement('call', '$stdlib.alloc.__alloc__') yield wasm.Statement('local.set', '$___new_reference___addr') diff --git a/phasm/wasm.py b/phasm/wasm.py index 5fa2506..7c5a982 100644 --- a/phasm/wasm.py +++ b/phasm/wasm.py @@ -186,7 +186,7 @@ class Module(WatSerializable): def __init__(self) -> None: self.imports: List[Import] = [] self.functions: List[Function] = [] - self.memory = ModuleMemory(b'\x04') # For ___new_reference___ + self.memory = ModuleMemory() def to_wat(self) -> str: """ diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index f62004d..2e6b706 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -88,7 +88,7 @@ class Suite: # Check if code formatting works assert self.code_py == '\n' + phasm_render(runner.phasm_ast) # \n for formatting in tests - # runner.call('stdlib.alloc.__init__') + runner.call('stdlib.alloc.__init__') wasm_args = [] if args: @@ -101,12 +101,9 @@ class Suite: continue if isinstance(arg, bytes): - # TODO: Implement and use the bytes constructor function - # TODO: call upon stdlib.alloc.__init__ and stdlib.alloc.__alloc__ - adr = runner.call('___new_reference___', len(arg) + 4) + adr = runner.call('stdlib.types.__alloc_bytes__', len(arg)) sys.stderr.write(f'Allocation 0x{adr:08x} {repr(arg)}\n') - runner.interpreter_write_memory(adr, len(arg).to_bytes(4, byteorder='little')) runner.interpreter_write_memory(adr + 4, arg) wasm_args.append(adr) continue