2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: AbsCSyn.lhs,v 1.40 2001/11/23 11:58:00 simonmar Exp $
6 \section[AbstractC]{Abstract C: the last stop before machine code}
8 This ``Abstract C'' data type describes the raw Spineless Tagless
9 machine model at a C-ish level; it is ``abstract'' in that it only
10 includes C-like structures that we happen to need. The conversion of
11 programs from @StgSyntax@ (basically a functional language) to
12 @AbstractC@ (basically imperative C) is the heart of code generation.
13 From @AbstractC@, one may convert to real C (for portability) or to
14 raw assembler/machine code.
25 mkAbstractCs, mkAbsCStmts, mkAlgAltsCSwitch,
34 MagicId(..), node, infoptr,
39 #include "HsVersions.h"
41 import {-# SOURCE #-} ClosureInfo ( ClosureInfo )
44 import Constants ( mAX_Vanilla_REG, mAX_Float_REG,
45 mAX_Double_REG, spRelToInt )
46 import CostCentre ( CostCentre, CostCentreStack )
47 import Literal ( mkMachInt, Literal(..) )
48 import ForeignCall ( CCallSpec )
49 import PrimRep ( PrimRep(..) )
50 import Unique ( Unique )
51 import StgSyn ( StgOp )
52 import TyCon ( TyCon )
53 import BitSet -- for liveness masks
58 @AbstractC@ is a list of Abstract~C statements, but the data structure
59 is tree-ish, for easier and more efficient putting-together.
65 | AbsCStmts AbstractC AbstractC
67 -- and the individual stmts...
70 A note on @CAssign@: In general, the type associated with an assignment
71 is the type of the lhs. However, when the lhs is a pointer to mixed
72 types (e.g. SpB relative), the type of the assignment is the type of
73 the rhs for float types, or the generic StgWord for all other types.
74 (In particular, a CharRep on the rhs is promoted to IntRep when
75 stored in a mixed type location.)
83 CAddrMode -- Put this in the program counter
84 -- eg `CJump (CReg (VanillaReg PtrRep 1))' puts Ret1 in PC
85 -- Enter can be done by:
86 -- CJump (CVal NodeRel zeroOff)
89 CAddrMode -- Fall through into this routine
90 -- (for the benefit of the native code generators)
91 -- Equivalent to CJump in C land
93 | CReturn -- Perform a return
94 CAddrMode -- Address of a RET_<blah> info table
95 ReturnInfo -- Whether it's a direct or vectored return
98 [(Literal, AbstractC)] -- alternatives
99 AbstractC -- default; if there is no real Abstract C in here
100 -- (e.g., all comments; see function "nonemptyAbsC"),
101 -- then that means the default _cannot_ occur.
102 -- If there is only one alternative & no default code,
103 -- then there is no need to check the tag.
105 -- CSwitch m [(tag,code)] AbsCNop == code
107 | CCodeBlock CLabel AbstractC
108 -- A labelled block of code; this "statement" is not
109 -- executed; rather, the labelled code will be hoisted
110 -- out to the top level (out of line) & it can be
113 | CInitHdr -- to initialise the header of a closure (both fixed/var parts)
115 CAddrMode -- address of the info ptr
116 CAddrMode -- cost centre to place in closure
117 -- CReg CurCostCentre or CC_HDR(R1.p{-Node-})
118 Int -- size of closure, for profiling
121 [CAddrMode] -- Results
123 [CAddrMode] -- Arguments
124 [MagicId] -- Potentially volatile/live registers
125 -- (to save/restore around the call/op)
127 -- INVARIANT: When a PrimOp which can cause GC is used, the
128 -- only live data is tidily on the STG stacks or in the STG
129 -- registers (the code generator ensures this).
131 -- Why this? Because if the arguments were arbitrary
132 -- addressing modes, they might be things like (Hp+6) which
133 -- will get utterly spongled by GC.
135 | CSimultaneous -- Perform simultaneously all the statements
136 AbstractC -- in the nested AbstractC. They are only
137 -- allowed to be CAssigns, COpStmts and AbsCNops, so the
138 -- "simultaneous" part just concerns making
139 -- sure that permutations work.
140 -- For example { a := b, b := a }
141 -- needs to go via (at least one) temporary
143 | CCheck -- heap or stack checks, or both.
144 CCheckMacro -- These might include some code to fill in tags
145 [CAddrMode] -- on the stack, so we can't use CMacroStmt below.
148 | CRetDirect -- Direct return
149 !Unique -- for making labels
150 AbstractC -- return code
152 Liveness -- stack liveness at the return point
154 -- see the notes about these next few; they follow below...
155 | CMacroStmt CStmtMacro [CAddrMode]
156 | CCallProfCtrMacro FAST_STRING [CAddrMode]
157 | CCallProfCCMacro FAST_STRING [CAddrMode]
159 {- The presence of this constructor is a makeshift solution;
160 it being used to work around a gcc-related problem of
161 handling typedefs within statement blocks (or, rather,
162 the inability to do so.)
164 The AbstractC flattener takes care of lifting out these
165 typedefs if needs be (i.e., when generating .hc code and
166 compiling 'foreign import dynamic's)
168 | CCallTypedef Bool {- True => use "typedef"; False => use "extern"-}
169 CCallSpec Unique [CAddrMode] [CAddrMode]
171 -- *** the next three [or so...] are DATA (those above are CODE) ***
174 CLabel -- The (full, not base) label to use for labelling the closure.
176 CAddrMode -- cost centre identifier to place in closure
177 [CAddrMode] -- free vars; ptrs, then non-ptrs.
179 | CSRT CLabel [CLabel] -- SRT declarations: basically an array of
180 -- pointers to static closures.
182 | CBitmap CLabel LivenessMask -- A bitmap to be emitted if and only if
183 -- it is larger than a target machine word.
185 | CClosureInfoAndCode
186 ClosureInfo -- Explains placement and layout of closure
187 AbstractC -- Slow entry point code
189 -- Fast entry point code, if any
190 String -- Closure description; NB we can't get this
191 -- from ClosureInfo, because the latter refers
192 -- to the *right* hand side of a defn, whereas
193 -- the "description" refers to *left* hand side
195 | CRetVector -- A labelled block of static data
199 Liveness -- stack liveness at the return point
201 | CClosureTbl -- table of constructors for enumerated types
202 TyCon -- which TyCon this table is for
204 | CModuleInitBlock -- module initialisation block
205 CLabel -- label for init block
206 AbstractC -- initialisation code
208 | CCostCentreDecl -- A cost centre *declaration*
209 Bool -- True <=> local => full declaration
210 -- False <=> extern; just say so
213 | CCostCentreStackDecl -- A cost centre stack *declaration*
214 CostCentreStack -- this is the declaration for a
215 -- pre-defined singleton CCS (see
218 | CSplitMarker -- Split into separate object modules here
220 -- C_SRT is what StgSyn.SRT gets translated to...
221 -- we add a label for the table, and expect only the 'offset/length' form
224 | C_SRT CLabel !Int{-offset-} !Int{-length-}
226 needsSRT :: C_SRT -> Bool
227 needsSRT NoC_SRT = False
228 needsSRT (C_SRT _ _ _) = True
231 About @CMacroStmt@, etc.: notionally, they all just call some
232 arbitrary C~macro or routine, passing the @CAddrModes@ as arguments.
233 However, we distinguish between various flavours of these things,
234 mostly just to keep things somewhat less wild and wooly.
238 Some {\em essential} bits of the STG execution model are done with C
239 macros. An example is @STK_CHK@, which checks for stack-space
240 overflow. This enumeration type lists all such macros:
243 = ARGS_CHK -- arg satisfaction check
244 | ARGS_CHK_LOAD_NODE -- arg check for top-level functions
245 | UPD_CAF -- update CAF closure with indirection
246 | UPD_BH_UPDATABLE -- eager backholing
247 | UPD_BH_SINGLE_ENTRY -- more eager blackholing
248 | PUSH_UPD_FRAME -- push update frame
249 | PUSH_SEQ_FRAME -- push seq frame
250 | UPDATE_SU_FROM_UPD_FRAME -- pull Su out of the update frame
251 | SET_TAG -- set TagReg if it exists
253 | REGISTER_FOREIGN_EXPORT -- register a foreign exported fun
254 | REGISTER_IMPORT -- register an imported module
255 | REGISTER_DIMPORT -- register an imported module from
258 | GRAN_FETCH -- for GrAnSim only -- HWL
259 | GRAN_RESCHEDULE -- for GrAnSim only -- HWL
260 | GRAN_FETCH_AND_RESCHEDULE -- for GrAnSim only -- HWL
261 | THREAD_CONTEXT_SWITCH -- for GrAnSim only -- HWL
262 | GRAN_YIELD -- for GrAnSim only -- HWL
265 Heap/Stack checks. There are far too many of these.
270 = HP_CHK_NP -- heap/stack checks when
271 | STK_CHK_NP -- node points to the closure
273 | HP_CHK_SEQ_NP -- for 'seq' style case alternatives
275 | HP_CHK -- heap/stack checks when
276 | STK_CHK -- node doesn't point
278 -- case alternative heap checks:
280 | HP_CHK_NOREGS -- no registers live
281 | HP_CHK_UNPT_R1 -- R1 is boxed/unlifted
282 | HP_CHK_UNBX_R1 -- R1 is unboxed
283 | HP_CHK_F1 -- FloatReg1 (only) is live
284 | HP_CHK_D1 -- DblReg1 (only) is live
285 | HP_CHK_L1 -- LngReg1 (only) is live
286 | HP_CHK_UT_ALT -- unboxed tuple return.
288 | HP_CHK_GEN -- generic heap check
291 \item[@CCallProfCtrMacro@:]
292 The @String@ names a macro that, if \tr{#define}d, will bump one/some
293 of the STG-event profiling counters.
295 \item[@CCallProfCCMacro@:]
296 The @String@ names a macro that, if \tr{#define}d, will perform some
297 cost-centre-profiling-related action.
300 %************************************************************************
302 \subsection[CAddrMode]{C addressing modes}
304 %************************************************************************
308 = CVal RegRelative PrimRep
309 -- On RHS of assign: Contents of Magic[n]
310 -- On LHS of assign: location Magic[n]
311 -- (ie at addr Magic+n)
314 -- On RHS of assign: Address of Magic[n]; ie Magic+n
315 -- n=0 gets the Magic location itself
316 -- (NB: n=0 case superceded by CReg)
317 -- On LHS of assign: only sensible if n=0,
318 -- which gives the magic location itself
319 -- (NB: superceded by CReg)
321 | CReg MagicId -- To replace (CAddr MagicId 0)
323 | CTemp !Unique !PrimRep -- Temporary locations
324 -- ``Temporaries'' correspond to local variables in C, and registers in
327 | CLbl CLabel -- Labels in the runtime system, etc.
328 PrimRep -- the kind is so we can generate accurate C decls
330 | CCharLike CAddrMode -- The address of a static char-like closure for
331 -- the specified character. It is guaranteed to be in
332 -- the range mIN_CHARLIKE..mAX_CHARLIKE
334 | CIntLike CAddrMode -- The address of a static int-like closure for the
335 -- specified small integer. It is guaranteed to be in
336 -- the range mIN_INTLIKE..mAX_INTLIKE
340 | CJoinPoint -- This is used as the amode of a let-no-escape-bound
342 VirtualSpOffset -- Sp value after any volatile free vars
343 -- of the rhs have been saved on stack.
344 -- Just before the code for the thing is jumped to,
345 -- Sp will be set to this value,
346 -- and then any stack-passed args pushed,
347 -- then the code for this thing will be entered
349 !PrimRep -- the kind of the result
350 CExprMacro -- the macro to generate a value
351 [CAddrMode] -- and its arguments
354 Various C macros for values which are dependent on the back-end layout.
360 | ARG_TAG -- stack argument tagging
361 | GET_TAG -- get current constructor tag
367 Convenience functions:
370 mkIntCLit :: Int -> CAddrMode
371 mkIntCLit i = CLit (mkMachInt (toInteger i))
373 mkCString :: FAST_STRING -> CAddrMode
374 mkCString s = CLit (MachStr s)
376 mkCCostCentre :: CostCentre -> CAddrMode
377 mkCCostCentre cc = CLbl (mkCC_Label cc) DataPtrRep
379 mkCCostCentreStack :: CostCentreStack -> CAddrMode
380 mkCCostCentreStack ccs = CLbl (mkCCS_Label ccs) DataPtrRep
383 %************************************************************************
385 \subsection[RegRelative]{@RegRelatives@: ???}
387 %************************************************************************
392 | SpRel FastInt -- }- offsets in StgWords
393 | NodeRel FastInt -- }
394 | CIndex CAddrMode CAddrMode PrimRep -- pointer arithmetic :-)
395 -- CIndex a b k === (k*)a[b]
398 = DirectReturn -- Jump directly, if possible
399 | StaticVectoredReturn Int -- Fixed tag, starting at zero
400 | DynamicVectoredReturn CAddrMode -- Dynamic tag given by amode, starting at zero
402 hpRel :: VirtualHeapOffset -- virtual offset of Hp
403 -> VirtualHeapOffset -- virtual offset of The Thing
404 -> RegRelative -- integer offset
405 hpRel hp off = HpRel (iUnbox (hp - off))
407 spRel :: VirtualSpOffset -- virtual offset of Sp
408 -> VirtualSpOffset -- virtual offset of The Thing
409 -> RegRelative -- integer offset
410 spRel sp off = SpRel (iUnbox (spRelToInt sp off))
412 nodeRel :: VirtualHeapOffset
414 nodeRel off = NodeRel (iUnbox off)
418 %************************************************************************
420 \subsection[Liveness]{Liveness Masks}
422 %************************************************************************
424 We represent liveness bitmaps as a BitSet (whose internal
425 representation really is a bitmap). These are pinned onto case return
426 vectors to indicate the state of the stack for the garbage collector.
428 In the compiled program, liveness bitmaps that fit inside a single
429 word (StgWord) are stored as a single word, while larger bitmaps are
430 stored as a pointer to an array of words. When we compile via C
431 (especially when we bootstrap via HC files), we generate identical C
432 code regardless of whether words are 32- or 64-bit on the target
433 machine, by postponing the decision of how to store each liveness
434 bitmap to C compilation time (or rather, C preprocessing time).
437 type LivenessMask = [BitSet]
439 data Liveness = Liveness CLabel LivenessMask
442 %************************************************************************
444 \subsection[HeapOffset]{@Heap Offsets@}
446 %************************************************************************
448 This used to be a grotesquely complicated datatype in an attempt to
449 hide the details of header sizes from the compiler itself. Now these
450 constants are imported from the RTS, and we deal in real Ints.
453 type HeapOffset = Int -- ToDo: remove
455 type VirtualHeapOffset = HeapOffset
456 type VirtualSpOffset = Int
458 type HpRelOffset = HeapOffset
459 type SpRelOffset = Int
462 %************************************************************************
464 \subsection[MagicId]{@MagicIds@: registers and such}
466 %************************************************************************
470 = BaseReg -- mentioned only in nativeGen
472 -- Argument and return registers
473 | VanillaReg -- pointers, unboxed ints and chars
475 FastInt -- its number (1 .. mAX_Vanilla_REG)
477 | FloatReg -- single-precision floating-point registers
478 FastInt -- its number (1 .. mAX_Float_REG)
480 | DoubleReg -- double-precision floating-point registers
481 FastInt -- its number (1 .. mAX_Double_REG)
484 | Sp -- Stack ptr; points to last occupied stack location.
485 | Su -- Stack update frame pointer
486 | SpLim -- Stack limit
487 | Hp -- Heap ptr; points to last occupied heap location.
488 | HpLim -- Heap limit register
489 | CurCostCentre -- current cost centre register.
490 | VoidReg -- see "VoidPrim" type; just a placeholder;
491 -- no actual register
492 | LongReg -- long int registers (64-bit, really)
493 PrimRep -- Int64Rep or Word64Rep
494 FastInt -- its number (1 .. mAX_Long_REG)
496 | CurrentTSO -- pointer to current thread's TSO
497 | CurrentNursery -- pointer to allocation area
498 | HpAlloc -- allocation count for heap check failure
501 node = VanillaReg PtrRep (_ILIT 1) -- A convenient alias for Node
502 tagreg = VanillaReg WordRep (_ILIT 2) -- A convenient alias for TagReg
507 We need magical @Eq@ because @VanillaReg@s come in multiple flavors.
510 instance Eq MagicId where
511 reg1 == reg2 = tag reg1 ==# tag reg2
513 tag BaseReg = (_ILIT(0) :: FastInt)
519 tag CurCostCentre = _ILIT(6)
520 tag VoidReg = _ILIT(7)
522 tag (VanillaReg _ i) = _ILIT(8) +# i
524 tag (FloatReg i) = _ILIT(8) +# maxv +# i
525 tag (DoubleReg i) = _ILIT(8) +# maxv +# maxf +# i
526 tag (LongReg _ i) = _ILIT(8) +# maxv +# maxf +# maxd +# i
528 maxv = iUnbox mAX_Vanilla_REG
529 maxf = iUnbox mAX_Float_REG
530 maxd = iUnbox mAX_Double_REG
533 Returns True for any register that {\em potentially} dies across
534 C calls (or anything near equivalent). We just say @True@ and
535 let the (machine-specific) registering macros sort things out...
538 isVolatileReg :: MagicId -> Bool
539 isVolatileReg any = True