From 2dbe2e00ddce3c8526310beca7d619206527811b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Tue, 31 May 2022 20:30:27 +0200 Subject: [PATCH] compileDo: Rearrange --- src/waforth.wat | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/waforth.wat b/src/waforth.wat index 1ce6d54..f88b43b 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -2192,6 +2192,11 @@ (call $compileEndDests (local.get $tos))) (func $compileDo (param $tos i32) (result i32) + ;; Save branch nesting + (i32.store (local.get $tos) (global.get $branchNesting)) + (local.set $tos (i32.add (local.get $tos) (i32.const 4))) + (global.set $branchNesting (i32.const 0)) + ;; 1: $diff_i = end index - current index ;; 2: $end_i (global.set $currentLocal (i32.add (global.get $currentLocal) (i32.const 2))) @@ -2199,11 +2204,6 @@ (then (global.set $lastLocal (global.get $currentLocal)))) - ;; Save branch nesting - (i32.store (local.get $tos) (global.get $branchNesting)) - (local.set $tos (i32.add (local.get $tos) (i32.const 4))) - (global.set $branchNesting (i32.const 0)) - ;; $1 = current index (temporary) (call $compilePop) (call $emitSetLocal (i32.sub (global.get $currentLocal) (i32.const 1))) @@ -2211,6 +2211,10 @@ (call $compilePop) (call $emitSetLocal (global.get $currentLocal)) + ;; Already emit the block, so we can jump out of it before starting DO + ;; in the conditional case + (call $emitBlock) + ;; startDo $1 (call $emitGetLocal (i32.sub (global.get $currentLocal) (i32.const 1))) (call $emitICall (i32.const 1) (i32.const 0x1 (; = START_DO_INDEX ;))) @@ -2221,7 +2225,6 @@ (call $emitSub) (call $emitSetLocal (i32.sub (global.get $currentLocal) (i32.const 1))) - (call $emitBlock) (call $emitLoop) (local.get $tos))