[project @ 2001-05-22 13:43:14 by simonpj]
[ghc-hetmet.git] / ghc / compiler / nativeGen / AbsCStixGen.lhs
index 7ad77c8..e6c566a 100644 (file)
@@ -1,5 +1,5 @@
 %
-% (c) The AQUA Project, Glasgow University, 1993-1996
+% (c) The AQUA Project, Glasgow University, 1993-1998
 %
 
 \begin{code}
@@ -14,28 +14,37 @@ import Stix
 import MachMisc
 
 import AbsCUtils       ( getAmodeRep, mixedTypeLocn,
-                         nonemptyAbsC, mkAbsCStmts, mkAbsCStmtList
+                         nonemptyAbsC, mkAbsCStmts
+                       )
+import PprAbsC          ( dumpRealC )
+import SMRep           ( fixedItblSize, 
+                         rET_SMALL, rET_BIG, 
+                         rET_VEC_SMALL, rET_VEC_BIG 
                        )
 import Constants       ( mIN_UPD_SIZE )
-import CLabel           ( CLabel )
+import CLabel           ( CLabel, mkReturnInfoLabel, mkReturnPtLabel,
+                          mkClosureTblLabel, mkClosureLabel,
+                         labelDynamic, mkSplitMarkerLabel )
 import ClosureInfo     ( infoTableLabelFromCI, entryLabelFromCI,
-                         fastLabelFromCI, closureUpdReqd
+                         fastLabelFromCI, closureUpdReqd,
+                         staticClosureNeedsLink
                        )
-import HeapOffs                ( hpRelToInt )
-import Literal         ( Literal(..) )
+import Literal         ( Literal(..), word2IntLit )
 import Maybes          ( maybeToBool )
-import OrdList         ( OrdList )
 import PrimOp          ( primOpNeedsWrapper, PrimOp(..) )
 import PrimRep         ( isFloatingRep, PrimRep(..) )
