interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / ng / src / a64.om

5511 lines · 119666 bytes Raw Blame History
1note a64: the Omega NG AArch64 back end (lane ng-a64), one unit of the ng compiler (DECISIONS.md D1b): ng is the
2note concatenation listed in seed/ng/ng.units, so this file has no fn main; its functions are a6_*, its globals a_*.
3note Design: seed/ng/a64/DESIGN.md. Instruction bits: Arm ARM DDI 0487 M.d, section named at each form (the source
4note of seed/rung2/table.s2). Input is the payload main read into region 0 of ir.om's arena (i_in before the no-caps
5note arena; the notes below still say i_in for it) from offset 0, a_inlen bytes; main's
6note hooks are a6_mode m (does the mode line name a mode of this unit) and a6_run (run it). `@ng a64-enc` takes an
7note encoder case list (seed/ng/a64/tests/enc.cases after its mode line) and writes one little-endian word per case.
8note OM-2 (hex words) comes from the unit's first line, at the top of ir.om.
9
10note ---- input and output pools ----
11global a_inlen ulong 1
12global a_cn ulong 1
13
14note a_emsg: line 0 the prefix, line k the class of refusal k (a6_die c k n)
15text a_emsg "a64:\n input unreadable\n input too large\n output failed\n bad encoder case at line\n unsupported\n immediate out of range\n branch out of range\n IR refused (exit status = IR.md s11 class) at payload byte\n reserved operand value\n"
16
17note ---- fatal refusal: one stderr line `a64: <message>[ <n>]` and exit code c (nothing on stdout) ----
18fn a6_lst k ulong to ulong
19note byte offset of line k of a_emsg
20i = ulong 0
21j = ulong 0
22while and lt j k lt i len a_emsg bound 4096
23if eq get a_emsg i 10
24j = add j 1
25end
26i = add i 1
27end
28ret i
29end
30
31global a_nbuf uchar 24
32
33fn a6_pnum v ulong to void
34p = ulong 23
35put a_nbuf 23 10
36x = v
37go = true
38while go bound 24
39p = sub p 1
40put a_nbuf p uchar add rem x 10 48
41x = div x 10
42if eq x 0
43go = false
44end
45end
46p = sub p 1
47put a_nbuf p 32
48w = write 2 a_nbuf p sub 24 p
49end
50
51fn a6_die c ulong k ulong n ulong to void
52w = write 2 a_emsg 0 4
53o = a6_lst k
54e = sub a6_lst add k 1 1
55w2 = write 2 a_emsg o sub e o
56if ne n 18446744073709551615
57a6_pnum n
58else
59w3 = write 2 a_emsg 4 1
60end
61exit int c
62end
63
64note ---- output: little-endian words into a_code (arena group a64code, no-caps 2026-11-11; it was a fixed 16 MiB),
65note then all of it to stdout. What bounds the ELF's code is the machine: B and BL reach 2^27 bytes (imm26), so a
66note word at or past 134217728 is `a64: branch out of range` (exit 5) before any output, as omc's AArch64 output
67note stops there; the Mach-O layout keeps its own 16 MiB less 16 KiB (a6_macho). A full arena is ng's arena full.
68fn a6_emitw w ulong to void
69o = get a_cn 0
70if gt add o 4 134217728
71a6_die 5 7 o
72end
73if ne ar_need 29 add o 3 0
74ar_die
75end
76note o is a multiple of 4, so the word is the low or the high half of one arena word (masked: a function's
77note second emission rewrites words of its first)
78x = add get ar_rb 107 shr o 3
79if eq and o 7 0
80put ar_m x or and get ar_m x 18446744069414584320 and w 4294967295
81else
82put ar_m x or and get ar_m x 4294967295 shl and w 4294967295 32
83end
84put a_cn 0 add o 4
85end
86
87fn a6_flush to void
88if eq ar_out 1 107 0 get a_cn 0 0
89a6_die 4 3 18446744073709551615
90end
91end
92
93note ======== encoder: one 32-bit A64 instruction word per form ========
94note A form is a row of the table set by a6_init: base word (the encoding diagram with every operand field 0,
95note Arm ARM DDI 0487 M.d section in the note above each group) and an operand kind. a6_enc f a b c d e builds the
96note word from up to five operands. Register numbers are 0..31 (31 is ZR or SP as the form says). An operand that
97note does not fit its field is refused (a6_die 6 or 7), never masked; so is an in-field value whose encoding the
98note form reserves (a6_die 9, a6_rsv). The names in a_fnam are the names the encoder case list
99note (seed/ng/a64/tests/enc.cases) uses, in form id order.
100note Kinds: 1 R3 (d n m sh amt): base | d | n<<5 | m<<16 | sh<<22 | amt<<10; 2 R4 (d n m a): | a<<10;
101note 3 EXT (d n m opt amt): | opt<<13 | amt<<10; 4 IMM12 (d n imm sh): | imm<<10 | sh<<22; 5 LOGI (d n value);
102note 6 MOVW (d imm16 hw): | imm<<5 | hw<<21; 7 BFM (d n immr imms); 8 CSEL (d n m cond): | cond<<12;
103note 9 LDST (t n imm12); 10 LDSR (t n m s): register offset LSL, s = scaled; 11 PAIR (t t2 n off): signed
104note byte offset /8 at bit 15; 12 B26 (words); 13 BCOND (cond words); 14 CB (t words); 15 TB (t bit words);
105note 16 BR (n); 17 SVC (imm16); 18 ADR (d bytes); 19 R2 (d n); 20 FCMP (n m); 21 EXTR (d n m lsb).
106note 22 RT (t): | t (MSR FPCR); 23 DUPE (d n idx): DUP element, S idx 0..3 at bit 19, D idx 0..1 at bit 20.
107note Signed operands (distances, pair offsets) are two's complement ulongs.
108
109global a_fbase ulong 256
110global a_fkind uchar 256
111global a_nform ulong 1
112
113text a_fnam "addx addw subx subw addsx addsw subsx subsw andx andw orrx orrw eorx eorw andsx bicx ornx mulx mulw smulh umulh sdivx udivx sdivw udivw lslvx lsrvx asrvx rorvx lslvw lsrvw asrvw maddx msubx addxe subsxe addxi addwi subxi subwi addsxi subsxi subswi addswi andxi orrxi eorxi andsxi andwi orrwi eorwi movzx movnx movkx movzw movnw ubfmx sbfmx ubfmw sbfmw extrx cselx csincx csinvx csnegx strb ldrb ldrsbx strh ldrh ldrshx strw ldrw ldrsw strx ldrx strs ldrs strd ldrd strbr ldrbr ldrsbxr strhr ldrhr ldrshxr strwr ldrwr ldrswr strxr ldrxr strsr ldrsr strdr ldrdr stpx ldpx stpxpre ldpxpre stpxpost ldpxpost stpd ldpd b bl bcond cbzx cbnzx cbzw cbnzw tbz tbnz ret br blr svc adr faddd fsubd fmuld fdivd fmaxd fmind fmaxnmd fminnmd fadds fsubs fmuls fdivs fmaxs fmins fmaxnms fminnms fsqrtd fabsd fnegd fmovd fsqrts fabss fnegs fmovs fcmpd fcmps fmaddd fmadds fcvtds fcvtsd fmovxd fmovdx fmovws fmovsw scvtfdx ucvtfdx scvtfsx ucvtfsx fcvtzsxd fcvtzuxd fcvtzsxs fcvtzuxs rorvw fcseld fcsels msrfpcr dup2d strqr strq ldrq ldrqr orr16b dupv4s dupv2d dups dupd fadd4s fadd2d fsub4s fsub2d fmul4s fmul2d fmla4s fmla2d"
114
115fn a6_fset i ulong b ulong k ulong to void
116put a_fbase i b
117put a_fkind i uchar k
118if ge i get a_nform 0
119put a_nform 0 add i 1
120end
121end
122
123note form ids used by the selector (index into a_fnam)
124const a_addx ulong 0
125const a_addw ulong 1
126const a_subx ulong 2
127const a_subw ulong 3
128const a_addsx ulong 4
129const a_subsx ulong 6
130const a_subsw ulong 7
131const a_andx ulong 8
132const a_orrx ulong 10
133const a_eorx ulong 12
134const a_andsx ulong 14
135const a_ornx ulong 16
136const a_mulx ulong 17
137const a_mulw ulong 18
138const a_smulh ulong 19
139const a_umulh ulong 20
140const a_sdivx ulong 21
141const a_udivx ulong 22
142const a_udivw ulong 24
143const a_lslvx ulong 25
144const a_lsrvx ulong 26
145const a_asrvx ulong 27
146const a_lslvw ulong 29
147const a_maddx ulong 32
148const a_msubx ulong 33
149const a_subsxe ulong 35
150const a_addxi ulong 36
151const a_subxi ulong 38
152const a_addsxi ulong 40
153const a_subsxi ulong 41
154const a_andxi ulong 44
155const a_orrxi ulong 45
156const a_eorxi ulong 46
157const a_movzx ulong 51
158const a_movnx ulong 52
159const a_movkx ulong 53
160const a_ubfmx ulong 56
161const a_sbfmx ulong 57
162const a_cselx ulong 61
163const a_csincx ulong 62
164const a_csnegx ulong 64
165const a_strb ulong 65
166const a_ldrb ulong 66
167const a_strx ulong 74
168const a_ldrx ulong 75
169const a_strd ulong 78
170const a_ldrd ulong 79
171const a_strbr ulong 80
172const a_ldrbr ulong 81
173const a_ldrxr ulong 90
174const a_stpx ulong 95
175const a_ldpx ulong 96
176const a_stpxpr ulong 97
177const a_ldpxpo ulong 100
178const a_stpd ulong 101
179const a_ldpd ulong 102
180const a_b ulong 103
181const a_bl ulong 104
182const a_bcond ulong 105
183const a_cbzx ulong 106
184const a_cbnzx ulong 107
185const a_ret ulong 112
186const a_svc ulong 115
187const a_adr ulong 116
188const a_fmovd ulong 136
189const a_fcmpd ulong 141
190const a_fmovxd ulong 147
191const a_fmovdx ulong 148
192const a_strsr ulong 91
193const a_ldrsr ulong 92
194const a_strdr ulong 93
195const a_ldrdr ulong 94
196const a_faddd ulong 117
197const a_fmaxnmd ulong 123
198const a_fminnmd ulong 124
199const a_fadds ulong 125
200const a_fmaxnms ulong 131
201const a_fminnms ulong 132
202const a_fsqrtd ulong 133
203const a_fabsd ulong 134
204const a_fnegd ulong 135
205const a_fsqrts ulong 137
206const a_fabss ulong 138
207const a_fnegs ulong 139
208const a_fcmps ulong 142
209const a_fmaddd ulong 143
210const a_fmadds ulong 144
211const a_fcvtds ulong 145
212const a_fcvtsd ulong 146
213const a_fmovws ulong 149
214const a_fmovsw ulong 150
215const a_scvtfdx ulong 151
216const a_ucvtfdx ulong 152
217const a_scvtfsx ulong 153
218const a_ucvtfsx ulong 154
219const a_fcvtzsxd ulong 155
220const a_fcvtzuxd ulong 156
221const a_fcvtzsxs ulong 157
222const a_fcvtzuxs ulong 158
223const a_fcseld ulong 160
224const a_fcsels ulong 161
225const a_msrfpcr ulong 162
226const a_dup2d ulong 163
227const a_strqr ulong 164
228const a_strq ulong 165
229const a_ldrq ulong 166
230const a_ldrqr ulong 167
231const a_orr16b ulong 168
232const a_dupv4s ulong 169
233const a_dupv2d ulong 170
234const a_dups ulong 171
235const a_dupd ulong 172
236const a_fadd4s ulong 173
237const a_fadd2d ulong 174
238const a_fsub4s ulong 175
239const a_fsub2d ulong 176
240const a_fmul4s ulong 177
241const a_fmul2d ulong 178
242const a_fmla4s ulong 179
243const a_fmla2d ulong 180
244note a_tbz: TBZ (form 110), used directly with a word distance by the fill tails
245const a_tbz ulong 110
246note a_d0: the move location of d0, the float argument 0 and result register (32 + V0)
247const a_d0 ulong 32
248
249note the form table. Sections: C6.2.6 ADD, C6.2.458 SUB, C6.2.8 ADDS, C6.2.465 SUBS, C6.2.15 AND, C6.2.302 ORR,
250note C6.2.156 EOR, C6.2.17 ANDS, C6.2.32 BIC, C6.2.300 ORN (shifted register; 32-bit forms clear bit 31)
251fn a6_formdp to void
252a6_fset 0 0x8b000000 1
253a6_fset 1 0x0b000000 1
254a6_fset 2 0xcb000000 1
255a6_fset 3 0x4b000000 1
256a6_fset 4 0xab000000 1
257a6_fset 5 0x2b000000 1
258a6_fset 6 0xeb000000 1
259a6_fset 7 0x6b000000 1
260a6_fset 8 0x8a000000 1
261a6_fset 9 0x0a000000 1
262a6_fset 10 0xaa000000 1
263a6_fset 11 0x2a000000 1
264a6_fset 12 0xca000000 1
265a6_fset 13 0x4a000000 1
266a6_fset 14 0xea000000 1
267a6_fset 15 0x8a200000 1
268a6_fset 16 0xaa200000 1
269note C6.2.293 MUL (MADD Ra=31), C6.2.370 SMULH, C6.2.505 UMULH, C6.2.366 SDIV, C6.2.490 UDIV, C6.2.271 LSLV,
270note C6.2.274 LSRV, C6.2.21 ASRV, C6.2.343 RORV
271a6_fset 17 0x9b007c00 1
272a6_fset 18 0x1b007c00 1
273a6_fset 19 0x9b407c00 1
274a6_fset 20 0x9bc07c00 1
275a6_fset 21 0x9ac00c00 1
276a6_fset 22 0x9ac00800 1
277a6_fset 23 0x1ac00c00 1
278a6_fset 24 0x1ac00800 1
279a6_fset 25 0x9ac02000 1
280a6_fset 26 0x9ac02400 1
281a6_fset 27 0x9ac02800 1
282a6_fset 28 0x9ac02c00 1
283a6_fset 29 0x1ac02000 1
284a6_fset 30 0x1ac02400 1
285a6_fset 31 0x1ac02800 1
286note C6.2.275 MADD, C6.2.291 MSUB
287a6_fset 32 0x9b000000 2
288a6_fset 33 0x9b008000 2
289note C6.2.4 ADD (extended register), C6.2.463 SUBS (extended register)
290a6_fset 34 0x8b200000 3
291a6_fset 35 0xeb200000 3
292note C6.2.5 ADD, C6.2.457 SUB, C6.2.7 ADDS, C6.2.464 SUBS (immediate)
293a6_fset 36 0x91000000 4
294a6_fset 37 0x11000000 4
295a6_fset 38 0xd1000000 4
296a6_fset 39 0x51000000 4
297a6_fset 40 0xb1000000 4
298a6_fset 41 0xf1000000 4
299a6_fset 42 0x71000000 4
300a6_fset 43 0x31000000 4
301note C6.2.14 AND, C6.2.301 ORR, C6.2.155 EOR, C6.2.16 ANDS (immediate)
302a6_fset 44 0x92000000 5
303a6_fset 45 0xb2000000 5
304a6_fset 46 0xd2000000 5
305a6_fset 47 0xf2000000 5
306a6_fset 48 0x12000000 5
307a6_fset 49 0x32000000 5
308a6_fset 50 0x52000000 5
309note C6.2.285 MOVZ, C6.2.284 MOVN, C6.2.283 MOVK
310a6_fset 51 0xd2800000 6
311a6_fset 52 0x92800000 6
312a6_fset 53 0xf2800000 6
313a6_fset 54 0x52800000 6
314a6_fset 55 0x12800000 6
315note C6.2.488 UBFM, C6.2.355 SBFM, C6.2.162 EXTR
316a6_fset 56 0xd3400000 7
317a6_fset 57 0x93400000 7
318a6_fset 58 0x53000000 7
319a6_fset 59 0x13000000 7
320a6_fset 60 0x93c00000 21
321note C6.2.115 CSEL, C6.2.117 CSINC, C6.2.118 CSINV, C6.2.119 CSNEG
322a6_fset 61 0x9a800000 8
323a6_fset 62 0x9a800400 8
324a6_fset 63 0xda800000 8
325a6_fset 64 0xda800400 8
326end
327
328note loads and stores: C6.2.417 STRB, C6.2.220 LDRB, C6.2.224 LDRSB, C6.2.419 STRH, C6.2.222 LDRH, C6.2.226
329note LDRSH, C6.2.415 STR, C6.2.216 LDR, C6.2.228 LDRSW, C7.2.331 STR (SIMD&FP), C7.2.191 LDR (SIMD&FP); unsigned
330note offset (kind 9) then the same bases as register offset (kind 10)
331fn a6_formls to void
332a6_fset 65 0x39000000 9
333a6_fset 66 0x39400000 9
334a6_fset 67 0x39800000 9
335a6_fset 68 0x79000000 9
336a6_fset 69 0x79400000 9
337a6_fset 70 0x79800000 9
338a6_fset 71 0xb9000000 9
339a6_fset 72 0xb9400000 9
340a6_fset 73 0xb9800000 9
341a6_fset 74 0xf9000000 9
342a6_fset 75 0xf9400000 9
343a6_fset 76 0xbd000000 9
344a6_fset 77 0xbd400000 9
345a6_fset 78 0xfd000000 9
346a6_fset 79 0xfd400000 9
347i = ulong 0
348while lt i 15 bound 15
349a6_fset add 80 i get a_fbase add 65 i 10
350i = add i 1
351end
352note C6.2.414 STP, C6.2.213 LDP (64-bit: signed offset, pre-index, post-index); C7.2.330 STP, C7.2.190 LDP (D)
353a6_fset 95 0xa9000000 11
354a6_fset 96 0xa9400000 11
355a6_fset 97 0xa9800000 11
356a6_fset 98 0xa9c00000 11
357a6_fset 99 0xa8800000 11
358a6_fset 100 0xa8c00000 11
359a6_fset 101 0x6d000000 11
360a6_fset 102 0x6d400000 11
361note C6.2.35 B, C6.2.43 BL, C6.2.34 B.cond, C6.2.78 CBZ, C6.2.77 CBNZ, C6.2.481 TBZ, C6.2.480 TBNZ,
362note C6.2.338 RET, C6.2.46 BR, C6.2.44 BLR, C6.2.466 SVC, C6.2.11 ADR
363a6_fset 103 0x14000000 12
364a6_fset 104 0x94000000 12
365a6_fset 105 0x54000000 13
366a6_fset 106 0xb4000000 14
367a6_fset 107 0xb5000000 14
368a6_fset 108 0x34000000 14
369a6_fset 109 0x35000000 14
370a6_fset 110 0x36000000 15
371a6_fset 111 0x37000000 15
372a6_fset 112 0xd65f0000 16
373a6_fset 113 0xd61f0000 16
374a6_fset 114 0xd63f0000 16
375a6_fset 115 0xd4000001 17
376a6_fset 116 0x10000000 18
377end
378
379note scalar floating point (ftype bits 22..23: 00 single, 01 double): C7.2.50 FADD, C7.2.161 FSUB, C7.2.133 FMUL,
380note C7.2.98 FDIV, C7.2.111 FMAX, C7.2.121 FMIN, C7.2.113 FMAXNM, C7.2.123 FMINNM; C7.2.157 FSQRT, C7.2.46 FABS,
381note C7.2.139 FNEG, C7.2.129 FMOV (register); C7.2.66 FCMP; C7.2.105 FMADD; C7.2.69 FCVT; C7.2.131 FMOV
382note (general); C7.2.236 SCVTF, C7.2.355 UCVTF, C7.2.92 FCVTZS, C7.2.96 FCVTZU (scalar, integer)
383fn a6_formfp to void
384a6_fset 117 0x1e602800 1
385a6_fset 118 0x1e603800 1
386a6_fset 119 0x1e600800 1
387a6_fset 120 0x1e601800 1
388a6_fset 121 0x1e604800 1
389a6_fset 122 0x1e605800 1
390a6_fset 123 0x1e606800 1
391a6_fset 124 0x1e607800 1
392a6_fset 125 0x1e202800 1
393a6_fset 126 0x1e203800 1
394a6_fset 127 0x1e200800 1
395a6_fset 128 0x1e201800 1
396a6_fset 129 0x1e204800 1
397a6_fset 130 0x1e205800 1
398a6_fset 131 0x1e206800 1
399a6_fset 132 0x1e207800 1
400a6_fset 133 0x1e61c000 19
401a6_fset 134 0x1e60c000 19
402a6_fset 135 0x1e614000 19
403a6_fset 136 0x1e604000 19
404a6_fset 137 0x1e21c000 19
405a6_fset 138 0x1e20c000 19
406a6_fset 139 0x1e214000 19
407a6_fset 140 0x1e204000 19
408a6_fset 141 0x1e602000 20
409a6_fset 142 0x1e202000 20
410a6_fset 143 0x1f400000 2
411a6_fset 144 0x1f000000 2
412a6_fset 145 0x1e22c000 19
413a6_fset 146 0x1e624000 19
414a6_fset 147 0x9e660000 19
415a6_fset 148 0x9e670000 19
416a6_fset 149 0x1e260000 19
417a6_fset 150 0x1e270000 19
418a6_fset 151 0x9e620000 19
419a6_fset 152 0x9e630000 19
420a6_fset 153 0x9e220000 19
421a6_fset 154 0x9e230000 19
422a6_fset 155 0x9e780000 19
423a6_fset 156 0x9e790000 19
424a6_fset 157 0x9e380000 19
425a6_fset 158 0x9e390000 19
426note C6.2.302 RORV, 32-bit variant (sf 0): w rotate right by the low 5 bits of m (u32 rotations)
427a6_fset 159 0x1ac02c00 1
428note FCSEL (scalar, SIMD&FP): Rd | Rn << 5 | cond << 12 | Rm << 16 on base 0x1e200c00, ftype bits 22..23 (00 S,
429note 01 D), the kind-8 shape. MSR (register) with o0 1 (op0 3), op1 3, CRn 4, CRm 4, op2 0, the FPCR tuple: the
430note one Rt field in bits 0..4 (kind 22). Wolfram: FromDigits["d5100000",16] + 2^19 + 3*2^16 + 4*2^12 + 4*2^8 gives
431note d51b4400 (NgA64Float-design.md); every word is checked against GNU objdump by gate.om (tests/enc.cases).
432a6_fset 160 0x1e600c00 8
433a6_fset 161 0x1e200c00 8
434a6_fset 162 0xd51b4400 22
435note DUP (general) Vd.2D, Xn: Q 1, imm5 01000 (kind 19: Rd, Rn << 5); STR (register, SIMD&FP) Qt, [Xn, Xm{, LSL
436note #4}]: size 00, opc 10, option 011 (kind 10, S at bit 12; like the other kind-10 forms it is given the
437note unsigned-offset base, STR Qt, [Xn, #imm] 0x3d800000, from which a6_enc derives 0x3ca06800), the fill's 16-byte
438note block store; both checked against GNU objdump by gate.om
439a6_fset 163 0x4e080c00 19
440a6_fset 164 0x3d800000 10
441note Packed 16-byte SIMD (NgVecA64, DESIGN.md section 12): C7.2.331 STR Qt, [Xn, #imm] (kind 9), C7.2.191 LDR Qt,
442note [Xn, #imm] (kind 9), C7.2.192 LDR Qt, [Xn, Xm{, LSL #4}] (kind 10 on the unsigned-offset base 0x3dc00000,
443note giving 0x3ce06800); C7.2.206 ORR Vd.16B, Vn.16B, Vm.16B (kind 1, MOV alias when Vn = Vm); C7.2.42 DUP
444note (element) vector Vd.4S/2D and scalar Sd/Dd from Vn.S/D[idx] (kind 23); C7.2.50 FADD, C7.2.161 FSUB,
445note C7.2.133 FMUL, C7.2.125 FMLA on 4S (Q=1, sz=0) and 2D (Q=1, sz=1) (kind 1).
446a6_fset 165 0x3d800000 9
447a6_fset 166 0x3dc00000 9
448a6_fset 167 0x3dc00000 10
449a6_fset 168 0x4ea01c00 1
450a6_fset 169 0x4e040400 23
451a6_fset 170 0x4e080400 23
452a6_fset 171 0x5e040400 23
453a6_fset 172 0x5e080400 23
454a6_fset 173 0x4e20d400 1
455a6_fset 174 0x4e60d400 1
456a6_fset 175 0x4ea0d400 1
457a6_fset 176 0x4ee0d400 1
458a6_fset 177 0x6e20dc00 1
459a6_fset 178 0x6e60dc00 1
460a6_fset 179 0x4e20cc00 1
461a6_fset 180 0x4e60cc00 1
462end
463
464fn a6_init to void
465a6_formdp
466a6_formls
467a6_formfp
468end
469
470fn a6_fld v ulong w ulong to ulong
471if ge v shl ulong 1 w
472a6_die 5 6 v
473end
474ret v
475end
476
477note a6_dsig: a signed distance d (two's complement ulong) that must fit w bits, masked to w bits (a6_die 7 if not)
478fn a6_dsig d ulong w ulong to ulong
479h = shl ulong 1 sub w 1
480if ne shr d 63 0
481if lt d sub 0 h
482a6_die 5 7 d
483end
484else
485if ge d h
486a6_die 5 7 d
487end
488end
489ret and d sub shl ulong 1 w 1
490end
491
492note ---- bitmask immediates (DDI 0487 M.d J1 shared pseudocode DecodeBitMasks, run backwards): a6_bmenc v w
493note returns N << 12 | immr << 6 | imms for the w-bit (32 or 64) value v, or 65536 when v has no encoding ----
494fn a6_rotl x ulong r ulong e ulong to ulong
495note rotate the low e bits of x left by r (0 <= r < e <= 64)
496if eq r 0
497ret x
498end
499m = ulong 18446744073709551615
500if lt e 64
501m = sub shl ulong 1 e 1
502end
503ret and or shl x r shr x sub e r m
504end
505
506note a6_bmsz: the smallest element size e (2..64) whose pattern repeats through all of x
507fn a6_bmsz x ulong to ulong
508e = ulong 64
509go = true
510while and go gt e 2 bound 6
511h = shr e 1
512m = sub shl ulong 1 h 1
513if eq and x m and shr x h m
514e = h
515else
516go = false
517end
518end
519ret e
520end
521
522fn a6_popc x ulong e ulong to ulong
523k = ulong 0
524i = ulong 0
525while lt i e bound 64
526k = add k and shr x i 1
527i = add i 1
528end
529ret k
530end
531
532fn a6_bmenc v ulong w ulong to ulong
533x = v
534if eq w 32
535x = and v 0xffffffff
536x = or x shl x 32
537end
538if or eq x 0 eq x 18446744073709551615
539ret 65536
540end
541e = a6_bmsz x
542if and eq w 32 eq e 64
543ret 65536
544end
545m = ulong 18446744073709551615
546if lt e 64
547m = sub shl ulong 1 e 1
548end
549el = and x m
550k = a6_popc el e
551ones = sub shl ulong 1 k 1
552r = ulong 0
553found = e
554while lt r e bound 64
555if eq a6_rotl el r e ones
556found = r
557r = e
558else
559r = add r 1
560end
561end
562if eq found e
563ret 65536
564end
565ret a6_bmpk e found k
566end
567
568note a6_bmpk: N:immr:imms for element size e, rotation r, k ones
569fn a6_bmpk e ulong r ulong k ulong to ulong
570if eq e 64
571ret or or shl ulong 1 12 shl r 6 sub k 1
572end
573pre = and 0x3f not sub shl e 1 1
574ret or shl r 6 or pre sub k 1
575end
576
577note a6_poff: a signed byte offset (multiple of 8, -512 .. 504) as the 7-bit imm7 field
578fn a6_poff off ulong to ulong
579if ne and off 7 0
580a6_die 5 6 off
581end
582q = shr off 3
583if ne shr off 63 0
584q = or q 0xe000000000000000
585if lt q 0xffffffffffffffc0
586a6_die 5 6 off
587end
588else
589if gt q 63
590a6_die 5 6 off
591end
592end
593ret and q 127
594end
595
596note a6_rdn: base | d | n << 5 (both 5-bit fields)
597fn a6_rdn b ulong d ulong n ulong to ulong
598ret or or b a6_fld d 5 shl a6_fld n 5 5
599end
600
601fn a6_enca k ulong b ulong x ulong y ulong z ulong u ulong to ulong
602note kinds 1..10 (a6_enc splits the table so each part stays small)
603if eq k 1
604ret or or a6_rdn b x y shl a6_fld z 5 16 shl a6_fld u 6 10
605end
606if eq k 4
607ret or or a6_rdn b x y shl a6_fld z 12 10 shl a6_fld u 1 22
608end
609if eq k 5
610c = a6_bmenc z shl ulong 32 shr b 31
611if eq c 65536
612a6_die 5 6 z
613end
614ret or a6_rdn b x y shl c 10
615end
616if eq k 6
617ret or or or b a6_fld x 5 shl a6_fld y 16 5 shl a6_fld z 2 21
618end
619if eq k 7
620ret or or a6_rdn b x y shl a6_fld z 6 16 shl a6_fld u 6 10
621end
622if eq k 9
623ret or a6_rdn b x y shl a6_fld z 12 10
624end
625if eq k 10
626ret or or a6_rdn or sub b 0x01000000 0x00206800 x y shl a6_fld z 5 16 shl a6_fld u 1 12
627end
628a6_die 5 5 k
629ret 0
630end
631
632note a6_encbr: kinds 12..18 (branches, BR, SVC, ADR)
633fn a6_encbr k ulong bs ulong a ulong b ulong c ulong to ulong
634if eq k 12
635ret or bs a6_dsig a 26
636end
637if eq k 13
638ret or or bs shl a6_dsig b 19 5 a6_fld a 4
639end
640if eq k 14
641ret or or bs shl a6_dsig b 19 5 a6_fld a 5
642end
643if eq k 15
644x = or or bs shl shr a6_fld b 6 5 31 shl and b 31 19
645ret or or x shl a6_dsig c 14 5 a6_fld a 5
646end
647if eq k 16
648ret or bs shl a6_fld a 5 5
649end
650if eq k 17
651ret or bs shl a6_fld a 16 5
652end
653y = a6_dsig b 21
654lo = shl and y 3 29
655hi = shl shr y 2 5
656ret or or or bs lo hi a6_fld a 5
657end
658
659note a6_rsv k bs c d e: refuse (a6_die 5 9) in-field operand values whose encoding Arm ARM DDI 0487 M.d leaves
660note UNDEFINED or unallocated. A 32-bit form (sf, bit 31 of the base, clear) takes shift amounts, BFM immr and imms
661note and EXTR lsb 0..31 and MOVZ/MOVN hw 0..1. Kind 1 shifted register: logical (base bits 28..24 = 01010, C6.2.14)
662note takes sh 0..3; add/sub (01011, C6.2.5) reserves sh 3; every other kind-1 form (multiply, two-source, float)
663note has opcode bits where sh and amt go, so both must be 0. Operands as a6_enc passes them: kind 1 sh d, amt e;
664note kind 6 hw c; kind 7 immr c, imms d; kind 21 lsb d.
665fn a6_rsv k ulong bs ulong c ulong d ulong e ulong to void
666w32 = eq shr bs 31 0
667if eq k 1
668cl = and shr bs 24 31
669if and ne cl 10 ne cl 11
670if ne or d e 0
671a6_die 5 9 or d e
672end
673end
674if and eq cl 11 eq d 3
675a6_die 5 9 d
676end
677if and w32 ge e 32
678a6_die 5 9 e
679end
680end
681if and and w32 eq k 6 ge c 2
682a6_die 5 9 c
683end
684if and and w32 or eq k 7 eq k 21 ge d 32
685a6_die 5 9 d
686end
687if and and w32 eq k 7 ge c 32
688a6_die 5 9 c
689end
690end
691
692note a6_enc f a b c d e: the instruction word of form f with operands a..e (unused operands 0)
693fn a6_enc f ulong a ulong b ulong c ulong d ulong e ulong to ulong
694if ge f get a_nform 0
695a6_die 5 5 f
696end
697k = ulong get a_fkind f
698bs = get a_fbase f
699a6_rsv k bs c d e
700if eq k 1
701x = a6_enca 1 bs a b c e
702ret or x shl a6_fld d 2 22
703end
704if eq k 3
705x = or a6_rdn bs a b shl a6_fld c 5 16
706ret or or x shl a6_fld d 3 13 shl a6_fld e 3 10
707end
708if eq k 2
709ret or or a6_rdn bs a b shl a6_fld c 5 16 shl a6_fld d 5 10
710end
711if eq k 8
712ret or or a6_rdn bs a b shl a6_fld c 5 16 shl a6_fld d 4 12
713end
714if eq k 11
715ret or or a6_rdn bs a c shl a6_fld b 5 10 shl a6_poff d 15
716end
717if and ge k 12 le k 18
718ret a6_encbr k bs a b c
719end
720if eq k 19
721ret a6_rdn bs a b
722end
723if eq k 20
724ret or or bs shl a6_fld a 5 5 shl a6_fld b 5 16
725end
726if eq k 21
727ret or or a6_rdn bs a b shl a6_fld c 5 16 shl a6_fld d 6 10
728end
729if eq k 22
730ret or bs a6_fld a 5
731end
732if eq k 23
733ret a6_encdp bs a b c
734end
735ret a6_enca k bs a b c d
736end
737
738fn a6_encdp bs ulong a ulong b ulong c ulong to ulong
739if ne and shr bs 19 1 0
740ret or a6_rdn bs a b shl a6_fld c 1 20
741end
742ret or a6_rdn bs a b shl a6_fld c 2 19
743end
744
745note a6_ew: emit the word of form f
746fn a6_ew f ulong a ulong b ulong c ulong d ulong e ulong to void
747a6_emitw a6_enc f a b c d e
748end
749
750note ======== in-band mode `a64 enc`: one word per case line `NAME a b c d e | expected` ========
751note Lines that are blank or start with `;` are skipped. Operands are decimal (a leading - makes the two's
752note complement) or lowercase 0x hex. A line that names no form or has a bad operand is refused (a6_die 5 4 line).
753global a_cv ulong 8
754
755fn a6_isws c ulong to bool
756ret or eq c 32 eq c 9
757end
758
759note a6_samew: a_fnam[i .. i+n) equals i_in[s .. s+n)
760fn a6_samew i ulong s ulong n ulong to bool
761q = ulong 0
762while lt q n bound 64
763if ne get a_fnam add i q ar_ib add s q
764ret false
765end
766q = add q 1
767end
768ret true
769end
770
771note a6_innm: byte j of a_fnam exists and is part of a name (and/or are eager, so the bound is tested here)
772fn a6_innm j ulong to bool
773if ge j len a_fnam
774ret false
775end
776ret ne get a_fnam j 32
777end
778
779note a6_fname: the form id whose name in a_fnam equals i_in[s .. s+n), or a_nform when there is none
780fn a6_fname s ulong n ulong to ulong
781i = ulong 0
782id = ulong 0
783ln = len a_fnam
784while lt i ln bound 4096
785j = i
786while a6_innm j bound 64
787j = add j 1
788end
789if eq sub j i n
790if a6_samew i s n
791ret id
792end
793end
794id = add id 1
795i = add j 1
796end
797ret get a_nform 0
798end
799
800note a6_numtk: the number at i_in[s .. e), into a_cv[7]; false if malformed
801fn a6_numtk s ulong e ulong to bool
802p = s
803ng = false
804if eq ar_ib p 45
805ng = true
806p = add p 1
807end
808if ge p e
809ret false
810end
811v = ulong 0
812hex = false
813if lt add p 1 e
814if and eq ar_ib p 48 eq ar_ib add p 1 120
815hex = true
816p = add p 2
817end
818end
819while lt p e bound 64
820c = ulong ar_ib p
821dg = ulong 99
822if and ge c 48 le c 57
823dg = sub c 48
824end
825if and hex and ge c 97 le c 102
826dg = sub c 87
827end
828if and hex ge dg 16
829ret false
830end
831if ge dg 10
832if not hex
833ret false
834end
835end
836if hex
837v = or shl v 4 dg
838else
839v = add mul v 10 dg
840end
841p = add p 1
842end
843if ng
844v = sub 0 v
845end
846put a_cv 7 v
847ret true
848end
849
850note a6_skipws: the first byte at or after p (before e) that is not a blank
851fn a6_skipws p ulong e ulong to ulong
852q = p
853while and lt q e a6_isws ulong ar_ib q bound 100000
854q = add q 1
855end
856ret q
857end
858
859note a6_tokend: the end of the operand token at p (a blank, `|` or e)
860fn a6_tokend p ulong e ulong to ulong
861q = p
862while lt q e bound 64
863c = ulong ar_ib q
864if or a6_isws c eq c 124
865ret q
866end
867q = add q 1
868end
869ret q
870end
871
872note a6_encln: one case line i_in[s .. e) (line number ln): the word for it
873fn a6_encln s ulong e ulong ln ulong to void
874p = a6_tokend s e
875f = a6_fname s sub p s
876if ge f get a_nform 0
877a6_die 5 4 ln
878end
879k = ulong 0
880while lt k 5 bound 5
881put a_cv k 0
882k = add k 1
883end
884k = ulong 0
885go = true
886while go bound 16
887p = a6_skipws p e
888if or ge p e eq ar_ib p 124
889go = false
890else
891q = a6_tokend p e
892if or ge k 5 not a6_numtk p q
893a6_die 5 4 ln
894end
895put a_cv k get a_cv 7
896k = add k 1
897p = q
898end
899end
900a6_ewcv f
901end
902
903note a6_ewcv: emit form f with the operands in a_cv[0..4]
904fn a6_ewcv f ulong to void
905a = get a_cv 0
906b = get a_cv 1
907c = get a_cv 2
908d = get a_cv 3
909e = get a_cv 4
910a6_ew f a b c d e
911end
912
913note a6_enctest: one word per case line of the payload; ln numbers lines as in the case file, whose line 1 is the
914note mode line main read apart
915fn a6_enctest to void
916n = get a_inlen 0
917p = ulong 0
918ln = ulong 1
919while lt p n bound 1000000
920ln = add ln 1
921s = p
922while and lt p n ne ar_ib p 10 bound 100000
923p = add p 1
924end
925q = s
926while and lt q p a6_isws ulong ar_ib q bound 100000
927q = add q 1
928end
929if lt q p
930if ne ar_ib q 59
931a6_encln q p ln
932end
933end
934p = add p 1
935end
936a6_flush
937exit 0
938end
939
940
941note ======== back end: ΩIR tables (ir.om) -> AArch64 ELF (seed/ng/a64/DESIGN.md) ========
942note Slice scope: integer scalar functions and exports (the bench-kernel subset grows from here). Allocated registers:
943note x0..x14 (caller-saved) and x19..x27 (callee-saved); scratch x15, x16, x17; x28 holds the stack floor (omc's map:
944note stack 0x10000000..0x10800000, sp starts at the top); x18 retains the Linux entry stack for env words, x29 unused; x30 link. Arguments x0..x7, result x0.
945note A narrow integer lives in its register extended to 64 bits by its type (zero: u8..u32, sign: i8..i32), so 64-bit
946note compares are exact and arithmetic followed by re-extension gives ΩIR's modulo 2^w result (IR.md s7.3).
947note Positions: function parameters are defined at 0; block x starts at a_bst[x] (its parameters are defined there),
948note op o is at a_opos[o] (its operands are read there, its result is defined at a_opos[o] + 1).
949note An interval is one range [a_vs, a_ve] per value, widened over every block where the value is live in or out.
950
951note The per-wire tables (a_vh .. a_nu), the liveness bitsets (a_lin .. a_ldef, blocks x words), the call sites
952note (a_cpc, a_cft) and the branch fixups (a_xpc, a_xfm, a_xa, a_xl: regions 108..111) are arena groups a64val,
953note a64live, a64call and a64fix (src/ir.om ar_*; no-caps 2026-11-11), grown per function and bounded by the arena
954note only: a full arena is `ng: arena full` with the ng-use record, exit 14. They were fixed at 262144 wires and
955note 2097152 liveness words per function and 1048576 call sites (a6_unsup), which calls_524289 (one function,
956note 1048591 wires) passed, and at 262144 fixups per function (a6_unsup), which 270000 checked adds in one function
957note passed. Blocks and positions per function stay IR.md s12's.
958const a_maxb ulong 65536
959const a_maxp ulong 4194304
960
961note a_vln[w] = the length wire of slice value wire w, plus 1 (0: not a slice); a_mal the machine argument list of
962note one call or of the function's parameters (a6_xcall, a6_xpar), a_man its length
963global a_mal ulong 136
964note a_mac: the class of machine argument k (1: f32/f64, in a V register; 0: integer, reference or length wire);
965note a_mab: its ABI place from a6_abi (register location, or 1000 + outgoing slot)
966global a_mac uchar 136
967global a_mab ulong 136
968note a_vf[w] = 1 when wire w holds an f32/f64 value (allocated from V registers, locations 32..63)
969global a_man ulong 1
970global a_bh ulong 65536
971global a_bst ulong 65536
972global a_ben ulong 65536
973global a_bpc ulong 65536
974global a_opos ulong 4194305
975global a_ccnt ulong 4194305
976global a_phd ulong 4194305
977note a_fs (per function): 0 values, 1 blocks, 2 bitset words per block, 3 positions, 4 spill slots, 5 callee-saved
978note mask, 6 has a call, 7 frame bytes, 8 function, 9 liveness changed, 10 trap codes used (mask), 11 saved count,
979note 12 fixups, 13 current block index, 14 E tail sites (0: no accumulator), 15 code offset of the E loop head
980global a_fs ulong 17
981note fusion: use counts by wire; a_skip 0 is the one consumer (cbr or select) a fused compare emitted at its own site,
982note skipped at the consumer's turn in a6_sel after a6_sel's refusals.
983global a_skip ulong 1
984note a_sform: 0 save pairs (STP/LDP, variant A), 1 separate STR/LDR (variant B); `@ng a64 split` selects 1
985global a_sform ulong 1
986note a_tgt: 0 static AArch64 Linux ELF (`@ng a64`, `@ng a64 split`), 1 dynamic arm64 Mach-O for macOS (`@ng a64
987note macho`, save pairs). Mach-O code is position independent: the start code derives the stack floor x28 from
988note its own address (ADR), and a global's address is x28 plus (class 0) or minus (class 1) a constant.
989global a_tgt ulong 1
990note a_bks: the image offset of the sixteen callback entries (a6_back), 0 when the unit has no callback function
991global a_bks ulong 1
992global a_ract ulong 64
993global a_svl ulong 32
994global a_ncl ulong 1
995global a_fpc ulong 65537
996global a_tpc ulong 16
997global a_mvd ulong 64
998global a_mvs ulong 64
999global a_mvn ulong 1
1000note E loop shape: tail call sites (call ops), accumulator wire and type
1001global a_tco ulong 64
1002global a_taw ulong 1
1003global a_tat ulong 1
1004note memory: alloca ops of the function with their frame offset and zero-filled size (16-byte multiple); global
1005note handle -> absolute address (class 0 in the RW pool from 0x10800000, class 1 in the code image); pool bytes
1006global a_alo ulong 64
1007global a_aof ulong 64
1008global a_asz ulong 64
1009global a_anum ulong 1
1010global a_gtot ulong 1
1011
1012const a_rorvx ulong 28
1013const a_lsrvw ulong 30
1014const a_asrvw ulong 31
1015const a_rorvw ulong 159
1016const a_tbnz ulong 111
1017const a_lsbx ulong 82
1018const a_strhr ulong 83
1019const a_ldrhr ulong 84
1020const a_lshx ulong 85
1021const a_strwr ulong 86
1022const a_ldrwr ulong 87
1023const a_lsw ulong 88
1024const a_strxr ulong 89
1025const a_stpxpo ulong 99
1026
1027note ---- small accessors ----
1028fn a6_wi v ulong to ulong
1029ret ulong get i_vno v
1030end
1031
1032fn a6_istk v ulong to bool
1033ret eq get i_vty v 20
1034end
1035
1036fn a6_arg o ulong k ulong to ulong
1037ret ulong get i_arg add ulong get i_oa0 o k
1038end
1039
1040fn a6_bix b ulong to ulong
1041ret ulong get i_bix b
1042end
1043
1044fn a6_vty v ulong to ulong
1045ret ulong get i_vty v
1046end
1047
1048fn a6_unsup k ulong to void
1049a6_die 5 5 k
1050end
1051
1052note bit width of an integer type id (bool 1); 0 for anything else
1053fn a6_wid t ulong to ulong
1054if or eq t 2 eq t 6
1055ret 8
1056end
1057if or eq t 3 eq t 7
1058ret 16
1059end
1060if or eq t 4 eq t 8
1061ret 32
1062end
1063if or eq t 5 eq t 9
1064ret 64
1065end
1066if eq t 1
1067ret 1
1068end
1069ret 0
1070end
1071
1072fn a6_sgn t ulong to bool
1073ret and ge t 6 le t 9
1074end
1075
1076note the value types this slice handles: bool and the integers
1077fn a6_isint t ulong to bool
1078ret and ge t 1 le t 9
1079end
1080
1081note ---- bit sets: k 0 live-in, 1 live-out, 2 upward-exposed uses, 3 definitions; block x, value w ----
1082fn a6_lget k ulong i ulong to ulong
1083if eq k 0
1084ret get ar_m add get ar_rb 101 i
1085end
1086if eq k 1
1087ret get ar_m add get ar_rb 102 i
1088end
1089if eq k 2
1090ret get ar_m add get ar_rb 103 i
1091end
1092ret get ar_m add get ar_rb 104 i
1093end
1094
1095fn a6_lset k ulong i ulong v ulong to void
1096if eq k 0
1097put ar_m add get ar_rb 101 i v
1098end
1099if eq k 1
1100put ar_m add get ar_rb 102 i v
1101end
1102if eq k 2
1103put ar_m add get ar_rb 103 i v
1104end
1105if eq k 3
1106put ar_m add get ar_rb 104 i v
1107end
1108end
1109
1110fn a6_bit k ulong x ulong w ulong to bool
1111i = add mul x get a_fs 2 shr w 6
1112ret ne and shr a6_lget k i and w 63 1 0
1113end
1114
1115fn a6_bset k ulong x ulong w ulong to void
1116i = add mul x get a_fs 2 shr w 6
1117a6_lset k i or a6_lget k i shl ulong 1 and w 63
1118end
1119
1120note ---- wires, layout, calls ----
1121note a6_wmap f: a_vh[wire] = value handle for every value of f (the order of ir_num)
1122fn a6_wmap f ulong to void
1123v = ulong get i_ftk f
1124put ar_m add get ar_rb 91 a6_wi v v
1125v = ulong get i_fp1 f
1126while ne v 0 bound 65
1127put ar_m add get ar_rb 91 a6_wi v v
1128v = ulong get i_vnx v
1129end
1130b = ulong get i_fb1 f
1131while ne b 0 bound 65537
1132a6_wmb b
1133b = ulong get i_bnx b
1134end
1135end
1136
1137fn a6_wmb b ulong to void
1138v = ulong get i_bp1 b
1139while ne v 0 bound 65
1140put ar_m add get ar_rb 91 a6_wi v v
1141v = ulong get i_vnx v
1142end
1143o = ulong get i_bo1 b
1144while ne o 0 bound 4194305
1145t = ulong get i_otk o
1146if ne t 0
1147put ar_m add get ar_rb 91 a6_wi t t
1148end
1149r = ulong get i_ors o
1150if ne r 0
1151put ar_m add get ar_rb 91 a6_wi r r
1152end
1153o = ulong get i_onx o
1154end
1155end
1156
1157note a6_lay f: block index tables and positions (a_fs 1 blocks, 3 positions)
1158fn a6_lay f ulong to void
1159nb = ulong 0
1160p = ulong 2
1161b = ulong get i_fb1 f
1162while ne b 0 bound 65537
1163put a_bh nb b
1164put a_bst nb p
1165p = add p 2
1166o = ulong get i_bo1 b
1167while ne o 0 bound 4194305
1168if ge p sub a_maxp 4
1169a6_unsup p
1170end
1171put a_opos o p
1172p = add p 2
1173o = ulong get i_onx o
1174end
1175put a_ben nb sub p 1
1176nb = add nb 1
1177b = ulong get i_bnx b
1178end
1179put a_fs 1 nb
1180put a_fs 3 p
1181end
1182
1183note a6_ccalls: a_ccnt[q] = the number of calls and host words at positions < q, for q in 0..positions; a_fs 6 =
1184note any call (x30 is saved). A host word's SVC clobbers x0 only, but its arguments go to x0..x2, so values live
1185note across it take callee-saved registers like values live across a call.
1186fn a6_ccalls to void
1187np = get a_fs 3
1188q = ulong 0
1189while le q np bound 4194305
1190put a_ccnt q 0
1191q = add q 1
1192end
1193put a_fs 6 0
1194x = ulong 0
1195while lt x get a_fs 1 bound 65536
1196o = ulong get i_bo1 get a_bh x
1197while ne o 0 bound 4194305
1198if and eq get i_oop o 64 not a6_istc o
1199put a_ccnt get a_opos o 1
1200put a_fs 6 1
1201end
1202if a6_hostcall ulong get i_oop o
1203put a_ccnt get a_opos o 1
1204end
1205if eq get i_oop o 111
1206put a_fs 6 1
1207end
1208o = ulong get i_onx o
1209end
1210x = add x 1
1211end
1212a6_cpre np
1213end
1214
1215fn a6_cpre np ulong to void
1216run = ulong 0
1217q = ulong 0
1218while le q np bound 4194305
1219t = get a_ccnt q
1220put a_ccnt q run
1221run = add run t
1222q = add q 1
1223end
1224end
1225
1226note ---- liveness (SSA with block parameters) ----
1227fn a6_lzero to void
1228n = mul get a_fs 1 get a_fs 2
1229i = ulong 0
1230while lt i n bound 16777217
1231put ar_m add get ar_rb 101 i 0
1232put ar_m add get ar_rb 102 i 0
1233put ar_m add get ar_rb 103 i 0
1234put ar_m add get ar_rb 104 i 0
1235i = add i 1
1236end
1237end
1238
1239note a6_lgen: per block the uses not defined earlier in it (2) and its definitions (3)
1240fn a6_lgen to void
1241x = ulong 0
1242while lt x get a_fs 1 bound 65536
1243b = get a_bh x
1244v = ulong get i_bp1 b
1245while ne v 0 bound 65
1246if not a6_istk v
1247a6_bset 3 x a6_wi v
1248end
1249v = ulong get i_vnx v
1250end
1251o = ulong get i_bo1 b
1252while ne o 0 bound 4194305
1253a6_lgo x o
1254o = ulong get i_onx o
1255end
1256x = add x 1
1257end
1258end
1259
1260fn a6_lgo x ulong o ulong to void
1261op = get i_oop o
1262n = ulong get i_ona o
1263k = ulong 0
1264while lt k n bound 1048576
1265if eq ir_okd uint op k 0
1266v = a6_arg o k
1267if not a6_istk v
1268w = a6_wi v
1269if not a6_bit 3 x w
1270a6_bset 2 x w
1271end
1272end
1273end
1274k = add k 1
1275end
1276r = ulong get i_ors o
1277if ne r 0
1278if not a6_istk r
1279a6_bset 3 x a6_wi r
1280end
1281end
1282end
1283
1284note word i of the union of the live-in sets of block b's successors (terminator t)
1285fn a6_succw t ulong i ulong to ulong
1286op = get i_oop t
1287lw = get a_fs 2
1288if eq op 60
1289s = a6_bix a6_arg t 0
1290ret get ar_m add get ar_rb 101 add mul s lw i
1291end
1292if eq op 61
1293s1 = a6_bix a6_arg t 2
1294s2 = a6_bix a6_arg t 3
1295ret or ar_w 101 add mul s1 lw i get ar_m add get ar_rb 101 add mul s2 lw i
1296end
1297ret 0
1298end
1299
1300fn a6_lblk x ulong to void
1301lw = get a_fs 2
1302t = ulong get i_bot get a_bh x
1303i = ulong 0
1304while lt i lw bound 16777217
1305j = add mul x lw i
1306so = a6_succw t i
1307put ar_m add get ar_rb 102 j so
1308nin = or ar_w 103 j and so not ar_w 104 j
1309if ne nin get ar_m add get ar_rb 101 j
1310put ar_m add get ar_rb 101 j nin
1311put a_fs 9 1
1312end
1313i = add i 1
1314end
1315end
1316
1317fn a6_lflow to void
1318go = true
1319while go bound 100000
1320put a_fs 9 0
1321x = get a_fs 1
1322while gt x 0 bound 65537
1323x = sub x 1
1324a6_lblk x
1325end
1326go = ne get a_fs 9 0
1327end
1328end
1329
1330note ---- intervals ----
1331fn a6_dpos v ulong to ulong
1332k = get i_vk v
1333if le k 1
1334ret 0
1335end
1336if eq k 2
1337ret get a_bst a6_bix ulong get i_vo v
1338end
1339ret add get a_opos ulong get i_vo v 1
1340end
1341
1342fn a6_ivinit to void
1343w = ulong 0
1344while lt w get a_fs 0 bound 16777217
1345v = get ar_m add get ar_rb 91 w
1346put ar_m add get ar_rb 96 w 0
1347ar_pc 94 w 0
1348ar_pc 99 w 0
1349if a6_istk v
1350ar_pc 95 w 254
1351put ar_m add get ar_rb 92 w 0
1352put ar_m add get ar_rb 93 w 0
1353else
1354p = a6_dpos v
1355ar_pc 95 w 255
1356put ar_m add get ar_rb 92 w p
1357put ar_m add get ar_rb 93 w p
1358if a6_isfv v
1359ar_pc 99 w 1
1360end
1361if eq get i_tk a6_vty v 1
1362if not a6_isvv v
1363a6_unsup a6_vty v
1364end
1365ar_pc 99 w 2
1366end
1367end
1368w = add w 1
1369end
1370end
1371
1372fn a6_ext w ulong p ulong to void
1373if lt p get ar_m add get ar_rb 92 w
1374put ar_m add get ar_rb 92 w p
1375end
1376if gt p get ar_m add get ar_rb 93 w
1377put ar_m add get ar_rb 93 w p
1378end
1379end
1380
1381note a6_ivuse: every non-token operand use extends its value to the op's position
1382fn a6_ivuse to void
1383x = ulong 0
1384while lt x get a_fs 1 bound 65536
1385o = ulong get i_bo1 get a_bh x
1386while ne o 0 bound 4194305
1387a6_ivop o
1388o = ulong get i_onx o
1389end
1390x = add x 1
1391end
1392end
1393
1394fn a6_ivop o ulong to void
1395op = get i_oop o
1396n = ulong get i_ona o
1397p = get a_opos o
1398k = ulong 0
1399while lt k n bound 1048576
1400if eq ir_okd uint op k 0
1401v = a6_arg o k
1402if not a6_istk v
1403a6_ext a6_wi v p
1404end
1405end
1406k = add k 1
1407end
1408end
1409
1410note a6_ivlive: live-in values cover their block's start, live-out values its end
1411fn a6_ivlive to void
1412x = ulong 0
1413lw = get a_fs 2
1414while lt x get a_fs 1 bound 65536
1415i = ulong 0
1416while lt i lw bound 16777217
1417a6_ivw x i get ar_m add get ar_rb 101 add mul x lw i get a_bst x
1418a6_ivw x i get ar_m add get ar_rb 102 add mul x lw i get a_ben x
1419i = add i 1
1420end
1421x = add x 1
1422end
1423end
1424
1425fn a6_ivw x ulong i ulong m ulong p ulong to void
1426if eq m 0
1427ret
1428end
1429j = ulong 0
1430while lt j 64 bound 64
1431if ne and shr m j 1 0
1432a6_ext add shl i 6 j p
1433end
1434j = add j 1
1435end
1436end
1437
1438note a6_ivcross: a_vx[w] = 1 when a call sits strictly inside w's interval
1439fn a6_ivcross to void
1440w = ulong 0
1441while lt w get a_fs 0 bound 16777217
1442s = get ar_m add get ar_rb 92 w
1443e = get ar_m add get ar_rb 93 w
1444if and eq uchar get ar_m add get ar_rb 95 w 255 gt e add s 1
1445if gt get a_ccnt e get a_ccnt add s 1
1446ar_pc 94 w 1
1447end
1448end
1449w = add w 1
1450end
1451end
1452
1453note ---- linear scan (Poletto and Sarkar), intervals in start order ----
1454fn a6_scan to void
1455np = get a_fs 3
1456q = ulong 0
1457while le q np bound 4194305
1458put a_phd q 0
1459q = add q 1
1460end
1461w = get a_fs 0
1462while gt w 0 bound 16777217
1463w = sub w 1
1464if ne uchar get ar_m add get ar_rb 95 w 254
1465s = get ar_m add get ar_rb 92 w
1466put ar_m add get ar_rb 97 w get a_phd s
1467put a_phd s add w 1
1468end
1469end
1470r = ulong 0
1471while lt r 64 bound 64
1472put a_ract r 0
1473r = add r 1
1474end
1475put a_fs 4 a6_outm
1476put a_fs 5 0
1477q = ulong 0
1478while le q np bound 4194305
1479h = get a_phd q
1480while ne h 0 bound 16777217
1481a6_alloc sub h 1
1482h = get ar_m add get ar_rb 97 sub h 1
1483end
1484q = add q 1
1485end
1486end
1487
1488fn a6_expire s ulong to void
1489r = ulong 0
1490while lt r 64 bound 64
1491a = get a_ract r
1492if ne a 0
1493if lt get ar_m add get ar_rb 93 sub a 1 s
1494put a_ract r 0
1495end
1496end
1497r = add r 1
1498end
1499end
1500
1501note a6_free x: a free register, caller-saved first unless the interval crosses a call (x); 255 if none
1502fn a6_free x bool to ulong
1503if not x
1504r = ulong 0
1505while lt r 15 bound 15
1506if eq get a_ract r 0
1507ret r
1508end
1509r = add r 1
1510end
1511end
1512r = ulong 19
1513while lt r 28 bound 9
1514if eq get a_ract r 0
1515ret r
1516end
1517r = add r 1
1518end
1519ret 255
1520end
1521
1522note a6_ffree x: a free V register (location 32 + V) for an f32/f64 interval: v0..v7 and v16..v28 (caller-saved)
1523note first unless it crosses a call (x), then d8..d15 (callee-saved: the low 64 bits are preserved, all a scalar
1524note needs); v29..v31 are the float scratch and never allocated; 255 if none
1525fn a6_ffree x bool to ulong
1526if not x
1527r = ulong 32
1528while lt r 61 bound 29
1529if or lt r 40 gt r 47
1530if eq get a_ract r 0
1531ret r
1532end
1533end
1534r = add r 1
1535end
1536end
1537r = ulong 40
1538while lt r 48 bound 8
1539if eq get a_ract r 0
1540ret r
1541end
1542r = add r 1
1543end
1544ret 255
1545end
1546
1547fn a6_give w ulong r ulong to void
1548ar_pc 95 w uchar r
1549put a_ract r add w 1
1550if ge r 19
1551if or lt r 28 and ge r 40 lt r 48
1552put a_fs 5 or get a_fs 5 shl ulong 1 r
1553end
1554end
1555end
1556
1557fn a6_slot w ulong to void
1558n = get a_fs 4
1559if eq uchar get ar_m add get ar_rb 99 w 2
1560n = and add n 1 not ulong 1
1561put ar_m add get ar_rb 96 w add n 1
1562put a_fs 4 add n 2
1563ret
1564end
1565put ar_m add get ar_rb 96 w add n 1
1566put a_fs 4 add n 1
1567end
1568
1569fn a6_alloc w ulong to void
1570a6_expire get ar_m add get ar_rb 92 w
1571x = ne uchar get ar_m add get ar_rb 94 w 0
1572if and x eq uchar get ar_m add get ar_rb 99 w 2
1573a6_slot w
1574ret
1575end
1576fl = ne uchar get ar_m add get ar_rb 99 w 0
1577r = ulong 255
1578if fl
1579r = a6_ffree x
1580else
1581r = a6_free x
1582end
1583if ne r 255
1584a6_give w r
1585ret
1586end
1587a6_spill w x fl
1588end
1589
1590note a6_spill w x: w found no free register of its class (x: callee-saved only). The active interval of that class
1591note ending last keeps its register when it ends no later than w (w gets a stack slot); otherwise it moves to a
1592note stack slot and w takes its register. The class is the integer registers, or the V registers when fl.
1593fn a6_spill w ulong x bool fl bool to void
1594best = ulong 255
1595bend = ulong 0
1596r = ulong 0
1597rh = ulong 28
1598if fl
1599r = 32
1600rh = 61
1601end
1602while lt r rh bound 61
1603if a6_okreg r x
1604a = get a_ract r
1605if ne a 0
1606e = get ar_m add get ar_rb 93 sub a 1
1607if or eq best 255 gt e bend
1608best = r
1609bend = e
1610end
1611end
1612end
1613r = add r 1
1614end
1615if and ne best 255 gt bend get ar_m add get ar_rb 93 w
1616v = sub get a_ract best 1
1617ar_pc 95 v 255
1618a6_slot v
1619a6_give w best
1620ret
1621end
1622a6_slot w
1623end
1624
1625fn a6_okreg r ulong x bool to bool
1626if ge r 32
1627if and ge r 40 le r 47
1628ret true
1629end
1630ret not x
1631end
1632if and ge r 19 le r 27
1633ret true
1634end
1635ret and not x lt r 15
1636end
1637
1638note ---- frame: spill slots at [sp, 8k], then the saved registers (callee-saved in use, x30 if any call), both
1639note below 4096 bytes; then the alloca areas (16-byte aligned); the whole frame below 2^24 ----
1640fn a6_frame to void
1641n = ulong 0
1642r = ulong 19
1643while lt r 28 bound 9
1644if ne and shr get a_fs 5 r 1 0
1645put a_svl n r
1646n = add n 1
1647end
1648r = add r 1
1649end
1650if ne get a_fs 6 0
1651put a_svl n 30
1652n = add n 1
1653end
1654r = ulong 40
1655while lt r 48 bound 8
1656if ne and shr get a_fs 5 r 1 0
1657put a_svl n r
1658n = add n 1
1659end
1660r = add r 1
1661end
1662put a_fs 11 n
1663f = mul 8 add get a_fs 4 n
1664f = and add f 15 not ulong 15
1665if gt f 4095
1666a6_unsup f
1667end
1668f = a6_alay f
1669if ge f 16777216
1670a6_unsup f
1671end
1672put a_fs 7 f
1673end
1674
1675note a6_alay b: give every alloca of the function an area from frame offset b up; returns the frame size
1676fn a6_alay b ulong to ulong
1677put a_anum 0 0
1678f = b
1679x = ulong 0
1680while lt x get a_fs 1 bound 65536
1681o = ulong get i_bo1 get a_bh x
1682while ne o 0 bound 4194305
1683if eq get i_oop o 50
1684f = a6_alo1 o f
1685end
1686o = ulong get i_onx o
1687end
1688x = add x 1
1689end
1690ret f
1691end
1692
1693fn a6_alo1 o ulong f ulong to ulong
1694k = get a_anum 0
1695if ge k len a_alo
1696a6_unsup 50
1697end
1698t = ulong get i_oty o
1699if ne get i_tk t 4
1700a6_unsup t
1701end
1702s = ir_tsize get i_t1 t
1703s = and add s 15 not ulong 15
1704if ge s 16777216
1705a6_unsup s
1706end
1707put a_alo k o
1708put a_aof k f
1709put a_asz k s
1710put a_anum 0 add k 1
1711ret add f s
1712end
1713
1714note a6_saves st: store (st) or reload the saved registers, as pairs (a_sform 0) or one by one (1); a pair is two
1715note registers of one class (STP/LDP X or D: x19..x27 and x30 come first, d8..d15 after them)
1716fn a6_saves st bool to void
1717n = get a_fs 11
1718base = mul 8 get a_fs 4
1719pair = and eq get a_sform 0 0 le add base mul 8 n 504
1720j = ulong 0
1721if pair
1722while lt add j 1 n bound 32
1723r1 = get a_svl j
1724r2 = get a_svl add j 1
1725if eq a6_rcls r1 a6_rcls r2
1726a6_pr st r1 r2 add base mul 8 j
1727j = add j 2
1728else
1729a6_sr st r1 add shr base 3 j
1730j = add j 1
1731end
1732end
1733end
1734while lt j n bound 32
1735a6_sr st get a_svl j add shr base 3 j
1736j = add j 1
1737end
1738end
1739
1740note a6_rcls r: the class of a saved register location: 0 integer, 1 V (location 32 + V)
1741fn a6_rcls r ulong to ulong
1742if ge r 32
1743ret 1
1744end
1745ret 0
1746end
1747
1748fn a6_pr st bool r1 ulong r2 ulong off ulong to void
1749if ge r1 32
1750if st
1751a6_ew a_stpd sub r1 32 sub r2 32 31 off 0
1752else
1753a6_ew a_ldpd sub r1 32 sub r2 32 31 off 0
1754end
1755ret
1756end
1757if st
1758a6_ew a_stpx r1 r2 31 off 0
1759else
1760a6_ew a_ldpx r1 r2 31 off 0
1761end
1762end
1763
1764fn a6_sr st bool r ulong k ulong to void
1765if ge r 32
1766if st
1767a6_ew a_strd sub r 32 31 k 0 0
1768else
1769a6_ew a_ldrd sub r 32 31 k 0 0
1770end
1771ret
1772end
1773if st
1774a6_ew a_strx r 31 k 0 0
1775else
1776a6_ew a_ldrx r 31 k 0 0
1777end
1778end
1779
1780note ---- branch fixups: a label below a_tlab is a block index, a_tlab + c the trap stub of code c. a_tlab is 2^32,
1781note above every block index (it was 1000, which a function of 1000 blocks or more reached: the block index was read
1782note as a trap code, and 1064 and up shifted by 64, trap 7; found by c2-00308's 513 nested ifs). Every trap branch
1783note names its stub as add a_tlab c: ten sites (traps 1, 3, 5 and 6, the prologue's stack check among them) still
1784note passed the literal 1000 + c after the change, read as block 1000 + c (b.cc 0, stub never marked) ----
1785const a_tlab ulong 4294967296
1786note prologue: sub sp; cmp sp, x28; b.lo trap 6 (before the first store: no write below the floor); saves
1787fn a6_pro to void
1788f = get a_fs 7
1789if eq f 0
1790ret
1791end
1792a6_spadj true f
1793a6_ew a_subsxe 31 31 28 3 0
1794a6_fx a_bcond 3 add a_tlab 6
1795a6_saves true
1796end
1797
1798fn a6_epi to void
1799f = get a_fs 7
1800if ne f 0
1801a6_saves false
1802a6_spadj false f
1803end
1804a6_ew a_ret 30 0 0 0 0
1805end
1806
1807note a6_spadj s f: SP = SP - f (s) or SP + f, f < 2^24, as #hi, LSL 12 and #lo
1808fn a6_spadj s bool f ulong to void
1809h = shr f 12
1810l = and f 4095
1811if s
1812if ne h 0
1813a6_ew a_subxi 31 31 h 1 0
1814end
1815if ne l 0
1816a6_ew a_subxi 31 31 l 0 0
1817end
1818ret
1819end
1820if ne h 0
1821a6_ew a_addxi 31 31 h 1 0
1822end
1823if ne l 0
1824a6_ew a_addxi 31 31 l 0 0
1825end
1826end
1827
1828fn a6_fx f ulong a ulong l ulong to void
1829if ne get a_fs 16 0
1830if and ge f a_bcond le f 109
1831if eq f a_bcond
1832a6_ew f xor a 1 2 0 0 0
1833else
1834a6_ew xor f 1 a 2 0 0 0
1835end
1836f = a_b
1837a = 0
1838end
1839end
1840n = get a_fs 12
1841if ne ar_need 30 n 0
1842ar_die
1843end
1844put ar_m add get ar_rb 108 n get a_cn 0
1845put ar_m add get ar_rb 109 n f
1846put ar_m add get ar_rb 110 n a
1847put ar_m add get ar_rb 111 n l
1848put a_fs 12 add n 1
1849if ge l a_tlab
1850put a_fs 10 or get a_fs 10 shl ulong 1 sub l a_tlab
1851end
1852a6_emitw 0
1853end
1854
1855note distance in words from pc to t, two's complement
1856fn a6_wd pc ulong t ulong to ulong
1857if ge t pc
1858ret div sub t pc 4
1859end
1860ret sub 0 div sub pc t 4
1861end
1862
1863fn a6_putw pc ulong w ulong to void
1864ar_bp 107 pc uchar and w 255
1865ar_bp 107 add pc 1 uchar and shr w 8 255
1866ar_bp 107 add pc 2 uchar and shr w 16 255
1867ar_bp 107 add pc 3 uchar and shr w 24 255
1868end
1869
1870note fixup i is a_xpc .. a_xl (regions 108..111) at index i; every fixup emits at least one code word below the
1871note 2^27-byte reach (a6_emitw), so a function has at most 2^25 of them: the bound below is that reach, not a capacity
1872fn a6_fixb to void
1873i = ulong 0
1874while lt i get a_fs 12 bound 33554433
1875l = get ar_m add get ar_rb 111 i
1876t = ulong 0
1877if ge l a_tlab
1878t = get a_tpc sub l a_tlab
1879else
1880t = get a_bpc l
1881end
1882a6_fix1 i t
1883i = add i 1
1884end
1885end
1886
1887fn a6_fix1 i ulong t ulong to void
1888pc = get ar_m add get ar_rb 108 i
1889f = get ar_m add get ar_rb 109 i
1890d = a6_wd pc t
1891if or eq f 110 eq f 111
1892a = get ar_m add get ar_rb 110 i
1893if and ge d 8192 lt d sub ulong 0 8192
1894put a_fs 16 1
1895ret
1896end
1897a6_putw pc a6_enc f and a 31 shr a 5 d 0 0
1898ret
1899end
1900if eq f a_b
1901a6_putw pc a6_enc f d 0 0 0 0
1902else
1903if and ge d 262144 lt d sub ulong 0 262144
1904put a_fs 16 1
1905ret
1906end
1907a6_putw pc a6_enc f ar_w 110 i d 0 0 0
1908end
1909end
1910
1911note a6_exit: the exit call, status in x0: Linux exit (mov x8, #93; svc #0), or for Mach-O Darwin exit
1912note (mov x16, #1; svc #0x80)
1913fn a6_exit to void
1914if eq get a_tgt 0 1
1915a6_ew a_movzx 16 1 0 0 0
1916a6_ew a_svc 128 0 0 0 0
1917ret
1918end
1919a6_ew a_movzx 8 93 0 0 0
1920a6_ew a_svc 0 0 0 0 0
1921end
1922
1923note trap tail (omc's: add x0, x1, #100; mov x8, #93; svc #0) and one stub per used code: mov x1, #c; b tail
1924fn a6_tstubs to void
1925m = get a_fs 10
1926if eq m 0
1927ret
1928end
1929t = get a_cn 0
1930a6_ew a_addxi 0 1 100 0 0
1931a6_exit
1932c = ulong 1
1933while lt c 15 bound 14
1934if ne and shr m c 1 0
1935put a_tpc c get a_cn 0
1936a6_ew a_movzx 1 c 0 0 0
1937a6_ew a_b a6_wd get a_cn 0 t 0 0 0 0
1938end
1939c = add c 1
1940end
1941end
1942
1943note ---- values in registers: operands, results, constants, canonical extension ----
1944note a6_src v s: a register holding value v (its own, or scratch s loaded from its slot)
1945fn a6_src v ulong s ulong to ulong
1946w = a6_wi v
1947r = ulong uchar get ar_m add get ar_rb 95 w
1948if lt r 32
1949ret r
1950end
1951a6_ew a_ldrx s 31 sub get ar_m add get ar_rb 96 w 1 0 0
1952ret s
1953end
1954
1955fn a6_dst v ulong to ulong
1956r = ulong uchar get ar_m add get ar_rb 95 a6_wi v
1957if lt r 32
1958ret r
1959end
1960ret 16
1961end
1962
1963fn a6_fin v ulong r ulong to void
1964w = a6_wi v
1965if ge ulong uchar get ar_m add get ar_rb 95 w 32
1966a6_ew a_strx r 31 sub get ar_m add get ar_rb 96 w 1 0 0
1967end
1968end
1969
1970note move location of value v: its register (x0..x30, or 32 + V for a float), or 64 + its slot
1971fn a6_loc v ulong to ulong
1972ret a6_wloc a6_wi v
1973end
1974
1975fn a6_wloc w ulong to ulong
1976r = ulong uchar get ar_m add get ar_rb 95 w
1977if eq uchar get ar_m add get ar_rb 99 w 2
1978if lt r 64
1979ret add 568 r
1980end
1981ret add 700 shr sub get ar_m add get ar_rb 96 w 1 1
1982end
1983if lt r 64
1984ret r
1985end
1986ret add 64 sub get ar_m add get ar_rb 96 w 1
1987end
1988
1989note ---- SLICE values (IR.md s4.2 kind 5): two wires. The value's own wire holds the address of element 0; its length
1990note wire (a6_shw: same interval, allocated like any wire) holds the element count. Every move of a slice moves both:
1991note block arguments (a6_mvv), call arguments and parameters (two machine argument positions, a6_xv) ----
1992fn a6_issl v ulong to bool
1993ret eq get i_tk a6_vty v 5
1994end
1995
1996fn a6_lw v ulong to ulong
1997ret sub get ar_m add get ar_rb 98 a6_wi v 1
1998end
1999
2000note a6_lsrc v s: a register holding slice v's element count (its length wire's own, or scratch s loaded from its slot)
2001fn a6_lsrc v ulong s ulong to ulong
2002l = a6_wloc a6_lw v
2003if lt l 32
2004ret l
2005end
2006a6_ew a_ldrx s 31 sub l 64 0 0
2007ret s
2008end
2009
2010note a6_mvv d s: value d = value s in the pending parallel move (both wires of a slice)
2011fn a6_mvv d ulong s ulong to void
2012a6_mvadd a6_loc d a6_loc s
2013if a6_issl d
2014a6_mvadd a6_wloc a6_lw d a6_wloc a6_lw s
2015end
2016end
2017
2018note a6_xv v: append v's machine arguments (one location, two for a slice) to a_mal
2019fn a6_xv v ulong to void
2020n = get a_man 0
2021if ge add n 2 len a_mal
2022a6_unsup n
2023end
2024put a_mal n a6_loc v
2025put a_mac n 0
2026if a6_isfv v
2027put a_mac n 1
2028end
2029if a6_isvv v
2030put a_mac n 2
2031end
2032n = add n 1
2033if a6_issl v
2034put a_mal n a6_wloc a6_lw v
2035put a_mac n 0
2036n = add n 1
2037end
2038put a_man 0 n
2039end
2040
2041note a6_xcall o: the locations of call o's machine arguments in a_mal; their count
2042fn a6_xcall o ulong to ulong
2043put a_man 0 0
2044n = sub ulong get i_ona o 2
2045k = ulong 0
2046while lt k n bound 65
2047a6_xv a6_arg o add k 2
2048k = add k 1
2049end
2050ret get a_man 0
2051end
2052
2053note a6_xpar f: the locations of f's machine parameters in a_mal; their count
2054fn a6_xpar f ulong to ulong
2055put a_man 0 0
2056p = ulong get i_fp1 f
2057while ne p 0 bound 65
2058a6_xv p
2059p = ulong get i_vnx p
2060end
2061ret get a_man 0
2062end
2063
2064note a6_abi n: the ABI place of each of the n machine arguments in a_mal (scalar AAPCS64): integer, reference and
2065note length wires take x0..x7 in order, f32/f64 values v0..v7 in order (a_mac), and the rest the outgoing slots 0.. in
2066note argument order; a_mab[k] is the register location, or 1000 + the slot. Returns the number of slots
2067fn a6_abi n ulong to ulong
2068ki = ulong 0
2069kf = ulong 0
2070ks = ulong 0
2071k = ulong 0
2072while lt k n bound 137
2073c = ulong get a_mac k
2074if eq c 2
2075if lt kf 8
2076put a_mab k add 600 kf
2077kf = add kf 1
2078else
2079ks = and add ks 1 not ulong 1
2080put a_mab k add 3000 ks
2081ks = add ks 2
2082end
2083else
2084if ne c 0
2085if lt kf 8
2086put a_mab k add 32 kf
2087kf = add kf 1
2088else
2089put a_mab k add 1000 ks
2090ks = add ks 1
2091end
2092else
2093if lt ki 8
2094put a_mab k ki
2095ki = add ki 1
2096else
2097put a_mab k add 1000 ks
2098ks = add ks 1
2099end
2100end
2101end
2102k = add k 1
2103end
2104ret ks
2105end
2106
2107note a6_xstk o: the outgoing slots call o needs (before allocation: a6_outm sizes the frame with it): its integer,
2108note scalar float and 16-byte vector arguments past eight in argument order
2109fn a6_xstk o ulong to ulong
2110n = sub ulong get i_ona o 2
2111ki = ulong 0
2112kf = ulong 0
2113ks = ulong 0
2114k = ulong 0
2115while lt k n bound 65
2116v = a6_arg o add k 2
2117if a6_isvv v
2118if lt kf 8
2119kf = add kf 1
2120else
2121ks = and add ks 1 not ulong 1
2122ks = add ks 2
2123end
2124else
2125if a6_isfv v
2126if lt kf 8
2127kf = add kf 1
2128else
2129ks = add ks 1
2130end
2131else
2132w = ulong 1
2133if a6_issl v
2134w = 2
2135end
2136while gt w 0 bound 2
2137if lt ki 8
2138ki = add ki 1
2139else
2140ks = add ks 1
2141end
2142w = sub w 1
2143end
2144end
2145end
2146k = add k 1
2147end
2148ret ks
2149end
2150
2151note a6_shw: one length wire per slice value, with the value's interval (after a6_ivlive)
2152fn a6_shw to void
2153n = get a_fs 0
2154w = ulong 0
2155while lt w n bound 16777217
2156put ar_m add get ar_rb 98 w 0
2157w = add w 1
2158end
2159w = ulong 0
2160while lt w n bound 16777217
2161if a6_wsl get ar_m add get ar_rb 91 w
2162m = get a_fs 0
2163if ne ar_need 26 m 0
2164ar_die
2165end
2166put ar_m add get ar_rb 98 w add m 1
2167put ar_m add get ar_rb 91 m 0
2168put ar_m add get ar_rb 96 m 0
2169ar_pc 94 m 0
2170ar_pc 99 m 0
2171ar_pc 95 m 255
2172put ar_m add get ar_rb 92 m get ar_m add get ar_rb 92 w
2173put ar_m add get ar_rb 93 m get ar_m add get ar_rb 93 w
2174put a_fs 0 add m 1
2175end
2176w = add w 1
2177end
2178end
2179
2180fn a6_wsl v ulong to bool
2181if eq v 0
2182ret false
2183end
2184if a6_istk v
2185ret false
2186end
2187ret a6_issl v
2188end
2189
2190fn a6_movi r ulong v ulong to void
2191if lt v 65536
2192a6_ew a_movzx r v 0 0 0
2193ret
2194end
2195if lt not v 65536
2196a6_ew a_movnx r not v 0 0 0
2197ret
2198end
2199first = true
2200h = ulong 0
2201while lt h 4 bound 4
2202c = and shr v mul h 16 65535
2203if ne c 0
2204if first
2205a6_ew a_movzx r c h 0 0
2206first = false
2207else
2208a6_ew a_movkx r c h 0 0
2209end
2210end
2211h = add h 1
2212end
2213end
2214
2215note a6_canon r t: re-extend register r to type t (zero for u8..u32, sign for i8..i32)
2216fn a6_canon r ulong t ulong to void
2217w = a6_wid t
2218if or ge w 64 le w 1
2219ret
2220end
2221if a6_sgn t
2222a6_ew a_sbfmx r r 0 sub w 1 0
2223else
2224a6_ew a_ubfmx r r 0 sub w 1 0
2225end
2226end
2227
2228fn a6_mov d ulong s ulong to void
2229if ne d s
2230a6_ew a_orrx d 31 s 0 0
2231end
2232end
2233
2234note ---- parallel moves (block arguments, call arguments, entry parameters); x15 breaks a cycle ----
2235fn a6_mvclr to void
2236put a_mvn 0 0
2237end
2238
2239fn a6_mvadd d ulong s ulong to void
2240n = get a_mvn 0
2241if ge n len a_mvd
2242a6_unsup n
2243end
2244if ne d s
2245put a_mvd n d
2246put a_mvs n s
2247put a_mvn 0 add n 1
2248end
2249end
2250
2251fn a6_isvq l ulong to bool
2252ret and ge l 600 lt l 632
2253end
2254
2255fn a6_mvq d ulong s ulong to void
2256if and a6_isvq d a6_isvq s
2257if ne d s
2258a6_ew a_orr16b sub d 600 sub s 600 sub s 600 0 0
2259end
2260ret
2261end
2262if a6_isvq d
2263a6_ew a_ldrq sub d 600 31 sub s 700 0 0
2264ret
2265end
2266if a6_isvq s
2267a6_ew a_strq sub s 600 31 sub d 700 0 0
2268ret
2269end
2270note v30, not v31: v31 is the parallel-move cycle breaker (location 631). A slot-to-slot
2271note swap loads the breaker into v31, then this copy would clobber it and both slots get one value.
2272a6_ew a_ldrq 30 31 sub s 700 0 0
2273a6_ew a_strq 30 31 sub d 700 0 0
2274end
2275
2276fn a6_mv1 d ulong s ulong to void
2277if or ge d 600 ge s 600
2278a6_mvq d s
2279ret
2280end
2281if and lt d 32 lt s 32
2282a6_mov d s
2283ret
2284end
2285if or a6_isvl d a6_isvl s
2286a6_mvf d s
2287ret
2288end
2289if lt d 32
2290a6_ew a_ldrx d 31 sub s 64 0 0
2291ret
2292end
2293if lt s 32
2294a6_ew a_strx s 31 sub d 64 0 0
2295ret
2296end
2297a6_ew a_ldrx 16 31 sub s 64 0 0
2298a6_ew a_strx 16 31 sub d 64 0 0
2299end
2300
2301note a6_isvl l: move location l is a V register (32 + V)
2302fn a6_isvl l ulong to bool
2303ret and ge l 32 lt l 64
2304end
2305
2306note a6_mvf d s: a move with a V register on one side, bit for bit (an f32 rides in the low 32 bits of the D view):
2307note FMOV D, D; FMOV D, X or X, D (raw bits, never a numeric conversion); LDR D or STR D for a stack slot
2308fn a6_mvf d ulong s ulong to void
2309if and a6_isvl d a6_isvl s
2310if ne d s
2311a6_ew a_fmovd sub d 32 sub s 32 0 0 0
2312end
2313ret
2314end
2315if a6_isvl d
2316if lt s 32
2317a6_ew a_fmovdx sub d 32 s 0 0 0
2318else
2319a6_ew a_ldrd sub d 32 31 sub s 64 0 0
2320end
2321ret
2322end
2323if lt d 32
2324a6_ew a_fmovxd d sub s 32 0 0 0
2325else
2326a6_ew a_strd sub s 32 31 sub d 64 0 0
2327end
2328end
2329
2330note a6_isrc l: some pending move reads location l
2331fn a6_isrc l ulong to bool
2332i = ulong 0
2333while lt i get a_mvn 0 bound 64
2334if and ne get a_mvd i 999 eq get a_mvs i l
2335ret true
2336end
2337i = add i 1
2338end
2339ret false
2340end
2341
2342fn a6_pmove to void
2343left = get a_mvn 0
2344while gt left 0 bound 4096
2345prog = false
2346i = ulong 0
2347while lt i get a_mvn 0 bound 64
2348d = get a_mvd i
2349if ne d 999
2350if not a6_isrc d
2351a6_mv1 d get a_mvs i
2352put a_mvd i 999
2353left = sub left 1
2354prog = true
2355end
2356end
2357i = add i 1
2358end
2359if not prog
2360a6_mvbrk
2361end
2362end
2363end
2364
2365fn a6_mvbrk to void
2366i = ulong 0
2367while eq get a_mvd i 999 bound 64
2368i = add i 1
2369end
2370s = get a_mvs i
2371t = ulong 15
2372if ge s 600
2373t = 631
2374end
2375a6_mv1 t s
2376j = ulong 0
2377while lt j get a_mvn 0 bound 64
2378if and ne get a_mvd j 999 eq get a_mvs j s
2379put a_mvs j t
2380end
2381j = add j 1
2382end
2383end
2384
2385note ---- selection ----
2386fn a6_sel o ulong x ulong to void
2387op = ulong get i_oop o
2388r = ulong get i_ors o
2389if ne r 0
2390if and ne op 54 a6_issl r
2391a6_unsup op
2392end
2393end
2394note a compare-fused consumer (a_skip) is emitted at its compare, but still meets the refusal above (select of SLICE)
2395if eq o get a_skip 0
2396ret
2397end
2398if and ge op 111 le op 116
2399a6_spool o op
2400ret
2401end
2402if a6_isvo o op
2403a6_svec o op
2404ret
2405end
2406if a6_isfo o op
2407a6_sflt o op
2408ret
2409end
2410if eq op 1
2411a6_sconst o
2412ret
2413end
2414if and ge op 10 le op 19
2415a6_sarith o op
2416ret
2417end
2418if and ge op 20 le op 25
2419a6_sshift o op
2420ret
2421end
2422if and ge op 30 le op 35
2423a6_scmp o op
2424ret
2425end
2426if and ge op 2 le op 37
2427a6_smisc o op
2428ret
2429end
2430if and ge op 60 le op 66
2431a6_sterm o op x
2432ret
2433end
2434if and ge op 70 le op 82
2435a6_schk o op
2436ret
2437end
2438if and ge op 42 le op 59
2439a6_smem o op
2440ret
2441end
2442if eq op 110
2443a6_shost o
2444ret
2445end
2446if and ge op 90 le op 93
2447ret
2448end
2449a6_unsup op
2450end
2451
2452note ---- memory (IR.md s7.5): a REF is the address of its object in one register, a SLICE its address and count wires;
2453note only REF to ARRAY or SLICE of bool or integers, and only these ops: load, store, alloca, gaddr, len, slice, fill.
2454note Coverage (s8) is ngcheck's: an access here follows the check that covers it ----
2455fn a6_smem o ulong op ulong to void
2456if eq op 42
2457a6_sload o
2458ret
2459end
2460if eq op 43
2461a6_sstore o
2462ret
2463end
2464if eq op 50
2465a6_salloc o
2466ret
2467end
2468r = ulong get i_ors o
2469d = a6_dst r
2470if eq op 52
2471a6_movi d get ar_m add get ar_rb 90 get i_oim o
2472if eq get a_tgt 0 1
2473if eq ar_c 88 get i_oim o 1
2474a6_ew a_subx d 28 d 0 0
2475else
2476a6_ew a_addx d 28 d 0 0
2477end
2478end
2479a6_fin r d
2480ret
2481end
2482if eq op 53
2483if a6_issl a6_arg o 0
2484a6_mov d a6_lsrc a6_arg o 0 17
2485else
2486a6_movi d a6_alen a6_arg o 0
2487end
2488a6_fin r d
2489ret
2490end
2491if eq op 54
2492a6_sslc o
2493ret
2494end
2495if eq op 51
2496a6_sfill o
2497ret
2498end
2499a6_unsup op
2500end
2501
2502note fill tok r v (op 51; DESIGN.md section 10): every element of r (a REF to an ARRAY, count from the type, or a
2503note SLICE, count from its length wire) = v, for bool, integer, f32 and f64 elements. x17 = v's raw bits repeated to
2504note 64 bits (a6_fpat); v31 = DUP x17 (16 bytes); x15 = the byte count (count << log2 size: a REF's is a constant, a
2505note slice's count times its element size fits its one storage object); the base stays in its register. From the end
2506note down: 16-byte blocks (STR Q31, [base, x15]) while 16 or more bytes remain, then an 8, 4, 2 and 1-byte tail, each
2507note when that bit of the remaining count is set; tails narrower than the element are never needed and not emitted.
2508note Each store starts a whole number of elements past the base, so the pattern lines up with the elements whatever
2509note the base's alignment; a count of 0 stores nothing; no byte outside the span is written and nothing is read
2510fn a6_sfill o ulong to void
2511v = a6_arg o 2
2512t = a6_vty v
2513if not a6_iselt t
2514a6_unsup t
2515end
2516a = a6_arg o 1
2517a6_aref a
2518sz = a6_lsz t
2519b = a6_src a 16
2520if a6_isfv v
2521x = a6_fsrc v 31
2522a6_ew a6_ff t a_fmovxd a_fmovws 17 x 0 0 0
2523else
2524x = a6_src v 17
2525a6_fmask x sz
2526end
2527a6_fpat sz
2528if a6_issl a
2529c = a6_lsrc a 15
2530a6_ew a_addx 15 31 c 0 sz
2531else
2532a6_movi 15 shl a6_alen a sz
2533end
2534a6_ew a_dup2d 31 17 0 0 0
2535a6_ew a_subsxi 31 15 16 0 0
2536a6_ew a_bcond 3 4 0 0 0
2537a6_ew a_subxi 15 15 16 0 0
2538a6_ew a_strqr 31 b 15 0 0
2539a6_ew a_b sub ulong 0 4 0 0 0 0
2540a6_ew a_tbz 15 3 3 0 0
2541a6_ew a_subxi 15 15 8 0 0
2542a6_ew a_strxr 17 b 15 0 0
2543if lt sz 3
2544a6_ew a_tbz 15 2 3 0 0
2545a6_ew a_subxi 15 15 4 0 0
2546a6_ew a_strwr 17 b 15 0 0
2547end
2548if lt sz 2
2549a6_ew a_tbz 15 1 3 0 0
2550a6_ew a_subxi 15 15 2 0 0
2551a6_ew a_strhr 17 b 15 0 0
2552end
2553if lt sz 1
2554a6_ew a_tbz 15 0 3 0 0
2555a6_ew a_subxi 15 15 1 0 0
2556a6_ew a_strbr 17 b 15 0 0
2557end
2558end
2559
2560note a6_fmask x sz: x17 = the low 8 << sz bits of integer register x (a signed value's canonical extension dropped)
2561fn a6_fmask x ulong sz ulong to void
2562if eq sz 3
2563a6_mov 17 x
2564ret
2565end
2566a6_ew a_andxi 17 x sub shl ulong 1 shl ulong 8 sz 1 0 0
2567end
2568
2569note a6_fpat sz: x17 (an element of 1 << sz bytes, zero above it) repeated to fill all 64 bits
2570fn a6_fpat sz ulong to void
2571if eq sz 0
2572a6_ew a_orrx 17 17 17 0 8
2573end
2574if le sz 1
2575a6_ew a_orrx 17 17 17 0 16
2576end
2577if le sz 2
2578a6_ew a_orrx 17 17 17 0 32
2579end
2580end
2581
2582note a6_alen v: the element count of the ARRAY that REF v refers to
2583fn a6_alen v ulong to ulong
2584t = a6_vty v
2585if ne get i_tk t 4
2586a6_unsup t
2587end
2588a = ulong get i_t1 t
2589if ne get i_tk a 2
2590a6_unsup a
2591end
2592e = ulong get i_t1 a
2593if not a6_iselt e
2594a6_unsup e
2595end
2596ret ulong get i_t2 a
2597end
2598
2599note load: LDR (register offset, scaled by the element size); the signed narrow types sign-extend (canonical form)
2600fn a6_ldf t ulong to ulong
2601if or eq t 1 eq t 2
2602ret a_ldrbr
2603end
2604if eq t 6
2605ret a_lsbx
2606end
2607if eq t 3
2608ret a_ldrhr
2609end
2610if eq t 7
2611ret a_lshx
2612end
2613if eq t 4
2614ret a_ldrwr
2615end
2616if eq t 8
2617ret a_lsw
2618end
2619ret a_ldrxr
2620end
2621
2622fn a6_stf t ulong to ulong
2623if or eq t 1 or eq t 2 eq t 6
2624ret a_strbr
2625end
2626if or eq t 3 eq t 7
2627ret a_strhr
2628end
2629if or eq t 4 eq t 8
2630ret a_strwr
2631end
2632ret a_strxr
2633end
2634
2635note the S bit of a register-offset access: 0 for one-byte elements, else scale the index by the size
2636fn a6_sh1 t ulong to ulong
2637if or eq t 1 eq a6_wid t 8
2638ret 0
2639end
2640ret 1
2641end
2642
2643note a6_aref v: v is a REF to an ARRAY or a SLICE of bool, integer, f32 or f64 elements (refused otherwise)
2644fn a6_aref v ulong to void
2645if a6_issl v
2646e = ulong get i_t1 a6_vty v
2647if not a6_iselt e
2648a6_unsup e
2649end
2650ret
2651end
2652n = a6_alen v
2653end
2654
2655note a6_iselt e: an element type of this slice's memory ops: bool, the integers, f32 and f64
2656fn a6_iselt e ulong to bool
2657ret or a6_isint e a6_isft e
2658end
2659
2660note a6_lsz t: log2 of the byte size of element type t (bool, the integers, f32 and f64)
2661fn a6_lsz t ulong to ulong
2662if eq t 13
2663ret 2
2664end
2665if eq t 14
2666ret 3
2667end
2668w = a6_wid t
2669if le w 8
2670ret 0
2671end
2672if eq w 16
2673ret 1
2674end
2675if eq w 32
2676ret 2
2677end
2678ret 3
2679end
2680
2681note slice r off n: x16 = address of element off (b + off << log2 size), x15 = n (operands read before either result
2682note wire is written: both may reuse operand registers), then the value's wire and its length wire
2683fn a6_sslc o ulong to void
2684r = ulong get i_ors o
2685e = ulong get i_t1 a6_vty r
2686if not a6_iselt e
2687a6_unsup e
2688end
2689a6_aref a6_arg o 1
2690b = a6_src a6_arg o 1 16
2691f = a6_src a6_arg o 2 17
2692a6_ew a_addx 16 b f 0 a6_lsz e
2693n = a6_src a6_arg o 3 17
2694a6_mov 15 n
2695d = a6_dst r
2696if ne d 16
2697a6_mov d 16
2698end
2699a6_fin r d
2700l = a6_wloc a6_lw r
2701if lt l 32
2702a6_mov l 15
2703else
2704a6_ew a_strx 15 31 sub l 64 0 0
2705end
2706end
2707
2708fn a6_sload o ulong to void
2709t = a6_rty o
2710a6_aref a6_arg o 1
2711b = a6_src a6_arg o 1 16
2712i = a6_src a6_arg o 2 17
2713r = ulong get i_ors o
2714d = a6_dst r
2715a6_ew a6_ldf t d b i a6_sh1 t 0
2716a6_fin r d
2717end
2718
2719fn a6_sstore o ulong to void
2720v = a6_arg o 3
2721t = a6_vty v
2722if not a6_isint t
2723a6_unsup t
2724end
2725a6_aref a6_arg o 1
2726b = a6_src a6_arg o 1 16
2727i = a6_src a6_arg o 2 17
2728x = a6_src v 15
2729a6_ew a6_stf t x b i a6_sh1 t 0
2730end
2731
2732note alloca: the address of the op's frame area, then zero it 16 bytes per STP XZR, XZR (every logical invocation:
2733note the op runs on each pass of an E loop as well)
2734fn a6_salloc o ulong to void
2735k = ulong 0
2736while ne get a_alo k o bound 64
2737k = add k 1
2738end
2739f = get a_aof k
2740r = ulong get i_ors o
2741d = a6_dst r
2742h = shr f 12
2743b = ulong 31
2744if ne h 0
2745a6_ew a_addxi d 31 h 1 0
2746b = d
2747end
2748a6_ew a_addxi d b and f 4095 0 0
2749a6_fin r d
2750a6_mov 16 d
2751a6_movi 17 shr get a_asz k 4
2752a6_ew a_stpxpo 31 31 16 16 0
2753a6_ew a_subsxi 17 17 1 0 0
2754a6_ew a_bcond 1 sub ulong 0 2 0 0 0
2755end
2756
2757note ---- host words (IR.md s7.11): read 53 and write 54, as Linux SVC 63 and 64 (x0 fd, x1 address, x2 count,
2758note x8 number; the kernel returns the count or minus errno in x0 and preserves every other register); cpufence 199
2759note as DSB SY (d5033f9f) and cpurelax 200 as YIELD (d503203f), omc's words (omc.om cpufence/cpurelax notes); monons
2760note 109 and realns 130 as omc's hclock (a6_hclk below) ----
2761fn a6_shost o ulong to void
2762w = get i_oim o
2763if or eq w 136 eq w 137
2764a6_henv o w
2765ret
2766end
2767if eq w 199
2768a6_emitw 0xd5033f9f
2769ret
2770end
2771if eq w 200
2772a6_emitw 0xd503203f
2773ret
2774end
2775if or eq w 109 eq w 130
2776if eq get a_tgt 0 1
2777a6_unsup w
2778end
2779a6_hclk o w
2780ret
2781end
2782if and ge w 241 le w 243
2783if ne get a_tgt 0 1
2784a6_unsup w
2785end
2786a6_hlib o w
2787ret
2788end
2789nr = ulong 64
2790if eq w 53
2791nr = 63
2792end
2793if and ne w 53 ne w 54
2794a6_unsup w
2795end
2796a = a6_arg o 2
2797a6_aref a
2798b = a6_src a 16
2799f = a6_src a6_arg o 3 17
2800a6_ew a_addx 17 b f 0 0
2801a6_mvclr
2802a6_mvadd 0 a6_loc a6_arg o 1
2803a6_mvadd 1 17
2804a6_mvadd 2 a6_loc a6_arg o 4
2805a6_pmove
2806if eq get a_tgt 0 1
2807a6_ew a_movzx 16 sub nr 60 0 0 0
2808a6_ew a_svc 128 0 0 0 0
2809a6_ew a_csnegx 0 0 0 3 0
2810else
2811a6_ew a_movzx 8 nr 0 0 0
2812a6_ew a_svc 0 0 0 0 0
2813end
2814r = ulong get i_ors o
2815if ne r 0
2816a6_mvfrom r 0
2817end
2818end
2819
2820note ---- calls into system libraries, Mach-O only (IR.md s7.11, DESIGN.md s13): libfn 241, libptr 242, libcall 243.
2821note The link page is the 16 KiB below the stack floor x28: +0 dlopen and +8 dlsym (bound by dyld, a6_macho), +16 the
2822note stack dyld entered on (start code). x16, x17 and x9 are scratch; a host word counts as a call for allocation, so
2823note no live value sits in x0..x14 or a caller-saved V register here.
2824note libfn k: 0 for k above 1, else the word at link page + 8k. MOV X0, #0; CMP X16, #2; B.HS +8;
2825note LDR X0, [X17, X16, LSL #3].
2826note libptr A off n: the address of byte off of A (the slice is checked by its chk.slice).
2827note libcall F A off n: A[off, off+n) is the register file, 128 bytes at least (fewer: trap 3; F = 0: trap 3):
2828note x0..x7 at 0..63, d0..d7 at 64..127. Saves x19 and x20 on the Omega stack (floor checked, trap 6), keeps the
2829note Omega sp in x19 and the file's address in x20, moves sp to the saved system stack, loads the sixteen registers,
2830note BLR X16, stores x0, x1 back at 0 and d0..d3 at 64, and restores. The word's value is x0.
2831fn a6_hlib o ulong w ulong to void
2832if eq w 241
2833k = a6_src a6_arg o 1 16
2834a6_mov 16 k
2835a6_emitw 0xd1401391
2836a6_emitw 0xd2800000
2837a6_emitw 0xf1000a1f
2838a6_emitw 0x54000042
2839a6_emitw 0xf8707a20
2840note k 2..17 with a callback function: the address of entry k - 2, as a class-1 address (x28 less a constant):
2841note SUB X17, X16, #2; x9 = x28 - (16 MiB - a_bks) + 8 * x17; CMP X17, #16; CSEL X0, X9, X0, LO
2842if ne get a_bks 0 0
2843a6_emitw 0xd1000a11
2844a6_movi 9 sub 16777216 get a_bks 0
2845a6_emitw 0xcb090389
2846a6_emitw 0x8b110d29
2847a6_emitw 0xf100423f
2848a6_emitw 0x9a803120
2849end
2850end
2851if eq w 242
2852a = a6_arg o 1
2853a6_aref a
2854b = a6_src a 16
2855f = a6_src a6_arg o 2 17
2856a6_ew a_addx 17 b f 0 0
2857a6_emitw 0xaa1103e0
2858end
2859if eq w 243
2860a = a6_arg o 2
2861a6_aref a
2862b = a6_src a 16
2863f = a6_src a6_arg o 3 17
2864a6_ew a_addx 17 b f 0 0
2865n = a6_src a6_arg o 4 16
2866a6_ew a_subsxi 31 n 128 0 0
2867a6_fx a_bcond 3 add a_tlab 3
2868fr = a6_src a6_arg o 1 16
2869a6_mov 16 fr
2870a6_ew a_subsxi 31 16 0 0 0
2871a6_fx a_bcond 0 add a_tlab 3
2872a6_emitw 0xa9bf53f3
2873a6_ew a_subsxe 31 31 28 3 0
2874a6_fx a_bcond 3 add a_tlab 6
2875a6_emitw 0x910003f3
2876a6_emitw 0xaa1103f4
2877a6_emitw 0xd1401389
2878a6_emitw 0xf9000d33
2879a6_emitw 0xf9400929
2880a6_emitw 0x9100013f
2881a6_emitw 0xa9400680
2882a6_emitw 0xa9410e82
2883a6_emitw 0xa9421684
2884a6_emitw 0xa9431e86
2885a6_emitw 0x6d440680
2886a6_emitw 0x6d450e82
2887a6_emitw 0x6d461684
2888a6_emitw 0x6d471e86
2889a6_emitw 0xd63f0200
2890a6_emitw 0xa9000680
2891a6_emitw 0x6d040680
2892a6_emitw 0x6d050e82
2893a6_emitw 0x9100027f
2894a6_emitw 0xa8c153f3
2895end
2896r = ulong get i_ors o
2897if ne r 0
2898a6_mvfrom r 0
2899end
2900end
2901
2902note monons 109 and realns 130 as omc's hclock: SVC 113 clock_gettime(CLOCK_MONOTONIC 1 / CLOCK_REALTIME 0) into a
2903note 16-byte area below SP (stack floor checked, trap 6), result sec * 10^9 + nsec with omc's traps (sec above
2904note 9223372036: B.HI trap 1; the add: B.VS trap 1); a failed call returns x0 (minus errno). The area is zeroed first,
2905note so a failed call reads 0 and 0 and cannot trap; CSEL LT then picks x0, which omc reaches by a branch. x0..x3, x8
2906note are scratch (a host word counts as a call for allocation).
2907fn a6_hclk o ulong w ulong to void
2908ck = ulong 1
2909if eq w 130
2910ck = ulong 0
2911end
2912a6_ew a_subxi 31 31 16 0 0
2913a6_ew a_subsxe 31 31 28 3 0
2914a6_fx a_bcond 3 add a_tlab 6
2915a6_ew a_stpx 31 31 31 0 0
2916a6_ew a_movzx 0 ck 0 0 0
2917a6_ew a_addxi 1 31 0 0 0
2918a6_ew a_movzx 8 113 0 0 0
2919a6_ew a_svc 0 0 0 0 0
2920a6_ew a_ldrx 1 31 0 0 0
2921a6_ew a_ldrx 2 31 1 0 0
2922a6_movi 3 9223372036
2923a6_ew a_subsx 31 1 3 0 0
2924a6_fx a_bcond 8 add a_tlab 1
2925a6_movi 3 1000000000
2926a6_ew a_mulx 1 1 3 0 0
2927a6_ew a_addsx 2 1 2 0 0
2928a6_fx a_bcond 6 add a_tlab 1
2929a6_ew a_subsxi 31 0 0 0 0
2930a6_ew a_cselx 0 0 2 11 0
2931a6_ew a_addxi 31 31 16 0 0
2932r = ulong get i_ors o
2933if ne r 0
2934a6_mvfrom r 0
2935end
2936end
2937
2938fn a6_rty o ulong to ulong
2939t = ulong get i_oty o
2940if not a6_isint t
2941a6_unsup t
2942end
2943ret t
2944end
2945
2946fn a6_sconst o ulong to void
2947r = ulong get i_ors o
2948t = a6_rty o
2949d = a6_dst r
2950a6_movi d get i_oim o
2951a6_fin r d
2952end
2953
2954note result of an op whose operands are already in registers a, b: computed into d, extended, stored
2955fn a6_sarith o ulong op ulong to void
2956r = ulong get i_ors o
2957t = a6_rty o
2958a = a6_src a6_arg o 0 16
2959b = ulong 31
2960if and ne op 15 ne op 19
2961b = a6_src a6_arg o 1 17
2962end
2963d = a6_dst r
2964a6_ar2 op t d a b
2965a6_canon d t
2966a6_fin r d
2967end
2968
2969fn a6_ar2 op ulong t ulong d ulong a ulong b ulong to void
2970if eq op 10
2971a6_ew a_addx d a b 0 0
2972end
2973if eq op 11
2974a6_ew a_subx d a b 0 0
2975end
2976if eq op 12
2977a6_ew a_mulx d a b 0 0
2978end
2979if eq op 13
2980a6_div d a b t
2981end
2982if eq op 14
2983a6_rem d a b t
2984end
2985if eq op 15
2986a6_ew a_subx d 31 a 0 0
2987end
2988if eq op 16
2989a6_ew a_andx d a b 0 0
2990end
2991if eq op 17
2992a6_ew a_orrx d a b 0 0
2993end
2994if eq op 18
2995a6_ew a_eorx d a b 0 0
2996end
2997if eq op 19
2998a6_not d a t
2999end
3000end
3001
3002fn a6_not d ulong a ulong t ulong to void
3003if eq t 1
3004a6_ew a_eorxi d a 1 0 0
3005else
3006a6_ew a_ornx d 31 a 0 0
3007end
3008end
3009
3010fn a6_div d ulong a ulong b ulong t ulong to void
3011if a6_sgn t
3012a6_ew a_sdivx d a b 0 0
3013else
3014a6_ew a_udivx d a b 0 0
3015end
3016end
3017
3018note rem: a - (a / b) * b, and 0 when b = 0 (IR.md s7.3); the flags are set before d may overwrite a or b
3019fn a6_rem d ulong a ulong b ulong t ulong to void
3020a6_div 15 a b t
3021a6_ew a_subsxi 31 b 0 0 0
3022a6_ew a_msubx d 15 b a 0
3023a6_ew a_cselx d 31 d 0 0
3024end
3025
3026note shl shr rotl rotr (amount modulo the width) and min max
3027fn a6_sshift o ulong op ulong to void
3028r = ulong get i_ors o
3029t = a6_rty o
3030a = a6_src a6_arg o 0 16
3031b = a6_src a6_arg o 1 17
3032d = a6_dst r
3033if ge op 24
3034a6_ew a_subsx 31 a b 0 0
3035a6_ew a_cselx d a b a6_mmc op t 0
3036else
3037a6_sh2 op t d a b
3038end
3039a6_canon d t
3040a6_fin r d
3041end
3042
3043note condition for min (24) / max (25): a selected when a < b / a > b
3044fn a6_mmc op ulong t ulong to ulong
3045s = a6_sgn t
3046if eq op 24
3047if s
3048ret 11
3049end
3050ret 3
3051end
3052if s
3053ret 12
3054end
3055ret 8
3056end
3057
3058fn a6_sh2 op ulong t ulong d ulong a ulong b ulong to void
3059w = a6_wid t
3060if ge op 22
3061a6_rot op t d a b
3062ret
3063end
3064if and lt w 32 gt w 1
3065a6_ew a_andxi 15 b sub w 1 0 0
3066b = 15
3067end
3068if eq w 32
3069a6_sh32 op t d a b
3070ret
3071end
3072if eq op 20
3073a6_ew a_lslvx d a b 0 0
3074ret
3075end
3076if a6_sgn t
3077a6_ew a_asrvx d a b 0 0
3078else
3079a6_ew a_lsrvx d a b 0 0
3080end
3081end
3082
3083fn a6_sh32 op ulong t ulong d ulong a ulong b ulong to void
3084if eq op 20
3085a6_ew a_lslvw d a b 0 0
3086ret
3087end
3088if a6_sgn t
3089a6_ew a_asrvw d a b 0 0
3090else
3091a6_ew a_lsrvw d a b 0 0
3092end
3093end
3094
3095note rotl = rotr by the negated amount (RORV takes it modulo the width). u8 and u16 (zero-extended) are copied across
3096note the low 32 bits in x16 (ORR x16, a, a, LSL w, and again by 16 for u8) and rotated as 32 bits: w divides 32 and
3097note the copy repeats every w bits, so the low w bits are the w-bit rotation by the amount modulo w; the caller
3098note re-extends d (a6_canon). x16 holds only a's spill reload and b is never in x16, so the copy clobbers nothing.
3099fn a6_rot op ulong t ulong d ulong a ulong b ulong to void
3100w = a6_wid t
3101if a6_sgn t
3102a6_unsup t
3103end
3104if lt w 8
3105a6_unsup t
3106end
3107n = b
3108if eq op 22
3109a6_ew a_subx 15 31 b 0 0
3110n = 15
3111end
3112if eq w 64
3113a6_ew a_rorvx d a n 0 0
3114ret
3115end
3116s = a
3117if lt w 32
3118a6_ew a_orrx 16 a a 0 w
3119if eq w 8
3120a6_ew a_orrx 16 16 16 0 16
3121end
3122s = 16
3123end
3124a6_ew a_rorvw d s n 0 0
3125end
3126
3127note compare: cmp a, b; cset d, cond (cset = csinc d, xzr, xzr, inverted cond)
3128fn a6_scmp o ulong op ulong to void
3129r = ulong get i_ors o
3130v = a6_arg o 0
3131t = a6_vty v
3132if not a6_isint t
3133a6_unsup t
3134end
3135a = a6_src v 16
3136b = a6_src a6_arg o 1 17
3137d = a6_dst r
3138n = a6_cuse o
3139if ne n 0
3140a6_cmpuse o n op t a b
3141ret
3142end
3143a6_ew a_subsx 31 a b 0 0
3144a6_ew a_csincx d 31 31 xor a6_cc op t 1 0
3145a6_fin r d
3146end
3147
3148note Compare fusion is local: a sole, immediately following cbr/select consumer. Operand lifetimes therefore do
3149note not change, no effect or flag-setting instruction moves, and a compare used elsewhere stays materialised.
3150fn a6_ucnt to void
3151w = ulong 0
3152while lt w get a_fs 0 bound 16777217
3153ar_pu 100 w 0
3154w = add w 1
3155end
3156x = ulong 0
3157while lt x get a_fs 1 bound 65536
3158o = ulong get i_bo1 get a_bh x
3159while ne o 0 bound 4194305
3160k = ulong 0
3161while lt k ulong get i_ona o bound 1048576
3162if eq ir_okd uint get i_oop o k 0
3163v = a6_arg o k
3164if not a6_istk v
3165w = a6_wi v
3166ar_pu 100 w add uint get ar_m add get ar_rb 100 w 1
3167end
3168end
3169k = add k 1
3170end
3171o = ulong get i_onx o
3172end
3173x = add x 1
3174end
3175end
3176fn a6_cuse o ulong to ulong
3177r = ulong get i_ors o
3178if ne uint get ar_m add get ar_rb 100 a6_wi r 1
3179ret 0
3180end
3181n = ulong get i_onx o
3182if eq n 0
3183ret 0
3184end
3185op = get i_oop n
3186if eq op 61
3187if eq a6_arg n 1 r
3188ret n
3189end
3190end
3191if eq op 2
3192if and eq a6_arg n 0 r and not a6_isfv ulong get i_ors n not a6_isvv ulong get i_ors n
3193ret n
3194end
3195end
3196ret 0
3197end
3198fn a6_czero v ulong to bool
3199if ne get i_vk v 4
3200ret false
3201end
3202o = get i_vo v
3203ret and eq get i_oop o 1 eq get i_oim o 0
3204end
3205note A single-bit AND followed by eq/ne zero can test its result with TBZ/TBNZ, no CMP or boolean.
3206fn a6_cbit v ulong to ulong
3207if ne get i_vk v 4
3208ret 64
3209end
3210o = ulong get i_vo v
3211if ne get i_oop o 16
3212ret 64
3213end
3214k = ulong 0
3215while lt k 2 bound 2
3216s = a6_arg o k
3217if eq get i_vk s 4
3218p = get i_vo s
3219if eq get i_oop p 1
3220m = get i_oim p
3221j = ulong 0
3222while lt j 64 bound 64
3223if eq m shl ulong 1 j
3224ret j
3225end
3226j = add j 1
3227end
3228end
3229end
3230k = add k 1
3231end
3232ret 64
3233end
3234fn a6_tbfx f ulong r ulong bit ulong l ulong to void
3235if ne get a_fs 16 0
3236a6_ew xor f 1 r bit 2 0 0
3237a6_fx a_b 0 l
3238else
3239a6_fx f add r shl bit 5 l
3240end
3241end
3242fn a6_cjump o ulong op ulong cc ulong a ulong b ulong l ulong to void
3243zero = false
3244if or eq op 30 eq op 31
3245zero = a6_czero a6_arg o 1
3246end
3247if zero
3248f = a_cbnzx
3249if eq cc 0
3250f = a_cbzx
3251end
3252bit = a6_cbit a6_arg o 0
3253if lt bit 64
3254f = 111
3255if eq cc 0
3256f = 110
3257end
3258a6_tbfx f a bit l
3259else
3260a6_fx f a l
3261end
3262else
3263a6_ew a_subsx 31 a b 0 0
3264a6_fx a_bcond cc l
3265end
3266end
3267
3268fn a6_cmpuse o ulong n ulong op ulong t ulong a ulong b ulong to void
3269put a_skip 0 n
3270cc = a6_cc op t
3271if eq get i_oop n 2
3272a6_ew a_subsx 31 a b 0 0
3273u = a6_src a6_arg n 1 17
3274v = a6_src a6_arg n 2 15
3275r = ulong get i_ors n
3276d = a6_dst r
3277a6_ew a_cselx d u v cc 0
3278a6_fin r d
3279ret
3280end
3281bt = a6_bix a6_arg n 2
3282bf = a6_bix a6_arg n 3
3283x = a6_bix ulong get i_ob n
3284inv = eq bt add x 1
3285l = bt
3286if inv
3287l = bf
3288cc = xor cc 1
3289end
3290a6_cjump o op cc a b l
3291if not inv
3292if ne bf add x 1
3293a6_fx a_b 0 bf
3294end
3295end
3296end
3297
3298fn a6_cc op ulong t ulong to ulong
3299if eq op 30
3300ret 0
3301end
3302if eq op 31
3303ret 1
3304end
3305s = a6_sgn t
3306if eq op 32
3307if s
3308ret 11
3309end
3310ret 3
3311end
3312if eq op 33
3313if s
3314ret 13
3315end
3316ret 9
3317end
3318if eq op 34
3319if s
3320ret 12
3321end
3322ret 8
3323end
3324if s
3325ret 10
3326end
3327ret 2
3328end
3329
3330note select (2), conv (36, integer to integer), bitcast (37, integer to integer)
3331fn a6_smisc o ulong op ulong to void
3332r = ulong get i_ors o
3333t = a6_rty o
3334if eq op 2
3335c = a6_src a6_arg o 0 16
3336a = a6_src a6_arg o 1 17
3337b = a6_src a6_arg o 2 15
3338d = a6_dst r
3339a6_ew a_subsxi 31 c 0 0 0
3340a6_ew a_cselx d a b 1 0
3341a6_fin r d
3342ret
3343end
3344if or eq op 36 eq op 37
3345v = a6_arg o 0
3346if not a6_isint a6_vty v
3347a6_unsup op
3348end
3349a = a6_src v 16
3350d = a6_dst r
3351a6_mov d a
3352a6_canon d t
3353a6_fin r d
3354ret
3355end
3356a6_unsup op
3357end
3358
3359note ---- scalar floats (IR.md s7.2 to s7.5 and s7.9; DESIGN.md section 9): an f32/f64 value lives in a V register
3360note (location 32 + V, a6_ffree) or a stack slot (8 bytes, LDR/STR D); an f32 uses the S view of its register. v29,
3361note v30 and v31 are the float scratch (operands reloaded from slots, a result bound for a slot, chk.conv bounds).
3362note FPCR is 0 (round to nearest even, no flush to zero, no default NaN) from _start on; a toward-zero conversion
3363note sets RMode 11 for its one rounding instruction and writes 0 back right after it (a6_rz) ----
3364fn a6_isft t ulong to bool
3365ret or eq t 13 eq t 14
3366end
3367
3368fn a6_isfv v ulong to bool
3369ret a6_isft a6_vty v
3370end
3371
3372note a6_rloc v: the register location of a call result or retv operand v: d0 (location 32) for a float, else x0
3373fn a6_rloc v ulong to ulong
3374if a6_isvv v
3375ret 600
3376end
3377if a6_isfv v
3378ret a_d0
3379end
3380ret 0
3381end
3382
3383note a6_ff t fd fs: form fd for an f64 operation, fs for an f32 one
3384fn a6_ff t ulong fd ulong fs ulong to ulong
3385if eq t 14
3386ret fd
3387end
3388ret fs
3389end
3390
3391note a6_fsrc v s: the V register holding float value v: its own, or scratch V s loaded from its slot (LDR D)
3392fn a6_fsrc v ulong s ulong to ulong
3393w = a6_wi v
3394r = ulong uchar get ar_m add get ar_rb 95 w
3395if a6_isvl r
3396ret sub r 32
3397end
3398a6_ew a_ldrd s 31 sub get ar_m add get ar_rb 96 w 1 0 0
3399ret s
3400end
3401
3402note a6_fdst v: the V register to compute float value v into: its own, or v31 when it lives in a slot (a6_ffin)
3403fn a6_fdst v ulong to ulong
3404r = ulong uchar get ar_m add get ar_rb 95 a6_wi v
3405if a6_isvl r
3406ret sub r 32
3407end
3408ret 31
3409end
3410
3411fn a6_ffin v ulong r ulong to void
3412w = a6_wi v
3413if ge ulong uchar get ar_m add get ar_rb 95 w 64
3414a6_ew a_strd r 31 sub get ar_m add get ar_rb 96 w 1 0 0
3415end
3416end
3417
3418note a6_isfo o op: op o is one of this slice's scalar float operations: a const, select, arithmetic, fma or load with
3419note an f32/f64 result; a compare, conv or bitcast with an f32/f64 side; a store of an f32/f64; a chk.conv of one
3420fn a6_isfo o ulong op ulong to bool
3421r = ulong get i_ors o
3422fr = false
3423if ne r 0
3424fr = a6_isfv r
3425end
3426if or eq op 1 eq op 2
3427ret fr
3428end
3429if and ge op 10 le op 28
3430ret fr
3431end
3432if or eq op 42 eq op 46
3433ret fr
3434end
3435if and ge op 30 le op 37
3436if fr
3437ret true
3438end
3439ret a6_isfv a6_arg o 0
3440end
3441if eq op 43
3442ret a6_isfv a6_arg o 3
3443end
3444if eq op 78
3445ret a6_isfv a6_arg o 1
3446end
3447ret false
3448end
3449
3450fn a6_sflt o ulong op ulong to void
3451r = ulong get i_ors o
3452if eq op 1
3453a6_fconst o r
3454ret
3455end
3456if eq op 2
3457a6_fpick o r
3458ret
3459end
3460if or or eq op 10 eq op 11 or eq op 12 eq op 13
3461a6_fbin o op r
3462ret
3463end
3464if or eq op 24 eq op 25
3465a6_fbin o op r
3466ret
3467end
3468if or eq op 15 or eq op 26 eq op 27
3469a6_fun o op r
3470ret
3471end
3472if and ge op 30 le op 35
3473a6_fcmp o op r
3474ret
3475end
3476if eq op 36
3477a6_fconv o r
3478ret
3479end
3480if eq op 37
3481a6_fbcast o r
3482ret
3483end
3484if eq op 46
3485a6_ffma o r
3486ret
3487end
3488if eq op 42
3489a6_fload o r
3490ret
3491end
3492if eq op 43
3493a6_fstore o
3494ret
3495end
3496if eq op 78
3497a6_fchk o
3498ret
3499end
3500a6_unsup op
3501end
3502
3503note const: the raw bits into x16 (MOVZ/MOVN/MOVK), then FMOV D, X or S, W: the sign and every NaN payload bit kept
3504fn a6_fconst o ulong r ulong to void
3505t = ulong get i_oty o
3506d = a6_fdst r
3507a6_movi 16 get i_oim o
3508a6_ew a6_ff t a_fmovdx a_fmovsw d 16 0 0 0
3509a6_ffin r d
3510end
3511
3512note select of floats: CMP c, #0; FCSEL d, a, b, NE (the selected operand's raw bits)
3513fn a6_fpick o ulong r ulong to void
3514t = ulong get i_oty o
3515c = a6_src a6_arg o 0 16
3516a = a6_fsrc a6_arg o 1 29
3517b = a6_fsrc a6_arg o 2 30
3518d = a6_fdst r
3519a6_ew a_subsxi 31 c 0 0 0
3520a6_ew a6_ff t a_fcseld a_fcsels d a b 1 0
3521a6_ffin r d
3522end
3523
3524note add sub mul div: FADD FSUB FMUL FDIV (one rounding each, never contracted); min max: FMINNM FMAXNM, the IR.md
3525note s7.3 rule (one quiet NaN gives the other operand, a signaling NaN a quiet NaN, -0 below +0)
3526fn a6_fbin o ulong op ulong r ulong to void
3527t = ulong get i_oty o
3528a = a6_fsrc a6_arg o 0 29
3529b = a6_fsrc a6_arg o 1 30
3530d = a6_fdst r
3531fm = a6_ff t a_faddd a_fadds
3532if le op 13
3533fm = add fm sub op 10
3534end
3535if eq op 24
3536fm = a6_ff t a_fminnmd a_fminnms
3537end
3538if eq op 25
3539fm = a6_ff t a_fmaxnmd a_fmaxnms
3540end
3541a6_ew fm d a b 0 0
3542a6_ffin r d
3543end
3544
3545note neg FNEG, fsqrt FSQRT, fabs FABS (FNEG and FABS change the sign bit only, NaNs included)
3546fn a6_fun o ulong op ulong r ulong to void
3547t = ulong get i_oty o
3548a = a6_fsrc a6_arg o 0 29
3549d = a6_fdst r
3550fm = a6_ff t a_fnegd a_fnegs
3551if eq op 26
3552fm = a6_ff t a_fsqrtd a_fsqrts
3553end
3554if eq op 27
3555fm = a6_ff t a_fabsd a_fabss
3556end
3557a6_ew fm d a 0 0 0
3558a6_ffin r d
3559end
3560
3561note compare: FCMP a, b; CSET d, cond. Unordered sets C and V and clears N and Z, so EQ, MI (lt), LS (le), GT and GE
3562note are false on it and NE true (IR.md s7.3); the integer LT and LE would be true there
3563fn a6_fcmp o ulong op ulong r ulong to void
3564v = a6_arg o 0
3565t = a6_vty v
3566a = a6_fsrc v 29
3567b = a6_fsrc a6_arg o 1 30
3568d = a6_dst r
3569a6_ew a6_ff t a_fcmpd a_fcmps a b 0 0 0
3570a6_ew a_csincx d 31 31 xor a6_fcc op 1 0
3571a6_fin r d
3572end
3573
3574note a6_fcc op: the condition of float compare op 30..35 (eq EQ 0, ne NE 1, lt MI 4, le LS 9, gt GT 12, ge GE 10),
3575note four bits per op from the low end
3576fn a6_fcc op ulong to ulong
3577ret and shr ulong 0xac9410 mul 4 sub op 30 15
3578end
3579
3580note a6_rz z on: toward-zero rounding around one instruction when z: FPCR = RMode 11 (0xc00000, through x15) before
3581note it, FPCR = 0 (the program's environment) after it
3582fn a6_rz z bool on bool to void
3583if not z
3584ret
3585end
3586if on
3587a6_ew a_movzx 15 0xc0 1 0 0
3588a6_ew a_msrfpcr 15 0 0 0 0
3589else
3590a6_ew a_msrfpcr 31 0 0 0 0
3591end
3592end
3593
3594note conv (IR.md s7.3; imm bits 0..1 the rounding of a float destination, 0 nearest even, 1 toward zero; bit 2
3595note saturation): int -> float SCVTF/UCVTF from the canonical X; f32 -> f64 FCVT (exact); f64 -> f32 FCVT, and with
3596note bit 2 an infinite result of a finite source becomes the largest finite of its sign (a6_fsat); float -> int
3597note a6_f2i. The source of f64 -> f32 is read (x17) before its register may be reused by the result
3598fn a6_fconv o ulong r ulong to void
3599t = ulong get i_oty o
3600v = a6_arg o 0
3601s = a6_vty v
3602im = get i_oim o
3603z = eq and im 3 1
3604if a6_isint s
3605a = a6_src v 16
3606d = a6_fdst r
3607a6_rz z true
3608fm = a6_ff t a_ucvtfdx a_ucvtfsx
3609if a6_sgn s
3610fm = a6_ff t a_scvtfdx a_scvtfsx
3611end
3612a6_ew fm d a 0 0 0
3613a6_rz z false
3614a6_ffin r d
3615ret
3616end
3617a = a6_fsrc v 29
3618if a6_isint t
3619d = a6_dst r
3620a6_f2i t s d a
3621a6_fin r d
3622ret
3623end
3624d = a6_fdst r
3625if eq t 14
3626a6_ew a_fcvtds d a 0 0 0
3627a6_ffin r d
3628ret
3629end
3630sat = ne and im 4 0
3631if sat
3632a6_ew a_fmovxd 17 a 0 0 0
3633end
3634a6_rz z true
3635a6_ew a_fcvtsd d a 0 0 0
3636a6_rz z false
3637if sat
3638a6_fsat d
3639end
3640a6_ffin r d
3641end
3642
3643note a6_fsat d: saturation of an f64 -> f32 conv whose source bits are in x17: when S d is infinite (exponent 255)
3644note and the source is finite (exponent below 2047), S d = its bits - 1, the largest finite of the same sign
3645fn a6_fsat d ulong to void
3646a6_ew a_fmovws 16 d 0 0 0
3647a6_ew a_ubfmx 15 16 23 30 0
3648a6_ew a_subsxi 31 15 255 0 0
3649a6_ew a_csincx 15 31 31 1 0
3650a6_ew a_ubfmx 17 17 52 62 0
3651a6_ew a_subsxi 31 17 2047 0 0
3652a6_ew a_cselx 15 31 15 0 0
3653a6_ew a_subx 16 16 15 0 0
3654a6_ew a_fmovsw d 16 0 0 0
3655end
3656
3657note a6_f2i t s d a: x d = the float of type s in V a, toward zero, as integer type t: FCVTZS (signed t) or FCVTZU X
3658note saturate at 64 bits and give 0 for NaN; a narrower t is clamped to its bounds (CMP with x17, CSEL GT/LT or HI)
3659fn a6_f2i t ulong s ulong d ulong a ulong to void
3660w = a6_wid t
3661if a6_sgn t
3662a6_ew a6_ff s a_fcvtzsxd a_fcvtzsxs d a 0 0 0
3663if lt w 64
3664a6_movi 17 sub shl ulong 1 sub w 1 1
3665a6_ew a_subsx 31 d 17 0 0
3666a6_ew a_cselx d 17 d 12 0
3667a6_movi 17 sub ulong 0 shl ulong 1 sub w 1
3668a6_ew a_subsx 31 d 17 0 0
3669a6_ew a_cselx d 17 d 11 0
3670end
3671ret
3672end
3673a6_ew a6_ff s a_fcvtzuxd a_fcvtzuxs d a 0 0 0
3674if lt w 64
3675a6_movi 17 sub shl ulong 1 w 1
3676a6_ew a_subsx 31 d 17 0 0
3677a6_ew a_cselx d 17 d 8 0
3678end
3679end
3680
3681note bitcast (IR.md s7.3, every bit kept): float -> int FMOV X, D or W, S, then the integer's canonical extension;
3682note int -> float FMOV D, X or S, W; float -> float FMOV D
3683fn a6_fbcast o ulong r ulong to void
3684t = ulong get i_oty o
3685v = a6_arg o 0
3686s = a6_vty v
3687if and a6_isft s a6_isft t
3688a = a6_fsrc v 29
3689d = a6_fdst r
3690a6_ew a_fmovd d a 0 0 0
3691a6_ffin r d
3692ret
3693end
3694if a6_isft s
3695a = a6_fsrc v 29
3696d = a6_dst r
3697a6_ew a6_ff s a_fmovxd a_fmovws d a 0 0 0
3698a6_canon d t
3699a6_fin r d
3700ret
3701end
3702a = a6_src v 16
3703d = a6_fdst r
3704a6_ew a6_ff t a_fmovdx a_fmovsw d a 0 0 0
3705a6_ffin r d
3706end
3707
3708note fma (IR.md s7.4, op 46 only): FMADD d, a, b, c = a x b + c, rounded once
3709fn a6_ffma o ulong r ulong to void
3710t = ulong get i_oty o
3711a = a6_fsrc a6_arg o 0 29
3712b = a6_fsrc a6_arg o 1 30
3713c = a6_fsrc a6_arg o 2 31
3714d = a6_fdst r
3715a6_ew a6_ff t a_fmaddd a_fmadds d a b c 0
3716a6_ffin r d
3717end
3718
3719note load and store of an f32/f64 element: LDR/STR S or D, register offset, the index scaled by the element size
3720fn a6_fload o ulong r ulong to void
3721t = ulong get i_oty o
3722a6_aref a6_arg o 1
3723b = a6_src a6_arg o 1 16
3724i = a6_src a6_arg o 2 17
3725d = a6_fdst r
3726a6_ew a6_ff t a_ldrdr a_ldrsr d b i 1 0
3727a6_ffin r d
3728end
3729
3730fn a6_fstore o ulong to void
3731v = a6_arg o 3
3732t = a6_vty v
3733a6_aref a6_arg o 1
3734b = a6_src a6_arg o 1 16
3735i = a6_src a6_arg o 2 17
3736x = a6_fsrc v 29
3737a6_ew a6_ff t a_strdr a_strsr x b i 1 0
3738end
3739
3740note chk.conv of a float v into integer type t (IR.md s7.9, trap 5): v as f64 in V a (FCVT D, S for an f32, exact);
3741note unordered traps (B.VS); then v <= lo (B.LS) or v >= hi (B.GE) traps, with the f64 bounds of the truncated value's
3742note range: unsigned lo -1, hi 2^w; signed w < 64 lo -(2^(w-1) + 1), hi 2^(w-1); i64 v < -2^63 (B.MI) or v >= 2^63.
3743note The bound bits (exponent 1023 + k at bit 52) are checked with Wolfram in DESIGN.md section 9
3744fn a6_fchk o ulong to void
3745t = ulong get i_oty o
3746v = a6_arg o 1
3747a = a6_fsrc v 29
3748if eq a6_vty v 13
3749a6_ew a_fcvtds 30 a 0 0 0
3750a = 30
3751end
3752a6_ew a_fcmpd a a 0 0 0
3753a6_fx a_bcond 6 add a_tlab 5
3754w = a6_wid t
3755lo = ulong 0xbff0000000000000
3756lc = ulong 9
3757hi = shl add ulong 1023 w 52
3758if a6_sgn t
3759hi = shl add ulong 1022 w 52
3760if eq w 64
3761lo = or 0x8000000000000000 hi
3762lc = 4
3763else
3764lo = or 0x8000000000000000 or hi shl ulong 1 sub 53 w
3765end
3766end
3767a6_movi 16 lo
3768a6_ew a_fmovdx 31 16 0 0 0
3769a6_ew a_fcmpd a 31 0 0 0
3770a6_fx a_bcond lc add a_tlab 5
3771a6_movi 16 hi
3772a6_ew a_fmovdx 31 16 0 0 0
3773a6_ew a_fcmpd a 31 0 0 0
3774a6_fx a_bcond 10 add a_tlab 5
3775end
3776
3777note ---- packed 16-byte float vectors: vec f32 4 and vec f64 2 (IR.md s7.3, s7.4, s7.5, s7.8; DESIGN.md s12) ----
3778fn a6_isvt t ulong to bool
3779if ne get i_tk t 1
3780ret false
3781end
3782e = ulong get i_t1 t
3783l = ulong get i_t2 t
3784if and eq e 13 eq l 4
3785ret true
3786end
3787ret and eq e 14 eq l 2
3788end
3789
3790fn a6_isvv v ulong to bool
3791ret a6_isvt a6_vty v
3792end
3793
3794fn a6_vf t ulong fd ulong fs ulong to ulong
3795if eq get i_t1 t 14
3796ret fd
3797end
3798ret fs
3799end
3800
3801fn a6_vsrc v ulong s ulong to ulong
3802w = a6_wi v
3803r = ulong uchar get ar_m add get ar_rb 95 w
3804if a6_isvl r
3805ret sub r 32
3806end
3807a6_ew a_ldrq s 31 shr sub get ar_m add get ar_rb 96 w 1 1 0 0
3808ret s
3809end
3810
3811fn a6_vdst v ulong to ulong
3812r = ulong uchar get ar_m add get ar_rb 95 a6_wi v
3813if a6_isvl r
3814ret sub r 32
3815end
3816ret 31
3817end
3818
3819fn a6_vfin v ulong r ulong to void
3820w = a6_wi v
3821if ge ulong uchar get ar_m add get ar_rb 95 w 64
3822a6_ew a_strq r 31 shr sub get ar_m add get ar_rb 96 w 1 1 0 0
3823end
3824end
3825
3826fn a6_isvo o ulong op ulong to bool
3827if and ge op 60 le op 66
3828ret false
3829end
3830r = ulong get i_ors o
3831if ne r 0
3832if a6_isvv r
3833ret true
3834end
3835end
3836if or eq op 58 eq op 59
3837ret true
3838end
3839if and ge op 100 le op 106
3840ret true
3841end
3842if eq op 43
3843ret a6_isvv a6_arg o 3
3844end
3845ret false
3846end
3847
3848fn a6_svec o ulong op ulong to void
3849r = ulong get i_ors o
3850if or or eq op 10 eq op 11 eq op 12
3851a6_vbin o op r
3852ret
3853end
3854if eq op 46
3855a6_vfma o r
3856ret
3857end
3858if eq op 58
3859a6_vld o r
3860ret
3861end
3862if eq op 59
3863a6_vst o
3864ret
3865end
3866if eq op 100
3867a6_vsplt o r
3868ret
3869end
3870if eq op 101
3871a6_vext o r
3872ret
3873end
3874a6_unsup op
3875end
3876
3877fn a6_vbin o ulong op ulong r ulong to void
3878t = ulong get i_oty o
3879a = a6_vsrc a6_arg o 0 29
3880b = a6_vsrc a6_arg o 1 30
3881d = a6_vdst r
3882fm = a6_vf t a_fadd2d a_fadd4s
3883if eq op 11
3884fm = a6_vf t a_fsub2d a_fsub4s
3885end
3886if eq op 12
3887fm = a6_vf t a_fmul2d a_fmul4s
3888end
3889a6_ew fm d a b 0 0
3890a6_vfin r d
3891end
3892
3893note packed fma (c + a * b, single rounding per lane): FMLA accumulates into Vd; when Vd aliases Va or Vb (and is
3894note not already Vc), compute in scratch v31 first so moving Vc never clobbers a live multiplicand
3895fn a6_vfma o ulong r ulong to void
3896fm = a6_vf ulong get i_oty o a_fmla2d a_fmla4s
3897a = a6_vsrc a6_arg o 0 29
3898b = a6_vsrc a6_arg o 1 30
3899c = a6_vsrc a6_arg o 2 31
3900d = a6_vdst r
3901if eq d c
3902a6_ew fm d a b 0 0
3903else
3904if or eq d a eq d b
3905if ne c 31
3906a6_ew a_orr16b 31 c c 0 0
3907end
3908a6_ew fm 31 a b 0 0
3909a6_ew a_orr16b d 31 31 0 0
3910else
3911a6_ew a_orr16b d c c 0 0
3912a6_ew fm d a b 0 0
3913end
3914end
3915a6_vfin r d
3916end
3917
3918fn a6_vsplt o ulong r ulong to void
3919t = ulong get i_oty o
3920a = a6_fsrc a6_arg o 0 29
3921d = a6_vdst r
3922a6_ew a6_vf t a_dupv2d a_dupv4s d a 0 0 0
3923a6_vfin r d
3924end
3925
3926fn a6_vext o ulong r ulong to void
3927v = a6_arg o 0
3928vt = a6_vty v
3929m = get i_oim o
3930if ge m ulong get i_t2 vt
3931a6_unsup m
3932end
3933a = a6_vsrc v 29
3934d = a6_fdst r
3935a6_ew a6_vf vt a_dupd a_dups d a m 0 0
3936a6_ffin r d
3937end
3938
3939fn a6_vqimm iv ulong sz ulong to ulong
3940if ne get i_vk iv 4
3941ret 4096
3942end
3943o = ulong get i_vo iv
3944if ne get i_oop o 1
3945ret 4096
3946end
3947k = get i_oim o
3948if ge k 65536
3949ret 4096
3950end
3951bo = shl k sz
3952if ne and bo 15 0
3953ret 4096
3954end
3955ret shr bo 4
3956end
3957
3958fn a6_vld o ulong r ulong to void
3959t = ulong get i_oty o
3960if not a6_isvt t
3961a6_unsup t
3962end
3963sz = a6_lsz ulong get i_t1 t
3964a6_aref a6_arg o 1
3965b = a6_src a6_arg o 1 16
3966iv = a6_arg o 2
3967d = a6_vdst r
3968q = a6_vqimm iv sz
3969if lt q 4096
3970a6_ew a_ldrq d b q 0 0
3971else
3972i = a6_src iv 17
3973a6_ew a_addx 15 31 i 0 sz
3974a6_ew a_ldrqr d b 15 0 0
3975end
3976a6_vfin r d
3977end
3978
3979fn a6_vst o ulong to void
3980v = a6_arg o 3
3981if not a6_isvv v
3982a6_unsup a6_vty v
3983end
3984sz = a6_lsz ulong get i_t1 a6_vty v
3985a6_aref a6_arg o 1
3986b = a6_src a6_arg o 1 16
3987iv = a6_arg o 2
3988x = a6_vsrc v 29
3989q = a6_vqimm iv sz
3990if lt q 4096
3991a6_ew a_strq x b q 0 0
3992else
3993i = a6_src iv 17
3994a6_ew a_addx 15 31 i 0 sz
3995a6_ew a_strqr x b 15 0 0
3996end
3997end
3998
3999note ---- terminators, calls, exit ----
4000fn a6_sterm o ulong op ulong x ulong to void
4001if eq op 60
4002a6_sbr o x
4003end
4004if eq op 61
4005a6_scbr o x
4006end
4007if eq op 62
4008a6_epi
4009end
4010if eq op 63
4011if a6_issl a6_arg o 1
4012a6_unsup 5
4013end
4014v = a6_arg o 1
4015a6_mvto a6_rloc v v
4016if ne get a_fs 14 0
4017a6_tsum 0 0
4018end
4019a6_epi
4020end
4021if eq op 64
4022a6_scall o
4023end
4024if eq op 65
4025a6_fx a_b 0 add a_tlab get i_oim o
4026end
4027if eq op 66
4028a6_mvto 0 a6_arg o 1
4029a6_exit
4030end
4031end
4032
4033note a6_mvto r v: register location r = value v
4034fn a6_mvto r ulong v ulong to void
4035a6_mv1 r a6_loc v
4036end
4037
4038note br: the values after the token go to the target's parameters after its token, as one parallel move
4039fn a6_sbr o ulong x ulong to void
4040t = a6_arg o 0
4041a6_mvclr
4042p = ulong get i_vnx get i_bp1 t
4043k = ulong 2
4044while ne p 0 bound 65
4045a6_mvv p a6_arg o k
4046p = ulong get i_vnx p
4047k = add k 1
4048end
4049a6_pmove
4050ti = a6_bix t
4051if ne ti add x 1
4052a6_fx a_b 0 ti
4053end
4054end
4055
4056fn a6_scbr o ulong x ulong to void
4057c = a6_src a6_arg o 1 16
4058bt = a6_bix a6_arg o 2
4059bf = a6_bix a6_arg o 3
4060if eq bt add x 1
4061a6_fx a_cbzx c bf
4062ret
4063end
4064a6_fx a_cbnzx c bt
4065if ne bf add x 1
4066a6_fx a_b 0 bf
4067end
4068end
4069
4070note call: the arguments a6_abi puts on the stack to the outgoing area [sp, 8j] (the first a6_outm slots of the
4071note frame; k below is 8 + j), the others to x0..x7 and v0..v7 (one parallel move), BL (fixed up when every function
4072note is placed), result from x0, or d0 for a float (a6_rloc)
4073fn a6_outq q ulong l ulong to void
4074if a6_isvq l
4075a6_ew a_strq sub l 600 31 q 0 0
4076ret
4077end
4078a6_ew a_ldrq 31 31 sub l 700 0 0
4079a6_ew a_strq 31 31 q 0 0
4080end
4081
4082fn a6_scall o ulong to void
4083n = a6_xcall o
4084ns = a6_abi n
4085i = ulong 0
4086while lt i n bound 137
4087p = get a_mab i
4088if ge p 1000
4089if ge p 3000
4090a6_outq shr sub p 3000 1 get a_mal i
4091else
4092k = sub p 992
4093l = get a_mal i
4094if lt l 32
4095a6_ew a_strx l 31 sub k 8 0 0
4096else
4097if a6_isvl l
4098a6_ew a_strd sub l 32 31 sub k 8 0 0
4099else
4100a6_ew a_ldrx 16 31 sub l 64 0 0
4101a6_ew a_strx 16 31 sub k 8 0 0
4102end
4103end
4104end
4105end
4106i = add i 1
4107end
4108a6_mvclr
4109i = ulong 0
4110while lt i n bound 137
4111p = get a_mab i
4112if lt p 1000
4113a6_mvadd p get a_mal i
4114end
4115i = add i 1
4116end
4117a6_pmove
4118c = get a_ncl 0
4119if ne ar_need 28 c 0
4120ar_die
4121end
4122put ar_m add get ar_rb 105 c get a_cn 0
4123put ar_m add get ar_rb 106 c a6_arg o 1
4124put a_ncl 0 add c 1
4125a6_emitw 0
4126r = ulong get i_ors o
4127if ne r 0
4128a6_mvfrom r a6_rloc r
4129end
4130end
4131
4132note a6_mvfrom v r: value v = register location r
4133fn a6_mvfrom v ulong r ulong to void
4134a6_mv1 a6_loc v r
4135end
4136
4137note ---- checks (IR.md s7.9): compare, then branch to the stub of the op's trap code ----
4138fn a6_schk o ulong op ulong to void
4139c = ulong get i_oim o
4140if le op 73
4141a6_cova o op c
4142ret
4143end
4144if eq op 74
4145a6_fx a_cbzx a6_src a6_arg o 1 16 add a_tlab c
4146ret
4147end
4148if eq op 75
4149a6_cdivo o c
4150ret
4151end
4152if eq op 76
4153a6_cshl o c
4154ret
4155end
4156if eq op 77
4157a6_cshift o c
4158ret
4159end
4160if eq op 78
4161a6_cconv o c
4162ret
4163end
4164if eq op 81
4165a6_ew a_subsx 31 a6_src a6_arg o 1 16 a6_src a6_arg o 2 17 0 0
4166a6_fx a_bcond 2 add a_tlab c
4167ret
4168end
4169if eq op 82
4170a6_fx a_cbzx a6_src a6_arg o 1 16 add a_tlab c
4171ret
4172end
4173if eq op 79
4174a6_cbnd o c
4175ret
4176end
4177if eq op 80
4178a6_cslc o c
4179ret
4180end
4181a6_unsup op
4182end
4183
4184note chk.bounds on a SLICE r: trap when e = 0, e > len r (B.LO on the borrow of len - e) or i > len r - e
4185fn a6_cbsl o ulong c ulong to void
4186i = a6_src a6_arg o 2 16
4187e = a6_src a6_arg o 3 17
4188a6_fx a_cbzx e add a_tlab c
4189l = a6_lsrc a6_arg o 1 15
4190a6_ew a_subsx 15 l e 0 0
4191a6_fx a_bcond 3 add a_tlab c
4192a6_ew a_subsx 31 i 15 0 0
4193a6_fx a_bcond 8 add a_tlab c
4194end
4195
4196note chk.slice on a SLICE r: trap when off > len r or n > len r - off
4197fn a6_csls o ulong c ulong to void
4198f = a6_src a6_arg o 2 16
4199n = a6_src a6_arg o 3 17
4200l = a6_lsrc a6_arg o 1 15
4201a6_ew a_subsx 31 f l 0 0
4202a6_fx a_bcond 8 add a_tlab c
4203a6_ew a_subx 15 l f 0 0
4204a6_ew a_subsx 31 n 15 0 0
4205a6_fx a_bcond 8 add a_tlab c
4206end
4207
4208note chk.bounds r i e: trap when i + e > len r or e = 0. A constant e (the covering form of every load and store,
4209note s8) is one compare of i with len - e (B.HI: unsigned, exact, no add that could wrap); e = 0 or e > len always
4210note traps; any other e computes len - e first (B.LO on a borrow)
4211fn a6_cbnd o ulong c ulong to void
4212if a6_issl a6_arg o 1
4213a6_cbsl o c
4214ret
4215end
4216l = a6_alen a6_arg o 1
4217v = a6_arg o 3
4218i = a6_src a6_arg o 2 16
4219if a6_iscon v
4220e = get i_oim get i_vo v
4221if or eq e 0 gt e l
4222a6_fx a_b 0 add a_tlab c
4223ret
4224end
4225a6_cmpk i sub l e
4226a6_fx a_bcond 8 add a_tlab c
4227ret
4228end
4229e = a6_src v 17
4230a6_fx a_cbzx e add a_tlab c
4231a6_movi 15 l
4232a6_ew a_subsx 15 15 e 0 0
4233a6_fx a_bcond 3 add a_tlab c
4234a6_ew a_subsx 31 i 15 0 0
4235a6_fx a_bcond 8 add a_tlab c
4236end
4237
4238note chk.slice r off n: trap when off > len r or n > len r - off
4239fn a6_cslc o ulong c ulong to void
4240if a6_issl a6_arg o 1
4241a6_csls o c
4242ret
4243end
4244l = a6_alen a6_arg o 1
4245f = a6_src a6_arg o 2 16
4246n = a6_src a6_arg o 3 17
4247a6_cmpk f l
4248a6_fx a_bcond 8 add a_tlab c
4249a6_movi 15 l
4250a6_ew a_subx 15 15 f 0 0
4251a6_ew a_subsx 31 n 15 0 0
4252a6_fx a_bcond 8 add a_tlab c
4253end
4254
4255note a6_cmpk r k: CMP Xr, #k (k <= 4095) or by x15
4256fn a6_cmpk r ulong k ulong to void
4257if le k 4095
4258a6_ew a_subsxi 31 r k 0 0
4259ret
4260end
4261a6_movi 15 k
4262a6_ew a_subsx 31 r 15 0 0
4263end
4264
4265note a6_iscon v: v is the result of a const op
4266fn a6_iscon v ulong to bool
4267if ne get i_vk v 4
4268ret false
4269end
4270ret eq get i_oop get i_vo v 1
4271end
4272
4273note chk.add sub mul neg: 64-bit by flags (V signed, C unsigned) or the high product; narrower types compute the exact
4274note result in 64 bits (x15) and trap when re-extending it changes it
4275fn a6_cova o ulong op ulong c ulong to void
4276v = a6_arg o 1
4277t = a6_vty v
4278if not a6_isint t
4279a6_unsup t
4280end
4281a = a6_src v 16
4282b = ulong 31
4283if ne op 73
4284b = a6_src a6_arg o 2 17
4285end
4286if eq a6_wid t 64
4287a6_cov64 op t a b c
4288ret
4289end
4290if eq op 70
4291a6_ew a_addx 15 a b 0 0
4292end
4293if eq op 71
4294a6_ew a_subx 15 a b 0 0
4295end
4296if eq op 72
4297a6_ew a_mulx 15 a b 0 0
4298end
4299if eq op 73
4300a6_ew a_subx 15 31 a 0 0
4301end
4302a6_cext t c
4303end
4304
4305note a6_cext t c: trap c unless x15 equals its own extension to t (x16 is free: the operands were read)
4306fn a6_cext t ulong c ulong to void
4307a6_mov 16 15
4308a6_canon 16 t
4309a6_ew a_subsx 31 15 16 0 0
4310a6_fx a_bcond 1 add a_tlab c
4311end
4312
4313fn a6_cov64 op ulong t ulong a ulong b ulong c ulong to void
4314s = a6_sgn t
4315l = add a_tlab c
4316if eq op 72
4317a6_cmul64 s a b l
4318ret
4319end
4320if eq op 70
4321a6_ew a_addsx 31 a b 0 0
4322end
4323if eq op 71
4324a6_ew a_subsx 31 a b 0 0
4325end
4326if eq op 73
4327a6_ew a_subsx 31 31 a 0 0
4328end
4329if s
4330a6_fx a_bcond 6 l
4331ret
4332end
4333if eq op 70
4334a6_fx a_bcond 2 l
4335end
4336if eq op 71
4337a6_fx a_bcond 3 l
4338end
4339if eq op 73
4340a6_fx a_bcond 1 l
4341end
4342end
4343
4344note signed: the high half must be the sign of the low half; unsigned: the high half must be 0
4345fn a6_cmul64 s bool a ulong b ulong l ulong to void
4346if s
4347a6_ew a_mulx 15 a b 0 0
4348a6_ew a_smulh 16 a b 0 0
4349a6_ew a_subsx 31 16 15 2 63
4350a6_fx a_bcond 1 l
4351else
4352a6_ew a_umulh 15 a b 0 0
4353a6_fx a_cbnzx 15 l
4354end
4355end
4356
4357note chk.divovf a b: trap when a = MIN and b = -1 of a's type: ((b + 1) | (a ^ MIN)) = 0
4358fn a6_cdivo o ulong c ulong to void
4359v = a6_arg o 1
4360t = a6_vty v
4361a = a6_src v 16
4362b = a6_src a6_arg o 2 17
4363a6_ew a_addxi 15 b 1 0 0
4364a6_movi 17 sub ulong 0 shl ulong 1 sub a6_wid t 1
4365a6_ew a_eorx 17 a 17 0 0
4366a6_ew a_orrx 15 15 17 0 0
4367a6_fx a_cbzx 15 add a_tlab c
4368end
4369
4370note chk.shl a b (signed, b already < width): 64-bit: (a << b) >> b must be a; narrower: a << b must survive extension
4371fn a6_cshl o ulong c ulong to void
4372v = a6_arg o 1
4373t = a6_vty v
4374a = a6_src v 16
4375b = a6_src a6_arg o 2 17
4376a6_ew a_lslvx 15 a b 0 0
4377if eq a6_wid t 64
4378a6_ew a_asrvx 17 15 b 0 0
4379a6_ew a_subsx 31 17 a 0 0
4380a6_fx a_bcond 1 add a_tlab c
4381ret
4382end
4383a6_cext t c
4384end
4385
4386note chk.shift b: trap unless 0 <= b < width (unsigned compare of the extended value)
4387fn a6_cshift o ulong c ulong to void
4388v = a6_arg o 1
4389t = a6_vty v
4390b = a6_src v 16
4391a6_ew a_subsxi 31 b a6_wid t 0 0
4392a6_fx a_bcond 2 add a_tlab c
4393end
4394
4395note chk.conv to integer type ty from integer v (float sources are not in the slice)
4396fn a6_cconv o ulong c ulong to void
4397d = ulong get i_oty o
4398v = a6_arg o 1
4399s = a6_vty v
4400if or not a6_isint s not a6_isint d
4401a6_unsup s
4402end
4403a = a6_src v 16
4404l = add a_tlab c
4405wd = a6_wid d
4406if eq wd 64
4407if and a6_sgn s eq d 5
4408a6_ew a_subsxi 31 a 0 0 0
4409a6_fx a_bcond 11 l
4410end
4411if and eq s 5 eq d 9
4412a6_ew a_subsxi 31 a 0 0 0
4413a6_fx a_bcond 11 l
4414end
4415ret
4416end
4417if eq s 5
4418a6_movi 15 sub shl ulong 1 wd 1
4419if a6_sgn d
4420a6_movi 15 sub shl ulong 1 sub wd 1 1
4421end
4422a6_ew a_subsx 31 a 15 0 0
4423a6_fx a_bcond 8 l
4424ret
4425end
4426a6_mov 15 a
4427a6_cext d c
4428end
4429
4430note ---- E loop shape (D13) ----
4431note A block ending `r = call f(...); s = add a r` (or `add r a`) `; retv s` inside f itself, with r and s used once
4432note each, becomes `acc = acc + a; f's parameters = the call's arguments; B head`: no call, no add, no retv. Every other
4433note `retv x` of f returns acc + x. acc (wire a_taw, type a_tat) is 0 at the head, which follows the entry parameter
4434note moves. Integer add is modulo 2^w (IR.md s7.3), hence associative and commutative, so every result is exact.
4435note A checked add (chk.add on r) is a second use of r and keeps the call. Stack depth falls, and trap 6 is not
4436note semantic (IR.md s7.9, D9).
4437fn a6_tre f ulong to void
4438put a_fs 14 0
4439x = ulong 0
4440while lt x get a_fs 1 bound 65536
4441a6_tblk f get a_bh x
4442x = add x 1
4443end
4444end
4445
4446fn a6_tblk f ulong b ulong to void
4447c = ulong 0
4448m = ulong 0
4449l = ulong 0
4450o = ulong get i_bo1 b
4451while ne o 0 bound 4194305
4452c = m
4453m = l
4454l = o
4455o = ulong get i_onx o
4456end
4457if eq c 0
4458ret
4459end
4460if not a6_tpat f c m l
4461ret
4462end
4463n = get a_fs 14
4464if ge n len a_tco
4465ret
4466end
4467put a_tco n c
4468put a_fs 14 add n 1
4469put a_tat 0 ulong get i_oty m
4470end
4471
4472note a6_tpat f c m l: the last three ops of a block are the E shape (call c to f, add m, retv l)
4473fn a6_tpat f ulong c ulong m ulong l ulong to bool
4474if ne get i_oop c 64
4475ret false
4476end
4477if ne get i_oop m 10
4478ret false
4479end
4480if ne get i_oop l 63
4481ret false
4482end
4483r = ulong get i_ors c
4484s = ulong get i_ors m
4485if eq r 0
4486ret false
4487end
4488if ne a6_arg c 1 f
4489ret false
4490end
4491if ne a6_arg l 1 s
4492ret false
4493end
4494if not or eq a6_arg m 0 r eq a6_arg m 1 r
4495ret false
4496end
4497if ne a6_uses r 1
4498ret false
4499end
4500if ne a6_uses s 1
4501ret false
4502end
4503ret a6_isint ulong get i_oty m
4504end
4505
4506note a6_uses v: the number of value operands in the function that are v
4507fn a6_uses v ulong to ulong
4508c = ulong 0
4509x = ulong 0
4510while lt x get a_fs 1 bound 65536
4511o = ulong get i_bo1 get a_bh x
4512while ne o 0 bound 4194305
4513op = get i_oop o
4514n = ulong get i_ona o
4515k = ulong 0
4516while lt k n bound 1048576
4517if eq ir_okd uint op k 0
4518if eq a6_arg o k v
4519c = add c 1
4520end
4521end
4522k = add k 1
4523end
4524o = ulong get i_onx o
4525end
4526x = add x 1
4527end
4528ret c
4529end
4530
4531fn a6_istc o ulong to bool
4532i = ulong 0
4533while lt i get a_fs 14 bound 64
4534if eq get a_tco i o
4535ret true
4536end
4537i = add i 1
4538end
4539ret false
4540end
4541
4542note a6_tacc: the accumulator is one more wire, defined at 0 and live to the end (it crosses every call in f)
4543fn a6_tacc to void
4544if eq get a_fs 14 0
4545ret
4546end
4547w = get a_fs 0
4548if ne ar_need 26 w 0
4549ar_die
4550end
4551put a_taw 0 w
4552put ar_m add get ar_rb 91 w 0
4553put ar_m add get ar_rb 96 w 0
4554ar_pc 94 w 0
4555ar_pc 99 w 0
4556ar_pc 95 w 255
4557put ar_m add get ar_rb 92 w 0
4558put ar_m add get ar_rb 93 w get a_fs 3
4559put a_fs 0 add w 1
4560end
4561
4562fn a6_accl to ulong
4563w = get a_taw 0
4564r = ulong uchar get ar_m add get ar_rb 95 w
4565if lt r 32
4566ret r
4567end
4568ret add 64 sub get ar_m add get ar_rb 96 w 1
4569end
4570
4571note a6_tsum d a: register d = acc + register a, re-extended to the accumulator's type (acc from its slot via x17)
4572fn a6_tsum d ulong a ulong to void
4573l = a6_accl
4574c = l
4575if ge l 32
4576a6_ew a_ldrx 17 31 sub l 64 0 0
4577c = ulong 17
4578end
4579a6_ew a_addx d c a 0 0
4580a6_canon d get a_tat 0
4581end
4582
4583note a6_stail o: the tail site at call o (the add and retv after it emit nothing)
4584fn a6_stail o ulong to void
4585m = ulong get i_onx o
4586r = ulong get i_ors o
4587v = a6_arg m 0
4588if eq v r
4589v = a6_arg m 1
4590end
4591a = a6_src v 16
4592l = a6_accl
4593if lt l 32
4594a6_tsum l a
4595else
4596a6_tsum 17 a
4597a6_ew a_strx 17 31 sub l 64 0 0
4598end
4599a6_mvclr
4600p = ulong get i_fp1 get a_fs 8
4601k = ulong 2
4602while ne p 0 bound 65
4603a6_mvv p a6_arg o k
4604p = ulong get i_vnx p
4605k = add k 1
4606end
4607a6_pmove
4608a6_ew a_b a6_wd get a_cn 0 get a_fs 15 0 0 0 0
4609end
4610
4611note ---- one function ----
4612fn a6_func f ulong to void
4613if eq get i_fk f 1
4614a6_unsup 1
4615end
4616nv = ulong ir_num uint f
4617if ne ar_need 26 add nv 1 0
4618ar_die
4619end
4620put a_fs 0 nv
4621put a_fs 8 f
4622a6_wmap f
4623a6_lay f
4624a6_tre f
4625a6_ucnt
4626put a_skip 0 0
4627put a_fs 16 0
4628lw = shr add nv 63 6
4629put a_fs 2 lw
4630if ne ar_need 27 mul get a_fs 1 lw 0
4631ar_die
4632end
4633a6_ccalls
4634a6_lzero
4635a6_lgen
4636a6_lflow
4637a6_ivinit
4638a6_ivuse
4639a6_ivlive
4640a6_tacc
4641a6_shw
4642a6_ivcross
4643a6_scan
4644a6_frame
4645pc = get a_cn 0
4646nc = get a_ncl 0
4647a6_fbody f
4648if ne get a_fs 16 0
4649put a_cn 0 pc
4650put a_ncl 0 nc
4651a6_fbody f
4652end
4653end
4654
4655note a6_al64: pad the code with NOP (C6.2.238, never executed: the previous function ends in RET or its trap stubs)
4656note to a 64-byte boundary. Text file offset k loads at 0x400000 + k, so this aligns the virtual address (D13).
4657fn a6_al64 to void
4658while ne and get a_cn 0 63 0 bound 16
4659a6_emitw 0xd503201f
4660end
4661end
4662
4663note a6_outm: the outgoing argument slots of the function, the most arguments past the eighth of any of its calls;
4664note they are frame slots 0.. (spill slots follow), so the callee finds argument 8+j at its [sp + frame + 8j]
4665fn a6_outm to ulong
4666m = ulong 0
4667x = ulong 0
4668while lt x get a_fs 1 bound 65536
4669o = ulong get i_bo1 get a_bh x
4670while ne o 0 bound 4194305
4671if eq get i_oop o 64
4672n = a6_xstk o
4673if gt n m
4674m = n
4675end
4676end
4677o = ulong get i_onx o
4678end
4679x = add x 1
4680end
4681ret m
4682end
4683
4684note a6_inpar n: the machine parameters of the n in a_mal (a6_xpar) that a6_abi puts on the stack, from the caller's
4685note outgoing area through x17 = sp + frame (parameter j of the stack at [x17, 8j])
4686fn a6_inpar n ulong to void
4687m = ulong 0
4688k = ulong 0
4689while lt k n bound 137
4690if ge get a_mab k 1000
4691m = add m 1
4692end
4693k = add k 1
4694end
4695if eq m 0
4696ret
4697end
4698f = get a_fs 7
4699a6_ew a_addxi 17 31 shr f 12 1 0
4700a6_ew a_addxi 17 17 and f 4095 0 0
4701k = ulong 0
4702while lt k n bound 137
4703p = get a_mab k
4704if ge p 1000
4705if ge p 3000
4706a6_inpq shr sub p 3000 1 get a_mal k
4707else
4708j = sub p 1000
4709d = get a_mal k
4710if lt d 32
4711a6_ew a_ldrx d 17 j 0 0
4712else
4713if a6_isvl d
4714a6_ew a_ldrd sub d 32 17 j 0 0
4715else
4716a6_ew a_ldrx 16 17 j 0 0
4717a6_ew a_strx 16 31 sub d 64 0 0
4718end
4719end
4720end
4721end
4722k = add k 1
4723end
4724end
4725
4726fn a6_inpq q ulong d ulong to void
4727if a6_isvq d
4728a6_ew a_ldrq sub d 600 17 q 0 0
4729ret
4730end
4731a6_ew a_ldrq 31 17 q 0 0
4732a6_ew a_strq 31 31 sub d 700 0 0
4733end
4734
4735fn a6_fbody f ulong to void
4736a6_al64
4737put a_skip 0 0
4738put a_fpc f get a_cn 0
4739put a_fs 12 0
4740put a_fs 10 0
4741a6_pro
4742a6_mvclr
4743n = a6_xpar f
4744ns = a6_abi n
4745k = ulong 0
4746while lt k n bound 137
4747p = get a_mab k
4748if lt p 1000
4749a6_mvadd get a_mal k p
4750end
4751k = add k 1
4752end
4753a6_pmove
4754a6_inpar n
4755if ne get a_fs 14 0
4756l = a6_accl
4757if lt l 32
4758a6_ew a_movzx l 0 0 0 0
4759else
4760a6_ew a_strx 31 31 sub l 64 0 0
4761end
4762put a_fs 15 get a_cn 0
4763end
4764x = ulong 0
4765while lt x get a_fs 1 bound 65536
4766put a_bpc x get a_cn 0
4767o = ulong get i_bo1 get a_bh x
4768while ne o 0 bound 4194305
4769if a6_istc o
4770a6_stail o
4771o = ulong 0
4772else
4773a6_sel o x
4774o = ulong get i_onx o
4775end
4776end
4777x = add x 1
4778end
4779a6_tstubs
4780a6_fixb
4781end
4782
4783note ---- module: ELF (omc's map), _start, functions, call fixups ----
4784note a6_hasf: some value of the module is an f32 or f64; then _start writes FPCR = 0 (MSR FPCR, XZR) before main
4785fn a6_hasf to bool
4786n = get i_n 3
4787f = ulong 1
4788while le f n bound 65537
4789p = ulong get i_fp1 f
4790while ne p 0 bound 65
4791if or a6_isfv p a6_isvv p
4792ret true
4793end
4794p = ulong get i_vnx p
4795end
4796b = ulong get i_fb1 f
4797while ne b 0 bound 65537
4798p = ulong get i_bp1 b
4799while ne p 0 bound 65
4800if or a6_isfv p a6_isvv p
4801ret true
4802end
4803p = ulong get i_vnx p
4804end
4805o = ulong get i_bo1 b
4806while ne o 0 bound 4194305
4807r = ulong get i_ors o
4808if ne r 0
4809if or a6_isfv r a6_isvv r
4810ret true
4811end
4812end
4813o = ulong get i_onx o
4814end
4815b = ulong get i_bnx b
4816end
4817f = add f 1
4818end
4819ret false
4820end
4821
4822note entry (D11): the single export (kind 2) with no parameters and result i32
4823fn a6_isent f ulong to bool
4824if ne get i_fk f 2
4825ret false
4826end
4827ret and eq get i_fnp f 0 eq get i_frs f 8
4828end
4829
4830fn a6_entry to ulong
4831n = get i_n 3
4832e = ulong 0
4833f = ulong 1
4834while le f n bound 65537
4835if a6_isent f
4836if ne e 0
4837a6_unsup f
4838end
4839e = f
4840end
4841f = add f 1
4842end
4843if eq e 0
4844a6_unsup 0
4845end
4846ret e
4847end
4848
4849note The callback entries of a Mach-O program (DESIGN.md s14). Sixteen entries of two words (MOVZ X16, #k; B body)
4850note and one body. Library code enters with the C convention on its own stack, inside a libcall. The body keeps
4851note x29, x30, x27 and x28 and the link words 16 and 24 in a 48-byte C frame, finds x28 again from its own address,
4852note makes the C stack the one later libcalls use (link word 16), moves to the Omega stack where the enclosing
4853note libcall left it (link word 24), calls the callback function with (k, x0..x4) and gives its x0 back.
4854fn a6_back to void
4855put a_bks 0 0
4856n = get i_n 3
4857b = ulong 0
4858f = ulong 1
4859while le f n bound 65537
4860if ge ulong get i_ffl f 2
4861if ne b 0
4862a6_unsup f
4863end
4864b = f
4865end
4866f = add f 1
4867end
4868if eq b 0
4869ret
4870end
4871if or ne ulong get i_fnp b 6 ne ulong get i_frs b 5
4872a6_unsup b
4873end
4874put a_bks 0 get a_cn 0
4875for k ulong 16
4876a6_ew a_movzx 16 k 0 0 0
4877a6_emitw add 335544320 sub 31 mul 2 k
4878end
4879a6_emitw 0xa9bd7bfd
4880a6_emitw 0x910003fd
4881a6_emitw 0xa90173fb
4882pc = get a_cn 0
4883a6_ew a_adr 9 0 0 0 0
4884a6_movi 10 sub 16777216 pc
4885a6_ew a_addx 28 9 10 0 0
4886a6_emitw 0xd1401389
4887a6_emitw 0xa9412d2a
4888a6_emitw 0xa9022fea
4889a6_emitw 0x910003fb
4890a6_emitw 0xf900093b
4891a6_emitw 0x9100017f
4892a6_emitw 0xaa0403e5
4893a6_emitw 0xaa0303e4
4894a6_emitw 0xaa0203e3
4895a6_emitw 0xaa0103e2
4896a6_emitw 0xaa0003e1
4897a6_emitw 0xaa1003e0
4898c = get a_ncl 0
4899if ne ar_need 28 c 0
4900ar_die
4901end
4902put ar_m add get ar_rb 105 c get a_cn 0
4903put ar_m add get ar_rb 106 c b
4904put a_ncl 0 add c 1
4905a6_emitw 0
4906a6_emitw 0x9100037f
4907a6_emitw 0xd1401389
4908a6_emitw 0xa9422fea
4909a6_emitw 0xa9012d2a
4910a6_emitw 0xa94173fb
4911a6_emitw 0xa8c37bfd
4912a6_emitw 0xd65f03c0
4913end
4914
4915fn a6_comp to void
4916put a_cn 0 176
4917if eq get a_tgt 0 1
4918put a_cn 0 16384
4919end
4920put a_ncl 0 0
4921e = a6_entry
4922if eq get a_tgt 0 1
4923a6_ew a_adr 16 0 0 0 0
4924a6_ew a_movzx 17 49152 0 0 0
4925a6_ew a_movkx 17 255 1 0 0
4926a6_ew a_addx 28 16 17 0 0
4927a6_ew a_movzx 17 128 1 0 0
4928a6_ew a_addx 16 28 17 0 0
4929note the link page (a6_macho, DESIGN.md s13) keeps the stack dyld entered on: MOV X9, SP; SUB X10, X28, #4, LSL #12;
4930note STR X9, [X10, #16]. libcall runs library code on that stack.
4931a6_emitw 0x910003e9
4932a6_emitw 0xd140138a
4933a6_emitw 0xf9000949
4934else
4935if a6_hasenv
4936a6_ew a_addxi 18 31 0 0 0
4937end
4938a6_ew a_movzx 28 4096 1 0 0
4939a6_ew a_movzx 16 4224 1 0 0
4940end
4941a6_ew a_addxi 31 16 0 0 0
4942if a6_hasf
4943fz = ulong 31
4944a6_ew a_msrfpcr fz 0 0 0 0
4945end
4946if ne ar_need 28 0 0
4947ar_die
4948end
4949put ar_m add get ar_rb 105 0 get a_cn 0
4950put ar_m add get ar_rb 106 0 e
4951put a_ncl 0 1
4952a6_emitw 0
4953a6_exit
4954a6_glay
4955put a_bks 0 0
4956if eq get a_tgt 0 1
4957a6_back
4958end
4959n = get i_n 3
4960f = ulong 1
4961while le f n bound 65537
4962a6_func f
4963f = add f 1
4964end
4965i = ulong 0
4966while lt i get a_ncl 0 bound 16777217
4967pc = get ar_m add get ar_rb 105 i
4968a6_putw pc a6_enc a_bl a6_wd pc get a_fpc get ar_m add get ar_rb 106 i 0 0 0 0
4969i = add i 1
4970end
4971if eq get a_tgt 0 1
4972a6_macho get a_cn 0
4973else
4974a6_elf get a_cn 0
4975end
4976end
4977
4978fn a6_pn o ulong v ulong k ulong to void
4979i = ulong 0
4980while lt i k bound 8
4981ar_bp 107 add o i uchar and shr v shl i 3 255
4982i = add i 1
4983end
4984end
4985
4986note ELF64 header and two PT_LOAD headers: text R+X at 0x400000 (file offset 0), RW pool at 0x10000000: the 8 MiB
4987note stack, then the class-0 globals (zero-filled, no file bytes)
4988fn a6_elf n ulong to void
4989i = ulong 0
4990while lt i 176 bound 176
4991ar_bp 107 i 0
4992i = add i 1
4993end
4994a6_pn 0 0x00010102464c457f 8
4995a6_pn 16 0x00b70002 4
4996a6_pn 20 1 4
4997a6_pn 24 0x4000b0 8
4998a6_pn 32 64 8
4999a6_pn 52 0x00380040 4
5000a6_pn 56 2 2
5001a6_pn 64 0x500000001 8
5002a6_pn 80 0x400000 8
5003a6_pn 88 0x400000 8
5004a6_pn 96 n 8
5005a6_pn 104 n 8
5006a6_pn 112 4096 8
5007a6_pn 120 0x600000001 8
5008a6_pn 136 0x10000000 8
5009a6_pn 144 0x10000000 8
5010a6_pn 160 add 0x800000 and add get a_gtot 0 4095 not ulong 4095 8
5011a6_pn 168 4096 8
5012end
5013
5014note a6_macho n: the Mach-O container around the code a_code[16384, n) (`@ng a64 macho`). File: a 16 KiB header
5015note page (mach_header_64 and 12 load commands, 680 bytes; the rest is room for the signature command), the code,
5016note zero padding to a 16 KiB multiple F, then 40 bytes of __LINKEDIT (24 of bind records, then the empty 16-byte
5017note string table). Memory, from the slid base B (0x100000000 before the slide): __TEXT B..B+F read+execute (F at
5018note most 16 MiB less 16 KiB), __DATA from B+16 MiB-16 KiB zero-filled read+write (the 16 KiB link page, 8 MiB of
5019note stack, then the class-0 globals, rounded up to 16 KiB), __LINKEDIT after it. dyld binds two words of the link
5020note page: dlopen at +0 and dlsym at +8 (LC_DYLD_INFO_ONLY bind_off F, bind_size 24). LC_MAIN enters at file
5021note offset 16384, the start code: ADR x16, .; x28 = x16 + 0xFFC000 (the stack floor B+16 MiB); the entry sp to
5022note link page +16; sp = x28 + 8 MiB; BL main; Darwin exit. Library code runs only through libcall (a6_hlib). The
5023note code signature (required on arm64 macOS) is added on the Mac by `codesign --sign -`, which this layout leaves
5024note room for.
5025fn a6_macho n ulong to void
5026f = and add n 16383 not ulong 16383
5027if gt f 16760832
5028a6_die 4 3 18446744073709551615
5029end
5030if ne ar_need 29 add f 39 0
5031ar_die
5032end
5033i = ulong 0
5034while lt i 16384 bound 16384
5035ar_bp 107 i 0
5036i = add i 1
5037end
5038gv = add 0x800000 and add get a_gtot 0 16383 not ulong 16383
5039a6_pn 0 0xfeedfacf 4
5040a6_pn 4 0x0100000c 4
5041a6_pn 12 2 4
5042a6_pn 16 12 4
5043a6_pn 20 680 4
5044a6_pn 24 0x200085 4
5045a6_pn 32 25 4
5046a6_pn 36 72 4
5047a6_pn 40 4997382908861767519 8
5048a6_pn 48 20306 8
5049a6_pn 64 0x100000000 8
5050a6_pn 104 25 4
5051a6_pn 108 152 4
5052a6_pn 112 92738097012575 8
5053a6_pn 120 0 8
5054a6_pn 128 0x100000000 8
5055a6_pn 136 f 8
5056a6_pn 152 f 8
5057a6_pn 160 5 4
5058a6_pn 164 5 4
5059a6_pn 168 1 4
5060a6_pn 176 128060447022943 8
5061a6_pn 184 0 8
5062a6_pn 192 92738097012575 8
5063a6_pn 200 0 8
5064a6_pn 208 0x100004000 8
5065a6_pn 216 sub n 16384 8
5066a6_pn 224 16384 4
5067a6_pn 228 6 4
5068a6_pn 240 0x80000400 4
5069a6_pn 256 25 4
5070a6_pn 260 72 4
5071a6_pn 264 71830128058207 8
5072a6_pn 272 0 8
5073a6_pn 280 0x100ffc000 8
5074a6_pn 288 add gv 16384 8
5075a6_pn 296 f 8
5076a6_pn 312 3 4
5077a6_pn 316 3 4
5078a6_pn 328 25 4
5079a6_pn 332 72 4
5080a6_pn 336 4919420967581409119 8
5081a6_pn 344 21577 8
5082a6_pn 352 add 0x101000000 gv 8
5083a6_pn 360 16384 8
5084a6_pn 368 f 8
5085a6_pn 376 40 8
5086a6_pn 384 1 4
5087a6_pn 388 1 4
5088a6_pn 400 0x80000022 4
5089a6_pn 404 48 4
5090a6_pn 416 f 4
5091a6_pn 420 24 4
5092a6_pn 448 2 4
5093a6_pn 452 24 4
5094a6_pn 456 add f 24 4
5095a6_pn 464 add f 24 4
5096a6_pn 468 16 4
5097a6_pn 472 11 4
5098a6_pn 476 80 4
5099a6_pn 552 14 4
5100a6_pn 556 32 4
5101a6_pn 560 12 4
5102a6_pn 564 7091318039310988591 8
5103a6_pn 572 431316624431 8
5104a6_pn 584 12 4
5105a6_pn 588 56 4
5106a6_pn 592 24 4
5107a6_pn 600 65536 4
5108a6_pn 604 65536 4
5109a6_pn 608 7091318039310988591 8
5110a6_pn 616 8391183929761688623 8
5111a6_pn 624 7816388778119818597 8
5112a6_pn 632 25193 8
5113a6_pn 640 50 4
5114a6_pn 644 24 4
5115a6_pn 648 1 4
5116a6_pn 652 720896 4
5117a6_pn 656 720896 4
5118a6_pn 664 27 4
5119a6_pn 668 24 4
5120a6_pn 672 7161089769139693422 8
5121a6_pn 680 212436143976 8
5122a6_pn 688 0x80000028 4
5123a6_pn 692 24 4
5124a6_pn 696 16384 8
5125note the bind records: dylib 1, _dlopen, pointer, segment 2 offset 0, bind; _dlsym, bind (the next word); done
5126a6_pn f 0x65706f6c645f4011 8
5127a6_pn add f 8 0x5f4090007251006e 8
5128a6_pn add f 16 0x0090006d79736c64 8
5129put a_cn 0 add f 40
5130end
5131
5132note a6_glay: class-0 globals in the RW pool from 0x10800000 (16-byte aligned, as omc), at most 1 GiB; class-1
5133note globals' bytes into the code image here (between the start code and the first function), padded to a word
5134note Their addresses are arena group a64glob (index = global id), bounded by the arena (no-caps 2026-11-11).
5135fn a6_glay to void
5136n = get i_n 2
5137p = ulong 0
5138g = ulong 1
5139if ne ar_need 25 n 0
5140ar_die
5141end
5142while le g n bound 134217729
5143s = ir_tsize uint get ar_m add get ar_rb 87 g
5144if eq ar_c 88 g 2
5145s = 16
5146end
5147if eq ar_c 88 g 1
5148if eq get a_tgt 0 1
5149put ar_m add get ar_rb 90 g sub 16777216 get a_cn 0
5150else
5151put ar_m add get ar_rb 90 g add 0x400000 get a_cn 0
5152end
5153a6_gdat g s
5154else
5155if eq get a_tgt 0 1
5156put ar_m add get ar_rb 90 g add 0x800000 p
5157else
5158put ar_m add get ar_rb 90 g add 0x10800000 p
5159end
5160p = and add add p s 15 not ulong 15
5161end
5162if gt p 1073741824
5163a6_unsup p
5164end
5165g = add g 1
5166end
5167put a_gtot 0 p
5168end
5169
5170fn a6_gdat g ulong s ulong to void
5171c = get a_cn 0
5172if gt add c add s 3 134217728
5173a6_die 5 7 c
5174end
5175if ne ar_need 29 add c add s 3 0
5176ar_die
5177end
5178d = get ar_m add get ar_rb 89 g
5179k = ulong 0
5180while lt k s bound 1073741824
5181ar_bp 107 add c k ar_bg 2 add d k
5182k = add k 1
5183end
5184while ne and k 3 0 bound 4
5185ar_bp 107 add c k 0
5186k = add k 1
5187end
5188put a_cn 0 add c k
5189end
5190
5191note the mode lines of this unit: 0 `@ng a64-enc` (encoder test), 1 `@ng a64` (compile, save pairs), 2 `@ng a64 split`
5192note (compile, separate saves)
5193text a_tmag "@ng a64-enc"
5194text a_tmg1 "@ng a64"
5195text a_tmg2 "@ng a64 split"
5196text a_tmg3 "@ng a64 macho"
5197fn a6_tlen k ulong to ulong
5198if eq k 0
5199ret len a_tmag
5200end
5201if eq k 1
5202ret len a_tmg1
5203end
5204if eq k 2
5205ret len a_tmg2
5206end
5207ret len a_tmg3
5208end
5209
5210fn a6_tch k ulong i ulong to ulong
5211if eq k 0
5212ret ulong get a_tmag i
5213end
5214if eq k 1
5215ret ulong get a_tmg1 i
5216end
5217if eq k 2
5218ret ulong get a_tmg2 i
5219end
5220ret ulong get a_tmg3 i
5221end
5222
5223note a6_isln k m: the mode line (length m, main's ng_mlb) is exactly mode line k
5224fn a6_isln k ulong m ulong to bool
5225n = a6_tlen k
5226if ne m n
5227ret false
5228end
5229i = ulong 0
5230while lt i n bound 16
5231if ne ng_mlb i a6_tch k i
5232ret false
5233end
5234i = add i 1
5235end
5236ret true
5237end
5238
5239note a6_mode m: main's hook before it reads the payload: the mode the mode line (length m) names, 0 `@ng a64-enc`,
5240note 1 `@ng a64`, 2 `@ng a64 split`, 3 `@ng a64 macho`; 255 when it is not a mode of this unit
5241fn a6_mode m ulong to ulong
5242k = ulong 0
5243while lt k 4 bound 4
5244if a6_isln k m
5245ret k
5246end
5247k = add k 1
5248end
5249ret 255
5250end
5251
5252note a6_run a n: run mode a (a6_mode) on the payload i_in[0, n) main read; exits
5253fn a6_run a ulong n ulong to void
5254put a_inlen 0 n
5255a6_init
5256if eq a 0
5257a6_enctest
5258end
5259if eq a 3
5260put a_sform 0 0
5261put a_tgt 0 1
5262else
5263put a_sform 0 sub a 1
5264end
5265a6_build n
5266end
5267
5268note a6_build n: read the ΩIR module in i_in[0, n), compile it, write the ELF to stdout, exit 0
5269fn a6_build n ulong to void
5270c = ir_read 0 n
5271if ne c 0
5272a6_die c 8 ir_badat
5273end
5274a6_comp
5275a6_flush
5276exit 0
5277end
5278
5279
5280note Pool operations name descriptors, never data addresses. The descriptor is loaded after operands.
5281fn a6_spool o ulong op ulong to void
5282if ne get a_tgt 0 0
5283a6_unsup op
5284end
5285if eq op 111
5286a6_pgrow o
5287ret
5288end
5289if ge op 115
5290a6_pio o op
5291ret
5292end
5293g = get i_oim o
5294a6_movi 16 ar_g 90 g
5295r = ulong get i_ors o
5296if eq op 112
5297d = a6_dst r
5298a6_ew a_ldrx d 16 1 0 0
5299a6_fin r d
5300ret
5301end
5302i = a6_src a6_arg o 1 17
5303a6_ew a_ldrx 15 16 1 0 0
5304a6_ew a_subsx 31 i 15 0 0
5305a6_fx a_bcond 2 add a_tlab 3
5306a6_ew a_ldrx 16 16 0 0 0
5307t = ulong ar_u 87 g
5308if eq op 113
5309if a6_isft t
5310d = a6_fdst r
5311a6_ew a6_ff t a_ldrdr a_ldrsr d 16 i 1 0
5312a6_ffin r d
5313else
5314d = a6_dst r
5315a6_ew a6_ldf t d 16 i a6_sh1 t 0
5316a6_fin r d
5317end
5318ret
5319end
5320v = a6_arg o 2
5321if a6_isft t
5322x = a6_fsrc v 29
5323a6_ew a6_ff t a_strdr a_strsr x 16 i 1 0
5324else
5325x = a6_src v 15
5326a6_ew a6_stf t x 16 i a6_sh1 t 0
5327end
5328end
5329
5330note Exact canonical en_help instruction sequence from omc, including the branch over its body.
5331note The ordinary call classification saves LR and protects values across the helper's clobbers.
5332fn a6_pgrow o ulong to void
5333g = get i_oim o
5334a6_mvclr
5335a6_mvadd 1 a6_loc a6_arg o 1
5336a6_pmove
5337a6_movi 0 ar_g 90 g
5338a6_movi 2 a6_lsz ulong ar_u 87 g
5339note The helper is a BL target, so retain D13 even though its body is inline.
5340while ne and add get a_cn 0 4 63 0
5341a6_emitw 3573751839
5342end
5343entry = add get a_cn 0 4
5344a6_emitw 335544362
5345a6_emitw 2852127721
5346a6_emitw 2852193258
5347a6_emitw 4181722403
5348a6_emitw 3942842719
5349a6_emitw 1409287209
5350a6_emitw 3531605988
5351a6_emitw 3405906052
5352a6_emitw 2596545861
5353a6_emitw 3036677093
5354a6_emitw 2596413765
5355a6_emitw 2436889765
5356a6_emitw 2457128101
5357a6_emitw 2596413542
5358a6_emitw 2436889798
5359a6_emitw 2457128134
5360a6_emitw 3943039167
5361a6_emitw 1409286793
5362a6_emitw 3019899142
5363a6_emitw 4181721376
5364a6_emitw 2852520929
5365a6_emitw 2852455394
5366a6_emitw 3531604003
5367a6_emitw 3531610888
5368a6_emitw 3556769793
5369a6_emitw 335544329
5370a6_emitw 3531603968
5371a6_emitw 2852455393
5372a6_emitw 3531604066
5373a6_emitw 3531605059
5374a6_emitw 2457862148
5375a6_emitw 3531603973
5376a6_emitw 3531611080
5377a6_emitw 3556769793
5378a6_emitw 2973760543
5379a6_emitw 1409286274
5380a6_emitw 4177527072
5381a6_emitw 4177528106
5382a6_emitw 3531603968
5383a6_emitw 3596551104
5384a6_emitw 2457862816
5385a6_emitw 3596551104
5386off = sub entry get a_cn 0
5387a6_emitw or 2483027968 and shr off 2 67108863
5388a6_mvfrom ulong get i_ors o 0
5389end
5390
5391fn a6_pio o ulong op ulong to void
5392a6_mvclr
5393a6_mvadd 0 a6_loc a6_arg o 1
5394a6_mvadd 1 a6_loc a6_arg o 2
5395a6_mvadd 2 a6_loc a6_arg o 3
5396a6_pmove
5397a6_movi 16 ar_g 90 get i_oim o
5398a6_ew a_ldrx 15 16 1 0 0
5399a6_ew a_subsx 31 1 15 0 0
5400a6_fx a_bcond 8 add a_tlab 3
5401a6_ew a_subx 15 15 1 0 0
5402a6_ew a_subsx 31 2 15 0 0
5403a6_fx a_bcond 8 add a_tlab 3
5404a6_ew a_ldrx 16 16 0 0 0
5405a6_ew a_addx 1 16 1 0 0
5406nr = ulong 64
5407if eq op 115
5408nr = 63
5409end
5410a6_ew a_movzx 8 nr 0 0 0
5411a6_ew a_svc 0 0 0 0 0
5412a6_mvfrom ulong get i_ors o 0
5413end
5414
5415fn a6_hostcall op ulong to bool
5416ret or or eq op 110 eq op 111 or eq op 115 eq op 116
5417end
5418
5419note Linux entry layout matches omc hargs: argc, argv and envp stay on the original kernel stack.
5420note x18 is excluded from the allocator and retains that initial stack only for modules using env words.
5421note Host ops are call boundaries: x0..x14 may be scratch. The kernel environment remains read-only.
5422fn a6_hasenv to bool
5423for j ulong get i_n 5 bound 4194305
5424o = add j 1
5425if eq get i_oop o 110
5426w = get i_oim o
5427if or eq w 136 eq w 137
5428ret true
5429end
5430end
5431end
5432ret false
5433end
5434
5435fn a6_envbr pos ulong form ulong reg ulong to void
5436a6_putw pos a6_enc form reg a6_wd pos get a_cn 0 0 0 0
5437end
5438
5439fn a6_henv o ulong w ulong to void
5440if ne get a_tgt 0 0
5441a6_unsup w
5442end
5443if eq w 137
5444a = a6_arg o 2
5445a6_aref a
5446b = a6_src a 16
5447off = a6_src a6_arg o 3 17
5448a6_ew a_addx 17 b off 0 0
5449a6_mvclr
5450a6_mvadd 0 a6_loc a6_arg o 1
5451a6_mvadd 1 17
5452limit = a6_loc a6_arg o 4
5453a6_mvadd 2 limit
5454a6_pmove
5455end
5456a6_ew a_ldrx 3 18 0 0 0
5457a6_ew a_addxi 3 3 2 0 0
5458a6_ew a_addx 3 18 3 0 3
5459if eq w 136
5460a6_movi 0 0
5461loop = get a_cn 0
5462a6_ew a_ldrx 4 3 0 0 0
5463done = get a_cn 0
5464a6_emitw 0
5465a6_ew a_addxi 0 0 1 0 0
5466a6_ew a_addxi 3 3 8 0 0
5467a6_ew a_b a6_wd get a_cn 0 loop 0 0 0 0
5468a6_envbr done a_cbzx 4
5469else
5470loop = get a_cn 0
5471a6_ew a_ldrx 4 3 0 0 0
5472a6_fx a_cbzx 4 add a_tlab 3
5473found = get a_cn 0
5474a6_emitw 0
5475a6_ew a_subxi 0 0 1 0 0
5476a6_ew a_addxi 3 3 8 0 0
5477a6_ew a_b a6_wd get a_cn 0 loop 0 0 0 0
5478a6_envbr found a_cbzx 0
5479a6_movi 5 0
5480scan = get a_cn 0
5481a6_ew a_ldrbr 6 4 5 0 0
5482done = get a_cn 0
5483a6_emitw 0
5484a6_ew a_addxi 5 5 1 0 0
5485a6_ew a_b a6_wd get a_cn 0 scan 0 0 0 0
5486a6_envbr done a_cbzx 6
5487a6_ew a_subsx 31 5 2 0 0
5488small = get a_cn 0
5489a6_emitw 0
5490a6_movi 0 0
5491copy = get a_cn 0
5492a6_ew a_subsx 31 0 5 0 0
5493done = get a_cn 0
5494a6_emitw 0
5495a6_ew a_ldrbr 6 4 0 0 0
5496a6_ew a_strbr 6 1 0 0 0
5497a6_ew a_addxi 0 0 1 0 0
5498a6_ew a_b a6_wd get a_cn 0 copy 0 0 0 0
5499a6_envbr done a_bcond 0
5500a6_mov 0 5
5501finish = get a_cn 0
5502a6_emitw 0
5503a6_envbr small a_bcond 8
5504a6_movi 0 18446744073709551511
5505a6_putw finish a6_enc a_b a6_wd finish get a_cn 0 0 0 0 0
5506end
5507r = ulong get i_ors o
5508if ne r 0
5509a6_mvfrom r 0
5510end
5511end