[Commit] nickle compile.c,1.137,1.138 execute.c,1.79,1.80 nickle.h,1.100,1.101 opcode.h,1.23,1.24

Keith Packard commit at keithp.com
Thu Jun 5 17:51:25 PDT 2003


Committed by: keithp

Update of /local/src/CVS/nickle
In directory home.keithp.com:/tmp/cvs-serv32499

Modified Files:
	compile.c execute.c nickle.h opcode.h 
Log Message:
Make multiple catch blocks in the same try statement handled at the same catch level instead of being nested

Index: compile.c
===================================================================
RCS file: /local/src/CVS/nickle/compile.c,v
retrieving revision 1.137
retrieving revision 1.138
diff -u -d -r1.137 -r1.138
--- compile.c	5 Jun 2003 15:03:34 -0000	1.137
+++ compile.c	5 Jun 2003 23:51:23 -0000	1.138
@@ -223,7 +223,8 @@
 ObjPtr	_CompileBoolExpr (ObjPtr obj, ExprPtr expr, Bool evaluate, ExprPtr stat, CodePtr code);
 void	CompilePatchLoop (ObjPtr obj, int start,
 			  int continue_offset,
-			  int break_offset);
+			  int break_offset,
+			  int catch_offset);
 ObjPtr	_CompileStat (ObjPtr obj, ExprPtr expr, Bool last, CodePtr code);
 ObjPtr	CompileFunc (ObjPtr obj, CodePtr code, ExprPtr stat, CodePtr previous, NonLocalPtr nonLocal);
 ObjPtr	CompileDecl (ObjPtr obj, ExprPtr decls, Bool evaluate, ExprPtr stat, CodePtr code);
@@ -1828,7 +1829,7 @@
 
 static ObjPtr
 CompileCatch (ObjPtr obj, ExprPtr catches, ExprPtr body, 
-	      ExprPtr stat, CodePtr code)
+	      ExprPtr stat, CodePtr code, int nest)
 {
     ENTER ();
     int		catch_inst, exception_inst;
@@ -1897,11 +1898,25 @@
 	/*
 	 * Patch non local returns inside
 	 */
-	CompilePatchLoop (obj, catch_inst, -1, -1);
+	CompilePatchLoop (obj, catch_inst, -1, -1, -1);
+	
+	/*
+	 * Unwind any peer catch blocks while executing catch
+	 */
+	if (nest)
+	{
+	    BuildInst (obj, OpUnwind, inst, stat);
+	    inst->unwind.twixt = 0;
+	    inst->unwind.catch = nest;
+	}
 	
 	BuildInst (obj, OpExceptionCall, inst, stat);
 	
-	NewInst (obj, OpBranch, exception_inst, stat);
+	exception_inst = obj->used;
+
+	BuildInst (obj, OpBranch, inst, stat);
+	inst->branch.offset = 0;
+	inst->branch.mod = BranchModCatch;
     
 	inst = ObjCode (obj, catch_inst);
 	inst->catch.offset = obj->used - catch_inst;
@@ -1909,15 +1924,18 @@
     
 	obj->nonLocal = NewNonLocal (obj->nonLocal, NonLocalTry, 0);
 	
-	obj = CompileCatch (obj, catches->tree.left, body, stat, code);
+	obj = CompileCatch (obj, catches->tree.left, body, stat, code, nest+1);
 	
 	obj->nonLocal = obj->nonLocal->prev;
 
-	BuildInst (obj, OpEndCatch, inst, stat);
-	
-	inst = ObjCode (obj, exception_inst);
-	inst->branch.offset = obj->used - exception_inst;
-	inst->branch.mod = BranchModNone;
+	if (!nest)
+	{
+	    BuildInst (obj, OpEndCatch, inst, stat);
+	    /*
+	     * Patch Catch branches inside
+	     */
+	    CompilePatchLoop (obj, exception_inst, -1, -1, obj->used);
+	}
     }
     else
 	obj = _CompileStat (obj, body, False, code);
