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"
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 | SplitMangle -- after mangler if splitting
82 | LlvmOpt -- Run LLVM opt tool over llvm assembly
83 | LlvmLlc -- LLVM bitcode to native assembly
84 | LlvmMangle -- Fix up TNTC by processing assembly produced by LLVM
85 | CmmCpp -- pre-process Cmm source
86 | Cmm -- parse & compile Cmm code
88 -- The final phase is a pseudo-phase that tells the pipeline to stop.
89 -- There is no runPhase case for it.
90 | StopLn -- Stop, but linking will follow, so generate .o file
93 instance Outputable Phase where
97 anyHsc = Hsc (panic "anyHsc")
99 isStopLn :: Phase -> Bool
100 isStopLn StopLn = True
103 eqPhase :: Phase -> Phase -> Bool
104 -- Equality of constructors, ignoring the HscSource field
105 -- NB: the HscSource field can be 'bot'; see anyHsc above
106 eqPhase (Unlit _) (Unlit _) = True
107 eqPhase (Cpp _) (Cpp _) = True
108 eqPhase (HsPp _) (HsPp _) = True
109 eqPhase (Hsc _) (Hsc _) = True
110 eqPhase Ccpp Ccpp = True
112 eqPhase HCc HCc = True
113 eqPhase SplitMangle SplitMangle = True
114 eqPhase SplitAs SplitAs = True
116 eqPhase LlvmOpt LlvmOpt = True
117 eqPhase LlvmLlc LlvmLlc = True
118 eqPhase LlvmMangle LlvmMangle = True
119 eqPhase CmmCpp CmmCpp = True
120 eqPhase Cmm Cmm = True
121 eqPhase StopLn StopLn = True
124 -- Partial ordering on phases: we want to know which phases will occur before
125 -- which others. This is used for sanity checking, to ensure that the
126 -- pipeline will stop at some point (see DriverPipeline.runPipeline).
127 happensBefore :: Phase -> Phase -> Bool
128 StopLn `happensBefore` _ = False
129 x `happensBefore` y = after_x `eqPhase` y || after_x `happensBefore` y
131 after_x = nextPhase x
133 nextPhase :: Phase -> Phase
134 -- A conservative approximation the next phase, used in happensBefore
135 nextPhase (Unlit sf) = Cpp sf
136 nextPhase (Cpp sf) = HsPp sf
137 nextPhase (HsPp sf) = Hsc sf
138 nextPhase (Hsc _) = HCc
139 nextPhase SplitMangle = As
140 nextPhase As = SplitAs
141 nextPhase LlvmOpt = LlvmLlc
143 nextPhase LlvmLlc = LlvmMangle
145 nextPhase LlvmLlc = As
147 nextPhase LlvmMangle = As
148 nextPhase SplitAs = StopLn
151 nextPhase CmmCpp = Cmm
154 nextPhase StopLn = panic "nextPhase: nothing after StopLn"
156 -- the first compilation phase for a given file is determined
158 startPhase :: String -> Phase
159 startPhase "lhs" = Unlit HsSrcFile
160 startPhase "lhs-boot" = Unlit HsBootFile
161 startPhase "hs" = Cpp HsSrcFile
162 startPhase "hs-boot" = Cpp HsBootFile
163 startPhase "hscpp" = HsPp HsSrcFile
164 startPhase "hspp" = Hsc HsSrcFile
165 startPhase "hcr" = Hsc ExtCoreFile
166 startPhase "hc" = HCc
168 startPhase "cpp" = Ccpp
170 startPhase "cc" = Ccpp
171 startPhase "cxx" = Ccpp
172 startPhase "split_s" = SplitMangle
175 startPhase "ll" = LlvmOpt
176 startPhase "bc" = LlvmLlc
177 startPhase "lm_s" = LlvmMangle
178 startPhase "o" = StopLn
179 startPhase "cmm" = CmmCpp
180 startPhase "cmmcpp" = Cmm
181 startPhase _ = StopLn -- all unknown file types
183 -- This is used to determine the extension for the output from the
184 -- current phase (if it generates a new file). The extension depends
185 -- on the next phase in the pipeline.
186 phaseInputExt :: Phase -> String
187 phaseInputExt (Unlit HsSrcFile) = "lhs"
188 phaseInputExt (Unlit HsBootFile) = "lhs-boot"
189 phaseInputExt (Unlit ExtCoreFile) = "lhcr"
190 phaseInputExt (Cpp _) = "lpp" -- intermediate only
191 phaseInputExt (HsPp _) = "hscpp" -- intermediate only
192 phaseInputExt (Hsc _) = "hspp" -- intermediate only
193 -- NB: as things stand, phaseInputExt (Hsc x) must not evaluate x
194 -- because runPipeline uses the StopBefore phase to pick the
195 -- output filename. That could be fixed, but watch out.
196 phaseInputExt HCc = "hc"
197 phaseInputExt Ccpp = "cpp"
198 phaseInputExt Cc = "c"
199 phaseInputExt SplitMangle = "split_s" -- not really generated
200 phaseInputExt As = "s"
201 phaseInputExt LlvmOpt = "ll"
202 phaseInputExt LlvmLlc = "bc"
203 phaseInputExt LlvmMangle = "lm_s"
204 phaseInputExt SplitAs = "split_s" -- not really generated
205 phaseInputExt CmmCpp = "cmm"
206 phaseInputExt Cmm = "cmmcpp"
207 phaseInputExt StopLn = "o"
209 haskellish_src_suffixes, haskellish_suffixes, cish_suffixes,
210 extcoreish_suffixes, haskellish_user_src_suffixes
212 haskellish_src_suffixes = haskellish_user_src_suffixes ++
213 [ "hspp", "hscpp", "hcr", "cmm", "cmmcpp" ]
214 haskellish_suffixes = haskellish_src_suffixes ++ ["hc", "raw_s"]
215 cish_suffixes = [ "c", "cpp", "C", "cc", "cxx", "s", "S", "ll", "bc" ]
216 extcoreish_suffixes = [ "hcr" ]
217 -- Will not be deleted as temp files:
218 haskellish_user_src_suffixes = [ "hs", "lhs", "hs-boot", "lhs-boot" ]
220 objish_suffixes :: [String]
221 -- Use the appropriate suffix for the system on which
222 -- the GHC-compiled code will run
223 #if mingw32_TARGET_OS || cygwin32_TARGET_OS
224 objish_suffixes = [ "o", "O", "obj", "OBJ" ]
226 objish_suffixes = [ "o" ]
229 dynlib_suffixes :: [String]
230 #ifdef mingw32_TARGET_OS
231 dynlib_suffixes = ["dll", "DLL"]
232 #elif defined(darwin_TARGET_OS)
233 dynlib_suffixes = ["dylib"]
235 dynlib_suffixes = ["so"]
238 isHaskellishSuffix, isHaskellSrcSuffix, isCishSuffix, isExtCoreSuffix,
239 isObjectSuffix, isHaskellUserSrcSuffix, isDynLibSuffix
241 isHaskellishSuffix s = s `elem` haskellish_suffixes
242 isHaskellSrcSuffix s = s `elem` haskellish_src_suffixes
243 isCishSuffix s = s `elem` cish_suffixes
244 isExtCoreSuffix s = s `elem` extcoreish_suffixes
245 isObjectSuffix s = s `elem` objish_suffixes
246 isHaskellUserSrcSuffix s = s `elem` haskellish_user_src_suffixes
247 isDynLibSuffix s = s `elem` dynlib_suffixes
249 isSourceSuffix :: String -> Bool
250 isSourceSuffix suff = isHaskellishSuffix suff || isCishSuffix suff
252 isHaskellishFilename, isHaskellSrcFilename, isCishFilename,
253 isExtCoreFilename, isObjectFilename, isHaskellUserSrcFilename,
254 isDynLibFilename, isSourceFilename
256 -- takeExtension return .foo, so we drop 1 to get rid of the .
257 isHaskellishFilename f = isHaskellishSuffix (drop 1 $ takeExtension f)
258 isHaskellSrcFilename f = isHaskellSrcSuffix (drop 1 $ takeExtension f)
259 isCishFilename f = isCishSuffix (drop 1 $ takeExtension f)
260 isExtCoreFilename f = isExtCoreSuffix (drop 1 $ takeExtension f)
261 isObjectFilename f = isObjectSuffix (drop 1 $ takeExtension f)
262 isHaskellUserSrcFilename f = isHaskellUserSrcSuffix (drop 1 $ takeExtension f)
263 isDynLibFilename f = isDynLibSuffix (drop 1 $ takeExtension f)
264 isSourceFilename f = isSourceSuffix (drop 1 $ takeExtension f)