[M3devel] fixing try/setjmp in a loop?

Jay K jay.krell at cornell.edu
Tue Feb 1 01:55:07 CET 2011


ps: I'm happy to report that my available time here
has become significantly reduced. Partly with that in mind,
but even w/o it, I am willing to go back to the old way.
m3front is full of mystery and I'm not sure I'm keen on understanding it.
 
 
 - Jay

 


From: hosking at cs.purdue.edu
Date: Mon, 31 Jan 2011 15:26:20 -0500
To: jay.krell at cornell.edu
CC: m3devel at elegosoft.com
Subject: Re: [M3devel] fixing try/setjmp in a loop?


I think we need to have a way to get the scope of the containing procedure.
TryStmt.Parse is not the right place.
If I get some time soon I can take a look at this.



On Jan 31, 2011, at 2:19 PM, Jay K wrote:

Right..I also tried Scope.Insert, didn't seem to help.
 
But I think I might be doing it too late. I'll try doing it in Parse instead of Check.
Though I don't recall if Parse knows enough -- if it knows if CaptureState will be called.
I agree/disagree about alloca up front, with the obvious reasons -- it is smaller code to alloca up front, and it somewhat
more closely resembles the old pattern:
 
IF Foo() THEN
  RETURN
ELSE TRY FINALLY
 
in the old way would use the stack, so we could too.
But the allocation is slower/larger now (alloca possibly aligning up more than needed).
Anyway, this point isn't the problem. The problem is my inability to work with m3front.
I'll look at it a bit more -- i.e. as I said, moving the code to Parse.
 
The other improvement I'd like to make:
 
currently, if I had it working, what I'm aiming for is:
struct EF1{ ... jmpbuf* jb } ef1;
jmpbuf* jb = 0;
 
jb = jb ? jb : alloca(sizeof(*jb));
ef1.jb = jb;
setjmp(ef1.jb);
 
but it'd be better to have:
 
struct EF1{ ... jmpbuf* jb } ef1;
ef1.jb = 0;
 
ef1.jb = ef1.jb ? ef1.jb : alloca(sizeof(*jb));
setjmp(ef1.jb);
 
 
ie. we don't need the extra pointer variable, the pointer in the record should suffice.
 
 
[not sure where to put this in email]
  I tried figuring out how to determine the current vs. topmost-in-current-function scope.
  I'm not sure if Scope.Top is about in a function or if it is module scope, or even perhaps
  some outside-all-modules scope. But Scope.Insert doesn't let you specify a scope,
  so I kinda hoped it'd just use the correct scope.
 
 
Anyway, I'll look again at introducing the Variable.New and Scope.Insert during Parse.
Even if it doesn't get used, that might be ok, since there is tracking of use, and
declaring it might not cause any waste if it isn't used.
 
 
Thanks,
 - Jay

 


From: hosking at cs.purdue.edu
Date: Mon, 31 Jan 2011 12:14:09 -0500
To: jay.krell at cornell.edu
CC: m3devel at elegosoft.com
Subject: Re: [M3devel] fixing try/setjmp in a loop?

Hi Jay,


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.




Antony Hosking | Associate Professor | Computer Science | Purdue University
305 N. University Street | West Lafayette | IN 47907 | USA
Office +1 765 494 6001 | Mobile +1 765 427 5484



On Jan 30, 2011, at 5:45 AM, Jay K wrote:

I'm trying to fix the inadvertant change I made where
FOR i := 1 TO 10 DO TRY FINALLY END

will call alloca 10 times.

I'm not familiar with the frontend, and it is hard for me to understand.
It seems like it is probably very well factored, written by someone who really knew what they were doing,
but it is dense, and not verbosely commented.

Something like this?? But this doesn't work. What am I missing?
And, we should go the next step and merge the jmpbuf pointer variable with the jmpbuf field in the EF1, right?


? 1.txt
Index: src/misc/Marker.i3
===================================================================
RCS file: /usr/cvs/cm3/m3-sys/m3front/src/misc/Marker.i3,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 Marker.i3
--- src/misc/Marker.i3    14 Jan 2001 13:40:31 -0000    1.1.1.1
+++ src/misc/Marker.i3    30 Jan 2011 10:41:18 -0000
@@ -64,9 +64,12 @@
 PROCEDURE SetLock (acquire: BOOLEAN;  var: CG.Var;  offset: INTEGER);
 (* generate the call to acquire or release a mutex *)
 
-PROCEDURE CaptureState (frame: CG.Var;  handler: CG.Label);
+PROCEDURE CaptureState (frame: CG.Var;  handler: CG.Label;
+                        jumpbuf: Variable.T);
 (* call 'setjmp' on 'frame's jmpbuf and branch to 'handler' on re-returns. *)
 
 PROCEDURE Reset ();
 
+PROCEDURE NewJumpbuf (): Variable.T;
+
 END Marker.
