-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z80.CodeGen.pas
1288 lines (1144 loc) · 36 KB
/
Z80.CodeGen.pas
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
unit Z80.CodeGen;
interface
uses Classes, Scopes, Globals;
procedure InitialiseCodeGen(PlatformFile, QuicheLibrary: String);
//BlockType can be used to specify whether the root/global block should
//use stack vars or static vars.
//For functions (Scope.Func <> nil) BlockType MUST be btDefault
function CodeGen(Scope: PScope;BlockType: TBlockType): Boolean;
procedure SaveAssemblyFile(FileName: String);
var
CodeGenErrorString: String;
CurrErrorCount: Integer; //In current routine
TotalErrorCount: Integer; //In current build
//Option for testing
//If True every primitive used will be logged
var LogPrimitives: Boolean;
PrimitiveLog: TStringList;
function UsesPrimitive(const Name: String): Boolean;
implementation
uses Fragments, ILData, SysUtils, Variables, ParserBase, QTypes, Operators,
PrimitivesEx, Compiler, Functions,
Z80.CPU, Z80.Optimise, Z80.CPUState;
var
CodeGenScope: PScope;
AsmCodeFull: TStringList;
AsmCodeScope: TStringList;
AsmDataFull: TStringList;
AsmDataScope: TStringList;
procedure DataGen;
var I: Integer;
V: PVariable;
S: String;
C: Integer;
begin
for I := 0 to VarGetCount-1 do
begin
V := VarIndexToData(I);
if V.Storage = vsStatic then
begin
S := V.GetAsmName + ': ';
case GetTypeSize(V.VarType) of
1: S := S + 'db 0';
2: S := S + ' dw 0';
else
S := 'db ';
for C := 1 to GetTypeSize(V.VarType) do
begin
if C <> 1 then
S := S + ',';
S := S + '0';
end;
end;
AsmDataScope.Append(S);
AsmDataFull.Append(S);
end;
end;
end;
procedure Line(S: String);
begin
AsmCodeFull.Add(S);
if Assigned(AsmCodeScope) then
AsmCodeScope.Add(S);
end;
procedure Lines(S: String);
begin
Line(S);
end;
procedure SaveAssemblyFile(FileName: String);
begin
AsmCodeFull.Append(AsmDataFull.Text);
AsmCodeFull.Add('__quiche_end:');
AsmCodeFull.SaveToFile(Filename);
end;
var CurrProcName: String;
var CurrBlockID: Integer;
CurrSourceLineNo: Integer;
LabelIndex: Integer; //Used to generate unique labels. Cleared at the start of each routine
procedure Error(Msg: String);
begin
inc(CurrErrorCount);
inc(TotalErrorCount);
Line('ERROR: ' + Msg + ' in ' + CurrProcName);
end;
function GetUniqueLabel: String;
begin
Result := '.x'+IntToStr(LabelIndex);
inc(LabelIndex);
end;
//Write an entire instruction line
procedure Instr(S: String);
begin
Line(' '+S);
end;
procedure Opcode(Op,P1,P2: String);
var S: String;
begin
S := ' ' + Op;
if P1 <> '' then
begin
S := S + ' ' + P1;
if P2 <> '' then
S := S + ',' + P2;
end;
Line(S);
end;
procedure GenLabel(Name: String);
begin
Line(Name + ':');
end;
procedure GenLibraryProc(ProcName: String;ILItem: PILItem);
var Code: String;
begin
if ProcName.Chars[0] = ':' then
Instr('call ' + ProcName.SubString(1) + ' ;Call')
else
begin
Code := Fragments.FragmentSub(ProcName, ILItem, CodeGenScope);
if Code = '' then
raise Exception.Create('Validation library code not found: ' + ProcName);
Line(' ;Fragment: ' + ProcName);
Lines(Code);
end;
end;
procedure GenLibraryParamProc(ProcName: String;const Param: TILParam;const Prefix: String);
var Code: String;
begin
{ if ProcName.Chars[0] = ':' then
Instr('call ' + ProcName.SubString(1) + ' ;Call')
else
} begin
Code := Fragments.FragmentParamSub(ProcName, Param, Prefix);
if Code = '' then
raise Exception.Create('Validation library code not found: ' + ProcName);
Line(' ;Fragment: ' + ProcName);
Lines(Code);
end;
end;
procedure GenCode(ProcName: String;ILItem: PILItem);
var
Prim: PPrimitiveNG;
Proc: TCodeGenProc;
begin
if ProcName = 'empty' then
EXIT;
if ProcName = '' then
EXIT;
if ProcName.Chars[0] = ':' then
GenLibraryProc(ProcName, ILItem)
else
begin
Prim := PrimFindByProcNameNG(ProcName);
if Assigned(Prim) then
begin
Line(' ;Prim: ' + ProcName);
Prim.Proc(ILItem);
end
else
begin
Proc := FindCodeGenProc(ProcName);
if Assigned(Proc) then
Proc(ILItem)
else
GenLibraryProc(ProcName, ILItem);
end;
end;
end;
//Sign extend register RIn to ROut
//Corrupts A and Flags
procedure GenSignExtend(RIn, ROut: Char);
begin
if RIn <> 'a' then
Instr('ld a,'+RIn); //Move value to A
Instr('rla'); //Move sign bit to Carry
Instr('sbc a,a'); //If carry we get -1, otherwise 0
if ROut <> 'a' then
Instr('ld '+ROut+',a'); //Move result to register
end;
//Branching
//Generates an unconditional jump to the given Block.
//If the block immediately follows the current code location no code is generated
//(i.e. fall-through)
procedure GenUncondJump(BlockID: Integer);
begin
if CurrBlockID <> BlockID - 1 then
Opcode('jp', CurrProcName + IntToStr(BlockID),'');
end;
procedure GenUncondBranch(ILItem: PILItem);
begin
Assert(ILItem.GetBranchBlockiD <> -1);
GenUncondJump(ILItem.GetBranchBlockID);
end;
//Generates a conditional jump, using Zero or Carry flag, to the given block
//If ZeroFlag is true, the jump uses the Zero flag, otherwise the Carry flag
//If Reverse is True the condition is inverted (i.e. Zero becomes Not Zero,
//Carry Set becomse Carry Clear).
procedure GenCondJump(ILItem: PILItem;Reverse: Boolean;BlockID: Integer);
var F: String;
begin
// Reverse := Reverse xor ILItem.BranchInvert;
Assert(ILItem.Dest.Kind = pkCondBranch);
case ILItem.Dest.Reg of
rA:
begin
// if ILItem.OpType <> rtBoolean then
GenLibraryProc('atozf', ILItem);
if Reverse then
F := 'z'
else
F := 'nz';
end;
rZF, rZFA:
if Reverse then
F := 'nz'
else
F := 'z';
rNZF, rNZFA:
if Reverse then
F := 'z'
else
F := 'nz';
rCF:
if Reverse then
F := 'nc'
else
F := 'c';
rNCF:
if Reverse then
F := 'c'
else
F := 'nc';
else
raise Exception.Create('TODO Flags');
end;
Opcode('jp', F, CurrProcName + IntToStr(BlockID));
end;
//Generate code to move FromReg to ToReg
//Currently only allows 'main' registers (ABCDEHL)
procedure GenRegMove(FromReg, ToReg: TCPUReg;Signed: Boolean);
begin
Assert(FromReg <> ToReg);
if FromReg in [rA, rB, rC, rD, rE, rH, rL] then
begin
if ToReg in [rA, rB, rC, rD, rE, rH, rL] then
//8-bit to 8-bit
Opcode('ld',CPUReg8ToChar[ToReg], CPUReg8ToChar[FromReg])
else if ToReg in [rBC, rHL, rDE] then
begin //8-bit to 16-bit
if FromReg <> CPURegPairToLow[ToReg] then
Opcode('ld',CPURegLowToChar[ToReg], CPUReg8ToChar[FromReg]);
if Signed then //Sign extend (if needed)
if FromReg = rA then
GenSignExtend(CPUReg8ToChar[FromReg], CPURegHighToChar[ToReg])
else
GenSignExtend(CPURegLowToChar[ToReg], CPURegHighToChar[ToReg])
else
Opcode('ld',CPURegHighToChar[ToReg], '$00');
end
else
Assert(False);
end
else if (FromReg in [rHL, rDE]) and (ToReg in [rHL, rDE]) then
begin
Assert(FromReg <> ToReg);
Opcode('ex','hl','de');
end
else if FromReg in [rHL, rDE, rBC] then
begin
Assert(ToReg in [rHL, rDE, rBC]);
Opcode('ld',CPURegLowToChar[ToReg],CPURegLowToChar[FromReg]);
Opcode('ld',CPURegHighToChar[ToReg],CPURegHighToChar[FromReg]);
end;
end;
//Generates the code to convert a boolean value from various sources into a boolean
//value in A
procedure GenToBoolean(Reg: TCPUReg;const Param: TILParam);
begin
case Reg of
rA, rAF: ;
rZF: GenLibraryParamProc('zftoboolean', Param, 'd');
rZFA: GenLibraryParamProc('notatoboolean', Param, 'd');
rNZF: GenLibraryParamProc('nzftoboolean', Param, 'd');
rNZFA: GenLibraryParamProc('atoboolean', Param, 'd');
rCPLA: GenLibraryParamProc('cpla', Param, 'd');
rCF: GenLibraryParamProc('cftoboolean', Param, 'd');
rNCF: GenLibraryParamProc('ncftoboolean', Param, 'd');
else
Assert(False);
end;
end;
//Code generators
//====================================
procedure ProcError(ILItem: PILItem);
begin
Error('No operation specified or illegal operation')
end;
procedure ProcEmpty(ILItem: PILItem);
begin
//Do nothing
end;
//===================================Validation
//aka overflow checking
//Raise an overflow error if bit 7 of the DestAlloc register is set
//Preserves all registers. Corrupts flags
procedure ProcDestB7SetOverflow(ILItem: PILItem);
var Reg: Char;
begin
Reg := CPUReg8ToChar[ILItem.Dest.Reg];
if Reg = 'a' then
begin //A reg
Instr('and a');
Instr('jp m,raise_overflow');
end
else
begin
OpCode('bit','7',Reg);
Instr('jp nz,raise_overflow');
end;
end;
//Raise an overflow error if bit 15 of the DestAlloc register is set
//Preserves all registers. Corrupts flags
procedure ProcDestB15SetOverflow(ILItem: PILItem);
var Reg: Char;
begin
Reg := CPURegHighToChar[ILItem.Dest.Reg];
if Reg = 'a' then
begin //A reg
Instr('and a');
Instr('jp m,raise_overflow');
end
else
begin
OpCode('bit','7',Reg);
Instr('jp nz,raise_overflow');
end;
end;
//====================================Assigns
procedure ProcAssignAbsS16Imm8(ILItem: PILItem);
var Variable: PVariable;
begin
Assert(ILItem.Param1.Kind = pkImmediate);
Assert(ILItem.Param1.Imm.VarType in [vtInt8, vtByte]);
Assert(ILItem.Dest.Kind = pkVarDest);
Variable := ILItem.Dest.ToVariable;
Assert(Variable.Storage = vsStatic);
if (ILItem.Param1.Imm.VarType = vtInt8) and (ILItem.Param1.Imm.IntValue < 0) then
GenLibraryProc('assign_abs16_imm8_neg', ILItem)
else
GenLibraryProc('assign_abs16_imm8', ILItem);
end;
//8 bit immediate value, extended to 16 bit
procedure ProcAssignRelS16Imm8(ILItem: PILItem);
var Variable: PVariable;
begin
Assert(ILItem.Param1.Kind = pkImmediate);
Assert(ILItem.Param1.Imm.VarType = vtInt8);
Assert(ILItem.Dest.Kind = pkVarDest);
Variable := ILItem.Dest.ToVariable;
Assert(Variable.Storage = vsStack);
if ILItem.Param1.Imm.IntValue < 0 then
GenLibraryProc('assign_rel16_imm8_neg', ILItem)
else
GenLibraryProc('assign_rel16_imm8', ILItem);
end;
//=====================================Maths
procedure ProcDec8Reg(ILItem: PILItem);
var Count: Integer;
I: Integer;
begin
if ILItem.Param2.Kind = pkNone then
Count := 1
else
Count := ILItem.Param2.Imm.ToInteger;
for I := 1 to abs(Count) do
if Count > 0 then
GenLibraryProc('dec8_reg', ILItem)
else
GenLibraryProc('inc8_reg', ILItem);
end;
procedure ProcDec16Reg(ILItem: PILItem);
var Count: Integer;
I: Integer;
begin
if ILItem.Param2.Kind = pkNone then
Count := 1
else
Count := ILItem.Param2.Imm.ToInteger;
for I := 1 to abs(Count) do
if Count > 0 then
GenLibraryProc('dec16_reg', ILItem)
else
GenLibraryProc('inc16_reg', ILItem);
end;
procedure ProcInc8Reg(ILItem: PILItem);
var Count: Integer;
I: Integer;
begin
if ILItem.Param2.Kind = pkNone then
Count := 1
else
Count := ILItem.Param2.Imm.ToInteger;
for I := 1 to abs(Count) do
if Count > 0 then
GenLibraryProc('inc8_reg', ILItem)
else
GenLibraryProc('dec8_reg', ILItem);
end;
procedure ProcInc16Reg(ILItem: PILItem);
var Count: Integer;
I: Integer;
begin
if ILItem.Param2.Kind = pkNone then
Count := 1
else
Count := ILItem.Param2.Imm.ToInteger;
for I := 1 to abs(Count) do
if Count > 0 then
GenLibraryProc('inc16_reg', ILItem)
else
GenLibraryProc('dec16_reg', ILItem);
end;
procedure ProcCall(ILItem: PILItem);
var Code: String;
begin
Assert(ILItem.Func <> nil);
Code := ILItem.Func.GetCallInstruction;
Instr(Code);
end;
//Register allocation (temporary)
//----------------------------------------------------------
//Allocate which registers (if any) parameters need to be loaded (or moved) into
//before a primitive can be generated
procedure TEMPRegAllocNG(var ILItem: PILItem;const Prim: PPrimitiveNG);
var Reg: TCPUReg;
Regs: TCPURegSet;
begin
if ILItem.Param1.Reg = rNone then
begin
//Prim doesn't use this parameter?
if Prim.LRegs = [] then
ILItem.Param1.Reg := rNone
//Param is immediate and Prim can handle an immediate value?
else if (ILItem.Param1.Kind = pkImmediate) and (rImm in Prim.LRegs) then
ILItem.Param1.Reg := rImm
else //...otherwise assign a register for the parameter to be loaded into
begin
//Get allowable registers
if ILItem.Op = opMove then
begin
if ILItem.Dest.Kind = pkPushByte then
Regs := [rA]
else if ILItem.Dest.Kind = pkPush then
Regs := [rHL, rDE, rBC]
// Regs := [rA, rB, rD, rH]
else if GetTypeSize(ILItem.Param1.GetVarType) = 1 then
Regs := [rA, rB, rC, rD, rE, rH, rL]
else
Regs := [rHL, rDE, rBC]
end
else
Regs := Prim.LRegs;
//Find an available register
Reg := Pred(rA);
ILItem.Param1.Reg := rNone;
repeat
Reg := Succ(Reg);
if Reg in Regs then
ILItem.Param1.Reg := Reg;
until (ILItem.Param1.Reg <> rNone) or (Reg = high(TCPUReg));
if ILItem.Param1.Reg = rNone then
raise Exception.Create('TEMNRegAlloc couldn''t find suitable Param1 register');
end;
end;
if ILItem.Param2.Reg = rNone then
begin
if Prim.RRegs = [] then
ILItem.Param2.Reg := rNone
else if (ILItem.Param2.Kind = pkImmediate) and (rImm in Prim.RRegs) then
ILItem.Param2.Reg := rImm
else
begin
//Get allowable registers
//TODO: Needs to be more flexible!
Regs := Prim.RRegs - [ILItem.Param1.Reg];
Reg := Pred(rA);
ILItem.Param2.Reg := rNone;
repeat
Reg := Succ(Reg);
if Reg in Regs then
ILItem.Param2.Reg := Reg;
until (ILItem.Param2.Reg <> rNone) or (Reg = high(TCPUReg));
if ILItem.Param2.Reg = rNone then
raise Exception.Create('TEMNRegAlloc couldn''t find suitable Param2 register');
end;
end;
case ILItem.Op of
opBranch, opDataLoad: Reg := rA; //Dummy value - The rest of this code only operates if = rNone
else
case ILItem.Dest.Kind of
pkNone: Reg := rA; //Dummy
pkCondBranch, pkVarDest, pkPush, pkPushByte:
Reg := ILItem.Dest.Reg
else
Assert(False);
end;
end;
if Reg = rNone then
begin
if ILItem.Op = opFuncCall then
begin
if ILItem.Func.ResultCount > 0 then
if GetTypeSize(ILItem.Func.FindResult.VarType) = 1 then
Reg := rA
else
Reg := rHL
end
else //Use result data from Primitive
if Prim.ResultInLReg then
begin
if ILItem.Dest.Kind in [pkPushByte, pkPush] then
case ILItem.Param1.Reg of
rA: Reg := rAF;
rB: Reg := rBC;
rD: Reg := rDE;
rH: Reg := rHL;
else
Reg := ILItem.Param1.Reg;
end
else
Reg := ILItem.Param1.Reg;
end
else
Reg := Prim.ResultReg;
case ILItem.Dest.Kind of
pkCondBranch, pkVarDest, pkPush, pkPushByte:
ILItem.Dest.Reg := Reg
else
Assert(False); //Other options should have been filtered out above
end;
end;
end;
procedure LoadParamNG(const Param: TILParam;Prim: PPrimitiveNG);
var
Variable: PVariable;
V: PVariable;
Prefix: String;
LoadStr: String;
Suffix: String;
begin
Prefix := 'load_';
case Param.Kind of
pkNone: ; //No param to load
pkImmediate:
case Param.Reg of
rNone, rImm: ; //Nothing to do. Imm is handled by Primitive itself
rA..rL: GenLibraryParamProc(Prefix + 'r8_imm', Param, 'p');
rHL, rDE, rBC: GenLibraryParamProc(Prefix + 'r16_imm', Param, 'p');
else
Assert(False, 'Invalid Reg for load');
end;
pkVarSource:
begin
V := Param.ToVariable;
case V.Storage of
vsStatic: LoadStr := '_abs';
vsStack: LoadStr := '_rel';
end;
//Suffix only used for 8 bit loads
if V.Storage = vsStack then
Suffix := ''
else if Param.Reg = rA then
Suffix := '_a'
else
Suffix := '_via_a';
case Param.Reg of
rNone, rImm: ; //Nothing to do. Imm is handled by Primitive itself
rA..rL:
begin
if pfnLoadRPHigh in Prim.Flags then
GenLibraryParamProc(Prefix + 'r8' + LoadStr + 'high' + Suffix, Param, 'p')
else if pfnLoadRPLow in Prim.Flags then
GenLibraryParamProc(Prefix + 'r8' + LoadStr + 'low' + Suffix, Param, 'p')
else
GenLibraryParamProc(Prefix + 'r8' + LoadStr + Suffix, Param, 'p');
end;
rHL..rBC:
begin
Variable := Param.ToVariable;
case GetTypeSize(Variable.VarType) of
1:
begin
GenLibraryParamProc(Prefix + 'r16low' + LoadStr + 'low' + Suffix, Param, 'p');
if (Variable.VarType = vtInt8) and not (pfnLoadRPLow in Prim.Flags) then
GenSignExtend(CPURegLowToChar[Param.Reg], CPURegHighToChar[Param.Reg])
else
GenLibraryParamProc(Prefix + 'r16high_zero', Param, 'p');
end;
2:
begin
if pfnLoadRPLow in Prim.Flags then
begin
GenLibraryParamProc(Prefix + 'r16' + LoadStr + 'low' + Suffix, Param, 'p');
GenLibraryParamProc('load_r16high_zero', Param, 'p');
end
else
GenLibraryParamProc(Prefix + 'r16'+LoadStr, Param, 'p');
end;
else
raise Exception.Create('Invalid type size for parameter');
end;
end;
else
raise Exception.Create('Invalid Reg for load');
end;
end;
else
Assert(False, 'Invalid param kind for param load');
end;
end;
//Sub to LoadBeforePrim
//Loads the Param in the register specified in the Param whilst preserving the value
//in the register named in Reg. (Reg can either not be touched, or can be moved
//elsewhere (e.g. another register or the stack) and move back before the function
//returns.
procedure LoadPreservingNG(const Param: TILParam;Reg: TCPUReg;Prim: PPrimitiveNG);
var
V: PVariable;
PreserveIn: TCPUReg;
begin
PreserveIn := rNone;
//Do we need to move P1 to avoid it getting trashed during the load?
//Trashing only happens if we load an 8-bit from static address (which
//needs to go via A)
if (Reg = rA) and (Param.Kind = pkVarSource) then
begin
V := Param.Variable;
if (V.Storage = vsStatic) and (GetTypeSize(V.VarType) = 1) then
begin //Find a register to preserve A into (which is neither A or the Param.Reg)
PreserveIn := CPUStateAllocReg8([rA, Param.Reg]);
GenRegMove(Reg, PreserveIn, False);
end;
end;
LoadParamNG(Param, Prim);
if PreserveIn <> rNone then
GenRegMove(PreserveIn, Reg, False);
end;
procedure LoadWithMoveNG(const Param: TILParam; FromReg, ToReg: TCPUReg;
Prim: PPrimitiveNG);
var
MoveBefore: Boolean; //Do the move before or after the load?
begin
//Move before if we're loading into FromReg, or if we're using FromReg during the load
//(only A can be trashed by a load. It's easier to always move before if FromReg is A)
MoveBefore := (FromReg = Param.Reg) or (FromReg = rA);
if MoveBefore then
GenRegMove(FromReg, ToReg, False);
LoadParamNG(Param, Prim);
if not MoveBefore then
GenRegMove(FromReg, ToReg, False);
end;
procedure LoadBothNG(ILItem: PILItem; Prim: PPrimitiveNG);
begin
//Select a register loading order so the second load won't trash the first.
//A will get trashed by an 8-bit load into a register other than A, so
//swap load order just in case
if ILItem.Param1.Reg = rA then
begin
LoadParamNG(ILItem.Param2, Prim);
LoadParamNG(ILItem.Param1, Prim);
end
else
begin
LoadParamNG(ILItem.Param1, Prim);
LoadParamNG(ILItem.Param2, Prim);
end;
end;
//Loads parameters from memory* into registers as needed.
//Data is loaded from the locations specified by the ILItem parameters into the
//registers specified by ILItem.Param1Alloc and ILItem.Param2Alloc, if any registers
//are specified there.
//* - can also handle parameters which are already in registers
procedure LoadBeforePrimNG(ILItem: PILItem; Prim: PPrimitiveNG);
var Swap: Boolean;
P1Reg: TCPUReg; //If P1 is already in a register
P2Reg: TCPUReg; //If P2 is already in a register
P1Move: Boolean; //P1 is already in a register but not the required one
P2Move: Boolean; //P2 is already in a register but not the required one
begin
//TODO: opDataLoad (not on NG yet): We can have third param!
// ...and we could have multiple DataMoves
// if (ILItem.Op = opDataLoad) and (ILItem.Param3.Kind <> pkNone) then
// Assert(False, 'Can''t handle DataLoad with three parameters yet');
if ILItem.Param1.Kind = pkVarSource then
P1Reg := RegStateFindVariable(ILItem.Param1.Variable, ILItem.Param1.VarVersion)
else
P1Reg := rNone;
P1Move := P1Reg <> ILItem.Param1.Reg;
if ILItem.Param2.Kind = pkVarSource then
P2Reg := RegStateFindVariable(ILItem.Param2.Variable, ILItem.Param2.VarVersion)
else
P2Reg := rNone;
P2Move := P2Reg <> ILItem.Param2.Reg;
if P1Reg = rNone then
begin //Load P1 data
if P2Reg = rNone then
//Load P2 data
LoadBothNG(ILItem, Prim)
else if P2Move then
//P2 is in a Reg but needs moving
LoadWithMoveNG(ILItem.Param1, P2Reg, ILItem.Param2.Reg, Prim)
else //P2 is in the correct Reg
LoadPreservingNG(ILItem.Param1, P2Reg, Prim)
end
else if P1Move then
begin //P1 is in a Reg but needs moving
if P2Reg = rNone then
//Load P2 data
LoadWithMoveNG(ILItem.Param2, P1Reg, ILItem.Param1.Reg, Prim)
else if P2Move then
begin //P2 is in a Reg but needs moving
Assert(False);
end
else
begin //P2 is in the correct Reg
Assert(False);
end
end
else
begin //P1 is in the correct Reg
if P2Reg = rNone then
//Load P2 data
LoadPreservingNG(ILItem.Param2, P1Reg, Prim)
else if P2Move then
begin //P2 is in a Reg but needs moving
Assert(False);
end
else
begin //P2 is in the correct Reg
//Do nothing
end
end
end;
//If validation is enabled, applies such validation.
//This routine is called after the primitive has executed.
//The validation routine (if there is one) is specifiied in the 'Validate' column
//of the Primitives table (spreadsheet)
procedure ValidateAfterPrimNG(ILItem: PILItem;Prim: PPrimitiveNG);
begin
if cgOverflowCheck in ILItem.CodeGenFlags then
//Validation for the operation itself
GenCode(Prim.ValidateProcName, ILItem);
end;
const //Routine names to keep the table source code size reasonable
b7s = 'bit7_set_overflow'; //test if bit 7 set. If so, overflow
b15s = 'bit15_set_overflow'; //test if bit 15 is set. If so, overflow
h9neq = 'high9_neq_overflow'; //overflow unless all of the highest 9 bits are equal (ie. all set or all clear)
h9nz = 'high9_nz_overflow'; //raise an overflow error unless the highest 9 bits are zero
hbnz = 'high_nz_overflow'; //overflow if the high register is non-zero
//Specifies the routine to use to validate a conversion from the type given in the row
//to the type given in the column.
//'' (empty): no validation is necessary for this conversion
//If a more optimised routine is available this can be specified in the primitives
//table ('Special validations on type conversion'). If so that routine will be used
//in preference to this table.
const ValidationMatrix: array[vtInt8..vtPointer,vtInt8..vtPointer] of String =
// To:
//From Int8 Integer Byte Word Pointer
{Int8} (('', '', b7s, b7s, b7s),
{Integer} (h9neq, '', h9nz, b15s, b15s),
{Byte} (b7s, '', '', '', ''),
{Word} (h9nz, b15s, hbnz, '', ''),
{Pointer} (h9nz, b15s, hbnz, '', ''));
//Generate a branch after the
procedure CondBranchAfterPrimNG(ILItem: PILItem;Prim: PPrimitiveNG);
begin
//If True block is following block then generate conditional jump for False
if ILItem.Param3.TrueBlockID = CurrBlockID + 1 then
GenCondJump(ILItem, True, ILItem.Param3.FalseBlockID)
else
begin //Otherwise generate condition jump for True...
GenCondJump(ILItem, False, ILItem.Param3.TrueBlockID);
//...and False doesn't 'fall though' then an unconditional jump for it.
if ILItem.Param3.FalseBlockID <> CurrBlockID + 1 then
GenUncondJump(ILItem.Param3.FalseBlockID);
end;
end;
//Store a value into a variable. Param contains the data for the variable including the
//Reg which currently contains the data.
//FromType is the current type of the data. This routine will arrange for it to be
//extended (or shortened) as necessary. No validation is performed (that should have
//been done by the caller)
procedure StoreToVariable(const Param: TILParam;FromType: TVarType);
var
Reg: Char;
StoreStr: String;
Suffix: String;
begin
Assert(Param.Kind = pkVarDest);
case Param.Variable.Storage of
vsStatic: StoreStr := 'store_abs';
vsStack: StoreStr := 'store_rel';
end;
//Store the output to the appropriate destination
case Param.Reg of
rNone: ;
rA..rL: //We have data in an 8-bit register
begin
SetRegStateVariable(Param.Reg, Param.Variable, Param.VarVersion);
if Param.Variable.Storage = vsStack then
Suffix := ''
else if Param.Reg = rA then
Suffix := '_a'
else
Suffix := '_via_a';
//Are we storing to a 1 or two byte destination?
case GetTypeSize(Param.Variable.VarType) of
1:
case GetTypeSize(FromType) of
1: GenLibraryParamProc(StoreStr + '8_r8' + Suffix, Param, 'd');
//If destination is 16 bit, zero extend data
// 2: GenLibraryParamProc(StoreStr + '16_r8' + Suffix, ILItem);
else
raise Exception.Create('Invalid variable size in StoreAfterPrim');
end;
2:
//Do we need to sign extend?
if IsSignedType(FromType) then
begin //Sign extend
GenLibraryParamProc(StoreStr + '16low_r8' + Suffix, Param, 'd');
Reg := CPUReg8ToChar[Param.Reg];
GenSignExtend(Reg, 'a'); ///'a'!!!
GenLibraryParamProc(StoreStr + '16high_a', Param, 'd');
end
else //Zero extend
GenLibraryParamProc(StoreStr + '16_r8' + Suffix, Param, 'd');
else
raise Exception.Create('Invalid type size in StoreAfterPrim');
end;
end;
rHL..rBC:
begin
SetRegStateVariable(Param.Reg, Param.Variable, Param.VarVersion);
if Param.Variable.Storage = vsStack then
Suffix := ''
else if Param.Reg = rA then
Suffix := '_a'
else
Suffix := '_via_a';
//Are we storing to a 1 or two byte destination?
case GetTypeSize(Param.Variable.VarType) of
//When shortening, any validation should have been done above
1: GenLibraryParamProc(StoreStr + '8_r16low' + Suffix, Param, 'd');
2: GenLibraryParamProc(StoreStr + '16_r16', Param, 'd');
else
raise Exception.Create('Invalid type size in StoreAfterPrim');
end;
end;
rZF, rZFA, rNZF, rNZFA, rCPLA, rCF, rNCF:
begin //For assignments
GenToBoolean(Param.Reg, Param);
GenLibraryParamProc(StoreStr + '8_a', Param, 'd');
end;
else
raise Exception.Create('Illegal DestAlloc in AllocAfterPrim');
end;
end;
//Takes the result from the register specified in ILItem.DestAlloc and stores it
//into the location specified in ILItem.Dest
//If a type conversion is to take place then:
// * if validation is enabled, will generate code to ensure the value will fit into
//the destination type
// * generates code to handle the type conversion (if necessary)
procedure StoreAfterPrimNG(ILItem: PILItem;Prim: PPrimitiveNG);
var
DestType: TVarType;
ValProcName: String;
begin
case ILItem.Op of
opDataLoad: ;
else
case ILItem.Dest.Kind of
pkNone: EXIT;
pkVarDest:
begin
DestType := ILItem.Dest.Variable.VarType;
//Validation for type conversions, if needed. No conversion needed if operator has fixed result type
if IsNumericType(DestType) then
if (DestType <> vtUnknown) and (ILItem.ResultType <> DestType) then
if cgOverflowCheck in ILItem.CodeGenFlags then
begin
ValProcName := '';
//Do we have a special case validation routine for conversion to said type?
case ILItem.ResultType of
vtInt8: ValProcName := Prim.ValidateToS8;
vtByte: ValProcName := Prim.ValidateToU8;
vtInteger: ValProcName := Prim.ValidateToS16;
vtWord, vtPointer: ValProcName := Prim.ValidateToU16;
else
raise Exception.Create('Unhandled type in StoreAfterPrimNG');
end;