+ -- Dunno if the above comment is still meaningful now. JRS 001024.
+
+ do { when (dopt Opt_DoCmmLinting dflags) $ do
+ { showPass dflags "CmmLint"
+ ; let lints = map cmmLint flat_abstractC
+ ; case firstJust lints of
+ Just err -> do { printDump err
+ ; ghcExit 1
+ }
+ Nothing -> return ()
+ }
+
+ ; showPass dflags "CodeOutput"
+ ; let filenm = dopt_OutName dflags
+ ; stubs_exist <- outputForeignStubs dflags foreign_stubs
+ ; case dopt_HscLang dflags of {
+ HscInterpreted -> return ();
+ HscAsm -> outputAsm dflags filenm flat_abstractC;
+ HscC -> outputC dflags filenm flat_abstractC stubs_exist
+ deps foreign_stubs;
+ HscJava ->
+#ifdef JAVA
+ outputJava dflags filenm mod_name tycons core_binds;
+#else
+ panic "Java support not compiled into this ghc";
+#endif
+ HscILX ->
+#ifdef ILX
+ let tycons = typeEnvTyCons type_env in
+ outputIlx dflags filenm mod_name tycons stg_binds;
+#else
+ panic "ILX support not compiled into this ghc";
+#endif
+ }
+ ; return stubs_exist
+ }
+
+doOutput :: String -> (Handle -> IO ()) -> IO ()
+doOutput filenm io_action = bracket (openFile filenm WriteMode) hClose io_action
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{C}
+%* *
+%************************************************************************
+
+\begin{code}
+outputC dflags filenm flat_absC
+ (stub_h_exists, _) dependencies foreign_stubs
+ = do
+ -- figure out which header files to #include in the generated .hc file:
+ --
+ -- * extra_includes from packages
+ -- * -#include options from the cmdline and OPTIONS pragmas
+ -- * the _stub.h file, if there is one.
+ --
+ let packages = dep_pkgs dependencies
+ pkg_configs <- getExplicitPackagesAnd packages
+ let pkg_names = map name pkg_configs
+
+ c_includes <- getPackageCIncludes pkg_configs
+ let cmdline_includes = cmdlineHcIncludes dflags -- -#include options
+
+ ffi_decl_headers
+ = case foreign_stubs of
+ NoStubs -> []
+ ForeignStubs _ _ fdhs _ -> map unpackFS (fst (removeDupsEq fdhs))
+ -- Remove duplicates, because distinct foreign import decls
+ -- may cite the same #include. Order doesn't matter.
+
+ all_headers = c_includes
+ ++ reverse cmdline_includes
+ ++ ffi_decl_headers
+
+ let cc_injects = unlines (map mk_include all_headers)
+ mk_include h_file =
+ case h_file of
+ '"':_{-"-} -> "#include "++h_file
+ '<':_ -> "#include "++h_file
+ _ -> "#include \""++h_file++"\""
+
+ doOutput filenm $ \ h -> do
+ hPutStr h ("/* GHC_PACKAGES " ++ unwords pkg_names ++ "\n*/\n")
+ hPutStr h cc_injects
+ when stub_h_exists $
+ hPutStrLn h ("#include \"" ++ (hscStubHOutName dflags) ++ "\"")
+ writeCs h flat_absC
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Assembler}
+%* *
+%************************************************************************
+
+\begin{code}
+outputAsm dflags filenm flat_absC
+
+#ifndef OMIT_NATIVE_CODEGEN
+
+ = do ncg_uniqs <- mkSplitUniqSupply 'n'
+ ncg_output_d <- _scc_ "NativeCodeGen"
+ nativeCodeGen dflags flat_absC ncg_uniqs
+ dumpIfSet_dyn dflags Opt_D_dump_asm "Asm code" (docToSDoc ncg_output_d)
+ _scc_ "OutputAsm" doOutput filenm $
+ \f -> printDoc LeftMode f ncg_output_d
+ where
+
+#else /* OMIT_NATIVE_CODEGEN */
+
+ = pprPanic "This compiler was built without a native code generator"
+ (text "Use -fvia-C instead")
+
+#endif
+\end{code}