<div dir="ltr">Hello,<div><br></div><div>I am bringing back that thread to life after a while.</div><div><br></div><div>I've spent some time on studding GHC source code related with that topic.</div><div>There are some conclusions I have made:</div><div><ul><li>In my first email, I thought that execution of function <span style="font-size:12.8px"><i>compileCoreToObj </i>in my program </span>produced some *.o file. I was wrong. That file was produced by earlier step, that is execution of <i>compileToCoreModule </i>function. <i style="font-size:12.8px">compileCoreToObj </i><span style="font-size:12.8px">has generated only *.hi file because exception has been raised.</span></li><li><span style="font-size:12.8px">I have found a way to produce valid interface file<br></span></li><li><span style="font-size:12.8px">I have discovered, why </span><i style="font-size:12.8px">compileCoreToObj </i><span style="font-size:12.8px">raises exceptions. It was because <i>stubDir</i> field in <i>DynFlags </i>structure has to be set for proper execution.</span></li><li><span style="font-size:12.8px">Artifacts produced by </span><i style="font-size:12.8px">compileCoreToObj </i><span style="font-size:12.8px">for a non-executable module </span><span style="font-size:12.8px">are: interface file and assembly output.</span></li></ul><div><span style="font-size:12.8px">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.</span></div></div><div><span style="font-size:12.8px">Code used for my tests is as follows:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">A.hs:</span><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><blockquote style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex" class="gmail_quote"></blockquote></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="font-family:monospace,monospace;font-size:12.8px">import GHC<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import DynFlags<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import GHC.Paths<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import qualified Debug.Trace as T<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import Panic<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import HscMain<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import HscTypes<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import Name<br></span><span style="font-family:monospace,monospace;font-size:12.8px">import UniqFM<br></span><span style="font-family:monospace,monospace;font-size:12.8px">main = do<br></span><span style="font-family:monospace,monospace;font-size:12.8px">    core <- getCore<br></span><span style="font-family:monospace,monospace;font-size:12.8px">    getLine<br></span><span style="font-family:monospace,monospace;font-size:12.8px">    compile HscAsm "foo" core</span><font face="monospace, monospace"><span style="font-size:12.8px"><br></span></font><span style="font-family:monospace,monospace;font-size:12.8px">getCore = defaultErrorHandler defaultFatalMessager defaultFlushOut<br></span><span style="font-family:monospace,monospace;font-size:12.8px">    $ runGhc (Just libdir) $ do<br></span><span style="font-family:monospace,monospace;font-size:12.8px">        df <- getSessionDynFlags<br></span><span style="font-family:monospace,monospace;font-size:12.8px">        setSessionDynFlags df<br></span><span style="font-family:monospace,monospace;font-size:12.8px">        core <- compileToCoreModule "B.hs"<br></span><span style="font-family:monospace,monospace;font-size:12.8px">        return core<br></span><span style="font-family:monospace,monospace;font-size:12.8px">compile target output_fn core = defaultErrorHandler defaultFatalMessager defaultFlushOut<br></span><span style="font-family:monospace,monospace;font-size:12.8px">    $ runGhc (Just libdir) $ do<br></span><span style="font-family:monospace,monospace;font-size:12.8px">        df <- getSessionDynFlags<br></span><span style="font-size:12.8px;font-family:monospace,monospace">        setSessionDynFlags df { hscTarget = target , stubDir = Just "./stub" }</span></blockquote><div><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote><blockquote style="margin:0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;border-right-width:1px;border-right-color:rgb(204,204,204);border-right-style:solid;padding-left:1ex;padding-right:1ex" class="gmail_quote"></blockquote></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="monospace, monospace"><span style="font-size:12.8px">        compileCoreToObj False core output_fn "bar"</span></font></blockquote><div> </div><div><div style="font-size:12.8px">B.hs:</div></div><div><blockquote style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex" class="gmail_quote"><span style="font-size:12.8px">module B (foo) where</span><span style="font-size:12.8px"><br></span><span style="font-size:12.8px">foo ::  a -> a<br></span><span style="font-size:12.8px">foo = id</span><span style="font-size:12.8px"><br></span><span style="font-size:12.8px">bar :: Eq a => a -> a -> Bool<br></span><span style="font-size:12.8px">bar = (==)</span></blockquote><div> </div><div style="font-size:12.8px"><i>getCore </i>and <i>compile</i> are separated to make sure, no flags set by the first function were used by the second  one.</div><div style="font-size:12.8px"><i>getLine </i>is used here to stop an execution of test for cleaning output directory form files generated by <i>getCore</i>.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Is there a way to get an object file as a result of <i>compileCoreToObj</i>?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Regards,</div><div style="font-size:12.8px">MWawrzos</div></div><div><span style="font-size:12.8px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-10-08 19:02 GMT+02:00 Edward Z. Yang <span dir="ltr"><<a href="mailto:ezyang@mit.edu" target="_blank">ezyang@mit.edu</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If we look at the source code for hscCompileCore, it would seem that it<br>
creates a ModGuts with mkModGuts which has an empty mg_exports, which<br>
means that in all cases there will be no exported items.  So it's not<br>
very useful!  You should file a bug.<br>
<br>
We should fix this, but in the mean time, you could simply copypaste the<br>
definition of hscCompileCore, and modify it so that you do set up<br>
the ModGuts correctly with a real mg_exports field.<br>
<br>
Edward<br>
<br>
Excerpts from Marek Wawrzos's message of 2015-10-08 07:22:01 -0700:<br>
<div class="HOEnZb"><div class="h5">> Hello,<br>
><br>
> I'm trying to compile some module with GHC API. I'm going to feed GHC with<br>
> Core generated by me.<br>
><br>
> So far is noticed, that function compileCoreToObj ends with an runtime<br>
> error on each call. In spide of this function produces some *.o and *.hi<br>
> files. However, produced interface file does not contain any item in<br>
> exports field, what makes compiled module unusable.<br>
><br>
> Because of this problem, I tried to achieve my goal by using steps<br>
> described here: <a href="https://wiki.haskell.org/GHC/As_a_library" rel="noreferrer" target="_blank">https://wiki.haskell.org/GHC/As_a_library</a> (the second<br>
> example)<br>
> I was able to print out Core produced form file. If I'm not wrong,<br>
> modifying structures passing on between sequent calls does not effect on<br>
> produced *.hi and *.o files. Both files are generated by `load<br>
> LoadAllTargets' call, and further calls (parseModule, loadModule, ...) does<br>
> not effect on generated files.<br>
><br>
> Could You give me any advices in that topic? Am I right about<br>
> compileCoreToObj and functions mentioned above?<br>
><br>
> Best Regards,<br>
> Marek<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr">Z poważaniem,<div>Marek Wawrzos</div></div></div>
</div>