[macruby-changes] [2745] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Oct 6 21:36:55 PDT 2009


Revision: 2745
          http://trac.macosforge.org/projects/ruby/changeset/2745
Author:   lsansonetti at apple.com
Date:     2009-10-06 21:36:52 -0700 (Tue, 06 Oct 2009)
Log Message:
-----------
in the objc->ruby 32-bit stub convert ruby exceptions to objc

Modified Paths:
--------------
    MacRuby/trunk/compiler.cpp
    MacRuby/trunk/compiler.h
    MacRuby/trunk/vm.cpp

Modified: MacRuby/trunk/compiler.cpp
===================================================================
--- MacRuby/trunk/compiler.cpp	2009-10-07 00:27:35 UTC (rev 2744)
+++ MacRuby/trunk/compiler.cpp	2009-10-07 04:36:52 UTC (rev 2745)
@@ -212,11 +212,10 @@
 }
 
 Instruction *
-RoxorCompiler::compile_protected_call(Function *func,
-	std::vector<Value *> &params)
+RoxorCompiler::compile_protected_call(Value *imp, std::vector<Value *> &params)
 {
     if (rescue_invoke_bb == NULL) {
-	CallInst *dispatch = CallInst::Create(func, 
+	CallInst *dispatch = CallInst::Create(imp, 
 		params.begin(), 
 		params.end(), 
 		"", 
@@ -227,7 +226,7 @@
 	BasicBlock *normal_bb = BasicBlock::Create(context, "normal",
 		bb->getParent());
 
-	InvokeInst *dispatch = InvokeInst::Create(func,
+	InvokeInst *dispatch = InvokeInst::Create(imp,
 		normal_bb, 
 		rescue_invoke_bb,
 		params.begin(), 
@@ -1923,6 +1922,19 @@
     }
 }
 
+Value *
+RoxorCompiler::compile_current_exception(void)
+{
+    if (currentExceptionFunc == NULL) {
+	// VALUE rb_vm_current_exception(void);
+	currentExceptionFunc = cast<Function>(
+		module->getOrInsertFunction(
+		    "rb_vm_current_exception", 
+		    RubyObjTy, NULL));
+    }
+    return CallInst::Create(currentExceptionFunc, "", bb);
+}
+
 typedef struct rb_vm_immediate_val {
     int type;
     union {
@@ -4738,17 +4750,7 @@
 	    break;
 
 	case NODE_ERRINFO:
-	    {
-		if (currentExceptionFunc == NULL) {
-		    // VALUE rb_vm_current_exception(void);
-		    currentExceptionFunc = cast<Function>(
-			    module->getOrInsertFunction(
-				"rb_vm_current_exception", 
-				RubyObjTy, NULL));
-		}
-		return CallInst::Create(currentExceptionFunc, "", bb);
-	    }
-	    break;
+	    return compile_current_exception();
 
 	case NODE_ENSURE:
 	    {
@@ -6758,6 +6760,10 @@
     Function::arg_iterator arg = f->arg_begin();
 
     bb = BasicBlock::Create(context, "EntryBlock", f);
+#if !__LP64__
+    // Prepare exception handler (see below).
+    rescue_invoke_bb = BasicBlock::Create(context, "rescue", f);
+#endif
 
     Value *sret = NULL;
     int sret_i = 0;
@@ -6805,8 +6811,7 @@
     }
 
     // Call the Ruby implementation.
-    Value *ret_val = CallInst::Create(imp, params.begin(), params.end(),
-	    "", bb);
+    Value *ret_val = compile_protected_call(imp, params);
 
     // Convert the return value into Objective-C type (if any).
     if (f_ret_type != VoidTy) {
@@ -6822,6 +6827,24 @@
 	ReturnInst::Create(context, bb);
     }
 
+#if !__LP64__
+    // The 32-bit Objective-C runtime doesn't use C++ exceptions, therefore
+    // we must convert Ruby exceptions to Objective-C ones.
+    bb = rescue_invoke_bb;
+    compile_landing_pad_header();
+
+    Function *ruby2ocExcHandlerFunc = NULL;
+    if (ruby2ocExcHandlerFunc == NULL) {
+	// void rb2oc_exc_handler(void);
+	ruby2ocExcHandlerFunc = cast<Function>(
+		module->getOrInsertFunction("rb2oc_exc_handler", VoidTy, NULL));
+    }
+    CallInst::Create(ruby2ocExcHandlerFunc, "", bb);
+
+    compile_landing_pad_footer();
+    new UnreachableInst(context, bb);
+#endif
+
     return f;
 }
 

Modified: MacRuby/trunk/compiler.h
===================================================================
--- MacRuby/trunk/compiler.h	2009-10-07 00:27:35 UTC (rev 2744)
+++ MacRuby/trunk/compiler.h	2009-10-07 04:36:52 UTC (rev 2745)
@@ -254,7 +254,7 @@
 	    return compile_const_pointer(ptr, PtrPtrTy);
 	}
 
-	Instruction *compile_protected_call(Function *func,
+	Instruction *compile_protected_call(Value *imp,
 		std::vector<Value *> &params);
 	void compile_dispatch_arguments(NODE *args,
 		std::vector<Value *> &arguments, int *pargc);
@@ -328,6 +328,7 @@
 	Value *compile_landing_pad_header(void);
 	Value *compile_landing_pad_header(const std::type_info &eh_type);
 	void compile_landing_pad_footer(bool pop_exception=true);
+	Value *compile_current_exception(void);
 	void compile_rethrow_exception(void);
 	void compile_pop_exception(void);
 	Value *compile_lvar_slot(ID name);

Modified: MacRuby/trunk/vm.cpp
===================================================================
--- MacRuby/trunk/vm.cpp	2009-10-07 00:27:35 UTC (rev 2744)
+++ MacRuby/trunk/vm.cpp	2009-10-07 04:36:52 UTC (rev 2745)
@@ -179,6 +179,7 @@
 
 extern "C" void *__cxa_allocate_exception(size_t);
 extern "C" void __cxa_throw(void *, void *, void (*)(void *));
+extern "C" void __cxa_rethrow(void);
 
 RoxorCore::RoxorCore(void)
 {
@@ -2791,8 +2792,24 @@
 #endif
 }
 
+#if !__LP64__
 extern "C"
 void
+rb2oc_exc_handler(void)
+{
+    VALUE exc = GET_VM()->current_exception();
+    if (exc != Qnil) {
+	id ocexc = rb_objc_create_exception(exc);
+	objc_exception_throw(ocexc);
+    }
+    else {
+	__cxa_rethrow();	
+    }
+}
+#endif
+
+extern "C"
+void
 rb_vm_raise_current_exception(void)
 {
     VALUE exception = GET_VM()->current_exception();
@@ -2844,6 +2861,7 @@
 	}
 	throw;
     }
+    return Qnil; // never reached
 }
 
 extern "C"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20091006/6ec0e686/attachment-0001.html>


More information about the macruby-changes mailing list