-import StixInfo                ( genCodeInfoTable )
-import StixMacro       ( macroCode )
-import StixPrim                ( primCode, amodeToStix, amodeToStix' )
-import UniqSupply      ( returnUs, thenUs, mapUs, getUnique, UniqSM )
-import Util            ( naturalMergeSortLe, panic )
-
-#ifdef REALLY_HASKELL_1_3
-ord = fromEnum :: Char -> Int
-#endif
+import StixInfo                ( genCodeInfoTable, genBitmapInfoTable )
+import StixMacro       ( macroCode, checkCode )
+import StixPrim                ( primCode, foreignCallCode, amodeToStix, amodeToStix' )
+import Outputable       ( pprPanic, ppr )
+import UniqSupply      ( returnUs, thenUs, mapUs, getUniqueUs, UniqSM )
+import Util            ( naturalMergeSortLe )
+import Panic           ( panic )
+import TyCon           ( tyConDataCons )
+import DataCon         ( dataConWrapId )
+import BitSet          ( intBS )
+import Name             ( NamedThing(..) )
+import CmdLineOpts     ( opt_Static, opt_EnsureSplittableC )
 \end{code}
 
 For each independent chunk of AbstractC code, we generate a list of
@@ -44,11 +53,10 @@ We leave the chunks separated so that register allocation can be
 performed locally within the chunk.
 
 \begin{code}
-genCodeAbstractC :: AbstractC -> UniqSM [[StixTree]]
+genCodeAbstractC :: AbstractC -> UniqSM [StixTree]
 
 genCodeAbstractC absC
-  = mapUs gentopcode (mkAbsCStmtList absC) `thenUs` \ trees ->
-    returnUs ([StComment SLIT("Native Code")] : trees)
+  = gentopcode absC
  where
  a2stix      = amodeToStix
  a2stix'     = amodeToStix'
@@ -56,7 +64,6 @@ genCodeAbstractC absC
  volrestores = volatileRestores
  p2stix      = primCode
  macro_code  = macroCode
- hp_rel             = hpRelToInt
  -- real code follows... ---------
 \end{code}
 
@@ -70,21 +77,38 @@ Here we handle top-level things, like @CCodeBlock@s and
     -> UniqSM [StixTree]
  -}
 
- gentopcode (CCodeBlock label absC)
+ gentopcode (CCodeBlock lbl absC)
   = gencode absC                               `thenUs` \ code ->
-    returnUs (StSegment TextSegment : StFunBegin label : code [StFunEnd label])
+    returnUs (StSegment TextSegment : StFunBegin lbl : code [StFunEnd lbl])
 
- gentopcode stmt@(CStaticClosure label _ _ _)
+ gentopcode stmt@(CStaticClosure lbl _ _ _)
   = genCodeStaticClosure stmt                  `thenUs` \ code ->
-    returnUs (StSegment DataSegment : StLabel label : code [])
-
- gentopcode stmt@(CRetUnVector _ _) = returnUs []
-
- gentopcode stmt@(CFlatRetVector label _)
+    returnUs (
+       if   opt_Static
+       then StSegment DataSegment 
+            : StLabel lbl : code []
+       else StSegment DataSegment 
+            : StData PtrRep [StInt 0] -- DLLised world, need extra zero word
+            : StLabel lbl : code []
+    )
+
+ gentopcode stmt@(CRetVector lbl _ _ _)
   = genCodeVecTbl stmt                         `thenUs` \ code ->
-    returnUs (StSegment TextSegment : code [StLabel label])
-
- gentopcode stmt@(CClosureInfoAndCode cl_info slow Nothing _ _ _)
+    returnUs (StSegment TextSegment : code [StLabel lbl])
+
+ gentopcode stmt@(CRetDirect uniq absC srt liveness)
+  = gencode absC                                      `thenUs` \ code ->
+    genBitmapInfoTable liveness srt closure_type False `thenUs` \ itbl ->
+    returnUs (StSegment TextSegment : 
+              itbl (StLabel lbl_info : StLabel lbl_ret : code []))
+  where 
+       lbl_info = mkReturnInfoLabel uniq
+       lbl_ret  = mkReturnPtLabel uniq
+       closure_type = case liveness of
+                        LvSmall _ -> rET_SMALL
+                        LvLarge _ -> rET_BIG
+
+ gentopcode stmt@(CClosureInfoAndCode cl_info slow Nothing _)
 
   | slow_is_empty
   = genCodeInfoTable stmt              `thenUs` \ itbl ->
@@ -99,7 +123,7 @@ Here we handle top-level things, like @CCodeBlock@s and
     slow_is_empty = not (maybeToBool (nonemptyAbsC slow))
     slow_lbl = entryLabelFromCI cl_info
 
- gentopcode stmt@(CClosureInfoAndCode cl_info slow (Just fast) _ _ _) =
+ gentopcode stmt@(CClosureInfoAndCode cl_info slow (Just fast) _) =
  -- ToDo: what if this is empty? ------------------------^^^^
     genCodeInfoTable stmt              `thenUs` \ itbl ->
     gencode slow                       `thenUs` \ slow_code ->
@@ -111,29 +135,75 @@ Here we handle top-level things, like @CCodeBlock@s and
     slow_lbl = entryLabelFromCI cl_info
     fast_lbl = fastLabelFromCI cl_info
 
+ gentopcode stmt@(CSRT lbl closures)
+  = returnUs [ StSegment TextSegment 
+            , StLabel lbl 
+            , StData DataPtrRep (map mk_StCLbl_for_SRT closures)
+            ]
+    where
+       mk_StCLbl_for_SRT :: CLabel -> StixTree
+       mk_StCLbl_for_SRT label
+          | labelDynamic label
+          = StIndex Int8Rep (StCLbl label) (StInt 1)
+          | otherwise
+          = StCLbl label
+
+ gentopcode stmt@(CBitmap lbl mask)
+  = returnUs [ StSegment TextSegment 
+            , StLabel lbl 
+            , StData WordRep (StInt (toInteger (length mask)) : 
+                               map  (StInt . toInteger . intBS) mask)
+            ]
+
+ gentopcode stmt@(CClosureTbl tycon)
+  = returnUs [ StSegment TextSegment
+             , StLabel (mkClosureTblLabel tycon)
+             , StData DataPtrRep (map (StCLbl . mkClosureLabel . getName . dataConWrapId) 
+                                      (tyConDataCons tycon) )
+             ]
+
+ gentopcode stmt@(CModuleInitBlock lbl absC)
+  = gencode absC                       `thenUs` \ code ->
+    getUniqLabelNCG                    `thenUs` \ tmp_lbl ->
+    getUniqLabelNCG                    `thenUs` \ flag_lbl ->
+    returnUs ( StSegment DataSegment
+            : StLabel flag_lbl
+            : StData IntRep [StInt 0]
+            : StSegment TextSegment
+            : StLabel lbl
+            : StCondJump tmp_lbl (StPrim IntNeOp       
+                                    [StInd IntRep (StCLbl flag_lbl),
+                                     StInt 0])
+            : StAssign IntRep (StInd IntRep (StCLbl flag_lbl)) (StInt 1)
+            : code 
+            [ StLabel tmp_lbl
+            , StAssign PtrRep stgSp
+                        (StIndex PtrRep stgSp (StInt (-1)))
+            , StJump NoDestInfo (StInd WordRep stgSp)
+            ])
+
  gentopcode absC
   = gencode absC                               `thenUs` \ code ->
     returnUs (StSegment TextSegment : code [])
-
 \end{code}
 
-Vector tables are trivial!
-
 \begin{code}
  {-
  genCodeVecTbl
     :: AbstractC
     -> UniqSM StixTreeList
  -}
- genCodeVecTbl (CFlatRetVector label amodes)
-  = returnUs (\xs -> vectbl : xs)
+ genCodeVecTbl (CRetVector lbl amodes srt liveness)
+  = genBitmapInfoTable liveness srt closure_type True `thenUs` \itbl ->
+    returnUs (\xs -> vectbl : itbl xs)
   where
     vectbl = StData PtrRep (reverse (map a2stix amodes))
+    closure_type = case liveness of
+                   LvSmall _ -> rET_VEC_SMALL
+                   LvLarge _ -> rET_VEC_BIG
 
 \end{code}
 
-Static closures are not so hard either.
-
 \begin{code}
  {-
  genCodeStaticClosure
@@ -141,24 +211,37 @@ Static closures are not so hard either.
     -> UniqSM StixTreeList
  -}
  genCodeStaticClosure (CStaticClosure _ cl_info cost_centre amodes)
-  = returnUs (\xs -> table : xs)
+  = returnUs (\xs -> table ++ xs)
   where
-    table = StData PtrRep (StCLbl info_lbl : body)
-    info_lbl = infoTableLabelFromCI cl_info
+    table = StData PtrRep [StCLbl (infoTableLabelFromCI cl_info)] : 
+           map do_one_amode amodes ++
+           [StData PtrRep (padding_wds ++ static_link)]
 
-    body = if closureUpdReqd cl_info then
-               take (max mIN_UPD_SIZE (length amodes')) (amodes' ++ zeros)
-          else
-               amodes'
+    do_one_amode amode 
+       = StData (promote_to_word (getAmodeRep amode)) [a2stix amode]
 
-    zeros = StInt 0 : zeros
+    -- We need to promote any item smaller than a word to a word
+    promote_to_word pk 
+       | sizeOf pk >= sizeOf IntRep  = pk
+       | otherwise                   = IntRep
+
+    upd_reqd = closureUpdReqd cl_info
 
-    amodes' = map amodeZeroVoid amodes
+    padding_wds
+       | upd_reqd  = take (max 0 (mIN_UPD_SIZE - length amodes)) zeros
+       | otherwise = []
 
+    static_link | upd_reqd || staticClosureNeedsLink cl_info = [StInt 0]
+               | otherwise                                  = []
+
+    zeros = StInt 0 : zeros
+
+    {- needed??? --SDM
        -- Watch out for VoidKinds...cf. PprAbsC
     amodeZeroVoid item
       | getAmodeRep item == VoidRep = StInt 0
       | otherwise = a2stix item
+    -}
 
 \end{code}
 
@@ -180,11 +263,14 @@ Now the individual AbstractC statements.
 
 \end{code}
 
-Split markers are a NOP in this land.
+Split markers just insert a __stg_split_marker, which is caught by the
+split-mangler later on and used to split the assembly into chunks.
 
 \begin{code}
 
- gencode CSplitMarker = returnUs id
+ gencode CSplitMarker
+   | opt_EnsureSplittableC = returnUs (\xs -> StLabel mkSplitMarkerLabel : xs)
+   | otherwise             = returnUs id
 
 \end{code}
 
@@ -208,12 +294,22 @@ addresses, etc.)
 
 \begin{code}
 
- gencode (CInitHdr cl_info reg_rel _ _)
+ gencode (CInitHdr cl_info reg_rel _)
   = let
-       lhs = a2stix (CVal reg_rel PtrRep)
+       lhs = a2stix reg_rel
        lbl = infoTableLabelFromCI cl_info
     in
-       returnUs (\xs -> StAssign PtrRep lhs (StCLbl lbl) : xs)
+       returnUs (\xs -> StAssign PtrRep (StInd PtrRep lhs) (StCLbl lbl) : xs)
+
+\end{code}
+
+Heap/Stack Checks.
+
+\begin{code}
+
+ gencode (CCheck macro args assts)
+  = gencode assts `thenUs` \assts_stix ->
+    checkCode macro args assts_stix
 
 \end{code}
 
@@ -242,40 +338,48 @@ Unconditional jumps, including the special ``enter closure'' operation.
 Note that the new entry convention requires that we load the InfoPtr (R2)
 with the address of the info table before jumping to the entry code for Node.
 
+For a vectored return, we must subtract the size of the info table to
+get at the return vector.  This depends on the size of the info table,
+which varies depending on whether we're profiling etc.
+
 \begin{code}
 
  gencode (CJump dest)
-  = returnUs (\xs -> StJump (a2stix dest) : xs)
+  = returnUs (\xs -> StJump NoDestInfo (a2stix dest) : xs)
 
  gencode (CFallThrough (CLbl lbl _))
   = returnUs (\xs -> StFallThrough lbl : xs)
 
  gencode (CReturn dest DirectReturn)
-  = returnUs (\xs -> StJump (a2stix dest) : xs)
+  = returnUs (\xs -> StJump NoDestInfo (a2stix dest) : xs)
 
  gencode (CReturn table (StaticVectoredReturn n))
-  = returnUs (\xs -> StJump dest : xs)
+  = returnUs (\xs -> StJump NoDestInfo dest : xs)
   where
     dest = StInd PtrRep (StIndex PtrRep (a2stix table)
-                                         (StInt (toInteger (-n-1))))
+                                 (StInt (toInteger (-n-fixedItblSize-1))))
 
  gencode (CReturn table (DynamicVectoredReturn am))
