1 -----------------------------------------------------------------------------
5 -- The datatypes and functions here encapsulate the
6 -- calling and return conventions used by the code generator.
8 -- (c) The University of Glasgow 2004
10 -----------------------------------------------------------------------------
14 -- Argument descriptors
15 mkArgDescr, argDescrType,
18 isBigLiveness, buildContLiveness, mkRegLiveness,
19 smallLiveness, mkLivenessCLit,
21 -- Register assignment
22 assignCallRegs, assignReturnRegs, assignPrimOpCallRegs,
25 constructSlowCall, slowArgs, slowCallPattern,
28 CtrlReturnConvention(..),
34 #include "HsVersions.h"
36 import CgUtils ( emitRODataLits, mkWordCLit )
39 import Constants ( mAX_FAMILY_SIZE_FOR_VEC_RETURNS,
40 mAX_Vanilla_REG, mAX_Float_REG,
41 mAX_Double_REG, mAX_Long_REG,
42 mAX_Real_Vanilla_REG, mAX_Real_Float_REG,
43 mAX_Real_Double_REG, mAX_Real_Long_REG,
47 import ClosureInfo ( ArgDescr(..), Liveness(..) )
48 import CgStackery ( getSpRelOffset )
50 import MachOp ( wordRep )
51 import Cmm ( CmmExpr(..), GlobalReg(..), CmmLit(..), CmmReg(..), node )
52 import CmmUtils ( mkLblExpr )
54 import Maybes ( mapCatMaybes )
57 import TyCon ( TyCon, tyConFamilySize )
58 import Bitmap ( Bitmap, mAX_SMALL_BITMAP_SIZE,
59 mkBitmap, intsToReverseBitmap )
60 import Util ( isn'tIn, sortLe )
61 import StaticFlags ( opt_Unregisterised )
62 import FastString ( LitString )
67 -------------------------------------------------------------------------
69 -- Making argument descriptors
71 -- An argument descriptor describes the layout of args on the stack,
72 -- both for * GC (stack-layout) purposes, and
73 -- * saving/restoring registers when a heap-check fails
75 -- Void arguments aren't important, therefore (contrast constructSlowCall)
77 -------------------------------------------------------------------------
79 -- bring in ARG_P, ARG_N, etc.
80 #include "../includes/StgFun.h"
82 -------------------------
83 argDescrType :: ArgDescr -> Int
84 -- The "argument type" RTS field type
85 argDescrType (ArgSpec n) = n
86 argDescrType (ArgGen liveness)
87 | isBigLiveness liveness = ARG_GEN_BIG
91 mkArgDescr :: Name -> [Id] -> FCode ArgDescr
93 = case stdPattern arg_reps of
94 Just spec_id -> return (ArgSpec spec_id)
95 Nothing -> do { liveness <- mkLiveness nm size bitmap
96 ; return (ArgGen liveness) }
98 arg_reps = filter nonVoidArg (map idCgRep args)
99 -- Getting rid of voids eases matching of standard patterns
101 bitmap = mkBitmap arg_bits
102 arg_bits = argBits arg_reps
103 size = length arg_bits
105 argBits :: [CgRep] -> [Bool] -- True for non-ptr, False for ptr
107 argBits (PtrArg : args) = False : argBits args
108 argBits (arg : args) = take (cgRepSizeW arg) (repeat True) ++ argBits args
110 stdPattern :: [CgRep] -> Maybe Int
111 stdPattern [PtrArg] = Just ARG_P
112 stdPattern [FloatArg] = Just ARG_F
113 stdPattern [DoubleArg] = Just ARG_D
114 stdPattern [LongArg] = Just ARG_L
115 stdPattern [NonPtrArg] = Just ARG_N
117 stdPattern [NonPtrArg,NonPtrArg] = Just ARG_NN
118 stdPattern [NonPtrArg,PtrArg] = Just ARG_NP
119 stdPattern [PtrArg,NonPtrArg] = Just ARG_PN
120 stdPattern [PtrArg,PtrArg] = Just ARG_PP
122 stdPattern [NonPtrArg,NonPtrArg,NonPtrArg] = Just ARG_NNN
123 stdPattern [NonPtrArg,NonPtrArg,PtrArg] = Just ARG_NNP
124 stdPattern [NonPtrArg,PtrArg,NonPtrArg] = Just ARG_NPN
125 stdPattern [NonPtrArg,PtrArg,PtrArg] = Just ARG_NPP
126 stdPattern [PtrArg,NonPtrArg,NonPtrArg] = Just ARG_PNN
127 stdPattern [PtrArg,NonPtrArg,PtrArg] = Just ARG_PNP
128 stdPattern [PtrArg,PtrArg,NonPtrArg] = Just ARG_PPN
129 stdPattern [PtrArg,PtrArg,PtrArg] = Just ARG_PPP
131 stdPattern [PtrArg,PtrArg,PtrArg,PtrArg] = Just ARG_PPPP
132 stdPattern [PtrArg,PtrArg,PtrArg,PtrArg,PtrArg] = Just ARG_PPPPP
133 stdPattern [PtrArg,PtrArg,PtrArg,PtrArg,PtrArg,PtrArg] = Just ARG_PPPPPP
134 stdPattern other = Nothing
137 -------------------------------------------------------------------------
141 -------------------------------------------------------------------------
143 mkLiveness :: Name -> Int -> Bitmap -> FCode Liveness
144 mkLiveness name size bits
145 | size > mAX_SMALL_BITMAP_SIZE -- Bitmap does not fit in one word
146 = do { let lbl = mkBitmapLabel name
147 ; emitRODataLits lbl ( mkWordCLit (fromIntegral size)
148 : map mkWordCLit bits)
149 ; return (BigLiveness lbl) }
151 | otherwise -- Bitmap fits in one word
153 small_bits = case bits of
155 [b] -> fromIntegral b
156 _ -> panic "livenessToAddrMode"
158 return (smallLiveness size small_bits)
160 smallLiveness :: Int -> StgWord -> Liveness
161 smallLiveness size small_bits = SmallLiveness bits
162 where bits = fromIntegral size .|. (small_bits `shiftL` bITMAP_BITS_SHIFT)
165 isBigLiveness :: Liveness -> Bool
166 isBigLiveness (BigLiveness _) = True
167 isBigLiveness (SmallLiveness _) = False
170 mkLivenessCLit :: Liveness -> CmmLit
171 mkLivenessCLit (BigLiveness lbl) = CmmLabel lbl
172 mkLivenessCLit (SmallLiveness bits) = mkWordCLit bits
175 -------------------------------------------------------------------------
177 -- Bitmap describing register liveness
178 -- across GC when doing a "generic" heap check
179 -- (a RET_DYN stack frame).
181 -- NB. Must agree with these macros (currently in StgMacros.h):
182 -- GET_NON_PTRS(), GET_PTRS(), GET_LIVENESS().
183 -------------------------------------------------------------------------
185 mkRegLiveness :: [(Id, GlobalReg)] -> Int -> Int -> StgWord
186 mkRegLiveness regs ptrs nptrs
187 = (fromIntegral nptrs `shiftL` 16) .|.
188 (fromIntegral ptrs `shiftL` 24) .|.
189 all_non_ptrs `xor` reg_bits regs
194 reg_bits ((id, VanillaReg i) : regs) | isFollowableArg (idCgRep id)
195 = (1 `shiftL` (i - 1)) .|. reg_bits regs
199 -------------------------------------------------------------------------
201 -- Pushing the arguments for a slow call
203 -------------------------------------------------------------------------
205 -- For a slow call, we must take a bunch of arguments and intersperse
206 -- some stg_ap_<pattern>_ret_info return addresses.
207 constructSlowCall :: [(CgRep,CmmExpr)] -> (CLabel, [(CgRep,CmmExpr)])
208 -- don't forget the zero case
212 stg_ap_0 = enterRtsRetLabel SLIT("stg_ap_0")
214 constructSlowCall amodes
215 = (stg_ap_pat, these ++ slowArgs rest)
217 stg_ap_pat = enterRtsRetLabel arg_pat
218 (arg_pat, these, rest) = matchSlowPattern amodes
220 enterRtsRetLabel arg_pat
221 | tablesNextToCode = mkRtsRetInfoLabel arg_pat
222 | otherwise = mkRtsRetLabel arg_pat
224 -- | 'slowArgs' takes a list of function arguments and prepares them for
225 -- pushing on the stack for "extra" arguments to a function which requires
226 -- fewer arguments than we currently have.
227 slowArgs :: [(CgRep,CmmExpr)] -> [(CgRep,CmmExpr)]
229 slowArgs amodes = (NonPtrArg, mkLblExpr stg_ap_pat) : args ++ slowArgs rest
230 where (arg_pat, args, rest) = matchSlowPattern amodes
231 stg_ap_pat = mkRtsRetInfoLabel arg_pat
233 matchSlowPattern :: [(CgRep,CmmExpr)]
234 -> (LitString, [(CgRep,CmmExpr)], [(CgRep,CmmExpr)])
235 matchSlowPattern amodes = (arg_pat, these, rest)
236 where (arg_pat, n) = slowCallPattern (map fst amodes)
237 (these, rest) = splitAt n amodes
239 -- These cases were found to cover about 99% of all slow calls:
240 slowCallPattern (PtrArg: PtrArg: PtrArg: PtrArg: PtrArg: PtrArg: _) = (SLIT("stg_ap_pppppp"), 6)
241 slowCallPattern (PtrArg: PtrArg: PtrArg: PtrArg: PtrArg: _) = (SLIT("stg_ap_ppppp"), 5)
242 slowCallPattern (PtrArg: PtrArg: PtrArg: PtrArg: _) = (SLIT("stg_ap_pppp"), 4)
243 slowCallPattern (PtrArg: PtrArg: PtrArg: VoidArg: _) = (SLIT("stg_ap_pppv"), 4)
244 slowCallPattern (PtrArg: PtrArg: PtrArg: _) = (SLIT("stg_ap_ppp"), 3)
245 slowCallPattern (PtrArg: PtrArg: VoidArg: _) = (SLIT("stg_ap_ppv"), 3)
246 slowCallPattern (PtrArg: PtrArg: _) = (SLIT("stg_ap_pp"), 2)
247 slowCallPattern (PtrArg: VoidArg: _) = (SLIT("stg_ap_pv"), 2)
248 slowCallPattern (PtrArg: _) = (SLIT("stg_ap_p"), 1)
249 slowCallPattern (VoidArg: _) = (SLIT("stg_ap_v"), 1)
250 slowCallPattern (NonPtrArg: _) = (SLIT("stg_ap_n"), 1)
251 slowCallPattern (FloatArg: _) = (SLIT("stg_ap_f"), 1)
252 slowCallPattern (DoubleArg: _) = (SLIT("stg_ap_d"), 1)
253 slowCallPattern (LongArg: _) = (SLIT("stg_ap_l"), 1)
254 slowCallPattern _ = panic "CgStackery.slowCallPattern"
256 -------------------------------------------------------------------------
258 -- Return conventions
260 -------------------------------------------------------------------------
262 -- A @CtrlReturnConvention@ says how {\em control} is returned.
264 data CtrlReturnConvention
265 = VectoredReturn Int -- size of the vector table (family size)
266 | UnvectoredReturn Int -- family size
268 ctrlReturnConvAlg :: TyCon -> CtrlReturnConvention
269 ctrlReturnConvAlg tycon
270 = case (tyConFamilySize tycon) of
271 size -> -- we're supposed to know...
272 if (size > (1::Int) && size <= mAX_FAMILY_SIZE_FOR_VEC_RETURNS) then
275 UnvectoredReturn size
276 -- NB: unvectored returns Include size 0 (no constructors), so that
277 -- the following perverse code compiles (it crashed GHC in 5.02)
279 -- data T2 = T2 !T1 Int
280 -- The only value of type T1 is bottom, which never returns anyway.
282 dataReturnConvPrim :: CgRep -> CmmReg
283 dataReturnConvPrim PtrArg = CmmGlobal (VanillaReg 1)
284 dataReturnConvPrim NonPtrArg = CmmGlobal (VanillaReg 1)
285 dataReturnConvPrim LongArg = CmmGlobal (LongReg 1)
286 dataReturnConvPrim FloatArg = CmmGlobal (FloatReg 1)
287 dataReturnConvPrim DoubleArg = CmmGlobal (DoubleReg 1)
288 dataReturnConvPrim VoidArg = panic "dataReturnConvPrim: void"
291 -- getSequelAmode returns an amode which refers to an info table. The info
292 -- table will always be of the RET(_VEC)?_(BIG|SMALL) kind. We're careful
293 -- not to handle real code pointers, just in case we're compiling for
294 -- an unregisterised/untailcallish architecture, where info pointers and
295 -- code pointers aren't the same.
297 -- The OnStack case of sequelToAmode delivers an Amode which is only
298 -- valid just before the final control transfer, because it assumes
299 -- that Sp is pointing to the top word of the return address. This
300 -- seems unclean but there you go.
302 getSequelAmode :: FCode CmmExpr
304 = do { EndOfBlockInfo virt_sp sequel <- getEndOfBlockInfo
306 OnStack -> do { sp_rel <- getSpRelOffset virt_sp
307 ; returnFC (CmmLoad sp_rel wordRep) }
309 UpdateCode -> returnFC (CmmLit (CmmLabel mkUpdInfoLabel))
310 CaseAlts lbl _ _ True -> returnFC (CmmLit (CmmLabel mkSeqInfoLabel))
311 CaseAlts lbl _ _ False -> returnFC (CmmLit (CmmLabel lbl))
314 -------------------------------------------------------------------------
316 -- Build a liveness mask for the current stack
318 -------------------------------------------------------------------------
320 -- There are four kinds of things on the stack:
322 -- - pointer variables (bound in the environment)
323 -- - non-pointer variables (boudn in the environment)
324 -- - free slots (recorded in the stack free list)
325 -- - non-pointer data slots (recorded in the stack free list)
327 -- We build up a bitmap of non-pointer slots by searching the environment
328 -- for all the pointer variables, and subtracting these from a bitmap
329 -- with initially all bits set (up to the size of the stack frame).
331 buildContLiveness :: Name -- Basis for label (only)
332 -> [VirtualSpOffset] -- Live stack slots
334 buildContLiveness name live_slots
335 = do { stk_usg <- getStkUsage
336 ; let StackUsage { realSp = real_sp,
337 frameSp = frame_sp } = stk_usg
339 start_sp :: VirtualSpOffset
340 start_sp = real_sp - retAddrSizeW
341 -- In a continuation, we want a liveness mask that
342 -- starts from just after the return address, which is
343 -- on the stack at real_sp.
345 frame_size :: WordOff
346 frame_size = start_sp - frame_sp
347 -- real_sp points to the frame-header for the current
348 -- stack frame, and the end of this frame is frame_sp.
349 -- The size is therefore real_sp - frame_sp - retAddrSizeW
350 -- (subtract one for the frame-header = return address).
352 rel_slots :: [WordOff]
353 rel_slots = sortLe (<=)
354 [ start_sp - ofs -- Get slots relative to top of frame
355 | ofs <- live_slots ]
357 bitmap = intsToReverseBitmap frame_size rel_slots
359 ; WARN( not (all (>=0) rel_slots),
360 ppr name $$ ppr live_slots $$ ppr frame_size $$ ppr start_sp $$ ppr rel_slots )
361 mkLiveness name frame_size bitmap }
364 -------------------------------------------------------------------------
366 -- Register assignment
368 -------------------------------------------------------------------------
370 -- How to assign registers for
372 -- 1) Calling a fast entry point.
373 -- 2) Returning an unboxed tuple.
374 -- 3) Invoking an out-of-line PrimOp.
376 -- Registers are assigned in order.
378 -- If we run out, we don't attempt to assign any further registers (even
379 -- though we might have run out of only one kind of register); we just
380 -- return immediately with the left-overs specified.
382 -- The alternative version @assignAllRegs@ uses the complete set of
383 -- registers, including those that aren't mapped to real machine
384 -- registers. This is used for calling special RTS functions and PrimOps
385 -- which expect their arguments to always be in the same registers.
387 assignCallRegs, assignPrimOpCallRegs, assignReturnRegs
388 :: [(CgRep,a)] -- Arg or result values to assign
389 -> ([(a, GlobalReg)], -- Register assignment in same order
390 -- for *initial segment of* input list
391 -- (but reversed; doesn't matter)
392 -- VoidRep args do not appear here
393 [(CgRep,a)]) -- Leftover arg or result values
396 = assign_regs args (mkRegTbl [node])
397 -- The entry convention for a function closure
398 -- never uses Node for argument passing; instead
399 -- Node points to the function closure itself
401 assignPrimOpCallRegs args
402 = assign_regs args (mkRegTbl_allRegs [])
403 -- For primops, *all* arguments must be passed in registers
405 assignReturnRegs args
406 = assign_regs args (mkRegTbl [])
407 -- For returning unboxed tuples etc,
410 assign_regs :: [(CgRep,a)] -- Arg or result values to assign
411 -> AvailRegs -- Regs still avail: Vanilla, Float, Double, Longs
412 -> ([(a, GlobalReg)], [(CgRep, a)])
413 assign_regs args supply
416 go [] acc supply = (acc, []) -- Return the results reversed (doesn't matter)
417 go ((VoidArg,_) : args) acc supply -- Skip void arguments; they aren't passed, and
418 = go args acc supply -- there's nothign to bind them to
419 go ((rep,arg) : args) acc supply
420 = case assign_reg rep supply of
421 Just (reg, supply') -> go args ((arg,reg):acc) supply'
422 Nothing -> (acc, (rep,arg):args) -- No more regs
424 assign_reg :: CgRep -> AvailRegs -> Maybe (GlobalReg, AvailRegs)
425 assign_reg FloatArg (vs, f:fs, ds, ls) = Just (FloatReg f, (vs, fs, ds, ls))
426 assign_reg DoubleArg (vs, fs, d:ds, ls) = Just (DoubleReg d, (vs, fs, ds, ls))
427 assign_reg LongArg (vs, fs, ds, l:ls) = Just (LongReg l, (vs, fs, ds, ls))
428 assign_reg PtrArg (v:vs, fs, ds, ls) = Just (VanillaReg v, (vs, fs, ds, ls))
429 assign_reg NonPtrArg (v:vs, fs, ds, ls) = Just (VanillaReg v, (vs, fs, ds, ls))
430 -- PtrArg and NonPtrArg both go in a vanilla register
431 assign_reg other not_enough_regs = Nothing
434 -------------------------------------------------------------------------
438 -------------------------------------------------------------------------
440 -- Vanilla registers can contain pointers, Ints, Chars.
441 -- Floats and doubles have separate register supplies.
443 -- We take these register supplies from the *real* registers, i.e. those
444 -- that are guaranteed to map to machine registers.
446 useVanillaRegs | opt_Unregisterised = 0
447 | otherwise = mAX_Real_Vanilla_REG
448 useFloatRegs | opt_Unregisterised = 0
449 | otherwise = mAX_Real_Float_REG
450 useDoubleRegs | opt_Unregisterised = 0
451 | otherwise = mAX_Real_Double_REG
452 useLongRegs | opt_Unregisterised = 0
453 | otherwise = mAX_Real_Long_REG
455 vanillaRegNos, floatRegNos, doubleRegNos, longRegNos :: [Int]
456 vanillaRegNos = regList useVanillaRegs
457 floatRegNos = regList useFloatRegs
458 doubleRegNos = regList useDoubleRegs
459 longRegNos = regList useLongRegs
461 allVanillaRegNos, allFloatRegNos, allDoubleRegNos, allLongRegNos :: [Int]
462 allVanillaRegNos = regList mAX_Vanilla_REG
463 allFloatRegNos = regList mAX_Float_REG
464 allDoubleRegNos = regList mAX_Double_REG
465 allLongRegNos = regList mAX_Long_REG
470 type AvailRegs = ( [Int] -- available vanilla regs.
473 , [Int] -- longs (int64 and word64)
476 mkRegTbl :: [GlobalReg] -> AvailRegs
478 = mkRegTbl' regs_in_use vanillaRegNos floatRegNos doubleRegNos longRegNos
480 mkRegTbl_allRegs :: [GlobalReg] -> AvailRegs
481 mkRegTbl_allRegs regs_in_use
482 = mkRegTbl' regs_in_use allVanillaRegNos allFloatRegNos allDoubleRegNos allLongRegNos
484 mkRegTbl' regs_in_use vanillas floats doubles longs
485 = (ok_vanilla, ok_float, ok_double, ok_long)
487 ok_vanilla = mapCatMaybes (select VanillaReg) vanillas
488 ok_float = mapCatMaybes (select FloatReg) floats
489 ok_double = mapCatMaybes (select DoubleReg) doubles
490 ok_long = mapCatMaybes (select LongReg) longs
491 -- rep isn't looked at, hence we can use any old rep.
493 select :: (Int -> GlobalReg) -> Int{-cand-} -> Maybe Int
494 -- one we've unboxed the Int, we make a GlobalReg
495 -- and see if it is already in use; if not, return its number.
497 select mk_reg_fun cand
499 reg = mk_reg_fun cand
501 if reg `not_elem` regs_in_use
505 not_elem = isn'tIn "mkRegTbl"