CgPrimOp.hs 47.7 KB
Newer Older
1 2 3 4
-----------------------------------------------------------------------------
--
-- Code generation for PrimOps.
--
Simon Marlow's avatar
Simon Marlow committed
5
-- (c) The University of Glasgow 2004-2006
6 7 8 9
--
-----------------------------------------------------------------------------

module CgPrimOp (
dterei's avatar
dterei committed
10 11
        cgPrimOp
    ) where
12

tibbe's avatar
tibbe committed
13
import BasicTypes
Simon Marlow's avatar
Simon Marlow committed
14
import ForeignCall
15
import ClosureInfo
Simon Marlow's avatar
Simon Marlow committed
16 17 18
import StgSyn
import CgForeignCall
import CgBindery
19
import CgMonad
tibbe's avatar
tibbe committed
20
import CgHeapery
Simon Marlow's avatar
Simon Marlow committed
21
import CgInfoTbls
tibbe's avatar
tibbe committed
22 23
import CgTicky
import CgProf
Simon Marlow's avatar
Simon Marlow committed
24
import CgUtils
25
import OldCmm
Simon Marlow's avatar
Simon Marlow committed
26
import CLabel
27
import OldCmmUtils
Simon Marlow's avatar
Simon Marlow committed
28 29
import PrimOp
import SMRep
30
import Module
Simon Marlow's avatar
Simon Marlow committed
31
import Constants
32
import Outputable
33
import FastString
34
import StaticFlags
35

Ian Lynagh's avatar
Ian Lynagh committed
36 37
import Control.Monad

38 39 40
-- ---------------------------------------------------------------------------
-- Code generation for PrimOps

dterei's avatar
dterei committed
41 42 43 44 45
cgPrimOp :: [CmmFormal]       -- where to put the results
         -> PrimOp            -- the op
         -> [StgArg]          -- arguments
         -> StgLiveVars       -- live vars, in case we need to save them
         -> Code
46 47 48

cgPrimOp results op args live
  = do arg_exprs <- getArgAmodes args
dterei's avatar
dterei committed
49
       let non_void_args = [ e | (r,e) <- arg_exprs, nonVoidArg r ]
50 51 52
       emitPrimOp results op non_void_args live


dterei's avatar
dterei committed
53 54 55 56 57
emitPrimOp :: [CmmFormal]       -- where to put the results
           -> PrimOp            -- the op
           -> [CmmExpr]         -- arguments
           -> StgLiveVars       -- live vars, in case we need to save them
           -> Code
58 59 60 61

--  First we handle various awkward cases specially.  The remaining
-- easy cases are then handled by translateOp, defined below.

Ian Lynagh's avatar
Ian Lynagh committed
62
emitPrimOp [res_r,res_c] IntAddCOp [aa,bb] _
dterei's avatar
dterei committed
63
{-
64 65 66
   With some bit-twiddling, we can define int{Add,Sub}Czh portably in
   C, and without needing any comparisons.  This may not be the
   fastest way to do it - if you have better code, please send it! --SDM
dterei's avatar
dterei committed
67

68
   Return : r = a + b,  c = 0 if no overflow, 1 on overflow.
dterei's avatar
dterei committed
69 70

   We currently don't make use of the r value if c is != 0 (i.e.
71 72
   overflow), we just convert to big integers and try again.  This
   could be improved by making r and c the correct values for
dterei's avatar
dterei committed
73 74 75 76 77 78
   plugging into a new J#.

   { r = ((I_)(a)) + ((I_)(b));                                 \
     c = ((StgWord)(~(((I_)(a))^((I_)(b))) & (((I_)(a))^r)))    \
         >> (BITS_IN (I_) - 1);                                 \
   }
79 80 81 82 83
   Wading through the mass of bracketry, it seems to reduce to:
   c = ( (~(a^b)) & (a^r) ) >>unsigned (BITS_IN(I_)-1)

-}
   = stmtsC [
84 85
        CmmAssign (CmmLocal res_r) (CmmMachOp mo_wordAdd [aa,bb]),
        CmmAssign (CmmLocal res_c) $
dterei's avatar
dterei committed
86 87 88 89 90 91 92
          CmmMachOp mo_wordUShr [
                CmmMachOp mo_wordAnd [
                    CmmMachOp mo_wordNot [CmmMachOp mo_wordXor [aa,bb]],
                    CmmMachOp mo_wordXor [aa, CmmReg (CmmLocal res_r)]
                ],
                CmmLit (mkIntCLit (wORD_SIZE_IN_BITS - 1))
          ]
93 94 95
     ]


Ian Lynagh's avatar
Ian Lynagh committed
96
emitPrimOp [res_r,res_c] IntSubCOp [aa,bb] _
97
{- Similarly:
dterei's avatar
dterei committed
98 99 100 101
   #define subIntCzh(r,c,a,b)                                   \
   { r = ((I_)(a)) - ((I_)(b));                                 \
     c = ((StgWord)((((I_)(a))^((I_)(b))) & (((I_)(a))^r)))     \
         >> (BITS_IN (I_) - 1);                                 \
102 103 104 105 106
   }

   c =  ((a^b) & (a^r)) >>unsigned (BITS_IN(I_)-1)
-}
   = stmtsC [
107 108
        CmmAssign (CmmLocal res_r) (CmmMachOp mo_wordSub [aa,bb]),
        CmmAssign (CmmLocal res_c) $
dterei's avatar
dterei committed
109 110 111 112 113 114 115
          CmmMachOp mo_wordUShr [
                CmmMachOp mo_wordAnd [
                    CmmMachOp mo_wordXor [aa,bb],
                    CmmMachOp mo_wordXor [aa, CmmReg (CmmLocal res_r)]
                ],
                CmmLit (mkIntCLit (wORD_SIZE_IN_BITS - 1))
          ]
116 117 118 119
     ]


emitPrimOp [res] ParOp [arg] live
120
  = do
dterei's avatar
dterei committed
121 122
        -- for now, just implement this in a C function
        -- later, we might want to inline it.
123
    vols <- getVolatileRegs live
124
    emitForeignCall' PlayRisky
dterei's avatar
dterei committed
125 126 127 128 129
        [CmmHinted res NoHint]
        (CmmCallee newspark CCallConv)
        [   (CmmHinted (CmmReg (CmmGlobal BaseReg)) AddrHint)
          , (CmmHinted arg AddrHint)  ]
        (Just vols)
130
        NoC_SRT -- No SRT b/c we do PlayRisky
131
        CmmMayReturn
Simon Marlow's avatar
Simon Marlow committed
132 133 134 135 136 137 138 139 140 141 142
  where
        newspark = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "newSpark")))

emitPrimOp [res] SparkOp [arg] live = do
    -- returns the value of arg in res.  We're going to therefore
    -- refer to arg twice (once to pass to newSpark(), and once to
    -- assign to res), so put it in a temporary.
    tmp <- newTemp bWord
    stmtC (CmmAssign (CmmLocal tmp) arg)

    vols <- getVolatileRegs live
143 144 145
    res' <- newTemp bWord
    emitForeignCall' PlayRisky
        [CmmHinted res' NoHint]
dterei's avatar
dterei committed
146 147 148 149
        (CmmCallee newspark CCallConv)
        [   (CmmHinted (CmmReg (CmmGlobal BaseReg)) AddrHint)
          , (CmmHinted arg AddrHint)  ]
        (Just vols)
Simon Marlow's avatar
Simon Marlow committed
150 151 152
        NoC_SRT -- No SRT b/c we do PlayRisky
        CmmMayReturn
    stmtC (CmmAssign (CmmLocal res) (CmmReg (CmmLocal tmp)))
153
  where
dterei's avatar
dterei committed
154
        newspark = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "newSpark")))
155

156 157 158 159 160 161 162
emitPrimOp [res] GetCCSOfOp [arg] _live
  = stmtC (CmmAssign (CmmLocal res) val)
  where
    val | opt_SccProfilingOn = costCentreFrom (cmmUntag arg)
        | otherwise          = CmmLit zeroCLit

emitPrimOp [res] GetCurrentCCSOp [_dummy_arg] _live
163
   = stmtC (CmmAssign (CmmLocal res) curCCS)
Simon Marlow's avatar
Simon Marlow committed
164

