X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FnativeGen%2FStixPrim.lhs;h=7583e1cd271cbda5e9bd3f8913275d688358701e;hb=a7d8f43718b167689c0a4a4c23b33a325e0239f1;hp=9f014888ab885b2d4c1da7c9c0e904c0157a0ab3;hpb=a5f7799965947977599a777dae10f103f9b9fd1a;p=ghc-hetmet.git diff --git a/ghc/compiler/nativeGen/StixPrim.lhs b/ghc/compiler/nativeGen/StixPrim.lhs index 9f01488..7583e1c 100644 --- a/ghc/compiler/nativeGen/StixPrim.lhs +++ b/ghc/compiler/nativeGen/StixPrim.lhs @@ -3,39 +3,54 @@ % \begin{code} -module StixPrim ( primCode, amodeToStix, amodeToStix' ) where +module StixPrim ( amodeToStix, amodeToStix', foreignCallCode ) +where #include "HsVersions.h" -import MachMisc -import MachRegs +-- import MachMisc import Stix -import StixInteger +import PprAbsC ( pprAmode ) import AbsCSyn hiding ( spRel ) import AbsCUtils ( getAmodeRep, mixedTypeLocn ) import SMRep ( fixedHdrSize ) -import Const ( Literal(..) ) -import CallConv ( cCallConv ) -import PrimOp ( PrimOp(..) ) -import PrimRep ( PrimRep(..), isFloatingRep ) -import UniqSupply ( returnUs, thenUs, UniqSM ) -import Constants ( mIN_INTLIKE ) +import Literal ( Literal(..), word2IntLit ) +import MachOp ( MachOp(..) ) +import PrimRep ( PrimRep(..), getPrimRepSizeInBytes ) +import UniqSupply ( returnUs, thenUs, getUniqueUs, UniqSM ) +import Constants ( wORD_SIZE, + mIN_INTLIKE, mIN_CHARLIKE, uF_UPDATEE, bLOCK_SIZE, + rESERVED_STACK_WORDS ) +import CLabel ( mkIntlikeClosureLabel, mkCharlikeClosureLabel, + mkMAP_FROZEN_infoLabel, + mkForeignLabel ) +import ForeignCall ( ForeignCall(..), CCallSpec(..), CCallTarget(..), + CCallConv(..), playSafe, playThreadSafe ) import Outputable +import Util ( notNull ) +import FastString +import FastTypes -import Char ( ord ) +#include "NCG.h" \end{code} -The main honcho here is primCode, which handles the guts of COpStmts. +The main honchos here are primCode and foreignCallCode, which handle the guts of COpStmts. \begin{code} -primCode +foreignCallCode :: [CAddrMode] -- results - -> PrimOp -- op + -> ForeignCall -- op -> [CAddrMode] -- args - -> UniqSM StixTreeList + -> UniqSM StixStmtList \end{code} +%************************************************************************ +%* * +\subsubsection{Code for foreign calls} +%* * +%************************************************************************ + First, the dreaded @ccall@. We can't handle @casm@s. Usually, this compiles to an assignment, but when the left-hand side @@ -44,259 +59,85 @@ is empty, we just perform the call and ignore the result. btw Why not let programmer use casm to provide assembly code instead of C code? ADR -The (MP) integer operations are a true nightmare. Since we don't have -a convenient abstract way of allocating temporary variables on the (C) -stack, we use the space just below HpLim for the @MP_INT@ structures, -and modify our heap check accordingly. +ToDo: saving/restoring of volatile regs around ccalls. -\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} +JRS, 001113: always do the call of suspendThread and resumeThread as a ccall +rather than inheriting the calling convention of the thing which we're really +calling. \begin{code} -primCode [res] IntegerCmpOp args@[aa1,sa1,da1, aa2,sa2,da2] - = gmpCompare res (aa1,sa1,da1, aa2,sa2,da2) - -primCode [res] Integer2IntOp arg@[aa,sa,da] - = gmpInteger2Int res (aa,sa,da) - -primCode [res] Integer2WordOp arg@[aa,sa,da] - = gmpInteger2Word res (aa,sa,da) - -primCode [res] Int2AddrOp [arg] - = simpleCoercion AddrRep res arg - -primCode [res] Addr2IntOp [arg] - = simpleCoercion IntRep res arg - -primCode [res] Int2WordOp [arg] - = simpleCoercion IntRep{-WordRep?-} res arg - -primCode [res] Word2IntOp [arg] - = simpleCoercion IntRep res arg -\end{code} - -\begin{code} -primCode [res] SameMutableArrayOp args - = let - compare = StPrim AddrEqOp (map amodeToStix args) - assign = StAssign IntRep (amodeToStix res) compare - in - returnUs (\xs -> assign : xs) - -primCode res@[_] SameMutableByteArrayOp args - = primCode res SameMutableArrayOp args -\end{code} - -Freezing an array of pointers is a double assignment. We fix the -header of the ``new'' closure because the lhs is probably a better -addressing mode for the indirection (most likely, it's a VanillaReg). - -\begin{code} - -primCode [lhs] UnsafeFreezeArrayOp [rhs] - = let - lhs' = amodeToStix lhs - rhs' = amodeToStix rhs - header = StInd PtrRep lhs' - assign = StAssign PtrRep lhs' rhs' - freeze = StAssign PtrRep header mutArrPtrsFrozen_info - in - returnUs (\xs -> assign : freeze : xs) - -primCode [lhs] UnsafeFreezeByteArrayOp [rhs] - = simpleCoercion PtrRep lhs rhs -primCode [lhs] UnsafeThawByteArrayOp [rhs] - = simpleCoercion PtrRep lhs rhs -\end{code} - -Returning the size of (mutable) byte arrays is just -an indexing operation. - -\begin{code} -primCode [lhs] SizeofByteArrayOp [rhs] - = let - lhs' = amodeToStix lhs - rhs' = amodeToStix rhs - sz = StIndex IntRep rhs' fixedHS - assign = StAssign IntRep lhs' (StInd IntRep sz) - in - returnUs (\xs -> assign : xs) - -primCode [lhs] SizeofMutableByteArrayOp [rhs] - = let - lhs' = amodeToStix lhs - rhs' = amodeToStix rhs - sz = StIndex IntRep rhs' fixedHS - assign = StAssign IntRep lhs' (StInd IntRep sz) - in - returnUs (\xs -> assign : xs) - -\end{code} - -Most other array primitives translate to simple indexing. - -\begin{code} - -primCode lhs@[_] IndexArrayOp args - = primCode lhs ReadArrayOp args - -primCode [lhs] ReadArrayOp [obj, ix] - = let - lhs' = amodeToStix lhs - obj' = amodeToStix obj - ix' = amodeToStix ix - base = StIndex IntRep obj' arrHS - assign = StAssign PtrRep lhs' (StInd PtrRep (StIndex PtrRep base ix')) - in - returnUs (\xs -> assign : xs) - -primCode [] WriteArrayOp [obj, ix, v] - = let - obj' = amodeToStix obj - ix' = amodeToStix ix - v' = amodeToStix v - base = StIndex IntRep obj' arrHS - assign = StAssign PtrRep (StInd PtrRep (StIndex PtrRep base ix')) v' - in - returnUs (\xs -> assign : xs) - -primCode lhs@[_] (IndexByteArrayOp pk) args - = primCode lhs (ReadByteArrayOp pk) args +foreignCallCode lhs call@(CCall (CCallSpec ctarget cconv safety)) rhs --- NB: indexing in "pk" units, *not* in bytes (WDP 95/09) + | not (playSafe safety) + = returnUs (\xs -> ccall : xs) -primCode [lhs] (ReadByteArrayOp pk) [obj, ix] - = let - lhs' = amodeToStix lhs - obj' = amodeToStix obj - ix' = amodeToStix ix - base = StIndex IntRep obj' arrHS - assign = StAssign pk lhs' (StInd pk (StIndex pk base ix')) - in - returnUs (\xs -> assign : xs) - -primCode [lhs] (IndexOffAddrOp pk) [obj, ix] - = let - lhs' = amodeToStix lhs - obj' = amodeToStix obj - ix' = amodeToStix ix - assign = StAssign pk lhs' (StInd pk (StIndex pk obj' ix')) - in - returnUs (\xs -> assign : xs) - -primCode [lhs] (IndexOffForeignObjOp pk) [obj, ix] - = let - lhs' = amodeToStix lhs - obj' = amodeToStix obj - ix' = amodeToStix ix - obj'' = StIndex PtrRep obj' fixedHS - assign = StAssign pk lhs' (StInd pk (StIndex pk obj'' ix')) - 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 - assign = StAssign pk (StInd pk (StIndex pk base ix')) v' - in - returnUs (\xs -> assign : xs) -\end{code} - -\begin{code} ---primCode lhs (CCallOp fn is_asm may_gc) rhs -primCode lhs (CCallOp (Left 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) - 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 - ForeignObjRep -> StIndex PtrRep base fixedHS - _ -> base -\end{code} - -Now the more mundane operations. - -\begin{code} -primCode lhs op rhs - = let - lhs' = map amodeToStix lhs - rhs' = map amodeToStix' rhs - pk = getAmodeRep (head lhs) + = save_thread_state `thenUs` \ save -> + load_thread_state `thenUs` \ load -> + getUniqueUs `thenUs` \ uniq -> + let + id = StixTemp (StixVReg uniq IntRep) + + is_threadSafe + | playThreadSafe safety = 1 + | otherwise = 0 + + suspend = StAssignReg IntRep id + (StCall (Left FSLIT("suspendThread")) {-no:cconv-} CCallConv + IntRep [StReg stgBaseReg, StInt is_threadSafe ]) + resume = StVoidable + (StCall (Left FSLIT("resumeThread")) {-no:cconv-} CCallConv + VoidRep [StReg id, StInt is_threadSafe ]) in - returnUs (\ xs -> simplePrim pk lhs' op rhs' : xs) -\end{code} - -\begin{code} -simpleCoercion - :: PrimRep - -> CAddrMode - -> CAddrMode - -> UniqSM StixTreeList - -simpleCoercion pk lhs rhs - = returnUs (\xs -> StAssign pk (amodeToStix lhs) (amodeToStix rhs) : xs) -\end{code} - -Here we try to rewrite primitives into a form the code generator can -understand. Any primitives not handled here must be handled at the -level of the specific code generator. - -\begin{code} -simplePrim - :: PrimRep -- Rep of first destination - -> [StixTree] -- Destinations - -> PrimOp - -> [StixTree] - -> StixTree -\end{code} - -Now look for something more conventional. - -\begin{code} -simplePrim pk [lhs] op rest = StAssign pk lhs (StPrim op rest) -simplePrim pk as op bs = simplePrim_error op + returnUs (\xs -> save (suspend : ccall : resume : load xs)) -simplePrim_error op - = error ("ERROR: primitive operation `"++show op++"'cannot be handled\nby the native-code generator. Workaround: use -fvia-C.\n(Perhaps you should report it as a GHC bug, also.)\n") + where + (cargs, stix_target) + = case ctarget of + StaticTarget nm -> (rhs, Left nm) + DynamicTarget | notNull rhs -- an assertion + -> (tail rhs, Right (amodeToStix (head rhs))) + CasmTarget _ + -> ncgPrimopMoan "Native code generator can't handle foreign call" + (ppr call) + + stix_args = map amodeToStix' cargs + + ccall = case lhs of + [] -> StVoidable (StCall stix_target cconv VoidRep stix_args) + [lhs] -> mkStAssign pk lhs' (StCall stix_target cconv pk stix_args) + where + lhs' = amodeToStix lhs + pk = case getAmodeRep lhs of + FloatRep -> FloatRep + DoubleRep -> DoubleRep + Int64Rep -> Int64Rep + Word64Rep -> Word64Rep + other -> IntRep + +-- a bit late to catch this here.. +foreignCallCode _ DNCall{} _ + = panic "foreignCallCode: .NET interop not supported via NCG; compile with -fvia-C" \end{code} -%--------------------------------------------------------------------- - -Here we generate the Stix code for CAddrModes. +%************************************************************************ +%* * +\subsubsection{Code for @CAddrMode@s} +%* * +%************************************************************************ When a character is fetched from a mixed type location, we have to do an extra cast. This is reflected in amodeCode', which is for rhs amodes that might possibly need the extra cast. \begin{code} -amodeToStix, amodeToStix' :: CAddrMode -> StixTree +amodeToStix, amodeToStix' :: CAddrMode -> StixExpr amodeToStix'{-'-} am@(CVal rr CharRep) - | mixedTypeLocn am = StPrim ChrOp [amodeToStix am] - | otherwise = amodeToStix am - -amodeToStix' am = amodeToStix am + | mixedTypeLocn am = StMachOp MO_NatS_to_32U [amodeToStix am] + | otherwise = amodeToStix am +amodeToStix' am + = amodeToStix am ----------- amodeToStix am@(CVal rr CharRep) @@ -306,68 +147,88 @@ amodeToStix am@(CVal rr CharRep) amodeToStix (CVal rr pk) = StInd pk (amodeToStix (CAddr rr)) amodeToStix (CAddr (SpRel off)) - = StIndex PtrRep stgSp (StInt (toInteger IBOX(off))) + = StIndex PtrRep (StReg stgSp) (StInt (toInteger (iBox off))) amodeToStix (CAddr (HpRel off)) - = StIndex IntRep stgHp (StInt (toInteger (- IBOX(off)))) + = StIndex IntRep (StReg stgHp) (StInt (toInteger (- (iBox off)))) amodeToStix (CAddr (NodeRel off)) - = StIndex IntRep stgNode (StInt (toInteger IBOX(off))) + = StIndex IntRep (StReg 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 (CTemp uniq pk) = StReg (StixTemp (StixVReg uniq pk)) amodeToStix (CLbl lbl _) = StCLbl lbl -- For CharLike and IntLike, we attempt some trivial constant-folding here. amodeToStix (CCharLike (CLit (MachChar c))) - = StLitLbl ((<>) (ptext SLIT("CHARLIKE_closure+")) (int off)) + = StIndex Word8Rep cHARLIKE_closure (StInt (toInteger off)) where - off = charLikeSize * ord c + off = charLikeSize * (c - mIN_CHARLIKE) amodeToStix (CCharLike x) - = StIndex PtrRep charLike off - where - off = StPrim IntMulOp [amodeToStix x, StInt (toInteger (fixedHdrSize+1))] + = panic "amodeToStix.CCharLike" -amodeToStix (CIntLike (CLit (MachInt i _))) - = StLitLbl ((<>) (ptext SLIT("INTLIKE_closure+")) (int off)) +amodeToStix (CIntLike (CLit (MachInt i))) + = StIndex Word8Rep iNTLIKE_closure (StInt (toInteger off)) where off = intLikeSize * (fromInteger (i - mIN_INTLIKE)) amodeToStix (CIntLike x) - = panic "CIntLike" + = panic "amodeToStix.CIntLike" amodeToStix (CLit core) = case core of - MachChar c -> StInt (toInteger (ord c)) + MachChar c -> StInt (toInteger c) MachStr s -> StString s - MachAddr a -> StInt a - MachInt i _ -> StInt (toInteger i) - MachLitLit s _ -> StLitLit s - MachFloat d -> StDouble d + MachNullAddr -> StInt 0 + MachInt i -> StInt i + MachWord w -> case word2IntLit core of MachInt iw -> StInt iw + MachLitLit s _ -> litLitErr + -- dreadful, but rare. + MachLabel l (Just x) -> StCLbl (mkForeignLabel (mkFastString (unpackFS l ++ '@':show x)) False) + MachLabel l _ -> StCLbl (mkForeignLabel l False{-ToDo: dynamic-}) + MachFloat d -> StFloat d MachDouble d -> StDouble d _ -> panic "amodeToStix:core literal" - -- A CLitLit is just a (CLit . MachLitLit) -amodeToStix (CLitLit s _) = StLitLit 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]), + = let + arg_amode = amodeToStix arg + in + case macro of + ENTRY_CODE -> arg_amode + ARG_TAG -> arg_amode -- just an integer no. of words + GET_TAG -> +#ifdef WORDS_BIGENDIAN + StMachOp MO_Nat_And + [StInd WordRep (StIndex PtrRep arg_amode + (StInt (toInteger (-1)))), + StInt 65535] +#else + StMachOp MO_Nat_Shr + [StInd WordRep (StIndex PtrRep arg_amode + (StInt (toInteger (-1)))), StInt 16] +#endif + UPD_FRAME_UPDATEE + -> StInd PtrRep (StIndex PtrRep arg_amode + (StInt (toInteger uF_UPDATEE))) + + BYTE_ARR_CTS -> StIndex IntRep arg_amode arrWordsHS + PTRS_ARR_CTS -> StIndex PtrRep arg_amode arrPtrsHS + ForeignObj_CLOSURE_DATA -> StInd PtrRep (StIndex PtrRep arg_amode fixedHS) + --- XXX!!! --- GET_TAG(info_ptr) is supposed to be get_itbl(info_ptr)->srt_len, --- which we've had to hand-code here. +amodeToStix other + = pprPanic "StixPrim.amodeToStix" (pprAmode other) + +litLitErr + = ncgPrimopMoan "native code generator can't handle lit-lits" empty \end{code} Sizes of the CharLike and IntLike closures that are arranged as arrays @@ -376,25 +237,87 @@ 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 :: StixExpr +iNTLIKE_closure = StCLbl mkIntlikeClosureLabel -- The CHARLIKE base -charLike :: StixTree +cHARLIKE_closure :: StixExpr +cHARLIKE_closure = StCLbl mkCharlikeClosureLabel -charLike = sStLitLbl SLIT("CHARLIKE_closure") +mutArrPtrsFrozen_info = StCLbl mkMAP_FROZEN_infoLabel --- Trees for the ErrorIOPrimOp - -topClosure, errorIO :: StixTree - -topClosure = StInd PtrRep (sStLitLbl SLIT("TopClosure")) -errorIO = StJump (StInd PtrRep (sStLitLbl SLIT("ErrorIO_innards"))) +-- these are the sizes of charLike and intLike closures, in _bytes_. +charLikeSize = (fixedHdrSize + 1) * (getPrimRepSizeInBytes PtrRep) +intLikeSize = (fixedHdrSize + 1) * (getPrimRepSizeInBytes PtrRep) +\end{code} -mutArrPtrsFrozen_info = sStLitLbl SLIT("MUT_ARR_PTRS_FROZEN_info") -charLikeSize = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep)) -intLikeSize = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep)) +\begin{code} +save_thread_state + = getUniqueUs `thenUs` \ tso_uq -> + let tso = StixTemp (StixVReg tso_uq PtrRep) in + returnUs (\xs -> + StAssignReg PtrRep tso (StReg stgCurrentTSO) + : StAssignMem PtrRep + (StMachOp MO_Nat_Add + [StReg tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))]) + (StReg stgSp) + : StAssignMem PtrRep + (StMachOp MO_Nat_Add + [StReg stgCurrentNursery, + StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))]) + (StMachOp MO_Nat_Add + [StReg stgHp, StInt (toInteger (1 * BYTES_PER_WORD))]) + : xs + ) + +load_thread_state + = getUniqueUs `thenUs` \ tso_uq -> + let tso = StixTemp (StixVReg tso_uq PtrRep) in + returnUs (\xs -> + StAssignReg PtrRep tso (StReg stgCurrentTSO) + : StAssignReg PtrRep + stgSp + (StInd PtrRep + (StMachOp MO_Nat_Add + [StReg tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))])) + : StAssignReg PtrRep + stgSpLim + (StMachOp MO_Nat_Add + [StReg tso, + StInt (toInteger ((TSO_STACK + rESERVED_STACK_WORDS) + *BYTES_PER_WORD))]) + : StAssignReg PtrRep + stgHp + (StMachOp MO_Nat_Sub + [StInd PtrRep + (StMachOp MO_Nat_Add + [StReg stgCurrentNursery, + StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))]), + StInt (toInteger (1 * BYTES_PER_WORD)) + ]) + : StAssignReg PtrRep + stgHpLim + (StIndex Word8Rep + (StInd PtrRep + (StIndex PtrRep (StReg stgCurrentNursery) + (StInt (toInteger BDESCR_START)) + ) + ) + (StMachOp MO_Nat_Sub + [StMachOp MO_NatU_Mul + [StInd WordRep + (StIndex PtrRep (StReg stgCurrentNursery) + (StInt (toInteger BDESCR_BLOCKS))), + StInt (toInteger bLOCK_SIZE{-in bytes-}) + ], + StInt (1 * BYTES_PER_WORD) + ] + ) + + ) + + : xs + ) \end{code}