From 3034869230b581367d39fca3ae635e75563d795f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sun, 24 Apr 2022 10:58:35 +0200 Subject: [PATCH] compiler: compile push & pop inline (infrastructure) --- scripts/word.wat | 10 +++++++++- src/waforth.wat | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/scripts/word.wat b/scripts/word.wat index 66e47b9..1b0d1fd 100644 --- a/scripts/word.wat +++ b/scripts/word.wat @@ -16,11 +16,19 @@ (get_local $tos) ;; Push - (call_indirect (type $push) (i32.const 43) (i32.const 1)) + (set_local $tos) + (i32.store (local.get $tos) (i32.const 43)) + (i32.add (local.get $tos) (i32.const 4)) ;; Word call (call_indirect (type $push) (i32.const 10) (i32.const 9)) + ;; Pop + (tee_local $tos (i32.sub (i32.const 4))) + (local.get $tos) + (i32.load) + (drop) + ;; Conditional (if (param i32) (result i32) (i32.ne (call_indirect (type $pop) (i32.const 2)) (i32.const 0)) (then diff --git a/src/waforth.wat b/src/waforth.wat index 041cbd0..eb322d0 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -2071,12 +2071,22 @@ (func $compilePushConst (param $n i32) + (call $emitSetLocal (i32.const 0)) ;; Save tos currently on operand stack + (call $emitGetLocal (i32.const 0)) ;; Put tos on operand stack again (call $emitConst (local.get $n)) - (call $emitICall (i32.const 1) (i32.const 1 (; = PUSH_INDEX ;)))) + (call $compilePush)) (func $compilePushLocal (param $n i32) + (call $emitSetLocal (i32.const 0)) ;; Save tos currently on operand stack + (call $emitGetLocal (i32.const 0)) ;; Put tos on operand stack again (call $emitGetLocal (local.get $n)) - (call $emitICall (i32.const 1) (i32.const 1 (; = PUSH_INDEX ;)))) + (call $compilePush)) + +(func $compilePush + (call $emitStore) + (call $emitGetLocal (i32.const 0)) ;; Put $tos+4 on operand stack + (call $emitConst (i32.const 4)) + (call $emitAdd)) (func $compileIf (call $compilePop) @@ -2187,8 +2197,12 @@ (global.set $cp (i32.add (global.get $cp) (i32.const 1)))) (func $compilePop - (call $emitICall (i32.const 2) (i32.const 2 (; = POP_INDEX ;)))) - + (call $emitConst (i32.const 4)) + (call $emitSub) + (call $emitTeeLocal (i32.const 0)) + (call $emitGetLocal (i32.const 0)) + (call $emitLoad)) + (func $compileCall (param $tos i32) (param $FINDToken i32) (result i32) (local $body i32) (local.set $body (call $body (local.get $FINDToken))) @@ -2265,10 +2279,19 @@ (global.set $cp (i32.add (global.get $cp) (i32.const 1))) (global.set $cp (call $leb128 (global.get $cp) (local.get $n)))) +(func $emitTeeLocal (param $n i32) + (i32.store8 (global.get $cp) (i32.const 0x22)) + (global.set $cp (i32.add (global.get $cp) (i32.const 1))) + (global.set $cp (call $leb128 (global.get $cp) (local.get $n)))) + (func $emitAdd (i32.store8 (global.get $cp) (i32.const 0x6a)) (global.set $cp (i32.add (global.get $cp) (i32.const 1)))) +(func $emitSub + (i32.store8 (global.get $cp) (i32.const 0x6b)) + (global.set $cp (i32.add (global.get $cp) (i32.const 1)))) + (func $emitEqualsZero (i32.store8 (global.get $cp) (i32.const 0x45)) (global.set $cp (i32.add (global.get $cp) (i32.const 1)))) @@ -2289,6 +2312,21 @@ (i32.store8 (global.get $cp) (i32.const 0x0f)) (global.set $cp (i32.add (global.get $cp) (i32.const 1)))) +(func $emitStore + (i32.store8 (global.get $cp) (i32.const 0x36)) + (global.set $cp (i32.add (global.get $cp) (i32.const 1))) + (i32.store8 (global.get $cp) (i32.const 0x02)) ;; Alignment + (global.set $cp (i32.add (global.get $cp) (i32.const 1))) + (i32.store8 (global.get $cp) (i32.const 0x00)) ;; Offset + (global.set $cp (i32.add (global.get $cp) (i32.const 1)))) + +(func $emitLoad + (i32.store8 (global.get $cp) (i32.const 0x28)) + (global.set $cp (i32.add (global.get $cp) (i32.const 1))) + (i32.store8 (global.get $cp) (i32.const 0x02)) ;; Alignment + (global.set $cp (i32.add (global.get $cp) (i32.const 1))) + (i32.store8 (global.get $cp) (i32.const 0x00)) ;; Offset + (global.set $cp (i32.add (global.get $cp) (i32.const 1)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Compilation state