Remove CPP from nativeGen/RegAlloc/Graph/TrivColorable.hs
[ghc-hetmet.git] / compiler / codeGen / CgCon.lhs
index 0d8d731..8768008 100644 (file)
@@ -1,4 +1,5 @@
 %
+% (c) The University of Glasgow 2006
 % (c) The GRASP Project, Glasgow University, 1992-1998
 %
 \section[CgCon]{Code generation for constructors}
@@ -20,40 +21,34 @@ module CgCon (
 import CgMonad
 import StgSyn
 
-import CgBindery       ( getArgAmodes, bindNewToNode,
-                         bindArgsToRegs, idInfoToAmode, stableIdInfo,
-                         heapIdInfo, CgIdInfo, bindArgsToStack
-                       )
-import CgStackery      ( mkVirtStkOffsets, freeStackSlots,
-                         getRealSp, getVirtSp, setRealAndVirtualSp )
-import CgUtils         ( addIdReps, cmmLabelOffW, emitRODataLits, emitDataLits )
-import CgCallConv      ( assignReturnRegs )
-import CgHeapery       ( allocDynClosure, layOutDynConstr, 
-                         layOutStaticConstr, mkStaticClosureFields )
-import CgTailCall      ( performReturn, emitKnownConReturnCode, returnUnboxedTuple )
-import CgProf          ( mkCCostCentreStack, ldvEnter, curCCS )
+import CgBindery
+import CgStackery
+import CgUtils
+import CgCallConv
+import CgHeapery
+import CgTailCall
+import CgProf
 import CgTicky
-import CgInfoTbls      ( emitClosureCodeAndInfoTable, dataConTagZ )
+import CgInfoTbls
 import CLabel
-import ClosureInfo     ( mkConLFInfo, mkLFArgument )
-import CmmUtils                ( mkLblExpr )
-import Cmm
-import SMRep           ( WordOff, CgRep, separateByPtrFollowness,
-                         fixedHdrSize, typeCgRep )
-import CostCentre      ( currentOrSubsumedCCS, dontCareCCS, CostCentreStack,
-                         currentCCS )
-import Constants       ( mIN_INTLIKE, mAX_INTLIKE, mIN_CHARLIKE, mAX_CHARLIKE )
-import TyCon           ( TyCon, tyConDataCons, isEnumerationTyCon, tyConName )
-import DataCon         ( DataCon, dataConRepArgTys, isNullaryRepDataCon,
-                         isUnboxedTupleCon, dataConWorkId, 
-                         dataConName, dataConRepArity
-                       )
-import Id              ( Id, idName, isDeadBinder )
-import Type            ( Type )
-import PrelInfo                ( maybeCharLikeCon, maybeIntLikeCon )
+import ClosureInfo
+import OldCmmUtils
+import OldCmm
+import SMRep
+import CostCentre
+import Constants
+import TyCon
+import DataCon
+import Id
+import IdInfo
+import Type
+import PrelInfo
 import Outputable
-import Util            ( lengthIs )
-import ListSetOps      ( assocMaybe )
+import ListSetOps
+import Util
+import Module
+import FastString
+import StaticFlags
 \end{code}
 
 
@@ -70,10 +65,10 @@ cgTopRhsCon :: Id           -- Name of thing bound to this RHS
            -> FCode (Id, CgIdInfo)
 cgTopRhsCon id con args
   = do { 
-       ; hmods <- getHomeModules
 #if mingw32_TARGET_OS
         -- Windows DLLs have a problem with static cross-DLL refs.
-        ; ASSERT( not (isDllConApp hmods con args) ) return ()
+       ; this_pkg <- getThisPackage
+        ; ASSERT( not (isDllConApp this_pkg con args) ) return ()
 #endif
        ; ASSERT( args `lengthIs` dataConRepArity con ) return ()
 
@@ -83,9 +78,9 @@ cgTopRhsCon id con args
        ; let
            name          = idName id
            lf_info       = mkConLFInfo con
-           closure_label = mkClosureLabel hmods name
+           closure_label = mkClosureLabel name $ idCafInfo id
            caffy         = any stgArgHasCafRefs args
-           (closure_info, amodes_w_offsets) = layOutStaticConstr hmods con amodes
+           (closure_info, amodes_w_offsets) = layOutStaticConstr con amodes
            closure_rep = mkStaticClosureFields
                             closure_info
                             dontCareCCS                -- Because it's static data
@@ -102,7 +97,7 @@ cgTopRhsCon id con args
        ; emitDataLits closure_label closure_rep
 
                -- RETURN
-       ; returnFC (id, stableIdInfo id (mkLblExpr closure_label) lf_info) }
+       ; returnFC (id, taggedStableIdInfo id (mkLblExpr closure_label) lf_info con) }
 \end{code}
 
 %************************************************************************
