testing system api revision
[watForth.git] / forth.wat
1 (; This program is free software: you can redistribute it and/or modify
2 it under the terms of the GNU General Public License as published by
3 the Free Software Foundation, either version 3 of the License, or
4 (at your option) any later version.
5
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU General Public License for more details.
10
11 You should have received a copy of the GNU General Public License
12 along with this program. If not, see <http://www.gnu.org/licenses/>. ;)
13 (module
14 (type $FUNCSIGi (func (result i32)))
15 (type $FUNCSIGii (func (param i32)))
16 (type $FUNCSIGiii (func))
17 (type $FUNCSIGiv (func (param i32 i32) (result i32)))
18 (type $FUNCSIGv (func (param i32) (result i32)))
19 (type $FUNCSIGvi (func (param i32 i32 i32) (result i32)))
20 (type $FUNCSIGvii (func (param i32 i32)))
21 (type $FUNCSIGviii (func (param i32 i32 i32)))
22 (import "env" "pop" (func $pop (result i32)))
23 (import "env" "push" (func $push (param i32)))
24 (import "env" "rinit" (func $rinit))
25 (import "env" "rpop" (func $rpop (result i32)))
26 (import "env" "rpush" (func $rpush (param i32)))
27 (import "env" "sys_read" (func $sys_read (param i32 i32 i32) (result i32)))
28 (import "env" "sys_fetch" (func $sys_fetch (param i32 i32 i32) (result i32)))
29 (import "env" "sys_listen" (func $sys_listen (param i32 i32 i32) (result i32)))
30 (import "env" "sys_write" (func $sys_write (param i32 i32 i32) (result i32)))
31 (import "env" "sys_echo" (func $sys_echo (param i32 i32)))
32 (import "env" "sys_echochar" (func $sys_echochar (param i32)))
33 (import "env" "sys_reflect" (func $sys_reflect (param i32)))
34 (import "env" "vocab_get" (func $vocab_get (param i32 i32) (result i32)))
35 (import "env" "vocab_set" (func $vocab_set (param i32 i32 i32)))
36 (import "env" "does_get" (func $does_get (param i32 i32) (result i32)))
37 (import "env" "does_set" (func $does_set (param i32 i32 i32)))
38 (import "env" "is_whitespace" (func $is_whitespace (param i32) (result i32)))
39 (import "env" "sys_parsenum" (func $sys_parsenum (param i32 i32 i32) (result i32)))
40 (import "env" "sys_stack" (func $sys_stack))
41 (import "env" "sys_words" (func $sys_words))
42 (table (;0;) 0 anyfunc)
43 (memory $0 1)
44 (; String Belt ;) ;; 0x0000 Size: 8192
45 (global $wordbelt i32 (i32.const 8192)) ;; 0x2000 Size: 4096
46 (global $inbuf i32 (i32.const 12288)) ;; 0x3000 Size: 2048
47 (global $inbuf_size i32 (i32.const 12292))
48 (global $inbuf_data i32 (i32.const 12296))
49 (global $kvars i32 (i32.const 14336)) ;; 0x3800 Size: 2048
50 (data (i32.const 12288) "\f8\07\00\00") ;; 2040 len
51 (global $mode_p i32 (i32.const 14336))
52 (global $here_p i32 (i32.const 14340))
53 (global $start_p i32 (i32.const 14344))
54 (global $base_p i32 (i32.const 14348))
55 (global $stringbelt_tail_p i32 (i32.const 14352))
56 (global $stringbelt_head_p i32 (i32.const 14356))
57 (global $wordbelt_tail_p i32 (i32.const 14360))
58 (global $wordbelt_head_p i32 (i32.const 14364))
59 (global $channel_in_p i32 (i32.const 14368))
60 (global $channel_out_p i32 (i32.const 14372))
61 (data (i32.const 14336) "\28\41\00\00") ;; MODE
62 (data (i32.const 14340) "\04\42\00\00") ;; HERE
63 (data (i32.const 14344) "\00\40\00\00") ;; START (16384) (Quit)
64 (data (i32.const 14348) "\0a\00\00\00") ;; BASE
65 (data (i32.const 14352) "\00\00\00\00") ;; STRINGBELT_TAIL
66 (data (i32.const 14356) "\00\00\00\00") ;; STRINGBELT_HEAD
67 (data (i32.const 14360) "\00\20\00\00") ;; WORDBELT_TAIL
68 (data (i32.const 14364) "\00\20\00\00") ;; WORDBELT_HEAD
69 (data (i32.const 14368) "\00\00\00\00") ;; CHANNEL-IN
70 (data (i32.const 14372) "\01\00\00\00") ;; CHANNEL-OUT
71 (; channel listeners 0x3c00 ;)
72 (global $channel_listeners_p i32 (i32.const 15360))
73 (data (i32.const 15360) "\00\40\00\00") ;; CHANNEL LISTENER 0 (quit)
74 (; Quit ;)
75 (global $quit_p i32 (i32.const 16384))
76 (data (i32.const 16384) "\01\00\00\00") ;; RINIT xt
77 (global $quit_ret_p i32 (i32.const 16388))
78 (data (i32.const 16388) "\10\40\00\00") ;; INTERPRET xt
79 (data (i32.const 16392) "\12\00\00\00") ;; JMP xt
80 (data (i32.const 16396) "\00\40\00\00") ;; quit location (16384)
81 (; Interpret ;)
82 (data (i32.const 16400) "\74\40\00\00") ;; WORD xt (16500)
83 (data (i32.const 16404) "\06\00\00\00") ;; DUP
84 (data (i32.const 16408) "\0e\00\00\00") ;; JZ:
85 (data (i32.const 16412) "\38\40\00\00") ;; INTERP-END addr (16444)
86 (data (i32.const 16416) "\02\00\00\00") ;; LIT xt
87 (data (i32.const 16420) "\00\38\00\00") ;; MODE addr (14336)
88 (data (i32.const 16424) "\0a\00\00\00") ;; @ (fetch) xt
89 (data (i32.const 16428) "\0c\00\00\00") ;; EXECUTE xt
90 (data (i32.const 16432) "\0d\00\00\00") ;; NOOP xt
91 (data (i32.const 16436) "\01\00\00\00") ;; RET
92 (data (i32.const 16440) "\10\00\00\00") ;; DROP <-- INTERP-END
93 (data (i32.const 16444) "\10\00\00\00") ;; DROP
94 (data (i32.const 16448) "\19\00\00\00") ;; BYE
95 (; Word ;)
96 (data (i32.const 16500) "\14\00\00\00") ;; WORDSTART
97 (data (i32.const 16504) "\05\00\00\00") ;; KEY <-- KEYLOOP
98 (data (i32.const 16508) "\06\00\00\00") ;; DUP
99 (data (i32.const 16512) "\18\00\00\00") ;; J-1: 18
100 (data (i32.const 16516) "\f0\40\00\00") ;; addr of WORDEND
101 (data (i32.const 16520) "\11\00\00\00") ;; WS?
102 (data (i32.const 16524) "\0f\00\00\00") ;; JNZ:
103 (data (i32.const 16528) "\bc\40\00\00") ;; addr of KEYDROP
104 (data (i32.const 16532) "\02\00\00\00") ;; LIT
105 (data (i32.const 16536) "\5c\00\00\00") ;; 92 (\ character)
106 (data (i32.const 16540) "\25\00\00\00") ;; =?
107 (data (i32.const 16544) "\0e\00\00\00") ;; JZ:
108 (data (i32.const 16548) "\e4\40\00\00") ;; addr of DOCHAR
109 (data (i32.const 16552) "\10\00\00\00") ;; DROP
110 (data (i32.const 16556) "\94\41\00\00") ;; DO-BACKSLASH (continue using this wbuf we started)
111 (data (i32.const 16560) "\0d\00\00\00") ;; NOOP
112 (data (i32.const 16564) "\12\00\00\00") ;; JMP:
113 (data (i32.const 16568) "\74\40\00\00") ;; addr of KEYLOOP-1 (get a new wbuf, call to wbuf+1 ate ours)
114 (data (i32.const 16572) "\10\00\00\00") ;; DROP <-- KEYDROP
115 (data (i32.const 16576) "\12\00\00\00") ;; JMP:
116 (data (i32.const 16580) "\78\40\00\00") ;; addr of KEYLOOP
117 (data (i32.const 16584) "\05\00\00\00") ;; KEY <-- WORDLOOP
118 (data (i32.const 16588) "\11\00\00\00") ;; WS?
119 (data (i32.const 16592) "\0f\00\00\00") ;; JNZ:
120 (data (i32.const 16596) "\f0\40\00\00") ;; addr of WORDEND
121 (data (i32.const 16600) "\06\00\00\00") ;; DUP
122 (data (i32.const 16604) "\18\00\00\00") ;; J-1:
123 (data (i32.const 16608) "\f0\40\00\00") ;; addr of WORDEND
124 (data (i32.const 16612) "\13\00\00\00") ;; WORDPUTC <-- DOCHAR
125 (data (i32.const 16616) "\12\00\00\00") ;; JMP:
126 (data (i32.const 16620) "\c8\40\00\00") ;; addr of WORDLOOP
127 (data (i32.const 16624) "\10\00\00\00") ;; DROP <-- WORDEND
128 (data (i32.const 16628) "\17\00\00\00") ;; WORDFINISH
129 (data (i32.const 16632) "\01\00\00\00") ;; RET
130 (; Exec Mode ;)
131 (data (i32.const 16680) "\1e\00\00\00") ;; 2DUP
132 (data (i32.const 16684) "\15\00\00\00") ;; DICT_GET
133 (data (i32.const 16688) "\06\00\00\00") ;; DUP
134 (data (i32.const 16692) "\0e\00\00\00") ;; JZ:
135 (data (i32.const 16696) "\4c\41\00\00") ;; donum -1 (16716)
136 (data (i32.const 16700) "\1f\00\00\00") ;; ROT
137 (data (i32.const 16704) "\20\00\00\00") ;; 2DROP
138 (data (i32.const 16708) "\0c\00\00\00") ;; EXECUTE
139 (data (i32.const 16712) "\01\00\00\00") ;; RET
140 (data (i32.const 16716) "\10\00\00\00") ;; DROP (xt from dictionary)
141 (data (i32.const 16720) "\16\00\00\00") ;; NUMBER <-- donum, pushes NUM, UNPARSED
142 (data (i32.const 16724) "\06\00\00\00") ;; DUP
143 (data (i32.const 16728) "\0f\00\00\00") ;; JNZ:
144 (data (i32.const 16732) "\68\41\00\00") ;; donum_err (16744)
145 (data (i32.const 16736) "\10\00\00\00") ;; DROP
146 (data (i32.const 16740) "\01\00\00\00") ;; RET
147 (data (i32.const 16744) "\20\00\00\00") ;; 2DROP <-- donum_err
148 (data (i32.const 16748) "\02\00\00\00") ;; LIT
149 (data (i32.const 16752) "\04\30\00\00") ;; INBUFSIZE LOCATION
150 (data (i32.const 16756) "\02\00\00\00") ;; LIT
151 (data (i32.const 16760) "\00\00\00\00") ;; 0
152 (data (i32.const 16764) "\0b\00\00\00") ;; !
153 (data (i32.const 16768) "\19\00\00\00") ;; BYE
154 (data (i32.const 16772) "\00\00\00\00") ;;
155 (data (i32.const 16776) "\00\00\00\00") ;;
156 (data (i32.const 16780) "\00\00\00\00") ;;
157 (data (i32.const 16784) "\00\00\00\00") ;;
158 (; Do Backslash ;)
159 (data (i32.const 16788) "\05\00\00\00") ;; KEY
160 (data (i32.const 16792) "\11\00\00\00") ;; WS?
161 (data (i32.const 16796) "\0f\00\00\00") ;; JNZ:
162 (data (i32.const 16800) "\c8\41\00\00") ;; addr of keypump + 3
163 (data (i32.const 16804) "\cc\40\00\00") ;; WORDLOOP + 1
164 (data (i32.const 16808) "\28\41\00\00") ;; EXECUTE-MODE
165 (data (i32.const 16812) "\01\00\00\00") ;; RET
166 (; Do Comment ;)
167 (data (i32.const 16828) "\18\00\00\00") ;; j-1: <-- keypump
168 (data (i32.const 16832) "\e0\41\00\00") ;; addr of end
169 (data (i32.const 16836) "\05\00\00\00") ;; KEY
170 (data (i32.const 16840) "\02\00\00\00") ;; LIT
171 (data (i32.const 16844) "\0a\00\00\00") ;; 10 (line feed)
172 (data (i32.const 16848) "\25\00\00\00") ;; =?
173 (data (i32.const 16852) "\0e\00\00\00") ;; JZ:
174 (data (i32.const 16856) "\bc\41\00\00") ;; addr of keypump
175 (data (i32.const 16860) "\10\00\00\00") ;; DROP
176 (data (i32.const 16864) "\01\00\00\00") ;; RET
177
178 (export "memory" (memory $0))
179 (export "main" (func $main))
180 (func $main (param $event_channel i32) (result i32)
181 block $use_current_channel
182 (; rstack contains channel barriers (numbers lower than 256)
183 which will reset channel to 0 when returning to the quit loop.
184 if an interrupt event is happening, load its handler and set
185 the input channel. ;)
186 get_local $event_channel
187 i32.eqz
188 br_if $use_current_channel
189 get_local $event_channel
190 i32.const 255
191 i32.gt_u
192 br_if $use_current_channel
193 get_global $channel_in_p
194 get_local $event_channel
195 i32.store
196 end
197 get_global $channel_listeners_p
198 get_local $event_channel
199 i32.const 2
200 i32.shl
201 i32.add
202 i32.load
203 call $interpret
204 )
205 (func $interpret (param $esi i32) (result i32)
206 (local $here i32)
207 (local $eax i32)
208 (local $inbuf_head i32)
209 (local $stringbelt_tail i32)
210 (local $stringbelt_head i32)
211 (local $wordbelt_tail i32)
212 (local $wordbelt_head i32)
213 (local $channel_in i32)
214 (local $channel_out i32)
215 get_global $here_p
216 i32.load
217 set_local $here
218 get_global $inbuf_data
219 set_local $inbuf_head
220 get_global $stringbelt_tail_p
221 i32.load
222 set_local $stringbelt_tail
223 get_global $stringbelt_head_p
224 i32.load
225 set_local $stringbelt_head
226 get_global $wordbelt_tail_p
227 i32.load
228 set_local $wordbelt_tail
229 get_global $wordbelt_head_p
230 i32.load
231 set_local $wordbelt_head
232 get_global $channel_in_p
233 i32.load
234 set_local $channel_in
235 get_global $channel_out_p
236 i32.load
237 set_local $channel_out
238 get_global $quit_p
239 set_local $esi
240 block $bye
241 loop $next
242 get_local $esi
243 get_local $esi
244 i32.const 4
245 i32.add
246 set_local $esi
247 i32.load
248 set_local $eax
249 loop $execloop
250 block $default block $op0 block $ret block $lit block $rinit
251 block $word block $key block $dup block $plus block $noop2 block $emit
252 block $fetch block $set block $execute block $noop block $jz block $jnz
253 block $drop block $wsbool block $jmp block $wordputc block $wordstart
254 block $dictget block $parsenum block $wordfinish block $jneg1 block $swap
255 block $words block $here block $dictset block $dup2 block $rot block $drop2
256 block $comma block $subtract block $inchan block $sethere block $eqbool
257 block $echostring block $strstart block $strput block $strend block $fetchinc
258 block $setinc block $finddoes block $definedoes block $stacktrace block $webfetch
259 block $outchan block $read
260 get_local $eax
261 br_table $op0 $ret (;2;)$lit $rinit (;4;)$word $key (;6;)$dup $plus
262 (;8;)$jmp $emit (;10;)$fetch $set (;12;)$execute $noop (;14;)$jz $jnz
263 (;16;)$drop $wsbool (;18;)$jmp $wordputc (;20;)$wordstart $dictget
264 (;22;)$parsenum $wordfinish (;24;)$jneg1 $bye (;26;)$swap $words
265 (;28;)$here $dictset (;30;)$dup2 $rot (;32;)$drop2 $comma
266 (;34;)$subtract $inchan (;36;)$sethere $eqbool (;38;)$echostring $strstart
267 (;40;)$strput $strend (;42;)$fetchinc $setinc (;44;)$finddoes $definedoes
268 (;46;)$stacktrace $webfetch (;48;)$outchan $read (;50;)$default
269 end ;; read
270 get_local $channel_in
271 call $pop ;; location to write
272 set_local $eax
273 call $pop
274 get_local $eax
275 call $sys_read
276 br $next
277 end ;; outchan
278 call $pop
279 set_local $channel_out
280 br $next
281 end ;; webfetch
282 call $pop
283 call $rpush
284 call $pop
285 set_local $eax
286 call $pop
287 get_local $eax
288 call $rpop
289 call $sys_fetch
290 br $next
291 end ;; stacktrace
292 call $sys_stack
293 get_local $esi
294 call $sys_reflect
295 br $next
296 end ;; definedoes
297 call $pop
298 call $rpush
299 call $pop
300 set_local $eax
301 call $pop
302 get_local $eax
303 call $rpop
304 call $does_set
305 br $next
306 end ;; finddoes
307 call $pop
308 set_local $eax
309 call $pop
310 get_local $eax
311 call $does_get
312 call $push
313 br $next
314 end ;; setinc
315 call $pop
316 call $rpush
317 call $pop
318 tee_local $eax
319 call $rpop
320 i32.store
321 get_local $eax
322 i32.const 4
323 i32.add
324 call $push
325 br $next
326 end ;; fetchinc
327 call $pop
328 tee_local $eax
329 i32.const 4
330 i32.add
331 call $push
332 get_local $eax
333 i32.load
334 call $push
335 br $next
336 end ;; strend
337 get_local $stringbelt_tail
338 get_local $stringbelt_head
339 get_local $stringbelt_tail
340 i32.const 4
341 i32.add
342 i32.sub
343 tee_local $eax (; n bytes ;)
344 i32.store
345 (; align to 32-bit ;)
346 get_local $stringbelt_head
347 i32.const 3
348 i32.add
349 i32.const 8188
350 i32.and
351 set_local $stringbelt_head
352 (; /align ;)
353 get_local $stringbelt_tail
354 i32.const 4
355 i32.add
356 call $push
357 get_local $eax
358 call $push
359 br $next
360 end ;; strput
361 block $sbhasspace2
362 get_local $stringbelt_head
363 get_global $wordbelt
364 i32.lt_u
365 br_if $sbhasspace2
366 i32.const 0
367 tee_local $stringbelt_head
368 get_local $stringbelt_tail
369 i32.load
370 i32.store
371 get_local $stringbelt_head
372 i32.const 4
373 i32.add
374 set_local $stringbelt_head
375 get_local $stringbelt_tail
376 i32.const 4
377 i32.add
378 set_local $stringbelt_tail
379 loop $copystringtostart
380 get_local $stringbelt_head
381 get_local $stringbelt_tail
382 i32.load16_u
383 i32.store16
384 get_local $stringbelt_head
385 i32.const 2
386 i32.add
387 set_local $stringbelt_head
388 get_local $stringbelt_tail
389 i32.const 2
390 i32.add
391 tee_local $stringbelt_tail
392 get_global $wordbelt
393 i32.le_u
394 br_if $copystringtostart
395 end
396 i32.const 0
397 set_local $stringbelt_tail
398 end
399 get_local $stringbelt_head
400 call $pop
401 i32.store16
402 get_local $stringbelt_head
403 i32.const 2
404 i32.add
405 set_local $stringbelt_head
406 br $next
407 end ;; strstart
408 block $sbhasspace
409 get_local $stringbelt_head
410 get_global $wordbelt
411 i32.const 8
412 i32.sub
413 i32.le_u
414 br_if $sbhasspace
415 i32.const 0
416 set_local $stringbelt_head
417 end
418 get_local $stringbelt_head
419 get_local $stringbelt_head
420 tee_local $stringbelt_tail
421 i32.const 0
422 i32.store
423 i32.const 4
424 i32.add
425 set_local $stringbelt_head
426 br $next
427 end ;; echostring
428 get_local $channel_out
429 call $pop
430 set_local $eax
431 call $pop
432 get_local $eax
433 call $sys_write
434 br $next
435 end ;; eqbool
436 block $equiv
437 call $pop
438 call $pop
439 tee_local $eax
440 i32.eq
441 get_local $eax
442 call $push
443 br_if $equiv
444 i32.const 0
445 call $push
446 br $next
447 end
448 i32.const 1
449 call $push
450 br $next
451 end ;; sethere
452 call $pop
453 set_local $here
454 br $next
455 end ;; inchan
456 call $pop
457 set_local $channel_in
458 br $next
459 end ;; subtract
460 call $pop
461 set_local $eax
462 call $pop
463 get_local $eax
464 i32.sub
465 call $push
466 br $next
467 end ;; comma
468 get_local $here
469 call $pop
470 i32.store
471 get_local $here
472 i32.const 4
473 i32.add
474 set_local $here
475 br $next
476 end ;; drop2
477 call $pop
478 call $pop
479 drop
480 drop
481 br $next
482 end ;; rot
483 call $pop
484 call $pop
485 set_local $eax
486 call $pop
487 call $rpush
488 call $push
489 call $rpop
490 call $push
491 get_local $eax
492 call $push
493 br $next
494 end ;; dup2
495 get_local $esi
496 call $rpush
497 call $pop
498 set_local $eax
499 call $pop
500 tee_local $esi
501 call $push
502 get_local $eax
503 call $push
504 get_local $esi
505 call $push
506 get_local $eax
507 call $push
508 call $rpop
509 set_local $esi
510 br $next
511 end ;; dictset
512 call $pop
513 call $rpush
514 call $pop
515 set_local $eax
516 call $pop
517 get_local $eax
518 call $rpop
519 call $vocab_set
520 br $next
521 end ;; here
522 get_local $here
523 call $push
524 br $next
525 end ;; words
526 call $sys_words
527 br $next
528 end ;; swap
529 call $pop
530 call $pop
531 set_local $eax
532 call $push
533 get_local $eax
534 call $push
535 br $next
536 end ;; jneg1
537 block $jneg1if
538 call $pop
539 i32.const -1
540 i32.eq
541 br_if $jneg1if
542 get_local $esi
543 i32.const 4
544 i32.add
545 set_local $esi
546 br $next
547 end
548 get_local $esi
549 i32.load
550 set_local $esi
551 br $next
552 end ;; wordfinish
553 get_local $wordbelt_tail
554 get_local $wordbelt_head
555 get_local $wordbelt_tail
556 i32.const 4
557 i32.add
558 i32.sub
559 tee_local $eax (; n bytes ;)
560 i32.store
561 (; align to 32-bit ;)
562 get_local $wordbelt_head
563 i32.const 3
564 i32.add
565 i32.const 12284
566 i32.and
567 set_local $wordbelt_head
568 (; /align ;)
569 get_local $wordbelt_tail
570 i32.const 4
571 i32.add
572 call $push
573 get_local $eax
574 call $push
575 br $next
576 end ;; parsenum
577 call $pop
578 call $rpush
579 call $pop
580 tee_local $eax
581 call $rpop
582 get_global $base_p
583 i32.load
584 call $sys_parsenum
585 get_local $eax
586 i32.load
587 call $push
588 call $push
589 br $next
590 end ;; dictget
591 call $pop
592 set_local $eax
593 call $pop
594 get_local $eax
595 call $vocab_get
596 call $push
597 br $next
598 end ;; wordstart
599 block $wbhasspace
600 get_local $wordbelt_head
601 get_global $inbuf
602 i32.const 8
603 i32.sub
604 i32.le_u
605 br_if $wbhasspace
606 get_global $wordbelt
607 set_local $wordbelt_head
608 end
609 get_local $wordbelt_head
610 get_local $wordbelt_head
611 tee_local $wordbelt_tail
612 i32.const 0
613 i32.store
614 i32.const 4
615 i32.add
616 set_local $wordbelt_head
617 br $next
618 end ;; wordputc
619 block $wbhasspace2
620 get_local $wordbelt_head
621 get_global $inbuf
622 i32.lt_u
623 br_if $wbhasspace2
624 get_global $wordbelt
625 tee_local $wordbelt_head
626 get_local $wordbelt_tail
627 i32.load
628 i32.store
629 get_local $wordbelt_head
630 i32.const 4
631 i32.add
632 set_local $wordbelt_head
633 get_local $wordbelt_tail
634 i32.const 4
635 i32.add
636 set_local $wordbelt_tail
637 loop $copywordtostart
638 get_local $wordbelt_head
639 get_local $wordbelt_tail
640 i32.load16_u
641 i32.store16
642 get_local $wordbelt_head
643 i32.const 2
644 i32.add
645 set_local $wordbelt_head
646 get_local $wordbelt_tail
647 i32.const 2
648 i32.add
649 tee_local $wordbelt_tail
650 get_global $inbuf
651 i32.le_u
652 br_if $copywordtostart
653 end
654 get_global $wordbelt
655 set_local $wordbelt_tail
656 end
657 get_local $wordbelt_head
658 call $pop
659 i32.store16
660 get_local $wordbelt_head
661 i32.const 2
662 i32.add
663 set_local $wordbelt_head
664 br $next
665 end ;; jmp
666 get_local $esi
667 i32.load
668 set_local $esi
669 br $next
670 end ;; wsbool
671 call $pop
672 tee_local $eax
673 call $is_whitespace
674 get_local $eax
675 call $push
676 call $push
677 br $next
678 end ;; drop
679 call $pop
680 drop
681 br $next
682 end ;; jnz
683 block $jnzif
684 call $pop
685 i32.eqz
686 br_if $jnzif
687 get_local $esi
688 i32.load
689 set_local $esi
690 br $next
691 end
692 get_local $esi
693 i32.const 4
694 i32.add
695 set_local $esi
696 br $next
697 end ;; jz
698 block $jzif
699 call $pop
700 i32.eqz
701 br_if $jzif
702 get_local $esi
703 i32.const 4
704 i32.add
705 set_local $esi
706 br $next
707 end
708 get_local $esi
709 i32.load
710 set_local $esi
711 br $next
712 end ;; noop
713 br $next
714 end ;; execute
715 call $pop
716 tee_local $eax
717 i32.const 256
718 i32.lt_u
719 br_if $execloop
720 get_local $esi
721 call $rpush
722 get_local $eax
723 set_local $esi
724 br $next
725 end ;; set
726 call $pop
727 set_local $eax
728 call $pop
729 get_local $eax
730 i32.store
731 br $next
732 end ;; fetch
733 call $pop
734 i32.load
735 call $push
736 br $next
737 end ;; emit (.)
738 call $pop
739 get_global $base_p
740 i32.load
741 call $sys_echo
742 br $next
743 end ;; noop2
744 br $next
745 end ;; plus
746 call $pop
747 call $pop
748 i32.add
749 call $push
750 br $next
751 end ;; dup
752 call $pop
753 tee_local $eax
754 get_local $eax
755 call $push
756 call $push
757 br $next
758 end ;; key
759 loop $key_loop
760 block $key_read
761 get_global $inbuf_size
762 i32.load
763 get_local $inbuf_head
764 get_global $inbuf_data
765 i32.sub
766 i32.le_u
767 br_if $key_read
768 get_local $inbuf_head
769 i32.load16_u
770 call $push
771 get_local $inbuf_head
772 i32.const 2
773 i32.add
774 set_local $inbuf_head
775 br $next
776 end ;; key_read
777 get_global $inbuf_size
778 i32.const 0 (; stdin hardcode ;)
779 get_global $inbuf_data
780 get_global $inbuf
781 i32.load
782 call $sys_read
783 i32.store
784 block $nullread
785 get_global $inbuf_size
786 i32.load
787 i32.eqz
788 br_if $nullread
789 get_global $inbuf_data
790 set_local $inbuf_head
791 br $key_loop
792 end ;; nullread
793 i32.const -1 ;; <- keyval sent if sz == 0
794 call $push
795 br $next
796 end ;; key_loop
797 end ;; word
798 br $next
799 end ;; rinit
800 call $rpop
801
802 call $rinit
803 i32.const 0
804 set_local $channel_in
805 br $next
806 end ;; lit
807 get_local $esi
808 get_local $esi
809 i32.const 4
810 i32.add
811 set_local $esi
812 i32.load
813 call $push
814 br $next
815 end ;; ret
816 block $gotonext
817 (; cannot jump lower than 256 because it is reserved for
818 opcodes, so overload popping that kind of retval with
819 a channel selector, for when an event handler yields and
820 is interrupted by another event handler. there are also
821 a max of 255 channels, which is the same as the opcode space ;)
822 call $rpop
823 tee_local $eax
824 i32.const 255
825 i32.gt_u
826 br_if $gotonext
827 get_local $eax
828 set_local $channel_in
829 call $rpop
830 set_local $eax
831 end ;; gotonext
832 get_local $eax
833 set_local $esi
834 br $next
835 end ;; op0
836 get_local $esi
837 call $rpush
838 br $bye
839 end ;; default
840 get_local $esi
841 call $rpush
842 get_local $eax
843 set_local $esi
844 br $next
845 end ;; execloop
846 end ;; next loop
847 end ;; bye
848 get_global $here_p
849 get_local $here
850 i32.store
851 get_global $stringbelt_tail_p
852 get_local $stringbelt_tail
853 i32.store
854 get_global $stringbelt_head_p
855 get_local $stringbelt_head
856 i32.store
857 get_global $wordbelt_tail_p
858 get_local $wordbelt_tail
859 i32.store
860 get_global $wordbelt_head_p
861 get_local $wordbelt_head
862 i32.store
863 get_global $channel_in_p
864 get_local $channel_in
865 i32.store
866 get_global $channel_out_p
867 get_local $channel_out
868 i32.store
869 i32.const 0
870 return
871 )
872 )