[project @ 2001-02-19 11:38:55 by sewardj]
[ghc-hetmet.git] / ghc / compiler / nativeGen / MachCode.lhs
index a586a4a..cfea55e 100644 (file)
@@ -9,7 +9,7 @@ This is a big module, but, if you pay attention to
 structure should not be too overwhelming.
 
 \begin{code}
-module MachCode ( stmt2Instrs, InstrBlock ) where
+module MachCode ( stmtsToInstrs, InstrBlock ) where
 
 #include "HsVersions.h"
 #include "nativeGen/NCG.h"
@@ -56,9 +56,85 @@ x `bind` f = f x
 Code extractor for an entire stix tree---stix statement level.
 
 \begin{code}
-stmt2Instrs :: StixTree {- a stix statement -} -> NatM InstrBlock
+stmtsToInstrs :: [StixTree] -> NatM InstrBlock
+stmtsToInstrs stmts
+   = liftStrings stmts [] []           `thenNat` \ lifted ->
+     mapNat stmtToInstrs lifted                `thenNat` \ instrss ->
+     returnNat (concatOL instrss)
+
+
+-- Lift StStrings out of top-level StDatas, putting them at the end of
+-- the block, and replacing them with StCLbls which refer to the lifted-out strings. 
+{- Motivation for this hackery provided by the following bug:
+   Stix:
+      (DataSegment)
+      Bogon.ping_closure :
+      (Data P_ Addr.A#_static_info)
+      (Data StgAddr (Str `alalal'))
+      (Data P_ (0))
+   results in:
+      .data
+              .align 8
+      .global Bogon_ping_closure
+      Bogon_ping_closure:
+              .long   Addr_Azh_static_info
+              .long   .Ln1a8
+      .Ln1a8:
+              .byte   0x61
+              .byte   0x6C
+              .byte   0x61
+              .byte   0x6C
+              .byte   0x61
+              .byte   0x6C
+              .byte   0x00
+              .long   0
+   ie, the Str is planted in-line, when what we really meant was to place
+   a _reference_ to the string there.  liftStrings will lift out all such
+   strings in top-level data and place them at the end of the block.
+
+   This is still a rather half-baked solution -- to do the job entirely right
+   would mean a complete traversal of all the Stixes, but there's currently no
+   real need for it, and it would be slow.  Also, potentially there could be
+   literal types other than strings which need lifting out?
+-}
+
+liftStrings :: [StixTree]    -- originals
+            -> [StixTree]    -- (reverse) originals with strings lifted out
+            -> [(CLabel, FAST_STRING)]   -- lifted strs, and their new labels
+            -> NatM [StixTree]
+
+-- First, examine the original trees and lift out strings in top-level StDatas.
+liftStrings (st:sts) acc_stix acc_strs
+   = case st of
+        StData sz datas
+           -> lift datas acc_strs      `thenNat` \ (datas_done, acc_strs1) ->
+              liftStrings sts ((StData sz datas_done):acc_stix) acc_strs1
+        other 
+           -> liftStrings sts (other:acc_stix) acc_strs
+     where
+        -- Handle a top-level StData
+        lift []     acc_strs = returnNat ([], acc_strs)
+        lift (d:ds) acc_strs
+           = lift ds acc_strs          `thenNat` \ (ds_done, acc_strs1) ->
+             case d of
+                StString s 
+                   -> getNatLabelNCG   `thenNat` \ lbl ->
+                      returnNat ((StCLbl lbl):ds_done, ((lbl,s):acc_strs1))
+                other
+                   -> returnNat (other:ds_done, acc_strs1)
+
+-- When we've run out of original trees, emit the lifted strings.
+liftStrings [] acc_stix acc_strs
+   = returnNat (reverse acc_stix ++ concatMap f acc_strs)
+     where
+        f (lbl,str) = [StSegment RoDataSegment, 
+                       StLabel lbl, 
+                       StString str, 
+                       StSegment TextSegment]
+
 
-stmt2Instrs stmt = case stmt of
+stmtToInstrs :: StixTree {- a stix statement -} -> NatM InstrBlock
+stmtToInstrs stmt = case stmt of
     StComment s    -> returnNat (unitOL (COMMENT s))
     StSegment seg  -> returnNat (unitOL (SEGMENT seg))
 
@@ -92,21 +168,22 @@ stmt2Instrs stmt = case stmt of
                     `consOL`  concatOL codes)
       where
        getData :: StixTree -> NatM (InstrBlock, Imm)
-
        getData (StInt i)        = returnNat (nilOL, ImmInteger i)
        getData (StDouble d)     = returnNat (nilOL, ImmDouble d)
        getData (StFloat d)      = returnNat (nilOL, ImmFloat d)
        getData (StCLbl l)       = returnNat (nilOL, ImmCLbl l)
-       getData (StString s)     =
-           getNatLabelNCG                  `thenNat` \ lbl ->
-           returnNat (toOL [LABEL lbl,
-                            ASCII True (_UNPK_ s)],
-                       ImmCLbl lbl)
+       getData (StString s)     = panic "MachCode.stmtToInstrs: unlifted StString"
        -- the linker can handle simple arithmetic...
        getData (StIndex rep (StCLbl lbl) (StInt off)) =
                returnNat (nilOL, 
                            ImmIndex lbl (fromInteger (off * sizeOf rep)))
 
+    -- Top-level lifted-out string.  The segment will already have been set
+    -- (see liftStrings above).
+    StString str
+      -> returnNat (unitOL (ASCII True (_UNPK_ str)))
+
+
 -- Walk a Stix tree, and insert dereferences to CLabels which are marked
 -- as labelDynamic.  stmt2Instrs calls derefDLL selectively, because
 -- not all such CLabel occurrences need this dereferencing -- SRTs don't
@@ -156,14 +233,17 @@ mangleIndexTree (StIndex pk base off)
   = StPrim IntAddOp [
        base,
        let s = shift pk
-       in  ASSERT(toInteger s == expectJust "MachCode" (exactLog2 (sizeOf pk)))
-           if s == 0 then off else StPrim SllOp [off, StInt s]
+       in  if s == 0 then off else StPrim SllOp [off, StInt (toInteger s)]
       ]
   where
-    shift DoubleRep    = 3::Integer
-    shift CharRep       = 2::Integer
-    shift Int8Rep       = 0::Integer
-    shift _            = IF_ARCH_alpha(3,2)
+    shift :: PrimRep -> Int
+    shift rep = case (fromInteger (sizeOf rep) :: Int) of
+                   1 -> 0
+                   2 -> 1
+                   4 -> 2
+                   8 -> 3
+                   other -> pprPanic "MachCode.mangleIndexTree.shift: unhandled rep size" 
+                                     (int other)
 \end{code}
 
 \begin{code}
@@ -262,7 +342,7 @@ getRegister (StString s)
        imm_lbl = ImmCLbl lbl
 
        code dst = toOL [
-           SEGMENT DataSegment,
+           SEGMENT RoDataSegment,
            LABEL lbl,
            ASCII True (_UNPK_ s),
            SEGMENT TextSegment,
@@ -553,7 +633,7 @@ getRegister (StScratchWord i)
    = getDeltaNat `thenNat` \ current_stack_offset ->
      let j = i+1   - (current_stack_offset `div` 4)
          code dst
-           = unitOL (LEA L (OpAddr (spRel (j+1))) (OpReg dst))
+           = unitOL (LEA L (OpAddr (spRel j)) (OpReg dst))
      in 
      returnNat (Any PtrRep code)
 
@@ -843,8 +923,8 @@ getRegister (StInd pk mem)
                      if   pk == DoubleRep || pk == FloatRep
                      then GLD size src dst
                      else case size of
-                             L -> MOV L    (OpAddr src) (OpReg dst)
-                             B -> MOVZxL B (OpAddr src) (OpReg dst)
+                             L  -> MOV L     (OpAddr src) (OpReg dst)
+                             BU -> MOVZxL BU (OpAddr src) (OpReg dst)
     in
        returnNat (Any pk code__2)
 
@@ -904,8 +984,8 @@ getRegister (StDouble d)
 -- Below that is the spill area.
 getRegister (StScratchWord i)
    | i >= 0 && i < 6
-   = let j        = i+1
-         code dst = unitOL (fpRelEA j dst)
+   = let
+         code dst = unitOL (fpRelEA (i-6) dst)
      in 
      returnNat (Any PtrRep code)
 
@@ -918,8 +998,6 @@ getRegister (StPrim primop [x]) -- unary PrimOps
       FloatNegOp     -> trivialUFCode FloatRep (FNEG F) x
       DoubleNegOp    -> trivialUFCode DoubleRep (FNEG DF) x
 
-      DoubleNegOp -> trivialUFCode DoubleRep (FNEG DF) x
-
       Double2FloatOp -> trivialUFCode FloatRep  (FxTOy DF F) x
       Float2DoubleOp -> trivialUFCode DoubleRep (FxTOy F DF) x
 
@@ -1727,7 +1805,7 @@ assignIntCode pk dst (StInd pks src)
        c_dst = registerCode reg_dst tmp  -- should be empty
        r_dst = registerName reg_dst tmp
        szs   = primRepToSize pks
-        opc   = case szs of L -> MOV L ; B -> MOVZxL B
+        opc   = case szs of L -> MOV L ; BU -> MOVZxL BU
 
        code  | isNilOL c_dst
               = c_addr `snocOL`