Index: src/misc/Marker.m3
===================================================================
RCS file: /usr/cvs/cm3/m3-sys/m3front/src/misc/Marker.m3,v
retrieving revision 1.9
diff -u -r1.9 Marker.m3
--- src/misc/Marker.m3    13 Jan 2011 14:58:28 -0000    1.9
+++ src/misc/Marker.m3    30 Jan 2011 10:41:18 -0000
@@ -10,7 +10,7 @@
 MODULE Marker;
 
 IMPORT CG, Error, Type, Variable, ProcType, ESet, Expr, AssignStmt;
-IMPORT M3ID, M3RT, Target, Module, RunTyme, Procedure, Host;
+IMPORT M3ID, M3RT, Target, Module, RunTyme, Procedure, Host, Addr;
 
 TYPE
   Kind = { zFINALLY, zFINALLYPROC, zLOCK, zEXIT, zTRY, zTRYELSE,
@@ -231,8 +231,10 @@
     END;
   END CallFinallyHandler;
 
-PROCEDURE CaptureState (frame: CG.Var;  handler: CG.Label) =
+PROCEDURE CaptureState (frame: CG.Var;  handler: CG.Label;
+                        jumpbuf: Variable.T) =
   VAR new: BOOLEAN;
+      label := CG.Next_label ();
   BEGIN
     (* int setjmp(void* ); *)
     IF (setjmp = NIL) THEN
@@ -262,13 +264,33 @@
                                         Target.Word.size, Target.Word.align,
                                         Target.Word.cg_type, 0);
     END;
-    
-    (* frame.jmpbuf = alloca(Csetjmp__Jumpbuf_size); *)
+
+    (* IF jumpuf # NIL THEN    
+     *   frame.jmpbuf = alloca(Csetjmp__Jumpbuf_size);
+     * END
+     *)
+
+    CG.Load_nil ();
+    Variable.Load (jumpbuf);
+    CG.If_compare (CG.Type.Addr, CG.Cmp.NE, label, CG.Maybe);
+
     CG.Start_call_direct (alloca, 0, Target.Address.cg_type);
     CG.Load_int (Target.Word.cg_type, Jumpbuf_size);
     CG.Pop_param (Target.Word.cg_type);
     CG.Call_direct (alloca, Target.Address.cg_type);
+
+    (* FUTURE: We should actually have a Variable
+     * for the entire EF1, including initializing the jumpbuf
+     * in it. That would save us this extra load/store,
+     * and save a local variable.
+     *)
+    Variable.LoadLValue (jumpbuf);
+    CG.Swap ();
+    CG.Store_indirect (CG.Type.Addr, 0, Target.Address.size);
+    Variable.Load (jumpbuf);
+
     CG.Store_addr (frame, M3RT.EF1_jmpbuf);
+    CG.Set_label (label);
 
     (* setmp(frame.jmpbuf) *)
     CG.Start_call_direct (setjmp, 0, Target.Integer.cg_type);
@@ -806,5 +828,15 @@
     tos          := 0;
   END Reset;
 
+PROCEDURE NewJumpbuf (): Variable.T =
+  VAR jumpbuf: Variable.T;
+  BEGIN
+   jumpbuf := Variable.New (M3ID.NoID, TRUE);
+   Variable.BindType (jumpbuf, Addr.T, indirect := FALSE,
+                      readonly := FALSE, open_array_ok := FALSE,
+                      needs_init := TRUE);
+   RETURN jumpbuf;
+  END NewJumpbuf;
+
 BEGIN
 END Marker.
Index: src/stmts/TryFinStmt.m3
===================================================================
RCS file: /usr/cvs/cm3/m3-sys/m3front/src/stmts/TryFinStmt.m3,v
retrieving revision 1.6
diff -u -r1.6 TryFinStmt.m3
--- src/stmts/TryFinStmt.m3    5 Jan 2011 14:34:54 -0000    1.6
+++ src/stmts/TryFinStmt.m3    30 Jan 2011 10:41:18 -0000
@@ -10,6 +10,7 @@
 
 IMPORT M3ID, CG, Token, Scanner, Stmt, StmtRep, Marker, Target, Type, Addr;
 IMPORT RunTyme, Procedure, ProcBody, M3RT, Scope, Fmt, Host, TryStmt, Module;
+IMPORT Variable;
 FROM Stmt IMPORT Outcome;
 
 TYPE
@@ -20,6 +21,7 @@
         viaProc  : BOOLEAN;
         scope    : Scope.T;
         handler  : HandlerProc;
+        jumpbuf  : Variable.T := NIL;
       OVERRIDES
         check       := Check;
         compile     := Compile;
@@ -63,6 +65,11 @@
     RETURN p;
   END Parse;
 
+PROCEDURE UsesSetjmp (p: P): BOOLEAN =
+  BEGIN
+    RETURN NOT (Target.Has_stack_walker OR p.viaProc);
+  END UsesSetjmp;
+
 PROCEDURE Check (p: P;  VAR cs: Stmt.CheckState) =
   VAR zz: Scope.T;  oc: Stmt.Outcomes;  name: INTEGER;
   BEGIN
