CodeGen.hs 53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14

-----------------------------------------------------------------------------
--
-- Generating machine code (instruction selection)
--
-- (c) The University of Glasgow 1996-2004
--
-----------------------------------------------------------------------------

-- This is a big module, but, if you pay attention to
-- (a) the sectioning, (b) the type signatures, and
-- (c) the #if blah_TARGET_ARCH} things, the
-- structure should not be too overwhelming.

15 16 17 18 19
module PPC.CodeGen (
        cmmTopCodeGen,
        generateJumpTableForInstr,
        InstrBlock
)
20 21 22 23 24

where

#include "HsVersions.h"
#include "nativeGen/NCG.h"
Simon Marlow's avatar
Simon Marlow committed
25
#include "../includes/MachDeps.h"
26 27 28 29 30

-- NCG stuff:
import PPC.Instr
import PPC.Cond
import PPC.Regs
tibbe's avatar
tibbe committed
31
import CPrim
32 33 34 35 36 37
import NCGMonad
import Instruction
import PIC
import Size
import RegClass
import Reg
38
import TargetReg
39 40 41 42
import Platform

-- Our intermediate code:
import BlockId
43
import PprCmm           ( pprExpr )
44
import OldCmm
45 46 47
import CLabel

-- The rest:
48
import StaticFlags      ( opt_PIC )
49 50
import OrdList
import Outputable
51
import Unique
52 53
import DynFlags

54
import Control.Monad    ( mapAndUnzipM )
55 56 57
import Data.Bits
import Data.Word

58 59 60
import BasicTypes
import FastString

61 62 63 64 65 66 67 68
-- -----------------------------------------------------------------------------
-- Top-level of the instruction selector

-- | 'InstrBlock's are the insn sequences generated by the insn selectors.
-- They are really trees of insns to facilitate fast appending, where a
-- left-to-right traversal (pre-order?) yields the insns in the correct
-- order.

69
cmmTopCodeGen
Simon Peyton Jones's avatar
Simon Peyton Jones committed
70 71
        :: RawCmmDecl
        -> NatM [NatCmmDecl CmmStatics Instr]
72

Ian Lynagh's avatar
Ian Lynagh committed
73
cmmTopCodeGen (CmmProc info lab (ListGraph blocks)) = do
74 75
  (nat_blocks,statics) <- mapAndUnzipM basicBlockCodeGen blocks
  picBaseMb <- getPicBaseMaybeNat
76
  dflags <- getDynFlags
77
  let proc = CmmProc info lab (ListGraph $ concat nat_blocks)
78 79 80 81 82
      tops = proc : concat statics
      os   = platformOS $ targetPlatform dflags
  case picBaseMb of
      Just picBase -> initializePicBase_ppc ArchPPC os picBase tops
      Nothing -> return tops
83

Ian Lynagh's avatar
Ian Lynagh committed
84
cmmTopCodeGen (CmmData sec dat) = do
85 86
  return [CmmData sec dat]  -- no translation, we just use CmmStatic

87 88 89
basicBlockCodeGen
        :: CmmBasicBlock
        -> NatM ( [NatBasicBlock Instr]
Simon Peyton Jones's avatar
Simon Peyton Jones committed
90
                , [NatCmmDecl CmmStatics Instr])
91 92 93 94 95 96 97 98

basicBlockCodeGen (BasicBlock id stmts) = do
  instrs <- stmtsToInstrs stmts
  -- code generation may introduce new basic block boundaries, which
  -- are indicated by the NEWBLOCK instruction.  We must split up the
  -- instruction stream into basic blocks again.  Also, we extract
  -- LDATAs here too.
  let
99 100 101 102 103 104 105 106
        (top,other_blocks,statics) = foldrOL mkBlocks ([],[],[]) instrs

        mkBlocks (NEWBLOCK id) (instrs,blocks,statics)
          = ([], BasicBlock id instrs : blocks, statics)
        mkBlocks (LDATA sec dat) (instrs,blocks,statics)
          = (instrs, blocks, CmmData sec dat:statics)
        mkBlocks instr (instrs,blocks,statics)
          = (instr:instrs, blocks, statics)
107 108 109 110 111 112 113 114 115
  -- in
  return (BasicBlock id top : other_blocks, statics)

stmtsToInstrs :: [CmmStmt] -> NatM InstrBlock
stmtsToInstrs stmts
   = do instrss <- mapM stmtToInstrs stmts
        return (concatOL instrss)

stmtToInstrs :: CmmStmt -> NatM InstrBlock
116
stmtToInstrs stmt = do
117
  dflags <- getDynFlags
118
  case stmt of
119
    CmmNop         -> return nilOL
120 121 122 123
    CmmComment s   -> return (unitOL (COMMENT s))

    CmmAssign reg src
      | isFloatType ty -> assignReg_FltCode size reg src
124 125
      | target32Bit (targetPlatform dflags) &&
        isWord64 ty    -> assignReg_I64Code      reg src
126 127 128
      | otherwise        -> assignReg_IntCode size reg src
        where ty = cmmRegType reg
              size = cmmTypeSize ty
129 130 131

    CmmStore addr src
      | isFloatType ty -> assignMem_FltCode size addr src
132 133
      | target32Bit (targetPlatform dflags) &&
        isWord64 ty      -> assignMem_I64Code      addr src
134 135 136
      | otherwise        -> assignMem_IntCode size addr src
        where ty = cmmExprType src
              size = cmmTypeSize ty
137

138
    CmmCall target result_regs args _
139 140
       -> genCCall target result_regs args

141
    CmmBranch id          -> genBranch id
142 143
    CmmCondBranch arg id  -> genCondJump id arg
    CmmSwitch arg ids     -> genSwitch arg ids
144
    CmmJump arg _         -> genJump arg
dterei's avatar
dterei committed
145
    CmmReturn             ->
146 147 148 149 150
      panic "stmtToInstrs: return statement should have been cps'd away"


--------------------------------------------------------------------------------
-- | 'InstrBlock's are the insn sequences generated by the insn selectors.
151 152
--      They are really trees of insns to facilitate fast appending, where a
--      left-to-right traversal yields the insns in the correct order.
153
--
154 155
type InstrBlock
        = OrdList Instr
156 157 158