-  = returnUs (\xs -> StJump dest : xs)
+  = returnUs (\xs -> StJump NoDestInfo dest : xs)
   where
     dest = StInd PtrRep (StIndex PtrRep (a2stix table) dyn_off)
-    dyn_off = StPrim IntSubOp [StPrim IntNegOp [a2stix am], StInt 1]
+    dyn_off = StPrim IntSubOp [StPrim IntNegOp [a2stix am], 
+                              StInt (toInteger (fixedItblSize+1))]
 
 \end{code}
 
 Now the PrimOps, some of which may need caller-saves register wrappers.
 
 \begin{code}
+ gencode (COpStmt results (StgFCallOp fcall _) args vols)
+  = ASSERT( null vols )
+    foreignCallCode (nonVoid results) fcall (nonVoid args)
 
- gencode (COpStmt results op args liveness_mask vols)
+ gencode (COpStmt results (StgPrimOp op) args vols)
   -- ToDo (ADR?): use that liveness mask
   | primOpNeedsWrapper op
   = let
-       saves = volsaves vols
+       saves    = volsaves vols
        restores = volrestores vols
     in
        p2stix (nonVoid results) op (nonVoid args)
@@ -285,7 +389,6 @@ Now the PrimOps, some of which may need caller-saves register wrappers.
   | otherwise = p2stix (nonVoid results) op (nonVoid args)
     where
        nonVoid = filter ((/= VoidRep) . getAmodeRep)
