[project @ 1996-06-11 13:18:54 by partain]
[ghc-hetmet.git] / ghc / compiler / codeGen / CgBindery.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
3 %
4 \section[CgBindery]{Utility functions related to doing @CgBindings@}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module CgBindery (
10         CgBindings(..), CgIdInfo(..){-dubiously concrete-},
11         StableLoc, VolatileLoc,
12
13         maybeAStkLoc, maybeBStkLoc,
14
15         stableAmodeIdInfo, heapIdInfo, newTempAmodeAndIdInfo,
16         letNoEscapeIdInfo, idInfoToAmode,
17
18         nukeVolatileBinds,
19
20         bindNewToAStack, bindNewToBStack,
21         bindNewToNode, bindNewToReg, bindArgsToRegs,
22         bindNewToTemp, bindNewPrimToAmode,
23         getArgAmode, getArgAmodes,
24         getCAddrModeAndInfo, getCAddrMode,
25         getCAddrModeIfVolatile, getVolatileRegs,
26         rebindToAStack, rebindToBStack
27     ) where
28
29 IMP_Ubiq(){-uitous-}
30 IMPORT_DELOOPER(CgLoop1)                -- here for paranoia-checking
31
32 import AbsCSyn
33 import CgMonad
34
35 import CgUsages         ( getHpRelOffset, getSpARelOffset, getSpBRelOffset )
36 import CLabel           ( mkClosureLabel )
37 import ClosureInfo      ( mkLFImported, mkConLFInfo, mkLFArgument )
38 import HeapOffs         ( VirtualHeapOffset(..),
39                           VirtualSpAOffset(..), VirtualSpBOffset(..)
40                         )
41 import Id               ( idPrimRep, toplevelishId, isDataCon,
42                           mkIdEnv, rngIdEnv, IdEnv(..),
43                           idSetToList,
44                           GenId{-instance NamedThing-}
45                         )
46 import Maybes           ( catMaybes )
47 import Name             ( isLocallyDefined, oddlyImportedName, Name{-instance NamedThing-} )
48 #ifdef DEBUG
49 import PprAbsC          ( pprAmode )
50 #endif
51 import PprStyle         ( PprStyle(..) )
52 import StgSyn           ( StgArg(..), StgLiveVars(..), GenStgArg(..) )
53 import Unpretty         ( uppShow )
54 import Util             ( zipWithEqual, panic )
55 \end{code}
56
57
58 %************************************************************************
59 %*                                                                      *
60 \subsection[Bindery-datatypes]{Data types}
61 %*                                                                      *
62 %************************************************************************
63
64 @(CgBinding a b)@ is a type of finite maps from a to b.
65
66 The assumption used to be that @lookupCgBind@ must get exactly one
67 match.  This is {\em completely wrong} in the case of compiling
68 letrecs (where knot-tying is used).  An initial binding is fed in (and
69 never evaluated); eventually, a correct binding is put into the
70 environment.  So there can be two bindings for a given name.
71
72 \begin{code}
73 type CgBindings = IdEnv CgIdInfo
74
75 data CgIdInfo
76   = MkCgIdInfo  Id      -- Id that this is the info for
77                 VolatileLoc
78                 StableLoc
79                 LambdaFormInfo
80
81 data VolatileLoc
82   = NoVolatileLoc
83   | TempVarLoc  Unique
84
85   | RegLoc      MagicId                 -- in one of the magic registers
86                                         -- (probably {Int,Float,Char,etc}Reg
87
88   | VirHpLoc    VirtualHeapOffset       -- Hp+offset (address of closure)
89
90   | VirNodeLoc  VirtualHeapOffset       -- Cts of offset indirect from Node
91                                         -- ie *(Node+offset)
92
93 data StableLoc
94   = NoStableLoc
95   | VirAStkLoc          VirtualSpAOffset
96   | VirBStkLoc          VirtualSpBOffset
97   | LitLoc              Literal
98   | StableAmodeLoc      CAddrMode
99
100 -- these are so StableLoc can be abstract:
101
102 maybeAStkLoc (VirAStkLoc offset) = Just offset
103 maybeAStkLoc _                   = Nothing
104
105 maybeBStkLoc (VirBStkLoc offset) = Just offset
106 maybeBStkLoc _                   = Nothing
107 \end{code}
108
109 %************************************************************************
110 %*                                                                      *
111 \subsection[Bindery-idInfo]{Manipulating IdInfo}
112 %*                                                                      *
113 %************************************************************************
114
115 \begin{code}
116 stableAmodeIdInfo i amode lf_info = MkCgIdInfo i NoVolatileLoc (StableAmodeLoc amode) lf_info
117 heapIdInfo i offset       lf_info = MkCgIdInfo i (VirHpLoc offset) NoStableLoc lf_info
118 tempIdInfo i uniq         lf_info = MkCgIdInfo i (TempVarLoc uniq) NoStableLoc lf_info
119
120 letNoEscapeIdInfo i spa spb lf_info
121   = MkCgIdInfo i NoVolatileLoc (StableAmodeLoc (CJoinPoint spa spb)) lf_info
122
123 newTempAmodeAndIdInfo :: Id -> LambdaFormInfo -> (CAddrMode, CgIdInfo)
124
125 newTempAmodeAndIdInfo name lf_info
126   = (temp_amode, temp_idinfo)
127   where
128     uniq        = uniqueOf name
129     temp_amode  = CTemp uniq (idPrimRep name)
130     temp_idinfo = tempIdInfo name uniq lf_info
131
132 idInfoToAmode :: PrimRep -> CgIdInfo -> FCode CAddrMode
133 idInfoToAmode kind (MkCgIdInfo _ vol stab _) = idInfoPiecesToAmode kind vol stab
134
135 idInfoPiecesToAmode :: PrimRep -> VolatileLoc -> StableLoc -> FCode CAddrMode
136
137 idInfoPiecesToAmode kind (TempVarLoc uniq) stable_loc   = returnFC (CTemp uniq kind)
138 idInfoPiecesToAmode kind (RegLoc magic_id) stable_loc   = returnFC (CReg magic_id)
139
140 idInfoPiecesToAmode kind NoVolatileLoc (LitLoc lit)           = returnFC (CLit lit)
141 idInfoPiecesToAmode kind NoVolatileLoc (StableAmodeLoc amode) = returnFC amode
142
143 idInfoPiecesToAmode kind (VirNodeLoc nd_off) stable_loc
144   = returnFC (CVal (NodeRel nd_off) kind)
145     -- Virtual offsets from Node increase into the closures,
146     -- and so do Node-relative offsets (which we want in the CVal),
147     -- so there is no mucking about to do to the offset.
148
149 idInfoPiecesToAmode kind (VirHpLoc hp_off) stable_loc
150   = getHpRelOffset hp_off `thenFC` \ rel_hp ->
151     returnFC (CAddr rel_hp)
152
153 idInfoPiecesToAmode kind NoVolatileLoc (VirAStkLoc i)
154   = getSpARelOffset i `thenFC` \ rel_spA ->
155     returnFC (CVal rel_spA kind)
156
157 idInfoPiecesToAmode kind NoVolatileLoc (VirBStkLoc i)
158   = getSpBRelOffset i `thenFC` \ rel_spB ->
159     returnFC (CVal rel_spB kind)
160
161 #ifdef DEBUG
162 idInfoPiecesToAmode kind NoVolatileLoc NoStableLoc = panic "idInfoPiecesToAmode: no loc"
163 #endif
164 \end{code}
165
166 %************************************************************************
167 %*                                                                      *
168 \subsection[Bindery-nuke-volatile]{Nuking volatile bindings}
169 %*                                                                      *
170 %************************************************************************
171
172 We sometimes want to nuke all the volatile bindings; we must be sure
173 we don't leave any (NoVolatile, NoStable) binds around...
174
175 \begin{code}
176 nukeVolatileBinds :: CgBindings -> CgBindings
177 nukeVolatileBinds binds
178   = mkIdEnv (foldr keep_if_stable [] (rngIdEnv binds))
179   where
180     keep_if_stable (MkCgIdInfo i _ NoStableLoc entry_info) acc = acc
181     keep_if_stable (MkCgIdInfo i _ stable_loc  entry_info) acc
182       = (i, MkCgIdInfo i NoVolatileLoc stable_loc entry_info) : acc
183 \end{code}
184
185
186 %************************************************************************
187 %*                                                                      *
188 \subsection[lookup-interface]{Interface functions to looking up bindings}
189 %*                                                                      *
190 %************************************************************************
191
192 I {\em think} all looking-up is done through @getCAddrMode(s)@.
193
194 \begin{code}
195 getCAddrModeAndInfo :: Id -> FCode (CAddrMode, LambdaFormInfo)
196
197 getCAddrModeAndInfo id
198   | not (isLocallyDefined name) || oddlyImportedName name
199   = returnFC (global_amode, mkLFImported id)
200
201   | isDataCon id
202   = returnFC (global_amode, mkConLFInfo id)
203
204   | otherwise = -- *might* be a nested defn: in any case, it's something whose
205                 -- definition we will know about...
206     lookupBindC id `thenFC` \ (MkCgIdInfo _ volatile_loc stable_loc lf_info) ->
207     idInfoPiecesToAmode kind volatile_loc stable_loc `thenFC` \ amode ->
208     returnFC (amode, lf_info)
209   where
210     name = getName id
211     global_amode = CLbl (mkClosureLabel id) kind
212     kind = idPrimRep id
213
214 getCAddrMode :: Id -> FCode CAddrMode
215 getCAddrMode name
216   = getCAddrModeAndInfo name `thenFC` \ (amode, _) ->
217     returnFC amode
218 \end{code}
219
220 \begin{code}
221 getCAddrModeIfVolatile :: Id -> FCode (Maybe CAddrMode)
222 getCAddrModeIfVolatile name
223   | toplevelishId name = returnFC Nothing
224   | otherwise
225   = lookupBindC name `thenFC` \ ~(MkCgIdInfo _ volatile_loc stable_loc lf_info) ->
226     case stable_loc of
227         NoStableLoc ->  -- Aha!  So it is volatile!
228             idInfoPiecesToAmode (idPrimRep name) volatile_loc NoStableLoc `thenFC` \ amode ->
229             returnFC (Just amode)
230
231         a_stable_loc -> returnFC Nothing
232 \end{code}
233
234 @getVolatileRegs@ gets a set of live variables, and returns a list of
235 all registers on which these variables depend.  These are the regs
236 which must be saved and restored across any C calls.  If a variable is
237 both in a volatile location (depending on a register) {\em and} a
238 stable one (notably, on the stack), we modify the current bindings to
239 forget the volatile one.
240
241 \begin{code}
242 getVolatileRegs :: StgLiveVars -> FCode [MagicId]
243
244 getVolatileRegs vars
245   = mapFCs snaffle_it (idSetToList vars) `thenFC` \ stuff ->
246     returnFC (catMaybes stuff)
247   where
248     snaffle_it var
249       = lookupBindC var `thenFC` \ (MkCgIdInfo _ volatile_loc stable_loc lf_info) ->
250         let
251             -- commoned-up code...
252             consider_reg reg
253               = if not (isVolatileReg reg) then
254                         -- Potentially dies across C calls
255                         -- For now, that's everything; we leave
256                         -- it to the save-macros to decide which
257                         -- regs *really* need to be saved.
258                     returnFC Nothing
259                 else
260                     case stable_loc of
261                       NoStableLoc -> returnFC (Just reg) -- got one!
262                       is_a_stable_loc ->
263                         -- has both volatile & stable locations;
264                         -- force it to rely on the stable location
265                         modifyBindC var nuke_vol_bind `thenC`
266                         returnFC Nothing
267         in
268         case volatile_loc of
269           RegLoc reg   -> consider_reg reg
270           VirHpLoc _   -> consider_reg Hp
271           VirNodeLoc _ -> consider_reg node
272           non_reg_loc  -> returnFC Nothing
273
274     nuke_vol_bind (MkCgIdInfo i _ stable_loc lf_info)
275       = MkCgIdInfo i NoVolatileLoc stable_loc lf_info
276 \end{code}
277
278 \begin{code}
279 getArgAmodes :: [StgArg] -> FCode [CAddrMode]
280 getArgAmodes [] = returnFC []
281 getArgAmodes (atom:atoms)
282   = getArgAmode  atom  `thenFC` \ amode ->
283     getArgAmodes atoms `thenFC` \ amodes ->
284     returnFC ( amode : amodes )
285
286 getArgAmode :: StgArg -> FCode CAddrMode
287
288 getArgAmode (StgVarArg var) = getCAddrMode var
289 getArgAmode (StgLitArg lit) = returnFC (CLit lit)
290 \end{code}
291
292 %************************************************************************
293 %*                                                                      *
294 \subsection[binding-and-rebinding-interface]{Interface functions for binding and re-binding names}
295 %*                                                                      *
296 %************************************************************************
297
298 \begin{code}
299 bindNewToAStack :: (Id, VirtualSpAOffset) -> Code
300 bindNewToAStack (name, offset)
301   = addBindC name info
302   where
303     info = MkCgIdInfo name NoVolatileLoc (VirAStkLoc offset) mkLFArgument
304
305 bindNewToBStack :: (Id, VirtualSpBOffset) -> Code
306 bindNewToBStack (name, offset)
307   = addBindC name info
308   where
309     info = MkCgIdInfo name NoVolatileLoc (VirBStkLoc offset) (panic "bindNewToBStack")
310            -- B-stack things shouldn't need lambda-form info!
311
312 bindNewToNode :: Id -> VirtualHeapOffset -> LambdaFormInfo -> Code
313 bindNewToNode name offset lf_info
314   = addBindC name info
315   where
316     info = MkCgIdInfo name (VirNodeLoc offset) NoStableLoc lf_info
317
318 -- Create a new temporary whose unique is that in the id,
319 -- bind the id to it, and return the addressing mode for the
320 -- temporary.
321 bindNewToTemp :: Id -> FCode CAddrMode
322 bindNewToTemp name
323   = let (temp_amode, id_info) = newTempAmodeAndIdInfo name mkLFArgument
324                 -- This is used only for things we don't know
325                 -- anything about; values returned by a case statement,
326                 -- for example.
327     in
328     addBindC name id_info       `thenC`
329     returnFC temp_amode
330
331 bindNewToReg :: Id -> MagicId -> LambdaFormInfo -> Code
332 bindNewToReg name magic_id lf_info
333   = addBindC name info
334   where
335     info = MkCgIdInfo name (RegLoc magic_id) NoStableLoc lf_info
336
337 bindNewToLit name lit
338   = addBindC name info
339   where
340     info = MkCgIdInfo name NoVolatileLoc (LitLoc lit) (error "bindNewToLit")
341
342 bindArgsToRegs :: [Id] -> [MagicId] -> Code
343 bindArgsToRegs args regs
344   = listCs (zipWithEqual "bindArgsToRegs" bind args regs)
345   where
346     arg `bind` reg = bindNewToReg arg reg mkLFArgument
347 \end{code}
348
349 @bindNewPrimToAmode@ works only for certain addressing modes, because
350 those are the only ones we've needed so far!
351
352 \begin{code}
353 bindNewPrimToAmode :: Id -> CAddrMode -> Code
354 bindNewPrimToAmode name (CReg reg) = bindNewToReg name reg (panic "bindNewPrimToAmode")
355                                                 -- was: mkLFArgument
356                                                 -- LFinfo is irrelevant for primitives
357 bindNewPrimToAmode name (CTemp uniq kind)
358   = addBindC name (tempIdInfo name uniq (panic "bindNewPrimToAmode"))
359         -- LFinfo is irrelevant for primitives
360
361 bindNewPrimToAmode name (CLit lit) = bindNewToLit name lit
362
363 bindNewPrimToAmode name (CVal (SpBRel _ offset) _)
364   = bindNewToBStack (name, offset)
365
366 bindNewPrimToAmode name (CVal (NodeRel offset) _)
367   = bindNewToNode name offset (panic "bindNewPrimToAmode node")
368   -- See comment on idInfoPiecesToAmode for VirNodeLoc
369
370 #ifdef DEBUG
371 bindNewPrimToAmode name amode
372   = panic ("bindNew...:"++(uppShow 80 (pprAmode PprDebug  amode)))
373 #endif
374 \end{code}
375
376 \begin{code}
377 rebindToAStack :: Id -> VirtualSpAOffset -> Code
378 rebindToAStack name offset
379   = modifyBindC name replace_stable_fn
380   where
381     replace_stable_fn (MkCgIdInfo i vol stab einfo)
382       = MkCgIdInfo i vol (VirAStkLoc offset) einfo
383
384 rebindToBStack :: Id -> VirtualSpBOffset -> Code
385 rebindToBStack name offset
386   = modifyBindC name replace_stable_fn
387   where
388     replace_stable_fn (MkCgIdInfo i vol stab einfo)
389       = MkCgIdInfo i vol (VirBStkLoc offset) einfo
390 \end{code}
391