Re: [MLton] understanding exnstack and Raise()
Matthew Fluet <[email protected]> Thu, 20 Jun 2019 13:21:48 -0400
| Newsgroups | gmane.comp.lang.ml.mlton.devel |
|---|---|
| Message-ID | <CAMrhFL4TTA-0DHpQLGKKHFJKytL3ULO8H1qDxUpvBhPnsdrKKg@mail.gmail.com> |
--===============3050756279259133462==
Content-Type: multipart/alternative; boundary="000000000000b0ff1c058bc496bc"
--000000000000b0ff1c058bc496bc
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Your example programs are too small; all of the `raise <exn>` expressions
in them will be turned into direct jumps to the appropriate handler.
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.
You need a larger program. In particular, you would want a non-tail
recursive function that raises after growing the stack. For example:
[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 ()))))
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)
val r =3D loop m handle E =3D> 13
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()
Is there a specific aspect of the exception handling implementation that
you'd like to know about?
On Thu, Jun 20, 2019 at 12:02 PM Jeffrey Murphy <[email protected]>
wrote:
> Hi,
>
> I=E2=80=99m trying to understand how exception handling is implemented. W=
hen 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?
>
> thanks,
> jeff
>
>
> trival ex 1 (try4.sml)
>
> $ cat try4.sml
> exception A
> val _ =3D raise A
>
> $ ./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)
>
>
>
> trivial ex 2 (try5.sml)
>
> $ 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
>
> $ ./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
> $
>
> $ git rev-parse --short HEAD
> 05004a0
> (May 15 commit)
>
>
>
>
> _______________________________________________
> MLton-devel mailing list
> [email protected]; [email protected]
> https://lists.sourceforge.net/lists/listinfo/mlton-devel
>
--000000000000b0ff1c058bc496bc
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><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 <exn>` expressions in them will be turned into direct jump=
s to the appropriate handler.</div><div class=3D"gmail_default" style=3D"fo=
nt-family:courier new,monospace;font-size:large"><br></div><div class=3D"gm=
ail_default" style=3D"font-family:courier new,monospace;font-size:large">Th=
e `Raise()` macros you see in the generated .c files are from `raise` expre=
ssions in the Basis Library implementation that correspond to error conditi=
ons 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:co=
urier new,monospace;font-size:large"><br></div><div class=3D"gmail_default"=
style=3D"font-family:courier new,monospace;font-size:large">You need a lar=
ger program.=C2=A0 In particular, you would want a non-tail recursive funct=
ion that raises after growing the stack.=C2=A0 For example:</div><div class=
=3D"gmail_default" style=3D"font-family:courier new,monospace;font-size:lar=
ge"><br></div><div class=3D"gmail_default" style=3D"font-family:courier new=
,monospace;font-size:large">[mtf@sulfur tmp]$ cat raise-example.sml<br>val =
n =3D valOf (Int.fromString (hd (CommandLine.arguments ())))<br>val m =3D v=
alOf (Int.fromString (hd (tl (CommandLine.arguments ()))))<br><br>exception=
E<br>fun loop i =3D<br>=C2=A0 =C2=A0if i =3D n<br>=C2=A0 =C2=A0 =C2=A0 the=
n raise E<br>=C2=A0 =C2=A0else if i =3D 0<br>=C2=A0 =C2=A0 =C2=A0 then n<br=
>=C2=A0 =C2=A0else 1 + loop(i - 1)<br><br>val r =3D loop m handle E =3D>=
13<br><br>val _ =3D print (concat ["r =3D ", Int.toString r, &qu=
ot;\n"])<br>[mtf@sulfur tmp]$ ../build/bin/mlton -codegen c -cc-opt &#=
39;-DDEBUG_CCODEGEN=3DTRUE' -keep g -debug true -expert true -keep ssa =
-keep ssa2 -keep rssa -keep machine raise-example.sml<br>[mtf@sulfur tmp]$ =
./raise-example 5 10 2>&1 | grep Raise<br>raise-example.0.c:1445: Ra=
ise()<br></div><div class=3D"gmail_default" style=3D"font-family:courier ne=
w,monospace;font-size:large"><br></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></div></div><br><div class=3D"gmail_quote"><=
div dir=3D"ltr" class=3D"gmail_attr">On Thu, Jun 20, 2019 at 12:02 PM Jeffr=
ey Murphy <<a href=3D"mailto:[email protected]">[email protected]<=
/a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0=
px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">H=
i, <br>
<br>
I=E2=80=99m trying to understand how exception handling is implemented. Whe=
n I set DEBUG_CCODEGEN to true in c-chunk.h and use the C codegen, I can se=
e 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>
<br>
thanks,<br>
jeff<br>
<br>
<br>
trival ex 1 (try4.sml)<br>
<br>
$ cat try4.sml<br>
exception A<br>
val _ =3D raise A<br>
<br>
$ ./build/bin/mlton -codegen c -keep g -debug true -expert true -keep ssa -=
keep ssa2 -keep rssa -keep machine=C2=A0 try4.sml<br>
$ ./try4 2>&1 | grep Raise<br>
$ ./try4 2>&1 | tail<br>
try4.1.c:216: Push (-12)<br>
try4.1.c:225: BNZ(0, L_106)<br>
try4.1.c:225: BNZ(1, L_106)<br>
try4.1.c:261: BNZ(0, L_35)<br>
try4.1.c:261: BNZ(1, L_35)<br>
try4.1.c:225: BNZ(1, L_106)<br>
try4.1.c:261: BNZ(0, L_35)<br>
try4.1.c:261: BNZ(1, L_35)<br>
unhandled exception: A<br>
try4.1.c:234: BNZ(0, L_26)<br>
<br>
<br>
<br>
trivial ex 2 (try5.sml)<br>
<br>
$ cat try5.sml<br>
exception A<br>
fun y z =3D raise A<br>
fun x z =3D y z handle A =3D> 1<br>
val _ =3D x 1<br>
<br>
$ ./build/bin/mlton -codegen c -keep g -debug true -expert true -keep ssa -=
keep ssa2 -keep rssa -keep machine=C2=A0 try5.sml<br>
$ ./try5 2>&1 | grep Raise<br>
$<br>
<br>
$ git rev-parse --short HEAD<br>
05004a0<br>
(May 15 commit)<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
MLton-devel mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">MLto=
[email protected]</a>; <a href=3D"mailto:[email protected]"=
target=3D"_blank">[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/mlton-devel" rel=3D=
"noreferrer" target=3D"_blank">https://lists.sourceforge.net/lists/listinfo=
/mlton-devel</a><br>
</blockquote></div>
--000000000000b0ff1c058bc496bc--
--===============3050756279259133462==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============3050756279259133462==
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
--===============3050756279259133462==--