@@ -141,11 +136,12 @@ which have exclusively size-zero (VoidRep) args, we generate no code
 at all.
 
 \begin{code}
-buildDynCon binder cc con []
-  = do hmods <- getHomeModules
-       returnFC (stableIdInfo binder
-                          (mkLblExpr (mkClosureLabel hmods (dataConName con)))
-                          (mkConLFInfo con))
+buildDynCon binder _ con []
+  = returnFC (taggedStableIdInfo binder
+                          (mkLblExpr (mkClosureLabel (dataConName con)
+                                      (idCafInfo binder)))
+                          (mkConLFInfo con)
+                           con)
 \end{code}
 
 The following three paragraphs about @Char@-like and @Int@-like
@@ -169,28 +165,40 @@ which is guaranteed in range.
 
 Because of this, we use can safely return an addressing mode.
 
+We don't support this optimisation when compiling into Windows DLLs yet
+because they don't support cross package data references well.
+
 \begin{code}
-buildDynCon binder cc con [arg_amode]
+
+
+buildDynCon binder _ con [arg_amode]
   | maybeIntLikeCon con 
+#if defined(mingw32_TARGET_OS)
+  , not opt_PIC
+#endif
   , (_, CmmLit (CmmInt val _)) <- arg_amode
   , let val_int = (fromIntegral val) :: Int
   , val_int <= mAX_INTLIKE && val_int >= mIN_INTLIKE
-  = do         { let intlike_lbl   = mkRtsDataLabel SLIT("stg_INTLIKE_closure")
+  = do         { let intlike_lbl   = mkCmmGcPtrLabel rtsPackageId (fsLit "stg_INTLIKE_closure")
              offsetW = (val_int - mIN_INTLIKE) * (fixedHdrSize + 1)
                -- INTLIKE closures consist of a header and one word payload
              intlike_amode = CmmLit (cmmLabelOffW intlike_lbl offsetW)
-       ; returnFC (stableIdInfo binder intlike_amode (mkConLFInfo con)) }
+       ; returnFC (taggedStableIdInfo binder intlike_amode (mkConLFInfo con) con) }
 
-buildDynCon binder cc con [arg_amode]
+buildDynCon binder _ con [arg_amode]
   | maybeCharLikeCon con 
+#if defined(mingw32_TARGET_OS)
+  , not opt_PIC
+#endif
   , (_, CmmLit (CmmInt val _)) <- arg_amode
   , let val_int = (fromIntegral val) :: Int
   , val_int <= mAX_CHARLIKE && val_int >= mIN_CHARLIKE
-  = do         { let charlike_lbl   = mkRtsDataLabel SLIT("stg_CHARLIKE_closure")
+  = do         { let charlike_lbl   = mkCmmGcPtrLabel rtsPackageId (fsLit "stg_CHARLIKE_closure")
              offsetW = (val_int - mIN_CHARLIKE) * (fixedHdrSize + 1)
                -- CHARLIKE closures consist of a header and one word payload
              charlike_amode = CmmLit (cmmLabelOffW charlike_lbl offsetW)
-       ; returnFC (stableIdInfo binder charlike_amode (mkConLFInfo con)) }
+       ; returnFC (taggedStableIdInfo binder charlike_amode (mkConLFInfo con) con) }
+
 \end{code}
 
 Now the general case.
@@ -198,12 +206,11 @@ Now the general case.
 \begin{code}
 buildDynCon binder ccs con args
   = do { 
-       ; hmods <- getHomeModules
        ; let
-           (closure_info, amodes_w_offsets) = layOutDynConstr hmods con args
+           (closure_info, amodes_w_offsets) = layOutDynConstr con args
 
        ; hp_off <- allocDynClosure closure_info use_cc blame_cc amodes_w_offsets
-       ; returnFC (heapIdInfo binder hp_off lf_info) }
+       ; returnFC (taggedHeapIdInfo binder hp_off lf_info con) }
   where
     lf_info = mkConLFInfo con
 
@@ -230,10 +237,12 @@ found a $con$.
 \begin{code}
 bindConArgs :: DataCon -> [Id] -> Code
 bindConArgs con args
-  = do hmods <- getHomeModules
+  = do
        let
-         bind_arg (arg, offset) = bindNewToNode arg offset (mkLFArgument arg)
-         (_, args_w_offsets)    = layOutDynConstr hmods con (addIdReps args)
+          -- The binding below forces the masking out of the tag bits
+          -- when accessing the constructor field.
+         bind_arg (arg, offset) = bindNewToUntagNode arg offset (mkLFArgument arg) (tagForCon con)
+         (_, args_w_offsets)    = layOutDynConstr con (addIdReps args)
        --
        ASSERT(not (isUnboxedTupleCon con)) return ()
        mapCs bind_arg args_w_offsets
