[project @ 2002-04-29 14:03:38 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeGen.lhs
index 411f1ad..56f64fc 100644 (file)
@@ -32,9 +32,9 @@ import DataCon                ( dataConTag, fIRST_TAG, dataConTyCon,
 import TyCon           ( TyCon(..), tyConFamilySize, isDataTyCon, tyConDataCons,
                          isFunTyCon, isUnboxedTupleTyCon )
 import Class           ( Class, classTyCon )
-import Type            ( Type, repType, splitRepFunTys )
+import Type            ( Type, repType, splitFunTys, dropForAlls )
 import Util            ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem,
-                         isSingleton, lengthIs )
+                         isSingleton, lengthIs, notNull )
 import DataCon         ( dataConRepArity )
 import Var             ( isTyVar )
 import VarSet          ( VarSet, varSetElems )
@@ -56,16 +56,15 @@ import ByteCodeFFI  ( taggedSizeW, untaggedSizeW, mkMarshalCode, moan64 )
 import Linker          ( lookupSymbol )
 
 import List            ( intersperse, sortBy, zip4 )
-import Foreign         ( Ptr(..), mallocBytes )
-import Addr            ( Addr(..), writeCharOffAddr )
+import Foreign         ( Ptr(..), castPtr, mallocBytes, pokeByteOff, Word8 )
 import CTypes          ( CInt )
 import Exception       ( throwDyn )
 
