Re: [MLton] understanding exnstack and Raise()

Jeffrey Murphy <[email protected]> Thu, 20 Jun 2019 15:40:09 -0400
Newsgroups gmane.comp.lang.ml.mlton.devel
Message-ID <[email protected]>
--===============7412165470128852538==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_07D023A2-2D52-4936-84FF-A3C1823C80E8"


--Apple-Mail=_07D023A2-2D52-4936-84FF-A3C1823C80E8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Starting with your final question first: I=E2=80=99m woking under the =
direction of Lukasz to make modifications to MLton. I=E2=80=99m working =
on modifying how stacks are organized in memory and I=E2=80=99m at the =
point (final point I hope!) of dealing with how an exception adjusts =
stackTop.  My approach so far has been to look at the compiler code =
(mainly backend.fun) and the output of the various stages to understand =
how exnStack is used to determine where stackTop points after an =
exception occurs.=20

Now, for the case you pointed out where the raise is turned into a =
direct jump, what is the effect on stackTop? Is that case essentially =
the same as a return to the previous stack frame?=20


> On Jun 20, 2019, at 1:21 PM, Matthew Fluet <[email protected]> =
wrote:
>=20
> Your example programs are too small; all of the `raise <exn>` =
expressions in them will be turned into direct jumps to the appropriate =
handler.
>=20
> The `Raise()` macros you see in the generated .c files are from =
`raise` expressions in the Basis Library implementation that correspond =
to error conditions that are never triggered; that's why you don't see a =
debugging message for them.
>=20
> You need a larger program.  In particular, you would want a non-tail =
recursive function that raises after growing the stack.  For example:
>=20
> [mtf@sulfur tmp]$ cat raise-example.sml
> val n =3D valOf (Int.fromString (hd (CommandLine.arguments ())))
> val m =3D valOf (Int.fromString (hd (tl (CommandLine.arguments ()))))
>=20
> exception E
> fun loop i =3D
>    if i =3D n
>       then raise E
>    else if i =3D 0
>       then n
>    else 1 + loop(i - 1)
>=20
> val r =3D loop m handle E =3D> 13
>=20
> val _ =3D print (concat ["r =3D ", Int.toString r, "\n"])
> [mtf@sulfur tmp]$ ../build/bin/mlton -codegen c -cc-opt =
'-DDEBUG_CCODEGEN=3DTRUE' -keep g -debug true -expert true -keep ssa =
-keep ssa2 -keep rssa -keep machine raise-example.sml
> [mtf@sulfur tmp]$ ./raise-example 5 10 2>&1 | grep Raise
> raise-example.0.c:1445: Raise()
>=20
> Is there a specific aspect of the exception handling implementation =
that you'd like to know about?
>=20
>=20
> On Thu, Jun 20, 2019 at 12:02 PM Jeffrey Murphy <[email protected] =
<mailto:[email protected]>> wrote:
> Hi,=20
>=20
> I=E2=80=99m trying to understand how exception handling is =
implemented. When I set DEBUG_CCODEGEN to true in c-chunk.h and use the =
C codegen, I can see Raise() macros being emitted, but they seem to not =
be referenced (no =E2=80=98Raise=E2=80=99 is printed when I run trivial =
programs). Is there doc I can read, or hints anyone can give me?
>=20
> thanks,
> jeff
>=20
>=20
> trival ex 1 (try4.sml)
>=20
> $ cat try4.sml
> exception A
> val _ =3D raise A
>=20
> $ ./build/bin/mlton -codegen c -keep g -debug true -expert true -keep =
ssa -keep ssa2 -keep rssa -keep machine  try4.sml
> $ ./try4 2>&1 | grep Raise
> $ ./try4 2>&1 | tail
> try4.1.c:216: Push (-12)
> try4.1.c:225: BNZ(0, L_106)
> try4.1.c:225: BNZ(1, L_106)
> try4.1.c:261: BNZ(0, L_35)
> try4.1.c:261: BNZ(1, L_35)
> try4.1.c:225: BNZ(1, L_106)
> try4.1.c:261: BNZ(0, L_35)
> try4.1.c:261: BNZ(1, L_35)
> unhandled exception: A
> try4.1.c:234: BNZ(0, L_26)
>=20
>=20
>=20
> trivial ex 2 (try5.sml)
>=20
> $ cat try5.sml
> exception A
> fun y z =3D raise A
> fun x z =3D y z handle A =3D> 1
> val _ =3D x 1
>=20
> $ ./build/bin/mlton -codegen c -keep g -debug true -expert true -keep =
ssa -keep ssa2 -keep rssa -keep machine  try5.sml
> $ ./try5 2>&1 | grep Raise
> $
>=20
> $ git rev-parse --short HEAD
> 05004a0
> (May 15 commit)
>=20
>=20
>=20
>=20
> _______________________________________________
> MLton-devel mailing list
> [email protected] =
<mailto:[email protected]>; [email protected] =
<mailto:[email protected]>
> https://lists.sourceforge.net/lists/listinfo/mlton-devel =
<https://lists.sourceforge.net/lists/listinfo/mlton-devel>
> _______________________________________________
> MLton-devel mailing list
> [email protected]; [email protected]
> https://lists.sourceforge.net/lists/listinfo/mlton-devel


