working: real time system (event-driven), coroutines, asynch sockets
[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_write" (func $sys_write (param i32 i32 i32)))
29 (import "env" "sys_send" (func $sys_send (param i32 i32 i32)))
30 (import "env" "sys_open" (func $sys_open (result i32)))
31 (import "env" "sys_close" (func $sys_close (param i32)))
32 (import "env" "sys_fetch" (func $sys_fetch (param i32 i32 i32) (result i32)))
33 (import "env" "sys_connect" (func $sys_connect (param i32 i32) (result i32)))
34 (import "env" "sys_echo" (func $sys_echo (param i32)))
35 (import "env" "sys_log" (func $sys_log (param i32 i32)))
36 (import "env" "sys_reflect" (func $sys_reflect (param i32)))
37 (import "env" "vocab_get" (func $vocab_get (param i32 i32) (result i32)))
38 (import "env" "vocab_set" (func $vocab_set (param i32 i32 i32)))
39 (import "env" "does_get" (func $does_get (param i32 i32) (result i32)))
40 (import "env" "does_set" (func $does_set (param i32 i32 i32)))
41 (import "env" "is_whitespace" (func $is_whitespace (param i32) (result i32)))
42 (import "env" "sys_parsenum" (func $sys_parsenum (param i32 i32) (result i32)))
43 (import "env" "sys_stack" (func $sys_stack))
44 (import "env" "sys_words" (func $sys_words))
45 (table (;0;) 0 anyfunc)
46 (memory $0 1)
47 (; String Belt ;) ;; 0x0000 Size: 8192
48 (global $wordbelt_base i32 (i32.const 8192) ) ;; 0x2000 Size: 4096
49 (global $wordbelt_bound i32 (i32.const 12288)) ;; 0x3000
50 (global $stdin_base i32 (i32.const 12288)) ;; 0x3000 Size: 2048
51 (global $kvars i32 (i32.const 14336)) ;; 0x3800 Size: 2048
52 (global $mode_p i32 (i32.const 14336))
53 (global $here_p i32 (i32.const 14340))
54 (global $start_p i32 (i32.const 14344))
55 (global $base_p i32 (i32.const 14348))
56 (global $stringbelt_tail_p i32 (i32.const 14352))
57 (global $stringbelt_head_p i32 (i32.const 14356))
58 (global $wordbelt_tail_p i32 (i32.const 14360))
59 (global $wordbelt_head_p i32 (i32.const 14364))
60 (data (i32.const 14336) "\28\41\00\00") ;; MODE
61 (data (i32.const 14340) "\04\5e\00\00") ;; HERE
62 (data (i32.const 14344) "\00\40\00\00") ;; START (16384) (Quit)
63 (data (i32.const 14348) "\0a\00\00\00") ;; BASE
64 (data (i32.const 14352) "\00\00\00\00") ;; STRINGBELT_TAIL
65 (data (i32.const 14356) "\00\00\00\00") ;; STRINGBELT_HEAD
66 (data (i32.const 14360) "\00\20\00\00") ;; WORDBELT_TAIL
67 (data (i32.const 14364) "\00\20\00\00") ;; WORDBELT_HEAD
68 (; Quit ;)
69 (global $quit_p i32 (i32.const 16384)) ;; 0x4000
70 (data (i32.const 16384) "\03\00\00\00") ;; RINIT xt
71 (global $quit_ret_p i32 (i32.const 16388))
72 (data (i32.const 16388) "\10\40\00\00") ;; INTERPRET xt
73 (data (i32.const 16392) "\12\00\00\00") ;; JMP xt
74 (data (i32.const 16396) "\00\40\00\00") ;; quit location (16384)
75 (; Interpret ;)
76 (data (i32.const 16400) "\74\40\00\00") ;; WORD xt (16500)
77 (data (i32.const 16404) "\0d\00\00\00") ;; (data (i32.const 16404) "\1e\00\00\00") ;; 2DUP
78 (data (i32.const 16408) "\0d\00\00\00") ;; (data (i32.const 16408) "\04\00\00\00") ;; SYS-LOG
79 (data (i32.const 16412) "\06\00\00\00") ;; DUP
80 (data (i32.const 16416) "\0e\00\00\00") ;; JZ:
81 (data (i32.const 16420) "\40\40\00\00") ;; INTERP-END addr (16444)
82 (data (i32.const 16424) "\02\00\00\00") ;; LIT xt
83 (data (i32.const 16428) "\00\38\00\00") ;; MODE addr (14336)
84 (data (i32.const 16432) "\0a\00\00\00") ;; @ (fetch) xt
85 (data (i32.const 16436) "\0c\00\00\00") ;; EXECUTE xt
86 (data (i32.const 16440) "\0d\00\00\00") ;; NOOP xt
87 (data (i32.const 16444) "\01\00\00\00") ;; RET
88 (data (i32.const 16448) "\10\00\00\00") ;; DROP <-- INTERP-END
89 (data (i32.const 16452) "\10\00\00\00") ;; DROP
90 ;; (data (i32.const 16456) "\01\00\00\00") ;; RET
91 (global $holy_bye i32 (i32.const 16456))
92 (data (i32.const 16456) "\19\00\00\00") ;; BYE <-- the Holy BYE
93 (; Word ;)
94 (data (i32.const 16500) "\14\00\00\00") ;; WORDSTART
95 (data (i32.const 16504) "\05\00\00\00") ;; KEY <-- KEYLOOP
96 (data (i32.const 16508) "\06\00\00\00") ;; DUP
97 (data (i32.const 16512) "\18\00\00\00") ;; J-1: 18
98 (data (i32.const 16516) "\f0\40\00\00") ;; addr of WORDEND
99 (data (i32.const 16520) "\11\00\00\00") ;; WS?
100 (data (i32.const 16524) "\0f\00\00\00") ;; JNZ:
101 (data (i32.const 16528) "\bc\40\00\00") ;; addr of KEYDROP
102 (data (i32.const 16532) "\02\00\00\00") ;; LIT
103 (data (i32.const 16536) "\5c\00\00\00") ;; 92 (\ character)
104 (data (i32.const 16540) "\25\00\00\00") ;; =?
105 (data (i32.const 16544) "\0e\00\00\00") ;; JZ:
106 (data (i32.const 16548) "\e4\40\00\00") ;; addr of DOCHAR
107 (data (i32.const 16552) "\10\00\00\00") ;; DROP
108 (data (i32.const 16556) "\94\41\00\00") ;; DO-BACKSLASH (continue using this wbuf we started)
109 (data (i32.const 16560) "\0d\00\00\00") ;; NOOP
110 (data (i32.const 16564) "\12\00\00\00") ;; JMP:
111 (data (i32.const 16568) "\74\40\00\00") ;; addr of KEYLOOP-1 (get a new wbuf, call to wbuf+1 ate ours)
112 (data (i32.const 16572) "\10\00\00\00") ;; DROP <-- KEYDROP
113 (data (i32.const 16576) "\12\00\00\00") ;; JMP:
114 (data (i32.const 16580) "\78\40\00\00") ;; addr of KEYLOOP
115 (data (i32.const 16584) "\05\00\00\00") ;; KEY <-- WORDLOOP
116 (data (i32.const 16588) "\11\00\00\00") ;; WS?
117 (data (i32.const 16592) "\0f\00\00\00") ;; JNZ:
118 (data (i32.const 16596) "\f0\40\00\00") ;; addr of WORDEND
119 (data (i32.const 16600) "\06\00\00\00") ;; DUP
120 (data (i32.const 16604) "\18\00\00\00") ;; J-1:
121 (data (i32.const 16608) "\f0\40\00\00") ;; addr of WORDEND
122 (data (i32.const 16612) "\13\00\00\00") ;; WORDPUTC <-- DOCHAR
123 (data (i32.const 16616) "\12\00\00\00") ;; JMP:
124 (data (i32.const 16620) "\c8\40\00\00") ;; addr of WORDLOOP
125 (data (i32.const 16624) "\10\00\00\00") ;; DROP <-- WORDEND
126 (data (i32.const 16628) "\17\00\00\00") ;; WORDFINISH
127 (data (i32.const 16632) "\01\00\00\00") ;; 2DUP //RET
128 (data (i32.const 16636) "\04\00\00\00") ;; .S
129 (data (i32.const 16640) "\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 (; Channel Table ;)
178 (; 1 FLAGS: AWAITER | RUNNING ]LSB ;)
179 (; 1 reserved ;)
180 (; 1 AWAITER CHANNEL ;)
181 (; 1 OUT CHANNEL ;)
182 (; 8 START | START-DEFAULT ;)
183 (; 16 BUFFER ADDRESSES: BASE, TAIL, HEAD, BOUND ;)
184 (global $channel_table_p i32 (i32.const 16900))
185 (global $channel_entry_size i32 (i32.const 28))
186 (global $channel_max i32 (i32.const 255))
187 (data (i32.const 16900) "\00\00\00\01") ;; STDIN (COUT: 1)
188 (data (i32.const 16904) "\00\40\00\00") ;; STDIN-START (QUIT)
189 (data (i32.const 16908) "\00\40\00\00") ;; STDIN-START-DEFAULT
190 (data (i32.const 16912) "\00\30\00\00") ;; STDIN-BUFFER-BASE
191 (data (i32.const 16916) "\00\30\00\00") ;; STDIN-BUFFER-TAIL
192 (data (i32.const 16920) "\00\30\00\00") ;; STDIN-BUFFER-HEAD
193 (data (i32.const 16924) "\00\38\00\00") ;; STDIN-BUFFER-BOUND
194 (data (i32.const 16928) "\00\00\00\00") ;; STDOUT
195 (data (i32.const 16932) "\00\00\00\00") ;; STDOUT (TODO: error handler)
196 (data (i32.const 16936) "\00\00\00\00") ;; STDOUT
197 (data (i32.const 16940) "\00\00\00\00") ;; STDOUT
198 (data (i32.const 16944) "\00\00\00\00") ;; STDOUT
199 (data (i32.const 16948) "\00\00\00\00") ;; STDOUT
200 (data (i32.const 16952) "\00\00\00\00") ;; STDOUT
201 (data (i32.const 16956) "\00\00\00\00") ;; STDERR (null)
202 (data (i32.const 16960) "\00\00\00\00") ;; STDERR (TODO: error handler)
203 (; 16900 + ((4 * 7)=>28 * 256)=>7168 = 24068 | 0x5e04 === HERE ;)
204 (export "memory" (memory $0))
205 (func $lit_rstack (param $here i32) (param $start i32) (result i32)
206 (local $eax i32) (local $ecx i32)
207 i32.const 0
208 set_local $ecx
209 block $backup_loop
210 call $rpop
211 tee_local $eax
212 get_global $holy_bye
213 i32.eq
214 br_if $backup_loop
215 get_local $eax
216 call $push
217 get_local $ecx
218 i32.const 1
219 i32.add
220 set_local $ecx
221 end
222 block $output_done
223 block $output_loop
224 get_local $ecx
225 i32.eqz
226 br_if $output_done
227 get_local $ecx
228 i32.const -1
229 i32.add
230 set_local $ecx
231 get_local $here
232 i32.const 2 ;; lit
233 i32.store
234 get_local $here
235 i32.const 4
236 i32.add
237 tee_local $here
238 call $pop
239 i32.store
240 get_local $here
241 i32.const 4
242 i32.add
243 tee_local $here
244 i32.const 52 ;; rpush_op
245 i32.store
246 get_local $here
247 i32.const 4
248 i32.add
249 set_local $here
250 br $output_loop
251 end
252 end
253 get_local $here
254 i32.const 18 ;; jmp
255 i32.store
256 get_local $here
257 i32.const 4
258 i32.add
259 tee_local $here
260 get_local $start
261 i32.store
262 get_local $here
263 i32.const 4
264 i32.add
265 return
266 )
267 (func $close_channel (param $channel_p i32)
268 block $no_close
269 get_local $channel_p
270 i32.const 3
271 i32.le_u
272 br_if $no_close
273 get_local $channel_p
274 call $sys_close
275 end
276 get_global $channel_table_p
277 get_global $channel_entry_size
278 get_local $channel_p
279 i32.mul
280 i32.add
281 tee_local $channel_p
282 i32.const 4
283 i32.add
284 get_local $channel_p
285 i32.const 8
286 i32.add
287 i32.load
288 i32.store ;; restore awaiter's "start" to original
289 get_local $channel_p
290 i32.const 0
291 i32.store8 ;; clear target thread's flags
292 )
293 (func $forth_min (param $i1 i32) (param $i2 i32) (result i32)
294 block $is_greater
295 get_local $i1
296 get_local $i2
297 i32.lt_u
298 br_if $is_greater
299 get_local $i2
300 return
301 end
302 get_local $i1
303 return
304 )
305 (export "main" (func $main))
306 (func $main (param $event_channel i32) (result i32)
307 get_local $event_channel
308 call $rinit
309 get_global $holy_bye
310 call $rpush
311 call $interpret
312 return
313 )
314 (func $interpret (param $channel_in i32) (result i32)
315 (local $here i32)
316 (local $eax i32)
317 (local $esi i32)
318 (local $inbuf_base i32)
319 (local $inbuf_tail i32)
320 (local $inbuf_head i32)
321 (local $inbuf_bound i32)
322 (local $stringbelt_tail i32)
323 (local $stringbelt_head i32)
324 (local $wordbelt_tail i32)
325 (local $wordbelt_head i32)
326 (local $channel_out i32)
327
328 i32.const 0
329 set_local $eax
330
331 loop $recurse_loop
332 block $close_yield_channel
333 get_local $eax
334 i32.eqz
335 br_if $close_yield_channel
336 get_local $eax
337 call $close_channel
338 end
339 (; channel in setup ;)
340 get_global $channel_table_p
341 get_global $channel_entry_size
342 get_global $channel_max
343 get_local $channel_in
344 call $forth_min
345 i32.mul
346 i32.add
347 set_local $eax
348 block $check_run
349 get_local $eax
350 i32.load8_u
351 i32.const 1
352 i32.and
353 i32.eqz ;; (FLAGS & 1) => running, return 0
354 br_if $check_run
355 i32.const 0
356 return
357 end
358 get_local $eax
359 get_local $eax
360 i32.load8_u
361 i32.const 1
362 i32.or
363 i32.store8 ;; set running flag
364 get_local $eax
365 i32.const 3
366 i32.add
367 i32.load8_u
368 set_local $channel_out
369
370 get_local $eax
371 i32.const 4
372 i32.add
373 i32.load
374 set_local $esi
375 get_local $eax
376 i32.const 12
377 i32.add
378 i32.load
379 set_local $inbuf_base
380 get_local $eax
381 i32.const 16
382 i32.add
383 i32.load
384 set_local $inbuf_tail
385 get_local $eax
386 i32.const 20
387 i32.add
388 i32.load
389 set_local $inbuf_head
390 get_local $eax
391 i32.const 24
392 i32.add
393 i32.load
394 set_local $inbuf_bound
395 (; /channel in setup ;)
396 get_global $here_p
397 i32.load
398 set_local $here
399 get_global $stringbelt_tail_p
400 i32.load
401 set_local $stringbelt_tail
402 get_global $stringbelt_head_p
403 i32.load
404 set_local $stringbelt_head
405 get_global $wordbelt_tail_p
406 i32.load
407 set_local $wordbelt_tail
408 get_global $wordbelt_head_p
409 i32.load
410 set_local $wordbelt_head
411 block $bye
412 block $awaiting
413 loop $next
414 get_local $esi
415 get_local $esi
416 i32.const 4
417 i32.add
418 set_local $esi
419 i32.load
420 set_local $eax
421
422 loop $execloop
423 block $default block $op0 block $ret block $lit block $rinit
424 block $logword block $key block $dup block $plus block $noop2 block $emit
425 block $fetch block $set block $execute block $noop block $jz block $jnz
426 block $drop block $wsbool block $jmp block $wordputc block $wordstart
427 block $dictget block $parsenum block $wordfinish block $jneg1 block $swap
428 block $words block $here block $dictset block $dup2 block $rot block $drop2
429 block $comma block $subtract block $inchan block $sethere block $eqbool
430 block $echostring block $strstart block $strput block $strend block $fetchinc
431 block $setinc block $finddoes block $definedoes block $stacktrace block $webfetch
432 block $outchan block $read block $openchannel block $rpush_op
433 get_local $eax
434 br_table $op0 $ret (;2;)$lit $rinit (;4;)$logword $key (;6;)$dup $plus
435 (;8;)$jmp $emit (;10;)$fetch $set (;12;)$execute $noop (;14;)$jz $jnz
436 (;16;)$drop $wsbool (;18;)$jmp $wordputc (;20;)$wordstart $dictget
437 (;22;)$parsenum $wordfinish (;24;)$jneg1 $bye (;26;)$swap $words
438 (;28;)$here $dictset (;30;)$dup2 $rot (;32;)$drop2 $comma
439 (;34;)$subtract $inchan (;36;)$sethere $eqbool (;38;)$echostring $strstart
440 (;40;)$strput $strend (;42;)$fetchinc $setinc (;44;)$finddoes $definedoes
441 (;46;)$stacktrace $webfetch (;48;)$outchan $read (;50;)$awaiting $openchannel
442 (;52;)$rpush_op $default
443 end ;; rpush_op
444 call $pop
445 call $rpush
446 br $next
447 end ;; openchannel
448 (; Get addr of channel block ;)
449 get_global $channel_table_p
450 get_global $channel_entry_size
451 call $sys_open
452 tee_local $eax
453 call $rpush ;;save to rstack
454 get_local $eax
455 i32.mul
456 i32.add
457 (; Set Out-Channel to 1 by default ;)
458 tee_local $eax
459 i32.const 3
460 i32.add ;; out channel
461 i32.const 1
462 i32.store8
463 (; leave a copy of channel_p on stack ;)
464 get_local $eax
465 (; Get addr of ch-start and ch-default-start ;)
466 get_local $eax
467 i32.const 4
468 i32.add ;; addr of channel start
469 tee_local $eax
470 get_local $eax
471 i32.const 4
472 i32.add ;; addr of channel default start
473 (; Store the user-provided address in both ;)
474 call $pop
475 tee_local $eax
476 i32.store
477 get_local $eax
478 i32.store
479 (; go bo buf-base (channel_p + 12), put HERE in it ;)
480 tee_local $eax
481 i32.const 12
482 i32.add
483 get_local $here
484 i32.store
485 (; set buf-tail ;)
486 get_local $eax
487 i32.const 16
488 i32.add
489 get_local $here
490 i32.store
491 (; set buf-head ;)
492 get_local $eax
493 i32.const 20
494 i32.add
495 get_local $here
496 i32.store
497 (; set buf-bound ;)
498 get_local $eax
499 i32.const 24
500 i32.add
501 (; set buf-bound = here += 512 ;)
502 get_local $here
503 i32.const 512
504 i32.add
505 tee_local $here
506 i32.store
507 (; return channel number ;)
508 call $rpop
509 call $push
510 br $next
511 end ;; read
512 get_local $channel_in
513 call $pop ;; location to write
514 set_local $eax
515 call $pop
516 get_local $eax
517 call $sys_read
518 br $next
519 end ;; outchan
520 call $pop
521 set_local $channel_out
522 br $next
523 end ;; webfetch
524 call $pop ;; u
525 call $rpush
526 call $pop ;; addr
527 set_local $eax
528 call $pop ;; callback
529 get_local $eax
530 call $rpop
531 call $sys_fetch
532 i32.const -1
533 i32.eq
534 br_if $bye
535 br $next
536 end ;; stacktrace
537 get_local $esi
538 call $rpush
539 call $sys_stack
540 call $rpop
541 drop
542 br $next
543 end ;; definedoes
544 call $pop
545 call $rpush
546 call $pop
547 set_local $eax
548 call $pop
549 get_local $eax
550 call $rpop
551 call $does_set
552 br $next
553 end ;; finddoes
554 call $pop
555 set_local $eax
556 call $pop
557 get_local $eax
558 call $does_get
559 call $push
560 br $next
561 end ;; setinc
562 call $pop
563 call $rpush
564 call $pop
565 tee_local $eax
566 call $rpop
567 i32.store
568 get_local $eax
569 i32.const 4
570 i32.add
571 call $push
572 br $next
573 end ;; fetchinc
574 call $pop
575 tee_local $eax
576 i32.const 4
577 i32.add
578 call $push
579 get_local $eax
580 i32.load
581 call $push
582 br $next
583 end ;; strend
584 get_local $stringbelt_tail
585 get_local $stringbelt_head
586 get_local $stringbelt_tail
587 i32.const 4
588 i32.add
589 i32.sub
590 tee_local $eax (; n bytes ;)
591 i32.store
592 (; align to 32-bit ;)
593 get_local $stringbelt_head
594 i32.const 3
595 i32.add
596 i32.const 8188
597 i32.and
598 set_local $stringbelt_head
599 (; /align ;)
600 get_local $stringbelt_tail
601 i32.const 4
602 i32.add
603 call $push
604 get_local $eax
605 call $push
606 br $next
607 end ;; strput
608 block $sbhasspace2
609 get_local $stringbelt_head
610 get_global $wordbelt_base
611 i32.lt_u
612 br_if $sbhasspace2
613 i32.const 0
614 tee_local $stringbelt_head
615 get_local $stringbelt_tail
616 i32.load
617 i32.store
618 get_local $stringbelt_head
619 i32.const 4
620 i32.add
621 set_local $stringbelt_head
622 get_local $stringbelt_tail
623 i32.const 4
624 i32.add
625 set_local $stringbelt_tail
626 loop $copystringtostart
627 get_local $stringbelt_head
628 get_local $stringbelt_tail
629 i32.load16_u
630 i32.store16
631 get_local $stringbelt_head
632 i32.const 2
633 i32.add
634 set_local $stringbelt_head
635 get_local $stringbelt_tail
636 i32.const 2
637 i32.add
638 tee_local $stringbelt_tail
639 get_global $wordbelt_base
640 i32.le_u
641 br_if $copystringtostart
642 end
643 i32.const 0
644 set_local $stringbelt_tail
645 end
646 get_local $stringbelt_head
647 call $pop
648 i32.store16
649 get_local $stringbelt_head
650 i32.const 2
651 i32.add
652 set_local $stringbelt_head
653 br $next
654 end ;; strstart
655 block $sbhasspace
656 get_local $stringbelt_head
657 get_global $wordbelt_base
658 i32.const 8
659 i32.sub
660 i32.le_u
661 br_if $sbhasspace
662 i32.const 0
663 set_local $stringbelt_head
664 end
665 get_local $stringbelt_head
666 get_local $stringbelt_head
667 tee_local $stringbelt_tail
668 i32.const 0
669 i32.store
670 i32.const 4
671 i32.add
672 set_local $stringbelt_head
673 br $next
674 end ;; echostring
675 get_local $channel_out
676 call $pop
677 set_local $eax
678 call $pop
679 get_local $eax
680 call $sys_send
681 br $next
682 end ;; eqbool
683 block $equiv
684 call $pop
685 call $pop
686 tee_local $eax
687 i32.eq
688 get_local $eax
689 call $push
690 br_if $equiv
691 i32.const 0
692 call $push
693 br $next
694 end
695 i32.const 1
696 call $push
697 br $next
698 end ;; sethere
699 call $pop
700 set_local $here
701 br $next
702 end ;; inchan
703 call $pop
704 set_local $channel_in
705 br $next
706 end ;; subtract
707 call $pop
708 set_local $eax
709 call $pop
710 get_local $eax
711 i32.sub
712 call $push
713 br $next
714 end ;; comma
715 get_local $here
716 call $pop
717 i32.store
718 get_local $here
719 i32.const 4
720 i32.add
721 set_local $here
722 br $next
723 end ;; drop2
724 call $pop
725 call $pop
726 drop
727 drop
728 br $next
729 end ;; rot
730 call $pop
731 call $pop
732 set_local $eax
733 call $pop
734 call $rpush
735 call $push
736 call $rpop
737 call $push
738 get_local $eax
739 call $push
740 br $next
741 end ;; dup2
742 get_local $esi
743 call $rpush
744 call $pop
745 set_local $eax
746 call $pop
747 tee_local $esi
748 call $push
749 get_local $eax
750 call $push
751 get_local $esi
752 call $push
753 get_local $eax
754 call $push
755 call $rpop
756 set_local $esi
757 br $next
758 end ;; dictset
759 call $pop
760 call $rpush
761 call $pop
762 set_local $eax
763 call $pop
764 get_local $eax
765 call $rpop
766 call $vocab_set
767 br $next
768 end ;; here
769 get_local $here
770 call $push
771 br $next
772 end ;; words
773 call $sys_words
774 br $next
775 end ;; swap
776 call $pop
777 call $pop
778 set_local $eax
779 call $push
780 get_local $eax
781 call $push
782 br $next
783 end ;; jneg1
784 block $jneg1if
785 call $pop
786 i32.const -1
787 i32.eq
788 br_if $jneg1if
789 get_local $esi
790 i32.const 4
791 i32.add
792 set_local $esi
793 br $next
794 end
795 get_local $esi
796 i32.load
797 set_local $esi
798 br $next
799 end ;; wordfinish
800 get_local $wordbelt_head
801 get_local $wordbelt_tail
802 i32.sub
803 set_local $eax (; n bytes ;)
804 (; align to 32-bit ;)
805 get_local $wordbelt_head
806 i32.const 3
807 i32.add
808 i32.const 12284
809 i32.and
810 set_local $wordbelt_head
811 (; /align ;)
812 get_local $wordbelt_tail
813 call $push
814 get_local $eax
815 call $push
816 br $next
817 end ;; parsenum
818 call $pop
819 call $rpush
820 call $pop
821 tee_local $eax
822 call $rpop
823 call $sys_parsenum
824 get_local $eax
825 i32.load
826 call $push
827 call $push
828 br $next
829 end ;; dictget
830 call $pop
831 set_local $eax
832 call $pop
833 get_local $eax
834 call $vocab_get
835 call $push
836 br $next
837 end ;; wordstart
838 block $wbhasspace
839 get_local $wordbelt_head
840 get_global $wordbelt_bound
841 i32.const 4
842 i32.sub
843 i32.le_u
844 br_if $wbhasspace
845 get_global $wordbelt_base
846 set_local $wordbelt_head
847 end
848 get_local $wordbelt_head
849 set_local $wordbelt_tail
850 br $next
851 end ;; wordputc
852 block $wbhasspace2
853 get_local $wordbelt_head
854 get_global $wordbelt_bound
855 i32.lt_u
856 br_if $wbhasspace2
857 get_global $wordbelt_base
858 set_local $wordbelt_head
859 loop $copywordtostart
860 get_local $wordbelt_head
861 get_local $wordbelt_tail
862 i32.load16_u
863 i32.store16
864 get_local $wordbelt_head
865 i32.const 2
866 i32.add
867 set_local $wordbelt_head
868 get_local $wordbelt_tail
869 i32.const 2
870 i32.add
871 tee_local $wordbelt_tail
872 get_global $wordbelt_bound
873 i32.lt_u
874 br_if $copywordtostart
875 end
876 get_global $wordbelt_base
877 set_local $wordbelt_tail
878 end
879 get_local $wordbelt_head
880 call $pop
881 i32.store16
882 get_local $wordbelt_head
883 i32.const 2
884 i32.add
885 set_local $wordbelt_head
886 br $next
887 end ;; jmp
888 get_local $esi
889 i32.load
890 set_local $esi
891 br $next
892 end ;; wsbool
893 call $pop
894 tee_local $eax
895 call $is_whitespace
896 get_local $eax
897 call $push
898 call $push
899 br $next
900 end ;; drop
901 call $pop
902 drop
903 br $next
904 end ;; jnz
905 block $jnzif
906 call $pop
907 i32.eqz
908 br_if $jnzif
909 get_local $esi
910 i32.load
911 set_local $esi
912 br $next
913 end
914 get_local $esi
915 i32.const 4
916 i32.add
917 set_local $esi
918 br $next
919 end ;; jz
920 block $jzif
921 call $pop
922 i32.eqz
923 br_if $jzif
924 get_local $esi
925 i32.const 4
926 i32.add
927 set_local $esi
928 br $next
929 end
930 get_local $esi
931 i32.load
932 set_local $esi
933 br $next
934 end ;; noop
935 br $next
936 end ;; execute
937 call $pop
938 tee_local $eax
939 i32.const 256
940 i32.lt_u
941 br_if $execloop
942 get_local $esi
943 call $rpush
944 get_local $eax
945 set_local $esi
946 br $next
947 end ;; set
948 call $pop
949 set_local $eax
950 call $pop
951 get_local $eax
952 i32.store
953 br $next
954 end ;; fetch
955 call $pop
956 i32.load
957 call $push
958 br $next
959 end ;; emit (.)
960 call $pop
961 call $sys_echo
962 br $next
963 end ;; noop2
964 br $next
965 end ;; plus
966 call $pop
967 call $pop
968 i32.add
969 call $push
970 br $next
971 end ;; dup
972 call $pop
973 tee_local $eax
974 get_local $eax
975 call $push
976 call $push
977 br $next
978 end ;; key
979 loop $key_loop
980 block $key_read
981 get_local $inbuf_head
982 get_local $inbuf_tail
983 i32.ge_u
984 br_if $key_read
985 block $key_echo
986 get_local $channel_out
987 i32.const -1
988 i32.add
989 br_if $key_echo
990 (; if current channel's default start is QUIT ;)
991 get_global $channel_table_p
992 get_global $channel_entry_size
993 get_local $channel_in
994 i32.mul
995 i32.add
996 i32.const 4
997 i32.add
998 i32.load
999 get_global $quit_p
1000 i32.ne
1001 br_if $key_echo
1002 i32.const 1
1003 get_local $inbuf_head
1004 i32.const 2
1005 call $sys_write
1006 end
1007 get_local $inbuf_head
1008 i32.load16_u
1009 call $push
1010 get_local $inbuf_head
1011 i32.const 2
1012 i32.add
1013 set_local $inbuf_head
1014 br $next
1015 end ;; key_read
1016 get_local $channel_in
1017 get_local $inbuf_base
1018 get_local $inbuf_bound
1019 get_local $inbuf_base
1020 i32.sub
1021 call $sys_read
1022 tee_local $eax
1023 get_local $inbuf_base
1024 i32.add
1025 set_local $inbuf_tail
1026 get_local $inbuf_base
1027 set_local $inbuf_head
1028 block $nullread
1029 get_local $eax
1030 i32.eqz
1031 br_if $nullread
1032 br $key_loop
1033 end ;; nullread
1034 block $pendingword
1035 get_local $wordbelt_head
1036 get_local $wordbelt_tail
1037 i32.sub
1038 i32.eqz
1039 br_if $pendingword
1040 i32.const 32
1041 call $push
1042 br $next
1043 end ;; pendingword
1044 br $bye
1045 end ;; key_loop
1046 end ;; logword
1047 call $pop
1048 tee_local $eax
1049 call $pop
1050 get_local $eax
1051 call $sys_log
1052 br $next
1053 end ;; rinit (unused)
1054 call $rinit
1055 get_global $holy_bye
1056 call $rpush
1057 br $next
1058 end ;; lit
1059 get_local $esi
1060 get_local $esi
1061 i32.const 4
1062 i32.add
1063 set_local $esi
1064 i32.load
1065 call $push
1066 br $next
1067 end ;; ret
1068 call $rpop
1069 set_local $esi
1070 br $next
1071 end ;; op0 (yield?)
1072 get_local $esi
1073 call $rpush
1074 br $bye
1075 end ;; default
1076 get_local $esi
1077 call $rpush
1078 get_local $eax
1079 set_local $esi
1080 br $next
1081 end ;; execloop
1082 end ;; next loop
1083 end ;; awaiting
1084
1085 (; set provided channel's waiter to this channel ;)
1086 get_global $channel_table_p
1087 get_global $channel_entry_size
1088 call $pop
1089 i32.mul
1090 i32.add
1091 tee_local $eax
1092 i32.const 2
1093 i32.add
1094 get_local $channel_in
1095 i32.store8
1096 (; set awaiter flag ;)
1097 get_local $eax
1098 get_local $eax
1099 i32.load8_u
1100 i32.const 2
1101 i32.or
1102 i32.store8
1103
1104 (; channel status save ;)
1105 get_global $channel_table_p
1106 get_global $channel_entry_size
1107 get_local $channel_in
1108 i32.mul
1109 i32.add
1110 tee_local $eax
1111 (; set buffer base ;)
1112 i32.const 12
1113 i32.add
1114 get_local $inbuf_base
1115 i32.store
1116 (; set buffer tail ;)
1117 get_local $eax
1118 i32.const 16
1119 i32.add
1120 get_local $inbuf_tail
1121 i32.store
1122 (; set buffer head ;)
1123 get_local $eax
1124 i32.const 20
1125 i32.add
1126 get_local $inbuf_head
1127 i32.store
1128 (; set buffer bound ;)
1129 get_local $eax
1130 i32.const 24
1131 i32.add
1132 get_local $inbuf_bound
1133 i32.store
1134
1135 (; set buffer buffer start ;)
1136 get_local $eax
1137 i32.const 4
1138 i32.add
1139 get_local $here
1140 i32.store
1141 (; backup return stack here, returning to esi ;)
1142 get_local $here
1143 get_local $esi
1144 call $lit_rstack
1145 set_local $here
1146
1147 i32.const -1
1148 set_local $inbuf_base ;; temporary bool "await-exit"
1149
1150 (; /awaiting ;)
1151 end ;; bye
1152
1153 get_global $channel_table_p
1154 get_global $channel_entry_size
1155 get_local $channel_in
1156 i32.mul
1157 i32.add
1158 tee_local $eax
1159 i32.const 3
1160 i32.add
1161 get_local $channel_out
1162 i32.store8
1163
1164 get_local $eax
1165 get_local $eax
1166 i32.load8_u
1167 i32.const -1
1168 i32.and
1169 i32.store8 (; toggle off running ;)
1170
1171 get_global $here_p
1172 get_local $here
1173 i32.store
1174 get_global $stringbelt_tail_p
1175 get_local $stringbelt_tail
1176 i32.store
1177 get_global $stringbelt_head_p
1178 get_local $stringbelt_head
1179 i32.store
1180 get_global $wordbelt_tail_p
1181 get_local $wordbelt_tail
1182 i32.store
1183 get_global $wordbelt_head_p
1184 get_local $wordbelt_head
1185 i32.store
1186
1187 block $check_awaiter
1188 get_local $inbuf_base
1189 i32.const -1
1190 i32.eq ;; don't check if "await-exit" is true
1191 br_if $check_awaiter
1192 get_local $eax
1193 i32.load8_u
1194 i32.const 2
1195 i32.and
1196 i32.eqz ;; (FLAGS & 2) => awaiter, run it
1197 br_if $check_awaiter
1198 get_local $eax
1199 i32.const 2
1200 i32.add
1201 i32.load8_u
1202 get_local $channel_in
1203 set_local $eax
1204 set_local $channel_in
1205 br $recurse_loop
1206 end
1207 end ;; recurse_loop
1208 get_local $channel_in
1209 call $close_channel
1210 i32.const 0
1211 return
1212 )
1213 )