interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / seed / rung4 / om4.s

4217 lines · 83631 bytes Raw Blame History
1; om4.s -- rung 4 of the Omega seed chain: om3 (seed/rung3/om3.s) grown into the systems subset a compiler
2; needs, written in asm2's language and built by asm2:
3; cat seed/rung2/table.s2 seed/rung3/table3.s2 seed/rung4/om4.s | seed/rung2/asm2.bin > om4.bin
4; Use: om4.bin < program.omegese > out. Two kinds of output (docs/one_layer/OMEGESE.md section 9 has the words):
5; - function mode (no `fn main`): the functions' words only, entry = word 0, to be called under the OSC-1
6; native rules (docs/osc/OSC-1-DESIGN.md section 7): parameters in x0..x5, result in x0 in canonical
7; form, OscRt * in x7, a trap calls OscRt.trap (offset 16) with x0 = rt, x1 = code. With more than one
8; function, word 0 is a B to the last one (the entry is still written last). A call may name any
9; function in the unit, earlier or later.
10; - program mode (a line `fn main`): a whole AArch64 Linux program (ELF file header laid out by hand, no
11; linker). Its start code sets the stack to a fixed 8 MiB pool, calls main, and exits with main's value;
12; a trap exits with status 100 + code.
13;
14; What om4 adds to om3: many functions, calls to any function in the unit (a names pass collects every
15; signature first; a later function is a call, never a new scalar), mutual recursion in program mode,
16; a call cycle in function mode refused as error 9 (the same refusal a function calling itself already
17; had), assigning to a function name refused as error 10; void functions and ret without a value;
18; signed div, rem and shl; for with a value as count;
19; true and false; local arrays (new, on the stack, only in a function's outermost block), array parameters
20; (view / edit), global arrays (global, fixed pools in the program's memory image, no heap), text literals
21; (text, read-only bytes with their length), get, put, len; read and write (Linux system calls on a
22; checked part of an array) and exit; note lines. Every array carries its length; every index is checked
23; (trap 3) unless it is a number, which is checked when om4 reads it.
24; Semantics follow the C path's interpreter (src/compiler/osc_interp.c): unsigned ops wrap to the width,
25; signed add/sub/mul/neg/div/shl trap 1 out of range, div/rem trap 2 on zero, a loop bound trap 4, a
26; conversion out of range trap 5, a shift amount outside 0..width-1 trap 7; om4 adds trap 3 (index or
27; system call range outside the array) and trap 6 (the stack pool is used up, program mode).
28;
29; Code generation (as om3): every name lives in its own register for its whole scope; temporaries are
30; registers freed at the end of each statement. Registers handed out: x0..x6 not taken by parameters,
31; x8..x15, x17 (never x7 = rt, x16 = scratch, x18, x19..x30; x28 = stack pool floor in program mode).
32; A call saves every register in use and x30 on the stack, loads the arguments into x0..x5 from that save
33; area, branches with BL, and loads the registers back; so a function needs no entry or exit code beyond
34; giving back the stack of its local arrays.
35;
36; Every machine word om4 writes is built from a fixed part read from the encoding diagram of the Arm ARM
37; DDI 0487 M.d section named beside it, ORed with register and number fields.
38;
39; om4's own registers: x18 input end, x19 input cursor, x20 output cursor, x21 output buffer start, x22 line
40; number, x23 names in scope, x24 next free pool slot, x25 block stack top, x26 trap list end, x27 return
41; type (0 outside a function, 10 void), x28 pool slots held by names, x29 pool start.
42; Memory: input 0x800000 (under 8 MiB), output 0x1000000 (8 MiB: 176-byte file header, 1 entry word, then
43; text bytes and functions), names 0x1800000 (32 bytes: packed name 8, register 4, type 4, length 4, kind
44; 4, address 8), blocks 0x1810000 (128 bytes), trap list 0x1820000 (8 bytes: site, code), message
45; 0x1830000, 0x1840000 unused (the 512-entry function table until it moved), state 0x1850000 (8-byte
46; slots: 1 globals and texts in the name table, 2 functions, 4 stack bytes of local arrays, 5 the path
47; has ended, 6 program mode, 7 global pool bytes, 9 last function, 10 value nesting depth, 11 a number
48; written as 2^63 or more, 12 recorded calls, 13 namescan phase: 0 not started, 1 collecting signatures,
49; 2 emitting). Call records, 24 bytes each (site, callee entry, caller entry), sit at 0x1860000, 65536 of
50; them. Function-mode cycle marks, one byte per function, sit at 0x19e0000. Functions, 4096 entries of
51; 128 bytes (name 8, address 8, return type 4, parameter count 4, then per parameter 16 bytes: type 4,
52; length 4, kind 4), sit at 0x19f0000 up to 0x1a70000. All three are past the file and zeroed with the
53; rest of the mapping.
54; Name kinds: 0 scalar, 1 local array, 2 view parameter, 3 edit parameter, 4 global array, 5 text.
55; Errors: "om4: line N: error E" on stderr, exit E: 1 read, 2 write, 3 unknown name, 4 line ends too
56; early, 5 extra words, 6 bad number, 7 type mismatch, 8 number out of range for its type, 9 not in the
57; subset, 10 name defined twice, 11 number with no type, 12 out of registers, 13 word over 8 characters,
58; 14 table or buffer full, 15 block structure, 16 syntax, 17 a line after ret or exit in the same block,
59; 18 a function that needs a value can reach its end, 19 one array passed twice in a call (aliasing),
60; 20 writing through a read-only array, 21 a number index outside the array, 22 bad text literal,
61; 23 array or pool too large.
62;
63; ---- ELF-64 file header (words as in seed/rung0/seed0.hex) ----
64 .w 0x464c457f
65 .w 0x00010102
66 .w 0x00000000
67 .w 0x00000000
68 .w 0x00b70002
69 .w 0x00000001
70 .w 0x00400078
71 .w 0x00000000
72 .w 0x00000040
73 .w 0x00000000
74 .w 0x00000000
75 .w 0x00000000
76 .w 0x00000000
77 .w 0x00380040
78 .w 0x00000001
79 .w 0x00000000
80; ---- program header: one PT_LOAD, read/write/execute, file at 0x400000 ----
81 .w 0x00000001
82 .w 0x00000007
83 .w 0x00000000
84 .w 0x00000000
85 .w 0x00400000
86 .w 0x00000000
87 .w 0x00400000
88 .w 0x00000000
89 .w 0x00003790 ; p_filesz = the size of om4.bin, 14224 bytes (asm2 has no size arithmetic; gate4.sh checks it)
90 .w 0x00000000
91 .w 0x01670000 ; p_memsz: through the function table at 0x19f0000, which ends at 0x1a70000
92 .w 0x00000000
93 .w 0x00001000
94 .w 0x00000000
95; ---- 0x400078: entry ----
96 b START
97 .w 0
98; ---- keyword table, 16 bytes per entry: packed word (8), code (4), 0 (4); made with
99; evidence/SEED-0/rung4-worksheet.sh. Codes: 1..9 types, 33..58 values, 64.. statements, 77 not in subset.
100:KWTAB
101 .w 0x6c6f6f62
102 .w 0x00000000
103 .w 1
104 .w 0
105 .w 0x61686375
106 .w 0x00000072
107 .w 2
108 .w 0
109 .w 0x00003875
110 .w 0x00000000
111 .w 2
112 .w 0
113 .w 0x6f687375
114 .w 0x00007472
115 .w 3
116 .w 0
117 .w 0x00363175
118 .w 0x00000000
119 .w 3
120 .w 0
121 .w 0x746e6975
122 .w 0x00000000
123 .w 4
124 .w 0
125 .w 0x00323375
126 .w 0x00000000
127 .w 4
128 .w 0
129 .w 0x6e6f6c75
130 .w 0x00000067
131 .w 5
132 .w 0
133 .w 0x00343675
134 .w 0x00000000
135 .w 5
136 .w 0
137 .w 0x65747962
138 .w 0x00000000
139 .w 6
140 .w 0
141 .w 0x00003869
142 .w 0x00000000
143 .w 6
144 .w 0
145 .w 0x726f6873
146 .w 0x00000074
147 .w 7
148 .w 0
149 .w 0x00363169
150 .w 0x00000000
151 .w 7
152 .w 0
153 .w 0x00746e69
154 .w 0x00000000
155 .w 8
156 .w 0
157 .w 0x00323369
158 .w 0x00000000
159 .w 8
160 .w 0
161 .w 0x676e6f6c
162 .w 0x00000000
163 .w 9
164 .w 0
165 .w 0x00343669
166 .w 0x00000000
167 .w 9
168 .w 0
169 .w 0x00646461
170 .w 0x00000000
171 .w 33
172 .w 0
173 .w 0x00627573
174 .w 0x00000000
175 .w 34
176 .w 0
177 .w 0x006c756d
178 .w 0x00000000
179 .w 35
180 .w 0
181 .w 0x00766964
182 .w 0x00000000
183 .w 36
184 .w 0
185 .w 0x006d6572
186 .w 0x00000000
187 .w 37
188 .w 0
189 .w 0x00646e61
190 .w 0x00000000
191 .w 38
192 .w 0
193 .w 0x0000726f
194 .w 0x00000000
195 .w 39
196 .w 0
197 .w 0x00726f78
198 .w 0x00000000
199 .w 40
200 .w 0
201 .w 0x006c6873
202 .w 0x00000000
203 .w 41
204 .w 0
205 .w 0x00726873
206 .w 0x00000000
207 .w 42
208 .w 0
209 .w 0x00007165
210 .w 0x00000000
211 .w 43
212 .w 0
213 .w 0x0000656e
214 .w 0x00000000
215 .w 44
216 .w 0
217 .w 0x0000746c
218 .w 0x00000000
219 .w 45
220 .w 0
221 .w 0x0000656c
222 .w 0x00000000
223 .w 46
224 .w 0
225 .w 0x00007467
226 .w 0x00000000
227 .w 47
228 .w 0
229 .w 0x00006567
230 .w 0x00000000
231 .w 48
232 .w 0
233 .w 0x0067656e
234 .w 0x00000000
235 .w 49
236 .w 0
237 .w 0x00746f6e
238 .w 0x00000000
239 .w 50
240 .w 0
241 .w 0x00746567
242 .w 0x00000000
243 .w 51
244 .w 0
245 .w 0x006e656c
246 .w 0x00000000
247 .w 52
248 .w 0
249 .w 0x64616572
250 .w 0x00000000
251 .w 53
252 .w 0
253 .w 0x74697277
254 .w 0x00000065
255 .w 54
256 .w 0
257 .w 0x65757274
258 .w 0x00000000
259 .w 55
260 .w 0
261 .w 0x736c6166
262 .w 0x00000065
263 .w 56
264 .w 0
265 .w 0x77656976
266 .w 0x00000000
267 .w 57
268 .w 0
269 .w 0x74696465
270 .w 0x00000000
271 .w 58
272 .w 0
273 .w 0x00006e66
274 .w 0x00000000
275 .w 64
276 .w 0
277 .w 0x00006f74
278 .w 0x00000000
279 .w 65
280 .w 0
281 .w 0x00646e65
282 .w 0x00000000
283 .w 66
284 .w 0
285 .w 0x00006669
286 .w 0x00000000
287 .w 67
288 .w 0
289 .w 0x65736c65
290 .w 0x00000000
291 .w 68
292 .w 0
293 .w 0x6c696877
294 .w 0x00000065
295 .w 69
296 .w 0
297 .w 0x6e756f62
298 .w 0x00000064
299 .w 70
300 .w 0
301 .w 0x00726f66
302 .w 0x00000000
303 .w 71
304 .w 0
305 .w 0x00746572
306 .w 0x00000000
307 .w 72
308 .w 0
309 .w 0x0000003d
310 .w 0x00000000
311 .w 73
312 .w 0
313 .w 0x00000028
314 .w 0x00000000
315 .w 74
316 .w 0
317 .w 0x00000029
318 .w 0x00000000
319 .w 75
320 .w 0
321 .w 0x64696f76
322 .w 0x00000000
323 .w 76
324 .w 0
325 .w 0x626f6c67
326 .w 0x00006c61
327 .w 78
328 .w 0
329 .w 0x74786574
330 .w 0x00000000
331 .w 79
332 .w 0
333 .w 0x00747570
334 .w 0x00000000
335 .w 80
336 .w 0
337 .w 0x65657266
338 .w 0x00000000
339 .w 81
340 .w 0
341 .w 0x74697865
342 .w 0x00000000
343 .w 82
344 .w 0
345 .w 0x65746f6e
346 .w 0x00000000
347 .w 83
348 .w 0
349 .w 0x0077656e
350 .w 0x00000000
351 .w 84
352 .w 0
353 .w 0x61657262
354 .w 0x0000006b
355 .w 77
356 .w 0
357 .w 0x70617274
358 .w 0x00000000
359 .w 77
360 .w 0
361 .w 0x006e776f
362 .w 0x00000000
363 .w 77
364 .w 0
365 .w 0
366 .w 0
367 .w 0
368 .w 0
369; ---- messages ("om4: line ", ": error "), each with its length ----
370:MSG1
371 .w 0x3a346d6f
372 .w 0x6e696c20
373 .w 0x00002065
374:MSG2
375 .w 0x7265203a
376 .w 0x20726f72
377 .w 0x00000000
378; ---- register pool: x0..x6, x8..x15, x17; the pool starts after the parameter registers ----
379:POOLC
380 .b 0
381 .b 1
382 .b 2
383 .b 3
384 .b 4
385 .b 5
386 .b 6
387 .b 8
388 .b 9
389 .b 10
390 .b 11
391 .b 12
392 .b 13
393 .b 14
394 .b 15
395 .b 17
396:POOLEND
397; ---- the pool index of each register x0..x17 (255: not in the pool) ----
398:REGIDX
399 .b 0
400 .b 1
401 .b 2
402 .b 3
403 .b 4
404 .b 5
405 .b 6
406 .b 255
407 .b 7
408 .b 8
409 .b 9
410 .b 10
411 .b 11
412 .b 12
413 .b 13
414 .b 14
415 .b 255
416 .b 15
417; ---- width in bits by type code 0..9 (bool 1) ----
418:TWID
419 .b 0
420 .b 1
421 .b 8
422 .b 16
423 .b 32
424 .b 64
425 .b 8
426 .b 16
427 .b 32
428 .b 64
429; ---- log2 of the element size by type code 0..9 ----
430:TLOG
431 .b 0
432 .b 0
433 .b 0
434 .b 1
435 .b 2
436 .b 3
437 .b 0
438 .b 1
439 .b 2
440 .b 3
441; ---- condition for eq ne lt le gt ge, unsigned then signed (Arm ARM Table C1-1, C1.2.4):
442; EQ 0, NE 1, LO 3 / LT 11, LS 9 / LE 13, HI 8 / GT 12, HS 2 / GE 10. The opposite condition is cond ^ 1.
443:CONDS
444 .b 0
445 .b 0
446 .b 1
447 .b 1
448 .b 3
449 .b 11
450 .b 9
451 .b 13
452 .b 8
453 .b 12
454 .b 2
455 .b 10
456 .b 0 ; pad to a word boundary (16 + 18 + 10 + 10 + 12 bytes of tables above)
457 .b 0
458; ---- load by element type 0..9, register offset scaled by the element size (LSL #log2 size; S = 1
459; except for bytes): uchar LDRB C6.2.221, ushort LDRH C6.2.223, uint LDR (32-bit) C6.2.218, ulong and
460; long LDR (64-bit) C6.2.218, byte LDRSB (64-bit) C6.2.225, short LDRSH (64-bit) C6.2.227, int LDRSW
461; C6.2.230. Fixed part: option 011 (LSL), Rm Rn Rt zero.
462:LDOP
463 .w 0
464 .w 0
465 .w 0x38606800
466 .w 0x78607800
467 .w 0xb8607800
468 .w 0xf8607800
469 .w 0x38a06800
470 .w 0x78a07800
471 .w 0xb8a07800
472 .w 0xf8607800
473; ---- store by element type: STRB C6.2.418, STRH C6.2.420, STR (32-bit, 64-bit) C6.2.416 ----
474:STOP
475 .w 0
476 .w 0
477 .w 0x38206800
478 .w 0x78207800
479 .w 0xb8207800
480 .w 0xf8207800
481 .w 0x38206800
482 .w 0x78207800
483 .w 0xb8207800
484 .w 0xf8207800
485; ---- the file header om4 writes in program mode: ELF-64 header and two PT_LOAD program headers
486; (the file at 0x400000 read/execute; the stack and global pools at 0x10000000 read/write, no file
487; bytes). Words 24, 26 (file size) and 40 (pool size) are filled in at the end. ----
488:ELFHDR
489 .w 0x464c457f
490 .w 0x00010102
491 .w 0x00000000
492 .w 0x00000000
493 .w 0x00b70002
494 .w 0x00000001
495 .w 0x004000b0 ; e_entry: the word after the header
496 .w 0x00000000
497 .w 0x00000040
498 .w 0x00000000
499 .w 0x00000000
500 .w 0x00000000
501 .w 0x00000000
502 .w 0x00380040
503 .w 0x00000002 ; e_phnum 2
504 .w 0x00000000
505 .w 0x00000001 ; PT_LOAD
506 .w 0x00000005 ; PF_R | PF_X
507 .w 0x00000000
508 .w 0x00000000
509 .w 0x00400000
510 .w 0x00000000
511 .w 0x00400000
512 .w 0x00000000
513 .w 0x00000000 ; p_filesz (filled in)
514 .w 0x00000000
515 .w 0x00000000 ; p_memsz (filled in)
516 .w 0x00000000
517 .w 0x00001000
518 .w 0x00000000
519 .w 0x00000001 ; PT_LOAD
520 .w 0x00000006 ; PF_R | PF_W
521 .w 0x00000000
522 .w 0x00000000
523 .w 0x10000000 ; the stack pool from 0x10000000, the global pools from 0x10800000
524 .w 0x00000000
525 .w 0x10000000
526 .w 0x00000000
527 .w 0x00000000 ; p_filesz 0
528 .w 0x00000000
529 .w 0x00000000 ; p_memsz (filled in: 8 MiB stack + global pools)
530 .w 0x00000000
531 .w 0x00001000
532 .w 0x00000000
533; ======================================================================================================
534; start: read all of stdin to 0x800000 (at most 8 MiB - 1 bytes); x18 = its end (no end byte is written)
535:START
536 movz.x x19 0x80 1
537 movz.x x12 0 0
538:RD
539 movz.x x0 0 0
540 add.x x1 x19 x12
541 movz.x x2 0x80 1
542 sub.x x2 x2 x12
543 cbz.x x2 E_FULL
544 movz.x x8 63 0
545 svc 0
546 cmp.x.i x0 0
547 b.eq RDDONE
548 b.lt E_READ
549 add.x x12 x12 x0
550 b RD
551:RDDONE
552 add.x x18 x19 x12
553 movz.x x9 0x185 1
554 movz.x x10 0 0
555:ST0
556 str.x xzr x9 0
557 add.x.i x9 x9 8
558 add.x.i x10 x10 1
559 cmp.x.i x10 16
560 b.lo ST0
561; ---- program mode when some line is `fn main` (blanks, fn, blanks, main, then a blank or the end) ----
562 mov.x x10 x19
563:PS_LINE
564 cmp.x x10 x18
565 b.hs PS_DONE
566 ldrb w0 x10 0
567 cmp.w.i w0 32
568 b.eq PS_ADV
569 cmp.w.i w0 9
570 b.eq PS_ADV
571 cmp.w.i w0 102
572 b.ne PS_SKIP
573 add.x.i x12 x10 1
574 cmp.x x12 x18
575 b.hs PS_DONE
576 ldrb w0 x12 0
577 cmp.w.i w0 110
578 b.ne PS_SKIP
579 add.x.i x12 x12 1
580 mov.x x13 x12
581:PS_B
582 cmp.x x12 x18
583 b.hs PS_DONE
584 ldrb w0 x12 0
585 cmp.w.i w0 32
586 b.eq PS_B1
587 cmp.w.i w0 9
588 b.ne PS_M
589:PS_B1
590 add.x.i x12 x12 1
591 b PS_B
592:PS_M
593 cmp.x x12 x13
594 b.eq PS_SKIP
595 add.x.i x13 x12 4
596 cmp.x x13 x18
597 b.hi PS_SKIP
598 ldrb w0 x12 0
599 cmp.w.i w0 109
600 b.ne PS_SKIP
601 ldrb w0 x12 1
602 cmp.w.i w0 97
603 b.ne PS_SKIP
604 ldrb w0 x12 2
605 cmp.w.i w0 105
606 b.ne PS_SKIP
607 ldrb w0 x12 3
608 cmp.w.i w0 110
609 b.ne PS_SKIP
610 cmp.x x13 x18
611 b.hs PS_YES
612 ldrb w0 x13 0
613 cmp.w.i w0 32
614 b.hi PS_SKIP
615:PS_YES
616 movz.x x9 0x185 1
617 movz.x x0 1 0
618 str.x x0 x9 6
619 b PS_DONE
620:PS_ADV
621 add.x.i x10 x10 1
622 b PS_LINE
623:PS_SKIP
624 cmp.x x10 x18
625 b.hs PS_DONE
626 ldrb w0 x10 0
627 add.x.i x10 x10 1
628 cmp.w.i w0 10
629 b.ne PS_SKIP
630 b PS_LINE
631:PS_DONE
632 movz.x x21 0x100 1
633 add.x.i x20 x21 180
634 movz.x x22 1 0
635 movz.x x23 0 0
636 movz.x x24 0 0
637 movz.x x25 0x181 1
638 movz.x x26 0x182 1
639 movz.x x27 0 0
640 movz.x x28 0 0
641 adr x29 POOLC
642; ---- one statement per line ----
643:STMT
644 movz.x x9 0x185 1
645 ldr.x x10 x9 13
646 cbz.x x10 NS_INIT
647 cmp.x.i x10 1
648 b.eq NS_LINE
649 b BODY_LINE
650:NS_INIT
651 movz.x x10 1 0
652 str.x x10 x9 13
653:NS_LINE
654 bl TOK
655 cbz.x x0 NS_LINE
656 cmp.x.i x0 3
657 b.eq NS_DONE
658 cmp.x.i x0 1
659 b.ne S_NOTE
660 cmp.x.i x2 2
661 b.ne S_NOTE
662 movz.x x9 0x6e66 0
663 cmp.x x1 x9
664 b.eq S_FN
665 b S_NOTE
666:NS_DONE
667 movz.x x9 0x185 1
668 movz.x x10 2 0
669 str.x x10 x9 13
670 str.x xzr x9 3
671 movz.x x19 0x80 1
672 b PS_DONE
673:BODY_LINE
674 mov.x x24 x28
675 bl TOK
676 cbz.x x0 STMT
677 cmp.x.i x0 3
678 b.eq S_EOF
679 cmp.x.i x0 1
680 b.ne E_SYNTAX
681 cmp.x.i x2 8
682 b.hi S_LONG
683 mov.x x12 x1
684 bl KW
685 cmp.x.i x0 83
686 b.eq S_NOTE
687 cmp.x.i x0 64
688 b.eq S_FN
689 cbz.x x27 S_TOP
690 cmp.x.i x0 66
691 b.eq S_END
692 cmp.x.i x0 68
693 b.eq S_ELSE
694 movz.x x9 0x185 1
695 ldr.x x10 x9 5
696 cbnz.x x10 E_DEAD
697 cbz.x x0 S_NAME
698 cmp.x.i x0 67
699 b.eq S_IF
700 cmp.x.i x0 69
701 b.eq S_WHILE
702 cmp.x.i x0 71
703 b.eq S_FOR
704 cmp.x.i x0 72
705 b.eq S_RET
706 cmp.x.i x0 80
707 b.eq S_PUT
708 cmp.x.i x0 81
709 b.eq E_SUBSET ; free: no heap and nothing to give back (decision 24), so not a word
710 cmp.x.i x0 82
711 b.eq S_EXIT
712 cmp.x.i x0 77
713 b.eq E_SUBSET
714 cmp.x.i x0 78
715 b.eq E_SUBSET
716 cmp.x.i x0 79
717 b.eq E_SUBSET
718 b E_SYNTAX
719:S_TOP
720 cmp.x.i x0 78
721 b.eq S_GLOBAL
722 cmp.x.i x0 79
723 b.eq S_TEXT
724 b E_SYNTAX
725; ---- note ...: the rest of the line is not read ----
726:S_NOTE
727 cmp.x x19 x18
728 b.hs STMT
729 ldrb w0 x19 0
730 cmp.w.i w0 10
731 b.eq STMT
732 add.x.i x19 x19 1
733 b S_NOTE
734; ---- a name at the start of a line: a call of a void function, or name [=] value ----
735:S_NAME
736 mov.x x1 x12
737 bl FINDNAME
738 cbnz.x x0 S_ASSIGN
739 mov.x x1 x12
740 bl FINDFN
741 cbz.x x0 S_ASSIGN
742 ldr.w w9 x0 4
743 cmp.x.i x9 10
744 b.ne E_TYPE
745 movz.x x1 255 0
746 bl CALL
747 bl EOL
748 b STMT
749; ---- fn name (param type | param view T N | param edit T N)* to type | void ----
750:S_FN
751 cbnz.x x27 E_BLOCK
752 bl TOK
753 cmp.x.i x0 1
754 b.ne E_SYNTAX
755 cmp.x.i x2 63
756 b.hi E_LONG
757 mov.x x12 x1
758 bl KW
759 cbnz.x x0 E_SYNTAX
760 mov.x x1 x12
761 sub.x x3 x19 x2
762 movz.x x9 0x185 1
763 ldr.x x10 x9 13
764 cmp.x.i x10 2
765 b.eq FN_KNOWN
766 bl NEWNAME
767 movz.x x9 0x185 1
768 ldr.x x10 x9 2
769
770 cmp.x.i x10 4095
771 b.hi E_FULL
772 add.x.i x11 x10 1
773 str.x x11 x9 2
774 movz.x x11 0x19f 1
775 ubfm.x x10 x10 57 56
776 add.x x11 x11 x10
777 str.x x11 x9 3
778 str.x x11 x9 9
779 str.x x12 x11 0
780 str.x x20 x11 1
781 str.x xzr x11 2
782 str.w w2 x11 30
783 movz.x x16 0x80 1
784 sub.x x16 x3 x16
785 str.w w16 x11 31
786 b FN_SCOPE
787:FN_KNOWN
788 bl FINDFN
789 cbz.x x0 E_SYNTAX
790 movz.x x9 0x185 1
791 str.x x0 x9 3
792 str.x x0 x9 9
793 str.x x20 x0 1
794:FN_SCOPE
795 ldr.x x23 x9 1
796
797 movz.x x13 0 0
798:FN_P
799 bl TOK
800 cbz.x x0 E_FEW
801 cmp.x.i x0 1
802 b.ne E_SYNTAX
803 cmp.x.i x2 8
804 b.hi E_LONG
805 mov.x x12 x1
806 bl KW
807 cmp.x.i x0 65
808 b.eq FN_TO
809 cbnz.x x0 E_SYNTAX
810 mov.x x1 x12
811 bl NEWNAME
812 cmp.x.i x13 6
813 b.hs E_SUBSET
814 sub.x.i sp sp 48
815 str.x x12 sp 0
816 bl TOK
817 cmp.x.i x0 1
818 b.ne E_SYNTAX
819 cmp.x.i x2 8
820 b.hi E_SYNTAX
821 bl KW
822 cmp.x.i x0 57
823 b.eq FN_ARR
824 cmp.x.i x0 58
825 b.eq FN_ARR
826 sub.x.i x9 x0 1
827 cmp.x.i x9 8
828 b.hi E_SYNTAX
829 str.x x0 sp 1
830 str.x xzr sp 2
831 str.x xzr sp 3
832 b FN_ADD
833; an array parameter: view (kind 2) or edit (kind 3), element type, length (a number 1..2^24)
834:FN_ARR
835 sub.x.i x0 x0 55
836 str.x x0 sp 3
837 bl TYPEWORD
838 bl ELEMTYPE
839 str.x x0 sp 1
840 bl TOK
841 cbz.x x0 E_FEW
842 cmp.x.i x0 2
843 b.ne E_SYNTAX
844 bl ARRLEN
845 str.x x1 sp 2
846:FN_ADD
847 adr x9 POOLC
848 ldrb.r w2 x9 x13
849 ldr.x x1 sp 0
850 ldr.x x3 sp 1
851 ldr.x x4 sp 2
852 ldr.x x5 sp 3
853 movz.x x6 0 0
854 bl ADDNAME
855 movz.x x9 0x185 1
856 ldr.x x11 x9 3
857 ubfm.x x10 x13 60 59
858 add.x x11 x11 x10
859 ldr.x x0 sp 1
860 str.w w0 x11 6
861 ldr.x x0 sp 2
862 str.w w0 x11 7
863 ldr.x x0 sp 3
864 str.w w0 x11 8
865 add.x.i sp sp 48
866 add.x.i x13 x13 1
867 b FN_P
868:FN_TO
869 bl TOK
870 cbz.x x0 E_FEW
871 cmp.x.i x0 1
872 b.ne E_SYNTAX
873 cmp.x.i x2 8
874 b.hi E_SYNTAX
875 bl KW
876 cmp.x.i x0 76
877 b.eq FN_VOID
878 sub.x.i x9 x0 1
879 cmp.x.i x9 8
880 b.hi E_SYNTAX
881 mov.x x27 x0
882 b FN_T2
883:FN_VOID
884 movz.x x27 10 0
885:FN_T2
886 movz.x x9 0x185 1
887 ldr.x x11 x9 3
888 str.w w27 x11 4
889 str.w w13 x11 5
890 str.x xzr x9 4
891 str.x xzr x9 5
892 adr x29 POOLC
893 add.x x29 x29 x13
894 movz.x x24 0 0
895 movz.x x28 0 0
896 movz.x x25 0x181 1
897 movz.x x26 0x182 1
898 bl EOL
899 movz.x x9 0x185 1
900 ldr.x x10 x9 13
901 cmp.x.i x10 1
902 b.ne STMT
903 movz.x x27 0 0
904 movz.x x23 0 0
905 b STMT
906; ---- name [=] value, or name = new T N v (a local array) ----
907:S_ASSIGN
908 sub.x.i sp sp 64
909 str.x x12 sp 0
910 bl TOK
911 mov.x x3 x0
912 mov.x x4 x1
913 mov.x x5 x2
914 cmp.x.i x3 1
915 b.ne SA1
916 cmp.x.i x5 8
917 b.hi SA1
918 mov.x x1 x4
919 bl KW
920 cmp.x.i x0 73
921 b.ne SA1
922 bl TOK
923 mov.x x3 x0
924 mov.x x4 x1
925 mov.x x5 x2
926:SA1
927 ldr.x x1 sp 0
928 bl FINDNAME
929 cbz.x x0 SA_NEW
930 ldr.w w9 x0 5
931 cbnz.x x9 E_TYPE
932 ldr.w w1 x0 2
933 str.x x1 sp 1
934 ldr.w w0 x0 3
935 str.x x0 sp 2
936 bl VALTOK
937 ldr.x x9 sp 2
938 cmp.x x2 x9
939 b.ne E_TYPE
940 ldr.x x3 sp 1
941 bl FORCE
942 b SA_END
943:SA_NEW
944 ldr.x x1 sp 0
945 movz.x x2 0 0
946 bl FINDFN
947 cbnz.x x0 E_TWICE
948 cmp.x.i x3 1
949 b.ne SA2
950 cmp.x.i x5 8
951 b.hi SA2
952 mov.x x1 x4
953 bl KW
954 cmp.x.i x0 84
955 b.eq SA_ARR
956:SA2
957 bl ALLOC
958 mov.x x28 x24
959 str.x x0 sp 1
960 mov.x x1 x0
961 movz.x x0 0 0
962 bl VALTOK
963 cmp.x.i x0 2
964 b.eq E_BARE
965 str.x x2 sp 2
966 ldr.x x3 sp 1
967 bl FORCE
968 ldr.x x1 sp 0
969 ldr.x x2 sp 1
970 ldr.x x3 sp 2
971 movz.x x4 0 0
972 movz.x x5 0 0
973 movz.x x6 0 0
974 bl ADDNAME
975:SA_END
976 add.x.i sp sp 64
977 bl EOL
978 b STMT
979; x = new T N v: N elements of type T on the stack, each set to v; only in the function's outermost block,
980; so the stack held by local arrays is known at every ret. SUB SP (C6.2.457); program mode: CMP SP, X28
981; (SUBS extended, C6.2.463), B.LO trap 6; MOV Xb, SP (ADD immediate, C6.2.5); then the fill loop.
982:SA_ARR
983 movz.x x9 0x181 1
984 cmp.x x25 x9
985 b.ne E_SUBSET
986 bl TYPEWORD
987 bl ELEMTYPE
988 str.x x0 sp 2
989 bl TOK
990 cmp.x.i x0 2
991 b.ne E_SYNTAX
992 bl ARRLEN
993 str.x x1 sp 3
994 ldr.x x2 sp 2
995 adr x9 TLOG
996 ldrb.r w9 x9 x2
997 lslv.x x1 x1 x9
998 add.x.i x1 x1 15
999 ubfm.x x1 x1 4 63
1000 ubfm.x x1 x1 60 59
1001 str.x x1 sp 4
1002 movz.x x9 0x185 1
1003 ldr.x x10 x9 4
1004 add.x x10 x10 x1
1005 movz.x x11 0x40 1
1006 cmp.x x10 x11
1007 b.hi E_TOOBIG
1008 str.x x10 x9 4
1009 bl ALLOC
1010 mov.x x28 x24
1011 str.x x0 sp 1
1012 ldr.x x0 sp 2
1013 movz.x x1 255 0
1014 bl VAL
1015 ldr.x x9 sp 2
1016 cmp.x x2 x9
1017 b.ne E_TYPE
1018 bl MATREG
1019 str.x x0 sp 5
1020 ldr.x x1 sp 4
1021 bl SPSUB
1022 bl STKCHK
1023 movz.x x0 0x03e0 0
1024 movk.x x0 0x9100 1 ; MOV Xb, SP = ADD Xb, SP, #0, C6.2.5 / C6.2.278
1025 ldr.x x1 sp 1
1026 orr.x x0 x0 x1
1027 bl EMIT
1028 bl ALLOC
1029 str.x x0 sp 6
1030 movz.x x9 0xd280 1 ; MOVZ Xi, #0, C6.2.285
1031 orr.x x0 x0 x9
1032 bl EMIT
1033 ldr.x x1 sp 3
1034 movz.x x9 4095 0
1035 cmp.x x1 x9
1036 b.ls SAA1
1037 bl ALLOC
1038 str.x x0 sp 7
1039 ldr.x x1 sp 3
1040 bl MATCONST
1041:SAA1
1042 str.x x20 sp 4
1043 ldr.x x9 sp 2
1044 adr x10 STOP
1045 ldr.w.r w0 x10 x9
1046 ldr.x x1 sp 5
1047 orr.x x0 x0 x1
1048 ldr.x x1 sp 1
1049 ubfm.x x1 x1 59 58
1050 orr.x x0 x0 x1
1051 ldr.x x1 sp 6
1052 ubfm.x x1 x1 48 47
1053 orr.x x0 x0 x1
1054 bl EMIT
1055 movz.x x0 0x0400 0
1056 movk.x x0 0x9100 1 ; ADD Xi, Xi, #1, C6.2.5
1057 ldr.x x1 sp 6
1058 orr.x x0 x0 x1
1059 ubfm.x x1 x1 59 58
1060 orr.x x0 x0 x1
1061 bl EMIT
1062 ldr.x x1 sp 3
1063 movz.x x9 4095 0
1064 cmp.x x1 x9
1065 b.hi SAA2
1066 movz.x x0 0x001f 0
1067 movk.x x0 0xf100 1 ; CMP Xi, #N, C6.2.96
1068 ubfm.x x1 x1 54 53
1069 orr.x x0 x0 x1
1070 b SAA3
1071:SAA2
1072 movz.x x0 0x001f 0
1073 movk.x x0 0xeb00 1 ; CMP Xi, Xn, C6.2.97
1074 ldr.x x1 sp 7
1075 ubfm.x x1 x1 48 47
1076 orr.x x0 x0 x1
1077:SAA3
1078 ldr.x x1 sp 6
1079 ubfm.x x1 x1 59 58
1080 orr.x x0 x0 x1
1081 bl EMIT
1082 ldr.x x0 sp 4
1083 sub.x x2 x0 x20
1084 ubfm.x x2 x2 2 20
1085 ubfm.x x2 x2 59 58
1086 movz.x x0 0x0003 0
1087 movk.x x0 0x5400 1 ; B.LO back to the store, C6.2.34
1088 orr.x x0 x0 x2
1089 bl EMIT
1090 ldr.x x1 sp 0
1091 ldr.x x2 sp 1
1092 ldr.x x3 sp 2
1093 ldr.x x4 sp 3
1094 movz.x x5 1 0
1095 movz.x x6 0 0
1096 bl ADDNAME
1097 b SA_END
1098; ---- ret [value]: the value into x0, give back the local arrays' stack, RET ----
1099:S_RET
1100 cmp.x.i x27 10
1101 b.eq SR_V
1102 mov.x x0 x27
1103 movz.x x1 0 0
1104 bl VAL
1105 cmp.x x2 x27
1106 b.ne E_TYPE
1107 movz.x x3 0 0
1108 bl FORCE
1109 bl EPI
1110 bl DEAD1
1111 bl EOL
1112 b STMT
1113:SR_V
1114 bl EOL
1115 bl EPI
1116 bl DEAD1
1117 b STMT
1118; ---- exit value (int): Linux exit (system call 93, C6.2.466 SVC); the path ends ----
1119:S_EXIT
1120 movz.x x0 8 0
1121 movz.x x1 255 0
1122 bl VAL
1123 cmp.x.i x2 8
1124 b.ne E_TYPE
1125 movz.x x3 0 0
1126 bl FORCE
1127 movz.x x0 0x0ba8 0
1128 movk.x x0 0xd280 1 ; MOVZ X8, #93, C6.2.285
1129 bl EMIT
1130 movz.x x0 0x0001 0
1131 movk.x x0 0xd400 1 ; SVC #0, C6.2.466
1132 bl EMIT
1133 bl DEAD1
1134 bl EOL
1135 b STMT
1136; ---- put A i v: index, then value, then the checked store ----
1137:S_PUT
1138 sub.x.i sp sp 64
1139 bl ARRNAME
1140 str.x x0 sp 0
1141 ldr.w w9 x0 5
1142 cmp.x.i x9 2
1143 b.eq E_RDONLY
1144 cmp.x.i x9 5
1145 b.eq E_RDONLY
1146 movz.x x0 5 0
1147 movz.x x1 255 0
1148 bl VAL
1149 str.x x0 sp 1
1150 str.x x1 sp 2
1151 str.x x2 sp 3
1152 ldr.x x9 sp 0
1153 ldr.w w0 x9 3
1154 movz.x x1 255 0
1155 bl VAL
1156 ldr.x x9 sp 0
1157 ldr.w w9 x9 3
1158 cmp.x x2 x9
1159 b.ne E_TYPE
1160 bl MATREG
1161 str.x x0 sp 4
1162 ldr.x x0 sp 0
1163 ldr.x x1 sp 1
1164 ldr.x x2 sp 2
1165 ldr.x x3 sp 3
1166 bl ELEMADDR
1167 ldr.x x9 sp 0
1168 ldr.w w9 x9 3
1169 adr x10 STOP
1170 ldr.w.r w2 x10 x9
1171 ldr.x x3 sp 4
1172 orr.x x2 x2 x3
1173 ubfm.x x0 x0 59 58
1174 orr.x x2 x2 x0
1175 ubfm.x x1 x1 48 47
1176 orr.x x0 x2 x1
1177 bl EMIT
1178 add.x.i sp sp 64
1179 bl EOL
1180 b STMT
1181; ---- global NAME T N: a fixed pool of N elements, zero at start, in the read/write part of the program
1182; image after the 8 MiB stack pool (address 0x10800000 + pool bytes so far); at most 1 GiB of pools ----
1183:S_GLOBAL
1184 bl TOPNAME
1185 bl TYPEWORD
1186 bl ELEMTYPE
1187 mov.x x13 x0
1188 bl TOK
1189 cmp.x.i x0 2
1190 b.ne E_SYNTAX
1191 bl ARRLEN
1192 mov.x x14 x1
1193 adr x9 TLOG
1194 ldrb.r w9 x9 x13
1195 lslv.x x1 x1 x9
1196 add.x.i x1 x1 15
1197 ubfm.x x1 x1 4 63
1198 ubfm.x x1 x1 60 59
1199 movz.x x9 0x185 1
1200 ldr.x x10 x9 7
1201 movz.x x6 0x1080 1
1202 add.x x6 x6 x10
1203 add.x x10 x10 x1
1204 movz.x x11 0x4000 1
1205 cmp.x x10 x11
1206 b.hi E_TOOBIG
1207 str.x x10 x9 7
1208 mov.x x1 x12
1209 movz.x x2 0 0
1210 mov.x x3 x13
1211 mov.x x4 x14
1212 movz.x x5 4 0
1213 bl ADDNAME
1214 movz.x x9 0x185 1
1215 str.x x23 x9 1
1216 bl EOL
1217 b STMT
1218; ---- text NAME "...": read-only bytes in the program image (escapes \n \t \\ \"), with their length ----
1219:S_TEXT
1220 bl TOPNAME
1221 bl TOK
1222 cmp.x.i x0 4
1223 b.ne E_SYNTAX
1224 sub.x x6 x20 x21
1225 movz.x x9 0x40 1
1226 add.x x6 x6 x9
1227 add.x x2 x1 x2
1228 movz.x x14 0 0
1229:TX1
1230 cmp.x x1 x2
1231 b.hs TX4
1232 ldrb w0 x1 0
1233 add.x.i x1 x1 1
1234 cmp.w.i w0 92
1235 b.ne TX3
1236 ldrb w0 x1 0
1237 add.x.i x1 x1 1
1238 cmp.w.i w0 110
1239 b.ne TXa
1240 movz.x x0 10 0
1241 b TX3
1242:TXa
1243 cmp.w.i w0 116
1244 b.ne TXb
1245 movz.x x0 9 0
1246 b TX3
1247:TXb
1248 cmp.w.i w0 92
1249 b.eq TX3
1250 cmp.w.i w0 34
1251 b.ne E_TEXT
1252:TX3
1253 bl EMITB
1254 add.x.i x14 x14 1
1255 b TX1
1256:TX4
1257 ubfm.x x9 x20 0 1
1258 cbz.x x9 TX5
1259 movz.x x0 0 0
1260 bl EMITB
1261 b TX4
1262:TX5
1263 mov.x x1 x12
1264 movz.x x2 0 0
1265 movz.x x3 2 0
1266 mov.x x4 x14
1267 movz.x x5 5 0
1268 bl ADDNAME
1269 movz.x x9 0x185 1
1270 str.x x23 x9 1
1271 bl EOL
1272 b STMT
1273:S_IF
1274 bl COND
1275 movz.x x1 1 0
1276 movz.x x2 0 0
1277 movz.x x3 0 0
1278 movz.x x4 0 0
1279 mov.x x5 x23
1280 mov.x x6 x28
1281 bl PUSHB
1282 bl EOL
1283 b STMT
1284; ---- else: jump over the else part; the if's false branch lands after that jump ----
1285:S_ELSE
1286 movz.x x9 0x181 1
1287 cmp.x x25 x9
1288 b.eq E_BLOCK
1289 sub.x.i x10 x25 128
1290 ldr.x x0 x10 0
1291 cmp.x.i x0 1
1292 b.ne E_BLOCK
1293 mov.x x11 x20
1294 movz.x x0 0x1400 1 ; B (distance filled at end), C6.2.35
1295 bl EMIT
1296 ldr.x x0 x10 1
1297 mov.x x1 x20
1298 bl PATCH19
1299 movz.x x0 2 0
1300 str.x x0 x10 0
1301 str.x x11 x10 1
1302 ldr.x x23 x10 5
1303 ldr.x x28 x10 6
1304 movz.x x9 0x185 1
1305 ldr.x x11 x9 5
1306 str.x x11 x10 7
1307 str.x xzr x9 5
1308 bl EOL
1309 b STMT
1310; ---- while cond [bound N]: hidden counter = 0; head: cond false -> exit; counter >= N -> trap 4;
1311; counter = counter + 1; body; B head ----
1312:S_WHILE
1313 sub.x.i sp sp 64
1314 str.x x23 sp 0
1315 str.x x28 sp 1
1316 bl ALLOC
1317 mov.x x28 x24
1318 str.x x0 sp 2
1319 movz.x x9 0xd280 1 ; MOVZ Xc, #0, C6.2.285
1320 orr.x x0 x0 x9
1321 bl EMIT
1322 str.x x20 sp 3
1323 bl COND
1324 str.x x0 sp 4
1325 movz.x x9 0x4240 0
1326 movk.x x9 0x000f 1
1327 str.x x9 sp 5
1328 bl TOK
1329 cbz.x x0 SW_NOB
1330 cmp.x.i x0 3
1331 b.eq SW_NOB
1332 cmp.x.i x0 1
1333 b.ne E_EXTRA
1334 cmp.x.i x2 8
1335 b.hi E_EXTRA
1336 bl KW
1337 cmp.x.i x0 70
1338 b.ne E_EXTRA
1339 bl TOK
1340 cbz.x x0 E_FEW
1341 cmp.x.i x0 2
1342 b.ne E_SYNTAX
1343 sub.x.i x9 x1 1
1344 movz.x x10 0x423f 0
1345 movk.x x10 0x000f 1
1346 cmp.x x9 x10
1347 b.hs E_RANGE
1348 str.x x1 sp 5
1349 bl EOL
1350:SW_NOB
1351 ldr.x x1 sp 5
1352 movz.x x9 4095 0
1353 cmp.x x1 x9
1354 b.hi SW_BIG
1355 movz.x x0 0x001f 0
1356 movk.x x0 0xf100 1 ; CMP Xc, #N = SUBS XZR, Xc, #N, C6.2.96
1357 ubfm.x x1 x1 54 53
1358 orr.x x0 x0 x1
1359 ldr.x x1 sp 2
1360 ubfm.x x1 x1 59 58
1361 orr.x x0 x0 x1
1362 bl EMIT
1363 b SW_G
1364:SW_BIG
1365 bl ALLOC
1366 mov.x x13 x0
1367 ldr.x x1 sp 5
1368 bl MATCONST
1369 movz.x x0 0x001f 0
1370 movk.x x0 0xeb00 1 ; CMP Xc, Xn = SUBS XZR, Xc, Xn, C6.2.97
1371 ldr.x x1 sp 2
1372 ubfm.x x1 x1 59 58
1373 orr.x x0 x0 x1
1374 ubfm.x x1 x13 48 47
1375 orr.x x0 x0 x1
1376 bl EMIT
1377:SW_G
1378 movz.x x0 2 0
1379 movz.x x1 4 0
1380 bl TRAPC
1381 movz.x x0 0x0400 0
1382 movk.x x0 0x9100 1 ; ADD Xc, Xc, #1, C6.2.5
1383 ldr.x x1 sp 2
1384 orr.x x0 x0 x1
1385 ubfm.x x1 x1 59 58
1386 orr.x x0 x0 x1
1387 bl EMIT
1388 ldr.x x0 sp 4
1389 movz.x x1 3 0
1390 ldr.x x2 sp 3
1391 ldr.x x3 sp 2
1392 movz.x x4 0 0
1393 ldr.x x5 sp 0
1394 ldr.x x6 sp 1
1395 bl PUSHB
1396 add.x.i sp sp 64
1397 b STMT
1398; ---- for i T K (K a number): i = 0; head: not i < K -> exit; body; latch i = add i 1; B head ----
1399; ---- for i T K: i = 0; head: not i < K -> exit; body; latch i = add i 1; B head. K a number, or a value
1400; of type T: a name is read at every pass, another value once before the loop; then the hidden counter
1401; guard of a while with no bound (trap 4 after 1000000 passes) ----
1402:S_FOR
1403 sub.x.i sp sp 80
1404 str.x x23 sp 0
1405 str.x x28 sp 1
1406 bl TOK
1407 cbz.x x0 E_FEW
1408 cmp.x.i x0 1
1409 b.ne E_SYNTAX
1410 cmp.x.i x2 8
1411 b.hi E_LONG
1412 str.x x1 sp 2
1413 bl KW
1414 cbnz.x x0 E_SYNTAX
1415 ldr.x x1 sp 2
1416 bl NEWNAME
1417 bl TYPEWORD
1418 str.x x0 sp 3
1419 cmp.x.i x0 1
1420 b.eq E_TYPE
1421 bl TOK
1422 cbz.x x0 E_FEW
1423 cmp.x.i x0 2
1424 b.ne SF_VAL
1425 ldr.x x2 sp 3
1426 movz.x x9 0x185 1
1427 ldr.x x3 x9 11
1428 bl RANGE
1429 str.x x1 sp 4
1430:SF_NUM
1431 bl EOL
1432 bl ALLOC
1433 mov.x x28 x24
1434 str.x x0 sp 5
1435 mov.x x2 x0
1436 ldr.x x1 sp 2
1437 ldr.x x3 sp 3
1438 movz.x x4 0 0
1439 movz.x x5 0 0
1440 movz.x x6 0 0
1441 bl ADDNAME
1442 ldr.x x0 sp 5
1443 movz.x x9 0xd280 1 ; MOVZ Xi, #0, C6.2.285
1444 orr.x x0 x0 x9
1445 bl EMIT
1446 str.x x20 sp 6
1447 ldr.x x1 sp 4
1448 movz.x x9 4095 0
1449 cmp.x x1 x9
1450 b.hi SF_BIG
1451 movz.x x0 0x001f 0
1452 movk.x x0 0xf100 1 ; CMP Xi, #K, C6.2.96
1453 ubfm.x x1 x1 54 53
1454 orr.x x0 x0 x1
1455 ldr.x x1 sp 5
1456 ubfm.x x1 x1 59 58
1457 orr.x x0 x0 x1
1458 bl EMIT
1459 b SF_B
1460:SF_BIG
1461 bl ALLOC
1462 mov.x x13 x0
1463 ldr.x x1 sp 4
1464 bl MATCONST
1465 mov.x x1 x13
1466 b SF_CMPR
1467; a value count
1468:SF_VAL
1469 mov.x x3 x0
1470 mov.x x4 x1
1471 mov.x x5 x2
1472 ldr.x x0 sp 3
1473 movz.x x1 255 0
1474 bl VALTOK
1475 cmp.x.i x0 3
1476 b.eq SF_TYPED
1477 ldr.x x9 sp 3
1478 cmp.x x2 x9
1479 b.ne E_TYPE
1480 bl MATREG
1481 str.x x0 sp 7
1482 bl EOL
1483 bl ALLOC
1484 mov.x x28 x24
1485 str.x x0 sp 5
1486 mov.x x2 x0
1487 ldr.x x1 sp 2
1488 ldr.x x3 sp 3
1489 movz.x x4 0 0
1490 movz.x x5 0 0
1491 movz.x x6 0 0
1492 bl ADDNAME
1493 ldr.x x0 sp 5
1494 movz.x x9 0xd280 1 ; MOVZ Xi, #0, C6.2.285
1495 orr.x x0 x0 x9
1496 bl EMIT
1497 bl ALLOC
1498 mov.x x28 x24
1499 str.x x0 sp 8
1500 movz.x x9 0xd280 1 ; MOVZ Xc, #0 (the hidden counter), C6.2.285
1501 orr.x x0 x0 x9
1502 bl EMIT
1503 str.x x20 sp 6
1504 ldr.x x1 sp 7
1505 movz.x x0 0x001f 0
1506 movk.x x0 0xeb00 1 ; CMP Xi, Xn, C6.2.97
1507 ubfm.x x1 x1 48 47
1508 orr.x x0 x0 x1
1509 ldr.x x1 sp 5
1510 ubfm.x x1 x1 59 58
1511 orr.x x0 x0 x1
1512 bl EMIT
1513 bl SF_BR
1514 str.x x0 sp 9
1515 bl ALLOC
1516 mov.x x13 x0
1517 movz.x x1 0x4240 0
1518 movk.x x1 0x000f 1
1519 bl MATCONST
1520 movz.x x0 0x001f 0
1521 movk.x x0 0xeb00 1 ; CMP Xc, Xm (the counter against 1000000), C6.2.97
1522 ldr.x x1 sp 8
1523 ubfm.x x1 x1 59 58
1524 orr.x x0 x0 x1
1525 ubfm.x x1 x13 48 47
1526 orr.x x0 x0 x1
1527 bl EMIT
1528 movz.x x0 2 0
1529 movz.x x1 4 0
1530 bl TRAPC
1531 movz.x x0 0x0400 0
1532 movk.x x0 0x9100 1 ; ADD Xc, Xc, #1, C6.2.5
1533 ldr.x x1 sp 8
1534 orr.x x0 x0 x1
1535 ubfm.x x1 x1 59 58
1536 orr.x x0 x0 x1
1537 bl EMIT
1538 ldr.x x0 sp 9
1539 b SF_PUSH
1540:SF_TYPED
1541 ldr.x x9 sp 3
1542 cmp.x x2 x9
1543 b.ne E_TYPE
1544 str.x x1 sp 4
1545 b SF_NUM
1546:SF_CMPR
1547 movz.x x0 0x001f 0
1548 movk.x x0 0xeb00 1 ; CMP Xi, Xk, C6.2.97
1549 ldr.x x9 sp 5
1550 ubfm.x x9 x9 59 58
1551 orr.x x0 x0 x9
1552 ubfm.x x1 x1 48 47
1553 orr.x x0 x0 x1
1554 bl EMIT
1555:SF_B
1556 bl SF_BR
1557:SF_PUSH
1558 movz.x x1 4 0
1559 ldr.x x2 sp 6
1560 ldr.x x3 sp 5
1561 ldr.x x4 sp 3
1562 ldr.x x5 sp 0
1563 ldr.x x6 sp 1
1564 bl PUSHB
1565 add.x.i sp sp 80
1566 b STMT
1567; SF_BR: B.HS (unsigned) or B.GE (signed) to the loop exit, C6.2.34 (distance filled at end); x0 = its place
1568; (the loop type is the caller frame slot 3, at sp + 40 once this routine has pushed 16 bytes)
1569:SF_BR
1570 str.x.pre x30 sp -16
1571 mov.x x13 x20
1572 movz.x x0 0x5400 1 ; B.cond, C6.2.34
1573 ldr.x x9 sp 5
1574 movz.x x1 2 0
1575 cmp.x.i x9 6
1576 b.lo SFB1
1577 movz.x x1 10 0
1578:SFB1
1579 orr.x x0 x0 x1
1580 bl EMIT
1581 mov.x x0 x13
1582 ldr.x.post x30 sp 16
1583 ret
1584; ---- end: close the innermost block, or the function ----
1585:S_END
1586 movz.x x9 0x181 1
1587 cmp.x x25 x9
1588 b.eq FINISH
1589 sub.x.i x25 x25 128
1590 movz.x x9 0x185 1
1591 ldr.x x0 x25 0
1592 cmp.x.i x0 2
1593 b.eq SE_D
1594 str.x xzr x9 5
1595 b SE_D2
1596:SE_D
1597 ldr.x x10 x9 5
1598 ldr.x x11 x25 7
1599 and.x x10 x10 x11
1600 str.x x10 x9 5
1601:SE_D2
1602 cmp.x.i x0 1
1603 b.eq SE_IF
1604 cmp.x.i x0 2
1605 b.eq SE_ELSE
1606 cmp.x.i x0 3
1607 b.eq SE_WHILE
1608 sub.x.i sp sp 48
1609 movz.x x0 1 0
1610 str.x x0 sp 0
1611 ldr.x x0 x25 3
1612 str.x x0 sp 1
1613 ldr.x x0 x25 4
1614 str.x x0 sp 2
1615 movz.x x0 3 0
1616 str.x x0 sp 3
1617 movz.x x0 1 0
1618 str.x x0 sp 4
1619 ldr.x x0 x25 4
1620 str.x x0 sp 5
1621 movz.x x0 33 0
1622 ldr.x x1 x25 3
1623 add.x.i x2 sp 0
1624 bl BINOP
1625 add.x.i sp sp 48
1626:SE_WHILE
1627 ldr.x x0 x25 2
1628 bl BACK
1629:SE_IF
1630 ldr.x x0 x25 1
1631 mov.x x1 x20
1632 bl PATCH19
1633 b SE_X
1634:SE_ELSE
1635 ldr.x x0 x25 1
1636 mov.x x1 x20
1637 bl PATCH26
1638:SE_X
1639 ldr.x x23 x25 5
1640 ldr.x x28 x25 6
1641 bl EOL
1642 b STMT
1643; ---- end of a function: a void function that can reach its end returns; trap stubs ----
1644; shared tail, function mode: MOV X0, X7; LDR X16, [X7, #16]; BR X16 (OscRt.trap); program mode:
1645; ADD X0, X1, #100; MOVZ X8, #93; SVC #0 (exit 100 + code). Per trap code used: MOVZ X1, #code; B tail.
1646:FINISH
1647 bl EOL
1648 movz.x x9 0x185 1
1649 ldr.x x10 x9 5
1650 cbnz.x x10 FI_0
1651 cmp.x.i x27 10
1652 b.ne E_NORET
1653 bl EPI
1654:FI_0
1655 movz.x x9 0x182 1
1656 cmp.x x26 x9
1657 b.eq FI_T
1658 mov.x x13 x20
1659 movz.x x9 0x185 1
1660 ldr.x x10 x9 6
1661 cbnz.x x10 FI_PT
1662 movz.x x0 0x03e0 0
1663 movk.x x0 0xaa07 1 ; MOV X0, X7 = ORR X0, XZR, X7, C6.2.282
1664 bl EMIT
1665 movz.x x0 0x08f0 0
1666 movk.x x0 0xf940 1 ; LDR X16, [X7, #16], unsigned offset imm12 = 2, C6.2.216
1667 bl EMIT
1668 movz.x x0 0x0200 0
1669 movk.x x0 0xd61f 1 ; BR X16, C6.2.46
1670 bl EMIT
1671 b FI_C0
1672:FI_PT
1673 movz.x x0 0x9020 0
1674 movk.x x0 0x9101 1 ; ADD X0, X1, #100, C6.2.5
1675 bl EMIT
1676 movz.x x0 0x0ba8 0
1677 movk.x x0 0xd280 1 ; MOVZ X8, #93, C6.2.285
1678 bl EMIT
1679 movz.x x0 0x0001 0
1680 movk.x x0 0xd400 1 ; SVC #0, C6.2.466
1681 bl EMIT
1682:FI_C0
1683 movz.x x14 1 0
1684:FI_C
1685 movz.x x15 0 0
1686 movz.x x12 0x182 1
1687:FI_S
1688 cmp.x x12 x26
1689 b.hs FI_N
1690 ldr.w w0 x12 1
1691 cmp.x x0 x14
1692 b.ne FI_NX
1693 cbnz.x x15 FI_P
1694 mov.x x15 x20
1695 movz.x x0 0x0001 0
1696 movk.x x0 0xd280 1 ; MOVZ X1, #code, C6.2.285
1697 ubfm.x x1 x14 59 58
1698 orr.x x0 x0 x1
1699 bl EMIT
1700 mov.x x0 x13
1701 bl BACK
1702:FI_P
1703 ldr.w w0 x12 0
1704 mov.x x1 x15
1705 bl PATCH19
1706:FI_NX
1707 add.x.i x12 x12 8
1708 b FI_S
1709:FI_N
1710 add.x.i x14 x14 1
1711 cmp.x.i x14 14
1712 b.ls FI_C
1713:FI_T
1714 movz.x x27 0 0
1715 movz.x x9 0x185 1
1716 ldr.x x23 x9 1
1717 str.x xzr x9 3
1718 b STMT
1719; ---- end of input: write the output ----
1720:S_EOF
1721 cbnz.x x27 E_BLOCK
1722 bl RESOLVE
1723 movz.x x9 0x185 1
1724 ldr.x x10 x9 2
1725 cbz.x x10 E_SYNTAX
1726 ldr.x x11 x9 6
1727 cbnz.x x11 EOF_PROG
1728 ldr.x x11 x9 1
1729 cbnz.x x11 E_SUBSET
1730 add.x.i x12 x21 180
1731 cmp.x.i x10 1
1732 b.eq WRITEOUT
1733 ldr.x x0 x9 9
1734 ldr.x x0 x0 1
1735 add.x.i x12 x21 176
1736 sub.x x2 x0 x12
1737 ubfm.x x2 x2 2 27
1738 movz.x x3 0x1400 1 ; B to the last function, C6.2.35
1739 orr.x x3 x3 x2
1740 str.w w3 x12 0
1741 b WRITEOUT
1742; program mode: start code = MOVZ X28, #0x1000, LSL 16 (stack pool floor); MOVZ X16, #0x1080, LSL 16;
1743; MOV SP, X16 (ADD immediate, C6.2.5); BL main (C6.2.43); [MOVZ X0, #0 for a void main]; MOVZ X8, #93;
1744; SVC #0. The entry word after the header is a B to it.
1745:EOF_PROG
1746 movz.x x2 4 0
1747 movz.x x1 0x616d 0
1748 movk.x x1 0x6e69 1
1749 bl FINDFN
1750 cbz.x x0 E_SYNTAX
1751 ldr.w w9 x0 5
1752 cbnz.x x9 E_SUBSET
1753 mov.x x14 x0
1754 add.x.i x12 x21 176
1755 sub.x x2 x20 x12
1756 ubfm.x x2 x2 2 27
1757 movz.x x3 0x1400 1 ; B, C6.2.35
1758 orr.x x3 x3 x2
1759 str.w w3 x12 0
1760 movz.x x0 0x001c 0
1761 movk.x x0 0xd2a2 1 ; MOVZ X28, #0x1000, LSL #16, C6.2.285
1762 bl EMIT
1763 movz.x x0 0x1010 0
1764 movk.x x0 0xd2a2 1 ; MOVZ X16, #0x1080, LSL #16, C6.2.285
1765 bl EMIT
1766 movz.x x0 0x021f 0
1767 movk.x x0 0x9100 1 ; MOV SP, X16 = ADD SP, X16, #0, C6.2.5
1768 bl EMIT
1769 ldr.x x0 x14 1
1770 sub.x x2 x0 x20
1771 ubfm.x x2 x2 2 27
1772 movz.x x0 0x9400 1 ; BL main, C6.2.43
1773 orr.x x0 x0 x2
1774 bl EMIT
1775 ldr.w w9 x14 4
1776 cmp.x.i x9 10
1777 b.ne EP1
1778 movz.x x0 0xd280 1 ; MOVZ X0, #0, C6.2.285
1779 bl EMIT
1780:EP1
1781 movz.x x0 0x0ba8 0
1782 movk.x x0 0xd280 1 ; MOVZ X8, #93, C6.2.285
1783 bl EMIT
1784 movz.x x0 0x0001 0
1785 movk.x x0 0xd400 1 ; SVC #0, C6.2.466
1786 bl EMIT
1787 adr x10 ELFHDR
1788 mov.x x11 x21
1789 movz.x x12 0 0
1790:EP2
1791 ldr.w.post w0 x10 4
1792 str.w.post w0 x11 4
1793 add.x.i x12 x12 1
1794 cmp.x.i x12 44
1795 b.lo EP2
1796 sub.x x0 x20 x21
1797 str.w w0 x21 24
1798 str.w w0 x21 26
1799 movz.x x9 0x185 1
1800 ldr.x x0 x9 7
1801 movz.x x1 0x80 1
1802 add.x x0 x0 x1
1803 str.w w0 x21 40
1804 mov.x x12 x21
1805:WRITEOUT
1806 sub.x x2 x20 x12
1807 cbz.x x2 EXIT0
1808 movz.x x0 1 0
1809 mov.x x1 x12
1810 movz.x x8 64 0
1811 svc 0
1812 cmp.x.i x0 0
1813 b.le E_WRITE
1814 add.x x12 x12 x0
1815 b WRITEOUT
1816:EXIT0
1817 movz.x x0 0 0
1818 movz.x x8 93 0
1819 svc 0
1820; VAL: read one value. x0 expected type (0 none), x1 destination register (255 none).
1821; Out: x0 kind (1 register, 2 bare number, 3 typed number, 4 flags), x1 register / number / condition,
1822; x2 type. Only the outermost op writes the destination. Clobbers x0..x17.
1823:VAL
1824 str.x.pre x30 sp -32
1825 str.x x0 sp 1
1826 str.x x1 sp 2
1827 bl TOK
1828 mov.x x3 x0
1829 mov.x x4 x1
1830 mov.x x5 x2
1831 ldr.x x0 sp 1
1832 ldr.x x1 sp 2
1833 ldr.x.post x30 sp 32
1834; VALTOK: the same with the first word already read (x3 kind, x4 word or number, x5 length).
1835; Frame (8-byte slots): 0 x30, 1 expected, 2 dest, 3 op, 4..6 a, 7..9 b, 10 type of a conversion.
1836:VALTOK
1837 str.x.pre x30 sp -128
1838 str.x x0 sp 1
1839 str.x x1 sp 2
1840 movz.x x9 0x185 1 ; nesting depth (state slot 10): at most 1024 values inside one another, else error 14
1841 ldr.x x10 x9 10
1842 add.x.i x10 x10 1
1843 str.x x10 x9 10
1844 cmp.x.i x10 1024
1845 b.hi E_FULL
1846 cbz.x x3 E_FEW
1847 cmp.x.i x3 3
1848 b.eq E_FEW
1849 cmp.x.i x3 4
1850 b.eq E_SYNTAX
1851 cmp.x.i x3 2
1852 b.ne VT_W
1853 mov.x x1 x4
1854 movz.x x9 0x185 1 ; a bare number: x2 = 1 when written as 2^63 or more (RANGE needs it later)
1855 ldr.x x2 x9 11
1856 movz.x x0 2 0
1857 ldr.x x9 sp 1
1858 cbz.x x9 VT_RET
1859 mov.x x3 x2
1860 mov.x x2 x9
1861 bl RANGE
1862 movz.x x0 3 0
1863 b VT_RET
1864:VT_W
1865 cmp.x.i x5 8
1866 b.hi VT_LONG
1867 mov.x x1 x4
1868 bl KW
1869 cbnz.x x0 VT_K
1870 str.x x1 sp 4
1871 bl FINDNAME
1872 cbz.x x0 VT_FN
1873 ldr.w w9 x0 5
1874 cbnz.x x9 E_TYPE
1875 ldr.w w1 x0 2
1876 ldr.w w2 x0 3
1877 movz.x x0 1 0
1878 b VT_RET
1879; a call of a function that gives a value
1880:VT_FN
1881 ldr.x x1 sp 4
1882 movz.x x2 0 0
1883 bl FINDFN
1884 cbz.x x0 E_UNDEF
1885:VT_FN2
1886 ldr.w w9 x0 4
1887 cmp.x.i x9 10
1888 b.eq E_TYPE
1889 ldr.x x1 sp 2
1890 bl CALL
1891 b VT_RET
1892:VT_K
1893 sub.x.i x9 x0 1
1894 cmp.x.i x9 8
1895 b.ls VT_TYPE
1896 cmp.x.i x0 74
1897 b.eq VT_PAREN
1898 cmp.x.i x0 77
1899 b.eq E_SUBSET
1900 str.x x0 sp 3
1901 cmp.x.i x0 51
1902 b.eq VT_GET
1903 cmp.x.i x0 52
1904 b.eq VT_LEN
1905 cmp.x.i x0 53
1906 b.eq VT_SYS
1907 cmp.x.i x0 54
1908 b.eq VT_SYS
1909 cmp.x.i x0 55
1910 b.eq VT_TRUE
1911 cmp.x.i x0 56
1912 b.eq VT_FALSE
1913 sub.x.i x9 x0 33
1914 cmp.x.i x9 17
1915 b.hi E_SYNTAX
1916 str.x x0 sp 3
1917 cmp.x.i x0 49
1918 b.hs VT_UN
1919; binary op: a, then b; a comparison used as a value is turned into a register at once (flags would not
1920; survive the next value)
1921 movz.x x0 0 0
1922 movz.x x1 255 0
1923 bl VAL
1924 cmp.x.i x0 4
1925 b.ne VB1
1926 bl MATREG
1927 mov.x x1 x0
1928 movz.x x0 1 0
1929 movz.x x2 1 0
1930:VB1
1931 str.x x0 sp 4
1932 str.x x1 sp 5
1933 str.x x2 sp 6
1934 movz.x x0 0 0
1935 movz.x x1 255 0
1936 bl VAL
1937 cmp.x.i x0 4
1938 b.ne VB2
1939 bl MATREG
1940 mov.x x1 x0
1941 movz.x x0 1 0
1942 movz.x x2 1 0
1943:VB2
1944 str.x x0 sp 7
1945 str.x x1 sp 8
1946 str.x x2 sp 9
1947; a bare number takes the type of the other value (in a shift too: shl 1 sh)
1948 ldr.x x10 sp 4
1949 ldr.x x11 sp 7
1950 cmp.x.i x10 2
1951 b.ne VB3
1952 cmp.x.i x11 2
1953 b.eq E_BARE
1954 ldr.x x1 sp 5
1955 ldr.x x2 sp 9
1956 ldr.x x3 sp 6
1957 bl RANGE
1958 str.x x2 sp 6
1959 movz.x x0 3 0
1960 str.x x0 sp 4
1961:VB3
1962 cmp.x.i x11 2
1963 b.ne VB4
1964 ldr.x x1 sp 8
1965 ldr.x x2 sp 6
1966 ldr.x x3 sp 9
1967 bl RANGE
1968 str.x x2 sp 9
1969 movz.x x0 3 0
1970 str.x x0 sp 7
1971:VB4
1972 ldr.x x0 sp 3
1973 cmp.x.i x0 41
1974 b.eq VB5
1975 cmp.x.i x0 42
1976 b.eq VB5
1977 ldr.x x1 sp 6
1978 ldr.x x2 sp 9
1979 cmp.x x1 x2
1980 b.ne E_TYPE
1981:VB5
1982 ldr.x x0 sp 3
1983 ldr.x x1 sp 2
1984 add.x.i x2 sp 32
1985 bl BINOP
1986:VT_RET
1987 movz.x x9 0x185 1
1988 ldr.x x10 x9 10
1989 sub.x.i x10 x10 1
1990 str.x x10 x9 10
1991 ldr.x.post x30 sp 128
1992 ret
1993; ( value )
1994:VT_PAREN
1995 ldr.x x0 sp 1
1996 ldr.x x1 sp 2
1997 bl VAL
1998 str.x x0 sp 4
1999 str.x x1 sp 5
2000 str.x x2 sp 6
2001 bl TOK
2002 cmp.x.i x0 1
2003 b.ne E_SYNTAX
2004 cmp.x.i x2 8
2005 b.hi E_SYNTAX
2006 bl KW
2007 cmp.x.i x0 75
2008 b.ne E_SYNTAX
2009 ldr.x x0 sp 4
2010 ldr.x x1 sp 5
2011 ldr.x x2 sp 6
2012 b VT_RET
2013; neg a, not a (a bare number takes the expected type)
2014:VT_UN
2015 ldr.x x0 sp 1
2016 movz.x x1 255 0
2017 bl VAL
2018 ldr.x x9 sp 3
2019 cmp.x.i x9 50
2020 b.ne VU1
2021 cmp.x.i x0 4
2022 b.ne VU1
2023 movz.x x9 1 0
2024 eor.x x1 x1 x9
2025 b VT_RET
2026:VU1
2027 cmp.x.i x0 2
2028 b.eq E_BARE
2029 str.x x2 sp 6
2030 bl MATREG
2031 str.x x0 sp 5
2032 ldr.x x0 sp 2
2033 bl DESTREG
2034 str.x x0 sp 7
2035 ldr.x x9 sp 3
2036 cmp.x.i x9 50
2037 b.eq VU_NOT
2038 ldr.x x9 sp 6
2039 cmp.x.i x9 1
2040 b.eq E_TYPE
2041 cmp.x.i x9 9
2042 b.eq VN64
2043 movz.x x0 0x03e0 0
2044 movk.x x0 0xcb00 1 ; NEG Xd, Xm = SUB Xd, XZR, Xm, C6.2.458
2045 cmp.x.i x9 4
2046 b.ne VN1
2047 movz.x x0 0x03e0 0
2048 movk.x x0 0x4b00 1 ; NEG Wd, Wm (32-bit SUB, C6.2.458): the u32 result comes zero-extended
2049 bl VU_E
2050 b VU_R
2051:VN1
2052 bl VU_E
2053 ldr.x x0 sp 7
2054 ldr.x x1 sp 6
2055 cmp.x.i x1 6
2056 b.hs VN2
2057 bl CANON
2058 b VU_R
2059:VN2
2060 movz.x x2 1 0
2061 bl CHKEXT
2062 b VU_R
2063:VN64
2064 movz.x x0 0x03e0 0
2065 movk.x x0 0xeb00 1 ; NEGS Xd, Xm = SUBS Xd, XZR, Xm, C6.2.465
2066 bl VU_E
2067 movz.x x0 6 0
2068 movz.x x1 1 0
2069 bl TRAPC
2070 b VU_R
2071:VU_NOT
2072 ldr.x x9 sp 6
2073 cmp.x.i x9 1
2074 b.ne VNT1
2075 movz.x x0 0 0
2076 movk.x x0 0xd240 1 ; EOR Xd, Xn, #1 (immediate, N=1 immr=0 imms=0), C6.2.155
2077 ldr.x x1 sp 7
2078 ldr.x x2 sp 5
2079 movz.x x3 0 0
2080 bl E3
2081 b VU_R
2082:VNT1
2083 movz.x x0 0x03e0 0
2084 movk.x x0 0xaa20 1 ; MVN Xd, Xm = ORN Xd, XZR, Xm, C6.2.300
2085 cmp.x.i x9 4
2086 b.ne VNT2
2087 movz.x x0 0x03e0 0
2088 movk.x x0 0x2a20 1 ; MVN Wd, Wm (32-bit ORN, C6.2.300)
2089 bl VU_E
2090 b VU_R
2091:VNT2
2092 bl VU_E
2093 ldr.x x0 sp 7
2094 ldr.x x1 sp 6
2095 cmp.x.i x1 6
2096 b.hs VU_R
2097 bl CANON
2098:VU_R
2099 movz.x x0 1 0
2100 ldr.x x1 sp 7
2101 ldr.x x2 sp 6
2102 b VT_RET
2103:VU_E
2104 ldr.x x1 sp 7
2105 movz.x x2 0 0
2106 ldr.x x3 sp 5
2107 b E3
2108; a type word: a typed number (uint 5) or a conversion of the next value
2109:VT_TYPE
2110 str.x x0 sp 10
2111 bl TOK
2112 cmp.x.i x0 2
2113 b.ne VTT1
2114 ldr.x x2 sp 10
2115 movz.x x9 0x185 1 ; x3 = the number was written as 2^63 or more
2116 ldr.x x3 x9 11
2117 bl RANGE
2118 movz.x x0 3 0
2119 b VT_RET
2120:VTT1
2121 mov.x x3 x0
2122 mov.x x4 x1
2123 mov.x x5 x2
2124 movz.x x0 0 0
2125 movz.x x1 255 0
2126 bl VALTOK
2127 cmp.x.i x0 2
2128 b.eq E_BARE
2129 str.x x2 sp 6
2130 bl MATREG
2131 str.x x0 sp 5
2132 ldr.x x1 sp 6
2133 ldr.x x2 sp 10
2134 cmp.x.i x1 1
2135 b.eq E_SUBSET
2136 cmp.x.i x2 1
2137 b.eq E_SUBSET
2138 adr x9 TWID
2139 ldrb.r w3 x9 x1
2140 ldrb.r w4 x9 x2
2141 cmp.x.i x1 6
2142 b.hs VC_SS
2143 cmp.x.i x2 6
2144 b.hs VC_US
2145 cmp.x x3 x4
2146 b.ls VC_OK
2147 mov.x x5 x4
2148 b VC_LSR
2149:VC_US
2150 cmp.x x3 x4
2151 b.lo VC_OK
2152 sub.x.i x5 x4 1
2153 b VC_LSR
2154:VC_SS
2155 cmp.x.i x2 6
2156 b.hs VC_S2
2157 mov.x x5 x4
2158 cmp.x.i x5 63
2159 b.ls VC_LSR
2160 movz.x x5 63 0
2161 b VC_LSR
2162:VC_S2
2163 cmp.x x3 x4
2164 b.ls VC_OK
2165 ldr.x x0 sp 5
2166 ldr.x x1 sp 10
2167 movz.x x2 5 0
2168 bl CHKEXT
2169 b VC_OK
2170; out of range when any bit at or above x5 is set: LSR Xt, Xa, #x5 = UBFM Xt, Xa, #x5, #63 (C6.2.487);
2171; CBNZ Xt -> trap 5 (C6.2.77)
2172:VC_LSR
2173 bl ALLOC
2174 str.x x0 sp 7
2175 movz.x x1 0xfc00 0
2176 movk.x x1 0xd340 1
2177 orr.x x1 x1 x0
2178 ubfm.x x5 x5 48 47
2179 orr.x x1 x1 x5
2180 ldr.x x2 sp 5
2181 ubfm.x x2 x2 59 58
2182 orr.x x0 x1 x2
2183 bl EMIT
2184 ldr.x x0 sp 7
2185 movz.x x9 0xb500 1 ; CBNZ Xt, C6.2.77
2186 orr.x x0 x0 x9
2187 movz.x x1 5 0
2188 bl TRAPSITE
2189:VC_OK
2190 movz.x x0 1 0
2191 ldr.x x1 sp 5
2192 ldr.x x2 sp 10
2193 b VT_RET
2194; ======================================================================================================
2195; BINOP: x0 op (33..48), x1 destination (255 none), x2 address of two values (kind, number/register,
2196; type) a then b, types already settled. Out: as VAL. Frame: 0 x30, 1 op, 2 rd, 3 values, 4 ra,
2197; 5 rb or immediate, 6 type, 7 1 if b is an immediate, 8 temporary.
2198:BINOP
2199 str.x.pre x30 sp -80
2200 str.x x0 sp 1
2201 str.x x1 sp 2
2202 str.x x2 sp 3
2203 ldr.x x9 x2 2
2204 str.x x9 sp 6
2205 ldr.x x0 x2 0
2206 ldr.x x1 x2 1
2207 ldr.x x2 x2 2
2208 bl MATREG
2209 str.x x0 sp 4
2210 movz.x x9 0 0
2211 str.x x9 sp 7
2212 ldr.x x10 sp 3
2213 ldr.x x0 x10 3
2214 cmp.x.i x0 3
2215 b.ne BO_BREG
2216 ldr.x x1 x10 4
2217 ldr.x x11 sp 1
2218 cmp.x.i x11 41
2219 b.eq BO_IS
2220 cmp.x.i x11 42
2221 b.eq BO_IS
2222 cmp.x.i x11 35
2223 b.lo BO_II
2224 cmp.x.i x11 43
2225 b.lo BO_BREG
2226:BO_II
2227 movz.x x9 4095 0
2228 cmp.x x1 x9
2229 b.hi BO_BREG
2230 b BO_IMM
2231:BO_IS
2232 ldr.x x9 sp 6
2233 adr x12 TWID
2234 ldrb.r w9 x12 x9
2235 cmp.x x1 x9
2236 b.hs BO_BREG
2237:BO_IMM
2238 str.x x1 sp 5
2239 movz.x x9 1 0
2240 str.x x9 sp 7
2241 b BO_OPS
2242:BO_BREG
2243 ldr.x x10 sp 3
2244 ldr.x x0 x10 3
2245 ldr.x x1 x10 4
2246 ldr.x x2 x10 5
2247 bl MATREG
2248 str.x x0 sp 5
2249:BO_OPS
2250 ldr.x x0 sp 1
2251 cmp.x.i x0 43
2252 b.hs BO_CMP
2253 ldr.x x9 sp 6
2254 cmp.x.i x9 1
2255 b.ne BO_NB
2256 sub.x.i x10 x0 38
2257 cmp.x.i x10 2
2258 b.hi E_TYPE
2259:BO_NB
2260 ldr.x x0 sp 2
2261 bl DESTREG
2262 str.x x0 sp 2
2263 ldr.x x0 sp 1
2264 cmp.x.i x0 35
2265 b.lo BO_ADD
2266 b.eq BO_MUL
2267 cmp.x.i x0 38
2268 b.lo BO_DIV
2269 cmp.x.i x0 41
2270 b.lo BO_LOG
2271 b.eq BO_SHL
2272 b BO_SHR
2273; add, sub. u32: 32-bit form (zero-extends); i64: flag-setting form + B.VS trap 1; u8/u16: wrap with
2274; UBFM; i8/i16/i32: exact in 64 bits, then the sign-extension check (trap 1)
2275:BO_ADD
2276 ldr.x x9 sp 1
2277 ldr.x x10 sp 7
2278 cbnz.x x10 BA_I
2279 movz.x x0 0x8b00 1 ; ADD (shifted register) 64-bit, C6.2.6
2280 cmp.x.i x9 33
2281 b.eq BA_S
2282 movz.x x0 0xcb00 1 ; SUB (shifted register) 64-bit, C6.2.458
2283 b BA_S
2284:BA_I
2285 movz.x x0 0x9100 1 ; ADD (immediate) 64-bit, C6.2.5
2286 cmp.x.i x9 33
2287 b.eq BA_S
2288 movz.x x0 0xd100 1 ; SUB (immediate) 64-bit, C6.2.457
2289:BA_S
2290 ldr.x x9 sp 6
2291 cmp.x.i x9 4
2292 b.ne BA2
2293 movz.x x10 0x8000 1 ; sf (bit 31) = 0: the 32-bit form
2294 eor.x x0 x0 x10
2295 bl BO_W3
2296 b BO_RET
2297:BA2
2298 cmp.x.i x9 9
2299 b.ne BA3
2300 movz.x x10 0x2000 1 ; S (bit 29) = 1: ADDS/SUBS, C6.2.10, C6.2.11, C6.2.464, C6.2.465
2301 orr.x x0 x0 x10
2302 bl BO_W3
2303 movz.x x0 6 0
2304 movz.x x1 1 0
2305 bl TRAPC
2306 b BO_RET
2307:BA3
2308 bl BO_W3
2309 bl BO_FIX
2310 b BO_RET
2311; mul. i64: SMULH t (C6.2.379), MUL, CMP t, rd, ASR #63 (C6.2.97), B.NE trap 1
2312:BO_MUL
2313 ldr.x x9 sp 6
2314 cmp.x.i x9 9
2315 b.eq BM64
2316 movz.x x0 0x7c00 0
2317 movk.x x0 0x9b00 1 ; MUL 64-bit = MADD Xd, Xn, Xm, XZR, C6.2.293 / C6.2.275
2318 cmp.x.i x9 4
2319 b.ne BM1
2320 movz.x x10 0x8000 1
2321 eor.x x0 x0 x10
2322 bl BO_W3
2323 b BO_RET
2324:BM1
2325 bl BO_W3
2326 bl BO_FIX
2327 b BO_RET
2328:BM64
2329 bl ALLOC
2330 str.x x0 sp 8
2331 movz.x x0 0x7c00 0
2332 movk.x x0 0x9b40 1 ; SMULH, C6.2.379
2333 ldr.x x1 sp 8
2334 ldr.x x2 sp 4
2335 ldr.x x3 sp 5
2336 bl E3
2337 movz.x x0 0x7c00 0
2338 movk.x x0 0x9b00 1 ; MUL, C6.2.293
2339 bl BO_W3
2340 movz.x x0 0xfc1f 0
2341 movk.x x0 0xeb80 1 ; CMP Xt, Xd, ASR #63 = SUBS XZR, Xt, Xd, ASR #63, C6.2.465
2342 movz.x x1 0 0
2343 ldr.x x2 sp 8
2344 ldr.x x3 sp 2
2345 bl E3
2346 movz.x x0 1 0
2347 movz.x x1 1 0
2348 bl TRAPC
2349 b BO_RET
2350; div, rem (unsigned): CBZ Xb -> trap 2 (C6.2.78), unless b is a number other than 0; UDIV (C6.2.490);
2351; rem: t = a / b; rd = a - t * b (MSUB Xd, Xt, Xb, Xa, C6.2.291)
2352:BO_DIV
2353 ldr.x x9 sp 6
2354 cmp.x.i x9 6
2355 b.hs BO_SDIV
2356 ldr.x x10 sp 3
2357 ldr.x x0 x10 3
2358 cmp.x.i x0 3
2359 b.ne BD1
2360 ldr.x x0 x10 4
2361 cbnz.x x0 BD2
2362:BD1
2363 ldr.x x0 sp 5
2364 movz.x x9 0xb400 1 ; CBZ Xb, C6.2.78
2365 orr.x x0 x0 x9
2366 movz.x x1 2 0
2367 bl TRAPSITE
2368:BD2
2369 ldr.x x9 sp 1
2370 cmp.x.i x9 37
2371 b.eq BD_REM
2372 movz.x x0 0x0800 0
2373 movk.x x0 0x9ac0 1 ; UDIV 64-bit, C6.2.490
2374 bl BO_W3
2375 b BO_RET
2376:BD_REM
2377 bl ALLOC
2378 str.x x0 sp 8
2379 movz.x x0 0x0800 0
2380 movk.x x0 0x9ac0 1 ; UDIV, C6.2.490
2381 ldr.x x1 sp 8
2382 ldr.x x2 sp 4
2383 ldr.x x3 sp 5
2384 bl E3
2385 movz.x x0 0x8000 0
2386 movk.x x0 0x9b00 1 ; MSUB 64-bit, C6.2.291 (Ra at bit 10)
2387 ldr.x x1 sp 4
2388 ubfm.x x1 x1 54 53
2389 orr.x x0 x0 x1
2390 ldr.x x1 sp 2
2391 ldr.x x2 sp 8
2392 ldr.x x3 sp 5
2393 bl E3
2394 b BO_RET
2395; and, or, xor: canonical in, canonical out
2396:BO_LOG
2397 movz.x x0 0x8a00 1 ; AND (shifted register) 64-bit, C6.2.15
2398 ldr.x x9 sp 1
2399 cmp.x.i x9 39
2400 b.lo BL1
2401 movz.x x0 0xaa00 1 ; ORR (shifted register) 64-bit, C6.2.302
2402 b.eq BL1
2403 movz.x x0 0xca00 1 ; EOR (shifted register) 64-bit, C6.2.156
2404:BL1
2405 bl BO_W3
2406 b BO_RET
2407; shl (unsigned): by a number s: LSL #s = UBFM Xd, Xn, #(64-s) mod 64, #(63-s) (C6.2.487), then wrap;
2408; by a register: CMP Xb, #w; B.HS trap 7; LSLV (C6.2.271; 32-bit for u32)
2409:BO_SHL
2410 ldr.x x9 sp 6
2411 cmp.x.i x9 9
2412 b.eq BS_64
2413 ldr.x x10 sp 7
2414 cbnz.x x10 BS_IL
2415 bl BO_SCHK
2416 movz.x x0 0x2000 0
2417 movk.x x0 0x9ac0 1 ; LSLV 64-bit, C6.2.271
2418 ldr.x x9 sp 6
2419 cmp.x.i x9 4
2420 b.ne BS1
2421 movz.x x10 0x8000 1
2422 eor.x x0 x0 x10
2423 bl BO_W3
2424 b BO_RET
2425:BS1
2426 bl BO_W3
2427 bl BO_FIX
2428 b BO_RET
2429:BS_IL
2430 ldr.x x1 sp 5
2431 movz.x x2 64 0
2432 sub.x x2 x2 x1
2433 ubfm.x x2 x2 0 5
2434 movz.x x3 63 0
2435 sub.x x3 x3 x1
2436 movz.x x0 0xd340 1 ; UBFM 64-bit, C6.2.487
2437 ubfm.x x2 x2 48 47
2438 orr.x x0 x0 x2
2439 ubfm.x x3 x3 54 53
2440 orr.x x0 x0 x3
2441 ldr.x x1 sp 4
2442 ubfm.x x1 x1 59 58
2443 orr.x x0 x0 x1
2444 ldr.x x1 sp 2
2445 orr.x x0 x0 x1
2446 bl EMIT
2447 bl BO_FIX
2448 b BO_RET
2449; shr: unsigned LSR, signed ASR. By a number s: UBFM/SBFM Xd, Xn, #s, #63 (C6.2.487, C6.2.355);
2450; by a register: the amount check, then LSRV (C6.2.274) / ASRV (C6.2.21)
2451:BO_SHR
2452 ldr.x x10 sp 7
2453 cbnz.x x10 BR_I
2454 bl BO_SCHK
2455 movz.x x0 0x2400 0
2456 movk.x x0 0x9ac0 1 ; LSRV 64-bit, C6.2.274
2457 ldr.x x9 sp 6
2458 cmp.x.i x9 6
2459 b.lo BR1
2460 movz.x x0 0x2800 0
2461 movk.x x0 0x9ac0 1 ; ASRV 64-bit, C6.2.21
2462:BR1
2463 bl BO_W3
2464 b BO_RET
2465:BR_I
2466 movz.x x0 0xfc00 0
2467 movk.x x0 0xd340 1 ; UBFM Xd, Xn, #s, #63 = LSR, C6.2.487
2468 ldr.x x9 sp 6
2469 cmp.x.i x9 6
2470 b.lo BR2
2471 movz.x x0 0xfc00 0
2472 movk.x x0 0x9340 1 ; SBFM Xd, Xn, #s, #63 = ASR, C6.2.355
2473:BR2
2474 ldr.x x1 sp 5
2475 ubfm.x x1 x1 48 47
2476 orr.x x0 x0 x1
2477 ldr.x x1 sp 4
2478 ubfm.x x1 x1 59 58
2479 orr.x x0 x0 x1
2480 ldr.x x1 sp 2
2481 orr.x x0 x0 x1
2482 bl EMIT
2483 b BO_RET
2484; comparisons: CMP Xa, #imm (C6.2.96) or CMP Xa, Xb (C6.2.97); the result is the flags and a condition
2485:BO_CMP
2486 ldr.x x10 sp 7
2487 cbnz.x x10 BC_I
2488 movz.x x0 0x001f 0
2489 movk.x x0 0xeb00 1 ; CMP (shifted register) = SUBS XZR, Xa, Xb, C6.2.97
2490 ldr.x x1 sp 5
2491 ubfm.x x1 x1 48 47
2492 orr.x x0 x0 x1
2493 b BC_E
2494:BC_I
2495 movz.x x0 0x001f 0
2496 movk.x x0 0xf100 1 ; CMP (immediate) = SUBS XZR, Xa, #imm, C6.2.96
2497 ldr.x x1 sp 5
2498 ubfm.x x1 x1 54 53
2499 orr.x x0 x0 x1
2500:BC_E
2501 ldr.x x1 sp 4
2502 ubfm.x x1 x1 59 58
2503 orr.x x0 x0 x1
2504 bl EMIT
2505 ldr.x x9 sp 1
2506 sub.x.i x9 x9 43
2507 add.x x9 x9 x9
2508 ldr.x x10 sp 6
2509 cmp.x.i x10 6
2510 b.lo BC1
2511 add.x.i x9 x9 1
2512:BC1
2513 adr x10 CONDS
2514 ldrb.r w1 x10 x9
2515 movz.x x0 4 0
2516 movz.x x2 1 0
2517 b BO_RET2
2518:BO_RET
2519 movz.x x0 1 0
2520 ldr.x x1 sp 2
2521 ldr.x x2 sp 6
2522:BO_RET2
2523 ldr.x.post x30 sp 80
2524 ret
2525; BO_W3: x0 fixed part; emit it | rd | ra << 5 | (imm12 << 10, or rb << 16), fields from the BINOP frame
2526:BO_W3
2527 ldr.x x1 sp 2
2528 orr.x x0 x0 x1
2529 ldr.x x1 sp 4
2530 ubfm.x x1 x1 59 58
2531 orr.x x0 x0 x1
2532 ldr.x x1 sp 5
2533 ldr.x x2 sp 7
2534 cbnz.x x2 BW_I
2535 ubfm.x x1 x1 48 47
2536 orr.x x0 x0 x1
2537 b EMIT
2538:BW_I
2539 ubfm.x x1 x1 54 53
2540 orr.x x0 x0 x1
2541 b EMIT
2542; BO_FIX: after a 64-bit op on rd: u8/u16/u32 wrap (CANON), i8/i16/i32 overflow check (trap 1)
2543:BO_FIX
2544 ldr.x x1 sp 6
2545 ldr.x x0 sp 2
2546 cmp.x.i x1 6
2547 b.lo CANON
2548 cmp.x.i x1 9
2549 b.eq BF_X
2550 movz.x x2 1 0
2551 b CHKEXT
2552:BF_X
2553 ret
2554; BO_SCHK: shift amount in a register must be below the width: CMP Xb, #w (C6.2.96); B.HS trap 7
2555:BO_SCHK
2556 ldr.x x1 sp 6
2557 adr x9 TWID
2558 ldrb.r w1 x9 x1
2559 ldr.x x2 sp 5
2560 movz.x x0 0x001f 0
2561 movk.x x0 0xf100 1 ; CMP Xb, #w, C6.2.96
2562 ubfm.x x1 x1 54 53
2563 orr.x x0 x0 x1
2564 ubfm.x x2 x2 59 58
2565 orr.x x0 x0 x2
2566 str.x.pre x30 sp -16
2567 bl EMIT
2568 movz.x x0 2 0
2569 movz.x x1 7 0
2570 bl TRAPC
2571 ldr.x.post x30 sp 16
2572 ret
2573; ======================================================================================================
2574; COND: read a condition (a bool value); emit a branch taken when it is false; x0 = that branch's address
2575:COND
2576 str.x.pre x30 sp -16
2577 movz.x x0 1 0
2578 movz.x x1 255 0
2579 bl VAL
2580 cmp.x.i x2 1
2581 b.ne E_TYPE
2582 cmp.x.i x0 4
2583 b.ne CO1
2584 movz.x x9 1 0
2585 eor.x x1 x1 x9
2586 movz.x x0 0x5400 1 ; B.cond with the opposite condition, C6.2.34
2587 orr.x x0 x0 x1
2588 b CO2
2589:CO1
2590 bl MATREG
2591 movz.x x9 0xb400 1 ; CBZ Xr, C6.2.78
2592 orr.x x0 x0 x9
2593:CO2
2594 mov.x x10 x20
2595 bl EMIT
2596 mov.x x0 x10
2597 ldr.x.post x30 sp 16
2598 ret
2599; MATREG: x0 kind, x1 number/register/condition, x2 type -> x0 a register holding the value
2600:MATREG
2601 cmp.x.i x0 1
2602 b.ne MR1
2603 mov.x x0 x1
2604 ret
2605:MR1
2606 str.x.pre x30 sp -32
2607 str.x x0 sp 1
2608 str.x x1 sp 2
2609 str.x x2 sp 3
2610 bl ALLOC
2611 mov.x x3 x0
2612 ldr.x x0 sp 1
2613 ldr.x x1 sp 2
2614 ldr.x x2 sp 3
2615 str.x x3 sp 1
2616 bl FORCE
2617 ldr.x x0 sp 1
2618 ldr.x.post x30 sp 32
2619 ret
2620; FORCE: x0 kind, x1 number/register/condition, x2 type, x3 register: put the value in that register
2621:FORCE
2622 str.x.pre x30 sp -16
2623 cmp.x.i x0 1
2624 b.eq FO_R
2625 cmp.x.i x0 3
2626 b.eq FO_C
2627 cmp.x.i x0 4
2628 b.ne E_BARE
2629 mov.x x0 x3
2630 bl CSET
2631 b FO_X
2632:FO_R
2633 cmp.x x1 x3
2634 b.eq FO_X
2635 movz.x x0 0x03e0 0
2636 movk.x x0 0xaa00 1 ; MOV Xd, Xm = ORR Xd, XZR, Xm, C6.2.282
2637 orr.x x0 x0 x3
2638 ubfm.x x1 x1 48 47
2639 orr.x x0 x0 x1
2640 bl EMIT
2641 b FO_X
2642:FO_C
2643 mov.x x0 x3
2644 bl MATCONST
2645:FO_X
2646 ldr.x.post x30 sp 16
2647 ret
2648; CSET Xd, cond = CSINC Xd, XZR, XZR, opposite cond (C6.2.141). x0 register, x1 condition
2649:CSET
2650 movz.x x9 0x07e0 0
2651 movk.x x9 0x9a9f 1 ; CSINC 64-bit, Rm = Rn = 31, C6.2.141
2652 orr.x x9 x9 x0
2653 movz.x x0 1 0
2654 eor.x x1 x1 x0
2655 ubfm.x x1 x1 52 51
2656 orr.x x0 x9 x1
2657 b EMIT
2658; MATCONST: x0 register, x1 64-bit value: MOVZ (C6.2.285) or, for a negative value, MOVN (C6.2.284) with
2659; the first halfword that differs from the fill, then MOVK (C6.2.283) for each other such halfword
2660:MATCONST
2661 str.x.pre x30 sp -16
2662 mov.x x3 x0
2663 mov.x x4 x1
2664 movz.x x5 0 0
2665 movz.x x6 0 0
2666 movz.x x7 0xd280 1 ; MOVZ 64-bit, C6.2.285
2667 movz.x x8 0 0
2668 cmp.x.i x4 0
2669 b.ge MC2
2670 movz.x x6 0xffff 0
2671 movz.x x7 0x9280 1 ; MOVN 64-bit, C6.2.284
2672:MC2
2673 ubfm.x x2 x5 60 59
2674 lsrv.x x1 x4 x2
2675 ubfm.x x1 x1 0 15
2676 cmp.x x1 x6
2677 b.eq MC4
2678 movz.x x0 0xf280 1 ; MOVK 64-bit, C6.2.283
2679 cbnz.x x8 MC3
2680 eor.x x1 x1 x6
2681 mov.x x0 x7
2682 movz.x x8 1 0
2683:MC3
2684 ubfm.x x1 x1 59 58
2685 orr.x x0 x0 x1
2686 ubfm.x x2 x5 43 42
2687 orr.x x0 x0 x2
2688 orr.x x0 x0 x3
2689 bl EMIT
2690:MC4
2691 add.x.i x5 x5 1
2692 cmp.x.i x5 4
2693 b.lo MC2
2694 cbnz.x x8 MC5
2695 orr.x x0 x7 x3
2696 bl EMIT
2697:MC5
2698 ldr.x.post x30 sp 16
2699 ret
2700; CANON: x0 register, x1 type: u8/u16/u32 UBFM Xr, Xr, #0, #w-1 (C6.2.487); i8/i16/i32 SBFM (C6.2.355)
2701:CANON
2702 cmp.x.i x1 2
2703 b.lo CN_X
2704 cmp.x.i x1 5
2705 b.lo CN_U
2706 cmp.x.i x1 6
2707 b.lo CN_X
2708 cmp.x.i x1 9
2709 b.lo CN_S
2710:CN_X
2711 ret
2712:CN_U
2713 movz.x x9 0xd340 1 ; UBFM 64-bit, C6.2.487
2714 b CN_GO
2715:CN_S
2716 movz.x x9 0x9340 1 ; SBFM 64-bit, C6.2.355
2717:CN_GO
2718 adr x10 TWID
2719 ldrb.r w10 x10 x1
2720 sub.x.i x10 x10 1
2721 ubfm.x x10 x10 54 53
2722 orr.x x9 x9 x10
2723 orr.x x9 x9 x0
2724 ubfm.x x0 x0 59 58
2725 orr.x x0 x9 x0
2726 b EMIT
2727; CHKEXT: x0 register, x1 type (i8 i16 i32), x2 trap code: CMP Xr, Wr, SXTB/SXTH/SXTW (SUBS extended,
2728; option 100/101/110, C6.2.463), then B.NE trap
2729:CHKEXT
2730 sub.x.i x3 x1 2
2731 ubfm.x x3 x3 51 50
2732 movz.x x9 0x001f 0
2733 movk.x x9 0xeb20 1 ; SUBS (extended register) 64-bit, Rd 31 = CMP, C6.2.463
2734 orr.x x3 x3 x9
2735 ubfm.x x9 x0 48 47
2736 orr.x x3 x3 x9
2737 ubfm.x x9 x0 59 58
2738 orr.x x0 x3 x9
2739 str.x.pre x30 sp -16
2740 bl EMIT
2741 movz.x x0 1 0
2742 mov.x x1 x2
2743 bl TRAPC
2744 ldr.x.post x30 sp 16
2745 ret
2746; RANGE: x1 number, x2 type, x3 1 when the number was written as 2^63 or more (TK_NUM, state slot 11):
2747; error 8 unless the number is a value of that type
2748:RANGE
2749 adr x9 TWID
2750 ldrb.r w9 x9 x2
2751 cmp.x.i x9 64
2752 b.eq RG_64
2753 cbnz.x x3 E_RANGE
2754 movz.x x10 1 0
2755 lslv.x x10 x10 x9
2756 cmp.x.i x2 6
2757 b.hs RG_S
2758 cmp.x x1 x10
2759 b.hs E_RANGE
2760 ret
2761:RG_S
2762 ubfm.x x10 x10 1 63
2763 add.x x11 x1 x10
2764 add.x x10 x10 x10
2765 cmp.x x11 x10
2766 b.hs E_RANGE
2767 ret
2768:RG_64
2769 cmp.x.i x2 9
2770 b.eq RG_L
2771 cbnz.x x3 RG_OK
2772 cmp.x.i x1 0
2773 b.lt E_RANGE
2774:RG_OK
2775 ret
2776:RG_L
2777 cbnz.x x3 E_RANGE
2778 ret
2779; ======================================================================================================
2780; TOK: next word. x0 kind (0 end of line, 1 word, 2 number, 3 end of input), x1 packed word (first 8
2781; characters, first in the low byte) or number, x2 length. Clobbers x0..x7.
2782:TOK
2783 cmp.x x19 x18
2784 b.hs TK_EOF
2785 ldrb w0 x19 0
2786 cmp.w.i w0 32
2787 b.eq TK_SKIP
2788 cmp.w.i w0 9
2789 b.eq TK_SKIP
2790 cmp.w.i w0 13
2791 b.eq TK_SKIP
2792 cmp.w.i w0 10
2793 b.eq TK_NL
2794 cmp.w.i w0 34
2795 b.eq TK_TEXT
2796 cmp.w.i w0 32
2797 b.lo E_SYNTAX
2798 b TK_WORD
2799; a text literal: kind 4, x1 its first byte, x2 its length in the source (escapes not yet read); it ends
2800; at the next " that is not after a \, on the same line, and is followed by a blank, a line end or the end
2801:TK_TEXT
2802 add.x.i x19 x19 1
2803 mov.x x1 x19
2804:TT1
2805 cmp.x x19 x18
2806 b.hs E_TEXT
2807 ldrb w0 x19 0
2808 cmp.w.i w0 10
2809 b.eq E_TEXT
2810 cmp.w.i w0 34
2811 b.eq TT_END
2812 add.x.i x19 x19 1
2813 cmp.w.i w0 92
2814 b.ne TT1
2815 cmp.x x19 x18
2816 b.hs E_TEXT
2817 ldrb w0 x19 0
2818 cmp.w.i w0 10
2819 b.eq E_TEXT
2820 add.x.i x19 x19 1
2821 b TT1
2822:TT_END
2823 sub.x x2 x19 x1
2824 add.x.i x19 x19 1
2825 movz.x x0 4 0
2826 cmp.x x19 x18
2827 b.hs TT_OK
2828 ldrb w3 x19 0
2829 cmp.w.i w3 32
2830 b.hi E_TEXT
2831:TT_OK
2832 ret
2833:TK_NL
2834 add.x.i x19 x19 1
2835 add.x.i x22 x22 1
2836 movz.x x0 0 0
2837 ret
2838:TK_SKIP
2839 add.x.i x19 x19 1
2840 b TOK
2841:TK_EOF
2842 movz.x x0 3 0
2843 ret
2844:TK_WORD
2845 movz.x x1 0 0
2846 movz.x x2 0 0
2847 movz.x x3 0 0
2848:TK_CH
2849 cmp.x x19 x18
2850 b.hs TK_END
2851 ldrb w0 x19 0
2852 cmp.w.i w0 32
2853 b.ls TK_END
2854 cmp.x.i x2 8
2855 b.hs TK_LONG
2856 lslv.x x4 x0 x3
2857 orr.x x1 x1 x4
2858 add.x.i x3 x3 8
2859:TK_LONG
2860 add.x.i x2 x2 1
2861 add.x.i x19 x19 1
2862 b TK_CH
2863:TK_END
2864 ubfm.x x4 x1 0 7
2865 cmp.x.i x4 45
2866 b.eq TK_NUM
2867 sub.x.i x4 x4 48
2868 cmp.x.i x4 9
2869 b.ls TK_NUM
2870 movz.x x0 1 0
2871 ret
2872; a number: [-]decimal, at most 20 digits, a value of some whole-number type: -2^63 .. 2^64 - 1, error 8
2873; outside. State slot 11 = 1 when it is 2^63 or more (only ulong holds such a number), else 0.
2874:TK_NUM
2875 sub.x x5 x19 x2
2876 movz.x x6 0 0
2877 ldrb w0 x5 0
2878 cmp.w.i w0 45
2879 b.ne TK_N1
2880 movz.x x6 1 0
2881 add.x.i x5 x5 1
2882:TK_N1
2883 sub.x x0 x19 x5
2884 cbz.x x0 E_NUM
2885 cmp.x.i x0 20
2886 b.hi E_NUM
2887 movz.x x1 0 0
2888 movz.x x7 10 0
2889 movz.x x3 0x9999 0 ; x3 = 0x1999999999999999 = (2^64 - 1) / 10 rounded down: x1 * 10 + digit fits
2890 movk.x x3 0x9999 1 ; unless x1 > x3, or x1 = x3 and digit > 5
2891 movk.x x3 0x9999 2
2892 movk.x x3 0x1999 3
2893:TK_N2
2894 cmp.x x5 x19
2895 b.hs TK_N3
2896 ldrb w0 x5 0
2897 sub.x.i x0 x0 48
2898 cmp.x.i x0 9
2899 b.hi E_NUM
2900 cmp.x x1 x3
2901 b.hi E_RANGE
2902 b.lo TK_N2A
2903 cmp.x.i x0 5
2904 b.hi E_RANGE
2905:TK_N2A
2906 mul.x x1 x1 x7
2907 add.x x1 x1 x0
2908 add.x.i x5 x5 1
2909 b TK_N2
2910:TK_N3
2911 movz.x x7 0x8000 3
2912 movz.x x4 0x185 1
2913 str.x xzr x4 11
2914 cbnz.x x6 TK_NEG
2915 cmp.x x1 x7
2916 b.lo TK_N4
2917 movz.x x3 1 0
2918 str.x x3 x4 11
2919:TK_N4
2920 movz.x x0 2 0
2921 ret
2922:TK_NEG
2923 cmp.x x1 x7
2924 b.hi E_RANGE
2925 sub.x x1 xzr x1
2926 movz.x x0 2 0
2927 ret
2928; KW: x1 packed word -> x0 keyword code (0 none). Clobbers x9..x11.
2929:KW
2930 adr x9 KWTAB
2931:KW1
2932 ldr.w w0 x9 2
2933 cbz.w w0 KW2
2934 ldr.x x10 x9 0
2935 cmp.x x10 x1
2936 b.eq KW2
2937 add.x.i x9 x9 16
2938 b KW1
2939:KW2
2940 ret
2941; TYPEWORD: read a type name -> x0 type 1..9
2942:TYPEWORD
2943 str.x.pre x30 sp -16
2944 bl TOK
2945 cbz.x x0 E_FEW
2946 cmp.x.i x0 1
2947 b.ne E_SYNTAX
2948 cmp.x.i x2 8
2949 b.hi E_SYNTAX
2950 bl KW
2951 cmp.x.i x0 76
2952 b.eq E_SUBSET
2953 sub.x.i x9 x0 1
2954 cmp.x.i x9 8
2955 b.hi E_SYNTAX
2956 ldr.x.post x30 sp 16
2957 ret
2958; EOL: the line must end here
2959:EOL
2960 str.x.pre x30 sp -16
2961 bl TOK
2962 cbz.x x0 EOL1
2963 cmp.x.i x0 3
2964 b.ne E_EXTRA
2965:EOL1
2966 ldr.x.post x30 sp 16
2967 ret
2968; ALLOC: -> x0 the next free register of the pool. Clobbers x9.
2969:ALLOC
2970 add.x x0 x29 x24
2971 adr x9 POOLEND
2972 cmp.x x0 x9
2973 b.hs E_REGS
2974 ldrb w0 x0 0
2975 add.x.i x24 x24 1
2976 ret
2977; DESTREG: x0 destination register, or 255 for a new temporary
2978:DESTREG
2979 cmp.x.i x0 255
2980 b.eq ALLOC
2981 ret
2982; EMIT: x0 word -> output. Clobbers x9.
2983:EMIT
2984 movz.x x9 0x180 1
2985 cmp.x x20 x9
2986 b.hs E_FULL
2987 str.w.post w0 x20 4
2988 ret
2989; E3: emit x0 | x1 | x2 << 5 | x3 << 16. Clobbers x0..x3, x9.
2990:E3
2991 orr.x x0 x0 x1
2992 ubfm.x x2 x2 59 58
2993 orr.x x0 x0 x2
2994 ubfm.x x3 x3 48 47
2995 orr.x x0 x0 x3
2996 b EMIT
2997; TRAPC: x0 condition, x1 trap code: B.cond (C6.2.34) to the trap stub, distance filled at the end
2998:TRAPC
2999 movz.x x9 0x5400 1
3000 orr.x x0 x0 x9
3001; TRAPSITE: x0 branch word, x1 trap code: note the place, emit the word
3002:TRAPSITE
3003 movz.x x9 0x183 1
3004 cmp.x x26 x9
3005 b.hs E_FULL
3006 str.w w20 x26 0
3007 str.w w1 x26 1
3008 add.x.i x26 x26 8
3009 b EMIT
3010; PATCH19: x0 address of a B.cond/CBZ/CBNZ, x1 target: the word distance into bits 5..23
3011:PATCH19
3012 sub.x x2 x1 x0
3013 ubfm.x x2 x2 2 20
3014 ubfm.x x2 x2 59 58
3015 ldr.w w1 x0 0
3016 orr.x x1 x1 x2
3017 str.w w1 x0 0
3018 ret
3019; PATCH26: x0 address of a B, x1 target: the word distance into bits 0..25
3020:PATCH26
3021 sub.x x2 x1 x0
3022 ubfm.x x2 x2 2 27
3023 ldr.w w1 x0 0
3024 orr.x x1 x1 x2
3025 str.w w1 x0 0
3026 ret
3027; BACK: x0 earlier address: emit B to it (C6.2.35)
3028:BACK
3029 sub.x x2 x0 x20
3030 ubfm.x x2 x2 2 27
3031 movz.x x0 0x1400 1 ; B, C6.2.35
3032 orr.x x0 x0 x2
3033 b EMIT
3034; ======================================================================================================
3035; errors
3036:E_READ
3037 movz.x x0 1 0
3038 b FAIL
3039:E_WRITE
3040 movz.x x0 2 0
3041 b FAIL
3042:E_UNDEF
3043 movz.x x0 3 0
3044 b FAIL
3045:E_FEW
3046 movz.x x0 4 0
3047 b FAIL
3048:E_EXTRA
3049 movz.x x0 5 0
3050 b FAIL
3051:E_NUM
3052 movz.x x0 6 0
3053 b FAIL
3054:E_TYPE
3055 movz.x x0 7 0
3056 b FAIL
3057:E_RANGE
3058 movz.x x0 8 0
3059 b FAIL
3060:E_SUBSET
3061 movz.x x0 9 0
3062 b FAIL
3063:E_TWICE
3064 movz.x x0 10 0
3065 b FAIL
3066:E_BARE
3067 movz.x x0 11 0
3068 b FAIL
3069:E_REGS
3070 movz.x x0 12 0
3071 b FAIL
3072:E_LONG
3073 movz.x x0 13 0
3074 b FAIL
3075:E_FULL
3076 movz.x x0 14 0
3077 b FAIL
3078:E_BLOCK
3079 movz.x x0 15 0
3080 b FAIL
3081:E_SYNTAX
3082 movz.x x0 16 0
3083 b FAIL
3084; PUTDEC: x0 number: its decimal digits at x9 (x9 moves on)
3085:PUTDEC
3086 movz.x x1 10 0
3087 add.x.i x2 x9 40
3088 mov.x x3 x2
3089:PD1
3090 udiv.x x4 x0 x1
3091 msub.x x5 x4 x1 x0
3092 add.x.i x5 x5 48
3093 sub.x.i x3 x3 1
3094 strb w5 x3 0
3095 mov.x x0 x4
3096 cbnz.x x0 PD1
3097:PD2
3098 ldrb w5 x3 0
3099 strb w5 x9 0
3100 add.x.i x9 x9 1
3101 add.x.i x3 x3 1
3102 cmp.x x3 x2
3103 b.lo PD2
3104 ret
3105; div, rem (signed): CBZ Xb -> trap 2 unless b is a number other than 0; SDIV (C6.2.357); i64 div also
3106; traps 1 for MIN / -1: CMN Xb, #1 (ADDS immediate, C6.2.10); B.NE over the next two (C6.2.34);
3107; NEGS XZR, Xa (SUBS shifted register, C6.2.465) sets V only for MIN; B.VS trap 1. i8/i16/i32: the
3108; quotient is exact in 64 bits, then the sign-extension check (trap 1). rem: t = a / b; rd = a - t * b
3109; (MSUB, C6.2.291), always in range (MIN rem -1 = 0).
3110:BO_SDIV
3111 ldr.x x10 sp 3
3112 ldr.x x0 x10 3
3113 cmp.x.i x0 3
3114 b.ne SD1
3115 ldr.x x0 x10 4
3116 cbnz.x x0 SD2
3117:SD1
3118 ldr.x x0 sp 5
3119 movz.x x9 0xb400 1 ; CBZ Xb, C6.2.78
3120 orr.x x0 x0 x9
3121 movz.x x1 2 0
3122 bl TRAPSITE
3123:SD2
3124 ldr.x x9 sp 1
3125 cmp.x.i x9 37
3126 b.eq SD_REM
3127 ldr.x x9 sp 6
3128 cmp.x.i x9 9
3129 b.ne SD4
3130 ldr.x x10 sp 3
3131 ldr.x x0 x10 3
3132 cmp.x.i x0 3
3133 b.ne SD3
3134 ldr.x x0 x10 4
3135 cmn.x.i x0 1
3136 b.ne SD4
3137:SD3
3138 movz.x x0 0x041f 0
3139 movk.x x0 0xb100 1 ; CMN Xb, #1 = ADDS XZR, Xb, #1, C6.2.10
3140 ldr.x x1 sp 5
3141 ubfm.x x1 x1 59 58
3142 orr.x x0 x0 x1
3143 bl EMIT
3144 movz.x x0 0x0061 0
3145 movk.x x0 0x5400 1 ; B.NE +3 words, C6.2.34
3146 bl EMIT
3147 movz.x x0 0x03ff 0
3148 movk.x x0 0xeb00 1 ; NEGS XZR, Xa = SUBS XZR, XZR, Xa, C6.2.465
3149 ldr.x x1 sp 4
3150 ubfm.x x1 x1 48 47
3151 orr.x x0 x0 x1
3152 bl EMIT
3153 movz.x x0 6 0
3154 movz.x x1 1 0
3155 bl TRAPC
3156:SD4
3157 movz.x x0 0x0c00 0
3158 movk.x x0 0x9ac0 1 ; SDIV 64-bit, C6.2.357
3159 bl BO_W3
3160 ldr.x x9 sp 6
3161 cmp.x.i x9 9
3162 b.eq BO_RET
3163 bl BO_FIX
3164 b BO_RET
3165:SD_REM
3166 bl ALLOC
3167 str.x x0 sp 8
3168 movz.x x0 0x0c00 0
3169 movk.x x0 0x9ac0 1 ; SDIV, C6.2.357
3170 ldr.x x1 sp 8
3171 ldr.x x2 sp 4
3172 ldr.x x3 sp 5
3173 bl E3
3174 movz.x x0 0x8000 0
3175 movk.x x0 0x9b00 1 ; MSUB 64-bit, C6.2.291 (Ra at bit 10)
3176 ldr.x x1 sp 4
3177 ubfm.x x1 x1 54 53
3178 orr.x x0 x0 x1
3179 ldr.x x1 sp 2
3180 ldr.x x2 sp 8
3181 ldr.x x3 sp 5
3182 bl E3
3183 b BO_RET
3184; shl (i64): t = a << s (LSLV C6.2.271, or UBFM C6.2.487 for a number); X16 = t >> s arithmetic (ASRV
3185; C6.2.21, or SBFM C6.2.355); CMP X16, Xa (C6.2.97); B.NE trap 1 (a bit left the value); MOV Xd, Xt.
3186; (i8/i16/i32 shift in 64 bits exactly and take the sign-extension check of BO_FIX, as unsigned types
3187; take their wrap.)
3188:BS_64
3189 bl ALLOC
3190 str.x x0 sp 8
3191 ldr.x x10 sp 7
3192 cbnz.x x10 BS64_I
3193 bl BO_SCHK
3194 movz.x x0 0x2000 0
3195 movk.x x0 0x9ac0 1 ; LSLV Xt, Xa, Xb, C6.2.271
3196 ldr.x x1 sp 8
3197 ldr.x x2 sp 4
3198 ldr.x x3 sp 5
3199 bl E3
3200 movz.x x0 0x2810 0
3201 movk.x x0 0x9ac0 1 ; ASRV X16, Xt, Xb, C6.2.21
3202 movz.x x1 0 0
3203 ldr.x x2 sp 8
3204 ldr.x x3 sp 5
3205 bl E3
3206 b BS64_C
3207:BS64_I
3208 ldr.x x1 sp 5
3209 movz.x x2 64 0
3210 sub.x x2 x2 x1
3211 ubfm.x x2 x2 0 5
3212 movz.x x3 63 0
3213 sub.x x3 x3 x1
3214 movz.x x0 0xd340 1 ; UBFM Xt, Xa, #(64-s) mod 64, #63-s = LSL, C6.2.487
3215 ubfm.x x2 x2 48 47
3216 orr.x x0 x0 x2
3217 ubfm.x x3 x3 54 53
3218 orr.x x0 x0 x3
3219 ldr.x x1 sp 4
3220 ubfm.x x1 x1 59 58
3221 orr.x x0 x0 x1
3222 ldr.x x1 sp 8
3223 orr.x x0 x0 x1
3224 bl EMIT
3225 movz.x x0 0xfc10 0
3226 movk.x x0 0x9340 1 ; SBFM X16, Xt, #s, #63 = ASR, C6.2.355
3227 ldr.x x1 sp 5
3228 ubfm.x x1 x1 48 47
3229 orr.x x0 x0 x1
3230 ldr.x x1 sp 8
3231 ubfm.x x1 x1 59 58
3232 orr.x x0 x0 x1
3233 bl EMIT
3234:BS64_C
3235 movz.x x0 0x021f 0
3236 movk.x x0 0xeb00 1 ; CMP X16, Xa = SUBS XZR, X16, Xa, C6.2.97
3237 ldr.x x1 sp 4
3238 ubfm.x x1 x1 48 47
3239 orr.x x0 x0 x1
3240 bl EMIT
3241 movz.x x0 1 0
3242 movz.x x1 1 0
3243 bl TRAPC
3244 movz.x x0 0x03e0 0
3245 movk.x x0 0xaa00 1 ; MOV Xd, Xt = ORR Xd, XZR, Xt, C6.2.282
3246 ldr.x x1 sp 2
3247 orr.x x0 x0 x1
3248 ldr.x x1 sp 8
3249 ubfm.x x1 x1 48 47
3250 orr.x x0 x0 x1
3251 bl EMIT
3252 b BO_RET
3253; ======================================================================================================
3254; values new in om4 (inside VALTOK's frame: 0 x30, 1 expected, 2 dest, 3 op, 4.. free)
3255; true, false: bool numbers
3256:VT_TRUE
3257 movz.x x0 3 0
3258 movz.x x1 1 0
3259 movz.x x2 1 0
3260 b VT_RET
3261:VT_FALSE
3262 movz.x x0 3 0
3263 movz.x x1 0 0
3264 movz.x x2 1 0
3265 b VT_RET
3266; len A: the length of A, a ulong number
3267:VT_LEN
3268 bl ARRNAME
3269 ldr.w w1 x0 4
3270 movz.x x0 3 0
3271 movz.x x2 5 0
3272 b VT_RET
3273; get A i: the checked load (the load of the element type, register offset, LDOP table)
3274:VT_GET
3275 bl ARRNAME
3276 str.x x0 sp 4
3277 movz.x x0 5 0
3278 movz.x x1 255 0
3279 bl VAL
3280 mov.x x3 x2
3281 mov.x x2 x1
3282 mov.x x1 x0
3283 ldr.x x0 sp 4
3284 bl ELEMADDR
3285 str.x x0 sp 5
3286 str.x x1 sp 6
3287 ldr.x x0 sp 2
3288 bl DESTREG
3289 str.x x0 sp 7
3290 ldr.x x9 sp 4
3291 ldr.w w9 x9 3
3292 adr x10 LDOP
3293 ldr.w.r w0 x10 x9
3294 ldr.x x1 sp 7
3295 orr.x x0 x0 x1
3296 ldr.x x1 sp 5
3297 ubfm.x x1 x1 59 58
3298 orr.x x0 x0 x1
3299 ldr.x x1 sp 6
3300 ubfm.x x1 x1 48 47
3301 orr.x x0 x0 x1
3302 bl EMIT
3303 movz.x x0 1 0
3304 ldr.x x1 sp 7
3305 ldr.x x9 sp 4
3306 ldr.w w2 x9 3
3307 b VT_RET
3308; read fd A off n / write fd A off n: a long (bytes moved, or minus the Linux error number). fd int, off
3309; and n ulong; n <= len A and off <= len A - n, else trap 3 (CMP C6.2.97, B.HI C6.2.34, SUB C6.2.458).
3310; Then as a call: save the registers in use, X0 = fd, X1 = A + off (ADD through X16, C6.2.6), X2 = n,
3311; X8 = 63 (read) or 64 (write), SVC #0 (C6.2.466), the result from X0, the registers back.
3312; Frame: 4/5 fd, 6 A, 7/8 off, 9/10 n, 11 t, 12 saved count, 13 save bytes, 14 pool slot before.
3313:VT_SYS
3314 str.x x24 sp 14
3315 movz.x x0 8 0
3316 movz.x x1 255 0
3317 bl VAL
3318 cmp.x.i x2 8
3319 b.ne E_TYPE
3320 str.x x0 sp 4
3321 str.x x1 sp 5
3322 bl ARRNAME
3323 str.x x0 sp 6
3324 ldr.x x9 sp 3
3325 cmp.x.i x9 53
3326 b.ne VS1
3327 ldr.w w9 x0 5
3328 cmp.x.i x9 2
3329 b.eq E_RDONLY
3330 cmp.x.i x9 5
3331 b.eq E_RDONLY
3332:VS1
3333 movz.x x0 5 0
3334 movz.x x1 255 0
3335 bl VAL
3336 cmp.x.i x2 5
3337 b.ne E_TYPE
3338 bl MATREG
3339 str.x x0 sp 8
3340 movz.x x0 5 0
3341 movz.x x1 255 0
3342 bl VAL
3343 cmp.x.i x2 5
3344 b.ne E_TYPE
3345 bl MATREG
3346 str.x x0 sp 10
3347 bl ALLOC
3348 str.x x0 sp 11
3349 ldr.x x9 sp 6
3350 ldr.w w1 x9 4
3351 bl MATCONST
3352 movz.x x0 0x001f 0
3353 movk.x x0 0xeb00 1 ; CMP Xn, Xt, C6.2.97
3354 ldr.x x1 sp 10
3355 ubfm.x x1 x1 59 58
3356 orr.x x0 x0 x1
3357 ldr.x x1 sp 11
3358 ubfm.x x1 x1 48 47
3359 orr.x x0 x0 x1
3360 bl EMIT
3361 movz.x x0 8 0
3362 movz.x x1 3 0
3363 bl TRAPC
3364 movz.x x0 0 0
3365 movk.x x0 0xcb00 1 ; SUB Xt, Xt, Xn, C6.2.458
3366 ldr.x x1 sp 11
3367 ldr.x x2 sp 11
3368 ldr.x x3 sp 10
3369 bl E3
3370 movz.x x0 0x001f 0
3371 movk.x x0 0xeb00 1 ; CMP Xoff, Xt, C6.2.97
3372 ldr.x x1 sp 8
3373 ubfm.x x1 x1 59 58
3374 orr.x x0 x0 x1
3375 ldr.x x1 sp 11
3376 ubfm.x x1 x1 48 47
3377 orr.x x0 x0 x1
3378 bl EMIT
3379 movz.x x0 8 0
3380 movz.x x1 3 0
3381 bl TRAPC
3382 bl SAVE
3383 str.x x0 sp 12
3384 str.x x1 sp 13
3385 movz.x x0 0 0
3386 ldr.x x1 sp 4
3387 ldr.x x2 sp 5
3388 bl LOADARG
3389 movz.x x0 1 0
3390 ldr.x x9 sp 6
3391 bl BASEARG
3392 movz.x x0 16 0
3393 movz.x x1 1 0
3394 ldr.x x2 sp 8
3395 bl LOADARG
3396 movz.x x0 0x0021 0
3397 movk.x x0 0x8b10 1 ; ADD X1, X1, X16, C6.2.6
3398 bl EMIT
3399 movz.x x0 2 0
3400 movz.x x1 1 0
3401 ldr.x x2 sp 10
3402 bl LOADARG
3403 ldr.x x9 sp 3
3404 add.x.i x9 x9 10
3405 ubfm.x x9 x9 59 58
3406 movz.x x0 0x0008 0
3407 movk.x x0 0xd280 1 ; MOVZ X8, #63 or #64, C6.2.285
3408 orr.x x0 x0 x9
3409 bl EMIT
3410 movz.x x0 0x0001 0
3411 movk.x x0 0xd400 1 ; SVC #0, C6.2.466
3412 bl EMIT
3413 ldr.x x0 sp 2
3414 ldr.x x1 sp 14
3415 ldr.x x2 sp 12
3416 ldr.x x3 sp 13
3417 movz.x x4 1 0
3418 bl RESTORE
3419 mov.x x1 x0
3420 movz.x x0 1 0
3421 movz.x x2 9 0
3422 b VT_RET
3423; ======================================================================================================
3424; CALL: x0 function entry, x1 destination (255 none). Reads the arguments, then: SAVE; each argument into
3425; X0..X5 from the save area (LOADARG); BL (C6.2.43); RESTORE. Out: x0 1, x1 result register, x2 type.
3426; Frame: 0 x30, 1 entry, 2 dest, 3 pool slot before, 4 i, 5 parameter count, 6.. 3 per argument (kind,
3427; register/number, name entry of an array), 24 saved count, 25 save bytes.
3428:CALL
3429 sub.x.i sp sp 224
3430 str.x x30 sp 0
3431 str.x x0 sp 1
3432 str.x x1 sp 2
3433 str.x x24 sp 3
3434 str.x xzr sp 4
3435 ldr.w w9 x0 5
3436 str.x x9 sp 5
3437 movz.x x9 0x185 1
3438 ldr.x x10 x9 6
3439 cbnz.x x10 CA_ARG
3440 ldr.x x10 x9 3
3441 cmp.x x10 x0
3442 b.eq E_SUBSET
3443:CA_ARG
3444 ldr.x x9 sp 4
3445 ldr.x x10 sp 5
3446 cmp.x x9 x10
3447 b.hs CA_DONE
3448 ldr.x x11 sp 1
3449 ubfm.x x12 x9 60 59
3450 add.x x11 x11 x12
3451 ldr.w w12 x11 8
3452 cbnz.x x12 CA_ARR
3453 ldr.w w0 x11 6
3454 movz.x x1 255 0
3455 bl VAL
3456 cmp.x.i x0 4
3457 b.ne CA1
3458 bl MATREG
3459 mov.x x1 x0
3460 movz.x x0 1 0
3461 movz.x x2 1 0
3462:CA1
3463 ldr.x x9 sp 4
3464 ldr.x x11 sp 1
3465 ubfm.x x12 x9 60 59
3466 add.x x11 x11 x12
3467 ldr.w w12 x11 6
3468 cmp.x x2 x12
3469 b.ne E_TYPE
3470 movz.x x3 0 0
3471 b CA_PUT
3472:CA_ARR
3473 bl ARRARG
3474 mov.x x3 x2
3475 ldr.x x9 sp 4
3476 movz.x x10 0 0
3477:CA2
3478 cmp.x x10 x9
3479 b.hs CA_PUT
3480 movz.x x11 3 0
3481 mul.x x11 x11 x10
3482 add.x.i x11 x11 8
3483 ldr.x.r x12 sp x11
3484 cmp.x x12 x3
3485 b.eq E_ALIAS
3486 add.x.i x10 x10 1
3487 b CA2
3488:CA_PUT
3489 ldr.x x9 sp 4
3490 movz.x x11 3 0
3491 mul.x x11 x11 x9
3492 add.x.i x11 x11 6
3493 str.x.r x0 sp x11
3494 add.x.i x11 x11 1
3495 str.x.r x1 sp x11
3496 add.x.i x11 x11 1
3497 str.x.r x3 sp x11
3498 add.x.i x9 x9 1
3499 str.x x9 sp 4
3500 b CA_ARG
3501:CA_DONE
3502 bl SAVE
3503 str.x x0 sp 24
3504 str.x x1 sp 25
3505 movz.x x13 0 0
3506:CA3
3507 ldr.x x10 sp 5
3508 cmp.x x13 x10
3509 b.hs CA4
3510 movz.x x11 3 0
3511 mul.x x11 x11 x13
3512 add.x.i x11 x11 6
3513 ldr.x.r x1 sp x11
3514 add.x.i x11 x11 1
3515 ldr.x.r x2 sp x11
3516 mov.x x0 x13
3517 bl LOADARG
3518 add.x.i x13 x13 1
3519 b CA3
3520:CA4
3521 movz.x x9 0x185 1
3522 ldr.x x10 x9 12
3523 movz.x x11 1 1
3524 cmp.x x10 x11
3525 b.hs E_FULL
3526 movz.x x11 24 0
3527 mul.x x11 x11 x10
3528 movz.x x12 0x186 1
3529 add.x x11 x11 x12
3530 str.x x20 x11 0
3531 ldr.x x12 sp 1
3532 str.x x12 x11 1
3533 ldr.x x12 x9 3
3534 str.x x12 x11 2
3535 add.x.i x10 x10 1
3536 str.x x10 x9 12
3537 ldr.x x9 sp 1
3538
3539 ldr.x x0 x9 1
3540 sub.x x2 x0 x20
3541 ubfm.x x2 x2 2 27
3542 movz.x x0 0x9400 1 ; BL, C6.2.43
3543 orr.x x0 x0 x2
3544 bl EMIT
3545 ldr.x x9 sp 1
3546 ldr.w w9 x9 4
3547 movz.x x4 1 0
3548 cmp.x.i x9 10
3549 b.ne CA5
3550 movz.x x4 0 0
3551:CA5
3552 ldr.x x0 sp 2
3553 ldr.x x1 sp 3
3554 ldr.x x2 sp 24
3555 ldr.x x3 sp 25
3556 bl RESTORE
3557 mov.x x1 x0
3558 movz.x x0 1 0
3559 ldr.x x9 sp 1
3560 ldr.w w2 x9 4
3561 ldr.x x30 sp 0
3562 add.x.i sp sp 224
3563 ret
3564; ARRARG: an array argument for the parameter at x11 (type +24, length +28, kind +32): [view | edit] NAME.
3565; Its type and length must equal the parameter's; an edit parameter needs a writable array (local or edit,
3566; error 20); a global is never passed (error 19: only so is every pair of live references disjoint).
3567; Out: x0 5 (register x1) or 6 (address x1, a text), x2 the name entry.
3568:ARRARG
3569 str.x.pre x30 sp -32
3570 str.x x11 sp 1
3571 str.x xzr sp 2
3572 bl TOK
3573 cbz.x x0 E_FEW
3574 cmp.x.i x0 1
3575 b.ne E_SYNTAX
3576 cmp.x.i x2 8
3577 b.hi E_LONG
3578 str.x x1 sp 3
3579 bl KW
3580 cbz.x x0 AA1
3581 cmp.x.i x0 57
3582 b.lo E_SYNTAX
3583 cmp.x.i x0 58
3584 b.hi E_SYNTAX
3585 str.x x0 sp 2
3586 bl TOK
3587 cbz.x x0 E_FEW
3588 cmp.x.i x0 1
3589 b.ne E_SYNTAX
3590 cmp.x.i x2 8
3591 b.hi E_LONG
3592 str.x x1 sp 3
3593:AA1
3594 ldr.x x1 sp 3
3595 bl FINDNAME
3596 cbz.x x0 E_UNDEF
3597 ldr.w w9 x0 5
3598 cbz.x x9 E_TYPE
3599 cmp.x.i x9 4
3600 b.eq E_ALIAS
3601 ldr.x x11 sp 1
3602 ldr.w w10 x11 8
3603 cmp.x.i x10 3
3604 b.ne AA2
3605 ldr.x x12 sp 2
3606 cmp.x.i x12 57
3607 b.eq E_RDONLY ; view A to an edit parameter: passing a view as edit is error 20 (OMEGESE.md 9.4)
3608 cmp.x.i x9 1
3609 b.eq AA3
3610 cmp.x.i x9 3
3611 b.ne E_RDONLY
3612 b AA3
3613:AA2
3614 ldr.x x12 sp 2
3615 cmp.x.i x12 58
3616 b.eq E_TYPE
3617:AA3
3618 ldr.w w12 x0 3
3619 ldr.w w13 x11 6
3620 cmp.x x12 x13
3621 b.ne E_TYPE
3622 ldr.w w12 x0 4
3623 ldr.w w13 x11 7
3624 cmp.x x12 x13
3625 b.ne E_TYPE
3626 mov.x x2 x0
3627 cmp.x.i x9 5
3628 b.eq AA4
3629 ldr.w w1 x0 2
3630 movz.x x0 5 0
3631 b AA5
3632:AA4
3633 ldr.x x1 x0 3
3634 movz.x x0 6 0
3635:AA5
3636 ldr.x.post x30 sp 32
3637 ret
3638; SAVE: n = registers in use (parameters + pool slots), A = 8 (n + 1) rounded up to 16: SUB SP, SP, #A
3639; (C6.2.457); program mode: CMP SP, X28 (C6.2.463), B.LO trap 6; STR Xr, [SP, #8j] for each (C6.2.415,
3640; unsigned offset); STR X30, [SP, #8n]. Out: x0 n, x1 A.
3641:SAVE
3642 str.x.pre x30 sp -32
3643 adr x9 POOLC
3644 sub.x x10 x29 x9
3645 add.x x10 x10 x24
3646 str.x x10 sp 1
3647 add.x.i x11 x10 2
3648 ubfm.x x11 x11 1 63
3649 ubfm.x x11 x11 60 59
3650 str.x x11 sp 2
3651 mov.x x1 x11
3652 bl SPSUB
3653 bl STKCHK
3654 movz.x x13 0 0
3655:SV1
3656 ldr.x x10 sp 1
3657 cmp.x x13 x10
3658 b.hs SV2
3659 adr x9 POOLC
3660 ldrb.r w0 x9 x13
3661 movz.x x9 0x03e0 0
3662 movk.x x9 0xf900 1 ; STR Xr, [SP, #8j], C6.2.415
3663 orr.x x0 x0 x9
3664 ubfm.x x1 x13 54 53
3665 orr.x x0 x0 x1
3666 bl EMIT
3667 add.x.i x13 x13 1
3668 b SV1
3669:SV2
3670 movz.x x0 0x03fe 0
3671 movk.x x0 0xf900 1 ; STR X30, [SP, #8n], C6.2.415
3672 ubfm.x x1 x13 54 53
3673 orr.x x0 x0 x1
3674 bl EMIT
3675 ldr.x x0 sp 1
3676 ldr.x x1 sp 2
3677 ldr.x.post x30 sp 32
3678 ret
3679; LOADARG: x0 target register, x1 kind (1 or 5: register x2, read back from its save slot: LDR Xt, [SP,
3680; #8 slot], C6.2.216; 3 or 6: the number or address x2, MATCONST)
3681:LOADARG
3682 cmp.x.i x1 3
3683 b.eq MATCONST_X
3684 cmp.x.i x1 6
3685 b.eq MATCONST_X
3686 adr x9 REGIDX
3687 ldrb.r w9 x9 x2
3688 movz.x x10 0x03e0 0
3689 movk.x x10 0xf940 1 ; LDR Xt, [SP, #8j], C6.2.216
3690 orr.x x0 x0 x10
3691 ubfm.x x9 x9 54 53
3692 orr.x x0 x0 x9
3693 b EMIT
3694:MATCONST_X
3695 mov.x x1 x2
3696 b MATCONST
3697; BASEARG: x0 target register, x9 an array's name entry: its register from the save area, or its address
3698:BASEARG
3699 ldr.w w10 x9 5
3700 cmp.x.i x10 4
3701 b.hs BA_ADDR
3702 movz.x x1 1 0
3703 ldr.w w2 x9 2
3704 b LOADARG
3705:BA_ADDR
3706 ldr.x x1 x9 3
3707 b MATCONST
3708; RESTORE: x0 destination (255: a new temporary at pool slot x1), x1 pool slot before the arguments, x2
3709; saved count, x3 save bytes, x4 1 if there is a result in X0. MOV Xd, X0 (C6.2.282); LDR each register
3710; in use before the arguments except Xd (C6.2.216); LDR X30; ADD SP, SP, #A (C6.2.5). Out: x0 Xd.
3711:RESTORE
3712 str.x.pre x30 sp -64
3713 str.x x2 sp 2
3714 str.x x3 sp 3
3715 mov.x x24 x1
3716 str.x x1 sp 4
3717 movz.x x9 255 0
3718 str.x x9 sp 1
3719 cbz.x x4 RS2
3720 cmp.x.i x0 255
3721 b.ne RS1
3722 bl ALLOC
3723:RS1
3724 str.x x0 sp 1
3725 cbz.x x0 RS2
3726 movz.x x9 0x03e0 0
3727 movk.x x9 0xaa00 1 ; MOV Xd, X0 = ORR Xd, XZR, X0, C6.2.282
3728 orr.x x0 x0 x9
3729 bl EMIT
3730:RS2
3731 movz.x x13 0 0
3732 adr x9 POOLC
3733 sub.x x14 x29 x9
3734 ldr.x x9 sp 4
3735 add.x x14 x14 x9
3736:RS3
3737 cmp.x x13 x14
3738 b.hs RS4
3739 adr x9 POOLC
3740 ldrb.r w0 x9 x13
3741 ldr.x x9 sp 1
3742 cmp.x x0 x9
3743 b.eq RS5
3744 movz.x x9 0x03e0 0
3745 movk.x x9 0xf940 1 ; LDR Xr, [SP, #8j], C6.2.216
3746 orr.x x0 x0 x9
3747 ubfm.x x1 x13 54 53
3748 orr.x x0 x0 x1
3749 bl EMIT
3750:RS5
3751 add.x.i x13 x13 1
3752 b RS3
3753:RS4
3754 movz.x x0 0x03fe 0
3755 movk.x x0 0xf940 1 ; LDR X30, [SP, #8n], C6.2.216
3756 ldr.x x1 sp 2
3757 ubfm.x x1 x1 54 53
3758 orr.x x0 x0 x1
3759 bl EMIT
3760 ldr.x x1 sp 3
3761 bl SPADD
3762 ldr.x x0 sp 1
3763 ldr.x.post x30 sp 64
3764 ret
3765; ======================================================================================================
3766; ELEMADDR: x0 array name entry, index x1 kind, x2 number or register, x3 type. A number index must be
3767; inside the array (error 21) and needs no check; a register index: CMP Xi, #len (C6.2.96) or CMP Xi, Xt
3768; (C6.2.97), B.HS trap 3 (unsigned, so a negative signed index traps too). Out: x0 base register (the
3769; array's register, or a new one holding its address), x1 index register.
3770:ELEMADDR
3771 str.x.pre x30 sp -48
3772 str.x x0 sp 1
3773 cmp.x.i x3 1
3774 b.eq E_TYPE
3775 cmp.x.i x1 3
3776 b.ne EA_R
3777 ldr.w w9 x0 4
3778 cmp.x x2 x9
3779 b.hs E_BOUNDS
3780 str.x x2 sp 2
3781 bl ALLOC
3782 str.x x0 sp 3
3783 ldr.x x1 sp 2
3784 bl MATCONST
3785 b EA_B
3786:EA_R
3787 str.x x2 sp 3
3788 ldr.w w1 x0 4
3789 movz.x x9 4095 0
3790 cmp.x x1 x9
3791 b.hi EA_BIG
3792 movz.x x0 0x001f 0
3793 movk.x x0 0xf100 1 ; CMP Xi, #len, C6.2.96
3794 ubfm.x x1 x1 54 53
3795 orr.x x0 x0 x1
3796 b EA_C
3797:EA_BIG
3798 bl ALLOC
3799 str.x x0 sp 4
3800 bl MATCONST
3801 movz.x x0 0x001f 0
3802 movk.x x0 0xeb00 1 ; CMP Xi, Xt, C6.2.97
3803 ldr.x x1 sp 4
3804 ubfm.x x1 x1 48 47
3805 orr.x x0 x0 x1
3806:EA_C
3807 ldr.x x1 sp 3
3808 ubfm.x x1 x1 59 58
3809 orr.x x0 x0 x1
3810 bl EMIT
3811 movz.x x0 2 0
3812 movz.x x1 3 0
3813 bl TRAPC
3814:EA_B
3815 ldr.x x9 sp 1
3816 ldr.w w10 x9 5
3817 cmp.x.i x10 4
3818 b.hs EA_A
3819 ldr.w w0 x9 2
3820 b EA_X
3821:EA_A
3822 bl ALLOC
3823 str.x x0 sp 4
3824 ldr.x x9 sp 1
3825 ldr.x x1 x9 3
3826 bl MATCONST
3827 ldr.x x0 sp 4
3828:EA_X
3829 ldr.x x1 sp 3
3830 ldr.x.post x30 sp 48
3831 ret
3832; ARRNAME: read an array name -> x0 its entry
3833:ARRNAME
3834 str.x.pre x30 sp -16
3835 bl TOK
3836 cbz.x x0 E_FEW
3837 cmp.x.i x0 3
3838 b.eq E_FEW
3839 cmp.x.i x0 1
3840 b.ne E_SYNTAX
3841 cmp.x.i x2 8
3842 b.hi E_LONG
3843 bl FINDNAME
3844 cbz.x x0 E_UNDEF
3845 ldr.w w9 x0 5
3846 cbz.x x9 E_TYPE
3847 ldr.x.post x30 sp 16
3848 ret
3849; TOPNAME: read a new name for a global or a text -> x12
3850:TOPNAME
3851 str.x.pre x30 sp -16
3852 bl TOK
3853 cmp.x.i x0 1
3854 b.ne E_SYNTAX
3855 cmp.x.i x2 8
3856 b.hi E_LONG
3857 mov.x x12 x1
3858 bl KW
3859 cbnz.x x0 E_SYNTAX
3860 mov.x x1 x12
3861 bl NEWNAME
3862 ldr.x.post x30 sp 16
3863 ret
3864; FINDNAME: x1 packed name -> x0 its entry (0 none). Clobbers x9..x11.
3865:FINDNAME
3866 movz.x x9 0x180 1
3867 movz.x x10 0 0
3868:FD1
3869 cmp.x x10 x23
3870 b.hs FD0
3871 ldr.x x11 x9 0
3872 cmp.x x11 x1
3873 b.eq FDF
3874 add.x.i x9 x9 32
3875 add.x.i x10 x10 1
3876 b FD1
3877:FD0
3878 movz.x x0 0 0
3879 ret
3880:FDF
3881 mov.x x0 x9
3882 ret
3883; ADDNAME: x1 packed name, x2 register, x3 type, x4 length, x5 kind, x6 address. Clobbers x9, x10.
3884:ADDNAME
3885 cmp.x.i x23 1024
3886 b.hs E_FULL
3887 movz.x x9 0x180 1
3888 ubfm.x x10 x23 59 58
3889 add.x x9 x9 x10
3890 str.x x1 x9 0
3891 str.w w2 x9 2
3892 str.w w3 x9 3
3893 str.w w4 x9 4
3894 str.w w5 x9 5
3895 str.x x6 x9 3
3896 add.x.i x23 x23 1
3897 ret
3898; PUSHB: x0 branch to patch, x1 kind (1 if, 2 else, 3 while, 4 for), x2 head, x3 loop register,
3899; x4 its type, x5 names and x6 pool slots to restore at end; slot 7 = the then-part ended (for else)
3900:PUSHB
3901 movz.x x9 0x182 1
3902 cmp.x x25 x9
3903 b.hs E_FULL
3904 str.x x1 x25 0
3905 str.x x0 x25 1
3906 str.x x2 x25 2
3907 str.x x3 x25 3
3908 str.x x4 x25 4
3909 str.x x5 x25 5
3910 str.x x6 x25 6
3911 str.x xzr x25 7
3912 add.x.i x25 x25 128
3913 ret
3914; ELEMTYPE: x0 a type that an array may hold (uchar .. long, not bool)
3915:ELEMTYPE
3916 cmp.x.i x0 2
3917 b.lo E_SUBSET
3918 ret
3919; ARRLEN: x1 an array length: 1 .. 16777216 (error 8 below, 23 above; a number written as 2^63 or more,
3920; state slot 11, is above)
3921:ARRLEN
3922 movz.x x9 0x185 1
3923 ldr.x x9 x9 11
3924 cbnz.x x9 E_TOOBIG
3925 cmp.x.i x1 1
3926 b.lt E_RANGE
3927 movz.x x9 0x100 1
3928 cmp.x x1 x9
3929 b.hi E_TOOBIG
3930 ret
3931; EPI: ADD SP, SP, #stack of the local arrays (if any), then RET (C6.2.338)
3932:EPI
3933 str.x.pre x30 sp -16
3934 movz.x x9 0x185 1
3935 ldr.x x1 x9 4
3936 bl SPADD
3937 movz.x x0 0x03c0 0
3938 movk.x x0 0xd65f 1 ; RET (X30), C6.2.338
3939 bl EMIT
3940 ldr.x.post x30 sp 16
3941 ret
3942; SPADD / SPSUB: x1 bytes (a multiple of 16, below 2^24): ADD (C6.2.5) / SUB (C6.2.457) SP, SP, #low 12
3943; bits, then #high 12 bits LSL 12 (sh = 1, bit 22); nothing for a zero part
3944:SPADD
3945 movz.x x2 0x9100 1
3946 b SPOP
3947:SPSUB
3948 movz.x x2 0xd100 1
3949:SPOP
3950 str.x.pre x30 sp -32
3951 str.x x1 sp 1
3952 str.x x2 sp 2
3953 ubfm.x x3 x1 0 11
3954 cbz.x x3 SPO1
3955 movz.x x9 0x03ff 0
3956 orr.x x0 x2 x9
3957 ubfm.x x3 x3 54 53
3958 orr.x x0 x0 x3
3959 bl EMIT
3960:SPO1
3961 ldr.x x1 sp 1
3962 ubfm.x x3 x1 12 23
3963 cbz.x x3 SPO2
3964 ldr.x x2 sp 2
3965 movz.x x9 0x03ff 0
3966 movk.x x9 0x0040 1
3967 orr.x x0 x2 x9
3968 ubfm.x x3 x3 54 53
3969 orr.x x0 x0 x3
3970 bl EMIT
3971:SPO2
3972 ldr.x.post x30 sp 32
3973 ret
3974; STKCHK: program mode only: CMP SP, X28 (SUBS extended register, UXTX, Rn = SP, C6.2.463); B.LO trap 6
3975:STKCHK
3976 movz.x x9 0x185 1
3977 ldr.x x10 x9 6
3978 cbz.x x10 SK1
3979 str.x.pre x30 sp -16
3980 movz.x x0 0x63ff 0
3981 movk.x x0 0xeb3c 1 ; CMP SP, X28, C6.2.463
3982 bl EMIT
3983 movz.x x0 3 0
3984 movz.x x1 6 0
3985 bl TRAPC
3986 ldr.x.post x30 sp 16
3987:SK1
3988 ret
3989; DEAD1: the path has ended (ret, exit)
3990:DEAD1
3991 movz.x x9 0x185 1
3992 movz.x x10 1 0
3993 str.x x10 x9 5
3994 ret
3995; EMITB: x0 one byte -> output. Clobbers x9.
3996:EMITB
3997 movz.x x9 0x180 1
3998 cmp.x x20 x9
3999 b.hs E_FULL
4000 strb w0 x20 0
4001 add.x.i x20 x20 1
4002 ret
4003; ======================================================================================================
4004; errors new in om4, and FAIL
4005:E_DEAD
4006 movz.x x0 17 0
4007 b FAIL
4008:E_NORET
4009 movz.x x0 18 0
4010 b FAIL
4011:E_ALIAS
4012 movz.x x0 19 0
4013 b FAIL
4014:E_RDONLY
4015 movz.x x0 20 0
4016 b FAIL
4017:E_BOUNDS
4018 movz.x x0 21 0
4019 b FAIL
4020:E_TEXT
4021 movz.x x0 22 0
4022 b FAIL
4023:E_TOOBIG
4024 movz.x x0 23 0
4025 b FAIL
4026; FAIL: x0 code: "om4: line N: error E" and a newline on stderr (fd 2), exit E
4027:FAIL
4028 mov.x x12 x0
4029 movz.x x9 0x183 1
4030 adr x10 MSG1
4031 movz.x x11 10 0
4032 bl COPYN
4033 mov.x x0 x22
4034 bl PUTDEC
4035 adr x10 MSG2
4036 movz.x x11 8 0
4037 bl COPYN
4038 mov.x x0 x12
4039 bl PUTDEC
4040 movz.x x0 10 0
4041 strb w0 x9 0
4042 add.x.i x9 x9 1
4043 movz.x x0 2 0
4044 movz.x x1 0x183 1
4045 sub.x x2 x9 x1
4046 movz.x x8 64 0
4047 svc 0
4048 mov.x x0 x12
4049 movz.x x8 93 0
4050 svc 0
4051; COPYN: copy x11 bytes at x10 to x9 (x9 moves on)
4052:COPYN
4053 cbz.x x11 CN1
4054 ldrb w13 x10 0
4055 strb w13 x9 0
4056 add.x.i x9 x9 1
4057 add.x.i x10 x10 1
4058 sub.x.i x11 x11 1
4059 b COPYN
4060:CN1
4061 ret
4062; ======================================================================================================
4063; NEWNAME: x1 packed name, x2 its length, x3 its first byte: the name must not be in use (a name in scope,
4064; when 8 characters or fewer, or a function: error 10). Keeps x1..x8, x12..x14. Clobbers x0, x9..x11, x15..x17.
4065:NEWNAME
4066 str.x.pre x30 sp -16
4067 str.x x1 sp 1
4068 cmp.x.i x2 8
4069 b.hi NN1
4070 bl FINDNAME
4071 cbnz.x x0 E_TWICE
4072:NN1
4073 ldr.x x1 sp 1
4074 bl FINDFN
4075 cbnz.x x0 E_TWICE
4076 ldr.x x1 sp 1
4077 ldr.x.post x30 sp 16
4078 ret
4079; FINDFN: x1 packed name (first 8 characters), x2 length (any value up to 8 for a short name), x3 first
4080; byte (read only for a name over 8 characters) -> x0 its function entry (0 none). A function entry keeps
4081; its name's length at +120 and the name's place in the input at +124. Clobbers x9..x11, x15..x17.
4082:FINDFN
4083 movz.x x9 0x185 1
4084 ldr.x x10 x9 2
4085 movz.x x9 0x19f 1
4086:FF1
4087 cbz.x x10 FD0
4088 ldr.x x11 x9 0
4089 cmp.x x11 x1
4090 b.ne FF2
4091 ldr.w w11 x9 30
4092 cmp.x.i x2 8
4093 b.hi FF_LONG
4094 cmp.x.i x11 8
4095 b.ls FDF
4096 b FF2
4097:FF_LONG
4098 cmp.x x11 x2
4099 b.ne FF2
4100 ldr.w w15 x9 31
4101 movz.x x16 0x80 1
4102 add.x x15 x15 x16
4103 movz.x x16 8 0
4104:FF3
4105 cmp.x x16 x2
4106 b.hs FDF
4107 ldrb.r w17 x15 x16
4108 ldrb.r w11 x3 x16
4109 cmp.x x17 x11
4110 b.ne FF2
4111 add.x.i x16 x16 1
4112 b FF3
4113:FF2
4114 add.x.i x9 x9 128
4115 sub.x.i x10 x10 1
4116 b FF1
4117; S_LONG: a word over 8 characters at the start of a line: only a call of a void function
4118:S_LONG
4119 cbz.x x27 E_LONG
4120 movz.x x9 0x185 1
4121 ldr.x x10 x9 5
4122 cbnz.x x10 E_DEAD
4123 sub.x x3 x19 x2
4124 bl FINDFN
4125 cbz.x x0 E_LONG
4126 ldr.w w9 x0 4
4127 cmp.x.i x9 10
4128 b.ne E_TYPE
4129 movz.x x1 255 0
4130 bl CALL
4131 bl EOL
4132 b STMT
4133; VT_LONG: a word over 8 characters as a value: only a call of a function that gives a value
4134:VT_LONG
4135 mov.x x1 x4
4136 mov.x x2 x5
4137 sub.x x3 x19 x5
4138 bl FINDFN
4139 cbz.x x0 E_LONG
4140 b VT_FN2
4141; Resolve recorded BL sites; function mode must have an acyclic call graph.
4142; 65536 fixed records at 0x1860000: site, callee entry, caller entry. State slot 12 is count.
4143:RESOLVE
4144 movz.x x9 0x185 1
4145 ldr.x x10 x9 12
4146 movz.x x11 0x186 1
4147 movz.x x12 0 0
4148:RS_PATCH
4149 cmp.x x12 x10
4150 b.hs RS_GRAPH
4151 ldr.x x0 x11 0
4152 ldr.x x1 x11 1
4153 ldr.x x1 x1 1
4154 sub.x x1 x1 x0
4155 ubfm.x x1 x1 2 27
4156 movz.x x2 0x9400 1
4157 orr.x x1 x1 x2
4158 str.w w1 x0 0
4159 add.x.i x11 x11 24
4160 add.x.i x12 x12 1
4161 b RS_PATCH
4162:RS_GRAPH
4163 ldr.x x0 x9 6
4164 cbnz.x x0 RS_DONE
4165 ldr.x x1 x9 2
4166 movz.x x2 0 0
4167 movz.x x3 0x19e 1
4168 movz.x x4 0x19f 1
4169:RS_PASS
4170 cmp.x x2 x1
4171 b.hs RS_CHECK
4172 movz.x x5 0 0
4173:RS_FN
4174 cmp.x x5 x1
4175 b.hs RS_NEXT
4176 ldrb.r w0 x3 x5
4177 cbnz.x x0 RS_FNEXT
4178 ubfm.x x6 x5 57 56
4179 add.x x6 x6 x4
4180 movz.x x7 0 0
4181 movz.x x11 0x186 1
4182:RS_EDGE
4183 cmp.x x7 x10
4184 b.hs RS_MARK
4185 ldr.x x0 x11 2
4186 cmp.x x0 x6
4187 b.ne RS_ENEXT
4188 ldr.x x0 x11 1
4189 sub.x x0 x0 x4
4190 ubfm.x x0 x0 7 63
4191 ldrb.r w0 x3 x0
4192 cbz.x x0 RS_FNEXT
4193:RS_ENEXT
4194 add.x.i x7 x7 1
4195 add.x.i x11 x11 24
4196 b RS_EDGE
4197:RS_MARK
4198 movz.x x0 1 0
4199 add.x x6 x3 x5
4200 strb w0 x6 0
4201:RS_FNEXT
4202 add.x.i x5 x5 1
4203 b RS_FN
4204:RS_NEXT
4205 add.x.i x2 x2 1
4206 b RS_PASS
4207:RS_CHECK
4208 movz.x x5 0 0
4209:RS_CHECK1
4210 cmp.x x5 x1
4211 b.hs RS_DONE
4212 ldrb.r w0 x3 x5
4213 cbz.x x0 E_SUBSET
4214 add.x.i x5 x5 1
4215 b RS_CHECK1
4216:RS_DONE
4217 ret