<div dir="ltr">Woah, that's pretty neat. I didn't even realize that GHCi could do that. From a quick perusal of the GHCi docs, it looks like :print and :force are related commands. How is GHCi used to show the type of a closure though?</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 6, 2019 at 5:29 PM Simon Peyton Jones <<a href="mailto:simonpj@microsoft.com">simonpj@microsoft.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="EN-GB">
<div class="gmail-m_8752588313164823668WordSection1">
<p class="MsoNormal"><span>You might also find compiler/ghci/RtClosureInspect.hs useful.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>It’s the main module of the GHCi debugger, and allows Haskell code to inspect the Haskell heap.    So it has mechanisms for unpacking arbitrary closures, all in Haskell.<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>It’s tough going though.   It was written a long time ago by Pepe Iborra (in a GSoC project), and has not received much love since.  Maybe you could be a source of love?!<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<p class="MsoNormal"><span>Simon<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0cm 0cm 0cm 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Libraries <<a href="mailto:libraries-bounces@haskell.org" target="_blank">libraries-bounces@haskell.org</a>>
<b>On Behalf Of </b>Andrew Martin<br>
<b>Sent:</b> 06 February 2019 20:57<br>
<b>To:</b> Matthew Pickering <<a href="mailto:matthewtpickering@gmail.com" target="_blank">matthewtpickering@gmail.com</a>><br>
<b>Cc:</b> Haskell Libraries <<a href="mailto:libraries@haskell.org" target="_blank">libraries@haskell.org</a>><br>
<b>Subject:</b> Re: Peeking into StgInfoTable in cmm<u></u><u></u></span></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Or even better, it looks like cmmGetClosureType already combines the two for me.<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Wed, Feb 6, 2019 at 3:49 PM Andrew Martin <<a href="mailto:andrew.thaddeus@gmail.com" target="_blank">andrew.thaddeus@gmail.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Thanks. The source code of ghc-heap has led me to what I was looking for. That whole library hinges upon unpackClosure# (stg_unpackClosurezh), whose implementation opens with the telling:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">    info  = %GET_STD_INFO(UNTAG(closure));<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">In CmmParse.y, we find the implementation of GET_STD_INFO, and just a few lines below is the INFO_TYPE macro. The combination of these (along with a cmm implementation of UNTAG, which I am still unsure of exactly how to accomplish) should
 do the trick.<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Wed, Feb 6, 2019 at 2:53 PM Matthew Pickering <<a href="mailto:matthewtpickering@gmail.com" target="_blank">matthewtpickering@gmail.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal">Unless I am misunderstanding you want something which works like ghc-heap.<br>
<br>
If so, perhaps these three files will be instructive?<br>
<br>
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fblob%2Fmaster%2Flibraries%2Fghc-heap%2FGHC%2FExts%2FHeap%2FInfoTable.hsc&data=02%7C01%7Csimonpj%40microsoft.com%7C06915c8ff0cf44831aa208d68c75bb48%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636850834616930861&sdata=7AOF4PAMQw7jFWNo0t6Hq4tWiT%2FOscQ40xm25gag6Ys%3D&reserved=0" target="_blank">https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTable.hsc</a><br>
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fblob%2Fmaster%2Flibraries%2Fghc-heap%2FGHC%2FExts%2FHeap%2FInfoTableProf.hsc&data=02%7C01%7Csimonpj%40microsoft.com%7C06915c8ff0cf44831aa208d68c75bb48%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636850834616930861&sdata=YzGRBEBOVYFNdAQdfEk2wZqJ7t3vJW7VFs0B%2FfNY7ik%3D&reserved=0" target="_blank">https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/InfoTableProf.hsc</a><br>
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgitlab.haskell.org%2Fghc%2Fghc%2Fblob%2Fmaster%2Flibraries%2Fghc-heap%2FGHC%2FExts%2FHeap%2FClosures.hs%23L25&data=02%7C01%7Csimonpj%40microsoft.com%7C06915c8ff0cf44831aa208d68c75bb48%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636850834616940859&sdata=%2FrzmmYNnEBkPeFPMiwaerspjuOxJKI4B519Ev041zN4%3D&reserved=0" target="_blank">https://gitlab.haskell.org/ghc/ghc/blob/master/libraries/ghc-heap/GHC/Exts/Heap/Closures.hs#L25</a><br>
<br>
Cheers,<br>
<br>
Matt<br>
<br>
On Wed, Feb 6, 2019 at 7:29 PM Andrew Martin <<a href="mailto:andrew.thaddeus@gmail.com" target="_blank">andrew.thaddeus@gmail.com</a>> wrote:<br>
><br>
> For an optimization I’m implementing, I’m trying to write an inline primop that gives me lets me inspect the closure type of an arbitrary heap-allocated value. I’m close to figuring this out, and the FCode machinery is starting to make sense, but one thing
 eludes me. Starting from a StgHeader, I get the info table pointer. This is easy since it’s the first field in the struct. But next I have to deference the info table pointer and get the closure type. There are two fields at the beginning of an info table
 that may or may not be present. I don’t know how to figure out if they are present or not. I suspect that there is something I can get out of DynFlags that tells me this, but I can’t find anything else in the stg-to-cmm pass that reads from an info table.<br>
><br>
> Sent from my iPhone<br>
> _______________________________________________<br>
> Libraries mailing list<br>
> <a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
> <a href="https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Flibraries&data=02%7C01%7Csimonpj%40microsoft.com%7C06915c8ff0cf44831aa208d68c75bb48%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636850834616940859&sdata=7%2Fc3qcuTEGeUjd9lG%2FXYVnb3bZ8%2B61jEf4XidnEgfUw%3D&reserved=0" target="_blank">
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">-- <u></u><u></u></p>
<div>
<p class="MsoNormal">-Andrew Thaddeus Martin<u></u><u></u></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">-- <u></u><u></u></p>
<div>
<p class="MsoNormal">-Andrew Thaddeus Martin<u></u><u></u></p>
</div>
</div>
</div>
</div>

</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">-Andrew Thaddeus Martin</div>