[project @ 2002-02-12 15:17:13 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeFFI.lhs
index ae74f63..480deab 100644 (file)
@@ -1,22 +1,26 @@
 %
-% (c) The University of Glasgow 2000
+% (c) The University of Glasgow 2001
 %
-\section[ByteCodeGen]{Generate bytecode from Core}
+\section[ByteCodeGen]{Generate machine-code sequences for foreign import}
 
 \begin{code}
-module ByteCodeFFI ( taggedSizeW, untaggedSizeW, mkMarshalCode ) where
+module ByteCodeFFI ( taggedSizeW, untaggedSizeW, mkMarshalCode, moan64 ) where
 
 #include "HsVersions.h"
 
 import Outputable
 import PrimRep         ( PrimRep(..), getPrimRepSize, isFollowableRep )
 import ForeignCall     ( CCallConv(..) )
-import Bits            ( Bits(..), shiftR )
+
+-- DON'T remove apparently unused imports here .. there is ifdeffery
+-- below
+import Bits            ( Bits(..), shiftR, shiftL )
+import Foreign         ( newArray )
+
 import Word            ( Word8, Word32 )
-import Addr            ( Addr(..), writeWord8OffAddr )
 import Foreign         ( Ptr(..), mallocBytes )
-import IOExts          ( unsafePerformIO, trace )
-
+import IOExts          ( trace, unsafePerformIO )
+import IO              ( hPutStrLn, stderr )
 \end{code}
 
 %************************************************************************
@@ -44,15 +48,6 @@ sizeOfTagW :: PrimRep -> Int
 sizeOfTagW pr
    | isFollowableRep pr = 0
    | otherwise          = 1
-
--- Blast a bunch of bytes into malloc'd memory and return the addr.
-sendBytesToMallocville :: [Word8] -> IO Addr
-sendBytesToMallocville bytes
-   = do let n = length bytes
-        (Ptr a#) <- mallocBytes n
-        mapM ( \(off,byte) -> writeWord8OffAddr (A# a#) off byte )
-             (zip [0 ..] bytes)
-        return (A# a#)
 \end{code}
 
 %************************************************************************
@@ -63,6 +58,21 @@ sendBytesToMallocville bytes
 
 \begin{code}
 
+moan64 :: String -> SDoc -> a
+moan64 msg pp_rep
+   = unsafePerformIO (
+        hPutStrLn stderr (
+        "\nGHCi's bytecode generation machinery can't handle 64-bit\n" ++
+        "code properly yet.  You can work around this for the time being\n" ++
+        "by compiling this module and all those it imports to object code,\n" ++
+        "and re-starting your GHCi session.  The panic below contains information,\n" ++
+        "intended for the GHC implementors, about the exact place where GHC gave up.\n"
+        )
+     )
+     `seq`
+     pprPanic msg pp_rep
+
+
 -- For sparc_TARGET_ARCH, i386_TARGET_ARCH, etc.
 #include "nativeGen/NCG.h"
 
@@ -83,11 +93,11 @@ we don't clear our own (single) arg off the C stack.
 -}
 mkMarshalCode :: CCallConv
               -> (Int, PrimRep) -> Int -> [(Int, PrimRep)] 
-              -> Addr
+              -> IO (Ptr Word8)
 mkMarshalCode cconv (r_offW, r_rep) addr_offW arg_offs_n_reps
    = let bytes = mkMarshalCode_wrk cconv (r_offW, r_rep) 
                                    addr_offW arg_offs_n_reps
-     in  unsafePerformIO (sendBytesToMallocville bytes)
+     in  Foreign.newArray bytes
 
 
 
@@ -96,10 +106,10 @@ mkMarshalCode_wrk :: CCallConv
                   -> (Int, PrimRep) -> Int -> [(Int, PrimRep)] 
                   -> [Word8]
 
-#if i386_TARGET_ARCH
-
 mkMarshalCode_wrk cconv (r_offW, r_rep) addr_offW arg_offs_n_reps
 
+#if i386_TARGET_ARCH
+
    = let -- Don't change this without first consulting Intel Corp :-)
          bytes_per_word = 4
 
@@ -112,9 +122,11 @@ mkMarshalCode_wrk cconv (r_offW, r_rep) addr_offW arg_offs_n_reps
               [ let -- where this arg's bits start
                     a_bits_offW = a_offW + sizeOfTagW a_rep
                 in 
+                    -- reversed because x86 is little-endian
                     reverse 
                     [a_bits_offW .. a_bits_offW + untaggedSizeW a_rep - 1]
 
+                -- reversed because args are pushed L -> R onto C stack
                 | (a_offW, a_rep) <- reverse arg_offs_n_reps
               ]
 
@@ -135,12 +147,14 @@ mkMarshalCode_wrk cconv (r_offW, r_rep) addr_offW arg_offs_n_reps
             = [0x81, 0xC4] ++ lit32 lit
          movl_eax_offesimem offB       -- movl   %eax, offB(%esi)
             = [0x89, 0x86] ++ lit32 offB
+         movl_edx_offesimem offB       -- movl   %edx, offB(%esi)
+            = [0x89, 0x96] ++ lit32 offB
          ret                           -- ret
             = [0xC3]
-         fstl_offesimem        offB            -- fstl   offB(%esi)
-            = [0xDD, 0x96] ++ lit32 offB
-         fsts_offesimem        offB            -- fsts   offB(%esi)
-            = [0xD9, 0x96] ++ lit32 offB
+         fstpl_offesimem offB          -- fstpl   offB(%esi)
+            = [0xDD, 0x9E] ++ lit32 offB
+         fstps_offesimem offB          -- fstps   offB(%esi)
+            = [0xD9, 0x9E] ++ lit32 offB
          lit32 :: Int -> [Word8]
          lit32 i = let w32 = (fromIntegral i) :: Word32
                    in  map (fromIntegral . ( .&. 0xFF))
@@ -246,18 +260,27 @@ mkMarshalCode_wrk cconv (r_offW, r_rep) addr_offW arg_offs_n_reps
            movl        %edx, 4(%esi)
            movl        %eax, 8(%esi)
         or
-           fstl        4(%esi)
+           fstpl       4(%esi)
         or
-           fsts        4(%esi)
+           fstps       4(%esi)
      -}
-     ++ case r_rep of
-           IntRep    -> movl_eax_offesimem 4
-           WordRep   -> movl_eax_offesimem 4
-           AddrRep   -> movl_eax_offesimem 4
-           DoubleRep -> fstl_offesimem 4
-           FloatRep  -> fsts_offesimem 4
+     ++ let i32 = movl_eax_offesimem 4
+            i64 = movl_eax_offesimem 4 ++ movl_edx_offesimem 8
+            f32 = fstps_offesimem 4
+            f64 = fstpl_offesimem 4
+        in
+        case r_rep of
+           CharRep   -> i32
+           IntRep    -> i32
+           WordRep   -> i32
+           AddrRep   -> i32
+           DoubleRep -> f64  
+           FloatRep  -> f32
+           -- Word64Rep -> i64
+           -- Int64Rep  -> i64
            VoidRep   -> []
-           other     -> pprPanic "ByteCodeFFI.mkMarshalCode_wrk(x86)" (ppr r_rep)
+           other     -> moan64 "ByteCodeFFI.mkMarshalCode_wrk(x86)" 
+                               (ppr r_rep)
 
      {- Restore all the pushed regs and go home.
 
@@ -275,7 +298,198 @@ mkMarshalCode_wrk cconv (r_offW, r_rep) addr_offW arg_offs_n_reps
      ++ ret
      )
 
-#endif /* i386_TARGET_ARCH */
+#elif sparc_TARGET_ARCH
+
+   = let -- At least for sparc V8
+         bytes_per_word = 4
+
+         -- speaks for itself
+         w32_to_w8s_bigEndian :: Word32 -> [Word8]
+         w32_to_w8s_bigEndian w
+            =  [fromIntegral (0xFF .&. (w `shiftR` 24)),
+                fromIntegral (0xFF .&. (w `shiftR` 16)),
+                fromIntegral (0xFF .&. (w `shiftR` 8)),
+                fromIntegral (0xFF .&. w)]
+
+         -- addr and result bits offsetsW
+         offset_of_addr_bitsW = addr_offW + sizeOfTagW AddrRep
+         offset_of_res_bitsW  = r_offW + sizeOfTagW r_rep
+
+         offsets_to_pushW
+            = concat
+              [ let -- where this arg's bits start
+                    a_bits_offW = a_offW + sizeOfTagW a_rep
+                in 
+                    [a_bits_offW .. a_bits_offW + untaggedSizeW a_rep - 1]
+
+                | (a_offW, a_rep) <- arg_offs_n_reps
+              ]
+
+         total_argWs    = length offsets_to_pushW
+         argWs_on_stack = if total_argWs > 6 then total_argWs - 6 
+                                             else 0
+
+         -- The stack pointer must be kept 8-byte aligned, which means
+         -- we need to calculate this quantity too
+         argWs_on_stack_ROUNDED_UP
+            | odd argWs_on_stack = 1 + argWs_on_stack
+            | otherwise          = argWs_on_stack
+
+         -- some helpers to assemble sparc insns.
+         -- REGS
+         iReg, oReg, gReg, fReg :: Int -> Word32
+         iReg = fromIntegral . (+ 24)
+         oReg = fromIntegral . (+ 8)
+         gReg = fromIntegral . (+ 0)
+         fReg = fromIntegral
+
+         sp = oReg 6
+         i0 = iReg 0
+         i7 = iReg 7
+         o0 = oReg 0
+         o1 = oReg 1
+         o7 = oReg 7
+         g0 = gReg 0
+         g1 = gReg 1
+         f0 = fReg 0
+         f1 = fReg 1
+
+         -- INSN templates
+         insn_r_r_i :: Word32 -> Word32 -> Word32 -> Int -> Word32
+         insn_r_r_i op3 rs1 rd imm13
+            = (3 `shiftL` 30) 
+              .|. (rs1 `shiftL` 25)
+              .|. (op3 `shiftL` 19)
+              .|. (rd `shiftL` 14) 
+              .|. (1 `shiftL` 13) 
+              .|. mkSimm13 imm13
+
+         insn_r_i_r :: Word32 -> Word32 -> Int -> Word32 -> Word32
+         insn_r_i_r op3 rs1 imm13 rd
+            = (2 `shiftL` 30) 
+              .|. (rd `shiftL` 25)
+              .|. (op3 `shiftL` 19)
+              .|. (rs1 `shiftL` 14) 
+              .|. (1 `shiftL` 13) 
+              .|. mkSimm13 imm13
+
+         mkSimm13 :: Int -> Word32
+         mkSimm13 imm13 
+            = let imm13w = (fromIntegral imm13) :: Word32
+              in  imm13w .&. 0x1FFF             
+
+         -- REAL (non-synthetic) insns
+         -- or %rs1, %rs2, %rd
+         mkOR :: Word32 -> Word32 -> Word32 -> Word32
+         mkOR rs1 rs2 rd 
+            = (2 `shiftL` 30) 
+              .|. (rd `shiftL` 25)
+              .|. (op3_OR `shiftL` 19)
+              .|. (rs1 `shiftL` 14) 
+              .|. (0 `shiftL` 13) 
+              .|. rs2
+              where op3_OR = 2 :: Word32
+
+         -- ld(int)   [%rs + imm13], %rd
+         mkLD rs1 imm13 rd = insn_r_r_i 0x00{-op3_LD-} rd rs1 imm13
+
+         -- st(int)   %rs, [%rd + imm13]
+         mkST   = insn_r_r_i 0x04 -- op3_ST
+
+         -- st(float) %rs, [%rd + imm13]
+         mkSTF  = insn_r_r_i 0x24 -- op3_STF
+
+         -- jmpl     %rs + imm13, %rd
+         mkJMPL = insn_r_i_r 0x38 -- op3_JMPL
+
+         -- save     %rs + imm13, %rd
+         mkSAVE = insn_r_i_r 0x3C -- op3_SAVE
+
+         -- restore  %rs + imm13, %rd
+         mkRESTORE = insn_r_i_r 0x3D -- op3_RESTORE
+
+         -- SYNTHETIC insns
+         mkNOP             = mkOR g0 g0 g0
+         mkCALL reg        = mkJMPL reg 0 o7
+         mkRET             = mkJMPL i7 8 g0
+         mkRESTORE_TRIVIAL = mkRESTORE g0 0 g0
+     in
+     --trace (show (map fst arg_offs_n_reps))
+     concatMap w32_to_w8s_bigEndian (
+
+     {- On entry, %o0 is the arg passed from the interpreter.  After
+        the initial save insn, it will be in %i0.  Studying the sparc
+        docs one would have thought that the minimum frame size is 92
+        bytes, but gcc always uses at least 112, and indeed there are
+        segfaults a-plenty with 92.  So I use 112 here as well.  I
+        don't understand why, tho.  
+     -}
+     [mkSAVE sp (- ({-92-}112 + 4*argWs_on_stack_ROUNDED_UP)) sp]
+
+     {- For each arg in args_offs_n_reps, examine the associated PrimRep 
+        to determine how many payload (non-tag) words there are, and 
+        whether or not there is a tag.  This gives a bunch of offsets on 
+        the H stack.  Move the first 6 words into %o0 .. %o5 and the
+        rest on the stack, starting at [%sp+92].  Use %g1 as a temp.
+     -}
+     ++ let doArgW (offW, wordNo)
+              | wordNo < 6
+              = [mkLD i0 (bytes_per_word * offW) (oReg wordNo)]
+              | otherwise
+              = [mkLD i0 (bytes_per_word * offW) g1,
+                 mkST g1 sp (92 + bytes_per_word * (wordNo - 6))]
+        in  
+            concatMap doArgW (zip offsets_to_pushW [0 ..])
+
+     {- Get the addr to call into %g1, bearing in mind that there's 
+        an Addr# tag at the indicated location, and do the call:
+
+           ld     [4*(1 /*tag*/ +addr_offW) + %i0], %g1
+           call   %g1
+     -}
+     ++ [mkLD i0 (bytes_per_word * offset_of_addr_bitsW) g1,
+         mkCALL g1,
+         mkNOP]
+
+     {- Depending on what the return type is, get the result 
+        from %o0 or %o1:%o0 or %f0 or %f1:%f0.
+
+           st          %o0, [%i0 + 4]        -- 32 bit int
+        or
+           st          %o0, [%i0 + 4]        -- 64 bit int
+           st          %o1, [%i0 + 8]        -- or the other way round?
+        or
+           st          %f0, [%i0 + 4]        -- 32 bit float
+        or
+           st          %f0, [%i0 + 4]        -- 64 bit float
+           st          %f1, [%i0 + 8]        -- or the other way round?
+
+     -}
+     ++ let i32 = [mkST o0 i0 4]
+            i64 = [mkST o0 i0 4, mkST o1 i0 8]
+            f32 = [mkSTF f0 i0 4]
+            f64 = [mkSTF f0 i0 4, mkSTF f1 i0 8]
+        in
+            case r_rep of
+               CharRep   -> i32
+               IntRep    -> i32
+               WordRep   -> i32
+               AddrRep   -> i32
+               DoubleRep -> f64
+               FloatRep  -> f32
+               VoidRep   -> []
+               other     -> moan64 "ByteCodeFFI.mkMarshalCode_wrk(sparc)" 
+                                   (ppr r_rep)
+
+     ++ [mkRET,
+         mkRESTORE_TRIVIAL]  -- this is in the delay slot of the RET
+     )
+
+#else 
+
+   = undefined
+
+#endif
 
 \end{code}