2 % (c) The AQUA Project, Glasgow University, 1993-1998
6 module StixPrim ( amodeToStix, amodeToStix', foreignCallCode )
9 #include "HsVersions.h"
14 import PprAbsC ( pprAmode )
15 import AbsCSyn hiding ( spRel )
16 import AbsCUtils ( getAmodeRep, mixedTypeLocn )
17 import SMRep ( fixedHdrSize )
18 import Literal ( Literal(..), word2IntLit )
19 import MachOp ( MachOp(..) )
20 import PrimRep ( PrimRep(..), getPrimRepSizeInBytes )
21 import UniqSupply ( returnUs, thenUs, getUniqueUs, UniqSM )
22 import Constants ( wORD_SIZE,
23 mIN_INTLIKE, mIN_CHARLIKE, uF_UPDATEE, bLOCK_SIZE,
24 rESERVED_STACK_WORDS )
25 import CLabel ( mkIntlikeClosureLabel, mkCharlikeClosureLabel,
26 mkMAP_FROZEN_infoLabel, mkEMPTY_MVAR_infoLabel,
28 import ForeignCall ( ForeignCall(..), CCallSpec(..), CCallTarget(..),
29 CCallConv(..), playSafe )
36 The main honchos here are primCode and foreignCallCode, which handle the guts of COpStmts.
40 :: [CAddrMode] -- results
42 -> [CAddrMode] -- args
43 -> UniqSM StixStmtList
46 %************************************************************************
48 \subsubsection{Code for foreign calls}
50 %************************************************************************
52 First, the dreaded @ccall@. We can't handle @casm@s.
54 Usually, this compiles to an assignment, but when the left-hand side
55 is empty, we just perform the call and ignore the result.
57 btw Why not let programmer use casm to provide assembly code instead
60 ToDo: saving/restoring of volatile regs around ccalls.
62 JRS, 001113: always do the call of suspendThread and resumeThread as a ccall
63 rather than inheriting the calling convention of the thing which we're really
67 foreignCallCode lhs (CCall (CCallSpec (StaticTarget fn) cconv safety)) rhs
69 | not (playSafe safety)
70 = returnUs (\xs -> ccall : xs)
73 = save_thread_state `thenUs` \ save ->
74 load_thread_state `thenUs` \ load ->
75 getUniqueUs `thenUs` \ uniq ->
77 id = StixTemp (StixVReg uniq IntRep)
79 suspend = StAssignReg IntRep id
80 (StCall SLIT("suspendThread") {-no:cconv-} CCallConv
81 IntRep [StReg stgBaseReg])
83 (StCall SLIT("resumeThread") {-no:cconv-} CCallConv
86 returnUs (\xs -> save (suspend : ccall : resume : load xs))
89 args = map amodeCodeForCCall rhs
91 let base = amodeToStix' x
94 ArrayRep -> StIndex PtrRep base arrPtrsHS
95 ByteArrayRep -> StIndex IntRep base arrWordsHS
96 ForeignObjRep -> StInd PtrRep (StIndex PtrRep base fixedHS)
100 [] -> StVoidable (StCall fn cconv VoidRep args)
101 [lhs] -> mkStAssign pk lhs' (StCall fn cconv pk args)
103 lhs' = amodeToStix lhs
104 pk = case getAmodeRep lhs of
106 DoubleRep -> DoubleRep
109 foreignCallCode lhs call rhs
110 = ncgPrimopMoan "Native code generator can't handle foreign call" (ppr call)
113 %************************************************************************
115 \subsubsection{Code for @CAddrMode@s}
117 %************************************************************************
119 When a character is fetched from a mixed type location, we have to do
120 an extra cast. This is reflected in amodeCode', which is for rhs
121 amodes that might possibly need the extra cast.
124 amodeToStix, amodeToStix' :: CAddrMode -> StixExpr
126 amodeToStix'{-'-} am@(CVal rr CharRep)
127 | mixedTypeLocn am = StMachOp MO_NatS_to_32U [amodeToStix am]
128 | otherwise = amodeToStix am
133 amodeToStix am@(CVal rr CharRep)
135 = StInd IntRep (amodeToStix (CAddr rr))
137 amodeToStix (CVal rr pk) = StInd pk (amodeToStix (CAddr rr))
139 amodeToStix CBytesPerWord
140 = StInt (toInteger wORD_SIZE)
142 amodeToStix (CMem pk addr) = StInd pk (amodeToStix addr)
144 amodeToStix (CAddr (SpRel off))
145 = StIndex PtrRep (StReg stgSp) (StInt (toInteger (iBox off)))
147 amodeToStix (CAddr (HpRel off))
148 = StIndex IntRep (StReg stgHp) (StInt (toInteger (- (iBox off))))
150 amodeToStix (CAddr (NodeRel off))
151 = StIndex IntRep (StReg stgNode) (StInt (toInteger (iBox off)))
153 amodeToStix (CAddr (CIndex base off pk))
154 = StIndex pk (amodeToStix base) (amodeToStix off)
156 amodeToStix (CReg magic) = StReg (StixMagicId magic)
157 amodeToStix (CTemp uniq pk) = StReg (StixTemp (StixVReg uniq pk))
159 amodeToStix (CLbl lbl _) = StCLbl lbl
161 -- For CharLike and IntLike, we attempt some trivial constant-folding here.
163 amodeToStix (CCharLike (CLit (MachChar c)))
164 = StIndex Word8Rep cHARLIKE_closure (StInt (toInteger off))
166 off = charLikeSize * (c - mIN_CHARLIKE)
168 amodeToStix (CCharLike x)
169 = panic "amodeToStix.CCharLike"
171 amodeToStix (CIntLike (CLit (MachInt i)))
172 = StIndex Word8Rep iNTLIKE_closure (StInt (toInteger off))
174 off = intLikeSize * (fromInteger (i - mIN_INTLIKE))
176 amodeToStix (CIntLike x)
177 = panic "amodeToStix.CIntLike"
179 amodeToStix (CLit core)
181 MachChar c -> StInt (toInteger c)
182 MachStr s -> StString s
183 MachAddr a -> StInt a
185 MachWord w -> case word2IntLit core of MachInt iw -> StInt iw
186 MachLitLit s _ -> litLitErr
187 MachLabel l -> StCLbl (mkForeignLabel l False{-ToDo: dynamic-})
188 MachFloat d -> StFloat d
189 MachDouble d -> StDouble d
190 _ -> panic "amodeToStix:core literal"
192 amodeToStix (CMacroExpr _ macro [arg])
194 ENTRY_CODE -> amodeToStix arg
195 ARG_TAG -> amodeToStix arg -- just an integer no. of words
197 #ifdef WORDS_BIGENDIAN
199 [StInd WordRep (StIndex PtrRep (amodeToStix arg)
200 (StInt (toInteger (-1)))),
204 [StInd WordRep (StIndex PtrRep (amodeToStix arg)
205 (StInt (toInteger (-1)))),
209 -> StInd PtrRep (StIndex PtrRep (amodeToStix arg)
210 (StInt (toInteger uF_UPDATEE)))
213 = pprPanic "StixPrim.amodeToStix" (pprAmode other)
216 = ncgPrimopMoan "native code generator can't handle lit-lits" empty
219 Sizes of the CharLike and IntLike closures that are arranged as arrays
220 in the data segment. (These are in bytes.)
223 -- The INTLIKE base pointer
225 iNTLIKE_closure :: StixExpr
226 iNTLIKE_closure = StCLbl mkIntlikeClosureLabel
230 cHARLIKE_closure :: StixExpr
231 cHARLIKE_closure = StCLbl mkCharlikeClosureLabel
233 mutArrPtrsFrozen_info = StCLbl mkMAP_FROZEN_infoLabel
235 -- these are the sizes of charLike and intLike closures, in _bytes_.
236 charLikeSize = (fixedHdrSize + 1) * (sizeOf PtrRep)
237 intLikeSize = (fixedHdrSize + 1) * (sizeOf PtrRep)
243 = getUniqueUs `thenUs` \ tso_uq ->
244 let tso = StixTemp (StixVReg tso_uq ThreadIdRep) in
246 StAssignReg ThreadIdRep tso (StReg stgCurrentTSO)
249 [StReg tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))])
253 [StReg tso, StInt (toInteger (TSO_SU*BYTES_PER_WORD))])
257 [StReg stgCurrentNursery,
258 StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))])
260 [StReg stgHp, StInt (toInteger (1 * BYTES_PER_WORD))])
265 = getUniqueUs `thenUs` \ tso_uq ->
266 let tso = StixTemp (StixVReg tso_uq ThreadIdRep) in
268 StAssignReg ThreadIdRep tso (StReg stgCurrentTSO)
273 [StReg tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))]))
278 [StReg tso, StInt (toInteger (TSO_SU*BYTES_PER_WORD))]))
283 StInt (toInteger ((TSO_STACK + rESERVED_STACK_WORDS)
290 [StReg stgCurrentNursery,
291 StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))]),
292 StInt (toInteger (1 * BYTES_PER_WORD))
299 [StReg stgCurrentNursery,
300 StInt (toInteger (BDESCR_START * BYTES_PER_WORD))]),
301 StInt (toInteger (bLOCK_SIZE - (1 * BYTES_PER_WORD)))