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"
42 -- -----------------------------------------------------------------------------
43 -- Code generation for Foreign Calls
46 :: CmmHintFormals -- 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 = zip arg_exprs (map (typeHint.stgArgType) stg_args)
62 emitForeignCall results fcall arg_hints live
66 :: CmmHintFormals -- where to put the results
67 -> ForeignCall -- the op
68 -> [(CmmExpr,MachHint)] -- arguments
69 -> StgLiveVars -- live vars, in case we need to save them
72 emitForeignCall results (CCall (CCallSpec target cconv safety)) args live
73 = do vols <- getVolatileRegs live
75 emitForeignCall' safety results
76 (CmmCallee cmm_target cconv) call_args (Just vols) srt CmmMayReturn
78 (call_args, cmm_target)
80 StaticTarget lbl -> (args, CmmLit (CmmLabel
81 (mkForeignLabel lbl call_size False)))
82 DynamicTarget -> case args of (fn,_):rest -> (rest, fn)
84 -- in the stdcall calling convention, the symbol needs @size appended
85 -- to it, where size is the total number of bytes of arguments. We
86 -- attach this info to the CLabel here, and the CLabel pretty printer
87 -- will generate the suffix when the label is printed.
89 | StdCallConv <- cconv = Just (sum (map (arg_size.cmmExprRep.fst) args))
92 -- ToDo: this might not be correct for 64-bit API
93 arg_size rep = max (machRepByteWidth rep) wORD_SIZE
95 emitForeignCall _ (DNCall _) _ _
96 = panic "emitForeignCall: DNCall"
99 -- alternative entry point, used by CmmParse
102 -> CmmHintFormals -- where to put the results
103 -> CmmCallTarget -- the op
104 -> [(CmmExpr,MachHint)] -- arguments
105 -> Maybe [GlobalReg] -- live vars, in case we need to save them
106 -> C_SRT -- the SRT of the calls continuation
109 emitForeignCall' safety results target args vols srt ret
110 | not (playSafe safety) = do
111 temp_args <- load_args_into_temps args
112 let (caller_save, caller_load) = callerSaveVolatileRegs vols
114 stmtC (CmmCall target results temp_args CmmUnsafe ret)
118 -- Both 'id' and 'new_base' are KindNonPtr because they're
119 -- RTS only objects and are not subject to garbage collection
120 id <- newNonPtrTemp wordRep
121 new_base <- newNonPtrTemp (cmmRegRep (CmmGlobal BaseReg))
122 temp_args <- load_args_into_temps args
123 temp_target <- load_target_into_temp target
124 let (caller_save, caller_load) = callerSaveVolatileRegs vols
127 -- The CmmUnsafe arguments are only correct because this part
128 -- of the code hasn't been moved into the CPS pass yet.
129 -- Once that happens, this function will just emit a (CmmSafe srt) call,
130 -- and the CPS will will be the one to convert that
131 -- to this sequence of three CmmUnsafe calls.
132 stmtC (CmmCall (CmmCallee suspendThread CCallConv)
134 [ (CmmReg (CmmGlobal BaseReg), PtrHint) ]
136 stmtC (CmmCall temp_target results temp_args CmmUnsafe ret)
137 stmtC (CmmCall (CmmCallee resumeThread CCallConv)
138 [ (new_base, PtrHint) ]
139 [ (CmmReg (CmmLocal id), PtrHint) ]
141 -- Assign the result to BaseReg: we
142 -- might now have a different Capability!
143 stmtC (CmmAssign (CmmGlobal BaseReg) (CmmReg (CmmLocal new_base)))
147 suspendThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("suspendThread")))
148 resumeThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("resumeThread")))
151 -- we might need to load arguments into temporaries before
152 -- making the call, because certain global registers might
153 -- overlap with registers that the C calling convention uses
154 -- for passing arguments.
156 -- This is a HACK; really it should be done in the back end, but
157 -- it's easier to generate the temporaries here.
158 load_args_into_temps = mapM arg_assign_temp
159 where arg_assign_temp (e,hint) = do
160 tmp <- maybe_assign_temp e
163 load_target_into_temp (CmmCallee expr conv) = do
164 tmp <- maybe_assign_temp expr
165 return (CmmCallee tmp conv)
166 load_target_into_temp other_target =
170 | hasNoGlobalRegs e = return e
172 -- don't use assignTemp, it uses its own notion of "trivial"
173 -- expressions, which are wrong here.
174 -- this is a NonPtr because it only duplicates an existing
175 reg <- newNonPtrTemp (cmmExprRep e) --TODO FIXME NOW
176 stmtC (CmmAssign (CmmLocal reg) e)
177 return (CmmReg (CmmLocal reg))
179 -- -----------------------------------------------------------------------------
180 -- Save/restore the thread state in the TSO
182 -- This stuff can't be done in suspendThread/resumeThread, because it
183 -- refers to global registers which aren't available in the C world.
185 emitSaveThreadState = do
186 -- CurrentTSO->sp = Sp;
187 stmtC $ CmmStore (cmmOffset stgCurrentTSO tso_SP) stgSp
189 -- and save the current cost centre stack in the TSO when profiling:
190 when opt_SccProfilingOn $
191 stmtC (CmmStore (cmmOffset stgCurrentTSO tso_CCCS) curCCS)
193 -- CurrentNursery->free = Hp+1;
194 emitCloseNursery = stmtC $ CmmStore nursery_bdescr_free (cmmOffsetW stgHp 1)
196 emitLoadThreadState = do
197 tso <- newNonPtrTemp wordRep -- TODO FIXME NOW
200 CmmAssign (CmmLocal tso) stgCurrentTSO,
202 CmmAssign sp (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_SP)
204 -- SpLim = tso->stack + RESERVED_STACK_WORDS;
205 CmmAssign spLim (cmmOffsetW (cmmOffset (CmmReg (CmmLocal tso)) tso_STACK)
206 rESERVED_STACK_WORDS)
209 -- and load the current cost centre stack from the TSO when profiling:
210 when opt_SccProfilingOn $
211 stmtC (CmmStore curCCSAddr
212 (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_CCCS) wordRep))
214 emitOpenNursery = stmtsC [
215 -- Hp = CurrentNursery->free - 1;
216 CmmAssign hp (cmmOffsetW (CmmLoad nursery_bdescr_free wordRep) (-1)),
218 -- HpLim = CurrentNursery->start +
219 -- CurrentNursery->blocks*BLOCK_SIZE_W - 1;
222 (CmmLoad nursery_bdescr_start wordRep)
224 (CmmMachOp mo_wordMul [
225 CmmMachOp (MO_S_Conv I32 wordRep)
226 [CmmLoad nursery_bdescr_blocks I32],
227 CmmLit (mkIntCLit bLOCK_SIZE)
235 nursery_bdescr_free = cmmOffset stgCurrentNursery oFFSET_bdescr_free
236 nursery_bdescr_start = cmmOffset stgCurrentNursery oFFSET_bdescr_start
237 nursery_bdescr_blocks = cmmOffset stgCurrentNursery oFFSET_bdescr_blocks
239 tso_SP = tsoFieldB oFFSET_StgTSO_sp
240 tso_STACK = tsoFieldB oFFSET_StgTSO_stack
241 tso_CCCS = tsoProfFieldB oFFSET_StgTSO_CCCS
243 -- The TSO struct has a variable header, and an optional StgTSOProfInfo in
244 -- the middle. The fields we're interested in are after the StgTSOProfInfo.
245 tsoFieldB :: ByteOff -> ByteOff
247 | opt_SccProfilingOn = off + sIZEOF_StgTSOProfInfo + fixedHdrSize * wORD_SIZE
248 | otherwise = off + fixedHdrSize * wORD_SIZE
250 tsoProfFieldB :: ByteOff -> ByteOff
251 tsoProfFieldB off = off + fixedHdrSize * wORD_SIZE
255 stgCurrentTSO = CmmReg currentTSO
256 stgCurrentNursery = CmmReg currentNursery
259 spLim = CmmGlobal SpLim
261 hpLim = CmmGlobal HpLim
262 currentTSO = CmmGlobal CurrentTSO
263 currentNursery = CmmGlobal CurrentNursery
265 -- -----------------------------------------------------------------------------
266 -- For certain types passed to foreign calls, we adjust the actual
267 -- value passed to the call. For ByteArray#/Array# we pass the
268 -- address of the actual array, not the address of the heap object.
270 shimForeignCallArg :: StgArg -> CmmExpr -> CmmExpr
271 shimForeignCallArg arg expr
272 | tycon == arrayPrimTyCon || tycon == mutableArrayPrimTyCon
273 = cmmOffsetB expr arrPtrsHdrSize
275 | tycon == byteArrayPrimTyCon || tycon == mutableByteArrayPrimTyCon
276 = cmmOffsetB expr arrWordsHdrSize
280 -- should be a tycon app, since this is a foreign call
281 tycon = tyConAppTyCon (repType (stgArgType arg))