[M3devel] fixing try/setjmp in a loop?

Jay K jay.krell at cornell.edu
Sun Jan 30 11:45:17 CET 2011


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/20110130/b499da61/attachment-0001.html>


More information about the M3devel mailing list