-
 \end{code}
 
 Now the dreaded conditional jump.
@@ -314,8 +417,8 @@ Now the if statement.  Almost *all* flow of control are of this form.
                                Nothing -> gencode alt_code
                                Just dc -> mkIfThenElse discrim tag alt_code dc
 
-      [(tag1@(MachInt i1 _), alt_code1),
-       (tag2@(MachInt i2 _), alt_code2)]
+      [(tag1@(MachInt i1), alt_code1),
+       (tag2@(MachInt i2), alt_code2)]
        | deflt_is_empty && i1 == 0 && i2 == 1
        -> mkIfThenElse discrim tag1 alt_code1 alt_code2
        | deflt_is_empty && i1 == 1 && i2 == 0
@@ -325,7 +428,7 @@ Now the if statement.  Almost *all* flow of control are of this form.
       other | simple_discrim -> mkSimpleSwitches discrim alts deflt
 
        -- Otherwise, we need to do a bit of work.
-      other ->  getUnique                        `thenUs` \ u ->
+      other ->  getUniqueUs                      `thenUs` \ u ->
                gencode (AbsCStmts
                (CAssign (CTemp u pk) discrim)
                (CSwitch (CTemp u pk) alts deflt))
@@ -358,24 +461,28 @@ Finally, all of the disgusting AbstractC macros.
  gencode (CCallProfCCMacro macro _)
   = returnUs (\xs -> StComment macro : xs)
 
