-
Notifications
You must be signed in to change notification settings - Fork 3
/
codegen.e
4062 lines (3483 loc) · 114 KB
/
codegen.e
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-> ECX/codegen.e
/* ECX by Leif Salomonsson [ecx tele2 se] is Copyright (c) 2002-2008 */
/* Released under the ECX COMPILER LICENSE, See ECXCOMPILERLICENSE.TXT */
OPT MODULE
OPT PREPROCESS
OPT LARGE
OPT EXPORT
-> Sept 2007: ecx/codegen.e created. Moved all the codegen stuff into this module.
->#define DBG_CODEGEN
MODULE 'exec/lists'
MODULE '*compiler'
MODULE '*common'
MODULE '*binary'
MODULE '*runtime'
MODULE '*support'
MODULE '*opcodes68'
MODULE '*opcodesppc'
MODULE '*assembler' -> v55
-> v58
CONST D64STACKSIZE = 8*8
#define Put32(v) g_codeptr[]++ := v
OBJECT derefargs
pk -> copy of it_varexp.postkey
eo
ed
nd -> "nodest"
am -> copy of it_varexp.assignmod
ENDOBJECT
-> v49
OBJECT lastx
iid -> new IID_XXX
start
end
o
d
cond
rx
ENDOBJECT
-> v49, g_regusetable point to array of this now, isntead of CHAR
OBJECT oreg -> obtainable register
obtains:CHAR -> count of obtains (totals read and write)
write:CHAR -> number of write-obtains, or 0. was: 1 if obtained for write, else 0
ENDOBJECT
DEF g_codeptr:PTR TO LONG, g_codebuf, g_databuf
DEF g_gvarlist:PTR TO gvar
DEF g_currentproc:PTR TO proc
DEF link_codesize
DEF g_sizeofptr
DEF g_linelist:PTR TO linedef, g_codelablist:PTR TO codelab, g_multireturn:PTR TO multireturn
DEF g_optpowerpc, g_optmodule
DEF g_globalsize, g_rwreflist:PTR TO rwref, g_databufsize
DEF g_nilcheck, g_linedebug, g_stepdebug,g_stepdebug50
DEF link_reloc32list, link_nrofreloc32s
DEF g_linenum, g_numregalloc, g_numfregalloc
DEF g_stacksize, g_modulelist:PTR TO mlh
DEF g_objectlist:PTR TO object, g_symbolhunk
DEF g_naturalalign -> 1.8.2
DEF g_regusetab:PTR TO oreg
DEF g_lastx:PTR TO lastx
DEF g_dreg -> RX/DRX
DEF g_areg -> RX/ARX
DEF g_freg -> FPX
DEF g_vreg -> VX
DEF g_d64reg -> D64 v55
DEF g_ireg0 -> R3/D0 (non obtainable)
DEF g_ireg1 -> R4/D1
DEF g_freg0 -> FP1, F1
DEF g_stackreg -> R1/A7 (non obtainable, dedicated)
DEF g_selfreg -> R12/A0
DEF g_globreg -> R13/A4 (non obtainable, dedicated)
DEF g_framereg -> R1/A5 (non obtainable, dedicated)
DEF g_atemp -> R11/A6 (non obtainable)
DEF g_dtemp -> R12/D3 (non obtain)
DEF g_ftemp -> FP0/F0 (non obtain)
DEF g_d64temp -> v55
DEF g_safeimmlists -> v57, imported from main and used by doList()
CONST REGUSETABSIZE=96
#define PTRSIZE g_sizeofptr
#define REALSIZE 8
ENUM ISEQ, ISNE, ISGT, ISLT, ISGE, ISLE
ENUM IID_DUMMY,
IID_COPY, -> o1,d1, o2,d2
IID_RET, ->
IID_BIC,
IID_NEGREG, -> v42
IID_MULREG,
IID_DIVREG,
IID_SHRREG,
IID_SHLREG,
IID_ADDREG,
IID_SUBREG,
IID_ORREG,
IID_ANDREG,
IID_SICREG, -> v41
IID_XORREG, -> v55
IID_NOTREG, -> v55
IID_ASRREG, -> v55
IID_ABSREG, -> 2.2
-> v55
IID_NEGD64,
IID_MULD64,
IID_DIVD64,
IID_SHRD64,
IID_SHLD64,
IID_ADDD64,
IID_SUBD64,
IID_ORD64,
IID_ANDD64,
IID_SICD64,
IID_D642F,
IID_F2D64,
IID_D642I,
IID_I2D64,
IID_XORD64,
IID_NOTD64,
IID_ASRD64,
IID_ABSD64, -> 2.2
IID_FADDREG,
IID_FSUBREG,
IID_FMULREG,
IID_FDIVREG,
IID_I2FREG,
IID_F2IREG,
IID_FSICREG, -> v41
IID_FNEGREG, -> v44
IID_FABSREG, -> 2.2
IID_GOLAB,
IID_GOARX,
IID_VARADR, -> new
IID_GETRWD, -> new v34
IID_LABADR, -> v44
IID_GOSLAB,
IID_GOSARX,
IID_GETIMMSTR, -> v49 (str, arx)
IID_INCVAR, -> 1.5.3 (var,val)
IID_INCARX, -> 1.5.3 (arx,val)
IID_PUSH, -> 1.8.1 (size,o,d)
IID_POP -> 1.8.1 (size,o,d)
-> ofs:16, size:5, ax:5, flags:5, rsrvd:1
#define AxSizeOfsF(ax,size,ofs,flags) __axsizeofsf(ax,size,ofs,flags)
PROC __axsizeofsf(ax,size,ofs,flags) IS (Shl(ofs AND $FFFF,16) OR
Shl(size,11) OR
Shl(ax,6) OR
flags)
-> scale:5, idrx:5, size:5, ax:5, flags:5
#define AxSizeIdrxScaleF(ax,size,idrx,scale,flags) __axsizeidrxscalef(ax,size,idrx,scale,flags)
PROC __axsizeidrxscalef(ax,size,idrx,scale,flags) IS (Shl(scale,27) OR
Shl(idrx,22) OR
Shl(size,17) OR
Shl(ax,12) OR
Shl(flags,7))
#define AxSizeOfs(ax,size,ofs) AxSizeOfsF(ax,size,ofs,NIL)
#define AxSizeIdrxScale(ax,size,idrx,scale) AxSizeIdrxScaleF(ax,size,idrx,scale,NIL)
#define ARXPOarx(x) (Shr(x,6) AND $1F)
#define ARXPOsize(x) (Shr(x,11) AND $1F)
#define ARXPOofs(x) Shr(x,16)
#define ARXPOflags(x) (x AND $1F)
#define ARXPXarx(x) (Shr(x,12) AND $1F)
#define ARXPXsize(x) (Shr(x,17) AND $1F)
#define ARXPXidrx(x) (Shr(x,22) AND $1F)
#define ARXPXscale(x) (Shr(x,27) AND $1F)
#define ARXPXflags(x) (Shr(x,7) AND $1F)
-> 1.5.3
-> hintargs might be NIL, if so ignore it.
-> it IS safe to ignore hintargs completely. (unless you are dvs_#?())
-> A copy of result is always done by caller anyway.
OBJECT hintargs
-> type of register we would _prefer_ to have result in.
-> DREG/AREG/FREG/VREG
hregop:INT
-> if > -1 then we would _prefer_ result in this specific register #
hregnum:INT
-> if TRUE, hreg is register variable
-> getlist2() and mathlogic() checks this and avoids using if TRUE
hregisvar:INT
ENDOBJECT
OBJECT codegen
-> 1.6.1
fastnew_lif:PTR TO lif
fastdispose_lif:PTR TO lif
throw_lif:PTR TO lif
raise_lif:PTR TO lif
-> 1.8.0
ivar_exception:PTR TO gvar
pvar_exceptstruct:PTR TO gvar
ENDOBJECT
#define GLOBREG g_globreg
#define STACKREG g_stackreg
#define FRAMEREG g_framereg
#define IREG0 g_ireg0
#define IREG1 g_ireg1
#define IREG2 g_ireg2
#define FREG0 g_freg0
#define VREG0 g_vreg0
#define SELFREG g_selfreg
-> theese 3 are NOT obtainable !
#define ATEMP g_atemp
#define DTEMP g_dtemp
#define FTEMP g_ftemp
#define VTEMP g_vtemp
#define D64TEMP g_d64temp
#define EMPTYMETHOD(xxx,codegen) PROC xxx OF codegen IS WriteF('codegen.xxx is EMPTY !\n') BUT NIL
EMPTYMETHOD(doAsm(n), codegen)
EMPTYMETHOD(dispatcher(iid,o1,d1,o2,d2,e1,e2,e3), codegen)
EMPTYMETHOD(prochead(tab,tabsize, ftab,ftabsize), codegen)
EMPTYMETHOD(exceptblock(do), codegen)
EMPTYMETHOD(endproc1(), codegen)
EMPTYMETHOD(endproc2(), codegen)
EMPTYMETHOD(obtainDREG(), codegen)
EMPTYMETHOD(releaseDREG(r,b), codegen)
EMPTYMETHOD(obtainAREG(), codegen)
EMPTYMETHOD(releaseAREG(r,b), codegen)
EMPTYMETHOD(obtainFREG(), codegen)
EMPTYMETHOD(releaseFREG(r,b), codegen)
EMPTYMETHOD(obtainVREG(), codegen)
EMPTYMETHOD(releaseVREG(r,b), codegen)
EMPTYMETHOD(saveObtained(regusecopy), codegen)
EMPTYMETHOD(loadObtained(regusecopy), codegen)
EMPTYMETHOD(doProcfunc(n,meth), codegen)
EMPTYMETHOD(doLibfunc(n), codegen)
EMPTYMETHOD(doVarfunc(n), codegen)
EMPTYMETHOD(doIntfunc(n), codegen)
EMPTYMETHOD(newMemory(sizeo,sized), codegen)
EMPTYMETHOD(endMemory(sizeo,sized,varo,vard), codegen)
EMPTYMETHOD(newClassObject(obj), codegen)
EMPTYMETHOD(endClassObject(obj,varo,vard), codegen)
-> v47
EMPTYMETHOD(nilCheck(arx, line), codegen)
EMPTYMETHOD(copyObtainToDREG(o,d), codegen)
EMPTYMETHOD(copyObtainToAREG(o,d), codegen)
EMPTYMETHOD(copyObtainToFREG(o,d), codegen)
EMPTYMETHOD(copyObtainToVREG(o,d), codegen)
-> V48
EMPTYMETHOD(abiArgs(array,numargs,flags), codegen)
-> v49
EMPTYMETHOD(doReturn(n,r), codegen)
EMPTYMETHOD(abiReturns(mret), codegen)
-> v50
EMPTYMETHOD(secureReturn(ro, rd, regusecopy), codegen)
EMPTYMETHOD(resetObtainStart(), codegen)
-> V50 (implemented 1.5.4)
EMPTYMETHOD(mathlogichead(n, float, neg, ha), codegen)
-> v50 (1.5.3)
EMPTYMETHOD(quotehead(), codegen)
EMPTYMETHOD(quotetail(qr1,qr2,o,d,qarx), codegen)
EMPTYMETHOD(lockDREG(dreg), codegen)
EMPTYMETHOD(lockAREG(areg), codegen)
EMPTYMETHOD(lockFREG(freg), codegen)
-> v50 (1.5.4)
EMPTYMETHOD(getFreeDREG(), codegen)
EMPTYMETHOD(getFreeAREG(), codegen)
EMPTYMETHOD(getFreeFREG(), codegen)
-> v55 (1.10.0)
EMPTYMETHOD(getFreeD64(), codegen)
EMPTYMETHOD(lockD64(reg), codegen)
EMPTYMETHOD(copyObtainToD64(o,d), codegen)
EMPTYMETHOD(obtainD64(), codegen)
EMPTYMETHOD(releaseD64(r,b), codegen)
EMPTYMETHOD(copy(o1,d1,o2,d2), codegen)
EMPTYMETHOD(dispatcher2(iid,o1,d1,o2,d2,e1,e2,e3), codegen)
EMPTYMETHOD(dispatcher3(iid,o1,d1,o2,d2,e1,e2,e3), codegen)
-> v57
EMPTYMETHOD(putInit(globsize,ivarsize), codegen)
EMPTYMETHOD(putGlobInit(), codegen)
-> v58
EMPTYMETHOD(doMethod(n,as,as2,postkey,type), codegen)
PROC initCodegen() OF codegen
#ifdef DBG_CODEGEN
DEBUGF('initCodegen()\n')
#endif
NEW g_lastx
NEW g_regusetab[REGUSETABSIZE]
ENDPROC
PROC checkmemlist()
DEF s, a
DEBUGF('FreeStack=$\h AvailMem=$\h\n', s := FreeStack(), a := AvailMem($20000))
IF s < 1000 THEN Raise("STCK")
IF a < 1000000 THEN Raise("MEM")
ENDPROC
PROC clearRegs() -> for 68k and ppc, at label entry, inline asm, etc-
DEF a, t, var:REG PTR TO var
#ifdef DBG_CODEGEN
DEBUGF('clearRegs(): ')
#endif
#ifdef DBG_CODEGEN
DEBUGF('\n')
#endif
var := g_gvarlist
WHILE var
#ifdef DBG_CODEGEN
IF var.intreg THEN DEBUGF(' global \s:\d cleared\n', var.hln.name, var.treg)
#endif
var.intreg := FALSE -> clear
var := var.next
ENDWHILE
IF g_currentproc -> changed v50 (1.5.1)
var := g_currentproc.varchain
WHILE var
#ifdef DBG_CODEGEN
IF var.intreg THEN DEBUGF(' local \s:\d cleared\n', var.hln.name, var.treg)
#endif
var.intreg := FALSE -> clear
var := var.varchain
ENDWHILE
ENDIF
ENDPROC
PROC def_label(lab:PTR TO codelab)
clearRegs()
g_lastx.iid := NIL -> 1.6.0
g_lastx.end := NIL
lab.offset := currentOffset()
ENDPROC
PROC reverseCondition(cond) -> used also by ecxmain for RAISE functionality
SELECT cond
CASE ISEQ ; cond := ISNE
CASE ISNE ; cond := ISEQ
CASE ISGT ; cond := ISLE
CASE ISLT ; cond := ISGE
CASE ISLE ; cond := ISGT
CASE ISGE ; cond := ISLT
ENDSELECT
ENDPROC cond
#define inst_copy(o1,d1,o2,d2) self.copy(o1,d1,o2,d2)
#define inst_varadr(var,arx) self.dispatcher(IID_VARADR,NIL,var,NIL,arx,0,0,0)
#define inst_incvar(var,val) self.dispatcher(IID_INCVAR,NIL,var,NIL,val,0,0,0)
#define inst_decvar(var,val) self.dispatcher(IID_INCVAR,NIL,var,NIL,0-(val),0,0,0)
#define inst_incarx(arx,val) self.dispatcher(IID_INCARX,NIL,arx,NIL,val,0,0,0)
#define inst_decarx(arx,val) self.dispatcher(IID_INCARX,NIL,arx,NIL,0-(val),0,0,0)
#define inst_getrwd(rwdofs, ax) self.dispatcher(IID_GETRWD,NIL,rwdofs,NIL,ax,0,0,0)
#define inst_ret(rtsbool) self.dispatcher(IID_RET,NIL,rtsbool,0,0,0,0,0)
#define inst_negreg(d1) self.dispatcher(IID_NEGREG,NIL,d1,0,0,0,0,0)
#define inst_mulreg(o1,d1,d2) self.dispatcher(IID_MULREG,o1,d1,NIL,d2,0,0,0)
#define inst_divreg(o1,d1,d2) self.dispatcher(IID_DIVREG,o1,d1,NIL,d2,0,0,0)
#define inst_bic(o1,d1,c,o2,d2,label) self.dispatcher(IID_BIC,o1,d1,o2,d2,c,label,0)
#define inst_shlreg(o1,d1,d2) self.dispatcher(IID_SHLREG,o1,d1,NIL,d2,0,0,0)
#define inst_shrreg(o1,d1,d2) self.dispatcher(IID_SHRREG,o1,d1,NIL,d2,0,0,0)
#define inst_orreg(o1,d1,d2) self.dispatcher(IID_ORREG,o1,d1,NIL,d2,0,0,0)
#define inst_andreg(o1,d1,d2) self.dispatcher(IID_ANDREG,o1,d1,NIL,d2,0,0,0)
#define inst_golab(lab) self.dispatcher(IID_GOLAB,NIL,lab,0,0,0,0,0)
#define inst_goarx(lab) self.dispatcher(IID_GOARX,NIL,lab,0,0,0,0,0)
#define inst_addreg(o1,d1,d2) self.dispatcher(IID_ADDREG,o1,d1,NIL,d2,0,0,0)
#define inst_subreg(o1,d1,d2) self.dispatcher(IID_SUBREG,o1,d1,NIL,d2,0,0,0)
#define inst_sicreg(o1,d1,cond,reg) self.dispatcher(IID_SICREG,o1,d1,NIL,NIL,cond,reg,0)
#define inst_labadr(lab,reg) self.dispatcher(IID_LABADR,NIL,lab,NIL,reg,0,0,0)
#define inst_goslab(l) self.dispatcher(IID_GOSLAB,NIL,l,0,0,0,0,0)
#define inst_gosarx(a) self.dispatcher(IID_GOSARX,NIL,a,0,0,0,0,0)
#define inst_getimmstr(str, arx) self.dispatcher(IID_GETIMMSTR,0,0,0,0,str,arx,0)
#define inst_push(size,o,d) self.dispatcher(IID_PUSH,o,d,NIL,NIL,size,0,0)
#define inst_pop(size,o,d) self.dispatcher(IID_POP,o,d,NIL,NIL,size,0,0)
#define inst_asrreg(o1,d1,d2) self.dispatcher(IID_ASRREG,o1,d1,NIL,d2,0,0,0)
#define inst_xorreg(o1,d1,d2) self.dispatcher(IID_XORREG,o1,d1,NIL,d2,0,0,0)
#define inst_notreg(d1) self.dispatcher(IID_NOTREG,NIL,d1,0,0,0,0,0)
#define inst_absreg(d1) self.dispatcher(IID_ABSREG,NIL,d1,0,0,0,0,0)
#define inst_faddreg(o1,d1,d2) self.dispatcher2(IID_FADDREG,o1,d1,NIL,d2,0,0,0)
#define inst_fsubreg(o1,d1,d2) self.dispatcher2(IID_FSUBREG,o1,d1,NIL,d2,0,0,0)
#define inst_fmulreg(o1,d1,d2) self.dispatcher2(IID_FMULREG,o1,d1,NIL,d2,0,0,0)
#define inst_fdivreg(o1,d1,d2) self.dispatcher2(IID_FDIVREG,o1,d1,NIL,d2,0,0,0)
#define inst_fnegreg(d1) self.dispatcher2(IID_FNEGREG,NIL,d1,0,0,0,0,0)
#define inst_fsicreg(o1,d1,cond,freg,reg) self.dispatcher2(IID_FSICREG,o1,d1,NIL,NIL,cond,freg,reg)
#define inst_i2f(r,f) self.dispatcher2(IID_I2FREG,NIL,r,0,f,0,0,0)
#define inst_f2i(f,r) self.dispatcher2(IID_F2IREG,NIL,f,0,r,0,0,0)
#define inst_fabsreg(d1) self.dispatcher2(IID_FABSREG,NIL,d1,0,0,0,0,0)
-> 1.10.0
#define inst_addd64(o1,d1,d2) self.dispatcher3(IID_ADDD64,o1,d1,NIL,d2,0,0,0)
#define inst_subd64(o1,d1,d2) self.dispatcher3(IID_SUBD64,o1,d1,NIL,d2,0,0,0)
#define inst_i2d64(r,f) self.dispatcher3(IID_I2D64,NIL,r,0,f,0,0,0)
#define inst_f2d64(f,r) self.dispatcher3(IID_F2D64,NIL,f,0,r,0,0,0)
#define inst_d642f(f,r) self.dispatcher3(IID_D642F,NIL,f,0,r,0,0,0)
#define inst_d642i(f,r) self.dispatcher3(IID_D642I,NIL,f,0,r,0,0,0)
#define inst_sicd64(o1,d1,cond,reg64,reg) self.dispatcher3(IID_SICD64,o1,d1,NIL,NIL,cond,reg64,reg)
#define inst_shld64(o1,d1,d2) self.dispatcher3(IID_SHLD64,o1,d1,NIL,d2,0,0,0)
#define inst_shrd64(o1,d1,d2) self.dispatcher3(IID_SHRD64,o1,d1,NIL,d2,0,0,0)
#define inst_ord64(o1,d1,d2) self.dispatcher3(IID_ORD64,o1,d1,NIL,d2,0,0,0)
#define inst_andd64(o1,d1,d2) self.dispatcher3(IID_ANDD64,o1,d1,NIL,d2,0,0,0)
#define inst_negd64(d1) self.dispatcher3(IID_NEGD64,NIL,d1,0,0,0,0,0)
#define inst_muld64(o1,d1,d2) self.dispatcher3(IID_MULD64,o1,d1,NIL,d2,0,0,0)
#define inst_divd64(o1,d1,d2) self.dispatcher3(IID_DIVD64,o1,d1,NIL,d2,0,0,0)
#define inst_asrd64(o1,d1,d2) self.dispatcher3(IID_ASRD64,o1,d1,NIL,d2,0,0,0)
#define inst_xord64(o1,d1,d2) self.dispatcher3(IID_XORD64,o1,d1,NIL,d2,0,0,0)
#define inst_notd64(d1) self.dispatcher3(IID_NOTD64,NIL,d1,0,0,0,0,0)
#define inst_absd64(d1) self.dispatcher3(IID_ABSD64,NIL,d1,0,0,0,0,0)
PROC def_line(l)
IF l <= 0 THEN RETURN
IF g_linelist
IF g_linelist.line = l THEN RETURN
IF g_linelist.offset = (currentOffset()) THEN RETURN
ENDIF
g_linelist := NEW [g_linelist, l, currentOffset()]:linedef
IF g_stepdebug AND (g_optpowerpc=CPU_M68)
nop()
ENDIF
IF g_stepdebug50 AND (g_optpowerpc=CPU_M68)
nop()
nop()
nop()
ENDIF
ENDPROC
/*************************************************************
*************************** methods of codegen ***************
*************************************************************/
PROC endLocalClasses(proc:PTR TO proc) OF codegen
DEF va:PTR TO var, m:PTR TO proc
va := proc.locals00 -> v46, call .end() on classes
WHILE va
IF va.type.object
IF va.type.numes = 1
IF va.type.object.nrofmethods
IF va.type.object.destofs > -1 -> call .end() ?
m := va.type.object.methodtable[va.type.object.destofs/4]
IF m.flags AND PROCF_CLMETH = FALSE
inst_copy(va.o,va.d, AREG, SELFREG)
inst_copy(ARXPO,AxSizeOfs(SELFREG,PTRSIZE,va.type.object.classofs), AREG,ATEMP)
inst_copy(ARXPO,AxSizeOfs(ATEMP,PTRSIZE,va.type.object.destofs), AREG, ATEMP)
inst_gosarx(ATEMP)
ELSE -> 1.8.0
inst_copy(va.o,va.d, AREG,m.selfreg)
IF m.offset
inst_goslab(m)
ELSE
inst_labadr(va.type.object, ATEMP)
inst_copy(ARXPO,AxSizeOfs(ATEMP,PTRSIZE,va.type.object.destofs), AREG, ATEMP)
inst_gosarx(ATEMP)
ENDIF
ENDIF
clearRegs()
ENDIF ; ENDIF ; ENDIF ; ENDIF
va := va.next
ENDWHILE
ENDPROC
PROC sortRegAlloc(buf:PTR TO LONG, count)
DEF exit, va:PTR TO var, va2:PTR TO var, a
REPEAT
exit := TRUE
FOR a := 1 TO count-1
va := buf[a]
va2 := buf[a-1]
IF va2.usage < va.usage
buf[a] := buf[a-1]
buf[a-1] := va
exit := FALSE
ENDIF
ENDFOR
UNTIL exit
ENDPROC
PROC collectRegVars(buf:PTR TO LONG, firstvar, count, op, min)
DEF va:PTR TO var
va := firstvar
WHILE va
IF va.usage >= min
IF va.o <> op
IF va.trok THEN buf[count++] := va
ENDIF
ENDIF
va := va.next
ENDWHILE
ENDPROC count
-> v43: moved arg/local/frame init into DEF_PROC
PROC doProc(n:PTR TO item) OF codegen
DEF proc:PTR TO proc, va:REG PTR TO var, a
DEF o, d, buf:PTR TO item
DEF t1, t2
DEF count=0, fcount=0, exit, t
DEF returnssaved=FALSE, va2:PTR TO var
DEF allocregsbuf[256]:ARRAY OF LONG
DEF allocfregsbuf[256]:ARRAY OF LONG
proc := n.info -> !!
n++
#ifdef DBG_CODEGEN
DEBUGF('doproc() "\s"\n', proc.name)
#endif
/* auto reg alloc */
IF proc.handle = FALSE
IF g_numregalloc
count := collectRegVars(allocregsbuf, proc.args32, count, DREG, IF g_numregalloc < 0 THEN MINALLOCUSAGE ELSE 0)
count := collectRegVars(allocregsbuf, proc.locals32, count, DREG, IF g_numregalloc < 0 THEN MINALLOCUSAGE ELSE 0)
sortRegAlloc(allocregsbuf, count)
#ifdef DBG_CODEGEN
DEBUGF('doproc() "\s" found \d vars suitable for regalloc\n', proc.name, count)
#endif
ENDIF
IF g_numfregalloc
->IF proc.maxcalldepth = NIL -> no chance of exception happening ?
fcount := collectRegVars(allocfregsbuf, proc.args64, fcount, FREG, IF g_numfregalloc < 0 THEN MINALLOCUSAGE ELSE 0)
fcount := collectRegVars(allocfregsbuf, proc.locals64, fcount, FREG, IF g_numfregalloc < 0 THEN MINALLOCUSAGE ELSE 0)
->ENDIF
sortRegAlloc(allocfregsbuf, fcount)
#ifdef DBG_CODEGEN
DEBUGF('doproc() "\s" found \d vars suitable for fregalloc\n', proc.name, fcount)
#endif
ENDIF
ELSE -> v58
IF proc.except = FALSE THEN reportErr('HANDLE without EXCEPT')
ENDIF
IF g_linedebug THEN def_line(g_linenum)
g_currentproc := proc
#ifdef DBG_CODEGEN
DEBUGF('doproc() "\s" alloc and init locals\n', proc.name)
#endif
self.prochead(allocregsbuf, count, allocfregsbuf, fcount) -> v37, v45, 46, 48
->IF g_symbolhunk
-> IF proc.object
-> addMethDbgSym(proc.object.name, proc.name, proc.offset)
-> ENDIF
->ENDIF
g_stacksize := g_stacksize + proc.framesize -> v58
-> v50
self.resetObtainStart()
n := self.compileCode(n)
IF n.data <> KW_ENDPROC
IF n.data <> KW_IS THEN reportErr('unexpected end of source')
ENDIF
#ifdef DBG_CODEGEN
DEBUGF('doproc() "\s" final self=$\h, classinfo=$\h\n', proc.name, self, Long(self-4))
#endif
IF CtrlC() THEN reportErr('CtrlC')
IF g_linedebug THEN def_line(g_linenum)
self.endproc1()
-> now BEFORE returning values (v49) !!
-> means we CAN NOT used autoinit classes at ENDPROC
n := self.doReturn(n, FALSE)
self.endproc2()
->#ifdef DBG_CODEGEN
t := FALSE
FOR a := 0 TO REGUSETABSIZE-1
IF (g_regusetab[a].obtains) OR (g_regusetab[a].write)
DEBUGF(' !!! ENDPROC \s regusetab[\d] = \d,\d\n', proc.name, a, g_regusetab[a].obtains, g_regusetab[a].write)
t := TRUE
ENDIF
ENDFOR
IF t THEN reportIErr('REGUSETAB is inconsistent')
#ifdef DBG_CODEGEN
DEBUGF('doproc() "\s" DONE\n', proc.name)
#endif
-> v50
IF proc.framesize > (32*1024) THEN
reportErr('local stack usage exceeds 32k for procedure', proc.name)
g_currentproc := NIL
ENDPROC n
-> v48, now returns "end-statement" bool in second return value.
-> NIL = ENDIF,ENDFOR,ENDSELECT, etc..., <> NIL = n.data
PROC compileCode(n:PTR TO item, oneliner=FALSE) OF codegen
DEF t, as, as2, hln:PTR TO hln, var:PTR TO var, asm:PTR TO asm
DEF lab:PTR TO codelab, reg:PTR TO reg, e:PTR TO entry, a
DEF dh, dl -> v50
#ifdef DBG_CODEGEN
DEBUGF('compileCode($\h): line=\d \n', n, g_linenum)
#endif
IF FreeStack() < 4000 THEN Raise("STCK") -> 2.0
WHILE (t := n.data)
as := NIL
SELECT 256 OF t
CASE IT_SUBSTATEMENT
self.compileCode(n.info)
n++
CASE 10
g_linenum := n.info
IF g_linedebug THEN def_line(g_linenum)
n++
IF oneliner THEN RETURN n--, n.data
CASE IT_PROC
n := self.doProc(n)
CASE KW_EXCEPT
n++
IF n.data = KW_DO
n++
self.exceptblock(TRUE)
ELSE
self.exceptblock(FALSE)
ENDIF
CASE IT_LABEL
def_label(n.info.ident)
n++
CASE IT_ASM ; n := self.doAsm(n)
CASE KW_FOR ; n := self.doFor(n)
CASE KW_WHILE, KW_WHILEN ; n := self.doWhile(n)
CASE KW_LOOP ; n := self.doLoop(n)
CASE KW_REPEAT ; n := self.doRepeat(n)
CASE KW_SELECT
IF n[2].data = KW_OF
n := self.doSelectOf(n)
ELSE
n := self.doSelect(n)
ENDIF
CASE KW_IF
IF n[2].data = KW_THEN THEN n := self.doIfThenStat(n) ELSE n := self.doIfEndif(n)
CASE KW_RETURN ; n := self.doReturn(n,TRUE)
CASE KW_JUMP
n++
lab := n.info.ident
IF lab = NIL THEN reportErr('unknown dentifier', n.info.name)
IF lab.identID <> IDENT_LABEL THEN reportErr('label expected', n.info.name)
lab.referenced := 1
inst_golab(lab)
n++
CASE KW_INC
n++ ; var := n.info ; inst_incvar(var, 1) ; n++
CASE KW_DEC
n++ ; var := n.info ; inst_decvar(var, 1) ; n++
CASE IT_PFUNC ; n := self.doProcfunc(n,NIL)
CASE IT_IFUNC ; n := self.doIntfunc(n)
CASE IT_LFUNC ; n := self.doLibfunc(n)
CASE IT_VFUNC ; n := self.doVarfunc(n)
CASE IT_MASSIGN ; n := self.doMAssign(n) -> v49
CASE IT_VAREXP ; n := self.doVarstuff(n, TRUE, NIL)
CASE IT_UNIEXP ; n := self.doListUni(n)
CASE KW_INCBIN ; n := self.doIncbin(n)
CASE KW_CHAR -> also BYTE
putAlign(2)
n++
WHILE (t := n.data) <> 10
SELECT t
CASE IT_VALUE
IF n.num THEN reportErr('illegal value for CHAR/BYTE')
PutChar(g_codeptr, n.info)
g_codeptr := g_codeptr + 1
CASE IT_STRING
t := n.info
a := EstrLen(t) + 1
WHILE a-- DO PutChar(g_codeptr, t[]++) BUT g_codeptr := g_codeptr + 1
ENDSELECT
n++
ENDWHILE
putAlign(2) -> important for CHAR/BYTE
clearRegs()
CASE KW_INT -> also WORD
putAlign(2)
n++
WHILE n.data = IT_VALUE
IF n.num THEN reportErr('illegal value for INT/WORD')
PutInt(g_codeptr, n.info)
g_codeptr := g_codeptr + 2
n++
ENDWHILE
IF n.data <> 10 THEN reportErr('integer value expected')
clearRegs()
CASE KW_LONG -> also PTR
putAlign(2)
n++
WHILE (t := n.data) <> 10
SELECT t
CASE IT_LABEL
lab := n.info.ident
IF lab = NIL THEN reportErr('unknown inline label', n.info.name)
IF lab.identID <> IDENT_LABEL
reportErr('wrong type of label', n.info.name)
ENDIF
lab.referenced := 1
putReloc(lab)
CASE IT_VALUE
Put32(n.info)
CASE IT_STRING
lab := addRelStr(n.info)
putReloc(lab)
ENDSELECT
n++
ENDWHILE
clearRegs()
CASE KW_FLOAT -> 1.5.1
putAlign(4)
n++
WHILE n.data = IT_VALUE
Put32(n.info)
n++
ENDWHILE
clearRegs()
CASE KW_DOUBLE -> v50
putAlign(4)
n++
WHILE n.data = IT_VALUE
dh, dl := singToDoub(n.info)
PutLong(g_codeptr, dh)
PutLong(g_codeptr + 4, dl)
g_codeptr := g_codeptr + 8
n++
ENDWHILE
IF n.data <> 10 THEN reportErr('float value expected')
clearRegs()
CASE KW_WIDE -> v56
putAlign(4)
n++
WHILE n.data = IT_VALUE
PutLong(g_codeptr + 4, n.info)
PutLong(g_codeptr, IF n.info AND $80000000 THEN $FFFFFFFF ELSE NIL)
g_codeptr := g_codeptr + 8
n++
ENDWHILE
IF n.data <> 10 THEN reportErr('constant value expected')
clearRegs()
CASE KW_ELSE, KW_ELSEIF, KW_ENDIF, KW_CASE, KW_DEFAULT, KW_ENDSELECT,
KW_ENDFOR, KW_ENDWHILE, KW_ENDLOOP, KW_UNTIL, KW_ENDPROC, KW_IS
RETURN n, NIL
CASE KW_EXIT
RETURN n, n.data
CASE KW_BUT ; n++-> just ignore
CASE KW_VOID -> V47
n++
n := self.doExpression(n)
CASE KW_NOP
n++
CASE KW_STATIC -> 1.9.0
n := doStatic(n[1])
DEFAULT
#ifdef DBG_CODEGEN
DEBUGF('n.data=\d\n', n.data)
#endif
reportIErr(' statement does not compute')
ENDSELECT
ENDWHILE
#ifdef DBG_CODEGEN
DEBUGF('compileCode() DONE\n')
#endif
ENDPROC n, n.data
-> 1.9.0
PROC doStatic(n:PTR TO item)
DEF lab:PTR TO statlab, hln:PTR TO hln, ofs
WHILE n.data = IT_LABEL
hln := n.info
lab := hln.ident
n++
IF n.data = IT_STRING
n, ofs := makeStaticString(n)
lab.offset := ofs
ELSE
n, ofs := makeStaticList(n)
lab.offset := ofs
ENDIF
ENDWHILE
ENDPROC n
PROC makeStaticString(n:PTR TO item)
DEF ofs, t
putAlign(4)
ofs := currentOffset()
->WHILE TRUE
CopyMem(n.info, g_codeptr, t := EstrLen(n.info))
g_codeptr := g_codeptr + t
n++
-> EXIT n.data <> "+"
-> n++
->ENDWHILE
put8(NIL)
putAlign(4)
ENDPROC n, ofs
PROC makeStaticList(n:PTR TO item)
->DEF flags, esize, len, object:PTR TO object
->DEF start, size
DEF offset, ptr:PTR TO LONG
DEF sl:PTR TO it_statlist
sl := n.info
->flags := n.num
->len := n.info
->esize := n.info2 AND $FF
->object := IF n.info2 > 1000 THEN n.info2 ELSE NIL
-> do some alignment
IF sl.object
putAlign(sl.object.alignment)
ELSE
putAlign(sl.esize)
ENDIF
-> list len ?
IF sl.cplx
Put32(sl.len)
ENDIF
-> label of data at this offset
offset := currentOffset()
ptr := g_codeptr
-> compute size needed
->size := IF object = NIL THEN Mul(esize, len) ELSE (object.sizeof *
-> (Div(len, object.nrofmembers) +
->
-> IF Mod(len, object.nrofmembers) THEN 1 ELSE 0))
-> alloc space
g_codeptr := g_codeptr + sl.sizeof
n := evalStaticList(n, ptr, offset)
ENDPROC n, offset
PROC evalStaticList(n:PTR TO item, ptr:PTR TO LONG, oldoffset)
->DEF flags, esize, len, object:PTR TO object
DEF memb:PTR TO member
DEF start, ofs, size
DEF offset, a, dh, dl, t, hln:PTR TO hln
DEF lab:PTR TO codelab, tstr[50]:STRING
DEF sl:PTR TO it_statlist, len, esize
->flags := n.num
->len := n.info
->esize := n.info2 AND $FF
->object := IF n.info2 > 1000 THEN n.info2 ELSE NIL
sl := n.info
len := sl.len
esize := sl.esize
n++
start := ptr
-> eval and put data
IF esize = 4
WHILE len
t := n.data
IF t = IT_VALUE
ptr[]++ := n.info
n++
ELSEIF t = IT_STRING
n, ofs := makeStaticString(n)
addReloc(oldoffset + ptr - start)
ptr[]++ := ofs
ELSEIF t = IT_LABEL
hln := n.info
lab := hln.ident
IF lab = NIL THEN reportErr('unknown label', hln.name)
lab.referenced := 1
IF lab.offset
addReloc(oldoffset + ptr - start)
ptr[]++ := lab.offset
ELSE
lab.labrefs := NEW [lab.labrefs, oldoffset + ptr - start, REFADR]:LONG
ptr++
ENDIF
n++
ELSEIF t = "["
n, ofs := makeStaticList(n)
addReloc(oldoffset + ptr - start)
ptr[]++ := ofs
ELSE
reportIErr('evalStaticList ', StringF(tstr, 't=\d', t))
ENDIF
len--
ENDWHILE
ELSEIF sl.object <> NIL
offset := 0
WHILE len > 0
FOR a := 0 TO sl.object.nrofmembers-1
EXIT len = 0
memb := sl.object.membertable[a]
esize := memb.size
t := n.data
IF esize = 0 -> we support embedded objects !!
IF t <> "[" THEN reportErr('"[" expected for embedded array')
n, size := evalStaticList(n, ptr + memb.offset + offset, oldoffset + memb.offset + offset)
IF Mul(memb.numes, IF memb.esize = 255 THEN memb.object.sizeof ELSE memb.esize) < size
reportErr('immediate array bigger than member', memb.name)
ENDIF
ELSEIF t = IT_VALUE
SELECT esize
CASE 8 -> 1.8.2, was missing!
IF memb.flags AND MEMBF_FLOAT
dh, dl := singToDoub(n.info)
ELSE -> 2.3, was missing
dh := IF n.info AND $80000000 THEN -1 ELSE 0
dl := n.info
ENDIF
PutLong(ptr + memb.offset + offset, dh)
PutLong(ptr + memb.offset + offset + 4, dl)
CASE 4
PutLong(ptr + memb.offset + offset, n.info)
CASE 2
PutInt(ptr + memb.offset + offset, n.info)
CASE 1
PutChar(ptr + memb.offset + offset, n.info)
DEFAULT
reportIErr('evalStaticList object IT_VALUE esize ?')
ENDSELECT