-- | Register's passed up the tree.  If the stix code forces the register
159 160 161
--      to live in a pre-decided machine register, it comes out as @Fixed@;
--      otherwise, it comes out as @Any@, and the parent can decide which
--      register to put it in.
162 163
--
data Register
164 165
        = Fixed Size Reg InstrBlock
        | Any   Size (Reg -> InstrBlock)
166 167 168 169 170 171 172 173 174 175 176


swizzleRegisterRep :: Register -> Size -> Register
swizzleRegisterRep (Fixed _ reg code) size = Fixed size reg code
swizzleRegisterRep (Any _ codefn)     size = Any   size codefn


-- | Grab the Reg for a CmmReg
getRegisterReg :: CmmReg -> Reg

getRegisterReg (CmmLocal (LocalReg u pk))
177
  = RegVirtual $ mkVirtualReg u (cmmTypeSize pk)
178 179

getRegisterReg (CmmGlobal mid)
180 181 182 183 184 185
  = case globalRegMaybe mid of
        Just reg -> reg
        Nothing  -> pprPanic "getRegisterReg-memory" (ppr $ CmmGlobal mid)
        -- By this stage, the only MagicIds remaining should be the
        -- ones which map to a real machine register on this
        -- platform.  Hence ...
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209


{-
Now, given a tree (the argument to an CmmLoad) that references memory,
produce a suitable addressing mode.

A Rule of the Game (tm) for Amodes: use of the addr bit must
immediately follow use of the code part, since the code part puts
values in registers which the addr then refers to.  So you can't put
anything in between, lest it overwrite some of those registers.  If
you need to do some other computation between the code part and use of
the addr bit, first store the effective address from the amode in a
temporary, then do the other computation, and then use the temporary:

    code
    LEA amode, tmp
    ... other computation ...
    ... (tmp) ...
-}


-- | Convert a BlockId to some CmmStatic data
jumpTableEntry :: Maybe BlockId -> CmmStatic
jumpTableEntry Nothing = CmmStaticLit (CmmInt 0 wordWidth)
210 211
jumpTableEntry (Just blockid) = CmmStaticLit (CmmLabel blockLabel)
    where blockLabel = mkAsmTempLabel (getUnique blockid)
212 213 214 215 216 217 218 219 220 221 222 223 224 225



-- -----------------------------------------------------------------------------
-- General things for putting together code sequences

-- Expand CmmRegOff.  ToDo: should we do it this way around, or convert
-- CmmExprs into CmmRegOff?
mangleIndexTree :: CmmExpr -> CmmExpr
mangleIndexTree (CmmRegOff reg off)
  = CmmMachOp (MO_Add width) [CmmReg reg, CmmLit (CmmInt (fromIntegral off) width)]
  where width = typeWidth (cmmRegType reg)

mangleIndexTree _
226
        = panic "PPC.CodeGen.mangleIndexTree: no match"
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

-- -----------------------------------------------------------------------------
--  Code gen for 64-bit arithmetic on 32-bit platforms

{-
Simple support for generating 64-bit code (ie, 64 bit values and 64
bit assignments) on 32-bit platforms.  Unlike the main code generator
we merely shoot for generating working code as simply as possible, and
pay little attention to code quality.  Specifically, there is no
attempt to deal cleverly with the fixed-vs-floating register
distinction; all values are generated into (pairs of) floating
registers, even if this would mean some redundant reg-reg moves as a
result.  Only one of the VRegUniques is returned, since it will be
of the VRegUniqueLo form, and the upper-half VReg can be determined
by applying getHiVRegFromLo to it.
-}

244 245 246 247 248 249 250 251
data ChildCode64        -- a.k.a "Register64"
      = ChildCode64
           InstrBlock   -- code
           Reg          -- the lower 32-bit temporary which contains the
                        -- result; use getHiVRegFromLo to find the other
                        -- VRegUnique.  Rules of this simplified insn
                        -- selection game are therefore that the returned
                        -- Reg may be modified
252 253 254


-- | The dual to getAnyReg: compute an expression into a register, but
255
--      we don't mind which one it is.
256 257 258 259 260
getSomeReg :: CmmExpr -> NatM (Reg, InstrBlock)
getSomeReg expr = do
  r <- getRegister expr
  case r of
    Any rep code -> do
261 262 263 264
        tmp <- getNewRegNat rep
        return (tmp, code tmp)
    Fixed _ reg code ->
        return (reg, code)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

getI64Amodes :: CmmExpr -> NatM (AddrMode, AddrMode, InstrBlock)
getI64Amodes addrTree = do
    Amode hi_addr addr_code <- getAmode addrTree
    case addrOffset hi_addr 4 of
        Just lo_addr -> return (hi_addr, lo_addr, addr_code)
        Nothing      -> do (hi_ptr, code) <- getSomeReg addrTree
                           return (AddrRegImm hi_ptr (ImmInt 0),
                                   AddrRegImm hi_ptr (ImmInt 4),
                                   code)


assignMem_I64Code :: CmmExpr -> CmmExpr -> NatM InstrBlock
assignMem_I64Code addrTree valueTree = do
        (hi_addr, lo_addr, addr_code) <- getI64Amodes addrTree
280 281 282
        ChildCode64 vcode rlo <- iselExpr64 valueTree
        let
                rhi = getHiVRegFromLo rlo
283

284 285 286 287 288
                -- Big-endian store
                mov_hi = ST II32 rhi hi_addr
                mov_lo = ST II32 rlo lo_addr
        -- in
        return (vcode `appOL` addr_code `snocOL` mov_lo `snocOL` mov_hi)
289 290 291


assignReg_I64Code :: CmmReg  -> CmmExpr -> NatM InstrBlock
292
assignReg_I64Code (CmmLocal (LocalReg u_dst _)) valueTree = do
293
   ChildCode64 vcode r_src_lo <- iselExpr64 valueTree
294
   let
295
         r_dst_lo = RegVirtual $ mkVirtualReg u_dst II32
296 297 298 299 300 301 302 303 304
         r_dst_hi = getHiVRegFromLo r_dst_lo
         r_src_hi = getHiVRegFromLo r_src_lo
         mov_lo = MR r_dst_lo r_src_lo
         mov_hi = MR r_dst_hi r_src_hi
   -- in
   return (
        vcode `snocOL` mov_lo `snocOL` mov_hi
     )

305
assignReg_I64Code _ _
306 307 308 309 310 311 312 313 314
   = panic "assignReg_I64Code(powerpc): invalid lvalue"