+ gencode other
+  = pprPanic "AbsCStixGen.gencode" (dumpRealC other)
 \end{code}
 
-Here, we generate a jump table if there are more than four (integer) alternatives and
-the jump table occupancy is greater than 50%.  Otherwise, we generate a binary
-comparison tree.  (Perhaps this could be tuned.)
+Here, we generate a jump table if there are more than four (integer)
+alternatives and the jump table occupancy is greater than 50%.
+Otherwise, we generate a binary comparison tree.  (Perhaps this could
+be tuned.)
 
 \begin{code}
 
  intTag :: Literal -> Integer
- intTag (MachChar c)  = fromInt (ord c)
- intTag (MachInt i _) = i
- intTag _ = panic "intTag"
+ intTag (MachChar c)  = toInteger c
+ intTag (MachInt i)   = i
+ intTag (MachWord w)  = intTag (word2IntLit (MachWord w))
+ intTag _             = panic "intTag"
 
  fltTag :: Literal -> Rational
 
- fltTag (MachFloat f) = f
+ fltTag (MachFloat f)  = f
  fltTag (MachDouble d) = d
- fltTag _ = panic "fltTag"
+ fltTag x              = pprPanic "fltTag" (ppr x)
 
  {-
  mkSimpleSwitches
@@ -398,12 +505,14 @@ comparison tree.  (Perhaps this could be tuned.)
        highest = if floating then targetMaxDouble else targetMaxInt
     in
        (
-       if not floating && choices > 4 && highTag - lowTag < toInteger (2 * choices) then
+       if  not floating && choices > 4 
+            && highTag - lowTag < toInteger (2 * choices)
+        then
            mkJumpTable am' sortedAlts lowTag highTag udlbl
        else
            mkBinaryTree am' floating sortedAlts choices lowest highest udlbl
        )
-                                                       `thenUs` \ alt_code ->
+                                               `thenUs` \ alt_code ->
        gencode absC                            `thenUs` \ dflt_code ->
 
        returnUs (\xs -> alt_code (StLabel udlbl : dflt_code (StLabel ujlbl : xs)))
@@ -413,7 +522,8 @@ comparison tree.  (Perhaps this could be tuned.)
        choices = length alts
 
        (x@(MachChar _),_)  `leAlt` (y,_) = intTag x <= intTag y
-       (x@(MachInt _ _),_) `leAlt` (y,_) = intTag x <= intTag y
+       (x@(MachInt _), _)  `leAlt` (y,_) = intTag x <= intTag y
+       (x@(MachWord _), _) `leAlt` (y,_) = intTag x <= intTag y
        (x,_)               `leAlt` (y,_) = fltTag x <= fltTag y
 
 \end{code}
@@ -446,8 +556,9 @@ already finish with a jump to the join point.
        cjmpHi = StCondJump dflt (StPrim IntGtOp [am, StInt (toInteger highTag)])
 
        offset = StPrim IntSubOp [am, StInt lowTag]
+        dsts   = DestInfo (dflt : map fst branches)
 
-       jump = StJump (StInd PtrRep (StIndex PtrRep (StCLbl utlbl) offset))
+       jump = StJump dsts (StInd PtrRep (StIndex PtrRep (StCLbl utlbl) offset))
        tlbl = StLabel utlbl
        table = StData PtrRep (mkTable branches [lowTag..highTag] [])
     in