root/compiler/main/DriverPhases.hs

Revision 3475561ee6210f8c570b5bbc47ed9355e435279f, 9.9 KB (checked in by David Terei <davidterei@…>, 6 months ago)

Tabs -> Spaces + cleaning

  • Property mode set to 100644
Line 
1-----------------------------------------------------------------------------
2--  $Id: DriverPhases.hs,v 1.38 2005/05/17 11:01:59 simonmar Exp $
3--
4-- GHC Driver
5--
6-- (c) The University of Glasgow 2002
7--
8-----------------------------------------------------------------------------
9
10module DriverPhases (
11   HscSource(..), isHsBoot, hscSourceString,
12   Phase(..),
13   happensBefore, eqPhase, anyHsc, isStopLn,
14   startPhase,
15   phaseInputExt,
16
17   isHaskellishSuffix,
18   isHaskellSrcSuffix,
19   isObjectSuffix,
20   isCishSuffix,
21   isExtCoreSuffix,
22   isDynLibSuffix,
23   isHaskellUserSrcSuffix,
24   isSourceSuffix,
25
26   isHaskellishFilename,
27   isHaskellSrcFilename,
28   isObjectFilename,
29   isCishFilename,
30   isExtCoreFilename,
31   isDynLibFilename,
32   isHaskellUserSrcFilename,
33   isSourceFilename
34 ) where
35
36#include "HsVersions.h"
37
38import Outputable
39import System.FilePath
40
41-----------------------------------------------------------------------------
42-- Phases
43
44{-
45   Phase of the           | Suffix saying | Flag saying   | (suffix of)
46   compilation system     | ``start here''| ``stop after''| output file
47
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
54-}
55
56data HscSource
57   = HsSrcFile | HsBootFile | ExtCoreFile
58     deriving( Eq, Ord, Show )
59        -- Ord needed for the finite maps we build in CompManager
60
61
62hscSourceString :: HscSource -> String
63hscSourceString HsSrcFile   = ""
64hscSourceString HsBootFile  = "[boot]"
65hscSourceString ExtCoreFile = "[ext core]"
66
67isHsBoot :: HscSource -> Bool
68isHsBoot HsBootFile = True
69isHsBoot _          = False
70
71data Phase
72        = Unlit HscSource
73        | Cpp   HscSource
74        | HsPp  HscSource
75        | Hsc   HscSource
76        | Ccpp
77        | Cc
78        | Cobjc
79        | Cobjcpp
80        | HCc           -- Haskellised C (as opposed to vanilla C) compilation
81        | Splitter      -- Assembly file splitter (part of '-split-objs')
82        | SplitAs       -- Assembler for split assembly files (part of '-split-objs')
83        | As            -- Assembler for regular assembly files
84        | LlvmOpt       -- Run LLVM opt tool over llvm assembly
85        | LlvmLlc       -- LLVM bitcode to native assembly
86        | LlvmMangle    -- Fix up TNTC by processing assembly produced by LLVM
87        | CmmCpp        -- pre-process Cmm source
88        | Cmm           -- parse & compile Cmm code
89        | MergeStub     -- merge in the stub object file
90
91        -- The final phase is a pseudo-phase that tells the pipeline to stop.
92        -- There is no runPhase case for it.
93        | StopLn        -- Stop, but linking will follow, so generate .o file
94  deriving (Eq, Show)
95
96instance Outputable Phase where
97    ppr p = text (show p)
98
99anyHsc :: Phase
100anyHsc = Hsc (panic "anyHsc")
101
102isStopLn :: Phase -> Bool
103isStopLn StopLn = True
104isStopLn _      = False
105
106eqPhase :: Phase -> Phase -> Bool
107-- Equality of constructors, ignoring the HscSource field
108-- NB: the HscSource field can be 'bot'; see anyHsc above
109eqPhase (Unlit _)   (Unlit _)  = True
110eqPhase (Cpp   _)   (Cpp   _)  = True
111eqPhase (HsPp  _)   (HsPp  _)  = True
112eqPhase (Hsc   _)   (Hsc   _)  = True
113eqPhase Ccpp        Ccpp       = True
114eqPhase Cc          Cc         = True
115eqPhase Cobjc       Cobjc      = True
116eqPhase Cobjcpp     Cobjcpp    = True
117eqPhase HCc         HCc        = True
118eqPhase Splitter    Splitter   = True
119eqPhase SplitAs     SplitAs    = True
120eqPhase As          As         = True
121eqPhase LlvmOpt     LlvmOpt    = True
122eqPhase LlvmLlc     LlvmLlc    = True
123eqPhase LlvmMangle  LlvmMangle = True
124eqPhase CmmCpp      CmmCpp     = True
125eqPhase Cmm         Cmm        = True
126eqPhase MergeStub   MergeStub  = True
127eqPhase StopLn      StopLn     = True
128eqPhase _           _          = False
129
130-- Partial ordering on phases: we want to know which phases will occur before
131-- which others.  This is used for sanity checking, to ensure that the
132-- pipeline will stop at some point (see DriverPipeline.runPipeline).
133happensBefore :: Phase -> Phase -> Bool
134StopLn `happensBefore` _ = False
135x      `happensBefore` y = after_x `eqPhase` y || after_x `happensBefore` y
136        where
137          after_x = nextPhase x
138
139nextPhase :: Phase -> Phase
140-- A conservative approximation to the next phase, used in happensBefore
141nextPhase (Unlit sf) = Cpp  sf
142nextPhase (Cpp   sf) = HsPp sf
143nextPhase (HsPp  sf) = Hsc  sf
144nextPhase (Hsc   _)  = HCc
145nextPhase Splitter   = SplitAs
146nextPhase LlvmOpt    = LlvmLlc
147nextPhase LlvmLlc    = LlvmMangle
148nextPhase LlvmMangle = As
149nextPhase SplitAs    = MergeStub
150nextPhase As         = MergeStub
151nextPhase Ccpp       = As
152nextPhase Cc         = As
153nextPhase Cobjc      = As
154nextPhase Cobjcpp    = As
155nextPhase CmmCpp     = Cmm
156nextPhase Cmm        = HCc
157nextPhase HCc        = As
158nextPhase MergeStub  = StopLn
159nextPhase StopLn     = panic "nextPhase: nothing after StopLn"
160
161-- the first compilation phase for a given file is determined
162-- by its suffix.
163startPhase :: String -> Phase
164startPhase "lhs"      = Unlit HsSrcFile
165startPhase "lhs-boot" = Unlit HsBootFile
166startPhase "hs"       = Cpp   HsSrcFile
167startPhase "hs-boot"  = Cpp   HsBootFile
168startPhase "hscpp"    = HsPp  HsSrcFile
169startPhase "hspp"     = Hsc   HsSrcFile
170startPhase "hcr"      = Hsc   ExtCoreFile
171startPhase "hc"       = HCc
172startPhase "c"        = Cc
173startPhase "cpp"      = Ccpp
174startPhase "C"        = Cc
175startPhase "m"        = Cobjc
176startPhase "M"        = Cobjcpp
177startPhase "mm"       = Cobjcpp
178startPhase "cc"       = Ccpp
179startPhase "cxx"      = Ccpp
180startPhase "split_s"  = Splitter
181startPhase "s"        = As
182startPhase "S"        = As
183startPhase "ll"       = LlvmOpt
184startPhase "bc"       = LlvmLlc
185startPhase "lm_s"     = LlvmMangle
186startPhase "o"        = StopLn
187startPhase "cmm"      = CmmCpp
188startPhase "cmmcpp"   = Cmm
189startPhase _          = StopLn     -- all unknown file types
190
191-- This is used to determine the extension for the output from the
192-- current phase (if it generates a new file).  The extension depends
193-- on the next phase in the pipeline.
194phaseInputExt :: Phase -> String
195phaseInputExt (Unlit HsSrcFile)   = "lhs"
196phaseInputExt (Unlit HsBootFile)  = "lhs-boot"
197phaseInputExt (Unlit ExtCoreFile) = "lhcr"
198phaseInputExt (Cpp   _)           = "lpp"       -- intermediate only
199phaseInputExt (HsPp  _)           = "hscpp"     -- intermediate only
200phaseInputExt (Hsc   _)           = "hspp"      -- intermediate only
201        -- NB: as things stand, phaseInputExt (Hsc x) must not evaluate x
202        --     because runPipeline uses the StopBefore phase to pick the
203        --     output filename.  That could be fixed, but watch out.
204phaseInputExt HCc                 = "hc"
205phaseInputExt Ccpp                = "cpp"
206phaseInputExt Cobjc               = "m"
207phaseInputExt Cobjcpp             = "mm"
208phaseInputExt Cc                  = "c"
209phaseInputExt Splitter            = "split_s"
210phaseInputExt As                  = "s"
211phaseInputExt LlvmOpt             = "ll"
212phaseInputExt LlvmLlc             = "bc"
213phaseInputExt LlvmMangle          = "lm_s"
214phaseInputExt SplitAs             = "split_s"
215phaseInputExt CmmCpp              = "cmm"
216phaseInputExt Cmm                 = "cmmcpp"
217phaseInputExt MergeStub           = "o"
218phaseInputExt StopLn              = "o"
219
220haskellish_src_suffixes, haskellish_suffixes, cish_suffixes,
221    extcoreish_suffixes, haskellish_user_src_suffixes
222 :: [String]
223haskellish_src_suffixes      = haskellish_user_src_suffixes ++
224                               [ "hspp", "hscpp", "hcr", "cmm", "cmmcpp" ]
225haskellish_suffixes          = haskellish_src_suffixes ++ ["hc", "raw_s"]
226cish_suffixes                = [ "c", "cpp", "C", "cc", "cxx", "s", "S", "ll", "bc", "lm_s", "m", "M", "mm" ]
227extcoreish_suffixes          = [ "hcr" ]
228-- Will not be deleted as temp files:
229haskellish_user_src_suffixes = [ "hs", "lhs", "hs-boot", "lhs-boot" ]
230
231objish_suffixes :: [String]
232-- Use the appropriate suffix for the system on which
233-- the GHC-compiled code will run
234#if mingw32_TARGET_OS || cygwin32_TARGET_OS
235objish_suffixes     = [ "o", "O", "obj", "OBJ" ]
236#else
237objish_suffixes     = [ "o" ]
238#endif
239
240dynlib_suffixes :: [String]
241#ifdef mingw32_TARGET_OS
242dynlib_suffixes = ["dll", "DLL"]
243#elif defined(darwin_TARGET_OS)
244dynlib_suffixes = ["dylib"]
245#else
246dynlib_suffixes = ["so"]
247#endif
248
249isHaskellishSuffix, isHaskellSrcSuffix, isCishSuffix, isExtCoreSuffix,
250    isObjectSuffix, isHaskellUserSrcSuffix, isDynLibSuffix
251 :: String -> Bool
252isHaskellishSuffix     s = s `elem` haskellish_suffixes
253isHaskellSrcSuffix     s = s `elem` haskellish_src_suffixes
254isCishSuffix           s = s `elem` cish_suffixes
255isExtCoreSuffix        s = s `elem` extcoreish_suffixes
256isObjectSuffix         s = s `elem` objish_suffixes
257isHaskellUserSrcSuffix s = s `elem` haskellish_user_src_suffixes
258isDynLibSuffix         s = s `elem` dynlib_suffixes
259
260isSourceSuffix :: String -> Bool
261isSourceSuffix suff  = isHaskellishSuffix suff || isCishSuffix suff
262
263isHaskellishFilename, isHaskellSrcFilename, isCishFilename,
264    isExtCoreFilename, isObjectFilename, isHaskellUserSrcFilename,
265    isDynLibFilename, isSourceFilename
266 :: FilePath -> Bool
267-- takeExtension return .foo, so we drop 1 to get rid of the .
268isHaskellishFilename     f = isHaskellishSuffix     (drop 1 $ takeExtension f)
269isHaskellSrcFilename     f = isHaskellSrcSuffix     (drop 1 $ takeExtension f)
270isCishFilename           f = isCishSuffix           (drop 1 $ takeExtension f)
271isExtCoreFilename        f = isExtCoreSuffix        (drop 1 $ takeExtension f)
272isObjectFilename         f = isObjectSuffix         (drop 1 $ takeExtension f)
273isHaskellUserSrcFilename f = isHaskellUserSrcSuffix (drop 1 $ takeExtension f)
274isDynLibFilename         f = isDynLibSuffix         (drop 1 $ takeExtension f)
275isSourceFilename         f = isSourceSuffix         (drop 1 $ takeExtension f)
Note: See TracBrowser for help on using the browser.