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