2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[GHC_Main]{Main driver for Glasgow Haskell compiler}
7 module Main ( main ) where
9 #include "HsVersions.h"
11 import IO ( hPutStr, stderr )
13 import BasicTypes ( NewOrData(..) )
15 import RdrHsSyn ( RdrNameHsModule )
16 import FastString ( mkFastCharString, unpackFS )
17 import StringBuffer ( hGetStringBuffer )
18 import Parser ( parse )
19 import Lex ( PState(..), P, ParseResult(..) )
20 import SrcLoc ( mkSrcLoc )
22 import Rename ( renameModule )
24 import MkIface ( startIface, ifaceDecls, endIface )
25 import TcModule ( TcResults(..), typecheckModule )
26 import Desugar ( deSugar )
27 import SimplCore ( core2core )
28 import CoreLint ( endPass )
29 import CoreTidy ( tidyCorePgm )
30 import CoreToStg ( topCoreBindsToStg )
31 import StgSyn ( collectFinalStgBinders, pprStgBindings )
32 import SimplStg ( stg2stg )
33 import CodeGen ( codeGen )
34 import CodeOutput ( codeOutput )
36 import Module ( ModuleName, moduleNameUserString )
37 import AbsCSyn ( absCNop )
39 import ErrUtils ( ghcExit, doIfSet, dumpIfSet )
40 import Maybes ( maybeToBool, MaybeErr(..) )
41 import TyCon ( isDataTyCon )
42 import Class ( classTyCon )
43 import UniqSupply ( mkSplitUniqSupply )
45 import FiniteMap ( emptyFM )
47 import Char ( isSpace )
48 #if REPORT_TO_MOTHERLODE && __GLASGOW_HASKELL__ >= 303
51 import IOExts ( unsafePerformIO )
52 import NativeInfo ( os, arch )
64 parseModule :: IO (ModuleName, RdrNameHsModule)
66 buf <- hGetStringBuffer True{-expand tabs-} (unpackFS src_filename)
67 case parse buf PState{ bol = 0#, atbol = 1#,
68 context = [], glasgow_exts = glaexts,
69 loc = mkSrcLoc src_filename 1 } of
74 return (error "parseModule") -- just to get the types right
76 POk _ m@(HsModule mod _ _ _ _ _) ->
79 glaexts | opt_GlasgowExts = 1#
84 doIt :: ([CoreToDo], [StgToDo]) -> IO ()
86 doIt (core_cmds, stg_cmds)
88 (hPutStr stderr "Glasgow Haskell Compiler, version " >>
89 hPutStr stderr compiler_version >>
90 hPutStr stderr ", for Haskell 98\n" >>
91 hPutStr stderr "\tcompiled by GHC version " >>
92 hPutStr stderr booter_version >>
93 hPutStr stderr "\n") >>
95 -------------------------- Reader ----------------
98 parseModule >>= \ (mod_name, rdr_module) ->
100 dumpIfSet opt_D_dump_parsed "Parser" (ppr rdr_module) >>
102 dumpIfSet opt_D_source_stats "Source Statistics"
103 (ppSourceStats False rdr_module) >>
105 -- UniqueSupplies for later use (these are the only lower case uniques)
106 mkSplitUniqSupply 'r' >>= \ rn_uniqs -> -- renamer
107 mkSplitUniqSupply 'a' >>= \ tc_uniqs -> -- typechecker
108 mkSplitUniqSupply 'd' >>= \ ds_uniqs -> -- desugarer
109 mkSplitUniqSupply 'r' >>= \ ru_uniqs -> -- rules
110 mkSplitUniqSupply 'c' >>= \ c2s_uniqs -> -- core-to-stg
111 mkSplitUniqSupply 'u' >>= \ tidy_uniqs -> -- tidy up
112 mkSplitUniqSupply 'g' >>= \ st_uniqs -> -- stg-to-stg passes
113 mkSplitUniqSupply 'n' >>= \ ncg_uniqs -> -- native-code generator
115 -------------------------- Rename ----------------
116 show_pass "Renamer" >>
119 renameModule rn_uniqs rdr_module >>= \ maybe_rn_stuff ->
120 case maybe_rn_stuff of {
121 Nothing -> -- Hurrah! Renamer reckons that there's no need to
123 reportCompile mod_name "Compilation NOT required!" >>
126 Just (this_mod, rn_mod, iface_file_stuff, rn_name_supply, imported_modules) ->
127 -- Oh well, we've got to recompile for real
130 -------------------------- Start interface file ----------------
131 -- Safely past renaming: we can start the interface file:
132 -- (the iface file is produced incrementally, as we have
133 -- the information that we need...; we use "iface<blah>")
134 -- "endIface" finishes the job.
135 startIface this_mod iface_file_stuff >>= \ if_handle ->
138 -------------------------- Typechecking ----------------
139 show_pass "TypeCheck" >>
141 typecheckModule tc_uniqs rn_name_supply
142 iface_file_stuff rn_mod >>= \ maybe_tc_stuff ->
143 case maybe_tc_stuff of {
144 Nothing -> ghcExit 1; -- Type checker failed
146 Just (tc_results@(TcResults {tc_tycons = local_tycons,
147 tc_classes = local_classes,
148 tc_insts = inst_info })) ->
151 -------------------------- Desugaring ----------------
153 deSugar this_mod ds_uniqs tc_results >>= \ (desugared, rules, h_code, c_code) ->
156 -------------------------- Main Core-language transformations ----------------
158 core2core core_cmds desugared rules >>= \ (simplified, imp_rule_ids) ->
160 -- Do the final tidy-up
161 tidyCorePgm tidy_uniqs this_mod
162 simplified imp_rule_ids >>= \ (tidy_binds, tidy_imp_rule_ids) ->
165 -------------------------- Convert to STG code -------------------------------
166 show_pass "Core2Stg" >>
169 stg_binds = topCoreBindsToStg c2s_uniqs tidy_binds
172 -------------------------- Simplify STG code -------------------------------
173 show_pass "Stg2Stg" >>
175 stg2stg stg_cmds this_mod st_uniqs stg_binds >>= \ (stg_binds2, cost_centre_info) ->
178 -------------------------- Interface file -------------------------------
179 -- Dump instance decls and type signatures into the interface file
182 final_ids = collectFinalStgBinders (map fst stg_binds2)
184 ifaceDecls if_handle local_tycons local_classes
185 inst_info final_ids tidy_binds imp_rule_ids >>
186 endIface if_handle >>
187 -- We are definitely done w/ interface-file stuff at this point:
188 -- (See comments near call to "startIface".)
191 -------------------------- Code generation -------------------------------
192 show_pass "CodeGen" >>
194 codeGen this_mod imported_modules
196 local_tycons local_classes
197 stg_binds2 >>= \ abstractC ->
200 -------------------------- Code output -------------------------------
201 show_pass "CodeOutput" >>
203 codeOutput this_mod c_code h_code abstractC ncg_uniqs >>
206 -------------------------- Final report -------------------------------
207 reportCompile mod_name (showSDoc (ppSourceStats True rdr_module)) >>
212 -------------------------------------------------------------
213 -- ****** help functions:
216 = if opt_D_show_passes
217 then \ what -> hPutStr stderr ("*** "++what++":\n")
218 else \ what -> return ()
220 ppSourceStats short (HsModule name version exports imports decls src_loc)
221 = (if short then hcat else vcat)
223 [("ExportAll ", export_all), -- 1 if no export list
224 ("ExportDecls ", export_ds),
225 ("ExportModules ", export_ms),
226 ("Imports ", import_no),
227 (" ImpQual ", import_qual),
228 (" ImpAs ", import_as),
229 (" ImpAll ", import_all),
230 (" ImpPartial ", import_partial),
231 (" ImpHiding ", import_hiding),
232 ("FixityDecls ", fixity_ds),
233 ("DefaultDecls ", default_ds),
234 ("TypeDecls ", type_ds),
235 ("DataDecls ", data_ds),
236 ("NewTypeDecls ", newt_ds),
237 ("DataConstrs ", data_constrs),
238 ("DataDerivings ", data_derivs),
239 ("ClassDecls ", class_ds),
240 ("ClassMethods ", class_method_ds),
241 ("DefaultMethods ", default_method_ds),
242 ("InstDecls ", inst_ds),
243 ("InstMethods ", inst_method_ds),
244 ("TypeSigs ", bind_tys),
245 ("ValBinds ", val_bind_ds),
246 ("FunBinds ", fn_bind_ds),
247 ("InlineMeths ", method_inlines),
248 ("InlineBinds ", bind_inlines),
249 -- ("SpecialisedData ", data_specs),
250 -- ("SpecialisedInsts ", inst_specs),
251 ("SpecialisedMeths ", method_specs),
252 ("SpecialisedBinds ", bind_specs)
255 pp_val (str, 0) = empty
257 | not short = hcat [text str, int n]
258 | otherwise = hcat [text (trim str), equals, int n, semi]
260 trim ls = takeWhile (not.isSpace) (dropWhile isSpace ls)
262 fixity_ds = length [() | FixD d <- decls]
263 -- NB: this omits fixity decls on local bindings and
264 -- in class decls. ToDo
266 tycl_decls = [d | TyClD d <- decls]
267 (class_ds, data_ds, newt_ds, type_ds) = countTyClDecls tycl_decls
269 inst_decls = [d | InstD d <- decls]
270 inst_ds = length inst_decls
271 default_ds = length [() | DefD _ <- decls]
272 val_decls = [d | ValD d <- decls]
274 real_exports = case exports of { Nothing -> []; Just es -> es }
275 n_exports = length real_exports
276 export_ms = length [() | IEModuleContents _ <- real_exports]
277 export_ds = n_exports - export_ms
278 export_all = case exports of { Nothing -> 1; other -> 0 }
280 (val_bind_ds, fn_bind_ds, bind_tys, bind_specs, bind_inlines)
281 = count_binds (foldr ThenBinds EmptyBinds val_decls)
283 (import_no, import_qual, import_as, import_all, import_partial, import_hiding)
284 = foldr add6 (0,0,0,0,0,0) (map import_info imports)
285 (data_constrs, data_derivs)
286 = foldr add2 (0,0) (map data_info tycl_decls)
287 (class_method_ds, default_method_ds)
288 = foldr add2 (0,0) (map class_info tycl_decls)
289 (inst_method_ds, method_specs, method_inlines)
290 = foldr add3 (0,0,0) (map inst_info inst_decls)
293 count_binds EmptyBinds = (0,0,0,0,0)
294 count_binds (ThenBinds b1 b2) = count_binds b1 `add5` count_binds b2
295 count_binds (MonoBind b sigs _) = case (count_monobinds b, count_sigs sigs) of
296 ((vs,fs),(ts,_,ss,is)) -> (vs,fs,ts,ss,is)
298 count_monobinds EmptyMonoBinds = (0,0)
299 count_monobinds (AndMonoBinds b1 b2) = count_monobinds b1 `add2` count_monobinds b2
300 count_monobinds (PatMonoBind (VarPatIn n) r _) = (1,0)
301 count_monobinds (PatMonoBind p r _) = (0,1)
302 count_monobinds (FunMonoBind f _ m _) = (0,1)
304 count_sigs sigs = foldr add4 (0,0,0,0) (map sig_info sigs)
306 sig_info (Sig _ _ _) = (1,0,0,0)
307 sig_info (ClassOpSig _ _ _ _) = (0,1,0,0)
308 sig_info (SpecSig _ _ _) = (0,0,1,0)
309 sig_info (InlineSig _ _) = (0,0,0,1)
310 sig_info _ = (0,0,0,0)
312 import_info (ImportDecl _ _ qual as spec _)
313 = add6 (1, qual_info qual, as_info as, 0,0,0) (spec_info spec)
318 spec_info Nothing = (0,0,0,1,0,0)
319 spec_info (Just (False, _)) = (0,0,0,0,1,0)
320 spec_info (Just (True, _)) = (0,0,0,0,0,1)
322 data_info (TyData _ _ _ _ constrs derivs _ _)
323 = (length constrs, case derivs of {Nothing -> 0; Just ds -> length ds})
324 data_info other = (0,0)
326 class_info (ClassDecl _ _ _ meth_sigs def_meths _ _ _ _ _)
327 = case count_sigs meth_sigs of
329 (classops, addpr (count_monobinds def_meths))
330 class_info other = (0,0)
332 inst_info (InstDecl _ inst_meths inst_sigs _ _)
333 = case count_sigs inst_sigs of
335 (addpr (count_monobinds inst_meths), ss, is)
337 addpr :: (Int,Int) -> Int
338 add1 :: Int -> Int -> Int
339 add2 :: (Int,Int) -> (Int,Int) -> (Int, Int)
340 add3 :: (Int,Int,Int) -> (Int,Int,Int) -> (Int, Int, Int)
341 add4 :: (Int,Int,Int,Int) -> (Int,Int,Int,Int) -> (Int, Int, Int, Int)
342 add5 :: (Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int)
343 add6 :: (Int,Int,Int,Int,Int,Int) -> (Int,Int,Int,Int,Int,Int) -> (Int, Int, Int, Int, Int, Int)
347 add2 (x1,x2) (y1,y2) = (x1+y1,x2+y2)
348 add3 (x1,x2,x3) (y1,y2,y3) = (x1+y1,x2+y2,x3+y3)
349 add4 (x1,x2,x3,x4) (y1,y2,y3,y4) = (x1+y1,x2+y2,x3+y3,x4+y4)
350 add5 (x1,x2,x3,x4,x5) (y1,y2,y3,y4,y5) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5)
351 add6 (x1,x2,x3,x4,x5,x6) (y1,y2,y3,y4,y5,y6) = (x1+y1,x2+y2,x3+y3,x4+y4,x5+y5,x6+y6)
355 compiler_version :: String
357 case (show opt_HiVersion) of
359 ls@[x,y] -> "0." ++ ls
362 -- 10232353 => 10232.53
368 \ __GLASGOW_HASKELL__" of
374 reportCompile :: ModuleName -> String -> IO ()
375 #if REPORT_TO_MOTHERLODE && __GLASGOW_HASKELL__ >= 303
376 reportCompile mod_name info
377 | not opt_ReportCompile = return ()
381 sendTo sock (moduleNameUserString mod_name ++ ';': compiler_version ++
382 ';': os ++ ';':arch ++ '\n':' ':info ++ "\n") addr
383 return ()) `catch` (\ _ -> return ())
385 motherShip :: IO SockAddr
387 he <- getHostByName "laysan.dcs.gla.ac.uk"
388 case (hostAddresses he) of
389 [] -> IOERROR (userError "No address!")
390 (x:_) -> return (SockAddrInet motherShipPort x)
393 motherShipPort :: PortNumber
394 motherShipPort = mkPortNumber 12345
396 -- creates a socket capable of sending datagrams,
397 -- binding it to a port
398 -- ( 0 => have the system pick next available port no.)
399 udpSocket :: Int -> IO Socket
401 pr <- getProtocolNumber "udp"
402 s <- socket AF_INET Datagram pr
403 bindSocket s (SockAddrInet (mkPortNumber p) iNADDR_ANY)
406 reportCompile _ _ = return ()