@@ -2547,7 +2565,8 @@
 CompilePatchLoop (ObjPtr    obj, 
 		  int	    start,
 		  int	    continue_offset,
-		  int	    break_offset)
+		  int	    break_offset,
+		  int	    catch_offset)
 {
     InstPtr inst;
 
@@ -2566,6 +2585,10 @@
 		    if (continue_offset >= 0)
 			inst->branch.offset = continue_offset - start;
 		    break;
+		case BranchModCatch:
+		    if (catch_offset >= 0)
+			inst->branch.offset = catch_offset - start;
+		    break;
 		default:
 		    break;
 		}
@@ -2586,6 +2609,7 @@
 		    inst->farJump.farJump->inst = -2;
 		    break;
 		case BranchModNone:
+		case BranchModCatch:
 		    break;
 		}
 	    }
@@ -2597,11 +2621,12 @@
 		if (inst->code.code->func.body.obj)
 		    CompilePatchLoop (inst->code.code->func.body.obj, 0,
 				      continue_offset,
-				      break_offset);
+				      break_offset,
+				      -1);
 		if (inst->code.code->func.staticInit.obj)
 		    CompilePatchLoop (inst->code.code->func.staticInit.obj, 0,
 				      continue_offset,
-				      break_offset);
+				      break_offset, -1);
 	    }
 	    break;
 	default:
@@ -2638,7 +2663,7 @@
 		    CompileMoveObj (inst->code.code->func.body.obj, 0,
 				    depth + 1, amount);
 		if (inst->code.code->func.staticInit.obj)
-		    CompilePatchLoop (inst->code.code->func.staticInit.obj, 0,
+		    CompileMoveObj (inst->code.code->func.staticInit.obj, 0,
 				      depth + 1, amount);
 	    }
 	    break;
@@ -2719,6 +2744,7 @@
 	    inst->branch.offset = 0;	/* filled in by PatchLoop */
 	    inst->branch.mod = mod;
 	case BranchModNone:
+	case BranchModCatch:
 	    break;
 	}
     }
@@ -2915,7 +2941,7 @@
 	    inst->branch.mod = BranchModNone;
 	}
 	
-	CompilePatchLoop (obj, top_inst, continue_inst, obj->used);
+	CompilePatchLoop (obj, top_inst, continue_inst, obj->used, -1);
 	break;
     case DO:
 	/*
@@ -2950,7 +2976,7 @@
 	    inst->branch.offset = top_inst - ObjLast(obj);
 	    inst->branch.mod = BranchModNone;
 	}
-	CompilePatchLoop (obj, top_inst, continue_inst, obj->used);
+	CompilePatchLoop (obj, top_inst, continue_inst, obj->used, -1);
 	break;
     case FOR:
 	/*
@@ -3048,7 +3074,7 @@
 	    inst->branch.mod = BranchModNone;
 	}
 
-	CompilePatchLoop (obj, top_inst, continue_inst, obj->used);
+	CompilePatchLoop (obj, top_inst, continue_inst, obj->used, -1);
 	break;
     case SWITCH:
     case UNION:
@@ -3260,7 +3286,7 @@
 	    inst->branch.offset = obj->used - test_inst;
 	    inst->branch.mod = BranchModNone;
 	}
-	CompilePatchLoop (obj, top_inst, -1, obj->used);
+	CompilePatchLoop (obj, top_inst, -1, obj->used, -1);
 	break;
     case FUNC:
 	obj = CompileDecl (obj, expr, False, expr, code);
@@ -3327,7 +3353,7 @@
     case IMPORT:
 	break;
     case CATCH:
-	obj = CompileCatch (obj, expr->tree.left, expr->tree.right, expr, code);
+	obj = CompileCatch (obj, expr->tree.left, expr->tree.right, expr, code, 0);
 	break;
     case RAISE:
 	obj = CompileRaise (obj, expr, expr, code);
@@ -3346,10 +3372,10 @@
     case OpBranch:
     case OpFarJump:
     case OpDefault:
-    case OpCatch:
     case OpReturn:
     case OpReturnVoid:
     case OpTailCall:
+    case OpCatch:
     case OpRaise:
 	return True;
     default:
@@ -3651,6 +3677,7 @@
     "OpEnterDone",
     "OpLeaveDone",
     "OpFarJump",
+    "OpUnwind",
     /*
      * Expr op codes
      */
