MVP #1

Merged
jbwdevries merged 73 commits from idea_crc32 into master 2022-08-21 12:59:21 +00:00
4 changed files with 5 additions and 33 deletions
Showing only changes of commit 451a8e9158 - Show all commits

View File

@ -1,5 +1,4 @@
# TODO
- Implement foldl for bytes
- Replace ___new_reference___ by stdlib.alloc.__alloc__
- Implement a trace() builtin for debugging

View File

@ -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')

View File

@ -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:
"""

View File

@ -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