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 / om / compiler / osc_a64_fields.om

524 lines · 13428 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Encode/decode the variable fields of OSC's supported 64-bit AArch64 forms.
3note The opcode table supplies base bits and the class. Register kind, canonical
4note operands and range checks precede packing. Negative offsets use low field bits.
5note bridge c-type a64field OscA64Fields
6note bridge c-define OSC_A64_ERR_UNUSED_SHIFT 1
7note bridge c-define OSC_A64_ERR_UNUSED_COND 2
8note bridge c-define OSC_A64_ERR_UNUSED_IMM2 3
9note bridge c-define OSC_A64_ERR_UNUSED_IMM 4
10note bridge c-define OSC_A64_ERR_SHIFT_KIND 5
11note bridge c-define OSC_A64_ERR_SHIFT_AMOUNT 6
12note bridge c-define OSC_A64_ERR_IMM12 7
13note bridge c-define OSC_A64_ERR_LSL12 8
14note bridge c-define OSC_A64_ERR_IMMR 9
15note bridge c-define OSC_A64_ERR_IMMS 10
16note bridge c-define OSC_A64_ERR_COND 11
17note bridge c-define OSC_A64_ERR_IMM16 12
18note bridge c-define OSC_A64_ERR_MOV_SHIFT 13
19note bridge c-define OSC_A64_ERR_MEM_OFFSET 14
20note bridge c-define OSC_A64_ERR_PAIR_OFFSET 15
21note bridge c-define OSC_A64_ERR_WRITEBACK_ALIAS 16
22note bridge c-define OSC_A64_ERR_LOAD_ALIAS 17
23note bridge c-define OSC_A64_ERR_BRANCH_OFFSET 18
24note bridge c-define OSC_A64_ERR_BRANCH_COND 19
25note bridge c-define OSC_A64_ERR_UNUSED_REG 32
26note bridge c-define OSC_A64_ERR_REG_SP 36
27note bridge c-define OSC_A64_ERR_REG_ZR 40
28note bridge c-define OSC_A64_ERR_BAD_REG 44
29om2
30layout a64spec 24
31field base uint at 8 little
32field form uchar at 12 little
33field role uchar 4 at 13 little
34field flags uchar at 17 little
35end
36layout a64insn 24
37field opcode uchar at 0 little
38field reg uchar 4 at 1 little
39field shift uchar at 5 little
40field cond uchar at 6 little
41field imm long at 8 little
42field imm2 long at 16 little
43end
44layout a64field 40
45field rd uint at 0 little
46field rn uint at 4 little
47field rm uint at 8 little
48field ra uint at 12 little
49field shift uint at 16 little
50field cond uint at 20 little
51field imm long at 24 little
52field imm2 long at 32 little
53end
54
55const CLS_NONE ulong 0
56const CLS_RRR_SH ulong 1
57const CLS_RI ulong 2
58const CLS_R4 ulong 3
59const CLS_MULH ulong 4
60const CLS_R3 ulong 5
61const CLS_BFM ulong 6
62const CLS_CSINC ulong 7
63const CLS_MOV16 ulong 8
64const CLS_MEM ulong 9
65const CLS_PAIR ulong 10
66const CLS_B26 ulong 11
67const CLS_BCOND ulong 12
68const CLS_CB ulong 13
69const CLS_BREG ulong 14
70const CLS_BRK ulong 15
71
72export fn osc_a64_pack_fields form ulong base ulong f view a64field to ulong
73rd = ulong get f rd
74rn = shl ulong get f rn 5
75rm = shl ulong get f rm 16
76ra = shl ulong get f ra 10
77imm = bitcast ulong get f imm
78second = bitcast ulong get f imm2
79if eq form CLS_RRR_SH
80 regs = or rd or rn rm
81 returnbits = or shl ulong get f shift 22 shl imm 10
82 ret or base or regs returnbits
83end
84if eq form CLS_RI
85 ret or base or or rd rn or shl second 22 shl imm 10
86end
87if eq form CLS_R4
88 ret or base or or rd rn or rm ra
89end
90if or eq form CLS_MULH eq form CLS_R3
91 ret or base or rd or rn rm
92end
93if eq form CLS_BFM
94 ret or base or or rd rn or shl imm 16 shl second 10
95end
96if eq form CLS_CSINC
97 regs = or rd or rn rm
98 ret or base or regs shl ulong get f cond 12
99end
100if eq form CLS_MOV16
101 ret or base or rd or shl shr second 4 21 shl imm 5
102end
103if eq form CLS_MEM
104 ret or base or or rd rn shl shr imm 3 10
105end
106if eq form CLS_PAIR
107 offset = shl and shr imm 3 0x7f 15
108 ret or base or or rd rn or ra offset
109end
110if eq form CLS_B26
111 ret or base and shr imm 2 0x03ffffff
112end
113if or eq form CLS_BCOND eq form CLS_CB
114 offset = shl and shr imm 2 0x7ffff 5
115 if eq form CLS_BCOND
116 ret or base or offset ulong get f cond
117 end
118 ret or base or offset rd
119end
120if eq form CLS_BREG
121 ret or base rn
122end
123if eq form CLS_BRK
124 ret or base shl imm 5
125end
126trap 5
127end
128
129fn a64_scaled_signed fieldbits ulong signbit ulong scale ulong to long
130ret bitcast long shl sub xor fieldbits signbit signbit scale
131end
132
133export fn osc_a64_unpack_fields form ulong word ulong f edit a64field to void
134put f rd uint and word 31
135put f rn uint and shr word 5 31
136put f rm uint and shr word 16 31
137put f ra uint and shr word 10 31
138put f shift 0
139put f cond 0
140put f imm 0
141put f imm2 0
142if eq form CLS_RRR_SH
143 put f shift uint and shr word 22 3
144 put f imm long and shr word 10 63
145end
146if eq form CLS_RI
147 put f imm long and shr word 10 0xfff
148 put f imm2 long and shr word 22 1
149end
150if eq form CLS_BFM
151 put f imm long and shr word 16 63
152 put f imm2 long and shr word 10 63
153end
154if eq form CLS_CSINC
155 put f cond uint and shr word 12 15
156end
157if eq form CLS_MOV16
158 put f imm long and shr word 5 0xffff
159 put f imm2 long shl and shr word 21 3 4
160end
161if eq form CLS_MEM
162 put f imm long shl and shr word 10 0xfff 3
163end
164if eq form CLS_PAIR
165 put f imm a64_scaled_signed and shr word 15 0x7f 0x40 3
166end
167if eq form CLS_B26
168 put f imm a64_scaled_signed and word 0x03ffffff 0x02000000 2
169end
170if or eq form CLS_BCOND eq form CLS_CB
171 put f imm a64_scaled_signed and shr word 5 0x7ffff 0x40000 2
172 if eq form CLS_BCOND
173 put f cond uint and word 15
174 end
175end
176if eq form CLS_BRK
177 put f imm long and shr word 5 0xffff
178end
179end
180const ERR_UNUSED_SHIFT ulong 1
181const ERR_UNUSED_COND ulong 2
182const ERR_UNUSED_IMM2 ulong 3
183const ERR_UNUSED_IMM ulong 4
184const ERR_SHIFT_KIND ulong 5
185const ERR_SHIFT_AMOUNT ulong 6
186const ERR_IMM12 ulong 7
187const ERR_LSL12 ulong 8
188const ERR_IMMR ulong 9
189const ERR_IMMS ulong 10
190const ERR_COND ulong 11
191const ERR_IMM16 ulong 12
192const ERR_MOV_SHIFT ulong 13
193const ERR_MEM_OFFSET ulong 14
194const ERR_PAIR_OFFSET ulong 15
195const ERR_WRITEBACK_ALIAS ulong 16
196const ERR_LOAD_ALIAS ulong 17
197const ERR_BRANCH_OFFSET ulong 18
198const ERR_BRANCH_COND ulong 19
199
200note Pair flags come from the opcode table. Encoded base register 31 is SP
201note for the pair class, so it cannot alias a transfer even when that is XZR.
202export fn osc_a64_check_fields cls ulong f view a64field writeback ulong loadpair ulong to ulong
203imm = get f imm
204second = get f imm2
205if and ne cls CLS_RRR_SH ne get f shift 0
206 ret ERR_UNUSED_SHIFT
207end
208if and ne get f cond 0 and ne cls CLS_CSINC ne cls CLS_BCOND
209 ret ERR_UNUSED_COND
210end
211if ne second 0
212 if and ne cls CLS_RI and ne cls CLS_BFM ne cls CLS_MOV16
213 ret ERR_UNUSED_IMM2
214 end
215end
216if ne imm 0
217 if or eq cls CLS_R4 or eq cls CLS_MULH eq cls CLS_R3
218 ret ERR_UNUSED_IMM
219 end
220 if or eq cls CLS_CSINC eq cls CLS_BREG
221 ret ERR_UNUSED_IMM
222 end
223end
224if eq cls CLS_RRR_SH
225 if gt get f shift 2
226 ret ERR_SHIFT_KIND
227 end
228 if or lt imm 0 gt imm 63
229 ret ERR_SHIFT_AMOUNT
230 end
231end
232if eq cls CLS_RI
233 if or lt imm 0 gt imm 4095
234 ret ERR_IMM12
235 end
236 if and ne second 0 ne second 1
237 ret ERR_LSL12
238 end
239end
240if eq cls CLS_BFM
241 if or lt imm 0 gt imm 63
242 ret ERR_IMMR
243 end
244 if or lt second 0 gt second 63
245 ret ERR_IMMS
246 end
247end
248if and eq cls CLS_CSINC gt get f cond 14
249 ret ERR_COND
250end
251if or eq cls CLS_MOV16 eq cls CLS_BRK
252 if or lt imm 0 gt imm 65535
253 ret ERR_IMM16
254 end
255 if eq cls CLS_MOV16
256 if or lt second 0 or gt second 48 ne and second 15 0
257 ret ERR_MOV_SHIFT
258 end
259 end
260end
261if eq cls CLS_MEM
262 if or lt imm 0 or gt imm 32760 ne and imm 7 0
263 ret ERR_MEM_OFFSET
264 end
265end
266if eq cls CLS_PAIR
267 if or lt imm -512 or gt imm 504 ne and imm 7 0
268 ret ERR_PAIR_OFFSET
269 end
270 if and ne writeback 0 ne get f rn 31
271 base = get f rn
272 if or eq base get f rd eq base get f ra
273 ret ERR_WRITEBACK_ALIAS
274 end
275 end
276 if and ne loadpair 0 eq get f rd get f ra
277 ret ERR_LOAD_ALIAS
278 end
279end
280if eq cls CLS_B26
281 if or lt imm -134217728 or gt imm 134217724 ne and imm 3 0
282 ret ERR_BRANCH_OFFSET
283 end
284end
285if or eq cls CLS_BCOND eq cls CLS_CB
286 if and eq cls CLS_BCOND gt get f cond 13
287 ret ERR_BRANCH_COND
288 end
289 if or lt imm -1048576 or gt imm 1048572 ne and imm 3 0
290 ret ERR_BRANCH_OFFSET
291 end
292end
293ret 0
294end
295
296note Checked encoder result: the low word is an instruction on success.
297note The high word carries the existing diagnostic code on refusal.
298export fn osc_a64_checked_word form ulong base ulong f view a64field flags ulong to ulong
299 status = osc_a64_check_fields form f and flags 1 shr flags 1
300 if ne status 0
301 ret shl status 32
302 end
303 ret osc_a64_pack_fields form base f
304end
305
306note The opcode matcher has already checked the fixed bits. Unpack, check
307note and re-encode each candidate inside the same Omega invocation, so a
308note successful decode always proves the original word is canonical.
309export fn osc_a64_canonical_word form ulong base ulong word ulong flags ulong fields edit a64field to ulong
310 osc_a64_unpack_fields form word fields
311 if and eq form CLS_BREG eq get fields rn 31
312 ret 0
313 end
314 if ne osc_a64_checked_word form base fields flags word
315 ret 0
316 end
317 ret 1
318end
319
320note Plan the move-wide instructions for a 64-bit constant in lane order.
321note Each record holds the opcode in the high word, shift and immediate in
322note the low word. A tied MOVZ/MOVN cost selects MOVZ for stable code bytes.
323export fn osc_a64_materialize value ulong plan edit ulong 4 to ulong
324 zeros = ulong 0
325 ones = ulong 0
326 for lane ulong 4
327 chunk = and shr value mul lane 16 65535
328 if eq chunk 0
329 zeros = add zeros 1
330 end
331 if eq chunk 65535
332 ones = add ones 1
333 end
334 end
335 fill = ulong 0
336 initial = ulong 25
337 if gt ones zeros
338 fill = 65535
339 initial = 26
340 end
341 count = ulong 0
342 for lane ulong 4
343 shift = mul lane 16
344 chunk = and shr value shift 65535
345 if ne chunk fill
346 opcode = ulong 27
347 if eq count 0
348 opcode = initial
349 chunk = xor chunk fill
350 end
351 payload = or chunk shl shift 16
352 put plan count or shl opcode 32 payload
353 count = add count 1
354 end
355 end
356 if eq count 0
357 put plan 0 shl initial 32
358 ret 1
359 end
360 ret count
361end
362
363note The descriptor table is indexed in public opcode order. Class masks
364note select the variable bits excluded from the fixed opcode comparison.
365fn a64_mask form ulong to ulong
366 if eq form CLS_RRR_SH
367 ret 0x00dfffff
368 end
369 if eq form CLS_RI
370 ret 0x007fffff
371 end
372 if eq form CLS_R4
373 ret 0x001f7fff
374 end
375 if eq form CLS_MULH
376 ret 0x001f03ff
377 end
378 if eq form CLS_R3
379 ret 0x001f03ff
380 end
381 if eq form CLS_BFM
382 ret 0x003fffff
383 end
384 if eq form CLS_CSINC
385 ret 0x001ff3ff
386 end
387 if eq form CLS_MOV16
388 ret 0x007fffff
389 end
390 if eq form CLS_MEM
391 ret 0x003fffff
392 end
393 if eq form CLS_PAIR
394 ret 0x003fffff
395 end
396 if eq form CLS_B26
397 ret 0x03ffffff
398 end
399 if eq form CLS_BCOND
400 ret 0x00ffffef
401 end
402 if eq form CLS_CB
403 ret 0x00ffffff
404 end
405 if eq form CLS_BREG
406 ret 0x000003e0
407 end
408 if eq form CLS_BRK
409 ret 0x001fffe0
410 end
411 ret 0
412end
413
414fn a64_encode_registers spec view a64spec insn view a64insn fields edit a64field to ulong
415 regs = new uint 4 0
416 for i ulong 4
417 if and eq get spec role i 0 ne get insn reg i 0
418 ret shl add 32 i 32
419 end
420 end
421 for i ulong 4
422 role = get spec role i
423 regno = get insn reg i
424 if le regno 30
425 put regs i uint regno
426 else
427 if eq regno 33
428 if ne role 2
429 ret shl add 36 i 32
430 end
431 else
432 if eq regno 32
433 if ne role 1
434 ret shl add 40 i 32
435 end
436 else
437 ret shl add 44 i 32
438 end
439 end
440 put regs i 31
441 end
442 end
443 put fields rd get regs 0
444 put fields rn get regs 1
445 put fields rm get regs 2
446 put fields ra get regs 3
447 put fields shift uint get insn shift
448 put fields cond uint get insn cond
449 put fields imm get insn imm
450 put fields imm2 get insn imm2
451 ret osc_a64_checked_word ulong get spec form ulong get spec base fields ulong get spec flags
452end
453
454export fn osc_a64_write spec view uchar 24 insn view uchar 24 to ulong
455 fields = new uchar 40 0
456 ret a64_encode_registers spec insn fields
457end
458
459fn a64_decode_register role uchar bits uint to uchar
460 if eq role 0
461 ret 0
462 end
463 if lt bits 31
464 ret uchar bits
465 end
466 if eq role 1
467 ret 32
468 end
469 if eq role 2
470 ret 33
471 end
472 ret 255
473end
474
475fn a64_try_opcode word ulong op ulong spec view a64spec insn edit a64insn fields edit a64field to ulong
476 form = ulong get spec form
477 base = ulong get spec base
478 if eq osc_a64_canonical_word form base word ulong get spec flags fields 0
479 ret 0
480 end
481 put insn opcode uchar op
482 put insn reg 0 a64_decode_register get spec role 0 get fields rd
483 put insn reg 1 a64_decode_register get spec role 1 get fields rn
484 put insn reg 2 a64_decode_register get spec role 2 get fields rm
485 put insn reg 3 a64_decode_register get spec role 3 get fields ra
486 put insn shift uchar get fields shift
487 put insn cond uchar get fields cond
488 put insn imm get fields imm
489 put insn imm2 get fields imm2
490 ret 1
491end
492
493fn a64_matches specs view uchar * pos ulong word ulong to bool
494 form = ulong get specs add pos 12
495 base = ulong get specs add pos 8
496 base = or base shl ulong get specs add pos 9 8
497 base = or base shl ulong get specs add pos 10 16
498 base = or base shl ulong get specs add pos 11 24
499 ret and ne form 0 eq and word xor a64_mask form 0xffffffff base
500end
501
502fn a64_copy_spec specs view uchar * pos ulong spec edit uchar 24 to void
503 for i ulong 10
504 put spec add 8 i get specs add add pos 8 i
505 end
506end
507
508export fn osc_a64_read word ulong specs view uchar * insn edit uchar 24 to ulong
509 fields = new uchar 40 0
510 spec = new uchar 24 0
511 n = div len specs 24
512 op = ulong 1
513 while lt op n bound 255
514 pos = mul op 24
515 if a64_matches specs pos word
516 a64_copy_spec specs pos spec
517 if ne a64_try_opcode word op spec insn fields 0
518 ret 1
519 end
520 end
521 op = add op 1
522 end
523 ret 0
524end