[project @ 2001-11-03 00:33:24 by sof]
[ghc-hetmet.git] / ghc / compiler / nativeGen / Stix.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1998
3 %
4
5 \begin{code}
6 module Stix (
7         CodeSegment(..), StixReg(..), StixTree(..), StixTreeList,
8         pprStixTrees, pprStixTree, ppStixReg,
9         stixCountTempUses, stixSubst,
10         DestInfo(..), hasDestInfo,
11
12         stgBaseReg, stgNode, stgSp, stgSu, stgSpLim, 
13         stgHp, stgHpLim, stgTagReg, stgR9, stgR10, 
14         stgCurrentTSO, stgCurrentNursery,
15
16         fixedHS, arrWordsHS, arrPtrsHS,
17
18         NatM, initNat, thenNat, returnNat, 
19         mapNat, mapAndUnzipNat, mapAccumLNat,
20         getUniqueNat, getDeltaNat, setDeltaNat,
21         NatM_State, mkNatM_State,
22         uniqOfNatM_State, deltaOfNatM_State,
23
24         getUniqLabelNCG, getNatLabelNCG,
25         ncgPrimopMoan
26     ) where
27
28 #include "HsVersions.h"
29
30 import Ratio            ( Rational )
31 import IOExts           ( unsafePerformIO )
32 import IO               ( hPutStrLn, stderr )
33
34 import AbsCSyn          ( node, tagreg, MagicId(..) )
35 import ForeignCall      ( CCallConv )
36 import CLabel           ( mkAsmTempLabel, CLabel, pprCLabel )
37 import PrimRep          ( PrimRep(..) )
38 import PrimOp           ( PrimOp )
39 import Unique           ( Unique )
40 import SMRep            ( fixedHdrSize, arrWordsHdrSize, arrPtrsHdrSize )
41 import UniqSupply       ( UniqSupply, splitUniqSupply, uniqFromSupply,
42                           UniqSM, thenUs, returnUs, getUniqueUs )
43 import Outputable
44 import FastTypes
45 \end{code}
46
47 Here is the tag at the nodes of our @StixTree@.  Notice its
48 relationship with @PrimOp@ in prelude/PrimOp.
49
50 \begin{code}
51 data StixTree
52   = -- Segment (text or data)
53
54     StSegment CodeSegment
55
56     -- We can tag the leaves with constants/immediates.
57
58   | StInt       Integer     -- ** add Kind at some point
59   | StFloat     Rational
60   | StDouble    Rational
61   | StString    FAST_STRING
62   | StCLbl      CLabel      -- labels that we might index into
63
64     -- Abstract registers of various kinds
65
66   | StReg StixReg
67
68     -- A typed offset from a base location
69
70   | StIndex PrimRep StixTree StixTree -- kind, base, offset
71
72     -- An indirection from an address to its contents.
73
74   | StInd PrimRep StixTree
75
76     -- Assignment is typed to determine size and register placement
77
78   | StAssign PrimRep StixTree StixTree -- dst, src
79
80     -- A simple assembly label that we might jump to.
81
82   | StLabel CLabel
83
84     -- A function header and footer
85
86   | StFunBegin CLabel
87   | StFunEnd CLabel
88
89     -- An unconditional jump. This instruction may or may not jump
90     -- out of the register allocation domain (basic block, more or
91     -- less).  For correct register allocation when this insn is used
92     -- to jump through a jump table, we optionally allow a list of
93     -- the exact targets to be attached, so that the allocator can
94     -- easily construct the exact flow edges leaving this insn.
95     -- Dynamic targets are allowed.
96
97   | StJump DestInfo StixTree
98
99     -- A fall-through, from slow to fast
100
101   | StFallThrough CLabel
102
103     -- A conditional jump. This instruction can be non-terminal :-)
104     -- Only static, local, forward labels are allowed
105
106   | StCondJump CLabel StixTree
107
108     -- Raw data (as in an info table).
109
110   | StData PrimRep [StixTree]
111
112     -- Primitive Operations
113
114   | StPrim PrimOp [StixTree]
115
116     -- Calls to C functions
117
118   | StCall FAST_STRING CCallConv PrimRep [StixTree]
119
120     -- A volatile memory scratch array, which is allocated
121     -- relative to the stack pointer.  It is an array of
122     -- ptr/word/int sized things.  Do not expect to be preserved
123     -- beyond basic blocks or over a ccall.  Current max size
124     -- is 6, used in StixInteger.
125
126   | StScratchWord Int
127
128     -- Assembly-language comments
129
130   | StComment FAST_STRING
131
132
133 -- used by insnFuture in RegAllocInfo.lhs
134 data DestInfo
135    = NoDestInfo             -- no supplied dests; infer from context
136    | DestInfo [CLabel]      -- precisely these dests and no others
137
138 hasDestInfo NoDestInfo   = False
139 hasDestInfo (DestInfo _) = True
140
141 pprDests :: DestInfo -> SDoc
142 pprDests NoDestInfo      = text "NoDestInfo"
143 pprDests (DestInfo dsts) = brackets (hsep (map pprCLabel dsts))
144
145
146 pprStixTrees :: [StixTree] -> SDoc
147 pprStixTrees ts 
148   = vcat [
149        vcat (map pprStixTree ts),
150        char ' ',
151        char ' '
152     ]
153
154 pprStixTree :: StixTree -> SDoc
155 pprStixTree t 
156    = case t of
157        StSegment cseg   -> parens (ppCodeSegment cseg)
158        StInt i          -> parens (integer i)
159        StFloat rat      -> parens (text "Float" <+> rational rat)
160        StDouble rat     -> parens (text "Double" <+> rational rat)
161        StString str     -> parens (text "Str `" <> ptext str <> char '\'')
162        StComment str    -> parens (text "Comment" <+> ptext str)
163        StCLbl lbl       -> pprCLabel lbl
164        StReg reg        -> ppStixReg reg
165        StIndex k b o    -> parens (pprStixTree b <+> char '+' <> 
166                                    ppr k <+> pprStixTree o)
167        StInd k t        -> ppr k <> char '[' <> pprStixTree t <> char ']'
168        StAssign k d s   -> pprStixTree d <> text "  :=" <> ppr k 
169                                          <> text "  " <> pprStixTree s
170        StLabel ll       -> pprCLabel ll <+> char ':'
171        StFunBegin ll    -> char ' ' $$ parens (text "FunBegin" <+> pprCLabel ll)
172        StFunEnd ll      -> parens (text "FunEnd" <+> pprCLabel ll)
173        StJump dsts t    -> parens (text "Jump" <+> pprDests dsts <+> pprStixTree t)
174        StFallThrough ll -> parens (text "FallThru" <+> pprCLabel ll)
175        StCondJump l t   -> parens (text "JumpC" <+> pprCLabel l 
176                                                 <+> pprStixTree t)
177        StData k ds      -> parens (text "Data" <+> ppr k <+>
178                                    hsep (map pprStixTree ds))
179        StPrim op ts     -> parens (text "Prim" <+> ppr op <+> 
180                                    hsep (map pprStixTree ts))
181        StCall nm cc k args
182                         -> parens (text "Call" <+> ptext nm <+>
183                                    ppr cc <+> ppr k <+> 
184                                    hsep (map pprStixTree args))
185        StScratchWord i  -> text "ScratchWord" <> parens (int i)
186 \end{code}
187
188 Stix registers can have two forms.  They {\em may} or {\em may not}
189 map to real, machine-level registers.
190
191 \begin{code}
192 data StixReg
193   = StixMagicId MagicId -- Regs which are part of the abstract machine model
194
195   | StixTemp Unique PrimRep -- "Regs" which model local variables (CTemps) in
196                                         -- the abstract C.
197
198 ppStixReg (StixMagicId mid)
199    = ppMId mid
200 ppStixReg (StixTemp u pr)
201    = hcat [text "Temp(", ppr u, ppr pr, char ')']
202
203
204 ppMId BaseReg              = text "BaseReg"
205 ppMId (VanillaReg kind n)  = hcat [ppr kind, text "IntReg(", 
206                                    int (iBox n), char ')']
207 ppMId (FloatReg n)         = hcat [text "FltReg(", int (iBox n), char ')']
208 ppMId (DoubleReg n)        = hcat [text "DblReg(", int (iBox n), char ')']
209 ppMId (LongReg kind n)     = hcat [ppr kind, text "LongReg(", 
210                                    int (iBox n), char ')']
211 ppMId Sp                   = text "Sp"
212 ppMId Su                   = text "Su"
213 ppMId SpLim                = text "SpLim"
214 ppMId Hp                   = text "Hp"
215 ppMId HpLim                = text "HpLim"
216 ppMId CurCostCentre        = text "CCC"
217 ppMId VoidReg              = text "VoidReg"
218 \end{code}
219
220 We hope that every machine supports the idea of data segment and text
221 segment (or that it has no segments at all, and we can lump these
222 together).
223
224 \begin{code}
225 data CodeSegment = DataSegment | TextSegment | RoDataSegment deriving (Eq, Show)
226 ppCodeSegment = text . show
227
228 type StixTreeList = [StixTree] -> [StixTree]
229 \end{code}
230
231 Stix Trees for STG registers:
232 \begin{code}
233 stgBaseReg, stgNode, stgSp, stgSu, stgSpLim, stgHp, stgHpLim 
234         :: StixTree
235
236 stgBaseReg          = StReg (StixMagicId BaseReg)
237 stgNode             = StReg (StixMagicId node)
238 stgTagReg           = StReg (StixMagicId tagreg)
239 stgSp               = StReg (StixMagicId Sp)
240 stgSu               = StReg (StixMagicId Su)
241 stgSpLim            = StReg (StixMagicId SpLim)
242 stgHp               = StReg (StixMagicId Hp)
243 stgHpLim            = StReg (StixMagicId HpLim)
244 stgCurrentTSO       = StReg (StixMagicId CurrentTSO)
245 stgCurrentNursery   = StReg (StixMagicId CurrentNursery)
246 stgR9               = StReg (StixMagicId (VanillaReg WordRep (_ILIT 9)))
247 stgR10              = StReg (StixMagicId (VanillaReg WordRep (_ILIT 10)))
248
249 getNatLabelNCG :: NatM CLabel
250 getNatLabelNCG
251   = getUniqueNat `thenNat` \ u ->
252     returnNat (mkAsmTempLabel u)
253
254 getUniqLabelNCG :: UniqSM CLabel
255 getUniqLabelNCG
256   = getUniqueUs `thenUs` \ u ->
257     returnUs (mkAsmTempLabel u)
258
259 fixedHS     = StInt (toInteger fixedHdrSize)
260 arrWordsHS  = StInt (toInteger arrWordsHdrSize)
261 arrPtrsHS   = StInt (toInteger arrPtrsHdrSize)
262 \end{code}
263
264 Stix optimisation passes may wish to find out how many times a
265 given temporary appears in a tree, so as to be able to decide
266 whether or not to inline the assignment's RHS at usage site(s).
267
268 \begin{code}
269 stixCountTempUses :: Unique -> StixTree -> Int
270 stixCountTempUses u t 
271    = let qq = stixCountTempUses u
272      in
273      case t of
274         StReg reg
275            -> case reg of 
276                  StixTemp uu pr  -> if u == uu then 1 else 0
277                  StixMagicId mid -> 0
278
279         StIndex    pk t1 t2       -> qq t1 + qq t2
280         StInd      pk t1          -> qq t1
281         StAssign   pk t1 t2       -> qq t1 + qq t2
282         StJump     dsts t1        -> qq t1
283         StCondJump lbl t1         -> qq t1
284         StData     pk ts          -> sum (map qq ts)
285         StPrim     op ts          -> sum (map qq ts)
286         StCall     nm cconv pk ts -> sum (map qq ts)
287
288         StSegment _      -> 0
289         StInt _          -> 0
290         StFloat _        -> 0
291         StDouble _       -> 0
292         StString _       -> 0
293         StCLbl _         -> 0
294         StLabel _        -> 0
295         StFunBegin _     -> 0
296         StFunEnd _       -> 0
297         StFallThrough _  -> 0
298         StScratchWord _  -> 0
299         StComment _      -> 0
300
301
302 stixSubst :: Unique -> StixTree -> StixTree -> StixTree
303 stixSubst u new_u in_this_tree
304    = stixMapUniques f in_this_tree
305      where
306         f :: Unique -> Maybe StixTree
307         f uu = if uu == u then Just new_u else Nothing
308
309
310 stixMapUniques :: (Unique -> Maybe StixTree) -> StixTree -> StixTree
311 stixMapUniques f t
312    = let qq = stixMapUniques f
313      in
314      case t of
315         StReg reg
316            -> case reg of 
317                  StixMagicId mid -> t
318                  StixTemp uu pr  
319                     -> case f uu of
320                           Just xx -> xx
321                           Nothing -> t
322
323         StIndex    pk t1 t2       -> StIndex    pk (qq t1) (qq t2)
324         StInd      pk t1          -> StInd      pk (qq t1)
325         StAssign   pk t1 t2       -> StAssign   pk (qq t1) (qq t2)
326         StJump     dsts t1        -> StJump     dsts (qq t1)
327         StCondJump lbl t1         -> StCondJump lbl (qq t1)
328         StData     pk ts          -> StData     pk (map qq ts)
329         StPrim     op ts          -> StPrim     op (map qq ts)
330         StCall     nm cconv pk ts -> StCall     nm cconv pk (map qq ts)
331
332         StSegment _      -> t
333         StInt _          -> t
334         StFloat _        -> t
335         StDouble _       -> t
336         StString _       -> t
337         StCLbl _         -> t
338         StLabel _        -> t
339         StFunBegin _     -> t
340         StFunEnd _       -> t
341         StFallThrough _  -> t
342         StScratchWord _  -> t
343         StComment _      -> t
344 \end{code}
345
346 \begin{code}
347 data NatM_State = NatM_State UniqSupply Int
348 type NatM result = NatM_State -> (result, NatM_State)
349
350 mkNatM_State :: UniqSupply -> Int -> NatM_State
351 mkNatM_State = NatM_State
352
353 uniqOfNatM_State  (NatM_State us delta) = us
354 deltaOfNatM_State (NatM_State us delta) = delta
355
356
357 initNat :: NatM_State -> NatM a -> (a, NatM_State)
358 initNat init_st m = case m init_st of { (r,st) -> (r,st) }
359
360 thenNat :: NatM a -> (a -> NatM b) -> NatM b
361 thenNat expr cont st
362   = case expr st of { (result, st') -> cont result st' }
363
364 returnNat :: a -> NatM a
365 returnNat result st = (result, st)
366
367 mapNat :: (a -> NatM b) -> [a] -> NatM [b]
368 mapNat f []     = returnNat []
369 mapNat f (x:xs)
370   = f x          `thenNat` \ r  ->
371     mapNat f xs  `thenNat` \ rs ->
372     returnNat (r:rs)
373
374 mapAndUnzipNat :: (a -> NatM (b,c))   -> [a] -> NatM ([b],[c])
375 mapAndUnzipNat f [] = returnNat ([],[])
376 mapAndUnzipNat f (x:xs)
377   = f x                 `thenNat` \ (r1,  r2)  ->
378     mapAndUnzipNat f xs `thenNat` \ (rs1, rs2) ->
379     returnNat (r1:rs1, r2:rs2)
380
381 mapAccumLNat :: (acc -> x -> NatM (acc, y))
382                 -> acc
383                 -> [x]
384                 -> NatM (acc, [y])
385
386 mapAccumLNat f b []
387   = returnNat (b, [])
388 mapAccumLNat f b (x:xs)
389   = f b x                           `thenNat` \ (b__2, x__2) ->
390     mapAccumLNat f b__2 xs          `thenNat` \ (b__3, xs__2) ->
391     returnNat (b__3, x__2:xs__2)
392
393
394 getUniqueNat :: NatM Unique
395 getUniqueNat (NatM_State us delta)
396     = case splitUniqSupply us of
397          (us1,us2) -> (uniqFromSupply us1, (NatM_State us2 delta))
398
399 getDeltaNat :: NatM Int
400 getDeltaNat st@(NatM_State us delta)
401    = (delta, st)
402
403 setDeltaNat :: Int -> NatM ()
404 setDeltaNat delta (NatM_State us _)
405    = ((), NatM_State us delta)
406 \end{code}
407
408 Giving up in a not-too-inelegant way.
409
410 \begin{code}
411 ncgPrimopMoan :: String -> SDoc -> a
412 ncgPrimopMoan msg pp_rep
413    = unsafePerformIO (
414         hPutStrLn stderr (
415         "\n" ++
416         "You've fallen across an unimplemented case in GHC's native code generation\n" ++
417         "machinery.  You can work around this for the time being by compiling\n" ++ 
418         "this module via the C route, by giving the flag -fvia-C.\n" ++
419         "The panic below contains information, intended for the GHC implementors,\n" ++
420         "about the exact place where GHC gave up.  Please send it to us\n" ++
421         "at glasgow-haskell-bugs@haskell.org, so as to encourage us to fix this.\n"
422         )
423      )
424      `seq`
425      pprPanic msg pp_rep
426 \end{code}