Fwd: Compiling Core Haskell using GHC API

Marek Wawrzos marek.28.93 at gmail.com
Sat Feb 20 08:39:55 UTC 2016


I am bringing back that thread to life after a while.

I've spent some time on studding GHC source code related with that topic.
There are some conclusions I have made:

   - In my first email, I thought that execution of function
   *compileCoreToObj *in my program produced some *.o file. I was wrong.
   That file was produced by earlier step, that is execution of
   *function. *compileCoreToObj *has generated only *.hi file because
   exception has been raised.
   - I have found a way to produce valid interface file
   - I have discovered, why *compileCoreToObj *raises exceptions. It was
   because *stubDir* field in *DynFlags *structure has to be set for proper
   - Artifacts produced by *compileCoreToObj *for a non-executable module are:
   interface file and assembly output.

The last point is my main problem for now. I supposed that object file will
be generated, as it is generated by GHC executed on haskell module.
Code used for my tests is as follows:


import GHC
> import DynFlags
> import GHC.Paths
> import qualified Debug.Trace as T
> import Panic
> import HscMain
> import HscTypes
> import Name
> import UniqFM
> main = do
>     core <- getCore
>     getLine
>     compile HscAsm "foo" core
> getCore = defaultErrorHandler defaultFatalMessager defaultFlushOut
>     $ runGhc (Just libdir) $ do
>         df <- getSessionDynFlags
>         setSessionDynFlags df
>         core <- compileToCoreModule "B.hs"
>         return core
> compile target output_fn core = defaultErrorHandler defaultFatalMessager
> defaultFlushOut
>     $ runGhc (Just libdir) $ do
>         df <- getSessionDynFlags
>         setSessionDynFlags df { hscTarget = target , stubDir = Just
> "./stub" }

        compileCoreToObj False core output_fn "bar"


> module B (foo) where
> foo ::  a -> a
> foo = id
> bar :: Eq a => a -> a -> Bool
> bar = (==)

*getCore *and *compile* are separated to make sure, no flags set by the
first function were used by the second  one.
*getLine *is used here to stop an execution of test for cleaning output
directory form files generated by *getCore*.

Is there a way to get an object file as a result of *compileCoreToObj*?


2015-10-08 19:02 GMT+02:00 Edward Z. Yang <ezyang at mit.edu>:

> If we look at the source code for hscCompileCore, it would seem that it
> creates a ModGuts with mkModGuts which has an empty mg_exports, which
> means that in all cases there will be no exported items.  So it's not
> very useful!  You should file a bug.
> We should fix this, but in the mean time, you could simply copypaste the
> definition of hscCompileCore, and modify it so that you do set up
> the ModGuts correctly with a real mg_exports field.
> Edward
> Excerpts from Marek Wawrzos's message of 2015-10-08 07:22:01 -0700:
> > Hello,
> >
> > I'm trying to compile some module with GHC API. I'm going to feed GHC
> with
> > Core generated by me.
> >
> > So far is noticed, that function compileCoreToObj ends with an runtime
> > error on each call. In spide of this function produces some *.o and *.hi
> > files. However, produced interface file does not contain any item in
> > exports field, what makes compiled module unusable.
> >
> > Because of this problem, I tried to achieve my goal by using steps
> > described here: https://wiki.haskell.org/GHC/As_a_library (the second
> > example)
> > I was able to print out Core produced form file. If I'm not wrong,
> > modifying structures passing on between sequent calls does not effect on
> > produced *.hi and *.o files. Both files are generated by `load
> > LoadAllTargets' call, and further calls (parseModule, loadModule, ...)
> does
> > not effect on generated files.
> >
> > Could You give me any advices in that topic? Am I right about
> > compileCoreToObj and functions mentioned above?
> >
> > Best Regards,
> > Marek

Z poważaniem,
Marek Wawrzos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20160220/b63a0fbd/attachment-0001.html>

More information about the ghc-devs mailing list