waforth/scripts/word.wat

58 lines
1.7 KiB
Text
Raw Normal View History

2018-05-12 21:09:19 +02:00
;; Template for defining 'word' modules
;; Used to 'reverse engineer' the binary code to emit from the compiler
2018-05-31 21:11:04 +02:00
(module $quadruple
2018-05-12 21:09:19 +02:00
(import "env" "table" (table 4 anyfunc))
(import "env" "memory" (memory 1))
(import "env" "tos" (global $tos i32))
2018-05-12 21:09:19 +02:00
(type $void (func))
(type $push (func (param i32)))
(type $pop (func (result i32)))
(type $endLoop (func (param i32) (result i32)))
2018-05-24 22:11:51 +02:00
(func $word (param $n i32)
2018-05-27 16:40:47 +02:00
(local $index1 i32)
(local $end1 i32)
2019-03-14 10:46:42 +01:00
(local $incr1 i32)
2018-05-27 16:40:47 +02:00
2018-05-12 21:09:19 +02:00
;; Push
(call_indirect (type $push) (i32.const 43) (i32.const 1))
;; Word call
2018-05-24 22:11:51 +02:00
(call_indirect (type $push) (i32.const 10) (i32.const 9))
2018-05-12 21:09:19 +02:00
;; Conditional
(if (i32.ne (call_indirect (type $pop) (i32.const 2)) (i32.const 0))
(then
(nop)
(nop))
(else
(nop)
(nop)
(nop)))
;; do loop
2018-05-27 16:40:47 +02:00
(set_local $index1 (call_indirect (type $pop) (i32.const 2)))
(set_local $end1 (call_indirect (type $pop) (i32.const 2)))
2019-03-14 10:46:42 +01:00
(set_local $incr1 (i32.ge_s (get_local $end1) (get_local $index1)))
2018-05-12 21:09:19 +02:00
(block $endDoLoop
(loop $doLoop
(nop)
2018-05-27 16:40:47 +02:00
(set_local $index1 (i32.add (get_local $index1) (i32.const 1)))
2019-03-14 10:46:42 +01:00
(if (i32.eqz (get_local $incr1))
(then (br_if $endDoLoop (i32.le_s (get_local $index1) (get_local $end1))))
(else (br_if $endDoLoop (i32.ge_s (get_local $index1) (get_local $end1)))))
2018-05-12 21:09:19 +02:00
(br $doLoop)))
;; repeat loop
(block $endRepeatLoop
(loop $repeatLoop
(nop)
(br_if $endRepeatLoop (i32.eqz (call_indirect (type $pop) (i32.const 2))))
(nop)
(br $repeatLoop)))
2018-05-24 22:11:51 +02:00
(call $word (get_local $n)))
2018-05-12 21:09:19 +02:00
2019-03-09 20:17:04 +01:00
(elem (i32.const 44) $word))