2 % (c) The AQUA Project, Glasgow University, 1993-1998
7 CodeSegment(..), StixReg(..), StixTree(..), StixTreeList,
8 sStLitLbl, pprStixTrees,
10 stgBaseReg, stgNode, stgSp, stgSu, stgSpLim,
11 stgHp, stgHpLim, stgTagReg, stgR9, stgR10,
14 fixedHS, arrWordsHS, arrPtrsHS
17 #include "HsVersions.h"
19 import Ratio ( Rational )
21 import AbsCSyn ( node, tagreg, MagicId(..) )
22 import AbsCUtils ( magicIdPrimRep )
23 import CallConv ( CallConv, pprCallConv )
24 import CLabel ( mkAsmTempLabel, CLabel, pprCLabel, pprCLabel_asm )
25 import PrimRep ( PrimRep(..), showPrimRep )
26 import PrimOp ( PrimOp, pprPrimOp )
27 import Unique ( Unique )
28 import SMRep ( fixedHdrSize, arrWordsHdrSize, arrPtrsHdrSize )
29 import UniqSupply ( returnUs, thenUs, getUniqueUs, UniqSM )
33 Here is the tag at the nodes of our @StixTree@. Notice its
34 relationship with @PrimOp@ in prelude/PrimOp.
38 = -- Segment (text or data)
42 -- We can tag the leaves with constants/immediates.
44 | StInt Integer -- ** add Kind at some point
46 | StString FAST_STRING
47 | StLitLbl SDoc -- literal labels
48 -- (will be _-prefixed on some machines)
50 | StCLbl CLabel -- labels that we might index into
52 -- Abstract registers of various kinds
56 -- A typed offset from a base location
58 | StIndex PrimRep StixTree StixTree -- kind, base, offset
60 -- An indirection from an address to its contents.
62 | StInd PrimRep StixTree
64 -- Assignment is typed to determine size and register placement
66 | StAssign PrimRep StixTree StixTree -- dst, src
68 -- A simple assembly label that we might jump to.
72 -- A function header and footer
77 -- An unconditional jump. This instruction is terminal.
78 -- Dynamic targets are allowed
82 -- A fall-through, from slow to fast
84 | StFallThrough CLabel
86 -- A conditional jump. This instruction can be non-terminal :-)
87 -- Only static, local, forward labels are allowed
89 | StCondJump CLabel StixTree
91 -- Raw data (as in an info table).
93 | StData PrimRep [StixTree]
95 -- Primitive Operations
97 | StPrim PrimOp [StixTree]
99 -- Calls to C functions
101 | StCall FAST_STRING CallConv PrimRep [StixTree]
103 -- Assembly-language comments
105 | StComment FAST_STRING
107 sStLitLbl :: FAST_STRING -> StixTree
108 sStLitLbl s = StLitLbl (ptext s)
111 pprStixTrees :: [StixTree] -> SDoc
114 vcat (map ppStixTree ts),
119 paren t = char '(' <> t <> char ')'
121 ppStixTree :: StixTree -> SDoc
124 StSegment cseg -> paren (ppCodeSegment cseg)
125 StInt i -> paren (integer i)
126 StDouble rat -> paren (text "Double" <+> rational rat)
127 StString str -> paren (text "Str" <+> ptext str)
128 StComment str -> paren (text "Comment" <+> ptext str)
130 StCLbl lbl -> pprCLabel lbl
131 StReg reg -> ppStixReg reg
132 StIndex k b o -> paren (ppStixTree b <+> char '+' <>
133 pprPrimRep k <+> ppStixTree o)
134 StInd k t -> pprPrimRep k <> char '[' <> ppStixTree t <> char ']'
135 StAssign k d s -> ppStixTree d <> text " :=" <> pprPrimRep k
136 <> text " " <> ppStixTree s
137 StLabel ll -> pprCLabel ll <+> char ':'
138 StFunBegin ll -> char ' ' $$ paren (text "FunBegin" <+> pprCLabel ll)
139 StFunEnd ll -> paren (text "FunEnd" <+> pprCLabel ll)
140 StJump t -> paren (text "Jump" <+> ppStixTree t)
141 StFallThrough ll -> paren (text "FallThru" <+> pprCLabel ll)
142 StCondJump l t -> paren (text "JumpC" <+> pprCLabel l <+> ppStixTree t)
143 StData k ds -> paren (text "Data" <+> pprPrimRep k <+>
144 hsep (map ppStixTree ds))
145 StPrim op ts -> paren (text "Prim" <+> pprPrimOp op <+> hsep (map ppStixTree ts))
147 -> paren (text "Call" <+> ptext nm <+>
148 pprCallConv cc <+> pprPrimRep k <+> hsep (map ppStixTree args))
150 pprPrimRep = text . showPrimRep
153 Stix registers can have two forms. They {\em may} or {\em may not}
154 map to real, machine-level registers.
158 = StixMagicId MagicId -- Regs which are part of the abstract machine model
160 | StixTemp Unique PrimRep -- "Regs" which model local variables (CTemps) in
163 ppStixReg (StixMagicId mid)
165 ppStixReg (StixTemp u pr)
166 = hcat [text "Temp(", ppr u, ppr pr, char ')']
169 ppMId BaseReg = text "BaseReg"
170 ppMId (VanillaReg kind n) = hcat [text "IntReg(", int (I# n), char ')']
171 ppMId (FloatReg n) = hcat [text "FltReg(", int (I# n), char ')']
172 ppMId (DoubleReg n) = hcat [text "DblReg(", int (I# n), char ')']
173 ppMId (LongReg kind n) = hcat [text "LongReg(", int (I# n), char ')']
176 ppMId SpLim = text "SpLim"
178 ppMId HpLim = text "HpLim"
179 ppMId CurCostCentre = text "CCC"
180 ppMId VoidReg = text "VoidReg"
183 We hope that every machine supports the idea of data segment and text
184 segment (or that it has no segments at all, and we can lump these
188 data CodeSegment = DataSegment | TextSegment deriving (Eq, Show)
189 ppCodeSegment = text . show
191 type StixTreeList = [StixTree] -> [StixTree]
194 Stix Trees for STG registers:
196 stgBaseReg, stgNode, stgSp, stgSu, stgSpLim, stgHp, stgHpLim
199 stgBaseReg = StReg (StixMagicId BaseReg)
200 stgNode = StReg (StixMagicId node)
201 stgTagReg = StReg (StixMagicId tagreg)
202 stgSp = StReg (StixMagicId Sp)
203 stgSu = StReg (StixMagicId Su)
204 stgSpLim = StReg (StixMagicId SpLim)
205 stgHp = StReg (StixMagicId Hp)
206 stgHpLim = StReg (StixMagicId HpLim)
207 stgR9 = StReg (StixMagicId (VanillaReg WordRep ILIT(9)))
208 stgR10 = StReg (StixMagicId (VanillaReg WordRep ILIT(10)))
210 getUniqLabelNCG :: UniqSM CLabel
212 = getUniqueUs `thenUs` \ u ->
213 returnUs (mkAsmTempLabel u)
215 fixedHS = StInt (toInteger fixedHdrSize)
216 arrWordsHS = StInt (toInteger arrWordsHdrSize)
217 arrPtrsHS = StInt (toInteger arrPtrsHdrSize)