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
42 -- -----------------------------------------------------------------------------
43 -- Code generation for Foreign Calls
46 :: [HintedCmmFormal] -- where to put the results
47 -> ForeignCall -- the op
48 -> [StgArg] -- arguments
49 -> StgLiveVars -- live vars, in case we need to save them
51 cgForeignCall results fcall stg_args live
53 reps_n_amodes <- getArgAmodes stg_args
55 -- Get the *non-void* args, and jiggle them with shimForeignCall
56 arg_exprs = [ shimForeignCallArg stg_arg expr
57 | (stg_arg, (rep,expr)) <- stg_args `zip` reps_n_amodes,
60 arg_hints = zipWith CmmHinted
61 arg_exprs (map (typeForeignHint.stgArgType) stg_args)
63 emitForeignCall results fcall arg_hints live
67 :: [HintedCmmFormal] -- where to put the results
68 -> ForeignCall -- the op
69 -> [CmmHinted CmmExpr] -- arguments
70 -> StgLiveVars -- live vars, in case we need to save them
73 emitForeignCall results (CCall (CCallSpec target cconv safety)) args live
74 = do vols <- getVolatileRegs live
76 emitForeignCall' safety results
77 (CmmCallee cmm_target cconv) call_args (Just vols) srt CmmMayReturn
79 (call_args, cmm_target)
81 -- If the packageId is Nothing then the label is taken to be in the
82 -- package currently being compiled.
83 StaticTarget lbl mPkgId
86 Nothing -> ForeignLabelInThisPackage
87 Just pkgId -> ForeignLabelInPackage pkgId
90 (mkForeignLabel lbl call_size labelSource IsFunction)))
92 -- A label imported with "foreign import ccall "dynamic" ..."
93 -- Note: "dynamic" here doesn't mean "dynamic library".
94 -- Read the FFI spec for details.
95 DynamicTarget -> case args of
96 (CmmHinted fn _):rest -> (rest, fn)
97 [] -> panic "emitForeignCall: DynamicTarget []"
99 -- in the stdcall calling convention, the symbol needs @size appended
100 -- to it, where size is the total number of bytes of arguments. We
101 -- attach this info to the CLabel here, and the CLabel pretty printer
102 -- will generate the suffix when the label is printed.
104 | StdCallConv <- cconv = Just (sum (map (arg_size.cmmExprType.hintlessCmm) args))
105 | otherwise = Nothing
107 -- ToDo: this might not be correct for 64-bit API
108 arg_size rep = max (widthInBytes (typeWidth rep)) wORD_SIZE
111 -- alternative entry point, used by CmmParse
112 -- the new code generator has utility function emitCCall and emitPrimCall
113 -- which should be used instead of this (the equivalent emitForeignCall
114 -- is not presently exported.)
117 -> [HintedCmmFormal] -- where to put the results
118 -> CmmCallTarget -- the op
119 -> [CmmHinted CmmExpr] -- arguments
120 -> Maybe [GlobalReg] -- live vars, in case we need to save them
121 -> C_SRT -- the SRT of the calls continuation
124 emitForeignCall' safety results target args vols _srt ret
125 | not (playSafe safety) = do
126 temp_args <- load_args_into_temps args
127 let (caller_save, caller_load) = callerSaveVolatileRegs vols
128 let caller_load' = if ret == CmmNeverReturns then [] else caller_load
130 stmtC (CmmCall target results temp_args CmmUnsafe ret)
134 -- Both 'id' and 'new_base' are GCKindNonPtr because they're
135 -- RTS only objects and are not subject to garbage collection
137 new_base <- newTemp (cmmRegType (CmmGlobal BaseReg))
138 temp_args <- load_args_into_temps args
139 temp_target <- load_target_into_temp target
140 let (caller_save, caller_load) = callerSaveVolatileRegs vols
143 -- The CmmUnsafe arguments are only correct because this part
144 -- of the code hasn't been moved into the CPS pass yet.
145 -- Once that happens, this function will just emit a (CmmSafe srt) call,
146 -- and the CPS will be the one to convert that
147 -- to this sequence of three CmmUnsafe calls.
148 stmtC (CmmCall (CmmCallee suspendThread CCallConv)
149 [ CmmHinted id AddrHint ]
150 [ CmmHinted (CmmReg (CmmGlobal BaseReg)) AddrHint
151 , CmmHinted (CmmLit (CmmInt (fromIntegral (fromEnum (playInterruptible safety))) wordWidth)) NoHint]
153 stmtC (CmmCall temp_target results temp_args CmmUnsafe ret)
154 stmtC (CmmCall (CmmCallee resumeThread CCallConv)
155 [ CmmHinted new_base AddrHint ]
156 [ CmmHinted (CmmReg (CmmLocal id)) AddrHint ]
158 -- Assign the result to BaseReg: we
159 -- might now have a different Capability!
160 stmtC (CmmAssign (CmmGlobal BaseReg) (CmmReg (CmmLocal new_base)))
164 suspendThread, resumeThread :: CmmExpr
165 suspendThread = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "suspendThread")))
166 resumeThread = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "resumeThread")))
169 -- we might need to load arguments into temporaries before
170 -- making the call, because certain global registers might
171 -- overlap with registers that the C calling convention uses
172 -- for passing arguments.
174 -- This is a HACK; really it should be done in the back end, but
175 -- it's easier to generate the temporaries here.
176 load_args_into_temps :: [CmmHinted CmmExpr] -> FCode [CmmHinted CmmExpr]
177 load_args_into_temps = mapM arg_assign_temp
178 where arg_assign_temp (CmmHinted e hint) = do
179 tmp <- maybe_assign_temp e
180 return (CmmHinted tmp hint)
182 load_target_into_temp :: CmmCallTarget -> FCode CmmCallTarget
183 load_target_into_temp (CmmCallee expr conv) = do
184 tmp <- maybe_assign_temp expr
185 return (CmmCallee tmp conv)
186 load_target_into_temp other_target =
189 maybe_assign_temp :: CmmExpr -> FCode CmmExpr
191 | hasNoGlobalRegs e = return e
193 -- don't use assignTemp, it uses its own notion of "trivial"
194 -- expressions, which are wrong here.
195 -- this is a NonPtr because it only duplicates an existing
196 reg <- newTemp (cmmExprType e) --TODO FIXME NOW
197 stmtC (CmmAssign (CmmLocal reg) e)
198 return (CmmReg (CmmLocal reg))
200 -- -----------------------------------------------------------------------------
201 -- Save/restore the thread state in the TSO
203 -- This stuff can't be done in suspendThread/resumeThread, because it
204 -- refers to global registers which aren't available in the C world.
206 emitSaveThreadState :: Code
207 emitSaveThreadState = do
208 -- CurrentTSO->stackobj->sp = Sp;
209 stmtC $ CmmStore (cmmOffset (CmmLoad (cmmOffset stgCurrentTSO tso_stackobj) bWord)
212 -- and save the current cost centre stack in the TSO when profiling:
213 when opt_SccProfilingOn $
214 stmtC (CmmStore (cmmOffset stgCurrentTSO tso_CCCS) curCCS)
216 -- CurrentNursery->free = Hp+1;
217 emitCloseNursery :: Code
218 emitCloseNursery = stmtC $ CmmStore nursery_bdescr_free (cmmOffsetW stgHp 1)
220 emitLoadThreadState :: Code
221 emitLoadThreadState = do
222 tso <- newTemp bWord -- TODO FIXME NOW
223 stack <- newTemp bWord -- TODO FIXME NOW
226 CmmAssign (CmmLocal tso) stgCurrentTSO,
227 -- stack = tso->stackobj
228 CmmAssign (CmmLocal stack) (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_stackobj) bWord),
230 CmmAssign sp (CmmLoad (cmmOffset (CmmReg (CmmLocal stack)) stack_SP)
232 -- SpLim = stack->stack + RESERVED_STACK_WORDS;
233 CmmAssign spLim (cmmOffsetW (cmmOffset (CmmReg (CmmLocal stack)) stack_STACK)
234 rESERVED_STACK_WORDS),
236 -- HpAlloc is assumed to be set to non-zero only by a failed
237 -- a heap check, see HeapStackCheck.cmm:GC_GENERIC
238 CmmAssign hpAlloc (CmmLit zeroCLit)
241 -- and load the current cost centre stack from the TSO when profiling:
242 when opt_SccProfilingOn $
243 stmtC (CmmStore curCCSAddr
244 (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_CCCS) bWord))
246 emitOpenNursery :: Code
247 emitOpenNursery = stmtsC [
248 -- Hp = CurrentNursery->free - 1;
249 CmmAssign hp (cmmOffsetW (CmmLoad nursery_bdescr_free gcWord) (-1)),
251 -- HpLim = CurrentNursery->start +
252 -- CurrentNursery->blocks*BLOCK_SIZE_W - 1;
255 (CmmLoad nursery_bdescr_start bWord)
257 (CmmMachOp mo_wordMul [
258 CmmMachOp (MO_SS_Conv W32 wordWidth)
259 [CmmLoad nursery_bdescr_blocks b32],
260 CmmLit (mkIntCLit bLOCK_SIZE)
267 nursery_bdescr_free, nursery_bdescr_start, nursery_bdescr_blocks :: CmmExpr
268 nursery_bdescr_free = cmmOffset stgCurrentNursery oFFSET_bdescr_free
269 nursery_bdescr_start = cmmOffset stgCurrentNursery oFFSET_bdescr_start
270 nursery_bdescr_blocks = cmmOffset stgCurrentNursery oFFSET_bdescr_blocks
272 tso_stackobj, tso_CCCS, stack_STACK, stack_SP :: ByteOff
273 tso_stackobj = closureField oFFSET_StgTSO_stackobj
274 tso_CCCS = closureField oFFSET_StgTSO_CCCS
275 stack_STACK = closureField oFFSET_StgStack_stack
276 stack_SP = closureField oFFSET_StgStack_sp
278 closureField :: ByteOff -> ByteOff
279 closureField off = off + fixedHdrSize * wORD_SIZE
281 stgSp, stgHp, stgCurrentTSO, stgCurrentNursery :: CmmExpr
284 stgCurrentTSO = CmmReg currentTSO
285 stgCurrentNursery = CmmReg currentNursery
287 sp, spLim, hp, hpLim, currentTSO, currentNursery, hpAlloc :: CmmReg
289 spLim = CmmGlobal SpLim
291 hpLim = CmmGlobal HpLim
292 currentTSO = CmmGlobal CurrentTSO
293 currentNursery = CmmGlobal CurrentNursery
294 hpAlloc = CmmGlobal HpAlloc
296 -- -----------------------------------------------------------------------------
297 -- For certain types passed to foreign calls, we adjust the actual
298 -- value passed to the call. For ByteArray#/Array# we pass the
299 -- address of the actual array, not the address of the heap object.
301 shimForeignCallArg :: StgArg -> CmmExpr -> CmmExpr
302 shimForeignCallArg arg expr
303 | tycon == arrayPrimTyCon || tycon == mutableArrayPrimTyCon
304 = cmmOffsetB expr arrPtrsHdrSize
306 | tycon == byteArrayPrimTyCon || tycon == mutableByteArrayPrimTyCon
307 = cmmOffsetB expr arrWordsHdrSize
311 -- should be a tycon app, since this is a foreign call
312 tycon = tyConAppTyCon (repType (stgArgType arg))