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 :: CmmHintFormals -- 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 :: CmmHintFormals -- 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 -> CmmHintFormals -- 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 -- Both 'id' and 'new_base' are KindNonPtr because they're
121 -- RTS only objects and are not subject to garbage collection
122 id <- newNonPtrTemp wordRep
123 new_base <- newNonPtrTemp (cmmRegRep (CmmGlobal BaseReg))
124 temp_args <- load_args_into_temps args
125 temp_target <- load_target_into_temp target
126 let (caller_save, caller_load) = callerSaveVolatileRegs vols
129 stmtC (CmmCall (CmmForeignCall suspendThread CCallConv)
131 [ (CmmReg (CmmGlobal BaseReg), PtrHint) ]
133 stmtC (CmmCall temp_target results temp_args)
134 stmtC (CmmCall (CmmForeignCall resumeThread CCallConv)
135 [ (new_base, PtrHint) ]
136 [ (CmmReg (CmmLocal id), PtrHint) ]
138 -- Assign the result to BaseReg: we
139 -- might now have a different Capability!
140 stmtC (CmmAssign (CmmGlobal BaseReg) (CmmReg (CmmLocal new_base)))
144 suspendThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("suspendThread")))
145 resumeThread = CmmLit (CmmLabel (mkRtsCodeLabel SLIT("resumeThread")))
148 -- we might need to load arguments into temporaries before
149 -- making the call, because certain global registers might
150 -- overlap with registers that the C calling convention uses
151 -- for passing arguments.
153 -- This is a HACK; really it should be done in the back end, but
154 -- it's easier to generate the temporaries here.
155 load_args_into_temps = mapM arg_assign_temp
156 where arg_assign_temp (e,hint) = do
157 tmp <- maybe_assign_temp e
160 load_target_into_temp (CmmForeignCall expr conv) = do
161 tmp <- maybe_assign_temp expr
162 return (CmmForeignCall tmp conv)
163 load_target_into_temp other_target =
167 | hasNoGlobalRegs e = return e
169 -- don't use assignTemp, it uses its own notion of "trivial"
170 -- expressions, which are wrong here.
171 -- this is a NonPtr because it only duplicates an existing
172 reg <- newNonPtrTemp (cmmExprRep e) --TODO FIXME NOW
173 stmtC (CmmAssign (CmmLocal reg) e)
174 return (CmmReg (CmmLocal reg))
176 -- -----------------------------------------------------------------------------
177 -- Save/restore the thread state in the TSO
179 -- This stuff can't be done in suspendThread/resumeThread, because it
180 -- refers to global registers which aren't available in the C world.
182 emitSaveThreadState = do
183 -- CurrentTSO->sp = Sp;
184 stmtC $ CmmStore (cmmOffset stgCurrentTSO tso_SP) stgSp
186 -- and save the current cost centre stack in the TSO when profiling:
187 when opt_SccProfilingOn $
188 stmtC (CmmStore (cmmOffset stgCurrentTSO tso_CCCS) curCCS)
190 -- CurrentNursery->free = Hp+1;
191 emitCloseNursery = stmtC $ CmmStore nursery_bdescr_free (cmmOffsetW stgHp 1)
193 emitLoadThreadState = do
194 tso <- newNonPtrTemp wordRep -- TODO FIXME NOW
197 CmmAssign (CmmLocal tso) stgCurrentTSO,
199 CmmAssign sp (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_SP)
201 -- SpLim = tso->stack + RESERVED_STACK_WORDS;
202 CmmAssign spLim (cmmOffsetW (cmmOffset (CmmReg (CmmLocal tso)) tso_STACK)
203 rESERVED_STACK_WORDS)
206 -- and load the current cost centre stack from the TSO when profiling:
207 when opt_SccProfilingOn $
208 stmtC (CmmStore curCCSAddr
209 (CmmLoad (cmmOffset (CmmReg (CmmLocal tso)) tso_CCCS) wordRep))
211 emitOpenNursery = stmtsC [
212 -- Hp = CurrentNursery->free - 1;
213 CmmAssign hp (cmmOffsetW (CmmLoad nursery_bdescr_free wordRep) (-1)),
215 -- HpLim = CurrentNursery->start +
216 -- CurrentNursery->blocks*BLOCK_SIZE_W - 1;
219 (CmmLoad nursery_bdescr_start wordRep)
221 (CmmMachOp mo_wordMul [
222 CmmMachOp (MO_S_Conv I32 wordRep)
223 [CmmLoad nursery_bdescr_blocks I32],
224 CmmLit (mkIntCLit bLOCK_SIZE)
232 nursery_bdescr_free = cmmOffset stgCurrentNursery oFFSET_bdescr_free
233 nursery_bdescr_start = cmmOffset stgCurrentNursery oFFSET_bdescr_start
234 nursery_bdescr_blocks = cmmOffset stgCurrentNursery oFFSET_bdescr_blocks
236 tso_SP = tsoFieldB oFFSET_StgTSO_sp
237 tso_STACK = tsoFieldB oFFSET_StgTSO_stack
238 tso_CCCS = tsoProfFieldB oFFSET_StgTSO_CCCS
240 -- The TSO struct has a variable header, and an optional StgTSOProfInfo in
241 -- the middle. The fields we're interested in are after the StgTSOProfInfo.
242 tsoFieldB :: ByteOff -> ByteOff
244 | opt_SccProfilingOn = off + sIZEOF_StgTSOProfInfo + fixedHdrSize * wORD_SIZE
245 | otherwise = off + fixedHdrSize * wORD_SIZE
247 tsoProfFieldB :: ByteOff -> ByteOff
248 tsoProfFieldB off = off + fixedHdrSize * wORD_SIZE
252 stgCurrentTSO = CmmReg currentTSO
253 stgCurrentNursery = CmmReg currentNursery
256 spLim = CmmGlobal SpLim
258 hpLim = CmmGlobal HpLim
259 currentTSO = CmmGlobal CurrentTSO
260 currentNursery = CmmGlobal CurrentNursery
262 -- -----------------------------------------------------------------------------
263 -- For certain types passed to foreign calls, we adjust the actual
264 -- value passed to the call. For ByteArray#/Array# we pass the
265 -- address of the actual array, not the address of the heap object.
267 shimForeignCallArg :: StgArg -> CmmExpr -> CmmExpr
268 shimForeignCallArg arg expr
269 | tycon == arrayPrimTyCon || tycon == mutableArrayPrimTyCon
270 = cmmOffsetB expr arrPtrsHdrSize
272 | tycon == byteArrayPrimTyCon || tycon == mutableByteArrayPrimTyCon
273 = cmmOffsetB expr arrWordsHdrSize
277 -- should be a tycon app, since this is a foreign call
278 tycon = tyConAppTyCon (repType (stgArgType arg))