@@ -95,6 +102,9 @@
       END;
     END;
     TryStmt.PopHandler ();
+    IF UsesSetjmp (p) THEN
+     p.jumpbuf := Marker.NewJumpbuf ();
+    END;
   END Check;
 
 PROCEDURE HandlerName (uid: INTEGER): TEXT =
@@ -106,11 +116,16 @@
   END HandlerName;
 
 PROCEDURE Compile (p: P): Stmt.Outcomes =
+  VAR usesSetjmp := FALSE;
+      result: Stmt.Outcomes;
   BEGIN
-    IF Target.Has_stack_walker THEN RETURN Compile1 (p);
-    ELSIF p.viaProc            THEN RETURN Compile2 (p);
-    ELSE                            RETURN Compile3 (p);
+    IF Target.Has_stack_walker THEN result := Compile1 (p);
+    ELSIF p.viaProc            THEN result := Compile2 (p);
+    ELSE                            usesSetjmp := TRUE;
+                                    result := Compile3 (p);
     END;
+    <* ASSERT usesSetjmp = UsesSetjmp (p) *>
+    RETURN result;
   END Compile;
 
 PROCEDURE Compile1 (p: P): Stmt.Outcomes =
@@ -302,7 +317,9 @@
     l := CG.Next_label (3);
     CG.Set_label (l, barrier := TRUE);
     Marker.PushFrame (frame, M3RT.HandlerClass.Finally);
-    Marker.CaptureState (frame, l+1);
+    <* ASSERT UsesSetjmp (p) *>
+    <* ASSERT p.jumpbuf # NIL *>
+    Marker.CaptureState (frame, l+1, p.jumpbuf);
 
     (* compile the body *)
     Marker.PushFinally (l, l+1, frame);
Index: src/stmts/TryStmt.m3
===================================================================
RCS file: /usr/cvs/cm3/m3-sys/m3front/src/stmts/TryStmt.m3,v
retrieving revision 1.3
diff -u -r1.3 TryStmt.m3
--- src/stmts/TryStmt.m3    5 Jan 2011 14:34:54 -0000    1.3
+++ src/stmts/TryStmt.m3    30 Jan 2011 10:41:18 -0000
@@ -22,6 +22,7 @@
         hasElse   : BOOLEAN;
         elseBody  : Stmt.T;
         handled   : ESet.T;
+        jumpbuf   : Variable.T := NIL;
       OVERRIDES
         check       := Check;
         compile     := Compile;
@@ -153,6 +154,14 @@
     p.handles := h3;
   END ReverseHandlers;
 
+PROCEDURE UsesSetjmp (p: P): BOOLEAN =
+  BEGIN
+    IF (p.handles = NIL) AND (NOT p.hasElse) THEN
+      RETURN FALSE;
+    END;
+    RETURN NOT Target.Has_stack_walker;
+  END UsesSetjmp;
+
 PROCEDURE Check (p: P;  VAR cs: Stmt.CheckState) =
   VAR h: Handler;  handled: ESet.T;
   BEGIN
@@ -182,6 +191,10 @@
       WHILE (h # NIL) DO CheckHandler (h, cs); h := h.next; END;
       Stmt.TypeCheck (p.elseBody, cs);
     PopHandler ();
+
+    IF UsesSetjmp (p) THEN
+      p.jumpbuf := Marker.NewJumpbuf ();
+    END;
   END Check;
 
 PROCEDURE CheckLabels (h: Handler;  scope: Scope.T;  VAR cs: Stmt.CheckState) =
@@ -245,14 +258,19 @@
   END CheckHandler;
 
 PROCEDURE Compile (p: P): Stmt.Outcomes =
+  VAR usesSetjmp := FALSE;
+      result: Stmt.Outcomes;
   BEGIN
     IF (p.handles = NIL) AND (NOT p.hasElse) THEN
-      RETURN Stmt.Compile (p.body);
-    END;
-    IF Target.Has_stack_walker
-      THEN RETURN Compile1 (p);
-      ELSE RETURN Compile2 (p);
+      result := Stmt.Compile (p.body);
+    ELSIF Target.Has_stack_walker THEN
+      result := Compile1 (p);
+    ELSE
+      usesSetjmp := TRUE;
+      result := Compile2 (p);
     END;
+    <* ASSERT usesSetjmp = UsesSetjmp (p) *>
+    RETURN result;
   END Compile;
 
 PROCEDURE Compile1 (p: P): Stmt.Outcomes =
@@ -423,7 +441,9 @@
     END;
 
     (* capture the machine state *)
-    Marker.CaptureState (frame, l+1);
+    <* ASSERT UsesSetjmp (p) *>
+    <* ASSERT p.jumpbuf # NIL *>
+    Marker.CaptureState (frame, l+1, p.jumpbuf);
 
     (* compile the body *)
     oc := Stmt.Compile (p.body);

Thanks,
 - Jay


 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20110201/fbfe346f/attachment-0001.html>


More information about the M3devel mailing list