iselExpr64        :: CmmExpr -> NatM ChildCode64
iselExpr64 (CmmLoad addrTree ty) | isWord64 ty = do
    (hi_addr, lo_addr, addr_code) <- getI64Amodes addrTree
    (rlo, rhi) <- getNewRegPairNat II32
    let mov_hi = LD II32 rhi hi_addr
        mov_lo = LD II32 rlo lo_addr
315
    return $ ChildCode64 (addr_code `snocOL` mov_lo `snocOL` mov_hi)
316 317 318
                         rlo

iselExpr64 (CmmReg (CmmLocal (LocalReg vu ty))) | isWord64 ty
319
   = return (ChildCode64 nilOL (RegVirtual $ mkVirtualReg vu II32))
320 321 322 323

iselExpr64 (CmmLit (CmmInt i _)) = do
  (rlo,rhi) <- getNewRegPairNat II32
  let
324 325 326 327 328 329 330 331 332 333 334
        half0 = fromIntegral (fromIntegral i :: Word16)
        half1 = fromIntegral ((fromIntegral i `shiftR` 16) :: Word16)
        half2 = fromIntegral ((fromIntegral i `shiftR` 32) :: Word16)
        half3 = fromIntegral ((fromIntegral i `shiftR` 48) :: Word16)

        code = toOL [
                LIS rlo (ImmInt half1),
                OR rlo rlo (RIImm $ ImmInt half0),
                LIS rhi (ImmInt half3),
                OR rlo rlo (RIImm $ ImmInt half2)
                ]
335 336 337 338 339 340 341 342
  -- in
  return (ChildCode64 code rlo)

iselExpr64 (CmmMachOp (MO_Add _) [e1,e2]) = do
   ChildCode64 code1 r1lo <- iselExpr64 e1
   ChildCode64 code2 r2lo <- iselExpr64 e2
   (rlo,rhi) <- getNewRegPairNat II32
   let
343 344 345 346 347 348
        r1hi = getHiVRegFromLo r1lo
        r2hi = getHiVRegFromLo r2lo
        code =  code1 `appOL`
                code2 `appOL`
                toOL [ ADDC rlo r1lo r2lo,
                       ADDE rhi r1hi r2hi ]
349 350 351 352 353 354 355 356 357 358 359
   -- in
   return (ChildCode64 code rlo)

iselExpr64 (CmmMachOp (MO_UU_Conv W32 W64) [expr]) = do
    (expr_reg,expr_code) <- getSomeReg expr
    (rlo, rhi) <- getNewRegPairNat II32
    let mov_hi = LI rhi (ImmInt 0)
        mov_lo = MR rlo expr_reg
    return $ ChildCode64 (expr_code `snocOL` mov_lo `snocOL` mov_hi)
                         rlo
iselExpr64 expr
360
   = do dflags <- getDynFlags
361
        pprPanic "iselExpr64(powerpc)" (pprPlatform (targetPlatform dflags) expr)
362 363 364 365



getRegister :: CmmExpr -> NatM Register
366
getRegister e = do dflags <- getDynFlags
367 368 369
                   getRegister' dflags e

getRegister' :: DynFlags -> CmmExpr -> NatM Register
370

371
getRegister' _ (CmmReg (CmmGlobal PicBaseReg))
pho@cielonegro.org's avatar
pho@cielonegro.org committed
372 373 374 375
  = do
      reg <- getPicBaseNat archWordSize
      return (Fixed archWordSize reg nilOL)

376
getRegister' _ (CmmReg reg)
377 378
  = return (Fixed (cmmTypeSize (cmmRegType reg))
                  (getRegisterReg reg) nilOL)
379

380 381
getRegister' dflags tree@(CmmRegOff _ _)
  = getRegister' dflags (mangleIndexTree tree)
382 383 384 385

    -- for 32-bit architectuers, support some 64 -> 32 bit conversions:
    -- TO_W_(x), TO_W_(x >> 32)

386 387 388
getRegister' dflags (CmmMachOp (MO_UU_Conv W64 W32)
                     [CmmMachOp (MO_U_Shr W64) [x,CmmLit (CmmInt 32 _)]])
 | target32Bit (targetPlatform dflags) = do
389 390 391
  ChildCode64 code rlo <- iselExpr64 x
  return $ Fixed II32 (getHiVRegFromLo rlo) code

392 393 394
getRegister' dflags (CmmMachOp (MO_SS_Conv W64 W32)
                     [CmmMachOp (MO_U_Shr W64) [x,CmmLit (CmmInt 32 _)]])
 | target32Bit (targetPlatform dflags) = do
395 396 397
  ChildCode64 code rlo <- iselExpr64 x
  return $ Fixed II32 (getHiVRegFromLo rlo) code

398 399
getRegister' dflags (CmmMachOp (MO_UU_Conv W64 W32) [x])
 | target32Bit (targetPlatform dflags) = do
400 401 402
  ChildCode64 code rlo <- iselExpr64 x
  return $ Fixed II32 rlo code

403 404
getRegister' dflags (CmmMachOp (MO_SS_Conv W64 W32) [x])
 | target32Bit (targetPlatform dflags) = do
405
  ChildCode64 code rlo <- iselExpr64 x
406
  return $ Fixed II32 rlo code
407

408
getRegister' dflags (CmmLoad mem pk)
409 410
  | not (isWord64 pk)
  = do
411
        let platform = targetPlatform dflags
412
        Amode addr addr_code <- getAmode mem
413
        let code dst = ASSERT((targetClassOfReg platform dst == RcDouble) == isFloatType pk)
414 415 416 417 418
                       addr_code `snocOL` LD size dst addr
        return (Any size code)
          where size = cmmTypeSize pk

-- catch simple cases of zero- or sign-extended load
419
getRegister' _ (CmmMachOp (MO_UU_Conv W8 W32) [CmmLoad mem _]) = do
420 421 422 423 424
    Amode addr addr_code <- getAmode mem
    return (Any II32 (\dst -> addr_code `snocOL` LD II8 dst addr))

-- Note: there is no Load Byte Arithmetic instruction, so no signed case here

425
getRegister' _ (CmmMachOp (MO_UU_Conv W16 W32) [CmmLoad mem _]) = do
426 427 428
    Amode addr addr_code <- getAmode mem
    return (Any II32 (\dst -> addr_code `snocOL` LD II16 dst addr))

