[project @ 2000-06-16 09:32:32 by sewardj]
[ghc-hetmet.git] / ghc / compiler / nativeGen / StixPrim.lhs
index ee1b861..b7ca132 100644 (file)
@@ -15,15 +15,20 @@ import StixInteger
 import AbsCSyn                 hiding ( spRel )
 import AbsCUtils       ( getAmodeRep, mixedTypeLocn )
 import SMRep           ( fixedHdrSize )
-import Const           ( Literal(..) )
+import Literal         ( Literal(..), word2IntLit )
 import CallConv                ( cCallConv )
-import PrimOp          ( PrimOp(..) )
+import PrimOp          ( PrimOp(..), CCall(..), CCallTarget(..) )
 import PrimRep         ( PrimRep(..), isFloatingRep )
-import UniqSupply      ( returnUs, thenUs, UniqSM )
-import Constants       ( mIN_INTLIKE )
+import UniqSupply      ( returnUs, thenUs, getUniqueUs, UniqSM )
+import Constants       ( mIN_INTLIKE, uF_UPDATEE, bLOCK_SIZE )
+import CLabel          ( mkIntlikeClosureLabel, mkCharlikeClosureLabel,
+                         mkTopClosureLabel, mkErrorIO_innardsLabel,
+                         mkMAP_FROZEN_infoLabel, mkForeignLabel )
 import Outputable
 
-import Char            ( ord )
+import Char            ( ord, isAlpha, isDigit )
+
+#include "NCG.h"
 \end{code}
 
 The main honcho here is primCode, which handles the guts of COpStmts.
@@ -52,19 +57,20 @@ and modify our heap check accordingly.
 \begin{code}
 -- NB: ordering of clauses somewhere driven by
 -- the desire to getting sane patt-matching behavior
-primCode res@[ar,sr,dr] IntegerNegOp arg@[aa,sa,da]
-  = gmpNegate (ar,sr,dr) (aa,sa,da)
-\end{code}
+primCode res@[sr,dr] IntegerNegOp arg@[sa,da]
+  = gmpNegate (sr,dr) (sa,da)
 
-\begin{code}
-primCode [res] IntegerCmpOp args@[aa1,sa1,da1, aa2,sa2,da2]
-  = gmpCompare res (aa1,sa1,da1, aa2,sa2,da2)
+primCode [res] IntegerCmpOp args@[sa1,da1, sa2,da2]
+  = gmpCompare res (sa1,da1, sa2,da2)
+
+primCode [res] IntegerCmpIntOp args@[sa1,da1,ai]
+  = gmpCompareInt res (sa1,da1,ai)
 
-primCode [res] Integer2IntOp arg@[aa,sa,da]
-  = gmpInteger2Int res (aa,sa,da)
+primCode [res] Integer2IntOp arg@[sa,da]
+  = gmpInteger2Int res (sa,da)
 
-primCode [res] Integer2WordOp arg@[aa,sa,da]
-  = gmpInteger2Word res (aa,sa,da)
+primCode [res] Integer2WordOp arg@[sa,da]
+  = gmpInteger2Word res (sa,da)
 
 primCode [res] Int2AddrOp [arg]
   = simpleCoercion AddrRep res arg
@@ -89,6 +95,12 @@ primCode [res] SameMutableArrayOp args
 
 primCode res@[_] SameMutableByteArrayOp args
   = primCode res SameMutableArrayOp args
+
+primCode res@[_] SameMutVarOp args
+  = primCode res SameMutableArrayOp args
+
+primCode res@[_] SameMVarOp args
+  = primCode res SameMutableArrayOp args
 \end{code}
 
 Freezing an array of pointers is a double assignment.  We fix the
@@ -138,7 +150,6 @@ primCode [lhs] SizeofMutableByteArrayOp [rhs]
 Most other array primitives translate to simple indexing.
 
 \begin{code}
-
 primCode lhs@[_] IndexArrayOp args
   = primCode lhs ReadArrayOp args
 
@@ -147,7 +158,7 @@ primCode [lhs] ReadArrayOp [obj, ix]
        lhs' = amodeToStix lhs
        obj' = amodeToStix obj
        ix' = amodeToStix ix
