FIX #38000 Store StgArrWords payload size in bytes
[ghc-hetmet.git] / compiler / codeGen / CgPrimOp.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Code generation for PrimOps.
4 --
5 -- (c) The University of Glasgow 2004-2006
6 --
7 -----------------------------------------------------------------------------
8
9 module CgPrimOp (
10    cgPrimOp
11  ) where
12
13 import ForeignCall
14 import ClosureInfo
15 import StgSyn
16 import CgForeignCall
17 import CgBindery
18 import CgMonad
19 import CgInfoTbls
20 import CgUtils
21 import Cmm
22 import CLabel
23 import CmmUtils
24 import PrimOp
25 import SMRep
26 import Module
27 import Constants
28 import Outputable
29 import FastString
30
31 -- ---------------------------------------------------------------------------
32 -- Code generation for PrimOps
33
34 cgPrimOp   :: CmmFormals        -- where to put the results
35            -> PrimOp            -- the op
36            -> [StgArg]          -- arguments
37            -> StgLiveVars       -- live vars, in case we need to save them
38            -> Code
39
40 cgPrimOp results op args live
41   = do arg_exprs <- getArgAmodes args
42        let non_void_args = [ e | (r,e) <- arg_exprs, nonVoidArg r ] 
43        emitPrimOp results op non_void_args live
44
45
46 emitPrimOp :: CmmFormals        -- where to put the results
47            -> PrimOp            -- the op
48            -> [CmmExpr]         -- arguments
49            -> StgLiveVars       -- live vars, in case we need to save them
50            -> Code
51
52 --  First we handle various awkward cases specially.  The remaining
53 -- easy cases are then handled by translateOp, defined below.
54
55 emitPrimOp [res_r,res_c] IntAddCOp [aa,bb] _
56 {- 
57    With some bit-twiddling, we can define int{Add,Sub}Czh portably in
58    C, and without needing any comparisons.  This may not be the
59    fastest way to do it - if you have better code, please send it! --SDM
60   
61    Return : r = a + b,  c = 0 if no overflow, 1 on overflow.
62   
63    We currently don't make use of the r value if c is != 0 (i.e. 
64    overflow), we just convert to big integers and try again.  This
65    could be improved by making r and c the correct values for
66    plugging into a new J#.  
67    
68    { r = ((I_)(a)) + ((I_)(b));                                 \
69      c = ((StgWord)(~(((I_)(a))^((I_)(b))) & (((I_)(a))^r)))    \
70          >> (BITS_IN (I_) - 1);                                 \
71    } 
72    Wading through the mass of bracketry, it seems to reduce to:
73    c = ( (~(a^b)) & (a^r) ) >>unsigned (BITS_IN(I_)-1)
74
75 -}
76    = stmtsC [
77         CmmAssign (CmmLocal res_r) (CmmMachOp mo_wordAdd [aa,bb]),
78         CmmAssign (CmmLocal res_c) $
79           CmmMachOp mo_wordUShr [
80                 CmmMachOp mo_wordAnd [
81                     CmmMachOp mo_wordNot [CmmMachOp mo_wordXor [aa,bb]],
82                     CmmMachOp mo_wordXor [aa, CmmReg (CmmLocal res_r)]
83                 ], 
84                 CmmLit (mkIntCLit (wORD_SIZE_IN_BITS - 1))
85           ]
86      ]
87
88
89 emitPrimOp [res_r,res_c] IntSubCOp [aa,bb] _
90 {- Similarly:
91    #define subIntCzh(r,c,a,b)                                   \
92    { r = ((I_)(a)) - ((I_)(b));                                 \
93      c = ((StgWord)((((I_)(a))^((I_)(b))) & (((I_)(a))^r)))     \
94          >> (BITS_IN (I_) - 1);                                 \
95    }
96
97    c =  ((a^b) & (a^r)) >>unsigned (BITS_IN(I_)-1)
98 -}
99    = stmtsC [
100         CmmAssign (CmmLocal res_r) (CmmMachOp mo_wordSub [aa,bb]),
101         CmmAssign (CmmLocal res_c) $
102           CmmMachOp mo_wordUShr [
103                 CmmMachOp mo_wordAnd [
104                     CmmMachOp mo_wordXor [aa,bb],
105                     CmmMachOp mo_wordXor [aa, CmmReg (CmmLocal res_r)]
106                 ], 
107                 CmmLit (mkIntCLit (wORD_SIZE_IN_BITS - 1))
108           ]
109      ]
110
111
112 emitPrimOp [res] ParOp [arg] live
113   = do
114         -- for now, just implement this in a C function
115         -- later, we might want to inline it.
116     vols <- getVolatileRegs live
117     emitForeignCall' PlayRisky
118         [CmmHinted res NoHint]
119         (CmmCallee newspark CCallConv) 
120         [   (CmmHinted (CmmReg (CmmGlobal BaseReg)) AddrHint)
121           , (CmmHinted arg AddrHint)  ] 
122         (Just vols)
123         NoC_SRT -- No SRT b/c we do PlayRisky
124         CmmMayReturn
125   where
126         newspark = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "newSpark")))
127
128 emitPrimOp [res] ReadMutVarOp [mutv] _
129    = stmtC (CmmAssign (CmmLocal res) (cmmLoadIndexW mutv fixedHdrSize gcWord))
130
131 emitPrimOp [] WriteMutVarOp [mutv,var] live
132    = do
133         stmtC (CmmStore (cmmOffsetW mutv fixedHdrSize) var)
134         vols <- getVolatileRegs live
135         emitForeignCall' PlayRisky
136                 [{-no results-}]
137                 (CmmCallee (CmmLit (CmmLabel mkDirty_MUT_VAR_Label))
138                          CCallConv)
139                 [   (CmmHinted (CmmReg (CmmGlobal BaseReg)) AddrHint)
140                   , (CmmHinted mutv AddrHint)  ]
141                 (Just vols)
142                 NoC_SRT -- No SRT b/c we do PlayRisky
143                 CmmMayReturn
144
145 --  #define sizzeofByteArrayzh(r,a) \
146 --     r = ((StgArrWords *)(a))->bytes
147 emitPrimOp [res] SizeofByteArrayOp [arg] _
148    = stmtC $
149         CmmAssign (CmmLocal res) (cmmLoadIndexW arg fixedHdrSize bWord)
150
151 --  #define sizzeofMutableByteArrayzh(r,a) \
152 --      r = ((StgArrWords *)(a))->bytes
153 emitPrimOp [res] SizeofMutableByteArrayOp [arg] live
154    = emitPrimOp [res] SizeofByteArrayOp [arg] live
155
156
157 --  #define touchzh(o)                  /* nothing */
158 emitPrimOp [] TouchOp [_] _
159    = nopC
160
161 --  #define byteArrayContentszh(r,a) r = BYTE_ARR_CTS(a)
162 emitPrimOp [res] ByteArrayContents_Char [arg] _
163    = stmtC (CmmAssign (CmmLocal res) (cmmOffsetB arg arrWordsHdrSize))
164
165 --  #define stableNameToIntzh(r,s)   (r = ((StgStableName *)s)->sn)
166 emitPrimOp [res] StableNameToIntOp [arg] _
167    = stmtC (CmmAssign (CmmLocal res) (cmmLoadIndexW arg fixedHdrSize bWord))
168
169 --  #define eqStableNamezh(r,sn1,sn2)                                   \
170 --    (r = (((StgStableName *)sn1)->sn == ((StgStableName *)sn2)->sn))
171 emitPrimOp [res] EqStableNameOp [arg1,arg2] _
172    = stmtC (CmmAssign (CmmLocal res) (CmmMachOp mo_wordEq [
173                                 cmmLoadIndexW arg1 fixedHdrSize bWord,
174                                 cmmLoadIndexW arg2 fixedHdrSize bWord
175                          ]))
176
177
178 emitPrimOp [res] ReallyUnsafePtrEqualityOp [arg1,arg2] _
179    = stmtC (CmmAssign (CmmLocal res) (CmmMachOp mo_wordEq [arg1,arg2]))
180
181 --  #define addrToHValuezh(r,a) r=(P_)a
182 emitPrimOp [res] AddrToHValueOp [arg] _
183    = stmtC (CmmAssign (CmmLocal res) arg)
184
185 --  #define dataToTagzh(r,a)  r=(GET_TAG(((StgClosure *)a)->header.info))
186 --  Note: argument may be tagged!
187 emitPrimOp [res] DataToTagOp [arg] _
188    = stmtC (CmmAssign (CmmLocal res) (getConstrTag (cmmUntag arg)))
189
190 {- Freezing arrays-of-ptrs requires changing an info table, for the
191    benefit of the generational collector.  It needs to scavenge mutable
192    objects, even if they are in old space.  When they become immutable,
193    they can be removed from this scavenge list.  -}
194
195 --  #define unsafeFreezzeArrayzh(r,a)
196 --      {
197 --        SET_INFO((StgClosure *)a,&stg_MUT_ARR_PTRS_FROZEN0_info);
198 --        r = a;
199 --      }
200 emitPrimOp [res] UnsafeFreezeArrayOp [arg] _
201    = stmtsC [ setInfo arg (CmmLit (CmmLabel mkMAP_FROZEN_infoLabel)),
202              CmmAssign (CmmLocal res) arg ]
203
204 --  #define unsafeFreezzeByteArrayzh(r,a)       r=(a)
205 emitPrimOp [res] UnsafeFreezeByteArrayOp [arg] _
206    = stmtC (CmmAssign (CmmLocal res) arg)
207
208 -- Reading/writing pointer arrays
209
210 emitPrimOp [r] ReadArrayOp  [obj,ix]   _  = doReadPtrArrayOp r obj ix
211 emitPrimOp [r] IndexArrayOp [obj,ix]   _  = doReadPtrArrayOp r obj ix
212 emitPrimOp []  WriteArrayOp [obj,ix,v] _  = doWritePtrArrayOp obj ix v
213
214 -- IndexXXXoffAddr
215
216 emitPrimOp res IndexOffAddrOp_Char      args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8 res args
217 emitPrimOp res IndexOffAddrOp_WideChar  args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
218 emitPrimOp res IndexOffAddrOp_Int       args _ = doIndexOffAddrOp Nothing bWord res args
219 emitPrimOp res IndexOffAddrOp_Word      args _ = doIndexOffAddrOp Nothing bWord res args
220 emitPrimOp res IndexOffAddrOp_Addr      args _ = doIndexOffAddrOp Nothing bWord res args
221 emitPrimOp res IndexOffAddrOp_Float     args _ = doIndexOffAddrOp Nothing f32 res args
222 emitPrimOp res IndexOffAddrOp_Double    args _ = doIndexOffAddrOp Nothing f64 res args
223 emitPrimOp res IndexOffAddrOp_StablePtr args _ = doIndexOffAddrOp Nothing bWord res args
224 emitPrimOp res IndexOffAddrOp_Int8      args _ = doIndexOffAddrOp (Just mo_s_8ToWord)  b8  res args
225 emitPrimOp res IndexOffAddrOp_Int16     args _ = doIndexOffAddrOp (Just mo_s_16ToWord) b16 res args
226 emitPrimOp res IndexOffAddrOp_Int32     args _ = doIndexOffAddrOp (Just mo_s_32ToWord) b32 res args
227 emitPrimOp res IndexOffAddrOp_Int64     args _ = doIndexOffAddrOp Nothing b64 res args
228 emitPrimOp res IndexOffAddrOp_Word8     args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8   res args
229 emitPrimOp res IndexOffAddrOp_Word16    args _ = doIndexOffAddrOp (Just mo_u_16ToWord) b16 res args
230 emitPrimOp res IndexOffAddrOp_Word32    args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
231 emitPrimOp res IndexOffAddrOp_Word64    args _ = doIndexOffAddrOp Nothing b64 res args
232
233 -- ReadXXXoffAddr, which are identical, for our purposes, to IndexXXXoffAddr.
234
235 emitPrimOp res ReadOffAddrOp_Char      args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8 res args
236 emitPrimOp res ReadOffAddrOp_WideChar  args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
237 emitPrimOp res ReadOffAddrOp_Int       args _ = doIndexOffAddrOp Nothing bWord res args
238 emitPrimOp res ReadOffAddrOp_Word      args _ = doIndexOffAddrOp Nothing bWord res args
239 emitPrimOp res ReadOffAddrOp_Addr      args _ = doIndexOffAddrOp Nothing bWord res args
240 emitPrimOp res ReadOffAddrOp_Float     args _ = doIndexOffAddrOp Nothing f32 res args
241 emitPrimOp res ReadOffAddrOp_Double    args _ = doIndexOffAddrOp Nothing f64 res args
242 emitPrimOp res ReadOffAddrOp_StablePtr args _ = doIndexOffAddrOp Nothing bWord res args
243 emitPrimOp res ReadOffAddrOp_Int8      args _ = doIndexOffAddrOp (Just mo_s_8ToWord) b8  res args
244 emitPrimOp res ReadOffAddrOp_Int16     args _ = doIndexOffAddrOp (Just mo_s_16ToWord) b16 res args
245 emitPrimOp res ReadOffAddrOp_Int32     args _ = doIndexOffAddrOp (Just mo_s_32ToWord) b32 res args
246 emitPrimOp res ReadOffAddrOp_Int64     args _ = doIndexOffAddrOp Nothing b64 res args
247 emitPrimOp res ReadOffAddrOp_Word8     args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8  res args
248 emitPrimOp res ReadOffAddrOp_Word16    args _ = doIndexOffAddrOp (Just mo_u_16ToWord) b16 res args
249 emitPrimOp res ReadOffAddrOp_Word32    args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
250 emitPrimOp res ReadOffAddrOp_Word64    args _ = doIndexOffAddrOp Nothing b64 res args
251
252 -- IndexXXXArray
253
254 emitPrimOp res IndexByteArrayOp_Char      args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8 res args
255 emitPrimOp res IndexByteArrayOp_WideChar  args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32 res args
256 emitPrimOp res IndexByteArrayOp_Int       args _ = doIndexByteArrayOp Nothing bWord res args
257 emitPrimOp res IndexByteArrayOp_Word      args _ = doIndexByteArrayOp Nothing bWord res args
258 emitPrimOp res IndexByteArrayOp_Addr      args _ = doIndexByteArrayOp Nothing bWord res args
259 emitPrimOp res IndexByteArrayOp_Float     args _ = doIndexByteArrayOp Nothing f32 res args
260 emitPrimOp res IndexByteArrayOp_Double    args _ = doIndexByteArrayOp Nothing f64 res args
261 emitPrimOp res IndexByteArrayOp_StablePtr args _ = doIndexByteArrayOp Nothing bWord res args
262 emitPrimOp res IndexByteArrayOp_Int8      args _ = doIndexByteArrayOp (Just mo_s_8ToWord) b8  res args
263 emitPrimOp res IndexByteArrayOp_Int16     args _ = doIndexByteArrayOp (Just mo_s_16ToWord) b16  res args
264 emitPrimOp res IndexByteArrayOp_Int32     args _ = doIndexByteArrayOp (Just mo_s_32ToWord) b32  res args
265 emitPrimOp res IndexByteArrayOp_Int64     args _ = doIndexByteArrayOp Nothing b64  res args
266 emitPrimOp res IndexByteArrayOp_Word8     args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8  res args
267 emitPrimOp res IndexByteArrayOp_Word16    args _ = doIndexByteArrayOp (Just mo_u_16ToWord) b16  res args
268 emitPrimOp res IndexByteArrayOp_Word32    args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32  res args
269 emitPrimOp res IndexByteArrayOp_Word64    args _ = doIndexByteArrayOp Nothing b64  res args
270
271 -- ReadXXXArray, identical to IndexXXXArray.
272
273 emitPrimOp res ReadByteArrayOp_Char       args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8 res args
274 emitPrimOp res ReadByteArrayOp_WideChar   args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32 res args
275 emitPrimOp res ReadByteArrayOp_Int        args _ = doIndexByteArrayOp Nothing bWord res args
276 emitPrimOp res ReadByteArrayOp_Word       args _ = doIndexByteArrayOp Nothing bWord res args
277 emitPrimOp res ReadByteArrayOp_Addr       args _ = doIndexByteArrayOp Nothing bWord res args
278 emitPrimOp res ReadByteArrayOp_Float      args _ = doIndexByteArrayOp Nothing f32 res args
279 emitPrimOp res ReadByteArrayOp_Double     args _ = doIndexByteArrayOp Nothing f64 res args
280 emitPrimOp res ReadByteArrayOp_StablePtr  args _ = doIndexByteArrayOp Nothing bWord res args
281 emitPrimOp res ReadByteArrayOp_Int8       args _ = doIndexByteArrayOp (Just mo_s_8ToWord) b8  res args
282 emitPrimOp res ReadByteArrayOp_Int16      args _ = doIndexByteArrayOp (Just mo_s_16ToWord) b16  res args
283 emitPrimOp res ReadByteArrayOp_Int32      args _ = doIndexByteArrayOp (Just mo_s_32ToWord) b32  res args
284 emitPrimOp res ReadByteArrayOp_Int64      args _ = doIndexByteArrayOp Nothing b64  res args
285 emitPrimOp res ReadByteArrayOp_Word8      args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8  res args
286 emitPrimOp res ReadByteArrayOp_Word16     args _ = doIndexByteArrayOp (Just mo_u_16ToWord) b16  res args
287 emitPrimOp res ReadByteArrayOp_Word32     args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32  res args
288 emitPrimOp res ReadByteArrayOp_Word64     args _ = doIndexByteArrayOp Nothing b64  res args
289
290 -- WriteXXXoffAddr
291
292 emitPrimOp res WriteOffAddrOp_Char       args _ = doWriteOffAddrOp (Just mo_WordTo8) b8 res args
293 emitPrimOp res WriteOffAddrOp_WideChar   args _ = doWriteOffAddrOp (Just mo_WordTo32) b32 res args
294 emitPrimOp res WriteOffAddrOp_Int        args _ = doWriteOffAddrOp Nothing bWord res args
295 emitPrimOp res WriteOffAddrOp_Word       args _ = doWriteOffAddrOp Nothing bWord res args
296 emitPrimOp res WriteOffAddrOp_Addr       args _ = doWriteOffAddrOp Nothing bWord res args
297 emitPrimOp res WriteOffAddrOp_Float      args _ = doWriteOffAddrOp Nothing f32 res args
298 emitPrimOp res WriteOffAddrOp_Double     args _ = doWriteOffAddrOp Nothing f64 res args
299 emitPrimOp res WriteOffAddrOp_StablePtr  args _ = doWriteOffAddrOp Nothing bWord res args
300 emitPrimOp res WriteOffAddrOp_Int8       args _ = doWriteOffAddrOp (Just mo_WordTo8) b8  res args
301 emitPrimOp res WriteOffAddrOp_Int16      args _ = doWriteOffAddrOp (Just mo_WordTo16) b16 res args
302 emitPrimOp res WriteOffAddrOp_Int32      args _ = doWriteOffAddrOp (Just mo_WordTo32) b32 res args
303 emitPrimOp res WriteOffAddrOp_Int64      args _ = doWriteOffAddrOp Nothing b64 res args
304 emitPrimOp res WriteOffAddrOp_Word8      args _ = doWriteOffAddrOp (Just mo_WordTo8) b8  res args
305 emitPrimOp res WriteOffAddrOp_Word16     args _ = doWriteOffAddrOp (Just mo_WordTo16) b16 res args
306 emitPrimOp res WriteOffAddrOp_Word32     args _ = doWriteOffAddrOp (Just mo_WordTo32) b32 res args
307 emitPrimOp res WriteOffAddrOp_Word64     args _ = doWriteOffAddrOp Nothing b64 res args
308
309 -- WriteXXXArray
310
311 emitPrimOp res WriteByteArrayOp_Char      args _ = doWriteByteArrayOp (Just mo_WordTo8) b8 res args
312 emitPrimOp res WriteByteArrayOp_WideChar  args _ = doWriteByteArrayOp (Just mo_WordTo32) b32 res args
313 emitPrimOp res WriteByteArrayOp_Int       args _ = doWriteByteArrayOp Nothing bWord res args
314 emitPrimOp res WriteByteArrayOp_Word      args _ = doWriteByteArrayOp Nothing bWord res args
315 emitPrimOp res WriteByteArrayOp_Addr      args _ = doWriteByteArrayOp Nothing bWord res args
316 emitPrimOp res WriteByteArrayOp_Float     args _ = doWriteByteArrayOp Nothing f32 res args
317 emitPrimOp res WriteByteArrayOp_Double    args _ = doWriteByteArrayOp Nothing f64 res args
318 emitPrimOp res WriteByteArrayOp_StablePtr args _ = doWriteByteArrayOp Nothing bWord res args
319 emitPrimOp res WriteByteArrayOp_Int8      args _ = doWriteByteArrayOp (Just mo_WordTo8) b8  res args
320 emitPrimOp res WriteByteArrayOp_Int16     args _ = doWriteByteArrayOp (Just mo_WordTo16) b16  res args
321 emitPrimOp res WriteByteArrayOp_Int32     args _ = doWriteByteArrayOp (Just mo_WordTo32) b32  res args
322 emitPrimOp res WriteByteArrayOp_Int64     args _ = doWriteByteArrayOp Nothing b64  res args
323 emitPrimOp res WriteByteArrayOp_Word8     args _ = doWriteByteArrayOp (Just mo_WordTo8) b8  res args
324 emitPrimOp res WriteByteArrayOp_Word16    args _ = doWriteByteArrayOp (Just mo_WordTo16) b16  res args
325 emitPrimOp res WriteByteArrayOp_Word32    args _ = doWriteByteArrayOp (Just mo_WordTo32) b32  res args
326 emitPrimOp res WriteByteArrayOp_Word64    args _ = doWriteByteArrayOp Nothing b64  res args
327
328
329 -- The rest just translate straightforwardly
330 emitPrimOp [res] op [arg] _
331    | nopOp op
332    = stmtC (CmmAssign (CmmLocal res) arg)
333
334    | Just (mop,rep) <- narrowOp op
335    = stmtC (CmmAssign (CmmLocal res) $
336             CmmMachOp (mop rep wordWidth) [CmmMachOp (mop wordWidth rep) [arg]])
337
338 emitPrimOp [res] op args live
339    | Just prim <- callishOp op
340    = do vols <- getVolatileRegs live
341         emitForeignCall' PlayRisky
342            [CmmHinted res NoHint] 
343            (CmmPrim prim) 
344            [CmmHinted a NoHint | a<-args]  -- ToDo: hints?
345            (Just vols)
346            NoC_SRT -- No SRT b/c we do PlayRisky
347            CmmMayReturn
348
349    | Just mop <- translateOp op
350    = let stmt = CmmAssign (CmmLocal res) (CmmMachOp mop args) in
351      stmtC stmt
352
353 emitPrimOp _ op _ _
354  = pprPanic "emitPrimOp: can't translate PrimOp" (ppr op)
355
356
357 -- These PrimOps are NOPs in Cmm
358
359 nopOp :: PrimOp -> Bool
360 nopOp Int2WordOp     = True
361 nopOp Word2IntOp     = True
362 nopOp Int2AddrOp     = True
363 nopOp Addr2IntOp     = True
364 nopOp ChrOp          = True  -- Int# and Char# are rep'd the same
365 nopOp OrdOp          = True
366 nopOp _              = False
367
368 -- These PrimOps turn into double casts
369
370 narrowOp :: PrimOp -> Maybe (Width -> Width -> MachOp, Width)
371 narrowOp Narrow8IntOp   = Just (MO_SS_Conv, W8)
372 narrowOp Narrow16IntOp  = Just (MO_SS_Conv, W16)
373 narrowOp Narrow32IntOp  = Just (MO_SS_Conv, W32)
374 narrowOp Narrow8WordOp  = Just (MO_UU_Conv, W8)
375 narrowOp Narrow16WordOp = Just (MO_UU_Conv, W16)
376 narrowOp Narrow32WordOp = Just (MO_UU_Conv, W32)
377 narrowOp _              = Nothing
378
379 -- Native word signless ops
380
381 translateOp :: PrimOp -> Maybe MachOp
382 translateOp IntAddOp       = Just mo_wordAdd
383 translateOp IntSubOp       = Just mo_wordSub
384 translateOp WordAddOp      = Just mo_wordAdd
385 translateOp WordSubOp      = Just mo_wordSub
386 translateOp AddrAddOp      = Just mo_wordAdd
387 translateOp AddrSubOp      = Just mo_wordSub
388
389 translateOp IntEqOp        = Just mo_wordEq
390 translateOp IntNeOp        = Just mo_wordNe
391 translateOp WordEqOp       = Just mo_wordEq
392 translateOp WordNeOp       = Just mo_wordNe
393 translateOp AddrEqOp       = Just mo_wordEq
394 translateOp AddrNeOp       = Just mo_wordNe
395
396 translateOp AndOp          = Just mo_wordAnd
397 translateOp OrOp           = Just mo_wordOr
398 translateOp XorOp          = Just mo_wordXor
399 translateOp NotOp          = Just mo_wordNot
400 translateOp SllOp          = Just mo_wordShl
401 translateOp SrlOp          = Just mo_wordUShr
402
403 translateOp AddrRemOp      = Just mo_wordURem
404
405 -- Native word signed ops
406
407 translateOp IntMulOp        = Just mo_wordMul
408 translateOp IntMulMayOfloOp = Just (MO_S_MulMayOflo wordWidth)
409 translateOp IntQuotOp       = Just mo_wordSQuot
410 translateOp IntRemOp        = Just mo_wordSRem
411 translateOp IntNegOp        = Just mo_wordSNeg
412
413
414 translateOp IntGeOp        = Just mo_wordSGe
415 translateOp IntLeOp        = Just mo_wordSLe
416 translateOp IntGtOp        = Just mo_wordSGt
417 translateOp IntLtOp        = Just mo_wordSLt
418
419 translateOp ISllOp         = Just mo_wordShl
420 translateOp ISraOp         = Just mo_wordSShr
421 translateOp ISrlOp         = Just mo_wordUShr
422
423 -- Native word unsigned ops
424
425 translateOp WordGeOp       = Just mo_wordUGe
426 translateOp WordLeOp       = Just mo_wordULe
427 translateOp WordGtOp       = Just mo_wordUGt
428 translateOp WordLtOp       = Just mo_wordULt
429
430 translateOp WordMulOp      = Just mo_wordMul
431 translateOp WordQuotOp     = Just mo_wordUQuot
432 translateOp WordRemOp      = Just mo_wordURem
433
434 translateOp AddrGeOp       = Just mo_wordUGe
435 translateOp AddrLeOp       = Just mo_wordULe
436 translateOp AddrGtOp       = Just mo_wordUGt
437 translateOp AddrLtOp       = Just mo_wordULt
438
439 -- Char# ops
440
441 translateOp CharEqOp       = Just (MO_Eq wordWidth)
442 translateOp CharNeOp       = Just (MO_Ne wordWidth)
443 translateOp CharGeOp       = Just (MO_U_Ge wordWidth)
444 translateOp CharLeOp       = Just (MO_U_Le wordWidth)
445 translateOp CharGtOp       = Just (MO_U_Gt wordWidth)
446 translateOp CharLtOp       = Just (MO_U_Lt wordWidth)
447
448 -- Double ops
449
450 translateOp DoubleEqOp     = Just (MO_F_Eq W64)
451 translateOp DoubleNeOp     = Just (MO_F_Ne W64)
452 translateOp DoubleGeOp     = Just (MO_F_Ge W64)
453 translateOp DoubleLeOp     = Just (MO_F_Le W64)
454 translateOp DoubleGtOp     = Just (MO_F_Gt W64)
455 translateOp DoubleLtOp     = Just (MO_F_Lt W64)
456
457 translateOp DoubleAddOp    = Just (MO_F_Add W64)
458 translateOp DoubleSubOp    = Just (MO_F_Sub W64)
459 translateOp DoubleMulOp    = Just (MO_F_Mul W64)
460 translateOp DoubleDivOp    = Just (MO_F_Quot W64)
461 translateOp DoubleNegOp    = Just (MO_F_Neg W64)
462
463 -- Float ops
464
465 translateOp FloatEqOp     = Just (MO_F_Eq W32)
466 translateOp FloatNeOp     = Just (MO_F_Ne W32)
467 translateOp FloatGeOp     = Just (MO_F_Ge W32)
468 translateOp FloatLeOp     = Just (MO_F_Le W32)
469 translateOp FloatGtOp     = Just (MO_F_Gt W32)
470 translateOp FloatLtOp     = Just (MO_F_Lt W32)
471
472 translateOp FloatAddOp    = Just (MO_F_Add  W32)
473 translateOp FloatSubOp    = Just (MO_F_Sub  W32)
474 translateOp FloatMulOp    = Just (MO_F_Mul  W32)
475 translateOp FloatDivOp    = Just (MO_F_Quot W32)
476 translateOp FloatNegOp    = Just (MO_F_Neg  W32)
477
478 -- Conversions
479
480 translateOp Int2DoubleOp   = Just (MO_SF_Conv wordWidth W64)
481 translateOp Double2IntOp   = Just (MO_FS_Conv W64 wordWidth)
482
483 translateOp Int2FloatOp    = Just (MO_SF_Conv wordWidth W32)
484 translateOp Float2IntOp    = Just (MO_FS_Conv W32 wordWidth)
485
486 translateOp Float2DoubleOp = Just (MO_FF_Conv W32 W64)
487 translateOp Double2FloatOp = Just (MO_FF_Conv W64 W32)
488
489 -- Word comparisons masquerading as more exotic things.
490
491 translateOp SameMutVarOp           = Just mo_wordEq
492 translateOp SameMVarOp             = Just mo_wordEq
493 translateOp SameMutableArrayOp     = Just mo_wordEq
494 translateOp SameMutableByteArrayOp = Just mo_wordEq
495 translateOp SameTVarOp             = Just mo_wordEq
496 translateOp EqStablePtrOp          = Just mo_wordEq
497
498 translateOp _ = Nothing
499
500 -- These primops are implemented by CallishMachOps, because they sometimes
501 -- turn into foreign calls depending on the backend.
502
503 callishOp :: PrimOp -> Maybe CallishMachOp
504 callishOp DoublePowerOp  = Just MO_F64_Pwr
505 callishOp DoubleSinOp    = Just MO_F64_Sin
506 callishOp DoubleCosOp    = Just MO_F64_Cos
507 callishOp DoubleTanOp    = Just MO_F64_Tan
508 callishOp DoubleSinhOp   = Just MO_F64_Sinh
509 callishOp DoubleCoshOp   = Just MO_F64_Cosh
510 callishOp DoubleTanhOp   = Just MO_F64_Tanh
511 callishOp DoubleAsinOp   = Just MO_F64_Asin
512 callishOp DoubleAcosOp   = Just MO_F64_Acos
513 callishOp DoubleAtanOp   = Just MO_F64_Atan
514 callishOp DoubleLogOp    = Just MO_F64_Log
515 callishOp DoubleExpOp    = Just MO_F64_Exp
516 callishOp DoubleSqrtOp   = Just MO_F64_Sqrt
517
518 callishOp FloatPowerOp  = Just MO_F32_Pwr
519 callishOp FloatSinOp    = Just MO_F32_Sin
520 callishOp FloatCosOp    = Just MO_F32_Cos
521 callishOp FloatTanOp    = Just MO_F32_Tan
522 callishOp FloatSinhOp   = Just MO_F32_Sinh
523 callishOp FloatCoshOp   = Just MO_F32_Cosh
524 callishOp FloatTanhOp   = Just MO_F32_Tanh
525 callishOp FloatAsinOp   = Just MO_F32_Asin
526 callishOp FloatAcosOp   = Just MO_F32_Acos
527 callishOp FloatAtanOp   = Just MO_F32_Atan
528 callishOp FloatLogOp    = Just MO_F32_Log
529 callishOp FloatExpOp    = Just MO_F32_Exp
530 callishOp FloatSqrtOp   = Just MO_F32_Sqrt
531
532 callishOp _ = Nothing
533
534 ------------------------------------------------------------------------------
535 -- Helpers for translating various minor variants of array indexing.
536
537 -- Bytearrays outside the heap; hence non-pointers
538 doIndexOffAddrOp, doIndexByteArrayOp 
539         :: Maybe MachOp -> CmmType 
540         -> [LocalReg] -> [CmmExpr] -> Code
541 doIndexOffAddrOp maybe_post_read_cast rep [res] [addr,idx]
542    = mkBasicIndexedRead 0 maybe_post_read_cast rep res addr idx
543 doIndexOffAddrOp _ _ _ _
544    = panic "CgPrimOp: doIndexOffAddrOp"
545
546 doIndexByteArrayOp maybe_post_read_cast rep [res] [addr,idx]
547    = mkBasicIndexedRead arrWordsHdrSize maybe_post_read_cast rep res addr idx
548 doIndexByteArrayOp _ _ _ _ 
549    = panic "CgPrimOp: doIndexByteArrayOp"
550
551 doReadPtrArrayOp :: LocalReg -> CmmExpr -> CmmExpr -> Code
552 doReadPtrArrayOp res addr idx
553    = mkBasicIndexedRead arrPtrsHdrSize Nothing gcWord res addr idx
554
555
556 doWriteOffAddrOp, doWriteByteArrayOp 
557         :: Maybe MachOp -> CmmType 
558         -> [LocalReg] -> [CmmExpr] -> Code
559 doWriteOffAddrOp maybe_pre_write_cast rep [] [addr,idx,val]
560    = mkBasicIndexedWrite 0 maybe_pre_write_cast rep addr idx val
561 doWriteOffAddrOp _ _ _ _
562    = panic "CgPrimOp: doWriteOffAddrOp"
563
564 doWriteByteArrayOp maybe_pre_write_cast rep [] [addr,idx,val]
565    = mkBasicIndexedWrite arrWordsHdrSize maybe_pre_write_cast rep addr idx val
566 doWriteByteArrayOp _ _ _ _ 
567    = panic "CgPrimOp: doWriteByteArrayOp"
568
569 doWritePtrArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> Code
570 doWritePtrArrayOp addr idx val
571    = do mkBasicIndexedWrite arrPtrsHdrSize Nothing bWord addr idx val
572         stmtC (setInfo addr (CmmLit (CmmLabel mkMAP_DIRTY_infoLabel)))
573    -- the write barrier.  We must write a byte into the mark table:
574    -- bits8[a + header_size + StgMutArrPtrs_size(a) + x >> N]
575         stmtC $ CmmStore (
576           cmmOffsetExpr
577            (cmmOffsetExprW (cmmOffsetB addr arrPtrsHdrSize)
578                           (loadArrPtrsSize addr))
579            (CmmMachOp mo_wordUShr [idx,
580                                    CmmLit (mkIntCLit mUT_ARR_PTRS_CARD_BITS)])
581           ) (CmmLit (CmmInt 1 W8))
582
583 loadArrPtrsSize :: CmmExpr -> CmmExpr
584 loadArrPtrsSize addr = CmmLoad (cmmOffsetB addr off) bWord
585  where off = fixedHdrSize*wORD_SIZE + oFFSET_StgMutArrPtrs_ptrs
586
587 mkBasicIndexedRead :: ByteOff -> Maybe MachOp -> CmmType 
588                    -> LocalReg -> CmmExpr -> CmmExpr -> Code
589 mkBasicIndexedRead off Nothing read_rep res base idx
590    = stmtC (CmmAssign (CmmLocal res) (cmmLoadIndexOffExpr off read_rep base idx))
591 mkBasicIndexedRead off (Just cast) read_rep res base idx
592    = stmtC (CmmAssign (CmmLocal res) (CmmMachOp cast [
593                                 cmmLoadIndexOffExpr off read_rep base idx]))
594
595 mkBasicIndexedWrite :: ByteOff -> Maybe MachOp -> CmmType 
596                     -> CmmExpr -> CmmExpr -> CmmExpr -> Code
597 mkBasicIndexedWrite off Nothing write_rep base idx val
598    = stmtC (CmmStore (cmmIndexOffExpr off write_rep base idx) val)
599 mkBasicIndexedWrite off (Just cast) write_rep base idx val
600    = stmtC (CmmStore (cmmIndexOffExpr off write_rep base idx) (CmmMachOp cast [val]))
601
602 -- ----------------------------------------------------------------------------
603 -- Misc utils
604
605 cmmIndexOffExpr :: ByteOff -> CmmType -> CmmExpr -> CmmExpr -> CmmExpr
606 cmmIndexOffExpr off rep base idx
607    = cmmIndexExpr (typeWidth rep) (cmmOffsetB base off) idx
608
609 cmmLoadIndexOffExpr :: ByteOff -> CmmType -> CmmExpr -> CmmExpr -> CmmExpr
610 cmmLoadIndexOffExpr off rep base idx
611    = CmmLoad (cmmIndexOffExpr off rep base idx) rep
612
613 setInfo :: CmmExpr -> CmmExpr -> CmmStmt
614 setInfo closure_ptr info_ptr = CmmStore closure_ptr info_ptr
615