429
getRegister' _ (CmmMachOp (MO_SS_Conv W16 W32) [CmmLoad mem _]) = do
430 431 432
    Amode addr addr_code <- getAmode mem
    return (Any II32 (\dst -> addr_code `snocOL` LA II16 dst addr))

433
getRegister' dflags (CmmMachOp mop [x]) -- unary MachOps
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  = case mop of
      MO_Not rep   -> triv_ucode_int rep NOT

      MO_F_Neg w   -> triv_ucode_float w FNEG
      MO_S_Neg w   -> triv_ucode_int   w NEG

      MO_FF_Conv W64 W32 -> trivialUCode  FF32 FRSP x
      MO_FF_Conv W32 W64 -> conversionNop FF64 x

      MO_FS_Conv from to -> coerceFP2Int from to x
      MO_SF_Conv from to -> coerceInt2FP from to x

      MO_SS_Conv from to
        | from == to    -> conversionNop (intSize to) x

        -- narrowing is a nop: we treat the high bits as undefined
      MO_SS_Conv W32 to -> conversionNop (intSize to) x
      MO_SS_Conv W16 W8 -> conversionNop II8 x
      MO_SS_Conv W8  to -> triv_ucode_int to (EXTS II8)
      MO_SS_Conv W16 to -> triv_ucode_int to (EXTS II16)

      MO_UU_Conv from to
        | from == to -> conversionNop (intSize to) x
        -- narrowing is a nop: we treat the high bits as undefined
      MO_UU_Conv W32 to -> conversionNop (intSize to) x
      MO_UU_Conv W16 W8 -> conversionNop II8 x
      MO_UU_Conv W8 to  -> trivialCode to False AND x (CmmLit (CmmInt 255 W32))
461 462
      MO_UU_Conv W16 to -> trivialCode to False AND x (CmmLit (CmmInt 65535 W32))
      _ -> panic "PPC.CodeGen.getRegister: no match"
463 464

    where
465 466
        triv_ucode_int   width instr = trivialUCode (intSize   width) instr x
        triv_ucode_float width instr = trivialUCode (floatSize width) instr x
467 468

        conversionNop new_size expr
469
            = do e_code <- getRegister' dflags expr
470 471
                 return (swizzleRegisterRep e_code new_size)

472
getRegister' _ (CmmMachOp mop [x, y]) -- dyadic PrimOps
473
  = case mop of
474 475 476 477 478 479
      MO_F_Eq _ -> condFltReg EQQ x y
      MO_F_Ne _ -> condFltReg NE  x y
      MO_F_Gt _ -> condFltReg GTT x y
      MO_F_Ge _ -> condFltReg GE  x y
      MO_F_Lt _ -> condFltReg LTT x y
      MO_F_Le _ -> condFltReg LE  x y
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

      MO_Eq rep -> condIntReg EQQ  (extendUExpr rep x) (extendUExpr rep y)
      MO_Ne rep -> condIntReg NE   (extendUExpr rep x) (extendUExpr rep y)

      MO_S_Gt rep -> condIntReg GTT  (extendSExpr rep x) (extendSExpr rep y)
      MO_S_Ge rep -> condIntReg GE   (extendSExpr rep x) (extendSExpr rep y)
      MO_S_Lt rep -> condIntReg LTT  (extendSExpr rep x) (extendSExpr rep y)
      MO_S_Le rep -> condIntReg LE   (extendSExpr rep x) (extendSExpr rep y)

      MO_U_Gt rep -> condIntReg GU   (extendUExpr rep x) (extendUExpr rep y)
      MO_U_Ge rep -> condIntReg GEU  (extendUExpr rep x) (extendUExpr rep y)
      MO_U_Lt rep -> condIntReg LU   (extendUExpr rep x) (extendUExpr rep y)
      MO_U_Le rep -> condIntReg LEU  (extendUExpr rep x) (extendUExpr rep y)

      MO_F_Add w  -> triv_float w FADD
      MO_F_Sub w  -> triv_float w FSUB
      MO_F_Mul w  -> triv_float w FMUL
      MO_F_Quot w -> triv_float w FDIV
498

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
         -- optimize addition with 32-bit immediate
         -- (needed for PIC)
      MO_Add W32 ->
        case y of
          CmmLit (CmmInt imm immrep) | Just _ <- makeImmediate W32 True (-imm)
            -> trivialCode W32 True ADD x (CmmLit $ CmmInt imm immrep)
          CmmLit lit
            -> do
                (src, srcCode) <- getSomeReg x
                let imm = litToImm lit
                    code dst = srcCode `appOL` toOL [
                                    ADDIS dst src (HA imm),
                                    ADD dst dst (RIImm (LO imm))
                                ]
                return (Any II32 code)
          _ -> trivialCode W32 True ADD x y

      MO_Add rep -> trivialCode rep True ADD x y
      MO_Sub rep ->
        case y of    -- subfi ('substract from' with immediate) doesn't exist
          CmmLit (CmmInt imm immrep) | Just _ <- makeImmediate rep True (-imm)
            -> trivialCode rep True ADD x (CmmLit $ CmmInt (-imm) immrep)
          _ -> trivialCodeNoImm' (intSize rep) SUBF y x

      MO_Mul rep -> trivialCode rep True MULLW x y

      MO_S_MulMayOflo W32 -> trivialCodeNoImm' II32 MULLW_MayOflo x y
526

527 528
      MO_S_MulMayOflo _ -> panic "S_MulMayOflo (rep /= II32): not implemented"
      MO_U_MulMayOflo _ -> panic "U_MulMayOflo: not implemented"
529 530 531

      MO_S_Quot rep -> trivialCodeNoImm' (intSize rep) DIVW (extendSExpr rep x) (extendSExpr rep y)
      MO_U_Quot rep -> trivialCodeNoImm' (intSize rep) DIVWU (extendUExpr rep x) (extendUExpr rep y)
532

533 534
      MO_S_Rem rep -> remainderCode rep DIVW (extendSExpr rep x) (extendSExpr rep y)
      MO_U_Rem rep -> remainderCode rep DIVWU (extendUExpr rep x) (extendUExpr rep y)
535

536 537 538 539 540 541 542
      MO_And rep   -> trivialCode rep False AND x y
      MO_Or rep    -> trivialCode rep False OR x y
      MO_Xor rep   -> trivialCode rep False XOR x y

      MO_Shl rep   -> trivialCode rep False SLW x y
      MO_S_Shr rep -> trivialCode rep False SRAW (extendSExpr rep x) y
      MO_U_Shr rep -> trivialCode rep False SRW (extendUExpr rep x) y