-       base = StIndex IntRep obj' arrHS
+       base = StIndex IntRep obj' arrPtrsHS
        assign = StAssign PtrRep lhs' (StInd PtrRep (StIndex PtrRep base ix'))
     in
     returnUs (\xs -> assign : xs)
@@ -157,7 +168,7 @@ primCode [] WriteArrayOp [obj, ix, v]
        obj' = amodeToStix obj
        ix' = amodeToStix ix
        v' = amodeToStix v
-       base = StIndex IntRep obj' arrHS
+       base = StIndex IntRep obj' arrPtrsHS
        assign = StAssign PtrRep (StInd PtrRep (StIndex PtrRep base ix')) v'
     in
     returnUs (\xs -> assign : xs)
@@ -172,11 +183,14 @@ primCode [lhs] (ReadByteArrayOp pk) [obj, ix]
        lhs' = amodeToStix lhs
        obj' = amodeToStix obj
        ix' = amodeToStix ix
-       base = StIndex IntRep obj' arrHS
+       base = StIndex IntRep obj' arrWordsHS
        assign = StAssign pk lhs' (StInd pk (StIndex pk base ix'))
     in
     returnUs (\xs -> assign : xs)
 
+primCode lhs@[_] (ReadOffAddrOp pk) args
+  = primCode lhs (IndexOffAddrOp pk) args
+
 primCode [lhs] (IndexOffAddrOp pk) [obj, ix]
   = let
        lhs' = amodeToStix lhs
@@ -191,46 +205,118 @@ primCode [lhs] (IndexOffForeignObjOp pk) [obj, ix]
        lhs' = amodeToStix lhs
        obj' = amodeToStix obj
        ix' = amodeToStix ix
-       obj'' = StIndex PtrRep obj' fixedHS
+       obj'' = StIndex AddrRep obj' fixedHS
        assign = StAssign pk lhs' (StInd pk (StIndex pk obj'' ix'))
     in
     returnUs (\xs -> assign : xs)
 
+primCode [] (WriteOffAddrOp pk) [obj, ix, v]
+  = let
+       obj' = amodeToStix obj
+       ix' = amodeToStix ix
+       v' = amodeToStix v
+       assign = StAssign pk (StInd pk (StIndex pk obj' ix')) v'
+    in
+    returnUs (\xs -> assign : xs)
+
 primCode [] (WriteByteArrayOp pk) [obj, ix, v]
   = let
        obj' = amodeToStix obj
        ix' = amodeToStix ix
        v' = amodeToStix v