@@ -301,10 +310,15 @@ sure the @amodes@ passed don't conflict with each other.
 cgReturnDataCon :: DataCon -> [(CgRep, CmmExpr)] -> Code
 
 cgReturnDataCon con amodes
+  | isUnboxedTupleCon con = returnUnboxedTuple amodes
+      -- when profiling we can't shortcut here, we have to enter the closure
+      -- for it to be marked as "used" for LDV profiling.
+  | opt_SccProfilingOn    = build_it_then enter_it
+  | otherwise
   = ASSERT( amodes `lengthIs` dataConRepArity con )
     do { EndOfBlockInfo _ sequel <- getEndOfBlockInfo
        ; case sequel of
-           CaseAlts _ (Just (alts, deflt_lbl)) bndr _ 
+           CaseAlts _ (Just (alts, deflt_lbl)) bndr
              ->    -- Ho! We know the constructor so we can
                    -- go straight to the right alternative
                 case assocMaybe alts (dataConTagZ con) of {
@@ -324,11 +338,12 @@ cgReturnDataCon con amodes
                        | isDeadBinder bndr -> performReturn (jump_to deflt_lbl)
                        | otherwise         -> build_it_then (jump_to deflt_lbl) }
     
-           other_sequel        -- The usual case
-             | isUnboxedTupleCon con -> returnUnboxedTuple amodes
-              | otherwise -> build_it_then (emitKnownConReturnCode con)
+           _otherwise  -- The usual case
+              -> build_it_then emitReturnInstr
        }
   where
+    enter_it    = stmtsC [ CmmAssign nodeReg (cmmUntag (CmmReg nodeReg)),
+                           CmmJump (entryCode (closureInfoPtr (CmmReg nodeReg))) [] ]
     jump_to lbl = stmtC (CmmJump (CmmLit lbl) [])
     build_it_then return_code
       = do {   -- BUILD THE OBJECT IN THE HEAP
@@ -395,11 +410,14 @@ cgTyCon tycon
            -- Put the table after the data constructor decls, because the
            -- datatype closure table (for enumeration types)
            -- to (say) PrelBase_$wTrue_closure, which is defined in code_stuff
+            -- Note that the closure pointers are tagged.
+
+            -- XXX comment says to put table after constructor decls, but
+            -- code appears to put it before --- NR 16 Aug 2007
        ; extra <- 
           if isEnumerationTyCon tycon then do
-               tbl <- getCmm (emitRODataLits (mkLocalClosureTableLabel 
-                                               (tyConName tycon))
-                          [ CmmLabel (mkLocalClosureLabel (dataConName con))
+               tbl <- getCmm (emitRODataLits "cgTyCon" (mkLocalClosureTableLabel (tyConName tycon) NoCafRefs)
+                          [ CmmLabelOff (mkLocalClosureLabel (dataConName con) NoCafRefs) (tagForCon con)
                           | con <- tyConDataCons tycon])
                return [tbl]
           else
@@ -416,7 +434,6 @@ static closure, for a constructor.
 cgDataCon :: DataCon -> Code
 cgDataCon data_con
   = do {     -- Don't need any dynamic closure code for zero-arity constructors
-         hmods <- getHomeModules
 
        ; let
            -- To allow the debuggers, interpreters, etc to cope with
@@ -424,16 +441,16 @@ cgDataCon data_con
            -- time), we take care that info-table contains the
            -- information we need.
            (static_cl_info, _) = 
-               layOutStaticConstr hmods data_con arg_reps
+               layOutStaticConstr data_con arg_reps
 
            (dyn_cl_info, arg_things) = 
-               layOutDynConstr    hmods data_con arg_reps
+               layOutDynConstr    data_con arg_reps
 
            emit_info cl_info ticky_code
                = do { code_blks <- getCgStmts the_code
                     ; emitClosureCodeAndInfoTable cl_info [] code_blks }
                where
-                 the_code = do { ticky_code
+                 the_code = do { _ <- ticky_code
                                ; ldvEnter (CmmReg nodeReg)
                                ; body_code }
 
@@ -443,7 +460,10 @@ cgDataCon data_con
            body_code = do {    
                        -- NB: We don't set CC when entering data (WDP 94/06)
                             tickyReturnOldCon (length arg_things)
-                          ; performReturn (emitKnownConReturnCode data_con) }
+                           -- The case continuation code is expecting a tagged pointer
+                           ; stmtC (CmmAssign nodeReg
+                                              (tagCons data_con (CmmReg nodeReg)))
+                          ; performReturn emitReturnInstr }
                                -- noStmts: Ptr to thing already in Node
 
        ; whenC (not (isNullaryRepDataCon data_con))
@@ -451,6 +471,4 @@ cgDataCon data_con
 
                -- Dynamic-Closure first, to reduce forward references
        ; emit_info static_cl_info tickyEnterStaticCon }
-
-  where
 \end{code}