LlvmCodeGen.hs 5.17 KB
Newer Older
1 2 3 4
-- -----------------------------------------------------------------------------
-- | This is the top-level module in the LLVM code generator.
--

5
module LlvmCodeGen ( llvmCodeGen, llvmFixupAsm ) where
6 7 8

#include "HsVersions.h"

dterei's avatar
dterei committed
9
import Llvm
10 11 12 13
import LlvmCodeGen.Base
import LlvmCodeGen.CodeGen
import LlvmCodeGen.Data
import LlvmCodeGen.Ppr
14 15
import LlvmMangler

16
import CgUtils ( fixStgRegisters )
17 18
import OldCmm
import OldPprCmm
19 20 21 22

import BufWrite
import DynFlags
import ErrUtils
dterei's avatar
dterei committed
23
import FastString
24 25 26
import Outputable
import qualified Pretty as Prt
import UniqSupply
dterei's avatar
dterei committed
27
import Util
28
import SysTools ( figureLlvmVersion )
29

30
import Data.Maybe ( fromMaybe )
31 32 33
import System.IO

-- -----------------------------------------------------------------------------
dterei's avatar
dterei committed
34
-- | Top-level of the LLVM Code generator
35
--
Simon Peyton Jones's avatar
Simon Peyton Jones committed
36
llvmCodeGen :: DynFlags -> Handle -> UniqSupply -> [RawCmmGroup] -> IO ()
37
llvmCodeGen dflags h us cmms
38
  = let cmm = concat cmms
39
        (cdata,env) = {-# SCC "llvm_split" #-}
40
                      foldr split ([], initLlvmEnv dflags) cmm
41 42
        split (CmmData s d' ) (d,e) = ((s,d'):d,e)
        split (CmmProc i l _) (d,e) =
43
            let lbl = strCLabel_llvm env $ case i of
44 45
                        Nothing                   -> l
                        Just (Statics info_lbl _) -> info_lbl