-       base = StIndex IntRep obj' arrHS
+       base = StIndex IntRep obj' arrWordsHS
        assign = StAssign pk (StInd pk (StIndex pk base ix')) v'
     in
     returnUs (\xs -> assign : xs)
+
+primCode [] WriteForeignObjOp [obj, v]
+  = let
+       obj' = amodeToStix obj
+       v' = amodeToStix v
+       obj'' = StIndex AddrRep obj' (StInt 4711) -- fixedHS
+       assign = StAssign AddrRep (StInd AddrRep obj'') v'
+    in
+    returnUs (\xs -> assign : xs)
 \end{code}
 
+ToDo: saving/restoring of volatile regs around ccalls.
+
 \begin{code}
---primCode lhs (CCallOp fn is_asm may_gc) rhs
-primCode lhs (CCallOp (Left fn) is_asm may_gc cconv) rhs
+primCode lhs (CCallOp (CCall (StaticTarget fn) is_asm may_gc cconv)) rhs
   | is_asm = error "ERROR: Native code generator can't handle casm"
-  | may_gc = error "ERROR: Native code generator can't handle _ccall_GC_\n"
-  | otherwise
-  = case lhs of
-      [] -> returnUs (\xs -> (StCall fn cconv VoidRep args) : xs)
-      [lhs] ->
-         let lhs' = amodeToStix lhs
-             pk = if isFloatingRep (getAmodeRep lhs) then DoubleRep else IntRep
-             call = StAssign pk lhs' (StCall fn cconv pk args)
-         in
-             returnUs (\xs -> call : xs)
+  | not may_gc = returnUs (\xs -> ccall : xs)
+  | otherwise =
+       save_thread_state       `thenUs` \ save ->
+       load_thread_state       `thenUs` \ load -> 
+       getUniqueUs             `thenUs` \ uniq -> 
+       let
+          id  = StReg (StixTemp uniq IntRep)
+
+          suspend = StAssign IntRep id 
+                       (StCall SLIT("suspendThread") cconv IntRep [stgBaseReg])
+          resume  = StCall SLIT("resumeThread") cconv VoidRep [id]
+       in
+       returnUs (\xs -> save (suspend : ccall : resume : load xs))
+
   where
     args = map amodeCodeForCCall rhs
     amodeCodeForCCall x =
        let base = amodeToStix' x
        in
            case getAmodeRep x of
-             ArrayRep      -> StIndex PtrRep base arrHS
-             ByteArrayRep  -> StIndex IntRep base arrHS
+             ArrayRep      -> StIndex PtrRep base arrPtrsHS
+             ByteArrayRep  -> StIndex IntRep base arrWordsHS
              ForeignObjRep -> StIndex PtrRep base fixedHS
              _ -> base
+
+    ccall = case lhs of
+      [] -> StCall fn cconv VoidRep args
+      [lhs] ->
+         let lhs' = amodeToStix lhs
+             pk = if isFloatingRep (getAmodeRep lhs) then DoubleRep else IntRep
+         in
+             StAssign pk lhs' (StCall fn cconv pk args)
+\end{code}
+
+DataToTagOp won't work for 64-bit archs, as it is.
+
+\begin{code}
+primCode [lhs] DataToTagOp [arg]
+  = let lhs'        = amodeToStix lhs
+        arg'        = amodeToStix arg
+        infoptr     = StInd PtrRep arg'
+        word_32     = StInd WordRep (StIndex PtrRep infoptr (StInt (-1)))
+        masked_le32 = StPrim SrlOp [word_32, StInt 16]
+        masked_be32 = StPrim AndOp [word_32, StInt 65535]
+#ifdef WORDS_BIGENDIAN
+        masked      = masked_be32
+#else
+        masked      = masked_le32
+#endif
+        assign      = StAssign IntRep lhs' masked
+    in
+    returnUs (\xs -> assign : xs)
+\end{code}
+
+MutVars are pretty simple.
+#define writeMutVarzh(a,v)       (P_)(((StgMutVar *)(a))->var)=(v)
+
+\begin{code}
+primCode [] WriteMutVarOp [aa,vv]
+   = let aa_s      = amodeToStix aa
+         vv_s      = amodeToStix vv
+         var_field = StIndex PtrRep aa_s fixedHS
+         assign    = StAssign PtrRep (StInd PtrRep var_field) vv_s
+     in
+     returnUs (\xs -> assign : xs)
+
+primCode [rr] ReadMutVarOp [aa]
+   = let aa_s      = amodeToStix aa
+         rr_s      = amodeToStix rr
+         var_field = StIndex PtrRep aa_s fixedHS
+         assign    = StAssign PtrRep rr_s (StInd PtrRep var_field)
+     in
+     returnUs (\xs -> assign : xs)
 \end{code}
 
 Now the more mundane operations.
@@ -312,63 +398,79 @@ amodeToStix (CAddr (HpRel off))
 amodeToStix (CAddr (NodeRel off))
   = StIndex IntRep stgNode (StInt (toInteger IBOX(off)))
 
+amodeToStix (CAddr (CIndex base off pk))
+  = StIndex pk (amodeToStix base) (amodeToStix off)
+
 amodeToStix (CReg magic)    = StReg (StixMagicId magic)
 amodeToStix (CTemp uniq pk) = StReg (StixTemp uniq pk)
 
 amodeToStix (CLbl      lbl _) = StCLbl lbl
 
-amodeToStix (CTableEntry base off pk)
-  = StInd pk (StIndex pk (amodeToStix base) (amodeToStix off))
-
  -- For CharLike and IntLike, we attempt some trivial constant-folding here.
 
 amodeToStix (CCharLike (CLit (MachChar c)))
-  = StLitLbl ((<>) (ptext SLIT("CHARLIKE_closure+")) (int off))
+  = StIndex CharRep cHARLIKE_closure (StInt (toInteger off))
   where
     off = charLikeSize * ord c
 
 amodeToStix (CCharLike x)
-  = StIndex PtrRep charLike off
+  = StIndex CharRep cHARLIKE_closure off
   where
-    off = StPrim IntMulOp [amodeToStix x, StInt (toInteger (fixedHdrSize+1))]
+    off = StPrim IntMulOp [amodeToStix x, StInt (toInteger charLikeSize)]
 
-amodeToStix (CIntLike (CLit (MachInt i _)))
-  = StLitLbl ((<>) (ptext SLIT("INTLIKE_closure+")) (int off))
+amodeToStix (CIntLike (CLit (MachInt i)))
+  = StIndex CharRep{-yes,really-} iNTLIKE_closure (StInt (toInteger off))
   where
     off = intLikeSize * (fromInteger (i - mIN_INTLIKE))
 
 amodeToStix (CIntLike x)
   = panic "CIntLike"
 
- -- A CString is just a (CLit . MachStr)
-amodeToStix (CString s) = StString s
-
 amodeToStix (CLit core)
   = case core of
       MachChar c     -> StInt (toInteger (ord c))
       MachStr s             -> StString s
       MachAddr a     -> StInt a
-      MachInt i _    -> StInt (toInteger i)
-      MachLitLit s _ -> StLitLit s
+      MachInt i      -> StInt i
+      MachWord w     -> case word2IntLit core of MachInt iw -> StInt iw
+      MachLitLit s _ -> trace ("\nnativeGen WARNING: Reference to C entity `" 
+                                ++ (_UNPK_ s) ++ "' cannot be reliably compiled."
+                                ++ "\n\t\t   It may well crash your program."
+                                ++ "\n\t\t   Workaround: compile via C (use -fvia-C).\n"
+                              )
+                              (litLitToStix (_UNPK_ s))
       MachFloat d    -> StDouble d
       MachDouble d   -> StDouble d
       _ -> panic "amodeToStix:core literal"
 
- -- A CLitLit is just a (CLit . MachLitLit)
-amodeToStix (CLitLit s _) = StLitLit s
+amodeToStix (CLitLit s _)
+   = litLitToStix (_UNPK_ s)
 
 amodeToStix (CMacroExpr _ macro [arg])
   = case macro of
       ENTRY_CODE -> amodeToStix arg
       ARG_TAG    -> amodeToStix arg -- just an integer no. of words
-      GET_TAG    -> StPrim SrlOp 
-                       [StInd WordRep (StPrim IntSubOp [amodeToStix arg,
-                                                        StInt 1]),
+      GET_TAG    -> 
+#ifdef WORDS_BIGENDIAN
+                    StPrim AndOp 
+                       [StInd WordRep (StIndex PtrRep (amodeToStix arg)
+                                                (StInt (toInteger (-1)))),
+                        StInt 65535]
+#else
+                    StPrim SrlOp 
+                       [StInd WordRep (StIndex PtrRep (amodeToStix arg)
+                                                (StInt (toInteger (-1)))),
                         StInt 16]
-
--- XXX!!!
--- GET_TAG(info_ptr) is supposed to be  get_itbl(info_ptr)->srt_len,
--- which we've had to hand-code here.
+#endif
+      UPD_FRAME_UPDATEE
+         -> StInd PtrRep (StIndex PtrRep (amodeToStix arg) 
+                                         (StInt (toInteger uF_UPDATEE)))
+litLitToStix nm
+  | all is_id nm = StCLbl (mkForeignLabel (_PK_ nm) False{-ToDo: dynamic-})
+  | otherwise    = error ("\nlitLitToStix: can't handle `" ++ nm ++ "'\n" 
+                           ++ "suggested workaround: use flag -fvia-C\n")
+
+  where is_id c = isAlpha c || isDigit c || c == '_'
 \end{code}
 
 Sizes of the CharLike and IntLike closures that are arranged as arrays
@@ -377,25 +479,83 @@ in the data segment.  (These are in bytes.)
 \begin{code}
 -- The INTLIKE base pointer
 
-intLikePtr :: StixTree
-
-intLikePtr = StInd PtrRep (sStLitLbl SLIT("INTLIKE_closure"))
+iNTLIKE_closure :: StixTree
+iNTLIKE_closure = StCLbl mkIntlikeClosureLabel
 
 -- The CHARLIKE base
 
-charLike :: StixTree
-
-charLike = sStLitLbl SLIT("CHARLIKE_closure")
+cHARLIKE_closure :: StixTree
+cHARLIKE_closure = StCLbl mkCharlikeClosureLabel
 
 -- Trees for the ErrorIOPrimOp
 
 topClosure, errorIO :: StixTree
 
-topClosure = StInd PtrRep (sStLitLbl SLIT("TopClosure"))
-errorIO = StJump (StInd PtrRep (sStLitLbl SLIT("ErrorIO_innards")))
+topClosure = StInd PtrRep (StCLbl mkTopClosureLabel)
+errorIO = StJump (StInd PtrRep (StCLbl mkErrorIO_innardsLabel))
 
-mutArrPtrsFrozen_info = sStLitLbl SLIT("MUT_ARR_PTRS_FROZEN_info")
+mutArrPtrsFrozen_info = StCLbl mkMAP_FROZEN_infoLabel
 
+-- these are the sizes of charLike and intLike closures, in _bytes_.
 charLikeSize = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
 intLikeSize  = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
 \end{code}
+
+
+\begin{code}
+save_thread_state 
+   = getUniqueUs   `thenUs` \tso_uq -> 
+     let tso = StReg (StixTemp tso_uq ThreadIdRep) in
+     returnUs (\xs ->
+       StAssign ThreadIdRep tso stgCurrentTSO :
+       StAssign PtrRep
+          (StInd PtrRep (StPrim IntAddOp 
+               [tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))]))
+          stgSp :
+       StAssign PtrRep 
+          (StInd PtrRep (StPrim IntAddOp 
+               [tso, StInt (toInteger (TSO_SU*BYTES_PER_WORD))]))
+          stgSu :
+       StAssign PtrRep 
+          (StInd PtrRep (StPrim IntAddOp 
+               [tso, StInt (toInteger (TSO_SPLIM*BYTES_PER_WORD))]))
+          stgSpLim :
+       StAssign PtrRep
+          (StInd PtrRep (StPrim IntAddOp
+               [stgCurrentNursery, 
+                StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))]))
+          (StPrim IntAddOp [stgHp, StInt (toInteger (1 * BYTES_PER_WORD))]) :
+       xs
+     )
+
+load_thread_state 
+   = getUniqueUs   `thenUs` \tso_uq -> 
+     let tso = StReg (StixTemp tso_uq ThreadIdRep) in
+     returnUs (\xs ->
+       StAssign ThreadIdRep tso stgCurrentTSO :
+       StAssign PtrRep stgSp
+          (StInd PtrRep (StPrim IntAddOp 
+               [tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))])) :
+       StAssign PtrRep stgSu
+          (StInd PtrRep (StPrim IntAddOp 
+               [tso, StInt (toInteger (TSO_SU*BYTES_PER_WORD))])) :
+       StAssign PtrRep stgSpLim
+          (StInd PtrRep (StPrim IntAddOp 
+               [tso, StInt (toInteger (TSO_SPLIM*BYTES_PER_WORD))])) :
+       StAssign PtrRep stgHp
+          (StPrim IntSubOp [
+             StInd PtrRep (StPrim IntAddOp
+               [stgCurrentNursery, 
+                StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))]),
+             StInt (toInteger (1 * BYTES_PER_WORD))
+           ]) :
+       StAssign PtrRep stgHpLim
+          (StPrim IntAddOp [
+             StInd PtrRep (StPrim IntAddOp
+               [stgCurrentNursery, 
+                StInt (toInteger (BDESCR_START * BYTES_PER_WORD))]),
+             StInt (toInteger (bLOCK_SIZE - (1 * BYTES_PER_WORD)))
+           ]) :
+       xs
+     )
+\end{code}