From 257d731b96d0670e123a95017c0f243862d5225b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sun, 24 Apr 2022 12:14:41 +0200 Subject: [PATCH] compiler: fix bad local index offset --- src/waforth.wat | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/waforth.wat b/src/waforth.wat index b632458..041cbd0 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -1978,8 +1978,8 @@ (func $startColon (param $params i32) (i32.store8 (i32.const 0x1041 (; = MODULE_HEADER_FUNCTION_TYPE_BASE ;)) (local.get $params)) (global.set $cp (i32.const 0x1060 (; = MODULE_BODY_BASE ;))) - (global.set $currentLocal (i32.sub (local.get $params) (i32.const 1))) - (global.set $lastLocal (i32.sub (local.get $params) (i32.const 1))) + (global.set $currentLocal (local.get $params)) + (global.set $lastLocal (local.get $params)) (global.set $branchNesting (i32.const -1))) (func $endColon @@ -2006,7 +2006,13 @@ ;; Update #locals (i32.store (i32.const 0x1059 (; = MODULE_HEADER_LOCAL_COUNT_BASE ;)) - (call $leb128-4p (i32.add (global.get $lastLocal) (i32.const 1)))) + (call $leb128-4p + (i32.sub + (global.get $lastLocal) + ;; Subtract the number of incoming parameters. We could use a global to keep track of + ;; the type, but for now getting this from the module header (where we stored this at + ;; start of compilation) + (i32.load8_u (i32.const 0x1041 (; = MODULE_HEADER_FUNCTION_TYPE_BASE ;)))))) ;; Update table offset (i32.store