Ian Lynagh's avatar
Ian Lynagh committed
165
emitPrimOp [res] ReadMutVarOp [mutv] _
166
   = stmtC (CmmAssign (CmmLocal res) (cmmLoadIndexW mutv fixedHdrSize gcWord))
167 168

emitPrimOp [] WriteMutVarOp [mutv,var] live
169
   = do
dterei's avatar
dterei committed
170 171 172 173 174 175 176
        stmtC (CmmStore (cmmOffsetW mutv fixedHdrSize) var)
        vols <- getVolatileRegs live
        emitForeignCall' PlayRisky
                [{-no results-}]
                (CmmCallee (CmmLit (CmmLabel mkDirty_MUT_VAR_Label))
                         CCallConv)
                [   (CmmHinted (CmmReg (CmmGlobal BaseReg)) AddrHint)
177
                  , (CmmHinted mutv AddrHint)  ]
dterei's avatar
dterei committed
178
                (Just vols)
179
                NoC_SRT -- No SRT b/c we do PlayRisky
180
                CmmMayReturn
181

182
--  #define sizzeofByteArrayzh(r,a) \
183
--     r = ((StgArrWords *)(a))->bytes
Ian Lynagh's avatar
Ian Lynagh committed
184
emitPrimOp [res] SizeofByteArrayOp [arg] _
185
   = stmtC $
dterei's avatar
dterei committed
186
        CmmAssign (CmmLocal res) (cmmLoadIndexW arg fixedHdrSize bWord)
187

188
--  #define sizzeofMutableByteArrayzh(r,a) \
189
--      r = ((StgArrWords *)(a))->bytes
190 191 192 193
emitPrimOp [res] SizeofMutableByteArrayOp [arg] live
   = emitPrimOp [res] SizeofByteArrayOp [arg] live


194
--  #define touchzh(o)                  /* nothing */
Ian Lynagh's avatar
Ian Lynagh committed
195
emitPrimOp [] TouchOp [_] _
196 197
   = nopC

198
--  #define byteArrayContentszh(r,a) r = BYTE_ARR_CTS(a)
Ian Lynagh's avatar
Ian Lynagh committed
199
emitPrimOp [res] ByteArrayContents_Char [arg] _
200
   = stmtC (CmmAssign (CmmLocal res) (cmmOffsetB arg arrWordsHdrSize))
201

202
--  #define stableNameToIntzh(r,s)   (r = ((StgStableName *)s)->sn)
Ian Lynagh's avatar
Ian Lynagh committed
203
emitPrimOp [res] StableNameToIntOp [arg] _
204
   = stmtC (CmmAssign (CmmLocal res) (cmmLoadIndexW arg fixedHdrSize bWord))
205

dterei's avatar
dterei committed
206
--  #define eqStableNamezh(r,sn1,sn2)                                   \
207
--    (r = (((StgStableName *)sn1)->sn == ((StgStableName *)sn2)->sn))
Ian Lynagh's avatar
Ian Lynagh committed
208
emitPrimOp [res] EqStableNameOp [arg1,arg2] _
209
   = stmtC (CmmAssign (CmmLocal res) (CmmMachOp mo_wordEq [
dterei's avatar
dterei committed
210 211 212
                                cmmLoadIndexW arg1 fixedHdrSize bWord,
                                cmmLoadIndexW arg2 fixedHdrSize bWord
                         ]))
213 214


Ian Lynagh's avatar
Ian Lynagh committed
215
emitPrimOp [res] ReallyUnsafePtrEqualityOp [arg1,arg2] _
216
   = stmtC (CmmAssign (CmmLocal res) (CmmMachOp mo_wordEq [arg1,arg2]))
217

218
--  #define addrToHValuezh(r,a) r=(P_)a
Ian Lynagh's avatar
Ian Lynagh committed
219
emitPrimOp [res] AddrToAnyOp [arg] _
220
   = stmtC (CmmAssign (CmmLocal res) arg)
221

222
--  #define dataToTagzh(r,a)  r=(GET_TAG(((StgClosure *)a)->header.info))
Simon Marlow's avatar
Simon Marlow committed
223
--  Note: argument may be tagged!
Ian Lynagh's avatar
Ian Lynagh committed
224
emitPrimOp [res] DataToTagOp [arg] _
Simon Marlow's avatar
Simon Marlow committed
225
   = stmtC (CmmAssign (CmmLocal res) (getConstrTag (cmmUntag arg)))
226 227 228 229

{- Freezing arrays-of-ptrs requires changing an info table, for the
   benefit of the generational collector.  It needs to scavenge mutable
   objects, even if they are in old space.  When they become immutable,
dterei's avatar
dterei committed
230
   they can be removed from this scavenge list.  -}
231

232
--  #define unsafeFreezzeArrayzh(r,a)
dterei's avatar
dterei committed
233
--      {
234
--        SET_INFO((StgClosure *)a,&stg_MUT_ARR_PTRS_FROZEN0_info);
dterei's avatar
dterei committed
235 236
--        r = a;
--      }
Ian Lynagh's avatar
Ian Lynagh committed
237
emitPrimOp [res] UnsafeFreezeArrayOp [arg] _
238
   = stmtsC [ setInfo arg (CmmLit (CmmLabel mkMAP_FROZEN_infoLabel)),
239 240 241 242
       CmmAssign (CmmLocal res) arg ]
emitPrimOp [res] UnsafeFreezeArrayArrayOp [arg] _
   = stmtsC [ setInfo arg (CmmLit (CmmLabel mkMAP_FROZEN_infoLabel)),
       CmmAssign (CmmLocal res) arg ]
243

dterei's avatar
dterei committed
244
--  #define unsafeFreezzeByteArrayzh(r,a)       r=(a)
Ian Lynagh's avatar
Ian Lynagh committed
245
emitPrimOp [res] UnsafeFreezeByteArrayOp [arg] _
246
   = stmtC (CmmAssign (CmmLocal res) arg)
247

tibbe's avatar
tibbe committed
248 249 250 251 252 253 254 255 256 257 258 259 260
emitPrimOp [] CopyArrayOp [src,src_off,dst,dst_off,n] live =
    doCopyArrayOp src src_off dst dst_off n live
emitPrimOp [] CopyMutableArrayOp [src,src_off,dst,dst_off,n] live =
    doCopyMutableArrayOp src src_off dst dst_off n live
emitPrimOp [res] CloneArrayOp [src,src_off,n] live =
    emitCloneArray mkMAP_FROZEN_infoLabel res src src_off n live
emitPrimOp [res] CloneMutableArrayOp [src,src_off,n] live =
    emitCloneArray mkMAP_DIRTY_infoLabel res src src_off n live
emitPrimOp [res] FreezeArrayOp [src,src_off,n] live =
    emitCloneArray mkMAP_FROZEN_infoLabel res src src_off n live
emitPrimOp [res] ThawArrayOp [src,src_off,n] live =
    emitCloneArray mkMAP_DIRTY_infoLabel res src src_off n live

261 262 263 264 265
emitPrimOp [] CopyArrayArrayOp [src,src_off,dst,dst_off,n] live =
    doCopyArrayOp src src_off dst dst_off n live
emitPrimOp [] CopyMutableArrayArrayOp [src,src_off,dst,dst_off,n] live =
    doCopyMutableArrayOp src src_off dst dst_off n live

266 267
-- Reading/writing pointer arrays

