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