543
      _         -> panic "PPC.CodeGen.getRegister: no match"
544 545 546 547 548

  where
    triv_float :: Width -> (Size -> Reg -> Reg -> Reg -> Instr) -> NatM Register
    triv_float width instr = trivialCodeNoImm (floatSize width) instr x y

549
getRegister' _ (CmmLit (CmmInt i rep))
550 551
  | Just imm <- makeImmediate rep True i
  = let
552
        code dst = unitOL (LI dst imm)
553
    in
554
        return (Any (intSize rep) code)
555

556
getRegister' _ (CmmLit (CmmFloat f frep)) = do
557
    lbl <- getNewLabelNat
558
    dflags <- getDynFlags
559 560 561
    dynRef <- cmmMakeDynamicReference dflags addImportNat DataReference lbl
    Amode addr addr_code <- getAmode dynRef
    let size = floatSize frep
562
        code dst =
563 564
            LDATA ReadOnlyData (Statics lbl
                                   [CmmStaticLit (CmmFloat f frep)])
565 566 567
            `consOL` (addr_code `snocOL` LD size dst addr)
    return (Any size code)

568
getRegister' _ (CmmLit lit)
569 570 571 572 573 574 575 576
  = let rep = cmmLitType lit
        imm = litToImm lit
        code dst = toOL [
              LIS dst (HA imm),
              ADD dst dst (RIImm (LO imm))
          ]
    in return (Any (cmmTypeSize rep) code)

577
getRegister' dflags other = pprPanic "getRegister(ppc)" (pprExpr (targetPlatform dflags) other)
578

579 580
    -- extend?Rep: wrap integer expression of type rep
    -- in a conversion to II32
581
extendSExpr :: Width -> CmmExpr -> CmmExpr
582 583
extendSExpr W32 x = x
extendSExpr rep x = CmmMachOp (MO_SS_Conv rep W32) [x]
584 585

extendUExpr :: Width -> CmmExpr -> CmmExpr
586 587 588 589 590 591
extendUExpr W32 x = x
extendUExpr rep x = CmmMachOp (MO_UU_Conv rep W32) [x]

-- -----------------------------------------------------------------------------
--  The 'Amode' type: Memory addressing modes passed up the tree.

592 593
data Amode
        = Amode AddrMode InstrBlock
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

{-
Now, given a tree (the argument to an CmmLoad) that references memory,
produce a suitable addressing mode.

A Rule of the Game (tm) for Amodes: use of the addr bit must
immediately follow use of the code part, since the code part puts
values in registers which the addr then refers to.  So you can't put
anything in between, lest it overwrite some of those registers.  If
you need to do some other computation between the code part and use of
the addr bit, first store the effective address from the amode in a
temporary, then do the other computation, and then use the temporary:

    code
    LEA amode, tmp
    ... other computation ...
    ... (tmp) ...
-}

getAmode :: CmmExpr -> NatM Amode
getAmode tree@(CmmRegOff _ _) = getAmode (mangleIndexTree tree)

getAmode (CmmMachOp (MO_Sub W32) [x, CmmLit (CmmInt i _)])
  | Just off <- makeImmediate W32 True (-i)
  = do
        (reg, code) <- getSomeReg x
        return (Amode (AddrRegImm reg off) code)


getAmode (CmmMachOp (MO_Add W32) [x, CmmLit (CmmInt i _)])
  | Just off <- makeImmediate W32 True i
  = do
        (reg, code) <- getSomeReg x
        return (Amode (AddrRegImm reg off) code)

   -- optimize addition with 32-bit immediate
   -- (needed for PIC)
getAmode (CmmMachOp (MO_Add W32) [x, CmmLit lit])
  = do
        tmp <- getNewRegNat II32
        (src, srcCode) <- getSomeReg x
        let imm = litToImm lit
            code = srcCode `snocOL` ADDIS tmp src (HA imm)
        return (Amode (AddrRegImm tmp (LO imm)) code)

getAmode (CmmLit lit)
  = do
        tmp <- getNewRegNat II32
        let imm = litToImm lit
            code = unitOL (LIS tmp (HA imm))
        return (Amode (AddrRegImm tmp (LO imm)) code)
645

646 647 648 649 650
getAmode (CmmMachOp (MO_Add W32) [x, y])
  = do
        (regX, codeX) <- getSomeReg x
        (regY, codeY) <- getSomeReg y
        return (Amode (AddrRegReg regX regY) (codeX `appOL` codeY))
651

652 653 654 655 656 657 658 659 660 661
getAmode other
  = do
        (reg, code) <- getSomeReg other
        let
            off  = ImmInt 0
        return (Amode (AddrRegImm reg off) code)



--  The 'CondCode' type:  Condition codes passed up the tree.
662 663
data CondCode
        = CondCode Bool Cond InstrBlock
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

-- Set up a condition code for a conditional branch.

getCondCode :: CmmExpr -> NatM CondCode

-- almost the same as everywhere else - but we need to
-- extend small integers to 32 bit first

getCondCode (CmmMachOp mop [x, y])
  = case mop of
      MO_F_Eq W32 -> condFltCode EQQ x y
      MO_F_Ne W32 -> condFltCode NE  x y
      MO_F_Gt W32 -> condFltCode GTT x y
      MO_F_Ge W32 -> condFltCode GE  x y
      MO_F_Lt W32 -> condFltCode LTT x y
      MO_F_Le W32 -> condFltCode LE  x y

      MO_F_Eq W64 -> condFltCode EQQ x y
      MO_F_Ne W64 -> condFltCode NE  x y
      MO_F_Gt W64 -> condFltCode GTT x y
      MO_F_Ge W64 -> condFltCode GE  x y
      MO_F_Lt W64 -> condFltCode LTT x y
      MO_F_Le W64 -> condFltCode LE  x y

      MO_Eq rep -> condIntCode EQQ  (extendUExpr rep x) (extendUExpr rep y)
      MO_Ne rep -> condIntCode NE   (extendUExpr rep x) (extendUExpr rep y)

      MO_S_Gt rep -> condIntCode GTT  (extendSExpr rep x) (extendSExpr rep y)
      MO_S_Ge rep -> condIntCode GE   (extendSExpr rep x) (extendSExpr rep y)
      MO_S_Lt rep -> condIntCode LTT  (extendSExpr rep x) (extendSExpr rep y)
      MO_S_Le rep -> condIntCode LE   (extendSExpr rep x) (extendSExpr rep y)

      MO_U_Gt rep -> condIntCode GU   (extendUExpr rep x) (extendUExpr rep y)
      MO_U_Ge rep -> condIntCode GEU  (extendUExpr rep x) (extendUExpr rep y)
      MO_U_Lt rep -> condIntCode LU   (extendUExpr rep x) (extendUExpr rep y)
      MO_U_Le rep -> condIntCode LEU  (extendUExpr rep x) (extendUExpr rep y)