--Apple-Mail=_07D023A2-2D52-4936-84FF-A3C1823C80E8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><font=
 size=3D"4" class=3D"">Starting with your final question first: I=E2=80=99=
m woking under the direction of Lukasz to make modifications to MLton. =
I=E2=80=99m working on modifying how stacks are organized in memory and =
I=E2=80=99m at the point (final point I hope!) of dealing with how an =
exception adjusts stackTop. &nbsp;My approach so far has been to look at =
the compiler code (mainly backend.fun) and the output of the various =
stages to understand how exnStack is used to determine where stackTop =
points after an exception occurs.&nbsp;</font><div class=3D""><font =
size=3D"4" class=3D""><br class=3D""></font></div><div class=3D""><font =
size=3D"4" class=3D"">Now, for the case you pointed out where the raise =
is turned into a direct jump, what is the effect on stackTop? Is that =
case essentially the same as a return to the previous stack =
frame?&nbsp;<br class=3D""></font><div><font size=3D"4" class=3D""><br =
class=3D""></font></div><div><br class=3D""><blockquote type=3D"cite" =
class=3D""><div class=3D"">On Jun 20, 2019, at 1:21 PM, Matthew Fluet =
&lt;<a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div class=3D""><div dir=3D"ltr" =
class=3D""><div class=3D"gmail_default" style=3D"font-family:courier =
new,monospace;font-size:large">Your example programs are too small; all =
of the `raise &lt;exn&gt;` expressions in them will be turned into =
direct jumps to the appropriate handler.</div><div class=3D"gmail_default"=
 style=3D"font-family:courier new,monospace;font-size:large"><br =
class=3D""></div><div class=3D"gmail_default" style=3D"font-family:courier=
 new,monospace;font-size:large">The `Raise()` macros you see in the =
generated .c files are from `raise` expressions in the Basis Library =
implementation that correspond to error conditions that are never =
triggered; that's why you don't see a debugging message for =
them.</div><div class=3D"gmail_default" style=3D"font-family:courier =
new,monospace;font-size:large"><br class=3D""></div><div =
class=3D"gmail_default" style=3D"font-family:courier =
new,monospace;font-size:large">You need a larger program.&nbsp; In =
particular, you would want a non-tail recursive function that raises =
after growing the stack.&nbsp; For example:</div><div =
class=3D"gmail_default" style=3D"font-family:courier =
new,monospace;font-size:large"><br class=3D""></div><div =
class=3D"gmail_default" style=3D"font-family:courier =
new,monospace;font-size:large">[mtf@sulfur tmp]$ cat =
raise-example.sml<br class=3D"">val n =3D valOf (Int.fromString (hd =
(CommandLine.arguments ())))<br class=3D"">val m =3D valOf =
(Int.fromString (hd (tl (CommandLine.arguments ()))))<br class=3D""><br =
class=3D"">exception E<br class=3D"">fun loop i =3D<br class=3D"">&nbsp; =
&nbsp;if i =3D n<br class=3D"">&nbsp; &nbsp; &nbsp; then raise E<br =
class=3D"">&nbsp; &nbsp;else if i =3D 0<br class=3D"">&nbsp; &nbsp; =
&nbsp; then n<br class=3D"">&nbsp; &nbsp;else 1 + loop(i - 1)<br =
class=3D""><br class=3D"">val r =3D loop m handle E =3D&gt; 13<br =
class=3D""><br class=3D"">val _ =3D print (concat ["r =3D ", =
Int.toString r, "\n"])<br class=3D"">[mtf@sulfur tmp]$ =
../build/bin/mlton -codegen c -cc-opt '-DDEBUG_CCODEGEN=3DTRUE' -keep g =
-debug true -expert true -keep ssa -keep ssa2 -keep rssa -keep machine =
raise-example.sml<br class=3D"">[mtf@sulfur tmp]$ ./raise-example 5 10 =
2&gt;&amp;1 | grep Raise<br class=3D"">raise-example.0.c:1445: =
Raise()<br class=3D""></div><div class=3D"gmail_default" =
style=3D"font-family:courier new,monospace;font-size:large"><br =
class=3D""></div><div class=3D"gmail_default" style=3D"font-family:courier=
 new,monospace;font-size:large">Is there a specific aspect of the =
