1 -----------------------------------------------------------------------------
3 -- Code generation for foreign calls.
5 -- (c) The University of Glasgow 2004-2006
7 -----------------------------------------------------------------------------
14 emitSaveThreadState, -- will be needed by the Cmm parser
15 emitLoadThreadState, -- ditto
20 #include "HsVersions.h"
39 import MachRegs (callerSaveVolatileRegs)
40 -- HACK: this is part of the NCG so we shouldn't use this, but we need
41 -- it for now to eliminate the need for saved regs to be in CmmCall.
42 -- The long term solution is to factor callerSaveVolatileRegs
43 -- from nativeGen into codeGen
47 -- -----------------------------------------------------------------------------
48 -- Code generation for Foreign Calls
51 :: [(CmmReg,MachHint)] -- where to put the results
52 -> ForeignCall -- the op
53 -> [StgArg] -- arguments
54 -> StgLiveVars -- live vars, in case we need to save them
56 cgForeignCall results fcall stg_args live
58 reps_n_amodes <- getArgAmodes stg_args
60 -- Get the *non-void* args, and jiggle them with shimForeignCall
61 arg_exprs = [ shimForeignCallArg stg_arg expr
62 | (stg_arg, (rep,expr)) <- stg_args `zip` reps_n_amodes,
65 arg_hints = zip arg_exprs (map (typeHint.stgArgType) stg_args)
67 emitForeignCall results fcall arg_hints live
71 :: [(CmmReg,MachHint)] -- where to put the results
72 -> ForeignCall -- the op
73 -> [(CmmExpr,MachHint)] -- arguments
74 -> StgLiveVars -- live vars, in case we need to save them
77 emitForeignCall results (CCall (CCallSpec target cconv safety)) args live
78 = do vols <- getVolatileRegs live
79 emitForeignCall' safety results
80 (CmmForeignCall cmm_target cconv) call_args (Just vols)
82 (call_args, cmm_target)
84 StaticTarget lbl -> (args, CmmLit (CmmLabel
85 (mkForeignLabel lbl call_size False)))
86 DynamicTarget -> case args of (fn,_):rest -> (rest, fn)
88 -- in the stdcall calling convention, the symbol needs @size appended
89 -- to it, where size is the total number of bytes of arguments. We
90 -- attach this info to the CLabel here, and the CLabel pretty printer
91 -- will generate the suffix when the label is printed.
93 | StdCallConv <- cconv = Just (sum (map (arg_size.cmmExprRep.fst) args))
96 -- ToDo: this might not be correct for 64-bit API
97 arg_size rep = max (machRepByteWidth rep) wORD_SIZE
99 emitForeignCall results (DNCall _) args live
100 = panic "emitForeignCall: DNCall"
103 -- alternative entry point, used by CmmParse
106 -> [(CmmReg,MachHint)] -- where to put the results
107 -> CmmCallTarget -- the op
108 -> [(CmmExpr,MachHint)] -- arguments
109 -> Maybe [GlobalReg] -- live vars, in case we need to save them
111 emitForeignCall' safety results target args vols
112 | not (playSafe safety) = do
113 temp_args <- load_args_into_temps args
114 let (caller_save, caller_load) = callerSaveVolatileRegs vols
116 stmtC (CmmCall target results temp_args)
120 id <- newTemp wordRep
121 temp_args <- load_args_into_temps args
122 temp_target <- load_target_into_temp target
123 let (caller_save, caller_load) = callerSaveVolatileRegs vols
126 stmtC (CmmCall (CmmForeignCall suspendThread CCallConv)
128 [ (CmmReg (CmmGlobal BaseReg), PtrHint) ]
130 stmtC (CmmCall temp_target results temp_args)
131 stmtC (CmmCall (CmmForeignCall resumeThread CCallConv)
132 [ (CmmGlobal BaseReg, PtrHint) ]
133 -- Assign the result to BaseReg: we
134 -- might now have a different
136 [ (CmmReg id, PtrHint) ]
141 suspendThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("suspendThread")))
142 resumeThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("resumeThread")))
145 -- we might need to load arguments into temporaries before
146 -- making the call, because certain global registers might
147 -- overlap with registers that the C calling convention uses
148 -- for passing arguments.
150 -- This is a HACK; really it should be done in the back end, but
151 -- it's easier to generate the temporaries here.
152 load_args_into_temps = mapM arg_assign_temp
153 where arg_assign_temp (e,hint) = do
154 tmp <- maybe_assign_temp e
157 load_target_into_temp (CmmForeignCall expr conv) = do
158 tmp <- maybe_assign_temp expr
159 return (CmmForeignCall tmp conv)
160 load_target_info_temp other_target =
164 | hasNoGlobalRegs e = return e
166 -- don't use assignTemp, it uses its own notion of "trivial"
167 -- expressions, which are wrong here
168 reg <- newTemp (cmmExprRep e)
169 stmtC (CmmAssign reg e)
172 -- -----------------------------------------------------------------------------
173 -- Save/restore the thread state in the TSO
175 -- This stuff can't be done in suspendThread/resumeThread, because it
176 -- refers to global registers which aren't available in the C world.
178 emitSaveThreadState = do
179 -- CurrentTSO->sp = Sp;
180 stmtC $ CmmStore (cmmOffset stgCurrentTSO tso_SP) stgSp
182 -- and save the current cost centre stack in the TSO when profiling:
183 when opt_SccProfilingOn $
184 stmtC (CmmStore (cmmOffset stgCurrentTSO tso_CCCS) curCCS)
186 -- CurrentNursery->free = Hp+1;
187 emitCloseNursery = stmtC $ CmmStore nursery_bdescr_free (cmmOffsetW stgHp 1)
189 emitLoadThreadState = do
190 tso <- newTemp wordRep
193 CmmAssign tso stgCurrentTSO,
195 CmmAssign sp (CmmLoad (cmmOffset (CmmReg tso) tso_SP)
197 -- SpLim = tso->stack + RESERVED_STACK_WORDS;
198 CmmAssign spLim (cmmOffsetW (cmmOffset (CmmReg tso) tso_STACK)
199 rESERVED_STACK_WORDS)
202 -- and load the current cost centre stack from the TSO when profiling:
203 when opt_SccProfilingOn $
204 stmtC (CmmStore curCCSAddr
205 (CmmLoad (cmmOffset (CmmReg tso) tso_CCCS) wordRep))
207 emitOpenNursery = stmtsC [
208 -- Hp = CurrentNursery->free - 1;
209 CmmAssign hp (cmmOffsetW (CmmLoad nursery_bdescr_free wordRep) (-1)),
211 -- HpLim = CurrentNursery->start +
212 -- CurrentNursery->blocks*BLOCK_SIZE_W - 1;
215 (CmmLoad nursery_bdescr_start wordRep)
217 (CmmMachOp mo_wordMul [
218 CmmMachOp (MO_S_Conv I32 wordRep)
219 [CmmLoad nursery_bdescr_blocks I32],
220 CmmLit (mkIntCLit bLOCK_SIZE)
228 nursery_bdescr_free = cmmOffset stgCurrentNursery oFFSET_bdescr_free
229 nursery_bdescr_start = cmmOffset stgCurrentNursery oFFSET_bdescr_start
230 nursery_bdescr_blocks = cmmOffset stgCurrentNursery oFFSET_bdescr_blocks
232 tso_SP = tsoFieldB oFFSET_StgTSO_sp
233 tso_STACK = tsoFieldB oFFSET_StgTSO_stack
234 tso_CCCS = tsoProfFieldB oFFSET_StgTSO_CCCS
236 -- The TSO struct has a variable header, and an optional StgTSOProfInfo in
237 -- the middle. The fields we're interested in are after the StgTSOProfInfo.
238 tsoFieldB :: ByteOff -> ByteOff
240 | opt_SccProfilingOn = off + sIZEOF_StgTSOProfInfo + fixedHdrSize * wORD_SIZE
241 | otherwise = off + fixedHdrSize * wORD_SIZE
243 tsoProfFieldB :: ByteOff -> ByteOff
244 tsoProfFieldB off = off + fixedHdrSize * wORD_SIZE
248 stgCurrentTSO = CmmReg currentTSO
249 stgCurrentNursery = CmmReg currentNursery
252 spLim = CmmGlobal SpLim
254 hpLim = CmmGlobal HpLim
255 currentTSO = CmmGlobal CurrentTSO
256 currentNursery = CmmGlobal CurrentNursery
258 -- -----------------------------------------------------------------------------
259 -- For certain types passed to foreign calls, we adjust the actual
260 -- value passed to the call. For ByteArray#/Array# we pass the
261 -- address of the actual array, not the address of the heap object.
263 shimForeignCallArg :: StgArg -> CmmExpr -> CmmExpr
264 shimForeignCallArg arg expr
265 | tycon == arrayPrimTyCon || tycon == mutableArrayPrimTyCon
266 = cmmOffsetB expr arrPtrsHdrSize
268 | tycon == byteArrayPrimTyCon || tycon == mutableByteArrayPrimTyCon
269 = cmmOffsetB expr arrWordsHdrSize
273 -- should be a tycon app, since this is a foreign call
274 tycon = tyConAppTyCon (repType (stgArgType arg))