2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 % $Id: AbsCSyn.lhs,v 1.47 2002/04/29 14:03:39 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 MachOp ( MachOp(..) )
51 import Unique ( Unique )
52 import StgSyn ( StgOp )
53 import TyCon ( TyCon )
54 import BitSet -- for liveness masks
59 @AbstractC@ is a list of Abstract~C statements, but the data structure
60 is tree-ish, for easier and more efficient putting-together.
66 | AbsCStmts AbstractC AbstractC
68 -- and the individual stmts...
71 A note on @CAssign@: In general, the type associated with an assignment
72 is the type of the lhs. However, when the lhs is a pointer to mixed
73 types (e.g. SpB relative), the type of the assignment is the type of
74 the rhs for float types, or the generic StgWord for all other types.
75 (In particular, a CharRep on the rhs is promoted to IntRep when
76 stored in a mixed type location.)
84 CAddrMode -- Put this in the program counter
85 -- eg `CJump (CReg (VanillaReg PtrRep 1))' puts Ret1 in PC
86 -- Enter can be done by:
87 -- CJump (CVal NodeRel zeroOff)
90 CAddrMode -- Fall through into this routine
91 -- (for the benefit of the native code generators)
92 -- Equivalent to CJump in C land
94 | CReturn -- Perform a return
95 CAddrMode -- Address of a RET_<blah> info table
96 ReturnInfo -- Whether it's a direct or vectored return
99 [(Literal, AbstractC)] -- alternatives
100 AbstractC -- default; if there is no real Abstract C in here
101 -- (e.g., all comments; see function "nonemptyAbsC"),
102 -- then that means the default _cannot_ occur.
103 -- If there is only one alternative & no default code,
104 -- then there is no need to check the tag.
106 -- CSwitch m [(tag,code)] AbsCNop == code
108 | CCodeBlock CLabel AbstractC
109 -- A labelled block of code; this "statement" is not
110 -- executed; rather, the labelled code will be hoisted
111 -- out to the top level (out of line) & it can be
114 | CInitHdr -- to initialise the header of a closure (both fixed/var parts)
116 CAddrMode -- address of the info ptr
117 !CAddrMode -- cost centre to place in closure
118 -- CReg CurCostCentre or CC_HDR(R1.p{-Node-})
119 Int -- size of closure, for profiling
121 -- NEW CASES FOR EXPANDED PRIMOPS
123 | CMachOpStmt -- Machine-level operation
126 [CAddrMode] -- Arguments
127 (Maybe [MagicId]) -- list of regs which need to be preserved
128 -- across the primop. This is allowed to be Nothing only if
129 -- machOpIsDefinitelyInline returns True. And that in turn may
130 -- only return True if we are absolutely sure that the mach op
131 -- can be done inline on all platforms.
133 | CSequential -- Do the nested AbstractCs sequentially.
134 [AbstractC] -- In particular, as far as the AbsCUtils.doSimultaneously
135 -- is concerned, these stmts are to be treated as atomic
136 -- and are not to be reordered.
138 -- end of NEW CASES FOR EXPANDED PRIMOPS
141 [CAddrMode] -- Results
143 [CAddrMode] -- Arguments
144 [MagicId] -- Potentially volatile/live registers
145 -- (to save/restore around the call/op)
147 -- INVARIANT: When a PrimOp which can cause GC is used, the
148 -- only live data is tidily on the STG stacks or in the STG
149 -- registers (the code generator ensures this).
151 -- Why this? Because if the arguments were arbitrary
152 -- addressing modes, they might be things like (Hp+6) which
153 -- will get utterly spongled by GC.
155 | CSimultaneous -- Perform simultaneously all the statements
156 AbstractC -- in the nested AbstractC. They are only
157 -- allowed to be CAssigns, COpStmts and AbsCNops, so the
158 -- "simultaneous" part just concerns making
159 -- sure that permutations work.
160 -- For example { a := b, b := a }
161 -- needs to go via (at least one) temporary
163 | CCheck -- heap or stack checks, or both.
164 CCheckMacro -- These might include some code to fill in tags
165 [CAddrMode] -- on the stack, so we can't use CMacroStmt below.
168 | CRetDirect -- Direct return
169 !Unique -- for making labels
170 AbstractC -- return code
172 Liveness -- stack liveness at the return point
174 -- see the notes about these next few; they follow below...
175 | CMacroStmt CStmtMacro [CAddrMode]
176 | CCallProfCtrMacro FastString [CAddrMode]
177 | CCallProfCCMacro FastString [CAddrMode]
179 {- The presence of this constructor is a makeshift solution;
180 it being used to work around a gcc-related problem of
181 handling typedefs within statement blocks (or, rather,
182 the inability to do so.)
184 The AbstractC flattener takes care of lifting out these
185 typedefs if needs be (i.e., when generating .hc code and
186 compiling 'foreign import dynamic's)
188 | CCallTypedef Bool {- True => use "typedef"; False => use "extern"-}
189 CCallSpec Unique [CAddrMode] [CAddrMode]
191 -- *** the next three [or so...] are DATA (those above are CODE) ***
194 ClosureInfo -- Todo: maybe info_lbl & closure_lbl instead?
195 CAddrMode -- cost centre identifier to place in closure
196 [CAddrMode] -- free vars; ptrs, then non-ptrs.
198 | CSRT CLabel [CLabel] -- SRT declarations: basically an array of
199 -- pointers to static closures.
201 | CBitmap CLabel LivenessMask -- A bitmap to be emitted if and only if
202 -- it is larger than a target machine word.
204 | CClosureInfoAndCode
205 ClosureInfo -- Explains placement and layout of closure
206 AbstractC -- Slow entry point code
208 -- Fast entry point code, if any
209 String -- Closure description; NB we can't get this
210 -- from ClosureInfo, because the latter refers
211 -- to the *right* hand side of a defn, whereas
212 -- the "description" refers to *left* hand side
214 | CRetVector -- A labelled block of static data
218 Liveness -- stack liveness at the return point
220 | CClosureTbl -- table of constructors for enumerated types
221 TyCon -- which TyCon this table is for
223 | CModuleInitBlock -- module initialisation block
224 CLabel -- label for init block
225 AbstractC -- initialisation code
227 | CCostCentreDecl -- A cost centre *declaration*
228 Bool -- True <=> local => full declaration
229 -- False <=> extern; just say so
232 | CCostCentreStackDecl -- A cost centre stack *declaration*
233 CostCentreStack -- this is the declaration for a
234 -- pre-defined singleton CCS (see
237 | CSplitMarker -- Split into separate object modules here
239 -- C_SRT is what StgSyn.SRT gets translated to...
240 -- we add a label for the table, and expect only the 'offset/length' form
243 | C_SRT CLabel !Int{-offset-} !Int{-length-}
245 needsSRT :: C_SRT -> Bool
246 needsSRT NoC_SRT = False
247 needsSRT (C_SRT _ _ _) = True
250 About @CMacroStmt@, etc.: notionally, they all just call some
251 arbitrary C~macro or routine, passing the @CAddrModes@ as arguments.
252 However, we distinguish between various flavours of these things,
253 mostly just to keep things somewhat less wild and wooly.
257 Some {\em essential} bits of the STG execution model are done with C
258 macros. An example is @STK_CHK@, which checks for stack-space
259 overflow. This enumeration type lists all such macros:
262 = ARGS_CHK -- arg satisfaction check
263 | ARGS_CHK_LOAD_NODE -- arg check for top-level functions
264 | UPD_CAF -- update CAF closure with indirection
265 | UPD_BH_UPDATABLE -- eager backholing
266 | UPD_BH_SINGLE_ENTRY -- more eager blackholing
267 | PUSH_UPD_FRAME -- push update frame
268 | PUSH_SEQ_FRAME -- push seq frame
269 | UPDATE_SU_FROM_UPD_FRAME -- pull Su out of the update frame
270 | SET_TAG -- set TagReg if it exists
271 -- dataToTag# primop -- *only* used in unregisterised builds.
272 -- (see AbsCUtils.dsCOpStmt)
275 | REGISTER_FOREIGN_EXPORT -- register a foreign exported fun
276 | REGISTER_IMPORT -- register an imported module
277 | REGISTER_DIMPORT -- register an imported module from
280 | GRAN_FETCH -- for GrAnSim only -- HWL
281 | GRAN_RESCHEDULE -- for GrAnSim only -- HWL
282 | GRAN_FETCH_AND_RESCHEDULE -- for GrAnSim only -- HWL
283 | THREAD_CONTEXT_SWITCH -- for GrAnSim only -- HWL
284 | GRAN_YIELD -- for GrAnSim only -- HWL
287 Heap/Stack checks. There are far too many of these.
292 = HP_CHK_NP -- heap/stack checks when
293 | STK_CHK_NP -- node points to the closure
295 | HP_CHK_SEQ_NP -- for 'seq' style case alternatives
297 | HP_CHK -- heap/stack checks when
298 | STK_CHK -- node doesn't point
300 -- case alternative heap checks:
302 | HP_CHK_NOREGS -- no registers live
303 | HP_CHK_UNPT_R1 -- R1 is boxed/unlifted
304 | HP_CHK_UNBX_R1 -- R1 is unboxed
305 | HP_CHK_F1 -- FloatReg1 (only) is live
306 | HP_CHK_D1 -- DblReg1 (only) is live
307 | HP_CHK_L1 -- LngReg1 (only) is live
308 | HP_CHK_UT_ALT -- unboxed tuple return.
310 | HP_CHK_GEN -- generic heap check
313 \item[@CCallProfCtrMacro@:]
314 The @String@ names a macro that, if \tr{#define}d, will bump one/some
315 of the STG-event profiling counters.
317 \item[@CCallProfCCMacro@:]
318 The @String@ names a macro that, if \tr{#define}d, will perform some
319 cost-centre-profiling-related action.
322 %************************************************************************
324 \subsection[CAddrMode]{C addressing modes}
326 %************************************************************************
330 = CVal RegRelative PrimRep
331 -- On RHS of assign: Contents of Magic[n]
332 -- On LHS of assign: location Magic[n]
333 -- (ie at addr Magic+n)
336 -- On RHS of assign: Address of Magic[n]; ie Magic+n
337 -- n=0 gets the Magic location itself
338 -- (NB: n=0 case superceded by CReg)
339 -- On LHS of assign: only sensible if n=0,
340 -- which gives the magic location itself
341 -- (NB: superceded by CReg)
343 -- JRS 2002-02-05: CAddr is really scummy and should be fixed.
344 -- The effect is that the semantics of CAddr depend on what the
345 -- contained RegRelative is; it is decidely non-orthogonal.
347 | CReg MagicId -- To replace (CAddr MagicId 0)
349 | CTemp !Unique !PrimRep -- Temporary locations
350 -- ``Temporaries'' correspond to local variables in C, and registers in
353 | CLbl CLabel -- Labels in the runtime system, etc.
354 PrimRep -- the kind is so we can generate accurate C decls
356 | CCharLike CAddrMode -- The address of a static char-like closure for
357 -- the specified character. It is guaranteed to be in
358 -- the range mIN_CHARLIKE..mAX_CHARLIKE
360 | CIntLike CAddrMode -- The address of a static int-like closure for the
361 -- specified small integer. It is guaranteed to be in
362 -- the range mIN_INTLIKE..mAX_INTLIKE
366 | CJoinPoint -- This is used as the amode of a let-no-escape-bound
368 VirtualSpOffset -- Sp value after any volatile free vars
369 -- of the rhs have been saved on stack.
370 -- Just before the code for the thing is jumped to,
371 -- Sp will be set to this value,
372 -- and then any stack-passed args pushed,
373 -- then the code for this thing will be entered
375 !PrimRep -- the kind of the result
376 CExprMacro -- the macro to generate a value
377 [CAddrMode] -- and its arguments
379 | CBytesPerWord -- Word size, in bytes, on this platform
380 -- required for: half-word loads (used in fishing tags
381 -- out of info tables), and sizeofByteArray#.
384 Various C macros for values which are dependent on the back-end layout.
390 | ARG_TAG -- stack argument tagging
391 | GET_TAG -- get current constructor tag
397 Convenience functions:
400 mkIntCLit :: Int -> CAddrMode
401 mkIntCLit i = CLit (mkMachInt (toInteger i))
403 mkCString :: FastString -> CAddrMode
404 mkCString s = CLit (MachStr s)
406 mkCCostCentre :: CostCentre -> CAddrMode
407 mkCCostCentre cc = CLbl (mkCC_Label cc) DataPtrRep
409 mkCCostCentreStack :: CostCentreStack -> CAddrMode
410 mkCCostCentreStack ccs = CLbl (mkCCS_Label ccs) DataPtrRep
413 %************************************************************************
415 \subsection[RegRelative]{@RegRelatives@: ???}
417 %************************************************************************
422 | SpRel FastInt -- }- offsets in StgWords
423 | NodeRel FastInt -- }
424 | CIndex CAddrMode CAddrMode PrimRep -- pointer arithmetic :-)
425 -- CIndex a b k === (k*)a[b]
428 = DirectReturn -- Jump directly, if possible
429 | StaticVectoredReturn Int -- Fixed tag, starting at zero
430 | DynamicVectoredReturn CAddrMode -- Dynamic tag given by amode, starting at zero
432 hpRel :: VirtualHeapOffset -- virtual offset of Hp
433 -> VirtualHeapOffset -- virtual offset of The Thing
434 -> RegRelative -- integer offset
435 hpRel hp off = HpRel (iUnbox (hp - off))
437 spRel :: VirtualSpOffset -- virtual offset of Sp
438 -> VirtualSpOffset -- virtual offset of The Thing
439 -> RegRelative -- integer offset
440 spRel sp off = SpRel (iUnbox (spRelToInt sp off))
442 nodeRel :: VirtualHeapOffset
444 nodeRel off = NodeRel (iUnbox off)
448 %************************************************************************
450 \subsection[Liveness]{Liveness Masks}
452 %************************************************************************
454 We represent liveness bitmaps as a BitSet (whose internal
455 representation really is a bitmap). These are pinned onto case return
456 vectors to indicate the state of the stack for the garbage collector.
458 In the compiled program, liveness bitmaps that fit inside a single
459 word (StgWord) are stored as a single word, while larger bitmaps are
460 stored as a pointer to an array of words. When we compile via C
461 (especially when we bootstrap via HC files), we generate identical C
462 code regardless of whether words are 32- or 64-bit on the target
463 machine, by postponing the decision of how to store each liveness
464 bitmap to C compilation time (or rather, C preprocessing time).
467 type LivenessMask = [BitSet]
469 data Liveness = Liveness CLabel LivenessMask
472 %************************************************************************
474 \subsection[HeapOffset]{@Heap Offsets@}
476 %************************************************************************
478 This used to be a grotesquely complicated datatype in an attempt to
479 hide the details of header sizes from the compiler itself. Now these
480 constants are imported from the RTS, and we deal in real Ints.
483 type HeapOffset = Int -- ToDo: remove
485 type VirtualHeapOffset = HeapOffset
486 type VirtualSpOffset = Int
488 type HpRelOffset = HeapOffset
489 type SpRelOffset = Int
492 %************************************************************************
494 \subsection[MagicId]{@MagicIds@: registers and such}
496 %************************************************************************
500 = BaseReg -- mentioned only in nativeGen
502 -- Argument and return registers
503 | VanillaReg -- pointers, unboxed ints and chars
505 FastInt -- its number (1 .. mAX_Vanilla_REG)
507 | FloatReg -- single-precision floating-point registers
508 FastInt -- its number (1 .. mAX_Float_REG)
510 | DoubleReg -- double-precision floating-point registers
511 FastInt -- its number (1 .. mAX_Double_REG)
514 | Sp -- Stack ptr; points to last occupied stack location.
515 | Su -- Stack update frame pointer
516 | SpLim -- Stack limit
517 | Hp -- Heap ptr; points to last occupied heap location.
518 | HpLim -- Heap limit register
519 | CurCostCentre -- current cost centre register.
520 | VoidReg -- see "VoidPrim" type; just a placeholder;
521 -- no actual register
522 | LongReg -- long int registers (64-bit, really)
523 PrimRep -- Int64Rep or Word64Rep
524 FastInt -- its number (1 .. mAX_Long_REG)
526 | CurrentTSO -- pointer to current thread's TSO
527 | CurrentNursery -- pointer to allocation area
528 | HpAlloc -- allocation count for heap check failure
531 node = VanillaReg PtrRep (_ILIT 1) -- A convenient alias for Node
532 tagreg = VanillaReg WordRep (_ILIT 2) -- A convenient alias for TagReg
537 We need magical @Eq@ because @VanillaReg@s come in multiple flavors.
540 instance Eq MagicId where
541 reg1 == reg2 = tag reg1 ==# tag reg2
543 tag BaseReg = (_ILIT(0) :: FastInt)
549 tag CurCostCentre = _ILIT(6)
550 tag VoidReg = _ILIT(7)
552 tag (VanillaReg _ i) = _ILIT(8) +# i
554 tag (FloatReg i) = _ILIT(8) +# maxv +# i
555 tag (DoubleReg i) = _ILIT(8) +# maxv +# maxf +# i
556 tag (LongReg _ i) = _ILIT(8) +# maxv +# maxf +# maxd +# i
558 maxv = iUnbox mAX_Vanilla_REG
559 maxf = iUnbox mAX_Float_REG
560 maxd = iUnbox mAX_Double_REG
563 Returns True for any register that {\em potentially} dies across
564 C calls (or anything near equivalent). We just say @True@ and
565 let the (machine-specific) registering macros sort things out...
568 isVolatileReg :: MagicId -> Bool
569 isVolatileReg any = True