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,
28 import ForeignCall ( ForeignCall(..), CCallSpec(..), CCallTarget(..),
29 CCallConv(..), playSafe, playThreadSafe )
31 import Util ( notNull )
37 The main honchos here are primCode and foreignCallCode, which handle the guts of COpStmts.
41 :: [CAddrMode] -- results
43 -> [CAddrMode] -- args
44 -> UniqSM StixStmtList
47 %************************************************************************
49 \subsubsection{Code for foreign calls}
51 %************************************************************************
53 First, the dreaded @ccall@. We can't handle @casm@s.
55 Usually, this compiles to an assignment, but when the left-hand side
56 is empty, we just perform the call and ignore the result.
58 btw Why not let programmer use casm to provide assembly code instead
61 ToDo: saving/restoring of volatile regs around ccalls.
63 JRS, 001113: always do the call of suspendThread and resumeThread as a ccall
64 rather than inheriting the calling convention of the thing which we're really
68 foreignCallCode lhs call@(CCall (CCallSpec ctarget cconv safety)) rhs
70 | not (playSafe safety)
71 = returnUs (\xs -> ccall : xs)
74 = save_thread_state `thenUs` \ save ->
75 load_thread_state `thenUs` \ load ->
76 getUniqueUs `thenUs` \ uniq ->
78 id = StixTemp (StixVReg uniq IntRep)
81 | playThreadSafe safety = 1
84 suspend = StAssignReg IntRep id
85 (StCall (Left FSLIT("suspendThread")) {-no:cconv-} CCallConv
86 IntRep [StReg stgBaseReg, StInt is_threadSafe ])
88 (StCall (Left FSLIT("resumeThread")) {-no:cconv-} CCallConv
89 VoidRep [StReg id, StInt is_threadSafe ])
91 returnUs (\xs -> save (suspend : ccall : resume : load xs))
96 StaticTarget nm -> (rhs, Left nm)
97 DynamicTarget | notNull rhs -- an assertion
98 -> (tail rhs, Right (amodeToStix (head rhs)))
100 -> ncgPrimopMoan "Native code generator can't handle foreign call"
103 stix_args = map amodeToStix' cargs
106 [] -> StVoidable (StCall stix_target cconv VoidRep stix_args)
107 [lhs] -> mkStAssign pk lhs' (StCall stix_target cconv pk stix_args)
109 lhs' = amodeToStix lhs
110 pk = case getAmodeRep lhs of
112 DoubleRep -> DoubleRep
114 Word64Rep -> Word64Rep
118 %************************************************************************
120 \subsubsection{Code for @CAddrMode@s}
122 %************************************************************************
124 When a character is fetched from a mixed type location, we have to do
125 an extra cast. This is reflected in amodeCode', which is for rhs
126 amodes that might possibly need the extra cast.
129 amodeToStix, amodeToStix' :: CAddrMode -> StixExpr
131 amodeToStix'{-'-} am@(CVal rr CharRep)
132 | mixedTypeLocn am = StMachOp MO_NatS_to_32U [amodeToStix am]
133 | otherwise = amodeToStix am
138 amodeToStix am@(CVal rr CharRep)
140 = StInd IntRep (amodeToStix (CAddr rr))
142 amodeToStix (CVal rr pk) = StInd pk (amodeToStix (CAddr rr))
144 amodeToStix CBytesPerWord
145 = StInt (toInteger wORD_SIZE)
147 amodeToStix (CAddr (SpRel off))
148 = StIndex PtrRep (StReg stgSp) (StInt (toInteger (iBox off)))
150 amodeToStix (CAddr (HpRel off))
151 = StIndex IntRep (StReg stgHp) (StInt (toInteger (- (iBox off))))
153 amodeToStix (CAddr (NodeRel off))
154 = StIndex IntRep (StReg stgNode) (StInt (toInteger (iBox off)))
156 amodeToStix (CAddr (CIndex base off pk))
157 = StIndex pk (amodeToStix base) (amodeToStix off)
159 amodeToStix (CReg magic) = StReg (StixMagicId magic)
160 amodeToStix (CTemp uniq pk) = StReg (StixTemp (StixVReg uniq pk))
162 amodeToStix (CLbl lbl _) = StCLbl lbl
164 -- For CharLike and IntLike, we attempt some trivial constant-folding here.
166 amodeToStix (CCharLike (CLit (MachChar c)))
167 = StIndex Word8Rep cHARLIKE_closure (StInt (toInteger off))
169 off = charLikeSize * (c - mIN_CHARLIKE)
171 amodeToStix (CCharLike x)
172 = panic "amodeToStix.CCharLike"
174 amodeToStix (CIntLike (CLit (MachInt i)))
175 = StIndex Word8Rep iNTLIKE_closure (StInt (toInteger off))
177 off = intLikeSize * (fromInteger (i - mIN_INTLIKE))
179 amodeToStix (CIntLike x)
180 = panic "amodeToStix.CIntLike"
182 amodeToStix (CLit core)
184 MachChar c -> StInt (toInteger c)
185 MachStr s -> StString s
186 MachAddr a -> StInt a
188 MachWord w -> case word2IntLit core of MachInt iw -> StInt iw
189 MachLitLit s _ -> litLitErr
190 MachLabel l -> StCLbl (mkForeignLabel l False{-ToDo: dynamic-})
191 MachFloat d -> StFloat d
192 MachDouble d -> StDouble d
193 _ -> panic "amodeToStix:core literal"
195 amodeToStix (CMacroExpr _ macro [arg])
197 arg_amode = amodeToStix arg
200 ENTRY_CODE -> arg_amode
201 ARG_TAG -> arg_amode -- just an integer no. of words
203 #ifdef WORDS_BIGENDIAN
205 [StInd WordRep (StIndex PtrRep arg_amode
206 (StInt (toInteger (-1)))),
210 [StInd WordRep (StIndex PtrRep arg_amode
211 (StInt (toInteger (-1)))),
215 -> StInd PtrRep (StIndex PtrRep arg_amode
216 (StInt (toInteger uF_UPDATEE)))
218 BYTE_ARR_CTS -> StIndex IntRep arg_amode arrWordsHS
219 PTRS_ARR_CTS -> StIndex PtrRep arg_amode arrPtrsHS
220 ForeignObj_CLOSURE_DATA -> StInd PtrRep (StIndex PtrRep arg_amode fixedHS)
224 = pprPanic "StixPrim.amodeToStix" (pprAmode other)
227 = ncgPrimopMoan "native code generator can't handle lit-lits" empty
230 Sizes of the CharLike and IntLike closures that are arranged as arrays
231 in the data segment. (These are in bytes.)
234 -- The INTLIKE base pointer
236 iNTLIKE_closure :: StixExpr
237 iNTLIKE_closure = StCLbl mkIntlikeClosureLabel
241 cHARLIKE_closure :: StixExpr
242 cHARLIKE_closure = StCLbl mkCharlikeClosureLabel
244 mutArrPtrsFrozen_info = StCLbl mkMAP_FROZEN_infoLabel
246 -- these are the sizes of charLike and intLike closures, in _bytes_.
247 charLikeSize = (fixedHdrSize + 1) * (getPrimRepSizeInBytes PtrRep)
248 intLikeSize = (fixedHdrSize + 1) * (getPrimRepSizeInBytes PtrRep)
254 = getUniqueUs `thenUs` \ tso_uq ->
255 let tso = StixTemp (StixVReg tso_uq PtrRep) in
257 StAssignReg PtrRep tso (StReg stgCurrentTSO)
260 [StReg tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))])
264 [StReg tso, StInt (toInteger (TSO_SU*BYTES_PER_WORD))])
268 [StReg stgCurrentNursery,
269 StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))])
271 [StReg stgHp, StInt (toInteger (1 * BYTES_PER_WORD))])
276 = getUniqueUs `thenUs` \ tso_uq ->
277 let tso = StixTemp (StixVReg tso_uq PtrRep) in
279 StAssignReg PtrRep tso (StReg stgCurrentTSO)
284 [StReg tso, StInt (toInteger (TSO_SP*BYTES_PER_WORD))]))
289 [StReg tso, StInt (toInteger (TSO_SU*BYTES_PER_WORD))]))
294 StInt (toInteger ((TSO_STACK + rESERVED_STACK_WORDS)
301 [StReg stgCurrentNursery,
302 StInt (toInteger (BDESCR_FREE * BYTES_PER_WORD))]),
303 StInt (toInteger (1 * BYTES_PER_WORD))
309 (StIndex PtrRep (StReg stgCurrentNursery)
310 (StInt (toInteger BDESCR_START))
314 [StMachOp MO_NatU_Mul
316 (StIndex PtrRep (StReg stgCurrentNursery)
317 (StInt (toInteger BDESCR_BLOCKS))),
318 StInt (toInteger bLOCK_SIZE{-in bytes-})
320 StInt (1 * BYTES_PER_WORD)