exception handling implementation that you'd like to know =
about?</div><div class=3D"gmail_default" style=3D"font-family:courier =
new,monospace;font-size:large"><br class=3D""></div></div><br =
class=3D""><div class=3D"gmail_quote"><div dir=3D"ltr" =
class=3D"gmail_attr">On Thu, Jun 20, 2019 at 12:02 PM Jeffrey Murphy =
&lt;<a href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>&gt; wrote:<br =
class=3D""></div><blockquote class=3D"gmail_quote" style=3D"margin:0px =
0px 0px 0.8ex;border-left:1px solid =
rgb(204,204,204);padding-left:1ex">Hi, <br class=3D"">
<br class=3D"">
I=E2=80=99m trying to understand how exception handling is implemented. =
When I set DEBUG_CCODEGEN to true in c-chunk.h and use the C codegen, I =
can see Raise() macros being emitted, but they seem to not be referenced =
(no =E2=80=98Raise=E2=80=99 is printed when I run trivial programs). Is =
there doc I can read, or hints anyone can give me?<br class=3D"">
<br class=3D"">
thanks,<br class=3D"">
jeff<br class=3D"">
<br class=3D"">
<br class=3D"">
trival ex 1 (try4.sml)<br class=3D"">
<br class=3D"">
$ cat try4.sml<br class=3D"">
exception A<br class=3D"">
val _ =3D raise A<br class=3D"">
<br class=3D"">
$ ./build/bin/mlton -codegen c -keep g -debug true -expert true -keep =
ssa -keep ssa2 -keep rssa -keep machine&nbsp; try4.sml<br class=3D"">
$ ./try4 2&gt;&amp;1 | grep Raise<br class=3D"">
$ ./try4 2&gt;&amp;1 | tail<br class=3D"">
try4.1.c:216: Push (-12)<br class=3D"">
try4.1.c:225: BNZ(0, L_106)<br class=3D"">
try4.1.c:225: BNZ(1, L_106)<br class=3D"">
try4.1.c:261: BNZ(0, L_35)<br class=3D"">
try4.1.c:261: BNZ(1, L_35)<br class=3D"">
try4.1.c:225: BNZ(1, L_106)<br class=3D"">
try4.1.c:261: BNZ(0, L_35)<br class=3D"">
try4.1.c:261: BNZ(1, L_35)<br class=3D"">
unhandled exception: A<br class=3D"">
try4.1.c:234: BNZ(0, L_26)<br class=3D"">
<br class=3D"">
<br class=3D"">
<br class=3D"">
trivial ex 2 (try5.sml)<br class=3D"">
<br class=3D"">
$ cat try5.sml<br class=3D"">
exception A<br class=3D"">
fun y z =3D raise A<br class=3D"">
fun x z =3D y z handle A =3D&gt; 1<br class=3D"">
val _ =3D x 1<br class=3D"">
<br class=3D"">
$ ./build/bin/mlton -codegen c -keep g -debug true -expert true -keep =
ssa -keep ssa2 -keep rssa -keep machine&nbsp; try5.sml<br class=3D"">
$ ./try5 2&gt;&amp;1 | grep Raise<br class=3D"">
$<br class=3D"">
<br class=3D"">
$ git rev-parse --short HEAD<br class=3D"">
05004a0<br class=3D"">
(May 15 commit)<br class=3D"">
<br class=3D"">
<br class=3D"">
<br class=3D"">
<br class=3D"">
_______________________________________________<br class=3D"">
MLton-devel mailing list<br class=3D"">
<a href=3D"mailto:[email protected]" target=3D"_blank" =
class=3D"">[email protected]</a>; <a =
href=3D"mailto:[email protected]" target=3D"_blank" =
class=3D"">[email protected]</a><br class=3D"">
<a href=3D"https://lists.sourceforge.net/lists/listinfo/mlton-devel" =
rel=3D"noreferrer" target=3D"_blank" =
class=3D"">https://lists.sourceforge.net/lists/listinfo/mlton-devel</a><br=
 class=3D"">
</blockquote></div>
_______________________________________________<br class=3D"">MLton-devel =
mailing list<br class=3D""><a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a>; <a =
href=3D"mailto:[email protected]" =
class=3D"">[email protected]</a><br class=3D""><a =
href=3D"https://lists.sourceforge.net/lists/listinfo/mlton-devel" =
class=3D"">https://lists.sourceforge.net/lists/listinfo/mlton-devel</a><br=
 class=3D""></div></blockquote></div><br class=3D""></div></body></html>=

--Apple-Mail=_07D023A2-2D52-4936-84FF-A3C1823C80E8--


--===============7412165470128852538==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============7412165470128852538==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
MLton-devel mailing list
[email protected]; [email protected]
https://lists.sourceforge.net/lists/listinfo/mlton-devel

--===============7412165470128852538==--