blob: 4b46de41b9cee7840a556c8f1636724cfe4f7e09 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
|
<
txt .consume .|hu "%" deffd
<
"../compiler/elymasAsmOps.ey" include
> /ops sys .asm .defv
sys .asm .ops ==:sysasmops
{
quoted {
_ sys .typed .type 1 eq {
sysasmops -01 .|
} {
"sysasmops" "|" | -102 "." |
} ? *
} {
sysasmops -01 .
} ? *
} ":" defq
sys .asm "::" via
sys .asm .|peek ==:peek
[
8 /r15 :subqImm8Reg
/r15 :popqMem
] ==:generalHeaderPattern
[
8 /r15 :subqImm8Reg
/r14 /r15 :movqRegMem
/r14 /rsi :movqRegReg
0 /rdi :movqImmReg # this should match arbitrary INITIALSCOPESIZE, hence 0
0 /rax :movqImmReg
/rax :callqReg
/rax /r14 :movqRegReg
] ==:scopingHeaderPattern
[
0 /rax :movqImmReg
/rax :pushqReg
] ==:pushConstantPattern
[
0 /rax :movqImmReg
/rax :callqReg
] ==:callConstantPattern
[
0 :callqRel32
] ==:footerPattern
[
/rbx :popqReg
0 /rax :movqImmReg
/rax :pushqReg
/rbx :pushqReg
0 /rax :movqImmReg
/rax :jmpqReg
] ==:constantActiveGeneralPattern
[
16 /r15 :subqImm8Reg
8 /r15 :popqMemDisp8
/r14 /r15 :movqRegMem
0 /r14 :movqImmReg
0 /rdi :movqImmReg
/rdi :callqReg
/r15 /r14 :movqMemReg
8 /r15 :pushqMemDisp8
16 /r15 :addqImm8Reg
:retn
] ==:constantNormalFunctionScopedUntypedPattern
[
0 /rax :movqImmReg
/rax :jmpqReg
] ==:constantNormalFunctionUnscopedUntypedPattern
[
/rbx :popqReg
0 /rax :movqImmReg
/rax :pushqReg
/rbx :jmpqReg
] ==:constantPassivePattern
[
/r14 /rax :movqRegReg
] ==:staticLoadPattern
[
16 /rax /rax :movqMemDisp8Reg
] ==:staticLoadParentPattern
[
/rbx :popqReg
0 /rax /rax :movqMemDisp32Reg
] ==:staticLoadFromScopePattern
[
24 /rax /rcx :movqMemDisp8Reg # load extension area pointer
/rax /edx :movlMemReg # load scope length
/rdx :negqReg # prepare for substraction
/rbx :popqReg
0 1 /rdx /rcx /rax :movqMemIndexScaleDisp32Reg # load entry
] ==:staticLoadFromExtensionPattern
[
/rax :pushqReg
0 /rcx :movqImmReg
/rax /rcx :movqRegMem
/rbx :jmpqReg
] ==:staticLoadPassivePattern
[
/rax :pushqReg
0 /rcx :movqImmReg
/rax /rcx :movqRegMem
/rbx :pushqReg
0 /rax :movqImmReg
/rax :jmpqReg
] ==:staticLoadActivePattern
[
8 /r15 :subqImm8Reg
/r15 :popqMem
0 /rax :movqImmReg
/rax :pushqReg
/r15 :pushqMem
8 /r15 :addqImm8Reg
:retn
] ==:customFunctionConstantPassivePattern
[
8 /r15 :subqImm8Reg
/r15 :popqMem
] ==:customFunctionHeaderPattern
/NOP ==:NOP
/PUSH ==:PUSH
/CALL ==:CALL
/CALLSCOPED ==:CALLSCOPED
/STATIC ==:STATIC
/STATICTYPED ==:STATICTYPED
/STATICWRITE ==:STATICWRITE
/STATICDOT ==:STATICDOT
/NATIVE ==:NATIVE
/ARRAYSTAR ==:ARRAYSTAR
/UNTYPEDSCOPEDSTAR ==:UNTYPEDSCOPEDSTAR
/UNTYPEDUNSCOPEDSTAR ==:UNTYPEDUNSCOPEDSTAR
/STRINGSTAR ==:STRINGSTAR
{ =*f ==t
t { f } { 0 } ? *
} /andif deffd
{ ==e
[
0 e * STATICTYPED eq { 4 e * sys .typed .type 0 eq }' andif
0 e * PUSH eq { 1 e * sys .typed .type 0 eq }' andif
0 e * STRINGSTAR eq
] any
} /holdsInt deffd
{ ==o ==executingScope
0 ==containsScopeModifications
{ _ ==logic
logic { ==entry 0 entry * ==action
[
{ action CALL eq { 1 entry * "<" | ::rawCodeAddress eq }' andif }' {
1 =containsScopeModifications
}
{ action CALL eq { 1 entry * ">" | ::rawCodeAddress eq }' andif }' {
1 =containsScopeModifications
}
{ action CALL eq { 1 entry * ">'" | ::rawCodeAddress eq }' andif }' {
1 =containsScopeModifications
}
] conds
} each
} /testScopeModifications deffst
{ ==logic
[ NOP ] ==last
[ logic { ==entry 0 entry * ==action
[
{ action CALL eq { 1 entry * "|" | ::rawCodeAddress eq }' andif { 0 last * PUSH eq }' andif }' {
1 last * ::rawObject ==constant
constant executingScope sys .resolveInfo {
==mode -- ==parentCount 32 add ==offsetInScope ==inExtensionArea
inExtensionArea { offsetInScope 8 add =offsetInScope } rep
mode 16 div 1 band {
[ STATIC offsetInScope parentCount inExtensionArea ] =last
[ NOP ] =entry
} { } ? *
} {
executingScope dump
executingScope keys dump
constant dump
"resolution failed" die
} ? *
}
] conds
last
entry =last
} each last ]
} /rewriteConstantPipe deffst
{ ==logic
[ NOP ] ==last
[ logic { ==entry 0 entry * ==action
[
{ action CALL eq { 1 entry * "=" | ::rawCodeAddress eq }' andif { 0 last * PUSH eq }' andif }' {
1 last * ::rawObject ==constant
constant executingScope sys .resolveInfo {
==mode -- ==parentCount 32 add ==offsetInScope ==inExtensionArea
inExtensionArea { offsetInScope 8 add =offsetInScope } rep
mode 16 div 1 band {
[ STATICWRITE offsetInScope parentCount inExtensionArea ] =last
[ NOP ] =entry
} { } ? *
} {
executingScope dump
executingScope keys dump
constant dump
"resolution failed" die
} ? *
}
] conds
last
entry =last
} each last ]
} /rewriteConstantEquals deffst
{ _ ==logic
2 logic len range { ==i i logic * ==entry 0 entry * ==action i 1 sub logic * ==last i 2 sub logic * ==secondLast
[
{ action CALL eq
{ 1 entry * "." | ::rawCodeAddress eq }' andif
{ 0 last * PUSH eq }' andif
{ 0 secondLast * STATICTYPED eq }' andif
}' {
1 last * ::rawObject ==constant
4 secondLast * ==relevantScope
constant relevantScope sys .resolveInfo {
==mode -- ==parentCount 32 add ==offsetInScope ==inExtensionArea
inExtensionArea { offsetInScope 8 add =offsetInScope } rep
mode 16 div 1 band {
[ STATICDOT offsetInScope parentCount inExtensionArea ] i 1 sub logic =[]
[
{ mode 16 mod 0 eq }' {
[ NOP ] i logic =[]
} { mode 16 mod 1 eq }' {
[ CALL "*" | ::rawCodeAddress ] i logic =[]
} { 1 }' {
# TODO this might conceivably be legal though...
"Scope keys: " dump relevantScope keys dump
"Constant: " dump constant dump
"Mode: " dump mode dump
"static . resolution resulted in execution mode neither 0 nor 1" die
}
] conds
} { } ? *
} {
relevantScope dump
relevantScope keys dump
constant dump
"resolution failed" die
} ? *
}
] conds
} each
} /rewriteConstantDot deffst
{ _ ==logic
3 logic len range { ==i
i logic * ==entry
0 entry * ==action
i 1 sub logic * ==last
i 2 sub logic * ==secondLast
i 3 sub logic * ==thirdLast
action CALL eq
{ 1 entry * "*" | ::rawCodeAddress eq }' andif
{ 0 last * PUSH eq }' andif
{ secondLast holdsInt }' andif
{ thirdLast holdsInt }' andif
{
[
{ 1 last * |add ::rawAddress eq
{ 0 secondLast * PUSH eq }' andif
{ 1 secondLast * 128 lt }' andif
}' {
1 secondLast * ==value
[ NATIVE [
/rdx :popqReg
63 /rdx :btrqImm8Reg
[ 8 /rdx /rdx :movqMemDisp8Reg ] len :jcRel8
8 /rdx /rdx :movqMemDisp8Reg
value /rdx :addqImm8Reg
/rdx /rax :movqRegReg
32 /rax :shrqImm8Reg
[
63 /rdx :btsqImm8Reg
/rdx :pushqReg
0 :jmpRel8
] len :jnzRel8
63 /rdx :btsqImm8Reg
/rdx :pushqReg
[
/rdx :pushqReg
::internalAllocateInteger /rax :movqImmReg
/rax :callqReg
8 /rax :popqMemDisp8
/rax :pushqReg
] len :jmpRel8
/rdx :pushqReg
::internalAllocateInteger /rax :movqImmReg
/rax :callqReg
8 /rax :popqMemDisp8
/rax :pushqReg
] ] i logic =[]
[ NOP ] i 1 sub logic =[]
[ NOP ] i 2 sub logic =[]
}
{ 1 last * |add ::rawAddress eq }' {
[ NATIVE [
/rcx :popqReg
63 /rcx :btrqImm8Reg
[ 8 /rcx /rcx :movqMemDisp8Reg ] len :jcRel8
8 /rcx /rcx :movqMemDisp8Reg
/rdx :popqReg
63 /rdx :btrqImm8Reg
[ 8 /rdx /rdx :movqMemDisp8Reg ] len :jcRel8
8 /rdx /rdx :movqMemDisp8Reg
/rcx /rdx :addqRegReg
/rdx /rax :movqRegReg
32 /rax :shrqImm8Reg
[
63 /rdx :btsqImm8Reg
/rdx :pushqReg
0 :jmpRel8
] len :jnzRel8
63 /rdx :btsqImm8Reg
/rdx :pushqReg
[
/rdx :pushqReg
::internalAllocateInteger /rax :movqImmReg
/rax :callqReg
8 /rax :popqMemDisp8
/rax :pushqReg
] len :jmpRel8
/rdx :pushqReg
::internalAllocateInteger /rax :movqImmReg
/rax :callqReg
8 /rax :popqMemDisp8
/rax :pushqReg
] ] i logic =[]
[ NOP ] i 1 sub logic =[]
}
{ ==negatedOpcodeName ==functionName
{ 1 last * functionName | ::rawAddress eq }' {
[ NATIVE [
/rcx :popqReg
63 /rcx :btrqImm8Reg
[ 8 /rcx /rcx :movqMemDisp8Reg ] len :jcRel8
8 /rcx /rcx :movqMemDisp8Reg
/rdx :popqReg
63 /rdx :btrqImm8Reg
[ 8 /rdx /rdx :movqMemDisp8Reg ] len :jcRel8
8 /rdx /rdx :movqMemDisp8Reg
/rsi /rsi :xorqRegReg
/rcx /rdx :cmpqRegReg
[ /rsi :incqReg ] len negatedOpcodeName :
/rsi :incqReg
63 /rsi :btsqImm8Reg
/rsi :pushqReg
] ] i logic =[]
[ NOP ] i 1 sub logic =[]
}
} /logicalOperator deffst
/eq /jnzRel8 logicalOperator
/neq /jzRel8 logicalOperator
/le /jnleRel8 logicalOperator
/lt /jnlRel8 logicalOperator
/ge /jngeRel8 logicalOperator
/gt /jngRel8 logicalOperator
] conds
} rep
} each
} /rewriteArithmetics deffst
{ _ ==logic
1 logic len range { ==i i logic * ==entry 0 entry * ==action i 1 sub logic * ==last
[
{ action CALL eq
{ 1 entry * "*" | ::rawCodeAddress eq }' andif
{ 0 last * STATICTYPED eq }' andif
}' {
4 last * ==executedObject
executedObject sys .typed .type ==type
type [
{ "failed to optimize 'execution' of integer typed object" die }
{
[ STRINGSTAR ] i logic =[]
}
{ "failed to optimize 'execution' of float typed object" die }
{ "objects of type 3 should not appear" die }
{ "failed to optimize 'execution' of extension area" die }
{
# TODO think about handling typed functions
executedObject sys .typed .inputs len { } {
# handle untyped function
executedObject sys .capturedScope {
-- # ignore concrete scope
[ UNTYPEDSCOPEDSTAR ] i logic =[]
} {
[ UNTYPEDUNSCOPEDSTAR ] i logic =[]
} ? *
} ? *
}
{ "failed to optimize 'execution' of raw function opcodes object" die }
{
[ ARRAYSTAR ] i logic =[]
}
{ "failed to optimize 'execution' of function type descriptor" die }
{ } # TODO optimize scope execution one day
{ "failed to optimize 'execution' of name table" die }
{ "objects of type 11 should not appear" die }
{ "objects of type 12 should not appear" die }
{ "objects of type 13 should not appear" die }
{ "objects of type 14 should not appear" die }
{ "objects of type 15 should not appear" die }
] * *
}
] conds
} each
} /rewriteConstantStar deffst
{ _ ==logic
3 logic len range { ==i i logic * ==entry 0 entry * ==action i 1 sub logic * ==last i 2 sub logic * ==secondLast i 3 sub logic * ==thirdLast
[
{ action CALL eq
{ 1 entry * "*" | ::rawCodeAddress eq }' andif
{ 0 last * CALL eq }' andif
{ 1 last * "?" | ::rawCodeAddress eq }' andif
{ 0 secondLast * PUSH eq }' andif
{ 0 thirdLast * PUSH eq }' andif
{ 1 secondLast * ::rawObject sys .typed .type 5 eq }' andif
{ 1 thirdLast * ::rawObject sys .typed .type 5 eq }' andif
{ 1 secondLast * ::rawObject sys .typed .inputs len not }' andif # TODO: think about typed functions
{ 1 thirdLast * ::rawObject sys .typed .inputs len not }' andif # TODO: think about typed functions
}' {
1 secondLast * ::rawObject sys .capturedScope _ { -01 -- } { } ? * ==hasScope
1 thirdLast * ::rawObject sys .capturedScope _ { -01 -- } { } ? * hasScope or =hasScope
hasScope {
[ UNTYPEDSCOPEDSTAR ] i logic =[]
} {
[ UNTYPEDUNSCOPEDSTAR ] i logic =[]
} ? *
}
] conds
} each
} /rewriteConstantQuestionStar deffst
{ ==logic
[ NOP ] ==last
[ logic { ==entry 0 entry * ==action
[
{ action CALL eq { 1 entry * "?" | ::rawCodeAddress eq }' andif }' {
[ NATIVE [
/rcx :popqReg
/rdx :popqReg
/rax :popqReg
63 /rax :btrqImm8Reg
[ 8 /rax /rax :movqMemDisp8Reg ] len :jcRel8
8 /rax /rax :movqMemDisp8Reg
/rax /rax :testqRegReg
/rcx /rdx :cmovzqRegReg
/rdx :pushqReg
] ] =entry
}
{ action CALL eq { 1 entry * "_" | ::rawCodeAddress eq }' andif }' {
[ NATIVE [
0 /rsp :pushqMemDisp8
] ] =entry
}
{ action CALL eq { 1 entry * "--" | ::rawCodeAddress eq }' andif }' {
[ NATIVE [
8 /rsp :addqImm8Reg
] ] =entry
}
{ action CALL eq { 1 entry * "-" | ::rawCodeAddress eq }' andif { 0 last * PUSH eq }' andif }' {
1 last * ::rawObject ==constant
0 ==largestNumber
0 ==starUsed
constant {
_ 0 "*" * eq { 1 =starUsed } rep
0 "0" * sub largestNumber max =largestNumber
} each
[ /rax /rcx /rdx /rbx /rbp /rsi /rdi ] =*:availableRegisters
starUsed {
# TODO maybe implement this one day
} {
largestNumber |availableRegisters len gt {
# TODO maybe implement this one day
} {
[ NATIVE [
0 largestNumber 1 add range {
availableRegisters :popqReg
} each
constant {
0 "0" * sub availableRegisters :pushqReg
} each
] ] =entry
[ NOP ] =last
} ? *
} ? *
}
] conds
last
entry =last
} each last ]
} /rewriteSimpleFunctions deffst
o ::rawAddress ==addr
[ addr _ 4 add range peek each ] 256 math .unbase ==totalLength
[ addr 8 add _ 4 add range peek each ] 256 math .unbase ==codeLength
addr 16 add ==i
{ =*ops =*set =*get ==pattern
1 ==found
get ==j
pattern { _ j ops -01 { eq not { 0 =found }' rep }" { -- -- }" ? * j 1 add =j }' each
found _ { j set }' { }" ? *
} /generalMatch deff
{ { i }' { =i }' peek generalMatch }' /match deff
generalHeaderPattern match not { "failure while matching generalHeaderPattern" die }" rep
scopingHeaderPattern match ==isScoping
1 ==continueParsing
[ ] ==newLogic
{ newLogic [ -102 ] cat =newLogic }' /emitLogic deffst
{ continueParsing }' { i ==s
[
{ footerPattern match }' {
0 =continueParsing
}
{ pushConstantPattern match }' {
[ s 2 add _ 8 add range peek each ] 256 math .unbase ==pushedConstant
[ PUSH pushedConstant ] emitLogic
}
{ callConstantPattern match }' {
[ s 2 add _ 8 add range peek each ] 256 math .unbase ==calledAddress
calledAddress ==j
{ { j }' { =j }' peek generalMatch }' /callTargetMatch deff
[
{ calledAddress 105553116266496 lt }' { # HEAPBASE # FIXME: should use global constant
[ CALL calledAddress ] emitLogic
}
{ constantActiveGeneralPattern callTargetMatch }' {
[ calledAddress 3 add _ 8 add range peek each ] 256 math .unbase ==calledConstant
[ PUSH calledConstant ] emitLogic
[ CALL "*" | ::rawCodeAddress ] emitLogic
}
{ constantNormalFunctionScopedUntypedPattern callTargetMatch }' {
[ calledAddress 13 add _ 8 add range peek each ] 256 math .unbase ==functionScope
[ calledAddress 23 add _ 8 add range peek each ] 256 math .unbase ==finalAddress
[ CALLSCOPED finalAddress functionScope ] emitLogic
}
{ constantNormalFunctionUnscopedUntypedPattern callTargetMatch }' {
[ calledAddress 2 add _ 8 add range peek each ] 256 math .unbase ==finalAddress
[ CALL finalAddress ] emitLogic
}
{ constantPassivePattern callTargetMatch }' {
[ calledAddress 3 add _ 8 add range peek each ] 256 math .unbase ==pushedConstant
[ PUSH pushedConstant ] emitLogic
}
{ staticLoadPattern callTargetMatch }' {
0 ==parentCount
{ staticLoadParentPattern callTargetMatch }' {
parentCount 1 add =parentCount
} loop
j ==loadStart
[
{ staticLoadFromScopePattern callTargetMatch }' {
[ loadStart 4 add _ 4 add range peek each ] 256 math .unbase ==offsetInScope
[ calledAddress 8 sub _ 8 add range peek each ] 256 math .unbase ==exampleObjectOffset
[ calledAddress exampleObjectOffset add _ 8 add range peek each ] 256 math .unbase ::rawObject ==exampleObject
[ STATICTYPED offsetInScope parentCount 0 exampleObject ] emitLogic
}
{ staticLoadFromExtensionPattern callTargetMatch }' {
[ loadStart 14 add _ 4 add range peek each ] 256 math .unbase ==offsetInScope
[ calledAddress 8 sub _ 8 add range peek each ] 256 math .unbase ==exampleObjectOffset
[ calledAddress exampleObjectOffset add _ 8 add range peek each ] 256 math .unbase ::rawObject ==exampleObject
[ STATICTYPED offsetInScope parentCount 1 exampleObject ] emitLogic
}
{ 1 }' {
[ j j 16 add range peek each ] dump
o dump
j dump
"unparsed static load opcodes in sys .opt .hook (optimizing version)" die
}
] conds
[
{ staticLoadActivePattern callTargetMatch }' {
[ CALL "*" | ::rawCodeAddress ] emitLogic
}
{ staticLoadPassivePattern callTargetMatch }' {
# nothing to emit
}
{ 1 }' {
[ j j 16 add range peek each ] dump
o dump
j dump
"unparsed static load opcodes in sys .opt .hook (optimizing version)" die
}
] conds
}
{ customFunctionConstantPassivePattern callTargetMatch }' {
[ calledAddress 9 add _ 8 add range peek each ] 256 math .unbase ==pushedConstant
[ PUSH pushedConstant ] emitLogic
}
{ customFunctionHeaderPattern callTargetMatch }' {
[ CALL calledAddress ] emitLogic
}
{ 1 }' {
[ j j 16 add range peek each ] dump
o dump
j dump
"unparsed call target opcodes in sys .opt .hook (optimizing version)" die
}
] conds
}
{ 1 }' {
[ i i 16 add range peek each ] dump
o dump
i dump
"unparsed opcodes in sys .opt .hook (optimizing version)" die
}
] conds
} loop
[ ] ==newOpcodes
{ newOpcodes -01 cat =newOpcodes }' /emitOpcodes deffst
[ ] ==newReferences
{ ==ref
[
{ ref 105553116266496 lt } { } # HEAPBASE # FIXME: use a global constant
{ ref 123145302310912 ge } { } # %700000000000
{ 1 } { newReferences [ ref ] cat =newReferences }
] conds
}' /emitReference deffst
# [ :ud2 ] emitOpcodes # enable for further development
isScoping {
[
8 /r15 :subqImm8Reg
/r15 :popqMem
8 /r15 :subqImm8Reg
/r14 /r15 :movqRegMem
/r14 /rsi :movqRegReg
2 /rdi :movqImmReg # FIXME: this should use INITIALSCOPESIZE
::internalAllocateScope /rax :movqImmReg
/rax :callqReg
/rax /r14 :movqRegReg
] emitOpcodes
}" {
[
8 /r15 :subqImm8Reg
/r15 :popqMem
] emitOpcodes
}" ? *
newLogic
testScopeModifications
containsScopeModifications not {
rewriteConstantPipe
rewriteConstantEquals
} rep
rewriteConstantDot
rewriteConstantStar
rewriteConstantQuestionStar
rewriteArithmetics
rewriteSimpleFunctions
{ =*entry 0 entry ==action
[
{ action PUSH eq }' {
[
1 entry /rax :movqImmReg
/rax :pushqReg
] emitOpcodes
1 entry emitReference
}
{ action CALL eq }' {
[
1 entry /rax :movqImmReg
/rax :callqReg
] emitOpcodes
1 entry 16 sub emitReference
}
{ action CALLSCOPED eq }' {
[
8 /r15 :subqImm8Reg
/r14 /r15 :movqRegMem
2 entry /r14 :movqImmReg
1 entry /rdi :movqImmReg
/rdi :callqReg
/r15 /r14 :movqMemReg
8 /r15 :addqImm8Reg
] emitOpcodes
1 entry 16 sub emitReference
2 entry emitReference
}
{ action [ STATIC STATICTYPED ] eq any }' {
[
2 entry {
16 /r14 /rax :movqMemDisp8Reg
2 entry 1 sub { 16 /rax /rax :movqMemDisp8Reg } rep
3 entry {
24 /rax /rcx :movqMemDisp8Reg # load extension area pointer
/rax /edx :movlMemReg # load scope length
/rdx :negqReg # prepare for substraction # TODO the length calculation could be done beforehand
1 entry 1 /rdx /rcx :pushqMemIndexScaleDisp32 # push loaded entry to stack
} {
1 entry /rax :pushqMemDisp32
} ? *
} {
3 entry {
24 /r14 /rcx :movqMemDisp8Reg # load extension area pointer
/r14 /edx :movlMemReg # load scope length
/rdx :negqReg # prepare for substraction # TODO the length calculation could be done beforehand
1 entry 1 /rdx /rcx :pushqMemIndexScaleDisp32 # push loaded entry to stack
} {
1 entry /r14 :pushqMemDisp32
} ? *
} ? *
] emitOpcodes
}
{ action STATICWRITE eq }' {
[
2 entry {
16 /r14 /rax :movqMemDisp8Reg
2 entry 1 sub { 16 /rax /rax :movqMemDisp8Reg } rep
3 entry {
24 /rax /rcx :movqMemDisp8Reg # load extension area pointer
/rax /edx :movlMemReg # load scope length
/rdx :negqReg # prepare for substraction
1 entry 1 /rdx /rcx :popqMemIndexScaleDisp32 # load entry from stack
} {
1 entry /rax :popqMemDisp32
} ? *
} {
3 entry {
24 /r14 /rcx :movqMemDisp8Reg # load extension area pointer
/r14 /edx :movlMemReg # load scope length
/rdx :negqReg # prepare for substraction
1 entry 1 /rdx /rcx :popqMemIndexScaleDisp32 # load entry from stack
} {
1 entry /r14 :popqMemDisp32
} ? *
} ? *
] emitOpcodes
}
{ action STATICDOT eq }' {
[
/rax :popqReg
2 entry { 16 /rax /rax :movqMemDisp8Reg } rep
3 entry {
24 /rax /rcx :movqMemDisp8Reg # load extension area pointer
/rax /edx :movlMemReg # load scope length
/rdx :negqReg # prepare for substraction
1 entry 1 /rdx /rcx :pushqMemIndexScaleDisp32 # push loaded entry to stack
} {
1 entry /rax :pushqMemDisp32
} ? *
] emitOpcodes
}
{ action UNTYPEDSCOPEDSTAR eq }' {
[
/rax :popqReg
8 /r15 :subqImm8Reg
/r14 /r15 :movqRegMem
8 /rax /r14 :movqMemDisp8Reg
24 /rax /rax :movqMemDisp8Reg
16 /rax :addqImm8Reg
/rax :callqReg
/r15 /r14 :movqMemReg
8 /r15 :addqImm8Reg
] emitOpcodes
}
{ action UNTYPEDUNSCOPEDSTAR eq }' {
[
/rax :popqReg
24 /rax /rax :movqMemDisp8Reg
16 /rax :addqImm8Reg
/rax :callqReg
] emitOpcodes
}
{ action ARRAYSTAR eq }' {
[
/rsi :popqReg
/rax :popqReg
63 /rax :btrqImm8Reg
[ 8 /rax /rax :movqMemDisp8Reg ] len :jcRel8
8 /rax /rax :movqMemDisp8Reg
/rsi /ecx :movlMemReg
3 /rcx :shrqImm8Reg
/rcx :decqReg
:cqo
/rcx :idivqReg
0 /rdx :cmpqImm8Reg
[ /rcx /rdx :addqRegReg ] len :jgeRel8
/rcx /rdx :addqRegReg
8 8 /rdx /rsi :pushqMemIndexScaleDisp8
] emitOpcodes
}
{ action STRINGSTAR eq }' {
[
/rsi :popqReg
/rax :popqReg
63 /rax :btrqImm8Reg
[ 8 /rax /rax :movqMemDisp8Reg ] len :jcRel8
8 /rax /rax :movqMemDisp8Reg
16 /rsi /rcx :movqMemDisp8Reg
:cqo
/rcx :idivqReg
0 /rdx :cmpqImm8Reg
[ /rcx /rdx :addqRegReg ] len :jgeRel8
/rcx /rdx :addqRegReg
24 1 /rdx /rsi /rbx :movzxMem8IndexScaleDisp8Reg64
63 /rbx :btsqImm8Reg
/rbx :pushqReg
] emitOpcodes
}
{ action NATIVE eq }' {
1 entry emitOpcodes
}
{ action NOP eq }' {
}
{ 1 }' {
entry dump
"invalid intermediate code during optimize" die
}
] conds
} each
isScoping {
[
/r15 /r14 :movqMemReg
16 /r15 :addqImm8Reg
8 neg /r15 :jmpqMemDisp8
] emitOpcodes
}' {
[
/r15 :pushqMem
8 /r15 :addqImm8Reg
:retn
] emitOpcodes
}' ? *
newOpcodes newReferences o ::replace
1 executingScope # return something different from o to signal successful optimization
[ ] _ =newOpcodes
_ =newReferences
=newLogic
} /optimize deffd
0 ==recursionDepth
{
recursionDepth 1 add =recursionDepth
recursionDepth 3 lt { optimize } { } ? *
recursionDepth 1 sub =recursionDepth
} /hook sys .opt .deff
> --
# ensure that the optimizer is run often enough to finish optimize itself
# while in practice this is also achieved by the next freeze, it should be done explicitely
100 { scope ==s
"{" s sys .executeIdentifier =s
1 1
"add" s sys .executeIdentifier =s
"--" s sys .executeIdentifier =s
"}" s sys .executeIdentifier =s
*
} rep
# vim: syn=elymas
|