@@ -3761,6 +3788,7 @@
     case BranchModContinue:	return "BranchModContinue";
     case BranchModReturn:	return "BranchModReturn";
     case BranchModReturnVoid:	return "BranchModReturnVoid";
+    case BranchModCatch:	return "BranchModCatch";
     }
     return "?";
 }
@@ -3839,12 +3867,16 @@
 	}
 	break;
     case OpFarJump:
-	FilePrintf (FileStdout, "twixt %d catch %d frame %d inst %d mod %s\n",
+	FilePrintf (FileStdout, "twixt %d catch %d frame %d inst %d mod %s",
 		    inst->farJump.farJump->twixt,
 		    inst->farJump.farJump->catch,
 		    inst->farJump.farJump->frame,
 		    inst->farJump.farJump->inst,
 		    BranchModName (inst->farJump.mod));
+	break;
+    case OpUnwind:
+	FilePrintf (FileStdout, "twixt %d catch %d",
+		    inst->unwind.twixt, inst->unwind.catch);
 	break;
     case OpGlobal:
     case OpGlobalRef:

Index: execute.c
===================================================================
RCS file: /local/src/CVS/nickle/execute.c,v
retrieving revision 1.79
retrieving revision 1.80
diff -u -d -r1.79 -r1.80
--- execute.c	3 Jun 2003 06:03:56 -0000	1.79
+++ execute.c	5 Jun 2003 23:51:23 -0000	1.80
@@ -656,6 +656,15 @@
     EXIT ();
 }
 
+static void
+ThreadUnwind (Value thread, int twixt, int catch)
+{
+    while (twixt--)
+	thread->thread.continuation.twixts = thread->thread.continuation.twixts->continuation.twixts;
+    while (catch--)
+	thread->thread.continuation.catches = thread->thread.continuation.catches->continuation.catches;
+}
+
 #define ThreadBoxCheck(box,i,type) (BoxValueGet(box,i) == 0 ? ThreadBoxSetDefault(box,i,type,0) : 0)
 
 typedef struct _TypeChain {
@@ -1290,6 +1299,9 @@
 		    break;
 		case OpFarJump:
 		    ThreadFarJump (thread, value, inst->farJump.farJump, &next);
+		    break;
+		case OpUnwind:
+		    ThreadUnwind (thread, inst->unwind.twixt, inst->unwind.catch);
 		    break;
 		case OpEnd:
 		    SetSignalFinished ();

Index: nickle.h
===================================================================
RCS file: /local/src/CVS/nickle/nickle.h,v
retrieving revision 1.100
retrieving revision 1.101
diff -u -d -r1.100 -r1.101
--- nickle.h	22 Apr 2003 23:25:38 -0000	1.100
+++ nickle.h	5 Jun 2003 23:51:23 -0000	1.101
@@ -416,7 +416,7 @@
 
 typedef enum _branchMod {
     BranchModNone, BranchModBreak, BranchModContinue, 
-    BranchModReturn, BranchModReturnVoid
+    BranchModReturn, BranchModReturnVoid, BranchModCatch
 } BranchMod;
 
 typedef struct _instBranch {

Index: opcode.h
===================================================================
RCS file: /local/src/CVS/nickle/opcode.h,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- opcode.h	2 Aug 2002 06:07:12 -0000	1.23
+++ opcode.h	5 Jun 2003 23:51:23 -0000	1.24
@@ -32,6 +32,7 @@
     OpEnterDone,
     OpLeaveDone,
     OpFarJump,
+    OpUnwind,
     /*
      * Expr op codes
      */




More information about the Commit mailing list