word belts, utf16 strings, interrupt words, compiler modes
[watForth.git] / forth.wat
1 (module
2 (type $FUNCSIGi (func (result i32)))
3 (type $FUNCSIGii (func (param i32)))
4 (type $FUNCSIGiii (func))
5 (type $FUNCSIGiv (func (param i32 i32) (result i32)))
6 (type $FUNCSIG$v (func (param i32) (result i32)))
7 (type $FUNCSIG$vi (func (param i32 i32) (result i32)))
8 (import "env" "pop" (func $pop (result i32)))
9 (import "env" "push" (func $push (param i32)))
10 (import "env" "rinit" (func $rinit))
11 (import "env" "rpop" (func $rpop (result i32)))
12 (import "env" "rpush" (func $rpush (param i32)))
13 (import "env" "sys_read" (func $sys_read (param i32 i32) (result i32)))
14 (import "env" "sys_fetch" (func $sys_fetch (param i32 i32) (result i32)))
15 (import "env" "sys_listen" (func $sys_listen (param i32) (result i32)))
16 (import "env" "sys_write" (func $sys_write (param i32 i32) (result i32)))
17 (import "env" "sys_echo" (func $sys_echo (param i32)))
18 (import "env" "sys_echochar" (func $sys_echochar (param i32)))
19 (import "env" "sys_reflect" (func $sys_reflect (param i32)))
20 (import "env" "vocab_get" (func $vocab_get (param i32 i32) (result i32)))
21 (import "env" "vocab_set" (func $vocab_set (param i32 i32 i32) (result i32)))
22 (import "env" "is_whitespace" (func $is_whitespace (param i32) (result i32)))
23 (import "env" "sys_parsenum" (func $sys_parsenum (param i32 i32 i32) (result i32)))
24 (import "env" "sys_stack" (func $sys_stack))
25 (import "env" "sys_words" (func $sys_words))
26 (table (;0;) 0 anyfunc)
27 (memory $0 1)
28 (; String Belt ;) ;; 0x0000 Size: 8192
29 (global $wordbelt i32 (i32.const 8192)) ;; 0x2000 Size: 4096
30 (global $inbuf i32 (i32.const 12288)) ;; 0x3000 Size: 2048
31 (global $inbuf_size i32 (i32.const 12292))
32 (global $inbuf_data i32 (i32.const 12296))
33 (global $kvars i32 (i32.const 14336)) ;; 0x3800 Size: 2048
34 (data (i32.const 12288) "\fc\07\00\00") ;; 2044 len
35 (data (i32.const 14336) "\28\41\00\00") ;; MODE
36 (data (i32.const 14340) "\04\42\00\00") ;; HERE
37 (data (i32.const 14344) "\00\40\00\00") ;; START
38 (data (i32.const 14348) "\0a\00\00\00") ;; BASE
39 (data (i32.const 14352) "\00\00\00\00") ;; STRINGBELT_TAIL
40 (data (i32.const 14356) "\00\00\00\00") ;; STRINGBELT_HEAD
41 (data (i32.const 14360) "\00\20\00\00") ;; WORDBELT_TAIL
42 (data (i32.const 14364) "\00\20\00\00") ;; WORDBELT_HEAD
43 (data (i32.const 14368) "\00\00\00\00") ;; CHANNEL
44 (; Quit ;)
45 (data (i32.const 16384) "\03\00\00\00") ;; RINIT xt
46 (data (i32.const 16388) "\10\40\00\00") ;; INTERPRET xt
47 (data (i32.const 16392) "\12\00\00\00") ;; JMP xt
48 (data (i32.const 16396) "\00\40\00\00") ;; quit location (16384)
49 (; Interpret ;)
50 (data (i32.const 16400) "\74\40\00\00") ;; WORD xt (16500)
51 (data (i32.const 16404) "\06\00\00\00") ;; DUP
52 (data (i32.const 16408) "\0e\00\00\00") ;; JZ:
53 (data (i32.const 16412) "\38\40\00\00") ;; INTERP-END addr (16444)
54 (data (i32.const 16416) "\02\00\00\00") ;; LIT xt
55 (data (i32.const 16420) "\00\38\00\00") ;; MODE addr (14336)
56 (data (i32.const 16424) "\0a\00\00\00") ;; @ (fetch) xt
57 (data (i32.const 16428) "\0c\00\00\00") ;; EXECUTE xt
58 (data (i32.const 16432) "\0d\00\00\00") ;; NOOP xt
59 (data (i32.const 16436) "\01\00\00\00") ;; RET
60 (data (i32.const 16440) "\10\00\00\00") ;; DROP <-- INTERP-END
61 (data (i32.const 16444) "\10\00\00\00") ;; DROP
62 (data (i32.const 16448) "\19\00\00\00") ;; BYE
63 (; Word ;)
64 (data (i32.const 16500) "\14\00\00\00") ;; WORDSTART
65 (data (i32.const 16504) "\05\00\00\00") ;; KEY <-- KEYLOOP
66 (data (i32.const 16508) "\06\00\00\00") ;; DUP
67 (data (i32.const 16512) "\18\00\00\00") ;; J-1: 18
68 (data (i32.const 16516) "\f0\40\00\00") ;; addr of WORDEND
69 (data (i32.const 16520) "\11\00\00\00") ;; WS?
70 (data (i32.const 16524) "\0f\00\00\00") ;; JNZ:
71 (data (i32.const 16528) "\bc\40\00\00") ;; addr of KEYDROP
72 (data (i32.const 16532) "\02\00\00\00") ;; LIT
73 (data (i32.const 16536) "\5c\00\00\00") ;; 92 (\ character)
74 (data (i32.const 16540) "\25\00\00\00") ;; =?
75 (data (i32.const 16544) "\0e\00\00\00") ;; JZ:
76 (data (i32.const 16548) "\e4\40\00\00") ;; addr of DOCHAR
77 (data (i32.const 16552) "\10\00\00\00") ;; DROP
78 (data (i32.const 16556) "\c8\40\00\00") ;; WORDLOOP (continue using this wbuf we started)
79 (data (i32.const 16560) "\28\41\00\00") ;; EXECUTE-MODE
80 (data (i32.const 16564) "\12\00\00\00") ;; JMP:
81 (data (i32.const 16568) "\74\40\00\00") ;; addr of KEYLOOP-1 (get a new wbuf, call to wbuf+1 ate ours)
82 (data (i32.const 16572) "\10\00\00\00") ;; DROP <-- KEYDROP
83 (data (i32.const 16576) "\12\00\00\00") ;; JMP:
84 (data (i32.const 16580) "\78\40\00\00") ;; addr of KEYLOOP
85 (data (i32.const 16584) "\05\00\00\00") ;; KEY <-- WORDLOOP
86 (data (i32.const 16588) "\11\00\00\00") ;; WS?
87 (data (i32.const 16592) "\0f\00\00\00") ;; JNZ:
88 (data (i32.const 16596) "\f0\40\00\00") ;; addr of WORDEND
89 (data (i32.const 16600) "\06\00\00\00") ;; DUP
90 (data (i32.const 16604) "\18\00\00\00") ;; J-1:
91 (data (i32.const 16608) "\f0\40\00\00") ;; addr of WORDEND
92 (data (i32.const 16612) "\13\00\00\00") ;; WORDPUTC <-- DOCHAR
93 (data (i32.const 16616) "\12\00\00\00") ;; JMP:
94 (data (i32.const 16620) "\c8\40\00\00") ;; addr of WORDLOOP
95 (data (i32.const 16624) "\10\00\00\00") ;; DROP <-- WORDEND
96 (data (i32.const 16628) "\17\00\00\00") ;; WORDFINISH
97 (data (i32.const 16632) "\01\00\00\00") ;; RET
98 (; Exec Mode ;)
99 (data (i32.const 16680) "\1e\00\00\00") ;; DUP2
100 (data (i32.const 16684) "\15\00\00\00") ;; DICT_GET
101 (data (i32.const 16688) "\06\00\00\00") ;; DUP
102 (data (i32.const 16692) "\0e\00\00\00") ;; JZ:
103 (data (i32.const 16696) "\4c\41\00\00") ;; donum -1 (16716)
104 (data (i32.const 16700) "\1f\00\00\00") ;; ROT
105 (data (i32.const 16704) "\20\00\00\00") ;; DROP2
106 (data (i32.const 16708) "\0c\00\00\00") ;; EXECUTE
107 (data (i32.const 16712) "\01\00\00\00") ;; RET
108 (data (i32.const 16716) "\10\00\00\00") ;; DROP (xt from dictionary)
109 (data (i32.const 16720) "\16\00\00\00") ;; NUMBER <-- donum, pushes NUM, UNPARSED
110 (data (i32.const 16724) "\06\00\00\00") ;; DUP
111 (data (i32.const 16728) "\0f\00\00\00") ;; JNZ:
112 (data (i32.const 16732) "\68\41\00\00") ;; donum_err (16744)
113 (data (i32.const 16736) "\10\00\00\00") ;; DROP
114 (data (i32.const 16740) "\01\00\00\00") ;; RET
115 (data (i32.const 16744) "\10\00\00\00") ;; PARSE_ERR <-- donum_err
116 (data (i32.const 16748) "\10\00\00\00") ;; ( DROP DROP )
117 (data (i32.const 16752) "\19\00\00\00") ;; BYE
118 (; : definition ;)
119 (data (i32.const 16800) "\74\40\00\00") ;; WORD
120 (data (i32.const 16804) "\1c\00\00\00") ;; HERE
121 (data (i32.const 16808) "\1d\00\00\00") ;; VOCAB_SET
122 (data (i32.const 16812) "\01\00\00\00") ;; RET
123 (export "memory" (memory $0))
124 (export "main" (func $main))
125 (func $main (result i32)
126 call $interpret
127 )
128 (func $interpret (result i32)
129 (local $here i32)
130 (local $eax i32)
131 (local $esi i32)
132 (local $inbuf_head i32)
133 (local $stringbelt_tail i32)
134 (local $stringbelt_head i32)
135 (local $wordbelt_tail i32)
136 (local $wordbelt_head i32)
137 (local $channel i32)
138 i32.const 14340
139 i32.load
140 set_local $here
141 i32.const 14344
142 i32.load
143 set_local $esi
144 get_global $inbuf_data
145 set_local $inbuf_head
146 i32.const 14352
147 i32.load
148 set_local $stringbelt_tail
149 i32.const 14356
150 i32.load
151 set_local $stringbelt_head
152 i32.const 14360
153 i32.load
154 set_local $wordbelt_tail
155 i32.const 14364
156 i32.load
157 set_local $wordbelt_head
158 i32.const 14368
159 i32.load
160 set_local $channel
161 block $bye
162 loop $next
163 call $sys_stack
164 get_local $esi
165 call $sys_reflect
166 get_local $esi
167 get_local $esi
168 i32.const 4
169 i32.add
170 set_local $esi
171 i32.load
172 set_local $eax
173 loop $execloop
174 block $default block $op0 block $ret block $lit block $rinit
175 block $word block $key block $dup block $plus block $noop2 block $emit
176 block $fetch block $set block $execute block $noop block $jz block $jnz
177 block $drop block $wsbool block $jmp block $wordputc block $wordstart
178 block $dictget block $parsenum block $wordfinish block $jneg1 block $swap
179 block $words block $here block $dictset block $dup2 block $rot block $drop2
180 block $comma block $subtract block $keychan block $sethere block $eqbool
181 get_local $eax
182 br_table $op0 $ret (;2;)$lit $rinit (;4;)$word $key (;6;)$dup $plus
183 (;8;)$jmp $emit (;10;)$fetch $set (;12;)$execute $noop (;14;)$jz $jnz
184 (;16;)$drop $wsbool (;18;)$jmp $wordputc (;20;)$wordstart $dictget
185 (;22;)$parsenum $wordfinish (;24;)$jneg1 $bye (;26;)$swap $words
186 (;28;)$here $dictset (;30;)$dup2 $rot (;32;)$drop2 $comma
187 (;34;)$subtract $keychan (;36;)$sethere $eqbool $default
188 end ;; eqbool
189 block $equiv
190 call $pop
191 call $pop
192 tee_local $eax
193 i32.eq
194 get_local $eax
195 call $push
196 br_if $equiv
197 i32.const 0
198 call $push
199 br $next
200 end
201 i32.const 1
202 call $push
203 br $next
204 end ;; sethere
205 call $pop
206 set_local $here
207 br $next
208 end ;; keychan
209 call $pop
210 set_local $channel
211 br $next
212 end ;; subtract
213 call $pop
214 set_local $eax
215 call $pop
216 get_local $eax
217 i32.sub
218 call $push
219 br $next
220 end ;; comma
221 get_local $here
222 call $pop
223 i32.store
224 get_local $here
225 i32.const 4
226 i32.add
227 set_local $here
228 br $next
229 end ;; drop2
230 call $pop
231 call $pop
232 drop
233 drop
234 br $next
235 end ;; rot
236 call $pop
237 set_local $eax
238 call $pop
239 call $pop
240 get_local $eax
241 call $push
242 call $push
243 call $push
244 br $next
245 end ;; dup2
246 get_local $esi
247 call $rpush
248 call $pop
249 set_local $eax
250 call $pop
251 tee_local $esi
252 call $push
253 get_local $eax
254 call $push
255 get_local $esi
256 call $push
257 get_local $eax
258 call $push
259 call $rpop
260 set_local $esi
261 br $next
262 end ;; dictset
263 call $pop
264 call $rpush
265 call $pop
266 set_local $eax
267 call $pop
268 get_local $eax
269 call $rpop
270 call $vocab_set
271 drop
272 br $next
273 end ;; here
274 get_local $here
275 call $push
276 br $next
277 end ;; words
278 call $sys_words
279 br $next
280 end ;; swap
281 call $pop
282 call $pop
283 set_local $eax
284 call $push
285 get_local $eax
286 call $push
287 br $next
288 end ;; jneg1
289 block $jneg1if
290 call $pop
291 i32.const -1
292 i32.eq
293 br_if $jneg1if
294 get_local $esi
295 i32.const 4
296 i32.add
297 set_local $esi
298 br $next
299 end
300 get_local $esi
301 i32.load
302 set_local $esi
303 br $next
304 end ;; wordfinish
305 get_local $wordbelt_tail
306 get_local $wordbelt_head
307 get_local $wordbelt_tail
308 i32.const 4
309 i32.add
310 i32.sub
311 tee_local $eax (; n bytes ;)
312 i32.store
313 (; align to 32-bit ;)
314 get_local $wordbelt_head
315 i32.const 3
316 i32.add
317 i32.const 12284
318 i32.and
319 set_local $wordbelt_head
320 (; /align ;)
321 get_local $wordbelt_tail
322 i32.const 4
323 i32.add
324 call $push
325 get_local $eax
326 call $push
327 br $next
328 end ;; parsenum
329 call $pop
330 call $rpush
331 call $pop
332 tee_local $eax
333 call $rpop
334 i32.const 14348 (; load BASE ;)
335 i32.load
336 call $sys_parsenum
337 get_local $eax
338 i32.load
339 call $push
340 call $push
341 br $next
342 end ;; dictget
343 call $pop
344 set_local $eax
345 call $pop
346 get_local $eax
347 call $vocab_get
348 call $push
349 br $next
350 end ;; wordstart
351 block $wbhasspace
352 get_local $wordbelt_head
353 get_global $inbuf
354 i32.const 8
355 i32.sub
356 i32.le_u
357 br_if $wbhasspace
358 get_global $wordbelt
359 set_local $wordbelt_head
360 end
361 get_local $wordbelt_head
362 get_local $wordbelt_head
363 tee_local $wordbelt_tail
364 i32.const 0
365 i32.store
366 i32.const 4
367 i32.add
368 set_local $wordbelt_head
369 br $next
370 end ;; wordputc
371 block $wbhasspace2
372 get_local $wordbelt_head
373 get_global $inbuf
374 i32.lt_u
375 br_if $wbhasspace2
376 get_global $wordbelt
377 tee_local $wordbelt_head
378 get_local $wordbelt_tail
379 i32.load
380 i32.store
381 get_local $wordbelt_head
382 i32.const 4
383 i32.add
384 set_local $wordbelt_head
385 get_local $wordbelt_tail
386 i32.const 4
387 i32.add
388 set_local $wordbelt_tail
389 loop $copywordtostart
390 get_local $wordbelt_head
391 get_local $wordbelt_tail
392 i32.load16_u
393 i32.store16
394 get_local $wordbelt_head
395 i32.const 2
396 i32.add
397 set_local $wordbelt_head
398 get_local $wordbelt_tail
399 i32.const 2
400 i32.add
401 tee_local $wordbelt_tail
402 get_global $inbuf
403 i32.le_u
404 br_if $copywordtostart
405 end
406 get_global $wordbelt
407 set_local $wordbelt_tail
408 end
409 get_local $wordbelt_head
410 call $pop
411 i32.store16
412 get_local $wordbelt_head
413 i32.const 2
414 i32.add
415 set_local $wordbelt_head
416 br $next
417 end ;; jmp
418 get_local $esi
419 i32.load
420 set_local $esi
421 br $next
422 end ;; wsbool
423 call $pop
424 tee_local $eax
425 call $is_whitespace
426 get_local $eax
427 call $push
428 call $push
429 call $sys_stack
430 br $next
431 end ;; drop
432 call $pop
433 drop
434 br $next
435 end ;; jnz
436 block $jnzif
437 call $pop
438 i32.eqz
439 br_if $jnzif
440 get_local $esi
441 i32.load
442 set_local $esi
443 br $next
444 end
445 get_local $esi
446 i32.const 4
447 i32.add
448 set_local $esi
449 br $next
450 end ;; jz
451 block $jzif
452 call $pop
453 i32.eqz
454 br_if $jzif
455 get_local $esi
456 i32.const 4
457 i32.add
458 set_local $esi
459 br $next
460 end
461 get_local $esi
462 i32.load
463 set_local $esi
464 br $next
465 end ;; noop
466 br $next
467 end ;; execute
468 call $pop
469 tee_local $eax
470 i32.const 256
471 i32.lt_u
472 br_if $execloop
473 get_local $esi
474 call $rpush
475 get_local $eax
476 set_local $esi
477 br $next
478 end ;; set
479 call $pop
480 set_local $eax
481 call $pop
482 get_local $eax
483 i32.store
484 br $next
485 end ;; fetch
486 call $pop
487 i32.load
488 call $push
489 br $next
490 end ;; emit (.)
491 call $pop
492 call $sys_echo
493 br $next
494 end ;; noop2
495 br $next
496 end ;; plus
497 call $pop
498 call $pop
499 i32.add
500 call $push
501 br $next
502 end ;; dup
503 call $pop
504 tee_local $eax
505 get_local $eax
506 call $push
507 call $push
508 br $next
509 end ;; key
510 loop $key_loop
511 block $key_read
512 get_global $inbuf_size
513 i32.load
514 get_local $inbuf_head
515 get_global $inbuf_data
516 i32.sub
517 i32.le_u
518 br_if $key_read
519 get_local $inbuf_head
520 i32.load16_u
521 call $push
522 get_local $inbuf_head
523 i32.const 2
524 i32.add
525 set_local $inbuf_head
526 br $next
527 end ;; key_read
528 get_local $channel
529 get_global $inbuf
530 call $sys_read
531 block $nullread
532 get_global $inbuf_size
533 i32.load
534 i32.eqz
535 br_if $nullread
536 br $key_loop
537 end ;; nullread
538 i32.const -1 ;; <- keyval sent if sz == 0
539 call $push
540 br $next
541 end ;; key_loop
542 end ;; word
543 br $next
544 end ;; rinit
545 call $rinit
546 br $next
547 end ;; lit
548 get_local $esi
549 get_local $esi
550 i32.const 4
551 i32.add
552 set_local $esi
553 i32.load
554 call $push
555 br $next
556 end ;; ret
557 call $rpop
558 set_local $esi
559 br $next
560 end ;; op0
561 get_local $esi
562 call $sys_reflect
563 br $bye
564 end ;; default
565 get_local $esi
566 call $rpush
567 get_local $eax
568 set_local $esi
569 br $next
570 end ;; execloop
571 end ;; nextl
572 end ;; bye
573 i32.const 14340
574 get_local $here
575 i32.store
576 i32.const 14352
577 get_local $stringbelt_tail
578 i32.store
579 i32.const 14356
580 get_local $stringbelt_head
581 i32.store
582 i32.const 14360
583 get_local $wordbelt_tail
584 i32.store
585 i32.const 14364
586 get_local $wordbelt_head
587 i32.store
588 i32.const 14368
589 get_local $channel
590 i32.store
591 i32.const 0
592 )
593 )