<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
Right..I also tried Scope.Insert, didn't seem to help.<BR>
<BR>
But I think I might be doing it too late. I'll try doing it in Parse instead of Check.<BR>
Though I don't recall if Parse knows enough -- if it knows if CaptureState will be called.<BR>
I agree/disagree about alloca up front, with the obvious reasons -- it is smaller code to alloca up front, and it somewhat<BR>
more closely resembles the old pattern:<BR>
<BR>
IF Foo() THEN<BR>
RETURN<BR>
ELSE TRY FINALLY<BR>
<BR>
in the old way would use the stack, so we could too.<BR>
But the allocation is slower/larger now (alloca possibly aligning up more than needed).<BR>
Anyway, this point isn't the problem. The problem is my inability to work with m3front.<BR>
I'll look at it a bit more -- i.e. as I said, moving the code to Parse.<BR>
<BR>
The other improvement I'd like to make:<BR>
<BR>
currently, if I had it working, what I'm aiming for is:<BR>
struct EF1{ ... jmpbuf* jb } ef1;<BR>
jmpbuf* jb = 0;<BR>
<BR>
jb = jb ? jb : alloca(sizeof(*jb));<BR>
ef1.jb = jb;<BR>
setjmp(ef1.jb);<BR>
<BR>
but it'd be better to have:<BR>
<BR>
struct EF1{ ... jmpbuf* jb } ef1;<BR>
ef1.jb = 0;<BR>
<BR>
ef1.jb = ef1.jb ? ef1.jb : alloca(sizeof(*jb));<BR>
setjmp(ef1.jb);<BR>
<BR>
<BR>
ie. we don't need the extra pointer variable, the pointer in the record should suffice.<BR>
<BR>
<BR>
[not sure where to put this in email]<BR>
I tried figuring out how to determine the current vs. topmost-in-current-function scope.<BR>
I'm not sure if Scope.Top is about in a function or if it is module scope, or even perhaps<BR>
some outside-all-modules scope. But Scope.Insert doesn't let you specify a scope,<BR>
so I kinda hoped it'd just use the correct scope.<BR>
<BR>
<BR>
Anyway, I'll look again at introducing the Variable.New and Scope.Insert during Parse.<BR>
Even if it doesn't get used, that might be ok, since there is tracking of use, and<BR>
declaring it might not cause any waste if it isn't used.<BR>
<BR>
<BR>
Thanks,<BR>
- Jay<BR><BR> <BR>
<HR id=stopSpelling>
From: hosking@cs.purdue.edu<BR>Date: Mon, 31 Jan 2011 12:14:09 -0500<BR>To: jay.krell@cornell.edu<BR>CC: m3devel@elegosoft.com<BR>Subject: Re: [M3devel] fixing try/setjmp in a loop?<BR><BR>
<META name=Generator content="Microsoft SafeHTML">Hi Jay,
<DIV><BR></DIV>
<DIV>I've not had a chance to digest this, but I do understand the problem. We need the Variable to be entered into the outermost scope of the procedure in which the TRY appears so that it will have initialization code (assigning NIL) generated for it on entry to the procedure. Then your usage of the variable should simply check for NIL and alloca if it is, as you already appear to do here. It would be possible to have the initialization code perform the alloca but then we would have an alloca for every TRY block in a procedure even if they never execute. Probably not the best plan.</DIV>
<DIV><BR>
<DIV><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span>
<DIV style="WORD-WRAP: break-word"><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: 12px Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; COLOR: rgb(0,0,0); WORD-SPACING: 0px" class=ecxApple-style-span>
<DIV><FONT class=ecxApple-style-span color=#0000ff><FONT class=ecxApple-style-span face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span>Antony Hosking</SPAN></SPAN></FONT></FONT><FONT class=ecxApple-style-span face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN class=ecxApple-converted-space> </SPAN>|<SPAN class=ecxApple-converted-space> </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span>Associate Professor</SPAN></SPAN><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'" class=ecxApple-style-span> | Computer Science | Purdue University</SPAN></SPAN></FONT></DIV>
<DIV><FONT class=ecxApple-style-span face=GillSans-Light><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span>305 N. University Street | West Lafayette | IN 47907 | USA</SPAN></FONT></DIV>
<DIV><FONT class=ecxApple-style-span color=#0000ff face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span>Office</SPAN></SPAN></FONT><FONT class=ecxApple-style-span face=GillSans-Light><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span> +1 765 494 6001 |<SPAN class=ecxApple-converted-space> </SPAN></SPAN></SPAN></FONT><FONT class=ecxApple-style-span color=#0000ff face="Gill Sans"><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span><SPAN style="FONT-FAMILY: 'Gill Sans'; COLOR: rgb(0,0,255)" class=ecxApple-style-span>Mobile</SPAN></SPAN></FONT><FONT class=ecxApple-style-span face=GillSans-Light><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span><SPAN style="FONT-FAMILY: GillSans-Light" class=ecxApple-style-span><SPAN class=ecxApple-converted-space> </SPAN>+1 765 427 5484</SPAN></SPAN></FONT></DIV>
<DIV><FONT class=ecxApple-style-span face=GillSans-Light><BR class=ecxkhtml-block-placeholder></FONT></DIV></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN><BR class=ecxApple-interchange-newline></SPAN></DIV></SPAN></SPAN><BR class=ecxApple-interchange-newline></DIV><BR>
<DIV>
<DIV>On Jan 30, 2011, at 5:45 AM, Jay K wrote:</DIV><BR class=ecxApple-interchange-newline>
<BLOCKQUOTE><SPAN style="TEXT-TRANSFORM: none; TEXT-INDENT: 0px; BORDER-COLLAPSE: separate; FONT: medium Helvetica; WHITE-SPACE: normal; LETTER-SPACING: normal; WORD-SPACING: 0px" class=ecxApple-style-span>
<DIV style="FONT-FAMILY: Tahoma; FONT-SIZE: 10pt" class=ecxhmmessage>I'm trying to fix the inadvertant change I made where<BR>FOR i := 1 TO 10 DO TRY FINALLY END<BR><BR>will call alloca 10 times.<BR><BR>I'm not familiar with the frontend, and it is hard for me to understand.<BR>It seems like it is probably very well factored, written by someone who really knew what they were doing,<BR>but it is dense, and not verbosely commented.<BR><BR>Something like this?? But this doesn't work. What am I missing?<BR>And, we should go the next step and merge the jmpbuf pointer variable with the jmpbuf field in the EF1, right?<BR><BR><BR>? 1.txt<BR>Index: src/misc/Marker.i3<BR>===================================================================<BR>RCS file: /usr/cvs/cm3/m3-sys/m3front/src/misc/Marker.i3,v<BR>retrieving revision 1.1.1.1<BR>diff -u -r1.1.1.1 Marker.i3<BR>--- src/misc/Marker.i3 14 Jan 2001 13:40:31 -0000 1.1.1.1<BR>+++ src/misc/Marker.i3 30 Jan 2011 10:41:18 -0000<BR>@@ -64,9 +64,12 @@<BR> PROCEDURE SetLock (acquire: BOOLEAN; var: CG.Var; offset: INTEGER);<BR> (* generate the call to acquire or release a mutex *)<BR> <BR>-PROCEDURE CaptureState (frame: CG.Var; handler: CG.Label);<BR>+PROCEDURE CaptureState (frame: CG.Var; handler: CG.Label;<BR>+ jumpbuf: Variable.T);<BR> (* call 'setjmp' on 'frame's jmpbuf and branch to 'handler' on re-returns. *)<BR> <BR> PROCEDURE Reset ();<BR> <BR>+PROCEDURE NewJumpbuf (): Variable.T;<BR>+<BR> END Marker.<BR>Index: src/misc/Marker.m3<BR>===================================================================<BR>RCS file: /usr/cvs/cm3/m3-sys/m3front/src/misc/Marker.m3,v<BR>retrieving revision 1.9<BR>diff -u -r1.9 Marker.m3<BR>--- src/misc/Marker.m3 13 Jan 2011 14:58:28 -0000 1.9<BR>+++ src/misc/Marker.m3 30 Jan 2011 10:41:18 -0000<BR>@@ -10,7 +10,7 @@<BR> MODULE Marker;<BR> <BR> IMPORT CG, Error, Type, Variable, ProcType, ESet, Expr, AssignStmt;<BR>-IMPORT M3ID, M3RT, Target, Module, RunTyme, Procedure, Host;<BR>+IMPORT M3ID, M3RT, Target, Module, RunTyme, Procedure, Host, Addr;<BR> <BR> TYPE<BR> Kind = { zFINALLY, zFINALLYPROC, zLOCK, zEXIT, zTRY, zTRYELSE,<BR>@@ -231,8 +231,10 @@<BR> END;<BR> END CallFinallyHandler;<BR> <BR>-PROCEDURE CaptureState (frame: CG.Var; handler: CG.Label) =<BR>+PROCEDURE CaptureState (frame: CG.Var; handler: CG.Label;<BR>+ jumpbuf: Variable.T) =<BR> VAR new: BOOLEAN;<BR>+ label := CG.Next_label ();<BR> BEGIN<BR> (* int setjmp(void* ); *)<BR> IF (setjmp = NIL) THEN<BR>@@ -262,13 +264,33 @@<BR> Target.Word.size, Target.Word.align,<BR> Target.Word.cg_type, 0);<BR> END;<BR>- <SPAN class=ecxApple-converted-space> </SPAN><BR>- (* frame.jmpbuf = alloca(Csetjmp__Jumpbuf_size); *)<BR>+<BR>+ (* IF jumpuf # NIL THEN <SPAN class=ecxApple-converted-space> </SPAN><BR>+ * frame.jmpbuf = alloca(Csetjmp__Jumpbuf_size);<BR>+ * END<BR>+ *)<BR>+<BR>+ CG.Load_nil ();<BR>+ Variable.Load (jumpbuf);<BR>+ CG.If_compare (CG.Type.Addr, CG.Cmp.NE, label, CG.Maybe);<BR>+<BR> CG.Start_call_direct (alloca, 0, Target.Address.cg_type);<BR> CG.Load_int (Target.Word.cg_type, Jumpbuf_size);<BR> CG.Pop_param (Target.Word.cg_type);<BR> CG.Call_direct (alloca, Target.Address.cg_type);<BR>+<BR>+ (* FUTURE: We should actually have a Variable<BR>+ * for the entire EF1, including initializing the jumpbuf<BR>+ * in it. That would save us this extra load/store,<BR>+ * and save a local variable.<BR>+ *)<BR>+ Variable.LoadLValue (jumpbuf);<BR>+ CG.Swap ();<BR>+ CG.Store_indirect (CG.Type.Addr, 0, Target.Address.size);<BR>+ Variable.Load (jumpbuf);<BR>+<BR> CG.Store_addr (frame, M3RT.EF1_jmpbuf);<BR>+ CG.Set_label (label);<BR> <BR> (* setmp(frame.jmpbuf) *)<BR> CG.Start_call_direct (setjmp, 0, Target.Integer.cg_type);<BR>@@ -806,5 +828,15 @@<BR> tos := 0;<BR> END Reset;<BR> <BR>+PROCEDURE NewJumpbuf (): Variable.T =<BR>+ VAR jumpbuf: Variable.T;<BR>+ BEGIN<BR>+ jumpbuf := Variable.New (M3ID.NoID, TRUE);<BR>+ Variable.BindType (jumpbuf, Addr.T, indirect := FALSE,<BR>+ readonly := FALSE, open_array_ok := FALSE,<BR>+ needs_init := TRUE);<BR>+ RETURN jumpbuf;<BR>+ END NewJumpbuf;<BR>+<BR> BEGIN<BR> END Marker.<BR>Index: src/stmts/TryFinStmt.m3<BR>===================================================================<BR>RCS file: /usr/cvs/cm3/m3-sys/m3front/src/stmts/TryFinStmt.m3,v<BR>retrieving revision 1.6<BR>diff -u -r1.6 TryFinStmt.m3<BR>--- src/stmts/TryFinStmt.m3 5 Jan 2011 14:34:54 -0000 1.6<BR>+++ src/stmts/TryFinStmt.m3 30 Jan 2011 10:41:18 -0000<BR>@@ -10,6 +10,7 @@<BR> <BR> IMPORT M3ID, CG, Token, Scanner, Stmt, StmtRep, Marker, Target, Type, Addr;<BR> IMPORT RunTyme, Procedure, ProcBody, M3RT, Scope, Fmt, Host, TryStmt, Module;<BR>+IMPORT Variable;<BR> FROM Stmt IMPORT Outcome;<BR> <BR> TYPE<BR>@@ -20,6 +21,7 @@<BR> viaProc : BOOLEAN;<BR> scope : Scope.T;<BR> handler : HandlerProc;<BR>+ jumpbuf : Variable.T := NIL;<BR> OVERRIDES<BR> check := Check;<BR> compile := Compile;<BR>@@ -63,6 +65,11 @@<BR> RETURN p;<BR> END Parse;<BR> <BR>+PROCEDURE UsesSetjmp (p: P): BOOLEAN =<BR>+ BEGIN<BR>+ RETURN NOT (Target.Has_stack_walker OR p.viaProc);<BR>+ END UsesSetjmp;<BR>+<BR> PROCEDURE Check (p: P; VAR cs: Stmt.CheckState) =<BR> VAR zz: Scope.T; oc: Stmt.Outcomes; name: INTEGER;<BR> BEGIN<BR>@@ -95,6 +102,9 @@<BR> END;<BR> END;<BR> TryStmt.PopHandler ();<BR>+ IF UsesSetjmp (p) THEN<BR>+ p.jumpbuf := Marker.NewJumpbuf ();<BR>+ END;<BR> END Check;<BR> <BR> PROCEDURE HandlerName (uid: INTEGER): TEXT =<BR>@@ -106,11 +116,16 @@<BR> END HandlerName;<BR> <BR> PROCEDURE Compile (p: P): Stmt.Outcomes =<BR>+ VAR usesSetjmp := FALSE;<BR>+ result: Stmt.Outcomes;<BR> BEGIN<BR>- IF Target.Has_stack_walker THEN RETURN Compile1 (p);<BR>- ELSIF p.viaProc THEN RETURN Compile2 (p);<BR>- ELSE RETURN Compile3 (p);<BR>+ IF Target.Has_stack_walker THEN result := Compile1 (p);<BR>+ ELSIF p.viaProc THEN result := Compile2 (p);<BR>+ ELSE usesSetjmp := TRUE;<BR>+ result := Compile3 (p);<BR> END;<BR>+ <* ASSERT usesSetjmp = UsesSetjmp (p) *><BR>+ RETURN result;<BR> END Compile;<BR> <BR> PROCEDURE Compile1 (p: P): Stmt.Outcomes =<BR>@@ -302,7 +317,9 @@<BR> l := CG.Next_label (3);<BR> CG.Set_label (l, barrier := TRUE);<BR> Marker.PushFrame (frame, M3RT.HandlerClass.Finally);<BR>- Marker.CaptureState (frame, l+1);<BR>+ <* ASSERT UsesSetjmp (p) *><BR>+ <* ASSERT p.jumpbuf # NIL *><BR>+ Marker.CaptureState (frame, l+1, p.jumpbuf);<BR> <BR> (* compile the body *)<BR> Marker.PushFinally (l, l+1, frame);<BR>Index: src/stmts/TryStmt.m3<BR>===================================================================<BR>RCS file: /usr/cvs/cm3/m3-sys/m3front/src/stmts/TryStmt.m3,v<BR>retrieving revision 1.3<BR>diff -u -r1.3 TryStmt.m3<BR>--- src/stmts/TryStmt.m3 5 Jan 2011 14:34:54 -0000 1.3<BR>+++ src/stmts/TryStmt.m3 30 Jan 2011 10:41:18 -0000<BR>@@ -22,6 +22,7 @@<BR> hasElse : BOOLEAN;<BR> elseBody : Stmt.T;<BR> handled : ESet.T;<BR>+ jumpbuf : Variable.T := NIL;<BR> OVERRIDES<BR> check := Check;<BR> compile := Compile;<BR>@@ -153,6 +154,14 @@<BR> p.handles := h3;<BR> END ReverseHandlers;<BR> <BR>+PROCEDURE UsesSetjmp (p: P): BOOLEAN =<BR>+ BEGIN<BR>+ IF (p.handles = NIL) AND (NOT p.hasElse) THEN<BR>+ RETURN FALSE;<BR>+ END;<BR>+ RETURN NOT Target.Has_stack_walker;<BR>+ END UsesSetjmp;<BR>+<BR> PROCEDURE Check (p: P; VAR cs: Stmt.CheckState) =<BR> VAR h: Handler; handled: ESet.T;<BR> BEGIN<BR>@@ -182,6 +191,10 @@<BR> WHILE (h # NIL) DO CheckHandler (h, cs); h := h.next; END;<BR> Stmt.TypeCheck (p.elseBody, cs);<BR> PopHandler ();<BR>+<BR>+ IF UsesSetjmp (p) THEN<BR>+ p.jumpbuf := Marker.NewJumpbuf ();<BR>+ END;<BR> END Check;<BR> <BR> PROCEDURE CheckLabels (h: Handler; scope: Scope.T; VAR cs: Stmt.CheckState) =<BR>@@ -245,14 +258,19 @@<BR> END CheckHandler;<BR> <BR> PROCEDURE Compile (p: P): Stmt.Outcomes =<BR>+ VAR usesSetjmp := FALSE;<BR>+ result: Stmt.Outcomes;<BR> BEGIN<BR> IF (p.handles = NIL) AND (NOT p.hasElse) THEN<BR>- RETURN Stmt.Compile (p.body);<BR>- END;<BR>- IF Target.Has_stack_walker<BR>- THEN RETURN Compile1 (p);<BR>- ELSE RETURN Compile2 (p);<BR>+ result := Stmt.Compile (p.body);<BR>+ ELSIF Target.Has_stack_walker THEN<BR>+ result := Compile1 (p);<BR>+ ELSE<BR>+ usesSetjmp := TRUE;<BR>+ result := Compile2 (p);<BR> END;<BR>+ <* ASSERT usesSetjmp = UsesSetjmp (p) *><BR>+ RETURN result;<BR> END Compile;<BR> <BR> PROCEDURE Compile1 (p: P): Stmt.Outcomes =<BR>@@ -423,7 +441,9 @@<BR> END;<BR> <BR> (* capture the machine state *)<BR>- Marker.CaptureState (frame, l+1);<BR>+ <* ASSERT UsesSetjmp (p) *><BR>+ <* ASSERT p.jumpbuf # NIL *><BR>+ Marker.CaptureState (frame, l+1, p.jumpbuf);<BR> <BR> (* compile the body *)<BR> oc := Stmt.Compile (p.body);<BR><BR>Thanks,<BR> - Jay<BR></DIV></SPAN></BLOCKQUOTE></DIV><BR></DIV> </body>
</html>