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 / target / oir_x64.om

577 lines · 10238 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/target/oir_x64.om: Turing-Complete ΩIR binary (seed/ng/src/ir.om + opt.om) -> Register-Allocated x86-64 ELF64 compiler!
3note Supports straight-line ALU/bitwise/conv + full CFG blocks, parallel-copy block-param phi moves (br),
4note conditional branches (cbr), signed/unsigned comparisons (eq..ge), loops with back-edge live-range extension, and 2-pass rel32 patching.
5
6global oxValIdx ulong 256
7global oxValCnt ulong 1
8
9note Block offset table (indexed by block handle mod 256) + jump fixup list
10global oxBlkId ulong 128
11global oxBlkOff ulong 128
12global oxBlkStartPos ulong 128
13global oxBlkCnt ulong 1
14global oxFixPos ulong 256
15global oxFixBlk ulong 256
16global oxFixCnt ulong 1
17
18fn oxFindOrAddVal vid ulong to ulong
19n = get oxValCnt 0
20for i ulong n bound 256
21if eq get oxValIdx i vid
22ret i
23end
24end
25put oxValIdx n vid
26put oxValCnt 0 add n 1
27ret n
28end
29
30fn oxFindBlkIdx bid ulong to ulong
31n = get oxBlkCnt 0
32for i ulong n bound 128
33if eq get oxBlkId i bid
34ret i
35end
36end
37ret 999999
38end
39
40note Physical x86-64 register map for allocator slots 0..14 (avoids RSP=4):
41note 0=RAX(0), 1=RDI(7), 2=RSI(6), 3=RDX(2), 4=RCX(1), 5=RBX(3), 6=RBP(5), 7..14=R8..R15(8..15)
42fn oxX64Reg slot ulong to ulong
43if eq slot 0
44ret 0
45end
46if eq slot 1
47ret 7
48end
49if eq slot 2
50ret 6
51end
52if eq slot 3
53ret 2
54end
55if eq slot 4
56ret 1
57end
58if eq slot 5
59ret 3
60end
61if eq slot 6
62ret 5
63end
64ret add slot 1
65end
66
67fn oxPhysOfVal vid ulong to ulong
68n = get oxValCnt 0
69for i ulong n bound 256
70if eq get oxValIdx i vid
71ret oxX64Reg get raPhys i
72end
73end
74ret 0
75end
76
77note Emit x86-64 movabs r64, imm64
78fn oxEmitImm64 pr ulong imm ulong to void
79rex = ulong 72
80if ge pr 8
81rex = or rex 1
82end
83isEmitU8 rex
84isEmitU8 add 184 and pr 7
85isEmitU64 imm
86end
87
88fn oxEmitMov prDst ulong prSrc ulong to void
89if eq prDst prSrc
90ret
91end
92isEmitU8 isX64Rex prSrc prDst
93isEmitU8 137
94isEmitU8 isX64ModRm prSrc prDst
95end
96
97fn oxEmitPush pr ulong to void
98if ge pr 8
99isEmitU8 65
100end
101isEmitU8 add 80 and pr 7
102end
103
104fn oxEmitPop pr ulong to void
105if ge pr 8
106isEmitU8 65
107end
108isEmitU8 add 88 and pr 7
109end
110
111fn oxEmitAlu opc ulong pd ulong pn ulong pm ulong to void
112if eq pd pm
113if ne pd pn
114oxEmitPush pm
115oxEmitMov pd pn
116oxEmitPop 15
117isEmitU8 isX64Rex 15 pd
118isEmitU8 opc
119isEmitU8 isX64ModRm 15 pd
120ret
121end
122end
123oxEmitMov pd pn
124isEmitU8 isX64Rex pm pd
125isEmitU8 opc
126isEmitU8 isX64ModRm pm pd
127end
128
129fn oxEmitMul pd ulong pn ulong pm ulong to void
130if eq pd pm
131if ne pd pn
132oxEmitPush pm
133oxEmitMov pd pn
134oxEmitPop 15
135isEmitU8 isX64Rex pd 15
136isEmitU8 15
137isEmitU8 175
138isEmitU8 isX64ModRm pd 15
139ret
140end
141end
142oxEmitMov pd pn
143isEmitU8 isX64Rex pd pm
144isEmitU8 15
145isEmitU8 175
146isEmitU8 isX64ModRm pd pm
147end
148
149fn oxEmitNeg pd ulong pn ulong to void
150oxEmitMov pd pn
151rex = ulong 72
152if ge pd 8
153rex = or rex 1
154end
155isEmitU8 rex
156isEmitU8 247
157isEmitU8 or 216 and pd 7
158end
159
160note Emit signed/unsigned comparison: cmp pn, pm; setcc pd_b; movzx pd, pd_b
161fn oxCmpCc op ulong isSigned bool to ulong
162if eq op 30
163ret 148
164end
165if eq op 31
166ret 149
167end
168if isSigned
169if eq op 32
170ret 156
171end
172if eq op 33
173ret 158
174end
175if eq op 34
176ret 159
177end
178ret 157
179end
180if eq op 32
181ret 146
182end
183if eq op 33
184ret 150
185end
186if eq op 34
187ret 151
188end
189ret 147
190end
191
192fn oxEmitCmp op ulong pd ulong pn ulong pm ulong isSigned bool to void
193note cmp pn, pm (REX.W 0x39 /r: reg=pm, r/m=pn)
194isEmitU8 isX64Rex pm pn
195isEmitU8 57
196isEmitU8 isX64ModRm pm pn
197note setcc pd_low8 (REX 0x40|B 0x0F cc 0xC0|(pd&7))
198rexB = ulong 64
199if ge pd 8
200rexB = ulong 65
201end
202isEmitU8 rexB
203isEmitU8 15
204isEmitU8 oxCmpCc op isSigned
205isEmitU8 or 192 and pd 7
206note movzx pd, pd_low8 (REX.W 0x0F 0xB6 /r: reg=pd, r/m=pd)
207isEmitU8 isX64Rex pd pd
208isEmitU8 15
209isEmitU8 182
210isEmitU8 isX64ModRm pd pd
211end
212
213fn oxEmitJmpRel32 targetBid ulong to void
214isEmitU8 233
215f = get oxFixCnt 0
216put oxFixPos f get isLen 0
217put oxFixBlk f targetBid
218put oxFixCnt 0 add f 1
219isEmitU32 0
220end
221
222fn oxEmitJnzRel32 condPr ulong targetBid ulong to void
223note test condPr, condPr (REX.W 0x85 /r)
224isEmitU8 isX64Rex condPr condPr
225isEmitU8 133
226isEmitU8 isX64ModRm condPr condPr
227note jnz rel32 (0x0F 0x85 rel32)
228isEmitU8 15
229isEmitU8 133
230f = get oxFixCnt 0
231put oxFixPos f get isLen 0
232put oxFixBlk f targetBid
233put oxFixCnt 0 add f 1
234isEmitU32 0
235end
236
237fn oxEmitExit prVal ulong to void
238oxEmitMov 7 prVal
239isEmitU8 184
240isEmitU32 60
241isEmitU8 15
242isEmitU8 5
243end
244
245fn oxTouchUse av ulong pos ulong to void
246if eq av 0
247ret
248end
249vi = oxFindOrAddVal av
250if lt vi get raNVal 0
251if lt pos get raStart vi
252put raStart vi pos
253end
254if gt pos get raEnd vi
255put raEnd vi pos
256end
257end
258end
259
260fn oxExtendBackEdge targetBid ulong backPos ulong to void
261bi = oxFindBlkIdx targetBid
262if eq bi 999999
263ret
264end
265hdrPos = get oxBlkStartPos bi
266if ge hdrPos backPos
267ret
268end
269nv = get raNVal 0
270for i ulong nv bound 256
271s = get raStart i
272e = get raEnd i
273if and le s backPos ge e hdrPos
274if lt hdrPos s
275put raStart i hdrPos
276end
277if gt backPos e
278put raEnd i backPos
279end
280end
281end
282end
283
284fn oxScanDefs f uint to void
285b = get i_fb1 f
286pos = ulong 0
287while ne b 0 bound 128
288bc = get oxBlkCnt 0
289put oxBlkId bc ulong b
290put oxBlkStartPos bc pos
291put oxBlkCnt 0 add bc 1
292p = get i_bp1 b
293while ne p 0 bound 32
294if ne get i_vty p 20
295idx = oxFindOrAddVal ulong p
296if ge idx get raNVal 0
297id = raAddInterval pos pos RA_SPILL
298end
299end
300p = get i_vnx p
301end
302o = get i_bo1 b
303while ne o 0 bound 256
304res = ulong get i_ors o
305if ne res 0
306if ne get i_vty res 20
307idx = oxFindOrAddVal res
308if ge idx get raNVal 0
309id = raAddInterval pos pos RA_SPILL
310end
311end
312end
313pos = add pos 1
314o = get i_onx o
315end
316b = get i_bnx b
317end
318end
319
320fn oxScanOpUses o uint pos ulong to void
321op = ulong get i_oop o
322na = ulong get i_ona o
323a0 = ulong get i_oa0 o
324if eq op 60
325if gt na 2
326for k ulong sub na 2 bound 16
327oxTouchUse ulong get i_arg add a0 add k 2 pos
328end
329end
330oxExtendBackEdge ulong get i_arg a0 pos
331ret
332end
333if eq op 61
334oxTouchUse ulong get i_arg add a0 1 pos
335oxExtendBackEdge ulong get i_arg add a0 2 pos
336oxExtendBackEdge ulong get i_arg add a0 3 pos
337ret
338end
339for k ulong na bound 16
340oxTouchUse ulong get i_arg add a0 k pos
341end
342end
343
344fn oxSortIntervals to void
345n = get raNVal 0
346for i ulong n bound 256
347if gt i 0
348j = i
349while gt j 0 bound 256
350jm = sub j 1
351if le get raStart jm get raStart j
352break
353end
354ts = get raStart jm
355te = get raEnd jm
356tv = get oxValIdx jm
357put raStart jm get raStart j
358put raEnd jm get raEnd j
359put oxValIdx jm get oxValIdx j
360put raStart j ts
361put raEnd j te
362put oxValIdx j tv
363j = jm
364end
365end
366end
367end
368
369note 2-pass live-interval builder (handles rotated loops) + sort by start + shared regalloc.om (14 GPRs)!
370fn oxAllocFunc f uint to bool
371put oxValCnt 0 0
372put oxBlkCnt 0 0
373raReset
374oxScanDefs f
375b = get i_fb1 f
376pos = ulong 0
377while ne b 0 bound 128
378o = get i_bo1 b
379while ne o 0 bound 256
380oxScanOpUses o pos
381pos = add pos 1
382o = get i_onx o
383end
384b = get i_bnx b
385end
386oxSortIntervals
387tdSet TID_X64_AVX2 TDF_NGPR 14
388sp = raRunForTarget TID_X64_AVX2
389tdSet TID_X64_AVX2 TDF_NGPR 15
390if ne raVerifyColoring 0
391ret false
392end
393ret eq sp 0
394end
395
396note Emit parallel-copy block-param moves for `br target(tok, v1, v2, ...)` via push/pop (cycle-safe!)
397fn oxEmitBrPhi o uint to void
398dstP = new ulong 16 0
399a0 = ulong get i_oa0 o
400na = ulong get i_ona o
401tb = get i_arg a0
402nArg = ulong 0
403if gt na 2
404nArg = sub na 2
405end
406for k ulong nArg bound 16
407srcV = ulong get i_arg add a0 add k 2
408oxEmitPush oxPhysOfVal srcV
409end
410p = get i_bp1 tb
411di = ulong 0
412while ne p 0 bound 32
413if ne get i_vty p 20
414put dstP di ulong p
415di = add di 1
416end
417p = get i_vnx p
418end
419for k ulong di bound 16
420ri = sub sub di 1 k
421oxEmitPop oxPhysOfVal get dstP ri
422end
423oxEmitJmpRel32 ulong tb
424end
425
426fn oxLowerOneOp o uint to bool
427op = ulong get i_oop o
428res = ulong get i_ors o
429a0 = ulong get i_oa0 o
430if eq op 1
431oxEmitImm64 oxPhysOfVal res get i_oim o
432ret true
433end
434if eq op 10
435oxEmitAlu 1 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1
436ret true
437end
438if eq op 11
439oxEmitAlu 41 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1
440ret true
441end
442if eq op 12
443oxEmitMul oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1
444ret true
445end
446if eq op 15
447oxEmitNeg oxPhysOfVal res oxPhysOfVal ulong get i_arg a0
448ret true
449end
450if eq op 16
451oxEmitAlu 33 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1
452ret true
453end
454if eq op 17
455oxEmitAlu 9 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1
456ret true
457end
458if eq op 18
459oxEmitAlu 49 oxPhysOfVal res oxPhysOfVal ulong get i_arg a0 oxPhysOfVal ulong get i_arg add a0 1
460ret true
461end
462if and ge op 30 le op 35
463av0 = ulong get i_arg a0
464vt = ulong get i_vty av0
465isS = and ge vt 6 le vt 9
466oxEmitCmp op oxPhysOfVal res oxPhysOfVal av0 oxPhysOfVal ulong get i_arg add a0 1 isS
467ret true
468end
469if eq op 36
470oxEmitMov oxPhysOfVal res oxPhysOfVal ulong get i_arg a0
471ret true
472end
473if eq op 60
474oxEmitBrPhi o
475ret true
476end
477if eq op 61
478oxEmitJnzRel32 oxPhysOfVal ulong get i_arg add a0 1 ulong get i_arg add a0 2
479oxEmitJmpRel32 ulong get i_arg add a0 3
480ret true
481end
482if or eq op 63 eq op 66
483oxEmitExit oxPhysOfVal ulong get i_arg add a0 1
484ret true
485end
486if eq op 62
487oxEmitExit 0
488ret true
489end
490if and ge op 70 le op 93
491ret true
492end
493ret false
494end
495
496fn oxPatchFixups to bool
497fc = get oxFixCnt 0
498for i ulong fc bound 256
499fp = get oxFixPos i
500tb = get oxFixBlk i
501bi = oxFindBlkIdx tb
502if eq bi 999999
503ret false
504end
505tgt = get oxBlkOff bi
506rel = sub tgt add fp 4
507put isBuf fp uchar and rel 255
508put isBuf add fp 1 uchar and shr rel 8 255
509put isBuf add fp 2 uchar and shr rel 16 255
510put isBuf add fp 3 uchar and shr rel 24 255
511end
512ret true
513end
514
515fn oxCompileExport to ulong
516nf = get i_n 3
517expF = uint 0
518for fi ulong nf bound 64
519f = uint add fi 1
520if eq get i_fk f 2
521expF = f
522end
523end
524if eq expF 0
525ret 0
526end
527if not oxAllocFunc expF
528ret 0
529end
530isBufReset
531put oxFixCnt 0 0
532b = get i_fb1 expF
533while ne b 0 bound 128
534bi = oxFindBlkIdx ulong b
535if ne bi 999999
536put oxBlkOff bi get isLen 0
537end
538o = get i_bo1 b
539while ne o 0 bound 256
540if not oxLowerOneOp o
541ret 0
542end
543o = get i_onx o
544end
545b = get i_bnx b
546end
547if not oxPatchFixups
548ret 0
549end
550ret elfEmitStatic TID_X64_AVX2
551end
552
553fn main to int
554tdInitAll
555isInitRules
556tdCacheInit
557r = ar_rdin 0
558if ne r 0
559ret 2
560end
561ar_init
562n = get ar_inl 0
563rr = ir_read 0 n
564if ne rr 0
565ret int rr
566end
567op_run
568eln = oxCompileExport
569if eq eln 0
570ret 3
571end
572w = write 1 elfBuf 0 eln
573if ne w long eln
574ret 4
575end
576ret 0
577end