<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
p.Code, li.Code, div.Code
        {mso-style-name:Code;
        mso-style-link:"Code Char";
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:9.0pt;
        font-family:"Courier New";}
span.CodeChar
        {mso-style-name:"Code Char";
        mso-style-link:Code;
        font-family:"Courier New";}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">You might also find compiler/ghci/RtClosureInspect.hs useful.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">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?!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Simon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Libraries <libraries-bounces@haskell.org>
<b>On Behalf Of </b>Andrew Martin<br>
<b>Sent:</b> 06 February 2019 20:57<br>
<b>To:</b> Matthew Pickering <matthewtpickering@gmail.com><br>
<b>Cc:</b> Haskell Libraries <libraries@haskell.org><br>
<b>Subject:</b> Re: Peeking into StgInfoTable in cmm<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Or even better, it looks like cmmGetClosureType already combines the two for me.<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Wed, Feb 6, 2019 at 3:49 PM Andrew Martin <<a href="mailto:andrew.thaddeus@gmail.com">andrew.thaddeus@gmail.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;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:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">    info  = %GET_STD_INFO(UNTAG(closure));<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></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:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;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><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">-- <o:p></o:p></p>
<div>
<p class="MsoNormal">-Andrew Thaddeus Martin<o:p></o:p></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">-- <o:p></o:p></p>
<div>
<p class="MsoNormal">-Andrew Thaddeus Martin<o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>