From d18f1c695602412ebf4e6f5fc89b2466c8923042 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sun, 18 Dec 2022 14:31:17 +0100 Subject: [PATCH] Re-implemented allocation calculations --- phasm/compiler.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/phasm/compiler.py b/phasm/compiler.py index 71ee949..500f182 100644 --- a/phasm/compiler.py +++ b/phasm/compiler.py @@ -771,7 +771,30 @@ def _generate_struct_constructor(wgn: WasmGenerator, inp: ourlang.StructConstruc wgn.local.get(tmp_var) def _calculate_alloc_size(typ: Union[type3types.StructType3, type3types.Type3]) -> int: - return 0 # FIXME: Stub + if typ is type3types.u8: + return 1 + + if typ is type3types.u32 or typ is type3types.i32 or typ is type3types.f32: + return 4 + + if typ is type3types.u64 or typ is type3types.i64 or typ is type3types.f64: + return 8 + + if isinstance(typ, type3types.StructType3): + return sum( + _calculate_alloc_size(x) + for x in typ.members.values() + ) + + raise NotImplementedError(_calculate_alloc_size, typ) def _calculate_member_offset(struct_type3: type3types.StructType3, member: str) -> int: - return 0 # FIXME: Stub + result = 0 + + for mem, memtyp in struct_type3.members.items(): + if member == mem: + return result + + result += _calculate_alloc_size(memtyp) + + raise Exception(f'{member} not in {struct_type3}')