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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
|
%
% (c) The AQUA Project, Glasgow University, 1993-1995
%
\begin{code}
#include "HsVersions.h"
module AlphaGen (
alphaCodeGen,
-- and, for self-sufficiency
PprStyle, StixTree, CSeq
) where
IMPORT_Trace
import AbsCSyn ( AbstractC, MagicId(..), kindFromMagicId )
import AbsPrel ( PrimOp(..)
IF_ATTACK_PRAGMAS(COMMA tagOf_PrimOp)
IF_ATTACK_PRAGMAS(COMMA pprPrimOp)
)
import AsmRegAlloc ( runRegAllocate, extractMappedRegNos, mkReg,
Reg(..), RegLiveness(..), RegUsage(..), FutureLive(..),
MachineRegisters(..), MachineCode(..)
)
import CLabelInfo ( CLabel, isAsmTemp )
import AlphaCode {- everything -}
import MachDesc
import Maybes ( maybeToBool, Maybe(..) )
import OrdList -- ( mkEmptyList, mkUnitList, mkSeqList, mkParList, OrdList )
import Outputable
import PrimKind ( PrimKind(..), isFloatingKind )
import AlphaDesc
import Stix
import SplitUniq
import Unique
import Pretty
import Unpretty
import Util
type CodeBlock a = (OrdList a -> OrdList a)
\end{code}
%************************************************************************
%* *
\subsection[AlphaCodeGen]{Generating Alpha Code}
%* *
%************************************************************************
This is the top-level code-generation function for the Alpha.
\begin{code}
alphaCodeGen :: PprStyle -> [[StixTree]] -> SUniqSM Unpretty
alphaCodeGen sty trees =
mapSUs genAlphaCode trees `thenSUs` \ dynamicCodes ->
let
staticCodes = scheduleAlphaCode dynamicCodes
pretty = printLabeledCodes sty staticCodes
in
returnSUs pretty
\end{code}
This bit does the code scheduling. The scheduler must also deal with
register allocation of temporaries. Much parallelism can be exposed via
the OrdList, but more might occur, so further analysis might be needed.
\begin{code}
scheduleAlphaCode :: [AlphaCode] -> [AlphaInstr]
scheduleAlphaCode = concat . map (runRegAllocate freeAlphaRegs reservedRegs)
where
freeAlphaRegs :: AlphaRegs
freeAlphaRegs = mkMRegs (extractMappedRegNos freeRegs)
\end{code}
Registers passed up the tree. If the stix code forces the register
to live in a pre-decided machine register, it comes out as @Fixed@;
otherwise, it comes out as @Any@, and the parent can decide which
register to put it in.
\begin{code}
data Register
= Fixed Reg PrimKind (CodeBlock AlphaInstr)
| Any PrimKind (Reg -> (CodeBlock AlphaInstr))
registerCode :: Register -> Reg -> CodeBlock AlphaInstr
registerCode (Fixed _ _ code) reg = code
registerCode (Any _ code) reg = code reg
registerName :: Register -> Reg -> Reg
registerName (Fixed reg _ _) _ = reg
registerName (Any _ _) reg = reg
registerKind :: Register -> PrimKind
registerKind (Fixed _ pk _) = pk
registerKind (Any pk _) = pk
isFixed :: Register -> Bool
isFixed (Fixed _ _ _) = True
isFixed (Any _ _) = False
\end{code}
Memory addressing modes passed up the tree.
\begin{code}
data Amode = Amode Addr (CodeBlock AlphaInstr)
amodeAddr (Amode addr _) = addr
amodeCode (Amode _ code) = code
\end{code}
General things for putting together code sequences.
\begin{code}
asmVoid :: OrdList AlphaInstr
asmVoid = mkEmptyList
asmInstr :: AlphaInstr -> AlphaCode
asmInstr i = mkUnitList i
asmSeq :: [AlphaInstr] -> AlphaCode
asmSeq is = foldr (mkSeqList . asmInstr) asmVoid is
asmParThen :: [AlphaCode] -> CodeBlock AlphaInstr
asmParThen others code = mkSeqList (foldr mkParList mkEmptyList others) code
returnInstr :: AlphaInstr -> SUniqSM (CodeBlock AlphaInstr)
returnInstr instr = returnSUs (\xs -> mkSeqList (asmInstr instr) xs)
returnInstrs :: [AlphaInstr] -> SUniqSM (CodeBlock AlphaInstr)
returnInstrs instrs = returnSUs (\xs -> mkSeqList (asmSeq instrs) xs)
returnSeq :: (CodeBlock AlphaInstr) -> [AlphaInstr] -> SUniqSM (CodeBlock AlphaInstr)
returnSeq code instrs = returnSUs (\xs -> code (mkSeqList (asmSeq instrs) xs))
mkSeqInstr :: AlphaInstr -> (CodeBlock AlphaInstr)
mkSeqInstr instr code = mkSeqList (asmInstr instr) code
mkSeqInstrs :: [AlphaInstr] -> (CodeBlock AlphaInstr)
mkSeqInstrs instrs code = mkSeqList (asmSeq instrs) code
\end{code}
Top level alpha code generator for a chunk of stix code.
\begin{code}
genAlphaCode :: [StixTree] -> SUniqSM (AlphaCode)
genAlphaCode trees =
mapSUs getCode trees `thenSUs` \ blocks ->
returnSUs (foldr (.) id blocks asmVoid)
\end{code}
Code extractor for an entire stix tree---stix statement level.
\begin{code}
getCode
:: StixTree -- a stix statement
-> SUniqSM (CodeBlock AlphaInstr)
getCode (StSegment seg) = returnInstr (SEGMENT seg)
getCode (StAssign pk dst src)
| isFloatingKind pk = assignFltCode pk dst src
| otherwise = assignIntCode pk dst src
getCode (StLabel lab) = returnInstr (LABEL lab)
getCode (StFunBegin lab) = returnInstr (FUNBEGIN lab)
getCode (StFunEnd lab) = returnInstr (FUNEND lab)
getCode (StJump arg) = genJump arg
-- When falling through on the alpha, we still have to load pv with the
-- address of the next routine, so that it can load gp
getCode (StFallThrough lbl) = returnInstr (LDA pv (AddrImm (ImmCLbl lbl)))
getCode (StCondJump lbl arg) = genCondJump lbl arg
getCode (StData kind args) =
mapAndUnzipSUs getData args `thenSUs` \ (codes, imms) ->
returnSUs (\xs -> mkSeqList (asmInstr (DATA (kindToSize kind) imms))
(foldr1 (.) codes xs))
where
getData :: StixTree -> SUniqSM (CodeBlock AlphaInstr, Imm)
getData (StInt i) = returnSUs (id, ImmInteger i)
#if __GLASGOW_HASKELL__ >= 23
-- getData (StDouble d) = returnSUs (id, strImmLab (_showRational 30 d))
getData (StDouble d) = returnSUs (id, ImmLab (prettyToUn (ppRational d)))
#else
getData (StDouble d) = returnSUs (id, strImmLab (show d))
#endif
getData (StLitLbl s) = returnSUs (id, ImmLab s)
getData (StLitLit s) = returnSUs (id, strImmLab (cvtLitLit (_UNPK_ s)))
getData (StString s) =
getUniqLabelNCG `thenSUs` \ lbl ->
returnSUs (mkSeqInstrs [LABEL lbl, ASCII True (_UNPK_ s)], ImmCLbl lbl)
getData (StCLbl l) = returnSUs (id, ImmCLbl l)
getCode (StCall fn VoidKind args) = genCCall fn VoidKind args
getCode (StComment s) = returnInstr (COMMENT s)
\end{code}
Generate code to get a subtree into a register.
\begin{code}
getReg :: StixTree -> SUniqSM Register
getReg (StReg (StixMagicId stgreg)) =
case stgRegMap stgreg of
Just reg -> returnSUs (Fixed reg (kindFromMagicId stgreg) id)
-- cannae be Nothing
getReg (StReg (StixTemp u pk)) = returnSUs (Fixed (UnmappedReg u pk) pk id)
getReg (StDouble d) =
getUniqLabelNCG `thenSUs` \ lbl ->
getNewRegNCG PtrKind `thenSUs` \ tmp ->
let code dst = mkSeqInstrs [
SEGMENT DataSegment,
LABEL lbl,
#if __GLASGOW_HASKELL__ >= 23
-- DATA TF [strImmLab (_showRational 30 d)],
DATA TF [ImmLab (prettyToUn (ppRational d))],
#else
DATA TF [strImmLab (show d)],
#endif
SEGMENT TextSegment,
LDA tmp (AddrImm (ImmCLbl lbl)),
LD TF dst (AddrReg tmp)]
in
returnSUs (Any DoubleKind code)
getReg (StString s) =
getUniqLabelNCG `thenSUs` \ lbl ->
let code dst = mkSeqInstrs [
SEGMENT DataSegment,
LABEL lbl,
ASCII True (_UNPK_ s),
SEGMENT TextSegment,
LDA dst (AddrImm (ImmCLbl lbl))]
in
returnSUs (Any PtrKind code)
getReg (StLitLit s) | _HEAD_ s == '"' && last xs == '"' =
getUniqLabelNCG `thenSUs` \ lbl ->
let code dst = mkSeqInstrs [
SEGMENT DataSegment,
LABEL lbl,
ASCII False (init xs),
SEGMENT TextSegment,
LDA dst (AddrImm (ImmCLbl lbl))]
in
returnSUs (Any PtrKind code)
where
xs = _UNPK_ (_TAIL_ s)
getReg tree@(StIndex _ _ _) = getReg (mangleIndexTree tree)
getReg (StCall fn kind args) =
genCCall fn kind args `thenSUs` \ call ->
returnSUs (Fixed reg kind call)
where
reg = if isFloatingKind kind then f0 else v0
getReg (StPrim primop args) =
case primop of
CharGtOp -> case args of [x,y] -> trivialCode (CMP LT) [y,x]
CharGeOp -> case args of [x,y] -> trivialCode (CMP LE) [y,x]
CharEqOp -> trivialCode (CMP EQ) args
CharNeOp -> intNECode args
CharLtOp -> trivialCode (CMP LT) args
CharLeOp -> trivialCode (CMP LE) args
IntAddOp -> trivialCode (ADD Q False) args
IntSubOp -> trivialCode (SUB Q False) args
IntMulOp -> trivialCode (MUL Q False) args
IntQuotOp -> trivialCode (DIV Q False) args
IntDivOp -> call SLIT("stg_div") IntKind
IntRemOp -> trivialCode (REM Q False) args
IntNegOp -> trivialUCode (NEG Q False) args
IntAbsOp -> trivialUCode (ABS Q) args
AndOp -> trivialCode AND args
OrOp -> trivialCode OR args
NotOp -> trivialUCode NOT args
SllOp -> trivialCode SLL args
SraOp -> trivialCode SRA args
SrlOp -> trivialCode SRL args
ISllOp -> panic "AlphaGen:isll"
ISraOp -> panic "AlphaGen:isra"
ISrlOp -> panic "AlphaGen:isrl"
IntGtOp -> case args of [x,y] -> trivialCode (CMP LT) [y,x]
IntGeOp -> case args of [x,y] -> trivialCode (CMP LE) [y,x]
IntEqOp -> trivialCode (CMP EQ) args
IntNeOp -> intNECode args
IntLtOp -> trivialCode (CMP LT) args
IntLeOp -> trivialCode (CMP LE) args
WordGtOp -> case args of [x,y] -> trivialCode (CMP ULT) [y,x]
WordGeOp -> case args of [x,y] -> trivialCode (CMP ULE) [y,x]
WordEqOp -> trivialCode (CMP EQ) args
WordNeOp -> intNECode args
WordLtOp -> trivialCode (CMP ULT) args
WordLeOp -> trivialCode (CMP ULE) args
AddrGtOp -> case args of [x,y] -> trivialCode (CMP ULT) [y,x]
AddrGeOp -> case args of [x,y] -> trivialCode (CMP ULE) [y,x]
AddrEqOp -> trivialCode (CMP EQ) args
AddrNeOp -> intNECode args
AddrLtOp -> trivialCode (CMP ULT) args
AddrLeOp -> trivialCode (CMP ULE) args
FloatAddOp -> trivialFCode (FADD TF) args
FloatSubOp -> trivialFCode (FSUB TF) args
FloatMulOp -> trivialFCode (FMUL TF) args
FloatDivOp -> trivialFCode (FDIV TF) args
FloatNegOp -> trivialUFCode (FNEG TF) args
FloatGtOp -> cmpFCode (FCMP TF LE) EQ args
FloatGeOp -> cmpFCode (FCMP TF LT) EQ args
FloatEqOp -> cmpFCode (FCMP TF EQ) NE args
FloatNeOp -> cmpFCode (FCMP TF EQ) EQ args
FloatLtOp -> cmpFCode (FCMP TF LT) NE args
FloatLeOp -> cmpFCode (FCMP TF LE) NE args
FloatExpOp -> call SLIT("exp") DoubleKind
FloatLogOp -> call SLIT("log") DoubleKind
FloatSqrtOp -> call SLIT("sqrt") DoubleKind
FloatSinOp -> call SLIT("sin") DoubleKind
FloatCosOp -> call SLIT("cos") DoubleKind
FloatTanOp -> call SLIT("tan") DoubleKind
FloatAsinOp -> call SLIT("asin") DoubleKind
FloatAcosOp -> call SLIT("acos") DoubleKind
FloatAtanOp -> call SLIT("atan") DoubleKind
FloatSinhOp -> call SLIT("sinh") DoubleKind
FloatCoshOp -> call SLIT("cosh") DoubleKind
FloatTanhOp -> call SLIT("tanh") DoubleKind
FloatPowerOp -> call SLIT("pow") DoubleKind
DoubleAddOp -> trivialFCode (FADD TF) args
DoubleSubOp -> trivialFCode (FSUB TF) args
DoubleMulOp -> trivialFCode (FMUL TF) args
DoubleDivOp -> trivialFCode (FDIV TF) args
DoubleNegOp -> trivialUFCode (FNEG TF) args
DoubleGtOp -> cmpFCode (FCMP TF LE) EQ args
DoubleGeOp -> cmpFCode (FCMP TF LT) EQ args
DoubleEqOp -> cmpFCode (FCMP TF EQ) NE args
DoubleNeOp -> cmpFCode (FCMP TF EQ) EQ args
DoubleLtOp -> cmpFCode (FCMP TF LT) NE args
DoubleLeOp -> cmpFCode (FCMP TF LE) NE args
DoubleExpOp -> call SLIT("exp") DoubleKind
DoubleLogOp -> call SLIT("log") DoubleKind
DoubleSqrtOp -> call SLIT("sqrt") DoubleKind
DoubleSinOp -> call SLIT("sin") DoubleKind
DoubleCosOp -> call SLIT("cos") DoubleKind
DoubleTanOp -> call SLIT("tan") DoubleKind
DoubleAsinOp -> call SLIT("asin") DoubleKind
DoubleAcosOp -> call SLIT("acos") DoubleKind
DoubleAtanOp -> call SLIT("atan") DoubleKind
DoubleSinhOp -> call SLIT("sinh") DoubleKind
DoubleCoshOp -> call SLIT("cosh") DoubleKind
DoubleTanhOp -> call SLIT("tanh") DoubleKind
DoublePowerOp -> call SLIT("pow") DoubleKind
OrdOp -> coerceIntCode IntKind args
ChrOp -> chrCode args
Float2IntOp -> coerceFP2Int args
Int2FloatOp -> coerceInt2FP args
Double2IntOp -> coerceFP2Int args
Int2DoubleOp -> coerceInt2FP args
Double2FloatOp -> coerceFltCode args
Float2DoubleOp -> coerceFltCode args
where
call fn pk = getReg (StCall fn pk args)
getReg (StInd pk mem) =
getAmode mem `thenSUs` \ amode ->
let
code = amodeCode amode
src = amodeAddr amode
size = kindToSize pk
code__2 dst = code . mkSeqInstr (LD size dst src)
in
returnSUs (Any pk code__2)
getReg (StInt i)
| is8Bits i =
let
code dst = mkSeqInstr (OR zero (RIImm src) dst)
in
returnSUs (Any IntKind code)
| otherwise =
let
code dst = mkSeqInstr (LDI Q dst src)
in
returnSUs (Any IntKind code)
where
src = ImmInt (fromInteger i)
getReg leaf
| maybeToBool imm =
let
code dst = mkSeqInstr (LDA dst (AddrImm imm__2))
in
returnSUs (Any PtrKind code)
where
imm = maybeImm leaf
imm__2 = case imm of Just x -> x
\end{code}
Now, given a tree (the argument to an StInd) that references memory,
produce a suitable addressing mode.
\begin{code}
getAmode :: StixTree -> SUniqSM Amode
getAmode tree@(StIndex _ _ _) = getAmode (mangleIndexTree tree)
getAmode (StPrim IntSubOp [x, StInt i]) =
getNewRegNCG PtrKind `thenSUs` \ tmp ->
getReg x `thenSUs` \ register ->
let
code = registerCode register tmp
reg = registerName register tmp
off = ImmInt (-(fromInteger i))
in
returnSUs (Amode (AddrRegImm reg off) code)
getAmode (StPrim IntAddOp [x, StInt i]) =
getNewRegNCG PtrKind `thenSUs` \ tmp ->
getReg x `thenSUs` \ register ->
let
code = registerCode register tmp
reg = registerName register tmp
off = ImmInt (fromInteger i)
in
returnSUs (Amode (AddrRegImm reg off) code)
getAmode leaf
| maybeToBool imm =
returnSUs (Amode (AddrImm imm__2) id)
where
imm = maybeImm leaf
imm__2 = case imm of Just x -> x
getAmode other =
getNewRegNCG PtrKind `thenSUs` \ tmp ->
getReg other `thenSUs` \ register ->
let
code = registerCode register tmp
reg = registerName register tmp
in
returnSUs (Amode (AddrReg reg) code)
\end{code}
Try to get a value into a specific register (or registers) for a call.
The first 6 arguments go into the appropriate argument register
(separate registers for integer and floating point arguments, but used
in lock-step), and the remaining arguments are dumped to the stack,
beginning at 0(sp). Our first argument is a pair of the list of
remaining argument registers to be assigned for this call and the next
stack offset to use for overflowing arguments. This way, @getCallArg@
can be applied to all of a call's arguments using @mapAccumL@.
\begin{code}
getCallArg
:: ([(Reg,Reg)],Int) -- Argument registers and stack offset (accumulator)
-> StixTree -- Current argument
-> SUniqSM (([(Reg,Reg)],Int), CodeBlock AlphaInstr) -- Updated accumulator and code
-- We have to use up all of our argument registers first.
getCallArg ((iDst,fDst):dsts, offset) arg =
getReg arg `thenSUs` \ register ->
let
reg = if isFloatingKind pk then fDst else iDst
code = registerCode register reg
src = registerName register reg
pk = registerKind register
in
returnSUs (
if isFloatingKind pk then
((dsts, offset), if isFixed register then
code . mkSeqInstr (FMOV src fDst)
else code)
else
((dsts, offset), if isFixed register then
code . mkSeqInstr (OR src (RIReg src) iDst)
else code))
-- Once we have run out of argument registers, we move to the stack
getCallArg ([], offset) arg =
getReg arg `thenSUs` \ register ->
getNewRegNCG (registerKind register)
`thenSUs` \ tmp ->
let
code = registerCode register tmp
src = registerName register tmp
pk = registerKind register
sz = kindToSize pk
in
returnSUs (([], offset + 1), code . mkSeqInstr (ST sz src (spRel offset)))
\end{code}
Assignments are really at the heart of the whole code generation business.
Almost all top-level nodes of any real importance are assignments, which
correspond to loads, stores, or register transfers. If we're really lucky,
some of the register transfers will go away, because we can use the destination
register to complete the code generation for the right hand side. This only
fails when the right hand side is forced into a fixed register (e.g. the result
of a call).
\begin{code}
assignIntCode :: PrimKind -> StixTree -> StixTree -> SUniqSM (CodeBlock AlphaInstr)
assignIntCode pk (StInd _ dst) src =
getNewRegNCG IntKind `thenSUs` \ tmp ->
getAmode dst `thenSUs` \ amode ->
getReg src `thenSUs` \ register ->
let
code1 = amodeCode amode asmVoid
dst__2 = amodeAddr amode
code2 = registerCode register tmp asmVoid
src__2 = registerName register tmp
sz = kindToSize pk
code__2 = asmParThen [code1, code2] . mkSeqInstr (ST sz src__2 dst__2)
in
returnSUs code__2
assignIntCode pk dst src =
getReg dst `thenSUs` \ register1 ->
getReg src `thenSUs` \ register2 ->
let
dst__2 = registerName register1 zero
code = registerCode register2 dst__2
src__2 = registerName register2 dst__2
code__2 = if isFixed register2 then
code . mkSeqInstr (OR src__2 (RIReg src__2) dst__2)
else code
in
returnSUs code__2
assignFltCode :: PrimKind -> StixTree -> StixTree -> SUniqSM (CodeBlock AlphaInstr)
assignFltCode pk (StInd _ dst) src =
getNewRegNCG pk `thenSUs` \ tmp ->
getAmode dst `thenSUs` \ amode ->
getReg src `thenSUs` \ register ->
let
code1 = amodeCode amode asmVoid
dst__2 = amodeAddr amode
code2 = registerCode register tmp asmVoid
src__2 = registerName register tmp
sz = kindToSize pk
code__2 = asmParThen [code1, code2] . mkSeqInstr (ST sz src__2 dst__2)
in
returnSUs code__2
assignFltCode pk dst src =
getReg dst `thenSUs` \ register1 ->
getReg src `thenSUs` \ register2 ->
let
dst__2 = registerName register1 zero
code = registerCode register2 dst__2
src__2 = registerName register2 dst__2
code__2 = if isFixed register2 then
code . mkSeqInstr (FMOV src__2 dst__2)
else code
in
returnSUs code__2
\end{code}
Generating an unconditional branch. We accept two types of targets:
an immediate CLabel or a tree that gets evaluated into a register.
Any CLabels which are AsmTemporaries are assumed to be in the local
block of code, close enough for a branch instruction. Other CLabels
are assumed to be far away, so we use jmp.
\begin{code}
genJump
:: StixTree -- the branch target
-> SUniqSM (CodeBlock AlphaInstr)
genJump (StCLbl lbl)
| isAsmTemp lbl = returnInstr (BR target)
| otherwise = returnInstrs [LDA pv (AddrImm target), JMP zero (AddrReg pv) 0]
where
target = ImmCLbl lbl
genJump tree =
getReg tree `thenSUs` \ register ->
getNewRegNCG PtrKind `thenSUs` \ tmp ->
let
dst = registerName register pv
code = registerCode register pv
target = registerName register pv
in
if isFixed register then
returnSeq code [OR dst (RIReg dst) pv, JMP zero (AddrReg pv) 0]
else
returnSUs (code . mkSeqInstr (JMP zero (AddrReg pv) 0))
\end{code}
Conditional jumps are always to local labels, so we can use
branch instructions. We peek at the arguments to decide what kind
of comparison to do. For comparisons with 0, we're laughing, because
we can just do the desired conditional branch.
\begin{code}
genCondJump
:: CLabel -- the branch target
-> StixTree -- the condition on which to branch
-> SUniqSM (CodeBlock AlphaInstr)
genCondJump lbl (StPrim op [x, StInt 0]) =
getReg x `thenSUs` \ register ->
getNewRegNCG (registerKind register)
`thenSUs` \ tmp ->
let
code = registerCode register tmp
value = registerName register tmp
pk = registerKind register
target = ImmCLbl lbl
in
returnSeq code [BI (cmpOp op) value target]
where
cmpOp CharGtOp = GT
cmpOp CharGeOp = GE
cmpOp CharEqOp = EQ
cmpOp CharNeOp = NE
cmpOp CharLtOp = LT
cmpOp CharLeOp = LE
cmpOp IntGtOp = GT
cmpOp IntGeOp = GE
cmpOp IntEqOp = EQ
cmpOp IntNeOp = NE
cmpOp IntLtOp = LT
cmpOp IntLeOp = LE
cmpOp WordGtOp = NE
cmpOp WordGeOp = ALWAYS
cmpOp WordEqOp = EQ
cmpOp WordNeOp = NE
cmpOp WordLtOp = NEVER
cmpOp WordLeOp = EQ
cmpOp AddrGtOp = NE
cmpOp AddrGeOp = ALWAYS
cmpOp AddrEqOp = EQ
cmpOp AddrNeOp = NE
cmpOp AddrLtOp = NEVER
cmpOp AddrLeOp = EQ
genCondJump lbl (StPrim op [x, StDouble 0.0]) =
getReg x `thenSUs` \ register ->
getNewRegNCG (registerKind register)
`thenSUs` \ tmp ->
let
code = registerCode register tmp
value = registerName register tmp
pk = registerKind register
target = ImmCLbl lbl
in
returnSUs (code . mkSeqInstr (BF (cmpOp op) value target))
where
cmpOp FloatGtOp = GT
cmpOp FloatGeOp = GE
cmpOp FloatEqOp = EQ
cmpOp FloatNeOp = NE
cmpOp FloatLtOp = LT
cmpOp FloatLeOp = LE
cmpOp DoubleGtOp = GT
cmpOp DoubleGeOp = GE
cmpOp DoubleEqOp = EQ
cmpOp DoubleNeOp = NE
cmpOp DoubleLtOp = LT
cmpOp DoubleLeOp = LE
genCondJump lbl (StPrim op args)
| fltCmpOp op =
trivialFCode instr args `thenSUs` \ register ->
getNewRegNCG DoubleKind `thenSUs` \ tmp ->
let
code = registerCode register tmp
result = registerName register tmp
target = ImmCLbl lbl
in
returnSUs (code . mkSeqInstr (BF cond result target))
where
fltCmpOp op = case op of
FloatGtOp -> True
FloatGeOp -> True
FloatEqOp -> True
FloatNeOp -> True
FloatLtOp -> True
FloatLeOp -> True
DoubleGtOp -> True
DoubleGeOp -> True
DoubleEqOp -> True
DoubleNeOp -> True
DoubleLtOp -> True
DoubleLeOp -> True
_ -> False
(instr, cond) = case op of
FloatGtOp -> (FCMP TF LE, EQ)
FloatGeOp -> (FCMP TF LT, EQ)
FloatEqOp -> (FCMP TF EQ, NE)
FloatNeOp -> (FCMP TF EQ, EQ)
FloatLtOp -> (FCMP TF LT, NE)
FloatLeOp -> (FCMP TF LE, NE)
DoubleGtOp -> (FCMP TF LE, EQ)
DoubleGeOp -> (FCMP TF LT, EQ)
DoubleEqOp -> (FCMP TF EQ, NE)
DoubleNeOp -> (FCMP TF EQ, EQ)
DoubleLtOp -> (FCMP TF LT, NE)
DoubleLeOp -> (FCMP TF LE, NE)
genCondJump lbl (StPrim op args) =
trivialCode instr args `thenSUs` \ register ->
getNewRegNCG IntKind `thenSUs` \ tmp ->
let
code = registerCode register tmp
result = registerName register tmp
target = ImmCLbl lbl
in
returnSUs (code . mkSeqInstr (BI cond result target))
where
(instr, cond) = case op of
CharGtOp -> (CMP LE, EQ)
CharGeOp -> (CMP LT, EQ)
CharEqOp -> (CMP EQ, NE)
CharNeOp -> (CMP EQ, EQ)
CharLtOp -> (CMP LT, NE)
CharLeOp -> (CMP LE, NE)
IntGtOp -> (CMP LE, EQ)
IntGeOp -> (CMP LT, EQ)
IntEqOp -> (CMP EQ, NE)
IntNeOp -> (CMP EQ, EQ)
IntLtOp -> (CMP LT, NE)
IntLeOp -> (CMP LE, NE)
WordGtOp -> (CMP ULE, EQ)
WordGeOp -> (CMP ULT, EQ)
WordEqOp -> (CMP EQ, NE)
WordNeOp -> (CMP EQ, EQ)
WordLtOp -> (CMP ULT, NE)
WordLeOp -> (CMP ULE, NE)
AddrGtOp -> (CMP ULE, EQ)
AddrGeOp -> (CMP ULT, EQ)
AddrEqOp -> (CMP EQ, NE)
AddrNeOp -> (CMP EQ, EQ)
AddrLtOp -> (CMP ULT, NE)
AddrLeOp -> (CMP ULE, NE)
\end{code}
Now the biggest nightmare---calls. Most of the nastiness is buried in
getCallArg, which moves the arguments to the correct registers/stack
locations. Apart from that, the code is easy.
\begin{code}
genCCall
:: FAST_STRING -- function to call
-> PrimKind -- type of the result
-> [StixTree] -- arguments (of mixed type)
-> SUniqSM (CodeBlock AlphaInstr)
genCCall fn kind args =
mapAccumLNCG getCallArg (argRegs,stackArgLoc) args
`thenSUs` \ ((unused,_), argCode) ->
let
nRegs = length argRegs - length unused
code = asmParThen (map ($ asmVoid) argCode)
in
returnSeq code [
LDA pv (AddrImm (ImmLab (uppPStr fn))),
JSR ra (AddrReg pv) nRegs,
LDGP gp (AddrReg ra)]
where
mapAccumLNCG f b [] = returnSUs (b, [])
mapAccumLNCG f b (x:xs) =
f b x `thenSUs` \ (b__2, x__2) ->
mapAccumLNCG f b__2 xs `thenSUs` \ (b__3, xs__2) ->
returnSUs (b__3, x__2:xs__2)
\end{code}
Trivial (dyadic) instructions. Only look for constants on the right hand
side, because that's where the generic optimizer will have put them.
\begin{code}
trivialCode
:: (Reg -> RI -> Reg -> AlphaInstr)
-> [StixTree]
-> SUniqSM Register
trivialCode instr [x, StInt y]
| is8Bits y =
getReg x `thenSUs` \ register ->
getNewRegNCG IntKind `thenSUs` \ tmp ->
let
code = registerCode register tmp
src1 = registerName register tmp
src2 = ImmInt (fromInteger y)
code__2 dst = code . mkSeqInstr (instr src1 (RIImm src2) dst)
in
returnSUs (Any IntKind code__2)
trivialCode instr [x, y] =
getReg x `thenSUs` \ register1 ->
getReg y `thenSUs` \ register2 ->
getNewRegNCG IntKind `thenSUs` \ tmp1 ->
getNewRegNCG IntKind `thenSUs` \ tmp2 ->
let
code1 = registerCode register1 tmp1 asmVoid
src1 = registerName register1 tmp1
code2 = registerCode register2 tmp2 asmVoid
src2 = registerName register2 tmp2
code__2 dst = asmParThen [code1, code2] .
mkSeqInstr (instr src1 (RIReg src2) dst)
in
returnSUs (Any IntKind code__2)
trivialFCode
:: (Reg -> Reg -> Reg -> AlphaInstr)
-> [StixTree]
-> SUniqSM Register
trivialFCode instr [x, y] =
getReg x `thenSUs` \ register1 ->
getReg y `thenSUs` \ register2 ->
getNewRegNCG DoubleKind `thenSUs` \ tmp1 ->
getNewRegNCG DoubleKind `thenSUs` \ tmp2 ->
let
code1 = registerCode register1 tmp1
src1 = registerName register1 tmp1
code2 = registerCode register2 tmp2
src2 = registerName register2 tmp2
code__2 dst = asmParThen [code1 asmVoid, code2 asmVoid] .
mkSeqInstr (instr src1 src2 dst)
in
returnSUs (Any DoubleKind code__2)
\end{code}
Some bizarre special code for getting condition codes into registers.
Integer non-equality is a test for equality followed by an XOR with 1.
(Integer comparisons always set the result register to 0 or 1.) Floating
point comparisons of any kind leave the result in a floating point register,
so we need to wrangle an integer register out of things.
\begin{code}
intNECode
:: [StixTree]
-> SUniqSM Register
intNECode args =
trivialCode (CMP EQ) args `thenSUs` \ register ->
getNewRegNCG IntKind `thenSUs` \ tmp ->
let
code = registerCode register tmp
src = registerName register tmp
code__2 dst = code . mkSeqInstr (XOR src (RIImm (ImmInt 1)) dst)
in
returnSUs (Any IntKind code__2)
cmpFCode
:: (Reg -> Reg -> Reg -> AlphaInstr)
-> Cond
-> [StixTree]
-> SUniqSM Register
cmpFCode instr cond args =
trivialFCode instr args `thenSUs` \ register ->
getNewRegNCG DoubleKind `thenSUs` \ tmp ->
getUniqLabelNCG `thenSUs` \ lbl ->
let
code = registerCode register tmp
result = registerName register tmp
code__2 dst = code . mkSeqInstrs [
OR zero (RIImm (ImmInt 1)) dst,
BF cond result (ImmCLbl lbl),
OR zero (RIReg zero) dst,
LABEL lbl]
in
returnSUs (Any IntKind code__2)
\end{code}
Trivial unary instructions. Note that we don't have to worry about
matching an StInt as the argument, because genericOpt will already
have handled the constant-folding.
\begin{code}
trivialUCode
:: (RI -> Reg -> AlphaInstr)
-> [StixTree]
-> SUniqSM Register
trivialUCode instr [x] =
getReg x `thenSUs` \ register ->
getNewRegNCG IntKind `thenSUs` \ tmp ->
let
code = registerCode register tmp
src = registerName register tmp
code__2 dst = code . mkSeqInstr (instr (RIReg src) dst)
in
returnSUs (Any IntKind code__2)
trivialUFCode
:: (Reg -> Reg -> AlphaInstr)
-> [StixTree]
-> SUniqSM Register
trivialUFCode instr [x] =
getReg x `thenSUs` \ register ->
getNewRegNCG DoubleKind `thenSUs` \ tmp ->
let
code = registerCode register tmp
src = registerName register tmp
code__2 dst = code . mkSeqInstr (instr src dst)
in
returnSUs (Any DoubleKind code__2)
\end{code}
Simple coercions that don't require any code to be generated.
Here we just change the type on the register passed on up
\begin{code}
coerceIntCode :: PrimKind -> [StixTree] -> SUniqSM Register
coerceIntCode pk [x] =
getReg x `thenSUs` \ register ->
case register of
Fixed reg _ code -> returnSUs (Fixed reg pk code)
Any _ code -> returnSUs (Any pk code)
coerceFltCode :: [StixTree] -> SUniqSM Register
coerceFltCode [x] =
getReg x `thenSUs` \ register ->
case register of
Fixed reg _ code -> returnSUs (Fixed reg DoubleKind code)
Any _ code -> returnSUs (Any DoubleKind code)
\end{code}
Integer to character conversion.
\begin{code}
chrCode [x] =
getReg x `thenSUs` \ register ->
getNewRegNCG IntKind `thenSUs` \ reg ->
let
code = registerCode register reg
src = registerName register reg
code__2 dst = code . mkSeqInstr (ZAPNOT src (RIImm (ImmInt 1)) dst)
in
returnSUs (Any IntKind code__2)
\end{code}
More complicated integer/float conversions. Here we have to store
temporaries in memory to move between the integer and the floating
point register sets.
\begin{code}
coerceInt2FP :: [StixTree] -> SUniqSM Register
coerceInt2FP [x] =
getReg x `thenSUs` \ register ->
getNewRegNCG IntKind `thenSUs` \ reg ->
let
code = registerCode register reg
src = registerName register reg
code__2 dst = code . mkSeqInstrs [
ST Q src (spRel 0),
LD TF dst (spRel 0),
CVTxy Q TF dst dst]
in
returnSUs (Any DoubleKind code__2)
coerceFP2Int :: [StixTree] -> SUniqSM Register
coerceFP2Int [x] =
getReg x `thenSUs` \ register ->
getNewRegNCG DoubleKind `thenSUs` \ tmp ->
let
code = registerCode register tmp
src = registerName register tmp
code__2 dst = code . mkSeqInstrs [
CVTxy TF Q src tmp,
ST TF tmp (spRel 0),
LD Q dst (spRel 0)]
in
returnSUs (Any IntKind code__2)
\end{code}
Some random little helpers.
\begin{code}
is8Bits :: Integer -> Bool
is8Bits i = i >= -256 && i < 256
maybeImm :: StixTree -> Maybe Imm
maybeImm (StInt i)
| i >= toInteger minInt && i <= toInteger maxInt = Just (ImmInt (fromInteger i))
| otherwise = Just (ImmInteger i)
maybeImm (StLitLbl s) = Just (ImmLab s)
maybeImm (StLitLit s) = Just (strImmLab (cvtLitLit (_UNPK_ s)))
maybeImm (StCLbl l) = Just (ImmCLbl l)
maybeImm _ = Nothing
mangleIndexTree :: StixTree -> StixTree
mangleIndexTree (StIndex pk base (StInt i)) =
StPrim IntAddOp [base, off]
where
off = StInt (i * size pk)
size :: PrimKind -> Integer
size pk = case kindToSize pk of
{B -> 1; BU -> 1; W -> 2; WU -> 2; L -> 4; FF -> 4; SF -> 4; _ -> 8}
mangleIndexTree (StIndex pk base off) =
case pk of
CharKind -> StPrim IntAddOp [base, off]
_ -> StPrim IntAddOp [base, off__2]
where
off__2 = StPrim SllOp [off, StInt 3]
cvtLitLit :: String -> String
cvtLitLit "stdin" = "_iob+0" -- This one is probably okay...
cvtLitLit "stdout" = "_iob+56" -- but these next two are dodgy at best
cvtLitLit "stderr" = "_iob+112"
cvtLitLit s
| isHex s = s
| otherwise = error ("Native code generator can't handle ``" ++ s ++ "''")
where
isHex ('0':'x':xs) = all isHexDigit xs
isHex _ = False
-- Now, where have I seen this before?
isHexDigit c = isDigit c || c >= 'A' && c <= 'F' || c >= 'a' && c <= 'f'
\end{code}
spRel gives us a stack relative addressing mode for volatile temporaries
and for excess call arguments.
\begin{code}
spRel
:: Int -- desired stack offset in words, positive or negative
-> Addr
spRel n = AddrRegImm sp (ImmInt (n * 8))
stackArgLoc = 0 :: Int -- where to stack extra call arguments (beyond 6)
\end{code}
\begin{code}
getNewRegNCG :: PrimKind -> SUniqSM Reg
getNewRegNCG pk =
getSUnique `thenSUs` \ u ->
returnSUs (mkReg u pk)
\end{code}
|