-import PrelBase                ( Int(..) )
-import PrelGHC         ( ByteArray# )
-import PrelIOBase      ( IO(..) )
+import GlaExts         ( Int(..), ByteArray# )
+
 import Monad           ( when )
 import Maybe           ( isJust )
+import Char            ( ord )
 \end{code}
 
 %************************************************************************
@@ -95,7 +94,7 @@ byteCodeGen dflags binds local_tycons local_classes
                        --               ^^
                        -- better be no free vars in these top-level bindings
 
-        when (not (null mallocd))
+        when (notNull mallocd)
              (panic "ByteCodeGen.byteCodeGen: missing final emitBc?")
 
         dumpIfSet_dyn dflags Opt_D_dump_BCOs
@@ -116,8 +115,9 @@ coreExprToBCOs dflags expr
 
       -- create a totally bogus name for the top-level BCO; this
       -- should be harmless, since it's never used for anything
-      let invented_id   = mkSysLocal SLIT("Expr-Top-Level") (mkPseudoUnique3 0) 
-                                    (panic "invented_id's type")
+      let invented_id   = mkSysLocal FSLIT("Expr-Top-Level") 
+                               (mkPseudoUnique3 0) 
+                               (panic "invented_id's type")
       let invented_name = idName invented_id
 
          annexpr = freeVars expr
@@ -127,7 +127,7 @@ coreExprToBCOs dflags expr
          <- runBc (BcM_State [] 0 []) 
                   (schemeR True fvs (invented_id, annexpr))
 
-      when (not (null mallocd))
+      when (notNull mallocd)
            (panic "ByteCodeGen.coreExprToBCOs: missing final emitBc?")
 
       dumpIfSet_dyn dflags Opt_D_dump_BCOs
@@ -591,7 +591,7 @@ schemeE d s p other
 -- and enter.  Four cases:
 --
 -- 0.  (Nasty hack).
---     An application "PrelGHC.tagToEnum# <type> unboxed-int".
+--     An application "GHC.Prim.tagToEnum# <type> unboxed-int".
 --     The int will be on the stack.  Generate a code sequence
 --     to convert it to the relevant constructor, SLIDE and ENTER.
 --
@@ -642,16 +642,17 @@ schemeT d s p app
      )
 
    -- Case 2
-   | let isVoidRepAtom (_, AnnVar v)    = VoidRep == typePrimRep (idType v)
+   | [arg1,arg2] <- args_r_to_l,
+     let 
+        isVoidRepAtom (_, AnnVar v)    = typePrimRep (idType v) == VoidRep
          isVoidRepAtom (_, AnnNote n e) = isVoidRepAtom e
-     in  is_con_call && isUnboxedTupleCon con 
-         && ( (args_r_to_l `lengthIs` 2 && isVoidRepAtom (last (args_r_to_l)))
-              || (isSingleton args_r_to_l)
-            )
+        isVoidRepAtom _ = False
+     in  
+        isVoidRepAtom arg2
    = --trace (if isSingleton args_r_to_l
      --       then "schemeT: unboxed singleton"
      --       else "schemeT: unboxed pair with Void first component") (
-     schemeT d s p (head args_r_to_l)
+     schemeT d s p arg1
      --)
 
    -- Case 3
@@ -882,10 +883,10 @@ generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
                  DynamicTarget
                     -> returnBc (False, panic "ByteCodeGen.generateCCall(dyn)")
                  StaticTarget target
-                    -> let sym_to_find = _UNPK_ target in
+                    -> let sym_to_find = unpackFS target in
                        ioToBc (lookupSymbol sym_to_find) `thenBc` \res ->
                        case res of
-                           Just aa -> case aa of Ptr a# -> returnBc (True, A# a#)
+                           Just aa -> returnBc (True, aa)
                            Nothing -> ioToBc (linkFail "ByteCodeGen.generateCCall" 
                                                        sym_to_find)
                  CasmTarget _
@@ -935,7 +936,7 @@ generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
          recordMallocBc addr_of_marshaller     `thenBc_`
      let
          -- do the call
-         do_call      = unitOL (CCALL addr_of_marshaller)
+         do_call      = unitOL (CCALL (castPtr addr_of_marshaller))
          -- slide and return
          wrapup       = mkSLIDE r_tsizeW (d_after_r - r_tsizeW - s)
                         `snocOL` RETURN r_rep
@@ -963,22 +964,22 @@ mkDummyLiteral pr
 
 
 -- Convert (eg) 
---     PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
---                   -> (# PrelGHC.State# PrelGHC.RealWorld, PrelGHC.Int# #)
+--     GHC.Prim.Char# -> GHC.Prim.State# GHC.Prim.RealWorld
+--                   -> (# GHC.Prim.State# GHC.Prim.RealWorld, GHC.Prim.Int# #)
 --
 -- to  Just IntRep
 -- and check that an unboxed pair is returned wherein the first arg is VoidRep'd.
 --
 -- Alternatively, for call-targets returning nothing, convert
 --
---     PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
---                   -> (# PrelGHC.State# PrelGHC.RealWorld #)
+--     GHC.Prim.Char# -> GHC.Prim.State# GHC.Prim.RealWorld
+--                   -> (# GHC.Prim.State# GHC.Prim.RealWorld #)
 --
 -- to  Nothing
 
 maybe_getCCallReturnRep :: Type -> Maybe PrimRep
 maybe_getCCallReturnRep fn_ty
-   = let (a_tys, r_ty) = splitRepFunTys fn_ty
+   = let (a_tys, r_ty) = splitFunTys (dropForAlls fn_ty)
          maybe_r_rep_to_go  
             = if isSingleton r_reps then Nothing else Just (r_reps !! 1)
          (r_tycon, r_reps) 
@@ -1014,7 +1015,7 @@ atomRep other = pprPanic "atomRep" (ppr (deAnnotate (undefined,other)))
 -- as a consequence.
 implement_tagToId :: [Name] -> BcM BCInstrList
 implement_tagToId names
-   = ASSERT(not (null names))
+   = ASSERT( notNull names )
      getLabelsBc (length names)                        `thenBc` \ labels ->
      getLabelBc                                        `thenBc` \ label_fail ->
      getLabelBc                                `thenBc` \ label_exit ->
@@ -1093,7 +1094,8 @@ mkUnpackCode vars d p
         code_np = do_nptrs vreps_env_uszw ptrs_szw (reverse (map snd vreps_np))
         do_nptrs off_h off_s [] = nilOL
         do_nptrs off_h off_s (npr:nprs)
-           | npr `elem` [IntRep, WordRep, FloatRep, DoubleRep, CharRep, AddrRep]
+           | npr `elem` [IntRep, WordRep, FloatRep, DoubleRep, 
+                         CharRep, AddrRep, StablePtrRep]
            = approved
            | otherwise
            = moan64 "ByteCodeGen.mkUnpackCode" (ppr npr)
@@ -1174,6 +1176,7 @@ pushAtom True d p (AnnLit lit)
 
 pushAtom False d p (AnnLit lit)
    = case lit of
+        MachLabel fs -> code CodePtrRep
         MachWord w   -> code WordRep
         MachInt i    -> code IntRep
         MachFloat r  -> code FloatRep
@@ -1189,7 +1192,7 @@ pushAtom False d p (AnnLit lit)
         pushStr s 
            = let getMallocvilleAddr
                     = case s of
-                         CharStr s i -> returnBc (A# s)
+                         CharStr s i -> returnBc (Ptr s)
 
                          FastString _ l ba -> 
                             -- sigh, a string in the heap is no good to us.
@@ -1199,12 +1202,12 @@ pushAtom False d p (AnnLit lit)
                             -- at the same time.
                             let n = I# l
                             -- CAREFUL!  Chars are 32 bits in ghc 4.09+
-                            in  ioToBc (mallocBytes (n+1)) `thenBc` \ (Ptr a#) ->
-                                recordMallocBc (A# a#)     `thenBc_`
+                            in  ioToBc (mallocBytes (n+1)) `thenBc` \ ptr ->
+                                recordMallocBc ptr         `thenBc_`
                                 ioToBc (
-                                   do memcpy (Ptr a#) ba (fromIntegral n)
-                                      writeCharOffAddr (A# a#) n '\0'
-                                      return (A# a#)
+                                   do memcpy ptr ba (fromIntegral n)
+                                     pokeByteOff ptr n (fromIntegral (ord '\0') :: Word8)
+                                      return ptr
                                    )
                          other -> panic "ByteCodeGen.pushAtom.pushStr"
              in
@@ -1406,7 +1409,7 @@ bind x f    = f x
 data BcM_State 
    = BcM_State { bcos      :: [ProtoBCO Name], -- accumulates completed BCOs
                  nextlabel :: Int,             -- for generating local labels
-                 malloced  :: [Addr] }         -- ptrs malloced for current BCO
+                 malloced  :: [Ptr ()] }       -- ptrs malloced for current BCO
                                                 -- Should be free()d when it is GCd
 type BcM r = BcM_State -> IO (BcM_State, r)
 
@@ -1441,20 +1444,20 @@ mapBc f (x:xs)
     mapBc f xs   `thenBc` \ rs ->
     returnBc (r:rs)
 
-emitBc :: ([Addr] -> ProtoBCO Name) -> BcM ()
+emitBc :: ([Ptr ()] -> ProtoBCO Name) -> BcM ()
 emitBc bco st
    = return (st{bcos = bco (malloced st) : bcos st, malloced=[]}, ())
 
 newbcoBc :: BcM ()
 newbcoBc st
-   | not (null (malloced st)) 
+   | notNull (malloced st)
    = panic "ByteCodeGen.newbcoBc: missed prior emitBc?"
    | otherwise
    = return (st, ())
 
-recordMallocBc :: Addr -> BcM ()
+recordMallocBc :: Ptr a -> BcM ()
 recordMallocBc a st
-   = return (st{malloced = a : malloced st}, ())
+   = return (st{malloced = castPtr a : malloced st}, ())
 
 getLabelBc :: BcM Int
 getLabelBc st