46 47
                env' = funInsert lbl llvmFunTy e
            in (d,env')
48
    in do
49
        showPass dflags "LlVM CodeGen"
50
        bufh <- newBufHandle h
51
        dumpIfSet_dyn dflags Opt_D_dump_llvm "LLVM Code" $ docToSDoc pprLlvmHeader
52
        Prt.bufLeftRender bufh $ pprLlvmHeader
53
        ver  <- (fromMaybe defaultLlvmVersion) `fmap` figureLlvmVersion dflags
dterei's avatar
dterei committed
54 55
        env' <- {-# SCC "llvm_datas_gen" #-}
                cmmDataLlvmGens dflags bufh (setLlvmVer ver env) cdata []
56
        {-# SCC "llvm_procs_gen" #-}
dterei's avatar
dterei committed
57
             cmmProcLlvmGens dflags bufh us env' cmm 1 []
58 59
        bFlush bufh
        return  ()
60 61 62


-- -----------------------------------------------------------------------------
dterei's avatar
dterei committed
63
-- | Do LLVM code generation on all these Cmms data sections.
64
--
65
cmmDataLlvmGens :: DynFlags -> BufHandle -> LlvmEnv -> [(Section,CmmStatics)]
66
                -> [LlvmUnresData] -> IO ( LlvmEnv )
67

68
cmmDataLlvmGens dflags h env [] lmdata
dterei's avatar
dterei committed
69
  = let (env', lmdata') = {-# SCC "llvm_resolve" #-}
70
                          resolveLlvmDatas env lmdata
dterei's avatar
dterei committed
71 72
        lmdoc = {-# SCC "llvm_data_ppr" #-}
                Prt.vcat $ map pprLlvmData lmdata'
73
    in do
74
        dumpIfSet_dyn dflags Opt_D_dump_llvm "LLVM Code" $ docToSDoc lmdoc
75 76
        {-# SCC "llvm_data_out" #-}
            Prt.bufLeftRender h lmdoc
77 78
        return env'

79
cmmDataLlvmGens dflags h env (cmm:cmms) lmdata
80 81 82 83 84 85 86
  = let lm@(l, _, ty, _) = {-# SCC "llvm_data_gen" #-}
                           genLlvmData env cmm
        env' = {-# SCC "llvm_data_insert" #-}
               funInsert (strCLabel_llvm env l) ty env
        lmdata' = {-# SCC "llvm_data_append" #-}
                  lm:lmdata
    in cmmDataLlvmGens dflags h env' cmms lmdata'
87 88 89


-- -----------------------------------------------------------------------------
dterei's avatar
dterei committed
90
-- | Do LLVM code generation on all these Cmms procs.
91
--
Simon Peyton Jones's avatar
Simon Peyton Jones committed
92
cmmProcLlvmGens :: DynFlags -> BufHandle -> UniqSupply -> LlvmEnv -> [RawCmmDecl]
93 94
      -> Int         -- ^ count, used for generating unique subsections
      -> [[LlvmVar]] -- ^ info tables that need to be marked as 'used'
95 96
      -> IO ()

dterei's avatar
dterei committed
97 98
cmmProcLlvmGens _ _ _ _ [] _ []
  = return ()
99

100
cmmProcLlvmGens _ h _ _ [] _ ivars
101 102 103 104
  = let ivars' = concat ivars
        cast x = LMBitc (LMStaticPointer (pVarLift x)) i8Ptr
        ty     = (LMArray (length ivars') i8Ptr)
        usedArray = LMStaticArray (map cast ivars') ty
105
        lmUsed = (LMGlobalVar (fsLit "llvm.used") ty Appending
106
                  (Just $ fsLit "llvm.metadata") Nothing False, Just usedArray)
107
    in Prt.bufLeftRender h $ {-# SCC "llvm_used_ppr" #-}
dterei's avatar
dterei committed
108
                             pprLlvmData ([lmUsed], [])
dterei's avatar
dterei committed
109

110 111
cmmProcLlvmGens dflags h us env ((CmmData _ _) : cmms) count ivars
 = cmmProcLlvmGens dflags h us env cmms count ivars
112

113 114 115 116 117
cmmProcLlvmGens dflags h us env ((CmmProc _ _ (ListGraph [])) : cmms) count ivars
 = cmmProcLlvmGens dflags h us env cmms count ivars

cmmProcLlvmGens dflags h us env (cmm : cmms) count ivars = do
    (us', env', llvm) <- cmmLlvmGen dflags us (clearVars env) cmm
Simon Peyton Jones's avatar
Simon Peyton Jones committed
118
    let (docs, ivar) = mapAndUnzip (pprLlvmCmmDecl env' count) llvm
dterei's avatar
dterei committed
119
    Prt.bufLeftRender h $ {-# SCC "llvm_proc_ppr" #-} Prt.vcat docs
120
    cmmProcLlvmGens dflags h us' env' cmms (count + 2) (ivar ++ ivars)
121 122


dterei's avatar
dterei committed
123
-- | Complete LLVM code generation phase for a single top-level chunk of Cmm.
Simon Peyton Jones's avatar
Simon Peyton Jones committed
124 125
cmmLlvmGen :: DynFlags -> UniqSupply -> LlvmEnv -> RawCmmDecl
            -> IO ( UniqSupply, LlvmEnv, [LlvmCmmDecl] )
126
cmmLlvmGen dflags us env cmm = do
127
    -- rewrite assignments to global regs
dterei's avatar
dterei committed
128 129
    let fixed_cmm = {-# SCC "llvm_fix_regs" #-}
                    fixStgRegisters cmm
130 131

    dumpIfSet_dyn dflags Opt_D_dump_opt_cmm "Optimised Cmm"
Simon Peyton Jones's avatar
Simon Peyton Jones committed
132
        (pprCmmGroup (targetPlatform dflags) [fixed_cmm])
133 134

    -- generate llvm code from cmm
dterei's avatar
dterei committed
135 136
    let ((env', llvmBC), usGen) = {-# SCC "llvm_proc_gen" #-}
                                  initUs us $ genLlvmProc env fixed_cmm
137 138

    dumpIfSet_dyn dflags Opt_D_dump_llvm "LLVM Code"
Simon Peyton Jones's avatar
Simon Peyton Jones committed
139
        (vcat $ map (docToSDoc . fst . pprLlvmCmmDecl env' 0) llvmBC)
140 141 142

    return (usGen, env', llvmBC)