1 -----------------------------------------------------------------------------
2 -- $Id: DriverPhases.hs,v 1.38 2005/05/17 11:01:59 simonmar Exp $
6 -- (c) The University of Glasgow 2002
8 -----------------------------------------------------------------------------
11 HscSource(..), isHsBoot, hscSourceString,
13 happensBefore, eqPhase, anyHsc, isStopLn,
14 startPhase, -- :: String -> Phase
15 phaseInputExt, -- :: Phase -> String
23 isHaskellUserSrcSuffix,
32 isHaskellUserSrcFilename,
33 isSourceFilename -- :: FilePath -> Bool
36 #include "HsVersions.h"
38 import Panic ( panic )
39 import System.FilePath
41 -----------------------------------------------------------------------------
45 Phase of the | Suffix saying | Flag saying | (suffix of)
46 compilation system | ``start here''| ``stop after''| output file
48 literate pre-processor | .lhs | - | -
49 C pre-processor (opt.) | - | -E | -
50 Haskell compiler | .hs | -C, -S | .hc, .s
51 C compiler (opt.) | .hc or .c | -S | .s
52 assembler | .s or .S | -c | .o
53 linker | other | - | a.out
57 = HsSrcFile | HsBootFile | ExtCoreFile
58 deriving( Eq, Ord, Show )
59 -- Ord needed for the finite maps we build in CompManager
62 hscSourceString :: HscSource -> String
63 hscSourceString HsSrcFile = ""
64 hscSourceString HsBootFile = "[boot]"
65 hscSourceString ExtCoreFile = "[ext core]"
67 isHsBoot :: HscSource -> Bool
68 isHsBoot HsBootFile = True
78 | HCc -- Haskellised C (as opposed to vanilla C) compilation
79 | Mangle -- assembly mangling, now done by a separate script.
80 | SplitMangle -- after mangler if splitting
83 | LlvmAs -- LLVM assembly to bitcode file
84 | LlvmOpt -- Run LLVM opt tool over llvm assembly
85 | LlvmLlc -- LLVM bitcode to native assembly
86 | CmmCpp -- pre-process Cmm source
87 | Cmm -- parse & compile Cmm code
89 -- The final phase is a pseudo-phase that tells the pipeline to stop.
90 -- There is no runPhase case for it.
91 | StopLn -- Stop, but linking will follow, so generate .o file
95 anyHsc = Hsc (panic "anyHsc")
97 isStopLn :: Phase -> Bool
98 isStopLn StopLn = True
101 eqPhase :: Phase -> Phase -> Bool
102 -- Equality of constructors, ignoring the HscSource field
103 -- NB: the HscSource field can be 'bot'; see anyHsc above
104 eqPhase (Unlit _) (Unlit _) = True
105 eqPhase (Cpp _) (Cpp _) = True
106 eqPhase (HsPp _) (HsPp _) = True
107 eqPhase (Hsc _) (Hsc _) = True
108 eqPhase Ccpp Ccpp = True
110 eqPhase HCc HCc = True
111 eqPhase Mangle Mangle = True
112 eqPhase SplitMangle SplitMangle = True
113 eqPhase SplitAs SplitAs = True
115 eqPhase LlvmAs LlvmAs = True
116 eqPhase LlvmOpt LlvmOpt = True
117 eqPhase LlvmLlc LlvmLlc = True
118 eqPhase CmmCpp CmmCpp = True
119 eqPhase Cmm Cmm = True
120 eqPhase StopLn StopLn = True
123 -- Partial ordering on phases: we want to know which phases will occur before
124 -- which others. This is used for sanity checking, to ensure that the
125 -- pipeline will stop at some point (see DriverPipeline.runPipeline).
126 happensBefore :: Phase -> Phase -> Bool
127 StopLn `happensBefore` _ = False
128 x `happensBefore` y = after_x `eqPhase` y || after_x `happensBefore` y
130 after_x = nextPhase x
132 nextPhase :: Phase -> Phase
133 -- A conservative approximation the next phase, used in happensBefore
134 nextPhase (Unlit sf) = Cpp sf
135 nextPhase (Cpp sf) = HsPp sf
136 nextPhase (HsPp sf) = Hsc sf
137 nextPhase (Hsc _) = HCc
138 nextPhase HCc = Mangle
139 nextPhase Mangle = SplitMangle
140 nextPhase SplitMangle = As
141 nextPhase As = SplitAs
142 nextPhase LlvmAs = LlvmOpt
143 nextPhase LlvmOpt = LlvmLlc
144 nextPhase LlvmLlc = As
145 nextPhase SplitAs = StopLn
148 nextPhase CmmCpp = Cmm
150 nextPhase StopLn = panic "nextPhase: nothing after StopLn"
152 -- the first compilation phase for a given file is determined
154 startPhase :: String -> Phase
155 startPhase "lhs" = Unlit HsSrcFile
156 startPhase "lhs-boot" = Unlit HsBootFile
157 startPhase "hs" = Cpp HsSrcFile
158 startPhase "hs-boot" = Cpp HsBootFile
159 startPhase "hscpp" = HsPp HsSrcFile
160 startPhase "hspp" = Hsc HsSrcFile
161 startPhase "hcr" = Hsc ExtCoreFile
162 startPhase "hc" = HCc
164 startPhase "cpp" = Ccpp
166 startPhase "cc" = Ccpp
167 startPhase "cxx" = Ccpp
168 startPhase "raw_s" = Mangle
169 startPhase "split_s" = SplitMangle
172 startPhase "ll" = LlvmAs
173 startPhase "bc" = LlvmOpt
174 startPhase "opt_bc" = LlvmLlc
175 startPhase "o" = StopLn
176 startPhase "cmm" = CmmCpp
177 startPhase "cmmcpp" = Cmm
178 startPhase _ = StopLn -- all unknown file types
180 -- This is used to determine the extension for the output from the
181 -- current phase (if it generates a new file). The extension depends
182 -- on the next phase in the pipeline.
183 phaseInputExt :: Phase -> String
184 phaseInputExt (Unlit HsSrcFile) = "lhs"
185 phaseInputExt (Unlit HsBootFile) = "lhs-boot"
186 phaseInputExt (Unlit ExtCoreFile) = "lhcr"
187 phaseInputExt (Cpp _) = "lpp" -- intermediate only
188 phaseInputExt (HsPp _) = "hscpp" -- intermediate only
189 phaseInputExt (Hsc _) = "hspp" -- intermediate only
190 -- NB: as things stand, phaseInputExt (Hsc x) must not evaluate x
191 -- because runPipeline uses the StopBefore phase to pick the
192 -- output filename. That could be fixed, but watch out.
193 phaseInputExt HCc = "hc"
194 phaseInputExt Ccpp = "cpp"
195 phaseInputExt Cc = "c"
196 phaseInputExt Mangle = "raw_s"
197 phaseInputExt SplitMangle = "split_s" -- not really generated
198 phaseInputExt As = "s"
199 phaseInputExt LlvmAs = "ll"
200 phaseInputExt LlvmOpt = "bc"
201 phaseInputExt LlvmLlc = "opt_bc"
202 phaseInputExt SplitAs = "split_s" -- not really generated
203 phaseInputExt CmmCpp = "cmm"
204 phaseInputExt Cmm = "cmmcpp"
205 phaseInputExt StopLn = "o"
207 haskellish_src_suffixes, haskellish_suffixes, cish_suffixes,
208 extcoreish_suffixes, haskellish_user_src_suffixes
210 haskellish_src_suffixes = haskellish_user_src_suffixes ++
211 [ "hspp", "hscpp", "hcr", "cmm" ]
212 haskellish_suffixes = haskellish_src_suffixes ++ ["hc", "raw_s"]
213 cish_suffixes = [ "c", "cpp", "C", "cc", "cxx", "s", "S", "ll", "bc", "opt_bc" ]
214 extcoreish_suffixes = [ "hcr" ]
215 -- Will not be deleted as temp files:
216 haskellish_user_src_suffixes = [ "hs", "lhs", "hs-boot", "lhs-boot" ]
218 objish_suffixes :: [String]
219 -- Use the appropriate suffix for the system on which
220 -- the GHC-compiled code will run
221 #if mingw32_TARGET_OS || cygwin32_TARGET_OS
222 objish_suffixes = [ "o", "O", "obj", "OBJ" ]
224 objish_suffixes = [ "o" ]
227 dynlib_suffixes :: [String]
228 #ifdef mingw32_TARGET_OS
229 dynlib_suffixes = ["dll", "DLL"]
230 #elif defined(darwin_TARGET_OS)
231 dynlib_suffixes = ["dylib"]
233 dynlib_suffixes = ["so"]
236 isHaskellishSuffix, isHaskellSrcSuffix, isCishSuffix, isExtCoreSuffix,
237 isObjectSuffix, isHaskellUserSrcSuffix, isDynLibSuffix
239 isHaskellishSuffix s = s `elem` haskellish_suffixes
240 isHaskellSrcSuffix s = s `elem` haskellish_src_suffixes
241 isCishSuffix s = s `elem` cish_suffixes
242 isExtCoreSuffix s = s `elem` extcoreish_suffixes
243 isObjectSuffix s = s `elem` objish_suffixes
244 isHaskellUserSrcSuffix s = s `elem` haskellish_user_src_suffixes
245 isDynLibSuffix s = s `elem` dynlib_suffixes
247 isSourceSuffix :: String -> Bool
248 isSourceSuffix suff = isHaskellishSuffix suff || isCishSuffix suff
250 isHaskellishFilename, isHaskellSrcFilename, isCishFilename,
251 isExtCoreFilename, isObjectFilename, isHaskellUserSrcFilename,
252 isDynLibFilename, isSourceFilename
254 -- takeExtension return .foo, so we drop 1 to get rid of the .
255 isHaskellishFilename f = isHaskellishSuffix (drop 1 $ takeExtension f)
256 isHaskellSrcFilename f = isHaskellSrcSuffix (drop 1 $ takeExtension f)
257 isCishFilename f = isCishSuffix (drop 1 $ takeExtension f)
258 isExtCoreFilename f = isExtCoreSuffix (drop 1 $ takeExtension f)
259 isObjectFilename f = isObjectSuffix (drop 1 $ takeExtension f)
260 isHaskellUserSrcFilename f = isHaskellUserSrcSuffix (drop 1 $ takeExtension f)
261 isDynLibFilename f = isDynLibSuffix (drop 1 $ takeExtension f)
262 isSourceFilename f = isSourceSuffix (drop 1 $ takeExtension f)