Ian Lynagh's avatar
Ian Lynagh committed
268 269 270
emitPrimOp [r] ReadArrayOp  [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp [r] IndexArrayOp [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp []  WriteArrayOp [obj,ix,v] _  = doWritePtrArrayOp obj ix v
271

272 273 274 275 276 277 278 279 280 281 282
emitPrimOp [r] IndexArrayArrayOp_ByteArray         [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp [r] IndexArrayArrayOp_ArrayArray        [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp [r] ReadArrayArrayOp_ByteArray          [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp [r] ReadArrayArrayOp_MutableByteArray   [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp [r] ReadArrayArrayOp_ArrayArray         [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp [r] ReadArrayArrayOp_MutableArrayArray  [obj,ix]   _  = doReadPtrArrayOp r obj ix
emitPrimOp []  WriteArrayArrayOp_ByteArray         [obj,ix,v] _  = doWritePtrArrayOp obj ix v
emitPrimOp []  WriteArrayArrayOp_MutableByteArray  [obj,ix,v] _  = doWritePtrArrayOp obj ix v
emitPrimOp []  WriteArrayArrayOp_ArrayArray        [obj,ix,v] _  = doWritePtrArrayOp obj ix v
emitPrimOp []  WriteArrayArrayOp_MutableArrayArray [obj,ix,v] _  = doWritePtrArrayOp obj ix v

pumpkin's avatar
pumpkin committed
283
emitPrimOp [res] SizeofArrayOp [arg] _
dterei's avatar
dterei committed
284
   = stmtC $
285
       CmmAssign (CmmLocal res) (cmmLoadIndexW arg (fixedHdrSize + oFFSET_StgMutArrPtrs_ptrs) bWord)
pumpkin's avatar
pumpkin committed
286 287
emitPrimOp [res] SizeofMutableArrayOp [arg] live
   = emitPrimOp [res] SizeofArrayOp [arg] live
288 289 290 291
emitPrimOp [res] SizeofArrayArrayOp [arg] live
   = emitPrimOp [res] SizeofArrayOp [arg] live
emitPrimOp [res] SizeofMutableArrayArrayOp [arg] live
   = emitPrimOp [res] SizeofArrayOp [arg] live
pumpkin's avatar
pumpkin committed
292

293 294
-- IndexXXXoffAddr

Ian Lynagh's avatar
Ian Lynagh committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
emitPrimOp res IndexOffAddrOp_Char      args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8 res args
emitPrimOp res IndexOffAddrOp_WideChar  args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
emitPrimOp res IndexOffAddrOp_Int       args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res IndexOffAddrOp_Word      args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res IndexOffAddrOp_Addr      args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res IndexOffAddrOp_Float     args _ = doIndexOffAddrOp Nothing f32 res args
emitPrimOp res IndexOffAddrOp_Double    args _ = doIndexOffAddrOp Nothing f64 res args
emitPrimOp res IndexOffAddrOp_StablePtr args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res IndexOffAddrOp_Int8      args _ = doIndexOffAddrOp (Just mo_s_8ToWord)  b8  res args
emitPrimOp res IndexOffAddrOp_Int16     args _ = doIndexOffAddrOp (Just mo_s_16ToWord) b16 res args
emitPrimOp res IndexOffAddrOp_Int32     args _ = doIndexOffAddrOp (Just mo_s_32ToWord) b32 res args
emitPrimOp res IndexOffAddrOp_Int64     args _ = doIndexOffAddrOp Nothing b64 res args
emitPrimOp res IndexOffAddrOp_Word8     args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8   res args
emitPrimOp res IndexOffAddrOp_Word16    args _ = doIndexOffAddrOp (Just mo_u_16ToWord) b16 res args
emitPrimOp res IndexOffAddrOp_Word32    args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
emitPrimOp res IndexOffAddrOp_Word64    args _ = doIndexOffAddrOp Nothing b64 res args
311 312 313

-- ReadXXXoffAddr, which are identical, for our purposes, to IndexXXXoffAddr.

Ian Lynagh's avatar
Ian Lynagh committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
emitPrimOp res ReadOffAddrOp_Char      args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8 res args
emitPrimOp res ReadOffAddrOp_WideChar  args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
emitPrimOp res ReadOffAddrOp_Int       args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res ReadOffAddrOp_Word      args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res ReadOffAddrOp_Addr      args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res ReadOffAddrOp_Float     args _ = doIndexOffAddrOp Nothing f32 res args
emitPrimOp res ReadOffAddrOp_Double    args _ = doIndexOffAddrOp Nothing f64 res args
emitPrimOp res ReadOffAddrOp_StablePtr args _ = doIndexOffAddrOp Nothing bWord res args
emitPrimOp res ReadOffAddrOp_Int8      args _ = doIndexOffAddrOp (Just mo_s_8ToWord) b8  res args
emitPrimOp res ReadOffAddrOp_Int16     args _ = doIndexOffAddrOp (Just mo_s_16ToWord) b16 res args
emitPrimOp res ReadOffAddrOp_Int32     args _ = doIndexOffAddrOp (Just mo_s_32ToWord) b32 res args
emitPrimOp res ReadOffAddrOp_Int64     args _ = doIndexOffAddrOp Nothing b64 res args
emitPrimOp res ReadOffAddrOp_Word8     args _ = doIndexOffAddrOp (Just mo_u_8ToWord) b8  res args
emitPrimOp res ReadOffAddrOp_Word16    args _ = doIndexOffAddrOp (Just mo_u_16ToWord) b16 res args
emitPrimOp res ReadOffAddrOp_Word32    args _ = doIndexOffAddrOp (Just mo_u_32ToWord) b32 res args
emitPrimOp res ReadOffAddrOp_Word64    args _ = doIndexOffAddrOp Nothing b64 res args
330 331 332

-- IndexXXXArray

Ian Lynagh's avatar
Ian Lynagh committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
emitPrimOp res IndexByteArrayOp_Char      args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8 res args
emitPrimOp res IndexByteArrayOp_WideChar  args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32 res args
emitPrimOp res IndexByteArrayOp_Int       args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res IndexByteArrayOp_Word      args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res IndexByteArrayOp_Addr      args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res IndexByteArrayOp_Float     args _ = doIndexByteArrayOp Nothing f32 res args
emitPrimOp res IndexByteArrayOp_Double    args _ = doIndexByteArrayOp Nothing f64 res args
emitPrimOp res IndexByteArrayOp_StablePtr args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res IndexByteArrayOp_Int8      args _ = doIndexByteArrayOp (Just mo_s_8ToWord) b8  res args
emitPrimOp res IndexByteArrayOp_Int16     args _ = doIndexByteArrayOp (Just mo_s_16ToWord) b16  res args
emitPrimOp res IndexByteArrayOp_Int32     args _ = doIndexByteArrayOp (Just mo_s_32ToWord) b32  res args
emitPrimOp res IndexByteArrayOp_Int64     args _ = doIndexByteArrayOp Nothing b64  res args
emitPrimOp res IndexByteArrayOp_Word8     args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8  res args
emitPrimOp res IndexByteArrayOp_Word16    args _ = doIndexByteArrayOp (Just mo_u_16ToWord) b16  res args
emitPrimOp res IndexByteArrayOp_Word32    args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32  res args
emitPrimOp res IndexByteArrayOp_Word64    args _ = doIndexByteArrayOp Nothing b64  res args
349 350 351

-- ReadXXXArray, identical to IndexXXXArray.

Ian Lynagh's avatar
Ian Lynagh committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
emitPrimOp res ReadByteArrayOp_Char       args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8 res args
emitPrimOp res ReadByteArrayOp_WideChar   args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32 res args
emitPrimOp res ReadByteArrayOp_Int        args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res ReadByteArrayOp_Word       args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res ReadByteArrayOp_Addr       args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res ReadByteArrayOp_Float      args _ = doIndexByteArrayOp Nothing f32 res args
emitPrimOp res ReadByteArrayOp_Double     args _ = doIndexByteArrayOp Nothing f64 res args
emitPrimOp res ReadByteArrayOp_StablePtr  args _ = doIndexByteArrayOp Nothing bWord res args
emitPrimOp res ReadByteArrayOp_Int8       args _ = doIndexByteArrayOp (Just mo_s_8ToWord) b8  res args
emitPrimOp res ReadByteArrayOp_Int16      args _ = doIndexByteArrayOp (Just mo_s_16ToWord) b16  res args
emitPrimOp res ReadByteArrayOp_Int32      args _ = doIndexByteArrayOp (Just mo_s_32ToWord) b32  res args
emitPrimOp res ReadByteArrayOp_Int64      args _ = doIndexByteArrayOp Nothing b64  res args
emitPrimOp res ReadByteArrayOp_Word8      args _ = doIndexByteArrayOp (Just mo_u_8ToWord) b8  res args
emitPrimOp res ReadByteArrayOp_Word16     args _ = doIndexByteArrayOp (Just mo_u_16ToWord) b16  res args
emitPrimOp res ReadByteArrayOp_Word32     args _ = doIndexByteArrayOp (Just mo_u_32ToWord) b32  res args
emitPrimOp res ReadByteArrayOp_Word64     args _ = doIndexByteArrayOp Nothing b64  res args
368 369 370

-- WriteXXXoffAddr

Ian Lynagh's avatar
Ian Lynagh committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
emitPrimOp res WriteOffAddrOp_Char       args _ = doWriteOffAddrOp (Just mo_WordTo8) b8 res args
emitPrimOp res WriteOffAddrOp_WideChar   args _ = doWriteOffAddrOp (Just mo_WordTo32) b32 res args
emitPrimOp res WriteOffAddrOp_Int        args _ = doWriteOffAddrOp Nothing bWord res args
emitPrimOp res WriteOffAddrOp_Word       args _ = doWriteOffAddrOp Nothing bWord res args
emitPrimOp res WriteOffAddrOp_Addr       args _ = doWriteOffAddrOp Nothing bWord res args
emitPrimOp res WriteOffAddrOp_Float      args _ = doWriteOffAddrOp Nothing f32 res args
emitPrimOp res WriteOffAddrOp_Double     args _ = doWriteOffAddrOp Nothing f64 res args
emitPrimOp res WriteOffAddrOp_StablePtr  args _ = doWriteOffAddrOp Nothing bWord res args
emitPrimOp res WriteOffAddrOp_Int8       args _ = doWriteOffAddrOp (Just mo_WordTo8) b8  res args
emitPrimOp res WriteOffAddrOp_Int16      args _ = doWriteOffAddrOp (Just mo_WordTo16) b16 res args
emitPrimOp res WriteOffAddrOp_Int32      args _ = doWriteOffAddrOp (Just mo_WordTo32) b32 res args
emitPrimOp res WriteOffAddrOp_Int64      args _ = doWriteOffAddrOp Nothing b64 res args
emitPrimOp res WriteOffAddrOp_Word8      args _ = doWriteOffAddrOp (Just mo_WordTo8) b8  res args
emitPrimOp res WriteOffAddrOp_Word16     args _ = doWriteOffAddrOp (Just mo_WordTo16) b16 res args
emitPrimOp res WriteOffAddrOp_Word32     args _ = doWriteOffAddrOp (Just mo_WordTo32) b32 res args
emitPrimOp res WriteOffAddrOp_Word64     args _ = doWriteOffAddrOp Nothing b64 res args
387 388 389

-- WriteXXXArray

Ian Lynagh's avatar
Ian Lynagh committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
emitPrimOp res WriteByteArrayOp_Char      args _ = doWriteByteArrayOp (Just mo_WordTo8) b8 res args
emitPrimOp res WriteByteArrayOp_WideChar  args _ = doWriteByteArrayOp (Just mo_WordTo32) b32 res args
emitPrimOp res WriteByteArrayOp_Int       args _ = doWriteByteArrayOp Nothing bWord res args
emitPrimOp res WriteByteArrayOp_Word      args _ = doWriteByteArrayOp Nothing bWord res args
emitPrimOp res WriteByteArrayOp_Addr      args _ = doWriteByteArrayOp Nothing bWord res args
emitPrimOp res WriteByteArrayOp_Float     args _ = doWriteByteArrayOp Nothing f32 res args
emitPrimOp res WriteByteArrayOp_Double    args _ = doWriteByteArrayOp Nothing f64 res args
emitPrimOp res WriteByteArrayOp_StablePtr args _ = doWriteByteArrayOp Nothing bWord res args
emitPrimOp res WriteByteArrayOp_Int8      args _ = doWriteByteArrayOp (Just mo_WordTo8) b8  res args
emitPrimOp res WriteByteArrayOp_Int16     args _ = doWriteByteArrayOp (Just mo_WordTo16) b16  res args
emitPrimOp res WriteByteArrayOp_Int32     args _ = doWriteByteArrayOp (Just mo_WordTo32) b32  res args
emitPrimOp res WriteByteArrayOp_Int64     args _ = doWriteByteArrayOp Nothing b64  res args
emitPrimOp res WriteByteArrayOp_Word8     args _ = doWriteByteArrayOp (Just mo_WordTo8) b8  res args
emitPrimOp res WriteByteArrayOp_Word16    args _ = doWriteByteArrayOp (Just mo_WordTo16) b16  res args
emitPrimOp res WriteByteArrayOp_Word32    args _ = doWriteByteArrayOp (Just mo_WordTo32) b32  res args
emitPrimOp res WriteByteArrayOp_Word64    args _ = doWriteByteArrayOp Nothing b64  res args
406

tibbe's avatar
tibbe committed
407 408 409 410 411 412 413
-- Copying byte arrays

emitPrimOp [] CopyByteArrayOp [src,src_off,dst,dst_off,n] live =
    doCopyByteArrayOp src src_off dst dst_off n live
emitPrimOp [] CopyMutableByteArrayOp [src,src_off,dst,dst_off,n] live =
    doCopyMutableByteArrayOp src src_off dst dst_off n live

tibbe's avatar
tibbe committed
414 415 416 417 418 419
-- Population count
emitPrimOp [res] PopCnt8Op [w] live = emitPopCntCall res w W8 live
emitPrimOp [res] PopCnt16Op [w] live = emitPopCntCall res w W16 live
emitPrimOp [res] PopCnt32Op [w] live = emitPopCntCall res w W32 live
emitPrimOp [res] PopCnt64Op [w] live = emitPopCntCall res w W64 live
emitPrimOp [res] PopCntOp [w] live = emitPopCntCall res w wordWidth live
420 421

-- The rest just translate straightforwardly
Ian Lynagh's avatar
Ian Lynagh committed
422
emitPrimOp [res] op [arg] _
423
   | nopOp op
424
   = stmtC (CmmAssign (CmmLocal res) arg)
425 426

   | Just (mop,rep) <- narrowOp op
427
   = stmtC (CmmAssign (CmmLocal res) $
dterei's avatar
dterei committed
428
            CmmMachOp (mop rep wordWidth) [CmmMachOp (mop wordWidth rep) [arg]])
429 430 431 432

emitPrimOp [res] op args live
   | Just prim <- callishOp op
   = do vols <- getVolatileRegs live
dterei's avatar
dterei committed
433 434
        emitForeignCall' PlayRisky
           [CmmHinted res NoHint]
Ian Lynagh's avatar
Ian Lynagh committed
435
           (CmmPrim prim Nothing)
dterei's avatar
dterei committed
436 437
           [CmmHinted a NoHint | a<-args]  -- ToDo: hints?
           (Just vols)
438
           NoC_SRT -- No SRT b/c we do PlayRisky
439
           CmmMayReturn
440 441

   | Just mop <- translateOp op
442
   = let stmt = CmmAssign (CmmLocal res) (CmmMachOp mop args) in
443 444
     stmtC stmt

445
emitPrimOp [res_q, res_r] IntQuotRemOp [arg_x, arg_y] _
Ian Lynagh's avatar
Ian Lynagh committed
446 447 448 449 450 451 452 453
    = let genericImpl [CmmHinted res_q _, CmmHinted res_r _]
                      [CmmHinted arg_x _, CmmHinted arg_y _]
              = [CmmAssign (CmmLocal res_q)
                           (CmmMachOp (MO_S_Quot wordWidth) [arg_x, arg_y]),
                 CmmAssign (CmmLocal res_r)
                           (CmmMachOp (MO_S_Rem  wordWidth) [arg_x, arg_y])]
          genericImpl _ _ = panic "emitPrimOp IntQuotRemOp generic: bad lengths"
          stmt = CmmCall (CmmPrim (MO_S_QuotRem wordWidth) (Just genericImpl))
454 455 456 457 458 459
                         [CmmHinted res_q NoHint,
                          CmmHinted res_r NoHint]
                         [CmmHinted arg_x NoHint,
                          CmmHinted arg_y NoHint]
                         CmmMayReturn
      in stmtC stmt
460
emitPrimOp [res_q, res_r] WordQuotRemOp [arg_x, arg_y] _
Ian Lynagh's avatar
Ian Lynagh committed
461 462 463 464 465 466 467 468
    = let genericImpl [CmmHinted res_q _, CmmHinted res_r _]
                      [CmmHinted arg_x _, CmmHinted arg_y _]
              = [CmmAssign (CmmLocal res_q)
                           (CmmMachOp (MO_U_Quot wordWidth) [arg_x, arg_y]),
                 CmmAssign (CmmLocal res_r)
                           (CmmMachOp (MO_U_Rem  wordWidth) [arg_x, arg_y])]
          genericImpl _ _ = panic "emitPrimOp WordQuotRemOp generic: bad lengths"
          stmt = CmmCall (CmmPrim (MO_U_QuotRem wordWidth) (Just genericImpl))
469 470 471 472 473 474
                         [CmmHinted res_q NoHint,
                          CmmHinted res_r NoHint]
                         [CmmHinted arg_x NoHint,
                          CmmHinted arg_y NoHint]
                         CmmMayReturn
      in stmtC stmt
Ian Lynagh's avatar
Ian Lynagh committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
emitPrimOp [res_h, res_l] WordAdd2Op [arg_x, arg_y] _
 = do r1 <- newLocalReg (cmmExprType arg_x)
      r2 <- newLocalReg (cmmExprType arg_x)
      -- This generic implementation is very simple and slow. We might
      -- well be able to do better, but for now this at least works.
      let genericImpl [CmmHinted res_h _, CmmHinted res_l _]
                      [CmmHinted arg_x _, CmmHinted arg_y _]
           = [CmmAssign (CmmLocal r1)
                  (add (bottomHalf arg_x) (bottomHalf arg_y)),
              CmmAssign (CmmLocal r2)
                  (add (topHalf (CmmReg (CmmLocal r1)))
                       (add (topHalf arg_x) (topHalf arg_y))),
              CmmAssign (CmmLocal res_h)
                  (topHalf (CmmReg (CmmLocal r2))),
              CmmAssign (CmmLocal res_l)
                  (or (toTopHalf (CmmReg (CmmLocal r2)))
                      (bottomHalf (CmmReg (CmmLocal r1))))]
               where topHalf x = CmmMachOp (MO_U_Shr wordWidth) [x, hww]
                     toTopHalf x = CmmMachOp (MO_Shl wordWidth) [x, hww]
                     bottomHalf x = CmmMachOp (MO_And wordWidth) [x, hwm]
                     add x y = CmmMachOp (MO_Add wordWidth) [x, y]
                     or x y = CmmMachOp (MO_Or wordWidth) [x, y]
                     hww = CmmLit (CmmInt (fromIntegral (widthInBits halfWordWidth))
                                          wordWidth)
                     hwm = CmmLit (CmmInt halfWordMask wordWidth)
          genericImpl _ _ = panic "emitPrimOp WordAdd2Op generic: bad lengths"
          stmt = CmmCall (CmmPrim (MO_Add2 wordWidth) (Just genericImpl))
                         [CmmHinted res_h NoHint,
                          CmmHinted res_l NoHint]
                         [CmmHinted arg_x NoHint,
                          CmmHinted arg_y NoHint]
                         CmmMayReturn
      stmtC stmt
Ian Lynagh's avatar
Ian Lynagh committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
emitPrimOp [res_h, res_l] WordMul2Op [arg_x, arg_y] _
 = do let t = cmmExprType arg_x
      xlyl <- liftM CmmLocal $ newLocalReg t
      xlyh <- liftM CmmLocal $ newLocalReg t
      xhyl <- liftM CmmLocal $ newLocalReg t
      r    <- liftM CmmLocal $ newLocalReg t
      -- This generic implementation is very simple and slow. We might
      -- well be able to do better, but for now this at least works.
      let genericImpl [CmmHinted res_h _, CmmHinted res_l _]
                      [CmmHinted arg_x _, CmmHinted arg_y _]
           = [CmmAssign xlyl
                  (mul (bottomHalf arg_x) (bottomHalf arg_y)),
              CmmAssign xlyh
                  (mul (bottomHalf arg_x) (topHalf arg_y)),
              CmmAssign xhyl
                  (mul (topHalf arg_x) (bottomHalf arg_y)),
              CmmAssign r
                  (sum [topHalf    (CmmReg xlyl),
                        bottomHalf (CmmReg xhyl),
                        bottomHalf (CmmReg xlyh)]),
              CmmAssign (CmmLocal res_l)
                  (or (bottomHalf (CmmReg xlyl))
                      (toTopHalf (CmmReg r))),
              CmmAssign (CmmLocal res_h)
                  (sum [mul (topHalf arg_x) (topHalf arg_y),
                        bottomHalf (CmmReg xhyl),
                        bottomHalf (CmmReg xlyh),
                        topHalf    (CmmReg r)])]
               where topHalf x = CmmMachOp (MO_U_Shr wordWidth) [x, hww]
                     toTopHalf x = CmmMachOp (MO_Shl wordWidth) [x, hww]
                     bottomHalf x = CmmMachOp (MO_And wordWidth) [x, hwm]
                     add x y = CmmMachOp (MO_Add wordWidth) [x, y]
                     sum = foldl1 add
                     mul x y = CmmMachOp (MO_Mul wordWidth) [x, y]
                     or x y = CmmMachOp (MO_Or wordWidth) [x, y]
                     hww = CmmLit (CmmInt (fromIntegral (widthInBits halfWordWidth))
                                          wordWidth)
                     hwm = CmmLit (CmmInt halfWordMask wordWidth)
          genericImpl _ _ = panic "emitPrimOp WordMul2Op generic: bad lengths"
          stmt = CmmCall (CmmPrim (MO_U_Mul2 wordWidth) (Just genericImpl))
                         [CmmHinted res_h NoHint,
                          CmmHinted res_l NoHint]
                         [CmmHinted arg_x NoHint,
                          CmmHinted arg_y NoHint]
                         CmmMayReturn
      stmtC stmt
554

555 556 557
emitPrimOp _ op _ _
 = pprPanic "emitPrimOp: can't translate PrimOp" (ppr op)

Ian Lynagh's avatar
Ian Lynagh committed
558 559 560
newLocalReg :: CmmType -> FCode LocalReg
newLocalReg t = do u <- newUnique
                   return $ LocalReg u t
561 562 563

-- These PrimOps are NOPs in Cmm

Ian Lynagh's avatar
Ian Lynagh committed
564
nopOp :: PrimOp -> Bool
565 566 567 568
nopOp Int2WordOp     = True
nopOp Word2IntOp     = True
nopOp Int2AddrOp     = True
nopOp Addr2IntOp     = True
dterei's avatar
dterei committed
569 570 571
nopOp ChrOp          = True  -- Int# and Char# are rep'd the same
nopOp OrdOp          = True
nopOp _              = False
572 573 574

-- These PrimOps turn into double casts

575 576 577 578 579 580 581
narrowOp :: PrimOp -> Maybe (Width -> Width -> MachOp, Width)
narrowOp Narrow8IntOp   = Just (MO_SS_Conv, W8)
narrowOp Narrow16IntOp  = Just (MO_SS_Conv, W16)
narrowOp Narrow32IntOp  = Just (MO_SS_Conv, W32)
narrowOp Narrow8WordOp  = Just (MO_UU_Conv, W8)
narrowOp Narrow16WordOp = Just (MO_UU_Conv, W16)
narrowOp Narrow32WordOp = Just (MO_UU_Conv, W32)
dterei's avatar
dterei committed
582
narrowOp _              = Nothing
583 584 585

-- Native word signless ops

Ian Lynagh's avatar
Ian Lynagh committed
586
translateOp :: PrimOp -> Maybe MachOp
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
translateOp IntAddOp       = Just mo_wordAdd
translateOp IntSubOp       = Just mo_wordSub
translateOp WordAddOp      = Just mo_wordAdd
translateOp WordSubOp      = Just mo_wordSub
translateOp AddrAddOp      = Just mo_wordAdd
translateOp AddrSubOp      = Just mo_wordSub

translateOp IntEqOp        = Just mo_wordEq
translateOp IntNeOp        = Just mo_wordNe
translateOp WordEqOp       = Just mo_wordEq
translateOp WordNeOp       = Just mo_wordNe
translateOp AddrEqOp       = Just mo_wordEq
translateOp AddrNeOp       = Just mo_wordNe

translateOp AndOp          = Just mo_wordAnd
translateOp OrOp           = Just mo_wordOr
translateOp XorOp          = Just mo_wordXor
translateOp NotOp          = Just mo_wordNot
dterei's avatar
dterei committed
605 606
translateOp SllOp          = Just mo_wordShl
translateOp SrlOp          = Just mo_wordUShr
607

dterei's avatar
dterei committed
608
translateOp AddrRemOp      = Just mo_wordURem
609 610 611 612

-- Native word signed ops

translateOp IntMulOp        = Just mo_wordMul
613
translateOp IntMulMayOfloOp = Just (MO_S_MulMayOflo wordWidth)
614 615 616 617 618 619 620 621 622 623
translateOp IntQuotOp       = Just mo_wordSQuot
translateOp IntRemOp        = Just mo_wordSRem
translateOp IntNegOp        = Just mo_wordSNeg


translateOp IntGeOp        = Just mo_wordSGe
translateOp IntLeOp        = Just mo_wordSLe
translateOp IntGtOp        = Just mo_wordSGt
translateOp IntLtOp        = Just mo_wordSLt

dterei's avatar
dterei committed
624 625 626
translateOp ISllOp         = Just mo_wordShl
translateOp ISraOp         = Just mo_wordSShr
translateOp ISrlOp         = Just mo_wordUShr
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

-- Native word unsigned ops

translateOp WordGeOp       = Just mo_wordUGe
translateOp WordLeOp       = Just mo_wordULe
translateOp WordGtOp       = Just mo_wordUGt
translateOp WordLtOp       = Just mo_wordULt

translateOp WordMulOp      = Just mo_wordMul
translateOp WordQuotOp     = Just mo_wordUQuot
translateOp WordRemOp      = Just mo_wordURem

translateOp AddrGeOp       = Just mo_wordUGe
translateOp AddrLeOp       = Just mo_wordULe
translateOp AddrGtOp       = Just mo_wordUGt
translateOp AddrLtOp       = Just mo_wordULt

644
-- Char# ops
645

646 647 648 649 650 651
translateOp CharEqOp       = Just (MO_Eq wordWidth)
translateOp CharNeOp       = Just (MO_Ne wordWidth)
translateOp CharGeOp       = Just (MO_U_Ge wordWidth)
translateOp CharLeOp       = Just (MO_U_Le wordWidth)
translateOp CharGtOp       = Just (MO_U_Gt wordWidth)
translateOp CharLtOp       = Just (MO_U_Lt wordWidth)
652 653 654

-- Double ops

655 656 657 658 659 660
translateOp DoubleEqOp     = Just (MO_F_Eq W64)
translateOp DoubleNeOp     = Just (MO_F_Ne W64)
translateOp DoubleGeOp     = Just (MO_F_Ge W64)
translateOp DoubleLeOp     = Just (MO_F_Le W64)
translateOp DoubleGtOp     = Just (MO_F_Gt W64)
translateOp DoubleLtOp     = Just (MO_F_Lt W64)
661

662 663 664 665 666
translateOp DoubleAddOp    = Just (MO_F_Add W64)
translateOp DoubleSubOp    = Just (MO_F_Sub W64)
translateOp DoubleMulOp    = Just (MO_F_Mul W64)
translateOp DoubleDivOp    = Just (MO_F_Quot W64)
translateOp DoubleNegOp    = Just (MO_F_Neg W64)
667 668 669

-- Float ops

670 671 672 673 674 675
translateOp FloatEqOp     = Just (MO_F_Eq W32)
translateOp FloatNeOp     = Just (MO_F_Ne W32)
translateOp FloatGeOp     = Just (MO_F_Ge W32)
translateOp FloatLeOp     = Just (MO_F_Le W32)
translateOp FloatGtOp     = Just (MO_F_Gt W32)
translateOp FloatLtOp     = Just (MO_F_Lt W32)
676

677 678 679 680 681
translateOp FloatAddOp    = Just (MO_F_Add  W32)
translateOp FloatSubOp    = Just (MO_F_Sub  W32)
translateOp FloatMulOp    = Just (MO_F_Mul  W32)
translateOp FloatDivOp    = Just (MO_F_Quot W32)
translateOp FloatNegOp    = Just (MO_F_Neg  W32)
682 683 684

-- Conversions

685 686
translateOp Int2DoubleOp   = Just (MO_SF_Conv wordWidth W64)
translateOp Double2IntOp   = Just (MO_FS_Conv W64 wordWidth)
687

688 689
translateOp Int2FloatOp    = Just (MO_SF_Conv wordWidth W32)
translateOp Float2IntOp    = Just (MO_FS_Conv W32 wordWidth)
690

691 692
translateOp Float2DoubleOp = Just (MO_FF_Conv W32 W64)
translateOp Double2FloatOp = Just (MO_FF_Conv W64 W32)
693 694 695 696 697 698 699

-- Word comparisons masquerading as more exotic things.

translateOp SameMutVarOp           = Just mo_wordEq
translateOp SameMVarOp             = Just mo_wordEq
translateOp SameMutableArrayOp     = Just mo_wordEq
translateOp SameMutableByteArrayOp = Just mo_wordEq
700
translateOp SameMutableArrayArrayOp= Just mo_wordEq
701
translateOp SameTVarOp             = Just mo_wordEq
702 703 704 705 706 707 708
translateOp EqStablePtrOp          = Just mo_wordEq

translateOp _ = Nothing

-- These primops are implemented by CallishMachOps, because they sometimes
-- turn into foreign calls depending on the backend.

Ian Lynagh's avatar
Ian Lynagh committed
709
callishOp :: PrimOp -> Maybe CallishMachOp
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
callishOp DoublePowerOp  = Just MO_F64_Pwr
callishOp DoubleSinOp    = Just MO_F64_Sin
callishOp DoubleCosOp    = Just MO_F64_Cos
callishOp DoubleTanOp    = Just MO_F64_Tan
callishOp DoubleSinhOp   = Just MO_F64_Sinh
callishOp DoubleCoshOp   = Just MO_F64_Cosh
callishOp DoubleTanhOp   = Just MO_F64_Tanh
callishOp DoubleAsinOp   = Just MO_F64_Asin
callishOp DoubleAcosOp   = Just MO_F64_Acos
callishOp DoubleAtanOp   = Just MO_F64_Atan
callishOp DoubleLogOp    = Just MO_F64_Log
callishOp DoubleExpOp    = Just MO_F64_Exp
callishOp DoubleSqrtOp   = Just MO_F64_Sqrt

callishOp FloatPowerOp  = Just MO_F32_Pwr
callishOp FloatSinOp    = Just MO_F32_Sin
callishOp FloatCosOp    = Just MO_F32_Cos
callishOp FloatTanOp    = Just MO_F32_Tan
callishOp FloatSinhOp   = Just MO_F32_Sinh
callishOp FloatCoshOp   = Just MO_F32_Cosh
callishOp FloatTanhOp   = Just MO_F32_Tanh
callishOp FloatAsinOp   = Just MO_F32_Asin
callishOp FloatAcosOp   = Just MO_F32_Acos
callishOp FloatAtanOp   = Just MO_F32_Atan
callishOp FloatLogOp    = Just MO_F32_Log
callishOp FloatExpOp    = Just MO_F32_Exp
callishOp FloatSqrtOp   = Just MO_F32_Sqrt

callishOp _ = Nothing

------------------------------------------------------------------------------
-- Helpers for translating various minor variants of array indexing.

743
-- Bytearrays outside the heap; hence non-pointers
dterei's avatar
dterei committed
744 745 746
doIndexOffAddrOp, doIndexByteArrayOp
        :: Maybe MachOp -> CmmType
        -> [LocalReg] -> [CmmExpr] -> Code
747 748 749 750 751 752 753
doIndexOffAddrOp maybe_post_read_cast rep [res] [addr,idx]
   = mkBasicIndexedRead 0 maybe_post_read_cast rep res addr idx
doIndexOffAddrOp _ _ _ _
   = panic "CgPrimOp: doIndexOffAddrOp"

doIndexByteArrayOp maybe_post_read_cast rep [res] [addr,idx]
   = mkBasicIndexedRead arrWordsHdrSize maybe_post_read_cast rep res addr idx
dterei's avatar
dterei committed
754
doIndexByteArrayOp _ _ _ _
755 756
   = panic "CgPrimOp: doIndexByteArrayOp"

757
doReadPtrArrayOp :: LocalReg -> CmmExpr -> CmmExpr -> Code
758
doReadPtrArrayOp res addr idx
759
   = mkBasicIndexedRead arrPtrsHdrSize Nothing gcWord res addr idx
760 761


dterei's avatar
dterei committed
762 763 764
doWriteOffAddrOp, doWriteByteArrayOp
        :: Maybe MachOp -> CmmType
        -> [LocalReg] -> [CmmExpr] -> Code
765 766 767 768 769 770 771
doWriteOffAddrOp maybe_pre_write_cast rep [] [addr,idx,val]
   = mkBasicIndexedWrite 0 maybe_pre_write_cast rep addr idx val
doWriteOffAddrOp _ _ _ _
   = panic "CgPrimOp: doWriteOffAddrOp"

doWriteByteArrayOp maybe_pre_write_cast rep [] [addr,idx,val]
   = mkBasicIndexedWrite arrWordsHdrSize maybe_pre_write_cast rep addr idx val
dterei's avatar
dterei committed
772
doWriteByteArrayOp _ _ _ _
773 774
   = panic "CgPrimOp: doWriteByteArrayOp"

775
doWritePtrArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> Code
776
doWritePtrArrayOp addr idx val
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
   = do mkBasicIndexedWrite arrPtrsHdrSize Nothing bWord addr idx val
        stmtC (setInfo addr (CmmLit (CmmLabel mkMAP_DIRTY_infoLabel)))
   -- the write barrier.  We must write a byte into the mark table:
   -- bits8[a + header_size + StgMutArrPtrs_size(a) + x >> N]
        stmtC $ CmmStore (
          cmmOffsetExpr
           (cmmOffsetExprW (cmmOffsetB addr arrPtrsHdrSize)
                          (loadArrPtrsSize addr))
           (CmmMachOp mo_wordUShr [idx,
                                   CmmLit (mkIntCLit mUT_ARR_PTRS_CARD_BITS)])
          ) (CmmLit (CmmInt 1 W8))

loadArrPtrsSize :: CmmExpr -> CmmExpr
loadArrPtrsSize addr = CmmLoad (cmmOffsetB addr off) bWord
 where off = fixedHdrSize*wORD_SIZE + oFFSET_StgMutArrPtrs_ptrs
792

dterei's avatar
dterei committed
793 794
mkBasicIndexedRead :: ByteOff -> Maybe MachOp -> CmmType
                   -> LocalReg -> CmmExpr -> CmmExpr -> Code
795
mkBasicIndexedRead off Nothing read_rep res base idx
796
   = stmtC (CmmAssign (CmmLocal res) (cmmLoadIndexOffExpr off read_rep base idx))
797
mkBasicIndexedRead off (Just cast) read_rep res base idx
798
   = stmtC (CmmAssign (CmmLocal res) (CmmMachOp cast [
dterei's avatar
dterei committed
799
                                cmmLoadIndexOffExpr off read_rep base idx]))
800

dterei's avatar
dterei committed
801 802
mkBasicIndexedWrite :: ByteOff -> Maybe MachOp -> CmmType
                    -> CmmExpr -> CmmExpr -> CmmExpr -> Code
803 804 805 806 807 808 809 810
mkBasicIndexedWrite off Nothing write_rep base idx val
   = stmtC (CmmStore (cmmIndexOffExpr off write_rep base idx) val)
mkBasicIndexedWrite off (Just cast) write_rep base idx val
   = stmtC (CmmStore (cmmIndexOffExpr off write_rep base idx) (CmmMachOp cast [val]))

-- ----------------------------------------------------------------------------
-- Misc utils

811
cmmIndexOffExpr :: ByteOff -> CmmType -> CmmExpr -> CmmExpr -> CmmExpr
812
cmmIndexOffExpr off rep base idx
813
   = cmmIndexExpr (typeWidth rep) (cmmOffsetB base off) idx
814

815
cmmLoadIndexOffExpr :: ByteOff -> CmmType -> CmmExpr -> CmmExpr -> CmmExpr
816 817 818 819 820 821
cmmLoadIndexOffExpr off rep base idx
   = CmmLoad (cmmIndexOffExpr off rep base idx) rep

setInfo :: CmmExpr -> CmmExpr -> CmmStmt
setInfo closure_ptr info_ptr = CmmStore closure_ptr info_ptr

tibbe's avatar
tibbe committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
-- ----------------------------------------------------------------------------
-- Copying byte arrays

-- | Takes a source 'ByteArray#', an offset in the source array, a
-- destination 'MutableByteArray#', an offset into the destination
-- array, and the number of bytes to copy.  Copies the given number of
-- bytes from the source array to the destination array.
doCopyByteArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
                  -> StgLiveVars -> Code
doCopyByteArrayOp = emitCopyByteArray copy
  where
    -- Copy data (we assume the arrays aren't overlapping since
    -- they're of different types)
    copy _src _dst dst_p src_p bytes live =
        emitMemcpyCall dst_p src_p bytes (CmmLit (mkIntCLit 1)) live

-- | Takes a source 'MutableByteArray#', an offset in the source
-- array, a destination 'MutableByteArray#', an offset into the
-- destination array, and the number of bytes to copy.  Copies the
-- given number of bytes from the source array to the destination
-- array.
doCopyMutableByteArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
                         -> StgLiveVars -> Code
doCopyMutableByteArrayOp = emitCopyByteArray copy
  where
    -- The only time the memory might overlap is when the two arrays
    -- we were provided are the same array!
    -- TODO: Optimize branch for common case of no aliasing.
    copy src dst dst_p src_p bytes live =
        emitIfThenElse (cmmEqWord src dst)
        (emitMemmoveCall dst_p src_p bytes (CmmLit (mkIntCLit 1)) live)
        (emitMemcpyCall dst_p src_p bytes (CmmLit (mkIntCLit 1)) live)

emitCopyByteArray :: (CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
                  -> StgLiveVars -> Code)
                  -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
                  -> StgLiveVars
                  -> Code
860
emitCopyByteArray copy src src_off dst dst_off n live = do
tibbe's avatar
tibbe committed
861 862 863 864
    dst_p <- assignTemp $ cmmOffsetExpr (cmmOffsetB dst arrWordsHdrSize) dst_off
    src_p <- assignTemp $ cmmOffsetExpr (cmmOffsetB src arrWordsHdrSize) src_off
    copy src dst dst_p src_p n live

tibbe's avatar
tibbe committed
865 866 867
-- ----------------------------------------------------------------------------
-- Copying pointer arrays

868 869 870 871 872 873 874
-- EZY: This code has an unusually high amount of assignTemp calls, seen
-- nowhere else in the code generator.  This is mostly because these
-- "primitive" ops result in a surprisingly large amount of code.  It
-- will likely be worthwhile to optimize what is emitted here, so that
-- our optimization passes don't waste time repeatedly optimizing the
-- same bits of code.

tibbe's avatar
tibbe committed
875 876 877 878 879 880 881 882 883 884
-- | Takes a source 'Array#', an offset in the source array, a
-- destination 'MutableArray#', an offset into the destination array,
-- and the number of elements to copy.  Copies the given number of
-- elements from the source array to the destination array.
doCopyArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
              -> StgLiveVars -> Code
doCopyArrayOp = emitCopyArray copy
  where
    -- Copy data (we assume the arrays aren't overlapping since
    -- they're of different types)
885 886
    copy _src _dst dst_p src_p bytes live =
        emitMemcpyCall dst_p src_p bytes (CmmLit (mkIntCLit wORD_SIZE)) live
tibbe's avatar
tibbe committed
887 888 889 890 891 892 893 894 895 896 897 898 899 900

-- | Takes a source 'MutableArray#', an offset in the source array, a
-- destination 'MutableArray#', an offset into the destination array,
-- and the number of elements to copy.  Copies the given number of
-- elements from the source array to the destination array.
doCopyMutableArrayOp :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
                     -> StgLiveVars -> Code
doCopyMutableArrayOp = emitCopyArray copy
  where
    -- The only time the memory might overlap is when the two arrays
    -- we were provided are the same array!
    -- TODO: Optimize branch for common case of no aliasing.
    copy src dst dst_p src_p bytes live =
        emitIfThenElse (cmmEqWord src dst)
901 902
        (emitMemmoveCall dst_p src_p bytes (CmmLit (mkIntCLit wORD_SIZE)) live)
        (emitMemcpyCall dst_p src_p bytes (CmmLit (mkIntCLit wORD_SIZE)) live)
tibbe's avatar
tibbe committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957

emitCopyArray :: (CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
                  -> StgLiveVars -> Code)
              -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr
              -> StgLiveVars
              -> Code
emitCopyArray copy src0 src_off0 dst0 dst_off0 n0 live = do
    -- Assign the arguments to temporaries so the code generator can
    -- calculate liveness for us.
    src <- assignTemp_ src0
    src_off <- assignTemp_ src_off0
    dst <- assignTemp_ dst0
    dst_off <- assignTemp_ dst_off0
    n <- assignTemp_ n0

    -- Set the dirty bit in the header.
    stmtC (setInfo dst (CmmLit (CmmLabel mkMAP_DIRTY_infoLabel)))

    dst_elems_p <- assignTemp $ cmmOffsetB dst arrPtrsHdrSize
    dst_p <- assignTemp $ cmmOffsetExprW dst_elems_p dst_off
    src_p <- assignTemp $ cmmOffsetExprW (cmmOffsetB src arrPtrsHdrSize) src_off
    bytes <- assignTemp $ cmmMulWord n (CmmLit (mkIntCLit wORD_SIZE))

    copy src dst dst_p src_p bytes live

    -- The base address of the destination card table
    dst_cards_p <- assignTemp $ cmmOffsetExprW dst_elems_p (loadArrPtrsSize dst)

    emitSetCards dst_off dst_cards_p n live

-- | Takes an info table label, a register to return the newly
-- allocated array in, a source array, an offset in the source array,
-- and the number of elements to copy.  Allocates a new array and
-- initializes it form the source array.
emitCloneArray :: CLabel -> CmmFormal -> CmmExpr -> CmmExpr -> CmmExpr
               -> StgLiveVars -> Code
emitCloneArray info_p res_r src0 src_off0 n0 live = do
    -- Assign the arguments to temporaries so the code generator can
    -- calculate liveness for us.
    src <- assignTemp_ src0
    src_off <- assignTemp_ src_off0
    n <- assignTemp_ n0

    card_words <- assignTemp $ (n `cmmUShrWord`
                                (CmmLit (mkIntCLit mUT_ARR_PTRS_CARD_BITS)))
                  `cmmAddWord` CmmLit (mkIntCLit 1)
    size <- assignTemp $ n `cmmAddWord` card_words
    words <- assignTemp $ arrPtrsHdrSizeW `cmmAddWord` size

    arr_r <- newTemp bWord
    emitAllocateCall arr_r myCapability words live
    tickyAllocPrim (CmmLit (mkIntCLit arrPtrsHdrSize)) (n `cmmMulWord` wordSize)
        (CmmLit $ mkIntCLit 0)

    let arr = CmmReg (CmmLocal arr_r)
958
    emitSetDynHdr arr (CmmLit (CmmLabel info_p)) curCCS
tibbe's avatar
tibbe committed
959 960 961 962 963 964 965 966 967
    stmtC $ CmmStore (cmmOffsetB arr (fixedHdrSize * wORD_SIZE +
                                      oFFSET_StgMutArrPtrs_ptrs)) n
    stmtC $ CmmStore (cmmOffsetB arr (fixedHdrSize * wORD_SIZE +
                                      oFFSET_StgMutArrPtrs_size)) size

    dst_p <- assignTemp $ cmmOffsetB arr arrPtrsHdrSize
    src_p <- assignTemp $ cmmOffsetExprW (cmmOffsetB src arrPtrsHdrSize)
             src_off

968 969
    emitMemcpyCall dst_p src_p (n `cmmMulWord` wordSize)
        (CmmLit (mkIntCLit wORD_SIZE)) live
tibbe's avatar
tibbe committed
970 971

    emitMemsetCall (cmmOffsetExprW dst_p n)
972
        (CmmLit (mkIntCLit 1))
tibbe's avatar
tibbe committed
973
        (card_words `cmmMulWord` wordSize)
974
        (CmmLit (mkIntCLit wORD_SIZE))
tibbe's avatar
tibbe committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
        live
    stmtC $ CmmAssign (CmmLocal res_r) arr
  where
    arrPtrsHdrSizeW = CmmLit $ mkIntCLit $ fixedHdrSize +
                      (sIZEOF_StgMutArrPtrs_NoHdr `div` wORD_SIZE)
    wordSize = CmmLit (mkIntCLit wORD_SIZE)
    myCapability = CmmReg baseReg `cmmSubWord`
                   CmmLit (mkIntCLit oFFSET_Capability_r)

-- | Takes and offset in the destination array, the base address of
-- the card table, and the number of elements affected (*not* the
-- number of cards).  Marks the relevant cards as dirty.
emitSetCards :: CmmExpr -> CmmExpr -> CmmExpr -> StgLiveVars -> Code
emitSetCards dst_start dst_cards_start n live = do
    start_card <- assignTemp $ card dst_start
    emitMemsetCall (dst_cards_start `cmmAddWord` start_card)
991
        (CmmLit (mkIntCLit 1))
tibbe's avatar
tibbe committed
992 993
        ((card (dst_start `cmmAddWord` n) `cmmSubWord` start_card)
         `cmmAddWord` CmmLit (mkIntCLit 1))
994
        (CmmLit (mkIntCLit wORD_SIZE))
tibbe's avatar
tibbe committed
995 996 997 998 999 1000
        live
  where
    -- Convert an element index to a card index
    card i = i `cmmUShrWord` (CmmLit (mkIntCLit mUT_ARR_PTRS_CARD_BITS))

-- | Emit a call to @memcpy@.
1001 1002 1003
emitMemcpyCall :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> StgLiveVars
               -> Code
emitMemcpyCall dst src n align live = do
tibbe's avatar
tibbe committed
1004 1005 1006
    vols <- getVolatileRegs live
    emitForeignCall' PlayRisky
        [{-no results-}]
Ian Lynagh's avatar
Ian Lynagh committed
1007
        (CmmPrim MO_Memcpy Nothing)
tibbe's avatar
tibbe committed
1008 1009 1010
        [ (CmmHinted dst AddrHint)
        , (CmmHinted src AddrHint)
        , (CmmHinted n NoHint)
1011
        , (CmmHinted align NoHint)
tibbe's avatar
tibbe committed
1012 1013 1014 1015 1016 1017
        ]
        (Just vols)
        NoC_SRT -- No SRT b/c we do PlayRisky
        CmmMayReturn

-- | Emit a call to @memmove@.
1018 1019 1020
emitMemmoveCall :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> StgLiveVars
                -> Code
emitMemmoveCall dst src n align live = do
tibbe's avatar
tibbe committed
1021 1022 1023
    vols <- getVolatileRegs live
    emitForeignCall' PlayRisky
        [{-no results-}]
Ian Lynagh's avatar
Ian Lynagh committed
1024
        (CmmPrim MO_Memmove Nothing)
tibbe's avatar
tibbe committed
1025 1026 1027
        [ (CmmHinted dst AddrHint)
        , (CmmHinted src AddrHint)
        , (CmmHinted n NoHint)
1028
        , (CmmHinted align NoHint)
tibbe's avatar
tibbe committed
1029 1030 1031 1032 1033
        ]
        (Just vols)
        NoC_SRT -- No SRT b/c we do PlayRisky
        CmmMayReturn

1034 1035 1036 1037 1038
-- | Emit a call to @memset@.  The second argument must be a word but
-- its value must fit inside an unsigned char.
emitMemsetCall :: CmmExpr -> CmmExpr -> CmmExpr -> CmmExpr -> StgLiveVars
               -> Code
emitMemsetCall dst c n align live = do
tibbe's avatar
tibbe committed
1039 1040 1041
    vols <- getVolatileRegs live
    emitForeignCall' PlayRisky
        [{-no results-}]
Ian Lynagh's avatar
Ian Lynagh committed
1042
        (CmmPrim MO_Memset Nothing)
tibbe's avatar
tibbe committed
1043 1044 1045
        [ (CmmHinted dst AddrHint)
        , (CmmHinted c NoHint)
        , (CmmHinted n NoHint)
1046
        , (CmmHinted align NoHint)