701
      _ -> pprPanic "getCondCode(powerpc)" (pprMachOp mop)
702

703
getCondCode _ = panic "getCondCode(2)(powerpc)"
704 705 706 707 708 709 710 711 712 713 714 715 716 717



-- @cond(Int|Flt)Code@: Turn a boolean expression into a condition, to be
-- passed back up the tree.

condIntCode, condFltCode :: Cond -> CmmExpr -> CmmExpr -> NatM CondCode

--  ###FIXME: I16 and I8!
condIntCode cond x (CmmLit (CmmInt y rep))
  | Just src2 <- makeImmediate rep (not $ condUnsigned cond) y
  = do
        (src1, code) <- getSomeReg x
        let
718
            code' = code `snocOL`
719 720 721 722 723 724 725
                (if condUnsigned cond then CMPL else CMP) II32 src1 (RIImm src2)
        return (CondCode False cond code')

condIntCode cond x y = do
    (src1, code1) <- getSomeReg x
    (src2, code2) <- getSomeReg y
    let
726 727
        code' = code1 `appOL` code2 `snocOL`
                  (if condUnsigned cond then CMPL else CMP) II32 src1 (RIReg src2)
728 729 730 731 732 733
    return (CondCode False cond code')

condFltCode cond x y = do
    (src1, code1) <- getSomeReg x
    (src2, code2) <- getSomeReg y
    let
734 735
        code'  = code1 `appOL` code2 `snocOL` FCMP src1 src2
        code'' = case cond of -- twiddle CR to handle unordered case
736
                    GE -> code' `snocOL` CRNOR ltbit eqbit gtbit
737 738
                    LE -> code' `snocOL` CRNOR gtbit eqbit ltbit
                    _ -> code'
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
                 where
                    ltbit = 0 ; eqbit = 2 ; gtbit = 1
    return (CondCode True cond code'')



-- -----------------------------------------------------------------------------
-- Generating assignments

-- Assignments are really at the heart of the whole code generation
-- business.  Almost all top-level nodes of any real importance are
-- assignments, which correspond to loads, stores, or register
-- transfers.  If we're really lucky, some of the register transfers
-- will go away, because we can use the destination register to
-- complete the code generation for the right hand side.  This only
-- fails when the right hand side is forced into a fixed register
-- (e.g. the result of a call).

assignMem_IntCode :: Size -> CmmExpr -> CmmExpr -> NatM InstrBlock
assignReg_IntCode :: Size -> CmmReg  -> CmmExpr -> NatM InstrBlock

assignMem_FltCode :: Size -> CmmExpr -> CmmExpr -> NatM InstrBlock
assignReg_FltCode :: Size -> CmmReg  -> CmmExpr -> NatM InstrBlock

assignMem_IntCode pk addr src = do
    (srcReg, code) <- getSomeReg src
    Amode dstAddr addr_code <- getAmode addr
    return $ code `appOL` addr_code `snocOL` ST pk srcReg dstAddr

-- dst is a reg, but src could be anything
assignReg_IntCode _ reg src
    = do
        r <- getRegister src
        return $ case r of
            Any _ code         -> code dst
            Fixed _ freg fcode -> fcode `snocOL` MR dst freg
    where
        dst = getRegisterReg reg



-- Easy, isn't it?
assignMem_FltCode = assignMem_IntCode
assignReg_FltCode = assignReg_IntCode



genJump :: CmmExpr{-the branch target-} -> NatM InstrBlock

genJump (CmmLit (CmmLabel lbl))
  = return (unitOL $ JMP lbl)

genJump tree
  = do
        (target,code) <- getSomeReg tree
794
        return (code `snocOL` MTCTR target `snocOL` BCTR [] Nothing)
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822


-- -----------------------------------------------------------------------------
--  Unconditional branches
genBranch :: BlockId -> NatM InstrBlock
genBranch = return . toOL . mkJumpInstr


-- -----------------------------------------------------------------------------
--  Conditional jumps

{-
Conditional jumps are always to local labels, so we can use branch
instructions.  We peek at the arguments to decide what kind of
comparison to do.

SPARC: First, we have to ensure that the condition codes are set
according to the supplied comparison operation.  We generate slightly
different code for floating point comparisons, because a floating
point operation cannot directly precede a @BF@.  We assume the worst
and fill that slot with a @NOP@.

SPARC: Do not fill the delay slots here; you will confuse the register
allocator.
-}


genCondJump
823
    :: BlockId      -- the branch target
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
    -> CmmExpr      -- the condition on which to branch
    -> NatM InstrBlock

genCondJump id bool = do
  CondCode _ cond code <- getCondCode bool
  return (code `snocOL` BCC cond id)



-- -----------------------------------------------------------------------------
--  Generating C calls

-- Now the biggest nightmare---calls.  Most of the nastiness is buried in
-- @get_arg@, which moves the arguments to the correct registers/stack
-- locations.  Apart from that, the code is easy.
839
--
840 841 842
-- (If applicable) Do not fill the delay slots here; you will confuse the
-- register allocator.

843
genCCall :: CmmCallTarget            -- function to call
844 845
         -> [HintedCmmFormal]        -- where to put the result
         -> [HintedCmmActual]        -- arguments (of mixed type)
846 847
         -> NatM InstrBlock
genCCall target dest_regs argsAndHints
848
 = do dflags <- getDynFlags
849 850 851
      case platformOS (targetPlatform dflags) of
          OSLinux    -> genCCall' GCPLinux  target dest_regs argsAndHints
          OSDarwin   -> genCCall' GCPDarwin target dest_regs argsAndHints
Simon Marlow's avatar
Simon Marlow committed
852
          _ -> panic "PPC.CodeGen.genCCall: not defined for this os"
853 854 855 856 857 858

data GenCCallPlatform = GCPLinux | GCPDarwin

genCCall'
    :: GenCCallPlatform
    -> CmmCallTarget            -- function to call
859 860
    -> [HintedCmmFormal]        -- where to put the result
    -> [HintedCmmActual]        -- arguments (of mixed type)
861 862 863 864 865 866
    -> NatM InstrBlock

{-
    The PowerPC calling convention for Darwin/Mac OS X
    is described in Apple's document
    "Inside Mac OS X - Mach-O Runtime Architecture".
867

868 869 870 871 872 873
    PowerPC Linux uses the System V Release 4 Calling Convention
    for PowerPC. It is described in the
    "System V Application Binary Interface PowerPC Processor Supplement".

    Both conventions are similar:
    Parameters may be passed in general-purpose registers starting at r3, in
874 875
    floating point registers starting at f1, or on the stack.

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    But there are substantial differences:
    * The number of registers used for parameter passing and the exact set of
      nonvolatile registers differs (see MachRegs.lhs).
    * On Darwin, stack space is always reserved for parameters, even if they are
      passed in registers. The called routine may choose to save parameters from
      registers to the corresponding space on the stack.
    * On Darwin, a corresponding amount of GPRs is skipped when a floating point
      parameter is passed in an FPR.
    * SysV insists on either passing I64 arguments on the stack, or in two GPRs,
      starting with an odd-numbered GPR. It may skip a GPR to achieve this.
      Darwin just treats an I64 like two separate II32s (high word first).
    * I64 and FF64 arguments are 8-byte aligned on the stack for SysV, but only
      4-byte aligned like everything else on Darwin.
    * The SysV spec claims that FF32 is represented as FF64 on the stack. GCC on
      PowerPC Linux does not agree, so neither do we.
891

892 893 894 895 896 897 898 899 900
    According to both conventions, The parameter area should be part of the
    caller's stack frame, allocated in the caller's prologue code (large enough
    to hold the parameter lists for all called routines). The NCG already
    uses the stack for register spilling, leaving 64 bytes free at the top.
    If we need a larger parameter area than that, we just allocate a new stack
    frame just before ccalling.
-}


Ian Lynagh's avatar
Ian Lynagh committed
901
genCCall' _ (CmmPrim MO_WriteBarrier _) _ _
902 903
 = return $ unitOL LWSYNC

Ian Lynagh's avatar
Ian Lynagh committed
904 905
genCCall' _ (CmmPrim _ (Just mkStmts)) results args
    = stmtsToInstrs (mkStmts results args)
906

907
genCCall' gcp target dest_regs argsAndHints
908
  = ASSERT (not $ any (`elem` [II16]) $ map cmmTypeSize argReps)
909 910 911 912 913 914 915
        -- we rely on argument promotion in the codeGen
    do
        (finalStack,passArgumentsCode,usedRegs) <- passArguments
                                                        (zip args argReps)
                                                        allArgRegs allFPArgRegs
                                                        initialStackOffset
                                                        (toOL []) []
916

917
        (labelOrExpr, reduceToFF32) <- case target of
918 919
            CmmCallee (CmmLit (CmmLabel lbl)) _ -> return (Left lbl, False)
            CmmCallee expr _ -> return  (Right expr, False)
Ian Lynagh's avatar
Ian Lynagh committed
920
            CmmPrim mop _ -> outOfLineMachOp mop
921

922 923 924 925 926
        let codeBefore = move_sp_down finalStack `appOL` passArgumentsCode
            codeAfter = move_sp_up finalStack `appOL` moveResult reduceToFF32

        case labelOrExpr of
            Left lbl -> do
927
                return (         codeBefore
928
                        `snocOL` BL lbl usedRegs
929
                        `appOL`  codeAfter)
930
            Right dyn -> do
931 932 933 934
                (dynReg, dynCode) <- getSomeReg dyn
                return (         dynCode
                        `snocOL` MTCTR dynReg
                        `appOL`  codeBefore
935
                        `snocOL` BCTRL usedRegs
936
                        `appOL`  codeAfter)
937
    where
938 939 940
        initialStackOffset = case gcp of
                             GCPDarwin -> 24
                             GCPLinux  -> 8
941
            -- size of linkage area + size of arguments, in bytes
942 943 944 945 946 947
        stackDelta finalStack = case gcp of
                                GCPDarwin ->
                                    roundTo 16 $ (24 +) $ max 32 $ sum $
                                    map (widthInBytes . typeWidth) argReps
                                GCPLinux -> roundTo 16 finalStack

948
        -- need to remove alignment information
Ian Lynagh's avatar
Ian Lynagh committed
949
        argsAndHints' | (CmmPrim mop _) <- target,
950 951 952
                        (mop == MO_Memcpy ||
                         mop == MO_Memset ||
                         mop == MO_Memmove)
dterei's avatar
dterei committed
953
                      = init argsAndHints
954 955

                      | otherwise
dterei's avatar
dterei committed
956
                      = argsAndHints
957

958 959
        args = map hintlessCmm argsAndHints'
        argReps = map cmmExprType args
960

961 962
        roundTo a x | x `mod` a == 0 = x
                    | otherwise = x + a - (x `mod` a)
963 964 965 966

        move_sp_down finalStack
               | delta > 64 =
                        toOL [STU II32 sp (AddrRegImm sp (ImmInt (-delta))),
967 968 969 970 971
                              DELTA (-delta)]
               | otherwise = nilOL
               where delta = stackDelta finalStack
        move_sp_up finalStack
               | delta > 64 =
972 973
                        toOL [ADD sp sp (RIImm (ImmInt delta)),
                              DELTA 0]
974 975 976
               | otherwise = nilOL
               where delta = stackDelta finalStack

977 978 979 980 981 982 983 984

        passArguments [] _ _ stackOffset accumCode accumUsed = return (stackOffset, accumCode, accumUsed)
        passArguments ((arg,arg_ty):args) gprs fprs stackOffset
               accumCode accumUsed | isWord64 arg_ty =
            do
                ChildCode64 code vr_lo <- iselExpr64 arg
                let vr_hi = getHiVRegFromLo vr_lo

985 986
                case gcp of
                    GCPDarwin ->
987
                        do let storeWord vr (gpr:_) _ = MR gpr vr
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
                               storeWord vr [] offset
                                   = ST II32 vr (AddrRegImm sp (ImmInt offset))
                           passArguments args
                                         (drop 2 gprs)
                                         fprs
                                         (stackOffset+8)
                                         (accumCode `appOL` code
                                               `snocOL` storeWord vr_hi gprs stackOffset
                                               `snocOL` storeWord vr_lo (drop 1 gprs) (stackOffset+4))
                                         ((take 2 gprs) ++ accumUsed)
                    GCPLinux ->
                        do let stackOffset' = roundTo 8 stackOffset
                               stackCode = accumCode `appOL` code
                                   `snocOL` ST II32 vr_hi (AddrRegImm sp (ImmInt stackOffset'))
                                   `snocOL` ST II32 vr_lo (AddrRegImm sp (ImmInt (stackOffset'+4)))
                               regCode hireg loreg =
                                   accumCode `appOL` code
                                       `snocOL` MR hireg vr_hi
                                       `snocOL` MR loreg vr_lo

                           case gprs of
                               hireg : loreg : regs | even (length gprs) ->
                                   passArguments args regs fprs stackOffset
                                                 (regCode hireg loreg) (hireg : loreg : accumUsed)
                               _skipped : hireg : loreg : regs ->
                                   passArguments args regs fprs stackOffset
                                                 (regCode hireg loreg) (hireg : loreg : accumUsed)
                               _ -> -- only one or no regs left
                                   passArguments args [] fprs (stackOffset'+8)
                                                 stackCode accumUsed
1018

1019 1020 1021 1022 1023 1024
        passArguments ((arg,rep):args) gprs fprs stackOffset accumCode accumUsed
            | reg : _ <- regs = do
                register <- getRegister arg
                let code = case register of
                            Fixed _ freg fcode -> fcode `snocOL` MR reg freg
                            Any _ acode -> acode reg
1025 1026 1027 1028 1029 1030
                    stackOffsetRes = case gcp of
                                     -- The Darwin ABI requires that we reserve
                                     -- stack slots for register parameters
                                     GCPDarwin -> stackOffset + stackBytes
                                     -- ... the SysV ABI doesn't.
                                     GCPLinux -> stackOffset
1031 1032 1033
                passArguments args
                              (drop nGprs gprs)
                              (drop nFprs fprs)
1034
                              stackOffsetRes
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
                              (accumCode `appOL` code)
                              (reg : accumUsed)
            | otherwise = do
                (vr, code) <- getSomeReg arg
                passArguments args
                              (drop nGprs gprs)
                              (drop nFprs fprs)
                              (stackOffset' + stackBytes)
                              (accumCode `appOL` code `snocOL` ST (cmmTypeSize rep) vr stackSlot)
                              accumUsed
            where
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
                stackOffset' = case gcp of
                               GCPDarwin ->
                                   -- stackOffset is at least 4-byte aligned
                                   -- The Darwin ABI is happy with that.
                                   stackOffset
                               GCPLinux
                                   -- ... the SysV ABI requires 8-byte
                                   -- alignment for doubles.
                                | isFloatType rep && typeWidth rep == W64 ->
                                   roundTo 8 stackOffset
                                | otherwise ->
                                   stackOffset
1058
                stackSlot = AddrRegImm sp (ImmInt stackOffset')
1059 1060 1061 1062
                (nGprs, nFprs, stackBytes, regs)
                    = case gcp of
                      GCPDarwin ->
                          case cmmTypeSize rep of
1063
                          II8  -> (1, 0, 4, gprs)
1064 1065 1066 1067 1068 1069
                          II32 -> (1, 0, 4, gprs)
                          -- The Darwin ABI requires that we skip a
                          -- corresponding number of GPRs when we use
                          -- the FPRs.
                          FF32 -> (1, 1, 4, fprs)
                          FF64 -> (2, 1, 8, fprs)
1070 1071 1072
                          II16 -> panic "genCCall' passArguments II16"
                          II64 -> panic "genCCall' passArguments II64"
                          FF80 -> panic "genCCall' passArguments FF80"
1073 1074
                      GCPLinux ->
                          case cmmTypeSize rep of
1075
                          II8  -> (1, 0, 4, gprs)
1076 1077 1078 1079
                          II32 -> (1, 0, 4, gprs)
                          -- ... the SysV ABI doesn't.
                          FF32 -> (0, 1, 4, fprs)
                          FF64 -> (0, 1, 8, fprs)
1080 1081 1082
                          II16 -> panic "genCCall' passArguments II16"
                          II64 -> panic "genCCall' passArguments II64"
                          FF80 -> panic "genCCall' passArguments FF80"
1083

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
        moveResult reduceToFF32 =
            case dest_regs of
                [] -> nilOL
                [CmmHinted dest _hint]
                    | reduceToFF32 && isFloat32 rep   -> unitOL (FRSP r_dest f1)
                    | isFloat32 rep || isFloat64 rep -> unitOL (MR r_dest f1)
                    | isWord64 rep -> toOL [MR (getHiVRegFromLo r_dest) r3,
                                          MR r_dest r4]
                    | otherwise -> unitOL (MR r_dest r3)
                    where rep = cmmRegType (CmmLocal dest)
                          r_dest = getRegisterReg (CmmLocal dest)
1095
                _ -> panic "genCCall' moveResult: Bad dest_regs"
1096

1097
        outOfLineMachOp mop =
1098
            do
1099
                dflags <- getDynFlags
1100
                mopExpr <- cmmMakeDynamicReference dflags addImportNat CallReference $
1101
                              mkForeignLabel functionName Nothing ForeignLabelInThisPackage IsFunction
1102 1103 1104 1105 1106 1107 1108 1109 1110
                let mopLabelOrExpr = case mopExpr of
                        CmmLit (CmmLabel lbl) -> Left lbl
                        _ -> Right mopExpr
                return (mopLabelOrExpr, reduce)
            where
                (functionName, reduce) = case mop of
                    MO_F32_Exp   -> (fsLit "exp", True)
                    MO_F32_Log   -> (fsLit "log", True)
                    MO_F32_Sqrt  -> (fsLit "sqrt", True)
1111

1112 1113 1114
                    MO_F32_Sin   -> (fsLit "sin", True)
                    MO_F32_Cos   -> (fsLit "cos", True)
                    MO_F32_Tan   -> (fsLit "tan", True)