Add dump flag for timing output
[ghc.git] / compiler / main / DriverPhases.hs
1 {-# LANGUAGE CPP #-}
2
3 -----------------------------------------------------------------------------
4 -- $Id: DriverPhases.hs,v 1.38 2005/05/17 11:01:59 simonmar Exp $
5 --
6 -- GHC Driver
7 --
8 -- (c) The University of Glasgow 2002
9 --
10 -----------------------------------------------------------------------------
11
12 module DriverPhases (
13 HscSource(..), isHsBootOrSig, hscSourceString,
14 Phase(..),
15 happensBefore, eqPhase, anyHsc, isStopLn,
16 startPhase,
17 phaseInputExt,
18
19 isHaskellishSuffix,
20 isHaskellSrcSuffix,
21 isBackpackishSuffix,
22 isObjectSuffix,
23 isCishSuffix,
24 isDynLibSuffix,
25 isHaskellUserSrcSuffix,
26 isHaskellSigSuffix,
27 isSourceSuffix,
28
29 isHaskellishTarget,
30
31 isHaskellishFilename,
32 isHaskellSrcFilename,
33 isHaskellSigFilename,
34 isObjectFilename,
35 isCishFilename,
36 isDynLibFilename,
37 isHaskellUserSrcFilename,
38 isSourceFilename
39 ) where
40
41 #include "HsVersions.h"
42
43 import GhcPrelude
44
45 import {-# SOURCE #-} DynFlags
46 import Outputable
47 import Platform
48 import System.FilePath
49 import Binary
50 import Util
51
52 -----------------------------------------------------------------------------
53 -- Phases
54
55 {-
56 Phase of the | Suffix saying | Flag saying | (suffix of)
57 compilation system | ``start here''| ``stop after''| output file
58
59 literate pre-processor | .lhs | - | -
60 C pre-processor (opt.) | - | -E | -
61 Haskell compiler | .hs | -C, -S | .hc, .s
62 C compiler (opt.) | .hc or .c | -S | .s
63 assembler | .s or .S | -c | .o
64 linker | other | - | a.out
65 -}
66
67 -- Note [HscSource types]
68 -- ~~~~~~~~~~~~~~~~~~~~~~
69 -- There are three types of source file for Haskell code:
70 --
71 -- * HsSrcFile is an ordinary hs file which contains code,
72 --
73 -- * HsBootFile is an hs-boot file, which is used to break
74 -- recursive module imports (there will always be an
75 -- HsSrcFile associated with it), and
76 --
77 -- * HsigFile is an hsig file, which contains only type
78 -- signatures and is used to specify signatures for
79 -- modules.
80 --
81 -- Syntactically, hs-boot files and hsig files are quite similar: they
82 -- only include type signatures and must be associated with an
83 -- actual HsSrcFile. isHsBootOrSig allows us to abstract over code
84 -- which is indifferent to which. However, there are some important
85 -- differences, mostly owing to the fact that hsigs are proper
86 -- modules (you `import Sig` directly) whereas HsBootFiles are
87 -- temporary placeholders (you `import {-# SOURCE #-} Mod).
88 -- When we finish compiling the true implementation of an hs-boot,
89 -- we replace the HomeModInfo with the real HsSrcFile. An HsigFile, on the
90 -- other hand, is never replaced (in particular, we *cannot* use the
91 -- HomeModInfo of the original HsSrcFile backing the signature, since it
92 -- will export too many symbols.)
93 --
94 -- Additionally, while HsSrcFile is the only Haskell file
95 -- which has *code*, we do generate .o files for HsigFile, because
96 -- this is how the recompilation checker figures out if a file
97 -- needs to be recompiled. These are fake object files which
98 -- should NOT be linked against.
99
100 data HscSource
101 = HsSrcFile | HsBootFile | HsigFile
102 deriving( Eq, Ord, Show )
103 -- Ord needed for the finite maps we build in CompManager
104
105 instance Binary HscSource where
106 put_ bh HsSrcFile = putByte bh 0
107 put_ bh HsBootFile = putByte bh 1
108 put_ bh HsigFile = putByte bh 2
109 get bh = do
110 h <- getByte bh
111 case h of
112 0 -> return HsSrcFile
113 1 -> return HsBootFile
114 _ -> return HsigFile
115
116 hscSourceString :: HscSource -> String
117 hscSourceString HsSrcFile = ""
118 hscSourceString HsBootFile = "[boot]"
119 hscSourceString HsigFile = "[sig]"
120
121 -- See Note [isHsBootOrSig]
122 isHsBootOrSig :: HscSource -> Bool
123 isHsBootOrSig HsBootFile = True
124 isHsBootOrSig HsigFile = True
125 isHsBootOrSig _ = False
126
127 data Phase
128 = Unlit HscSource
129 | Cpp HscSource
130 | HsPp HscSource
131 | Hsc HscSource
132 | Ccxx -- Compile C++
133 | Cc -- Compile C
134 | Cobjc -- Compile Objective-C
135 | Cobjcxx -- Compile Objective-C++
136 | HCc -- Haskellised C (as opposed to vanilla C) compilation
137 | Splitter -- Assembly file splitter (part of '-split-objs')
138 | SplitAs -- Assembler for split assembly files (part of '-split-objs')
139 | As Bool -- Assembler for regular assembly files (Bool: with-cpp)
140 | LlvmOpt -- Run LLVM opt tool over llvm assembly
141 | LlvmLlc -- LLVM bitcode to native assembly
142 | LlvmMangle -- Fix up TNTC by processing assembly produced by LLVM
143 | CmmCpp -- pre-process Cmm source
144 | Cmm -- parse & compile Cmm code
145 | MergeForeign -- merge in the foreign object files
146
147 -- The final phase is a pseudo-phase that tells the pipeline to stop.
148 -- There is no runPhase case for it.
149 | StopLn -- Stop, but linking will follow, so generate .o file
150 deriving (Eq, Show)
151
152 instance Outputable Phase where
153 ppr p = text (show p)
154
155 anyHsc :: Phase
156 anyHsc = Hsc (panic "anyHsc")
157
158 isStopLn :: Phase -> Bool
159 isStopLn StopLn = True
160 isStopLn _ = False
161
162 eqPhase :: Phase -> Phase -> Bool
163 -- Equality of constructors, ignoring the HscSource field
164 -- NB: the HscSource field can be 'bot'; see anyHsc above
165 eqPhase (Unlit _) (Unlit _) = True
166 eqPhase (Cpp _) (Cpp _) = True
167 eqPhase (HsPp _) (HsPp _) = True
168 eqPhase (Hsc _) (Hsc _) = True
169 eqPhase Cc Cc = True
170 eqPhase Cobjc Cobjc = True
171 eqPhase HCc HCc = True
172 eqPhase Splitter Splitter = True
173 eqPhase SplitAs SplitAs = True
174 eqPhase (As x) (As y) = x == y
175 eqPhase LlvmOpt LlvmOpt = True
176 eqPhase LlvmLlc LlvmLlc = True
177 eqPhase LlvmMangle LlvmMangle = True
178 eqPhase CmmCpp CmmCpp = True
179 eqPhase Cmm Cmm = True
180 eqPhase MergeForeign MergeForeign = True
181 eqPhase StopLn StopLn = True
182 eqPhase Ccxx Ccxx = True
183 eqPhase Cobjcxx Cobjcxx = True
184 eqPhase _ _ = False
185
186 {- Note [Partial ordering on phases]
187
188 We want to know which phases will occur before which others. This is used for
189 sanity checking, to ensure that the pipeline will stop at some point (see
190 DriverPipeline.runPipeline).
191
192 A < B iff A occurs before B in a normal compilation pipeline.
193
194 There is explicitly not a total ordering on phases, because in registerised
195 builds, the phase `HsC` doesn't happen before nor after any other phase.
196
197 Although we check that a normal user doesn't set the stop_phase to HsC through
198 use of -C with registerised builds (in Main.checkOptions), it is still
199 possible for a ghc-api user to do so. So be careful when using the function
200 happensBefore, and don't think that `not (a <= b)` implies `b < a`.
201 -}
202 happensBefore :: DynFlags -> Phase -> Phase -> Bool
203 happensBefore dflags p1 p2 = p1 `happensBefore'` p2
204 where StopLn `happensBefore'` _ = False
205 x `happensBefore'` y = after_x `eqPhase` y
206 || after_x `happensBefore'` y
207 where after_x = nextPhase dflags x
208
209 nextPhase :: DynFlags -> Phase -> Phase
210 nextPhase dflags p
211 -- A conservative approximation to the next phase, used in happensBefore
212 = case p of
213 Unlit sf -> Cpp sf
214 Cpp sf -> HsPp sf
215 HsPp sf -> Hsc sf
216 Hsc _ -> maybeHCc
217 Splitter -> SplitAs
218 LlvmOpt -> LlvmLlc
219 LlvmLlc -> LlvmMangle
220 LlvmMangle -> As False
221 SplitAs -> MergeForeign
222 As _ -> MergeForeign
223 Ccxx -> As False
224 Cc -> As False
225 Cobjc -> As False
226 Cobjcxx -> As False
227 CmmCpp -> Cmm
228 Cmm -> maybeHCc
229 HCc -> As False
230 MergeForeign -> StopLn
231 StopLn -> panic "nextPhase: nothing after StopLn"
232 where maybeHCc = if platformUnregisterised (targetPlatform dflags)
233 then HCc
234 else As False
235
236 -- the first compilation phase for a given file is determined
237 -- by its suffix.
238 startPhase :: String -> Phase
239 startPhase "lhs" = Unlit HsSrcFile
240 startPhase "lhs-boot" = Unlit HsBootFile
241 startPhase "lhsig" = Unlit HsigFile
242 startPhase "hs" = Cpp HsSrcFile
243 startPhase "hs-boot" = Cpp HsBootFile
244 startPhase "hsig" = Cpp HsigFile
245 startPhase "hscpp" = HsPp HsSrcFile
246 startPhase "hspp" = Hsc HsSrcFile
247 startPhase "hc" = HCc
248 startPhase "c" = Cc
249 startPhase "cpp" = Ccxx
250 startPhase "C" = Cc
251 startPhase "m" = Cobjc
252 startPhase "M" = Cobjcxx
253 startPhase "mm" = Cobjcxx
254 startPhase "cc" = Ccxx
255 startPhase "cxx" = Ccxx
256 startPhase "split_s" = Splitter
257 startPhase "s" = As False
258 startPhase "S" = As True
259 startPhase "ll" = LlvmOpt
260 startPhase "bc" = LlvmLlc
261 startPhase "lm_s" = LlvmMangle
262 startPhase "o" = StopLn
263 startPhase "cmm" = CmmCpp
264 startPhase "cmmcpp" = Cmm
265 startPhase _ = StopLn -- all unknown file types
266
267 -- This is used to determine the extension for the output from the
268 -- current phase (if it generates a new file). The extension depends
269 -- on the next phase in the pipeline.
270 phaseInputExt :: Phase -> String
271 phaseInputExt (Unlit HsSrcFile) = "lhs"
272 phaseInputExt (Unlit HsBootFile) = "lhs-boot"
273 phaseInputExt (Unlit HsigFile) = "lhsig"
274 phaseInputExt (Cpp _) = "lpp" -- intermediate only
275 phaseInputExt (HsPp _) = "hscpp" -- intermediate only
276 phaseInputExt (Hsc _) = "hspp" -- intermediate only
277 -- NB: as things stand, phaseInputExt (Hsc x) must not evaluate x
278 -- because runPipeline uses the StopBefore phase to pick the
279 -- output filename. That could be fixed, but watch out.
280 phaseInputExt HCc = "hc"
281 phaseInputExt Ccxx = "cpp"
282 phaseInputExt Cobjc = "m"
283 phaseInputExt Cobjcxx = "mm"
284 phaseInputExt Cc = "c"
285 phaseInputExt Splitter = "split_s"
286 phaseInputExt (As True) = "S"
287 phaseInputExt (As False) = "s"
288 phaseInputExt LlvmOpt = "ll"
289 phaseInputExt LlvmLlc = "bc"
290 phaseInputExt LlvmMangle = "lm_s"
291 phaseInputExt SplitAs = "split_s"
292 phaseInputExt CmmCpp = "cmm"
293 phaseInputExt Cmm = "cmmcpp"
294 phaseInputExt MergeForeign = "o"
295 phaseInputExt StopLn = "o"
296
297 haskellish_src_suffixes, backpackish_suffixes, haskellish_suffixes, cish_suffixes,
298 haskellish_user_src_suffixes, haskellish_sig_suffixes
299 :: [String]
300 -- When a file with an extension in the haskellish_src_suffixes group is
301 -- loaded in --make mode, its imports will be loaded too.
302 haskellish_src_suffixes = haskellish_user_src_suffixes ++
303 [ "hspp", "hscpp" ]
304 haskellish_suffixes = haskellish_src_suffixes ++
305 [ "hc", "cmm", "cmmcpp" ]
306 cish_suffixes = [ "c", "cpp", "C", "cc", "cxx", "s", "S", "ll", "bc", "lm_s", "m", "M", "mm" ]
307
308 -- Will not be deleted as temp files:
309 haskellish_user_src_suffixes =
310 haskellish_sig_suffixes ++ [ "hs", "lhs", "hs-boot", "lhs-boot" ]
311 haskellish_sig_suffixes = [ "hsig", "lhsig" ]
312 backpackish_suffixes = [ "bkp" ]
313
314 objish_suffixes :: Platform -> [String]
315 -- Use the appropriate suffix for the system on which
316 -- the GHC-compiled code will run
317 objish_suffixes platform = case platformOS platform of
318 OSMinGW32 -> [ "o", "O", "obj", "OBJ" ]
319 _ -> [ "o" ]
320
321 dynlib_suffixes :: Platform -> [String]
322 dynlib_suffixes platform = case platformOS platform of
323 OSMinGW32 -> ["dll", "DLL"]
324 OSDarwin -> ["dylib", "so"]
325 _ -> ["so"]
326
327 isHaskellishSuffix, isBackpackishSuffix, isHaskellSrcSuffix, isCishSuffix,
328 isHaskellUserSrcSuffix, isHaskellSigSuffix
329 :: String -> Bool
330 isHaskellishSuffix s = s `elem` haskellish_suffixes
331 isBackpackishSuffix s = s `elem` backpackish_suffixes
332 isHaskellSigSuffix s = s `elem` haskellish_sig_suffixes
333 isHaskellSrcSuffix s = s `elem` haskellish_src_suffixes
334 isCishSuffix s = s `elem` cish_suffixes
335 isHaskellUserSrcSuffix s = s `elem` haskellish_user_src_suffixes
336
337 isObjectSuffix, isDynLibSuffix :: Platform -> String -> Bool
338 isObjectSuffix platform s = s `elem` objish_suffixes platform
339 isDynLibSuffix platform s = s `elem` dynlib_suffixes platform
340
341 isSourceSuffix :: String -> Bool
342 isSourceSuffix suff = isHaskellishSuffix suff
343 || isCishSuffix suff
344 || isBackpackishSuffix suff
345
346 -- | When we are given files (modified by -x arguments) we need
347 -- to determine if they are Haskellish or not to figure out
348 -- how we should try to compile it. The rules are:
349 --
350 -- 1. If no -x flag was specified, we check to see if
351 -- the file looks like a module name, has no extension,
352 -- or has a Haskell source extension.
353 --
354 -- 2. If an -x flag was specified, we just make sure the
355 -- specified suffix is a Haskell one.
356 isHaskellishTarget :: (String, Maybe Phase) -> Bool
357 isHaskellishTarget (f,Nothing) =
358 looksLikeModuleName f || isHaskellSrcFilename f || not (hasExtension f)
359 isHaskellishTarget (_,Just phase) =
360 phase `notElem` [ As True, As False, Cc, Cobjc, Cobjcxx, CmmCpp, Cmm
361 , StopLn]
362
363 isHaskellishFilename, isHaskellSrcFilename, isCishFilename,
364 isHaskellUserSrcFilename, isSourceFilename, isHaskellSigFilename
365 :: FilePath -> Bool
366 -- takeExtension return .foo, so we drop 1 to get rid of the .
367 isHaskellishFilename f = isHaskellishSuffix (drop 1 $ takeExtension f)
368 isHaskellSrcFilename f = isHaskellSrcSuffix (drop 1 $ takeExtension f)
369 isCishFilename f = isCishSuffix (drop 1 $ takeExtension f)
370 isHaskellUserSrcFilename f = isHaskellUserSrcSuffix (drop 1 $ takeExtension f)
371 isSourceFilename f = isSourceSuffix (drop 1 $ takeExtension f)
372 isHaskellSigFilename f = isHaskellSigSuffix (drop 1 $ takeExtension f)
373
374 isObjectFilename, isDynLibFilename :: Platform -> FilePath -> Bool
375 isObjectFilename platform f = isObjectSuffix platform (drop 1 $ takeExtension f)
376 isDynLibFilename platform f = isDynLibSuffix platform (drop 1 $ takeExtension f)
377