[macruby-changes] [2630] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Thu Sep 24 21:15:23 PDT 2009


Revision: 2630
          http://trac.macosforge.org/projects/ruby/changeset/2630
Author:   lsansonetti at apple.com
Date:     2009-09-24 21:15:23 -0700 (Thu, 24 Sep 2009)
Log Message:
-----------
moving to LLVM r82747

Revision Links:
--------------
    http://trac.macosforge.org/projects/ruby/changeset/82747

Modified Paths:
--------------
    MacRuby/trunk/README.rdoc
    MacRuby/trunk/bin/rubyc
    MacRuby/trunk/bridgesupport.cpp
    MacRuby/trunk/compiler.cpp
    MacRuby/trunk/compiler.h
    MacRuby/trunk/include/ruby/config.h.in
    MacRuby/trunk/rakelib/builder.rb
    MacRuby/trunk/vm.cpp
    MacRuby/trunk/vm.h

Modified: MacRuby/trunk/README.rdoc
===================================================================
--- MacRuby/trunk/README.rdoc	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/README.rdoc	2009-09-25 04:15:23 UTC (rev 2630)
@@ -21,11 +21,12 @@
 
 * Mac OS X 10.5.6 or later.
 
-* LLVM trunk, compiled for both i386 and x86_64.
+* LLVM ToT, compiled for both i386 and x86_64.
 
-  In case trunk reveals to be unstable we recommend revision 72741.
+  LLVM is a moving target and breaks periodically. We recommend to install
+  revision 82747.
 
-    $ svn co -r 72741 https://llvm.org/svn/llvm-project/llvm/trunk llvm-trunk
+    $ svn co -r 82747 https://llvm.org/svn/llvm-project/llvm/trunk llvm-trunk
     $ cd llvm-trunk
     $ ./configure
     $ UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" ENABLE_OPTIMIZED=1 make

Modified: MacRuby/trunk/bin/rubyc
===================================================================
--- MacRuby/trunk/bin/rubyc	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/bin/rubyc	2009-09-25 04:15:23 UTC (rev 2630)
@@ -101,7 +101,7 @@
 
     # Compile the bitcode as assembly.
     asm = gen_tmpfile(base, 's')
-    execute("#{@llc} -f #{bc} -o=#{asm}")
+    execute("#{@llc} -f #{bc} -o=#{asm} -march=x86-64 -enable-eh")
 
     # Finally compile the assembly.
     execute("#{@gcc} -c -arch x86_64 #{asm} -o #{output}")

Modified: MacRuby/trunk/bridgesupport.cpp
===================================================================
--- MacRuby/trunk/bridgesupport.cpp	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/bridgesupport.cpp	2009-09-25 04:15:23 UTC (rev 2630)
@@ -92,7 +92,7 @@
 	// void rb_vm_check_arity(int given, int requested);
 	checkArityFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_check_arity",
-		    Type::VoidTy, Type::Int32Ty, Type::Int32Ty, NULL));
+		    VoidTy, Int32Ty, Int32Ty, NULL));
     }
 
     std::vector<Value *> params;
@@ -118,12 +118,12 @@
 	// void rb_vm_set_struct(VALUE rcv, int field, VALUE val);
 	setStructFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_set_struct",
-		    Type::VoidTy, RubyObjTy, Type::Int32Ty, RubyObjTy, NULL));
+		    VoidTy, RubyObjTy, Int32Ty, RubyObjTy, NULL));
     }
 
     std::vector<Value *> params;
     params.push_back(rcv);
-    params.push_back(ConstantInt::get(Type::Int32Ty, field));
+    params.push_back(ConstantInt::get(Int32Ty, field));
     params.push_back(val);
 
     CallInst::Create(setStructFunc, params.begin(), params.end(), "", bb);
@@ -140,7 +140,7 @@
     arg++;			// sel
     Value *val = arg++; 	// val
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     assert((unsigned)field < bs_boxed->as.s->fields_count);
     const char *ftype = bs_boxed->as.s->fields[field].type;
@@ -152,7 +152,7 @@
 
     compile_set_struct(self, field, val);
 
-    ReturnInst::Create(val, bb);
+    ReturnInst::Create(context, val, bb);
 
     return f;
 }
@@ -162,7 +162,7 @@
 {
     // VALUE foo(VALUE self, SEL sel, int argc, VALUE *argv);
     Function *f = cast<Function>(module->getOrInsertFunction("",
-		RubyObjTy, RubyObjTy, PtrTy, Type::Int32Ty, RubyObjPtrTy,
+		RubyObjTy, RubyObjTy, PtrTy, Int32Ty, RubyObjPtrTy,
 		NULL));
     Function::arg_iterator arg = f->arg_begin();
     Value *klass = arg++; 	// self
@@ -170,12 +170,12 @@
     Value *argc = arg++; 	// argc
     Value *argv = arg++; 	// argv
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
-    BasicBlock *no_args_bb = BasicBlock::Create("no_args", f);
-    BasicBlock *args_bb  = BasicBlock::Create("args", f);
-    Value *has_args = new ICmpInst(ICmpInst::ICMP_EQ, argc,
-	    ConstantInt::get(Type::Int32Ty, 0), "", bb);
+    BasicBlock *no_args_bb = BasicBlock::Create(context, "no_args", f);
+    BasicBlock *args_bb  = BasicBlock::Create(context, "args", f);
+    Value *has_args = new ICmpInst(*bb, ICmpInst::ICMP_EQ, argc,
+	    ConstantInt::get(Int32Ty, 0));
 
     BranchInst::Create(no_args_bb, args_bb, has_args, bb);
 
@@ -196,10 +196,10 @@
 
 	std::vector<Value *> params;
 	params.push_back(new BitCastInst(fval, PtrTy, "", bb));
-	params.push_back(ConstantInt::get(Type::Int8Ty, 0));
+	params.push_back(ConstantInt::get(Int8Ty, 0));
 	params.push_back(ConstantInt::get(IntTy,
 		    GET_CORE()->get_sizeof(llvm_type)));
-	params.push_back(ConstantInt::get(Type::Int32Ty, 0));
+	params.push_back(ConstantInt::get(Int32Ty, 0));
 	CallInst::Create(memset_func, params.begin(), params.end(), "", bb);
 
 	fval = new LoadInst(fval, "", bb);
@@ -208,7 +208,7 @@
 	fields.push_back(fval);
     }
 
-    ReturnInst::Create(compile_new_struct(klass, fields), bb);
+    ReturnInst::Create(context, compile_new_struct(klass, fields), bb);
 
     // Arguments are given. Need to check given arity, then convert the given
     // Ruby values into the requested struct field types.
@@ -216,14 +216,14 @@
     fields.clear();
 
     compile_check_arity(argc,
-	    ConstantInt::get(Type::Int32Ty, bs_boxed->as.s->fields_count));
+	    ConstantInt::get(Int32Ty, bs_boxed->as.s->fields_count));
 
     for (unsigned i = 0; i < bs_boxed->as.s->fields_count; i++) {
 	const char *ftype = bs_boxed->as.s->fields[i].type;
 	const Type *llvm_type = convert_type(ftype);
 	Value *fval = new AllocaInst(llvm_type, "", bb);
 
-	Value *index = ConstantInt::get(Type::Int32Ty, i);
+	Value *index = ConstantInt::get(Int32Ty, i);
 	Value *arg = GetElementPtrInst::Create(argv, index, "", bb);
 	arg = new LoadInst(arg, "", bb);
 	arg = compile_conversion_to_c(ftype, arg, fval);
@@ -232,7 +232,7 @@
 	fields.push_back(arg);
     }
 
-    ReturnInst::Create(compile_new_struct(klass, fields), bb);
+    ReturnInst::Create(context, compile_new_struct(klass, fields), bb);
 
     return f;
 }
@@ -1233,7 +1233,7 @@
     FunctionType *ft = FunctionType::get(RubyObjTy, f_types, false);
     Function *f = cast<Function>(module->getOrInsertFunction("", ft));
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     Function::arg_iterator arg = f->arg_begin();
     arg++; // skip self
@@ -1247,8 +1247,8 @@
     stub_types.push_back(PtrTy);
 
     // Second argument is arity;
-    params.push_back(ConstantInt::get(Type::Int32Ty, argc));
-    stub_types.push_back(Type::Int32Ty);
+    params.push_back(ConstantInt::get(Int32Ty, argc));
+    stub_types.push_back(Int32Ty);
 
     // Third is an array of arguments.
     Value *argv;
@@ -1257,10 +1257,10 @@
 		"", bb);
     }
     else {
-	argv = new AllocaInst(RubyObjTy, ConstantInt::get(Type::Int32Ty, argc),
+	argv = new AllocaInst(RubyObjTy, ConstantInt::get(Int32Ty, argc),
 		"", bb);
 	for (int i = 0; i < argc; i++) {
-	    Value *index = ConstantInt::get(Type::Int32Ty, i);
+	    Value *index = ConstantInt::get(Int32Ty, i);
 	    Value *slot = GetElementPtrInst::Create(argv, index, "", bb);
 	    new StoreInst(arg++, slot, "", bb);
 	}
@@ -1276,7 +1276,7 @@
     // Call the stub and return its return value.
     CallInst *stub_call = CallInst::Create(stub_val, params.begin(),
 	    params.end(), "", bb); 
-    ReturnInst::Create(stub_call, bb);
+    ReturnInst::Create(context, stub_call, bb);
 
     return f;
 }

Modified: MacRuby/trunk/compiler.cpp
===================================================================
--- MacRuby/trunk/compiler.cpp	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/compiler.cpp	2009-09-25 04:15:23 UTC (rev 2630)
@@ -24,9 +24,6 @@
 llvm::Module *RoxorCompiler::module = NULL;
 RoxorCompiler *RoxorCompiler::shared = NULL;
 
-AnnotationID RoxorFunctionAnnotation::id =
-	AnnotationManager::getID("RoxorFunction");
-
 RoxorCompiler::RoxorCompiler(void)
 {
     fname = "";
@@ -60,6 +57,7 @@
     return_from_block = -1;
     return_from_block_ids = 0;
     ensure_pn = NULL;
+    current_scope = NULL;
 
     dispatcherFunc = NULL;
     fastPlusFunc = NULL;
@@ -133,12 +131,19 @@
     setCurrentClassFunc = NULL;
     getCacheFunc = NULL;
 
-    func_annotation = NULL;
+    VoidTy = Type::getVoidTy(context);
+    Int1Ty = Type::getInt1Ty(context);
+    Int8Ty = Type::getInt8Ty(context);
+    Int16Ty = Type::getInt16Ty(context);
+    Int32Ty = Type::getInt32Ty(context);
+    Int64Ty = Type::getInt64Ty(context);
+    FloatTy = Type::getFloatTy(context);
+    DoubleTy = Type::getDoubleTy(context);
 
 #if __LP64__
-    RubyObjTy = IntTy = Type::Int64Ty;
+    RubyObjTy = IntTy = Int64Ty;
 #else
-    RubyObjTy = IntTy = Type::Int32Ty;
+    RubyObjTy = IntTy = Int32Ty;
 #endif
 
     zeroVal = ConstantInt::get(IntTy, 0);
@@ -146,8 +151,8 @@
     twoVal = ConstantInt::get(IntTy, 2);
     threeVal = ConstantInt::get(IntTy, 3);
 
-    defaultScope = ConstantInt::get(Type::Int32Ty, SCOPE_DEFAULT);
-    publicScope = ConstantInt::get(Type::Int32Ty, SCOPE_PUBLIC);
+    defaultScope = ConstantInt::get(Int32Ty, SCOPE_DEFAULT);
+    publicScope = ConstantInt::get(Int32Ty, SCOPE_PUBLIC);
 
     RubyObjPtrTy = PointerType::getUnqual(RubyObjTy);
     RubyObjPtrPtrTy = PointerType::getUnqual(RubyObjPtrTy);
@@ -156,9 +161,9 @@
     falseVal = ConstantInt::get(RubyObjTy, Qfalse);
     undefVal = ConstantInt::get(RubyObjTy, Qundef);
     splatArgFollowsVal = ConstantInt::get(RubyObjTy, SPLAT_ARG_FOLLOWS);
-    PtrTy = PointerType::getUnqual(Type::Int8Ty);
+    PtrTy = PointerType::getUnqual(Int8Ty);
     PtrPtrTy = PointerType::getUnqual(PtrTy);
-    Int32PtrTy = PointerType::getUnqual(Type::Int32Ty);
+    Int32PtrTy = PointerType::getUnqual(Int32Ty);
 
 #if ROXOR_COMPILER_DEBUG
     level = 0;
@@ -203,7 +208,7 @@
 RoxorCompiler::is_value_a_fixnum(Value *val)
 {
     Value *andOp = BinaryOperator::CreateAnd(val, oneVal, "", bb);
-    return new ICmpInst(ICmpInst::ICMP_EQ, andOp, oneVal, "", bb); 
+    return new ICmpInst(*bb, ICmpInst::ICMP_EQ, andOp, oneVal);
 }
 
 Instruction *
@@ -219,7 +224,8 @@
 	return dispatch;
     }
     else {
-	BasicBlock *normal_bb = BasicBlock::Create("normal", bb->getParent());
+	BasicBlock *normal_bb = BasicBlock::Create(context, "normal",
+		bb->getParent());
 
 	InvokeInst *dispatch = InvokeInst::Create(func,
 		normal_bb, 
@@ -246,8 +252,8 @@
 	params.push_back(subnodeVal);
 	params.push_back(compile_sel(selEqq));
 	params.push_back(compile_const_pointer(NULL));
-	params.push_back(ConstantInt::get(Type::Int8Ty, 0));
-	params.push_back(ConstantInt::get(Type::Int32Ty, 1));
+	params.push_back(ConstantInt::get(Int8Ty, 0));
+	params.push_back(ConstantInt::get(Int32Ty, 1));
 	params.push_back(comparedToVal);
 
 	condVal = compile_optimized_dispatch_call(selEqq, 1, params);
@@ -260,21 +266,25 @@
     }
 
     Function *f = bb->getParent();
-    BasicBlock *nextTestBB = BasicBlock::Create("next_test", f);
+    BasicBlock *nextTestBB = BasicBlock::Create(context, "next_test", f);
 
     compile_boolean_test(condVal, thenBB, nextTestBB);
 
     bb = nextTestBB;
 }
 
-void RoxorCompiler::compile_boolean_test(Value *condVal, BasicBlock *ifTrueBB, BasicBlock *ifFalseBB)
+void
+RoxorCompiler::compile_boolean_test(Value *condVal, BasicBlock *ifTrueBB,
+	BasicBlock *ifFalseBB)
 {
     Function *f = bb->getParent();
-    BasicBlock *notFalseBB = BasicBlock::Create("not_false", f);
+    BasicBlock *notFalseBB = BasicBlock::Create(context, "not_false", f);
 
-    Value *notFalseCond = new ICmpInst(ICmpInst::ICMP_NE, condVal, falseVal, "", bb);
+    Value *notFalseCond = new ICmpInst(*bb, ICmpInst::ICMP_NE, condVal,
+	    falseVal);
     BranchInst::Create(notFalseBB, ifFalseBB, notFalseCond, bb);
-    Value *notNilCond = new ICmpInst(ICmpInst::ICMP_NE, condVal, nilVal, "", notFalseBB);
+    Value *notNilCond = new ICmpInst(*notFalseBB, ICmpInst::ICMP_NE, condVal,
+	    nilVal);
     BranchInst::Create(ifTrueBB, ifFalseBB, notNilCond, notFalseBB);
 }
 
@@ -291,9 +301,11 @@
 
 	case NODE_SPLAT:
 	    {
-		Value *condVal = compile_when_splat(comparedToVal, compile_node(args->nd_head));
+		Value *condVal = compile_when_splat(comparedToVal,
+			compile_node(args->nd_head));
 
-		BasicBlock *nextTestBB = BasicBlock::Create("next_test", bb->getParent());
+		BasicBlock *nextTestBB = BasicBlock::Create(context,
+			"next_test", bb->getParent());
 		compile_boolean_test(condVal, thenBB, nextTestBB);
 
 		bb = nextTestBB;
@@ -312,19 +324,20 @@
 }
 
 Function::ArgumentListType::iterator
-RoxorCompiler::compile_optional_arguments(Function::ArgumentListType::iterator iter,
-					  NODE *node)
+RoxorCompiler::compile_optional_arguments(
+	Function::ArgumentListType::iterator iter, NODE *node)
 {
     assert(nd_type(node) == NODE_OPT_ARG);
 
     do {
 	assert(node->nd_value != NULL);
 
-	Value *isUndefInst = new ICmpInst(ICmpInst::ICMP_EQ, iter, undefVal, "", bb);
+	Value *isUndefInst = new ICmpInst(*bb, ICmpInst::ICMP_EQ, iter,
+		undefVal);
 
 	Function *f = bb->getParent();
-	BasicBlock *arg_undef = BasicBlock::Create("arg_undef", f);
-	BasicBlock *next_bb = BasicBlock::Create("", f);
+	BasicBlock *arg_undef = BasicBlock::Create(context, "arg_undef", f);
+	BasicBlock *next_bb = BasicBlock::Create(context, "", f);
 
 	BranchInst::Create(arg_undef, next_bb, isUndefInst, bb);
 
@@ -453,7 +466,7 @@
 	//			  VALUE comparedTo, VALUE splat)
 	whenSplatFunc = cast<Function>
 	    (module->getOrInsertFunction("rb_vm_when_splat",
-					 RubyObjTy, PtrTy, Type::Int1Ty,
+					 RubyObjTy, PtrTy, Int1Ty,
 					 RubyObjTy, RubyObjTy, NULL));
     }
 
@@ -478,20 +491,16 @@
 
     GlobalVariable *gvar;
     if (iter == static_ustrings.end()) {
-	const ArrayType *str_type = ArrayType::get(Type::Int16Ty, len);
+	const ArrayType *str_type = ArrayType::get(Int16Ty, len);
 
 	std::vector<Constant *> ary_elements;
 	for (unsigned int i = 0; i < len; i++) {
-	    ary_elements.push_back(ConstantInt::get(Type::Int16Ty, str[i]));
+	    ary_elements.push_back(ConstantInt::get(Int16Ty, str[i]));
 	}
 
-	gvar = new GlobalVariable(
-		str_type,
-		true,
+	gvar = new GlobalVariable(*RoxorCompiler::module, str_type, true,
 		GlobalValue::InternalLinkage,
-		ConstantArray::get(str_type, ary_elements),
-		"",
-		RoxorCompiler::module);
+		ConstantArray::get(str_type, ary_elements), "");
 
 	static_ustrings[hash] = gvar;
     }
@@ -514,21 +523,17 @@
 
     GlobalVariable *gvar;
     if (iter == static_strings.end()) {
-	const ArrayType *str_type = ArrayType::get(Type::Int8Ty, len + 1);
+	const ArrayType *str_type = ArrayType::get(Int8Ty, len + 1);
 
 	std::vector<Constant *> ary_elements;
 	for (unsigned int i = 0; i < len; i++) {
-	    ary_elements.push_back(ConstantInt::get(Type::Int8Ty, str[i]));
+	    ary_elements.push_back(ConstantInt::get(Int8Ty, str[i]));
 	}
-	ary_elements.push_back(ConstantInt::get(Type::Int8Ty, 0));
+	ary_elements.push_back(ConstantInt::get(Int8Ty, 0));
 	
-	gvar = new GlobalVariable(
-		str_type,
-		true,
+	gvar = new GlobalVariable(*RoxorCompiler::module, str_type, true,
 		GlobalValue::InternalLinkage,
-		ConstantArray::get(str_type, ary_elements),
-		"",
-		RoxorCompiler::module);
+		ConstantArray::get(str_type, ary_elements), "");
 
 	static_strings[s] = gvar;
     }
@@ -546,13 +551,13 @@
 	// void *rb_vm_get_call_cache2(SEL sel, unsigned char super);
 	getCacheFunc = 
 	    cast<Function>(module->getOrInsertFunction(
-			"rb_vm_get_call_cache2", PtrTy, PtrTy, Type::Int8Ty,
+			"rb_vm_get_call_cache2", PtrTy, PtrTy, Int8Ty,
 			NULL));
     }
 
     std::vector<Value *> params;
     params.push_back(sel);
-    params.push_back(ConstantInt::get(Type::Int8Ty, super ? 1 : 0));
+    params.push_back(ConstantInt::get(Int8Ty, super ? 1 : 0));
 
     return CallInst::Create(getCacheFunc, params.begin(), params.end(), "", bb);
 }
@@ -576,13 +581,9 @@
     GlobalVariable *gvar;
     std::map<SEL, GlobalVariable *>::iterator iter = mcaches.find(sel);
     if (iter == mcaches.end()) {
-	gvar = new GlobalVariable(
-		PtrTy,
-		false,
-		GlobalValue::InternalLinkage,
-		Constant::getNullValue(PtrTy),
-		"",
-		RoxorCompiler::module);
+	gvar = new GlobalVariable(*RoxorCompiler::module, PtrTy, false,
+		GlobalValue::InternalLinkage, Constant::getNullValue(PtrTy),
+		"");
 	assert(gvar != NULL);
 	mcaches[sel] = gvar;
     }
@@ -606,13 +607,9 @@
 	ccaches.find(name);
     GlobalVariable *gvar;
     if (iter == ccaches.end()) {
-	gvar = new GlobalVariable(
-		PtrTy,
-		false,
-		GlobalValue::InternalLinkage,
-		Constant::getNullValue(PtrTy),
-		"",
-		RoxorCompiler::module);
+	gvar = new GlobalVariable(*RoxorCompiler::module, PtrTy, false,
+		GlobalValue::InternalLinkage, Constant::getNullValue(PtrTy),
+		"");
 	assert(gvar != NULL);
 	ccaches[name] = gvar;
     }
@@ -628,13 +625,9 @@
     std::map<SEL, GlobalVariable *>::iterator iter = sels.find(sel);
     GlobalVariable *gvar;
     if (iter == sels.end()) {
-	gvar = new GlobalVariable(
-		PtrTy,
-		false,
-		GlobalValue::InternalLinkage,
-		Constant::getNullValue(PtrTy),
-		"",
-		RoxorCompiler::module);
+	gvar = new GlobalVariable(*RoxorCompiler::module, PtrTy, false,
+		GlobalValue::InternalLinkage, Constant::getNullValue(PtrTy),
+		"");
 	assert(gvar != NULL);
 	sels[sel] = gvar;
     }
@@ -652,7 +645,7 @@
     uint64_t v;
     assert(sizeof(uint64_t) == sizeof(rb_vm_arity_t));
     memcpy(&v, &arity, sizeof(rb_vm_arity_t));
-    return ConstantInt::get(Type::Int64Ty, v);
+    return ConstantInt::get(Int64Ty, v);
 }
 
 void
@@ -665,21 +658,21 @@
 	prepareMethodFunc = 
 	    cast<Function>(module->getOrInsertFunction(
 			"rb_vm_prepare_method",
-			Type::VoidTy, RubyObjTy, Type::Int8Ty, PtrTy, PtrTy,
-			Type::Int64Ty, Type::Int32Ty, NULL));
+			VoidTy, RubyObjTy, Int8Ty, PtrTy, PtrTy, Int64Ty,
+			Int32Ty, NULL));
     }
 
     std::vector<Value *> params;
 
     params.push_back(classVal);
-    params.push_back(ConstantInt::get(Type::Int8Ty,
+    params.push_back(ConstantInt::get(Int8Ty,
 		!singleton && dynamic_class ? 1 : 0));
     params.push_back(sel);
 
     params.push_back(compile_const_pointer(new_function));
     rb_objc_retain((void *)body);
     params.push_back(compile_arity(arity));
-    params.push_back(ConstantInt::get(Type::Int32Ty, rb_vm_node_flags(body)));
+    params.push_back(ConstantInt::get(Int32Ty, rb_vm_node_flags(body)));
 
     CallInst::Create(prepareMethodFunc, params.begin(),
 	    params.end(), "", bb);
@@ -695,14 +688,14 @@
 	prepareMethodFunc = 
 	    cast<Function>(module->getOrInsertFunction(
 			"rb_vm_prepare_method2",
-			Type::VoidTy, RubyObjTy, Type::Int8Ty, PtrTy, PtrTy,
-			Type::Int64Ty, Type::Int32Ty, NULL));
+			VoidTy, RubyObjTy, Int8Ty, PtrTy, PtrTy, Int64Ty,
+			Int32Ty, NULL));
     }
 
     std::vector<Value *> params;
 
     params.push_back(classVal);
-    params.push_back(ConstantInt::get(Type::Int8Ty,
+    params.push_back(ConstantInt::get(Int8Ty,
 		!singleton && dynamic_class ? 1 : 0));
     params.push_back(sel);
 
@@ -713,7 +706,7 @@
 
     params.push_back(compile_arity(arity));
 
-    params.push_back(ConstantInt::get(Type::Int32Ty, rb_vm_node_flags(body)));
+    params.push_back(ConstantInt::get(Int32Ty, rb_vm_node_flags(body)));
 
     CallInst::Create(prepareMethodFunc, params.begin(),
 	    params.end(), "", bb);
@@ -730,15 +723,15 @@
 	types.push_back(RubyObjTy);
 	types.push_back(PtrTy);
 	types.push_back(PtrTy);
-	types.push_back(Type::Int8Ty);
-	types.push_back(Type::Int32Ty);
+	types.push_back(Int8Ty);
+	types.push_back(Int32Ty);
 	FunctionType *ft = FunctionType::get(RubyObjTy, types, true);
 	dispatcherFunc = cast<Function>
 	    (module->getOrInsertFunction("rb_vm_dispatch", ft));
     }
 
-    assert(func_annotation != NULL);
-    func_annotation->dispatch_lines.push_back(current_line);
+    assert(current_scope != NULL);
+    current_scope->dispatch_lines.push_back(current_line);
 
     return compile_protected_call(dispatcherFunc, params);
 }
@@ -779,8 +772,8 @@
     if (recv == current_self) {
 	opt = DISPATCH_SELF_ATTRASGN;
     }
-    params.push_back(ConstantInt::get(Type::Int8Ty, opt));
-    params.push_back(ConstantInt::get(Type::Int32Ty, argc));
+    params.push_back(ConstantInt::get(Int8Ty, opt));
+    params.push_back(ConstantInt::get(Int32Ty, argc));
     for (std::vector<Value *>::iterator i = args.begin();
 	 i != args.end();
 	 ++i) {
@@ -852,19 +845,19 @@
 	// VALUE rb_vm_masgn_get_elem_before_splat(VALUE ary, int offset);
 	masgnGetElemBeforeSplatFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_masgn_get_elem_before_splat",
-		    RubyObjTy, RubyObjTy, Type::Int32Ty, NULL));
+		    RubyObjTy, RubyObjTy, Int32Ty, NULL));
     }
     if (masgnGetElemAfterSplatFunc == NULL) {
 	// VALUE rb_vm_masgn_get_elem_after_splat(VALUE ary, int before_splat_count, int after_splat_count, int offset);
 	masgnGetElemAfterSplatFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_masgn_get_elem_after_splat",
-		    RubyObjTy, RubyObjTy, Type::Int32Ty, Type::Int32Ty, Type::Int32Ty, NULL));
+		    RubyObjTy, RubyObjTy, Int32Ty, Int32Ty, Int32Ty, NULL));
     }
     if (masgnGetSplatFunc == NULL) {
 	// VALUE rb_vm_masgn_get_splat(VALUE ary, int before_splat_count, int after_splat_count);
 	masgnGetSplatFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_masgn_get_splat",
-		    RubyObjTy, RubyObjTy, Type::Int32Ty, Type::Int32Ty, NULL));
+		    RubyObjTy, RubyObjTy, Int32Ty, Int32Ty, NULL));
     }
 
     NODE *before_splat = node->nd_head, *after_splat = NULL, *splat = NULL;
@@ -902,7 +895,7 @@
     for (int i = 0; l != NULL; ++i) {
 	std::vector<Value *> params;
 	params.push_back(val);
-	params.push_back(ConstantInt::get(Type::Int32Ty, i));
+	params.push_back(ConstantInt::get(Int32Ty, i));
 	Value *elt = CallInst::Create(masgnGetElemBeforeSplatFunc, params.begin(),
 		params.end(), "", bb);
 
@@ -914,8 +907,8 @@
     if (splat != NULL && splat != (NODE *)-1) {
 	std::vector<Value *> params;
 	params.push_back(val);
-	params.push_back(ConstantInt::get(Type::Int32Ty, before_splat_count));
-	params.push_back(ConstantInt::get(Type::Int32Ty, after_splat_count));
+	params.push_back(ConstantInt::get(Int32Ty, before_splat_count));
+	params.push_back(ConstantInt::get(Int32Ty, after_splat_count));
 	Value *elt = CallInst::Create(masgnGetSplatFunc, params.begin(),
 		params.end(), "", bb);
 
@@ -926,9 +919,9 @@
     for (int i = 0; l != NULL; ++i) {
 	std::vector<Value *> params;
 	params.push_back(val);
-	params.push_back(ConstantInt::get(Type::Int32Ty, before_splat_count));
-	params.push_back(ConstantInt::get(Type::Int32Ty, after_splat_count));
-	params.push_back(ConstantInt::get(Type::Int32Ty, i));
+	params.push_back(ConstantInt::get(Int32Ty, before_splat_count));
+	params.push_back(ConstantInt::get(Int32Ty, after_splat_count));
+	params.push_back(ConstantInt::get(Int32Ty, i));
 	Value *elt = CallInst::Create(masgnGetElemAfterSplatFunc, params.begin(),
 		params.end(), "", bb);
 
@@ -981,12 +974,12 @@
 	//	int dvars_size, ...);
 	std::vector<const Type *> types;
 	types.push_back(PtrTy);
-	types.push_back(Type::Int32Ty);
+	types.push_back(Int32Ty);
 	types.push_back(RubyObjTy);
-	types.push_back(Type::Int64Ty);
+	types.push_back(Int64Ty);
 	types.push_back(PtrPtrTy);
 	types.push_back(PtrTy);
-	types.push_back(Type::Int32Ty);
+	types.push_back(Int32Ty);
 	FunctionType *ft = FunctionType::get(PtrTy, types, true);
 	prepareBlockFunc = cast<Function>
 	    (module->getOrInsertFunction("rb_vm_prepare_block", ft));
@@ -999,7 +992,7 @@
 	&& current_block_node->nd_body == NULL) {
 	flags |= VM_BLOCK_EMPTY;
     }
-    params.push_back(ConstantInt::get(Type::Int32Ty, flags));
+    params.push_back(ConstantInt::get(Int32Ty, flags));
     params.push_back(current_self);
     rb_vm_arity_t arity = rb_vm_node_arity(current_block_node);
     params.push_back(compile_arity(arity));
@@ -1009,14 +1002,14 @@
 	    ? compile_const_pointer(NULL) : running_block);
 
     // Dvars.
-    params.push_back(ConstantInt::get(Type::Int32Ty, (int)dvars.size()));
+    params.push_back(ConstantInt::get(Int32Ty, (int)dvars.size()));
     for (std::vector<ID>::iterator iter = dvars.begin();
 	 iter != dvars.end(); ++iter) {
 	params.push_back(compile_lvar_slot(*iter));
     }
 
     // Lvars.
-    params.push_back(ConstantInt::get(Type::Int32Ty, (int)lvars.size()));
+    params.push_back(ConstantInt::get(Int32Ty, (int)lvars.size()));
     for (std::map<ID, Value *>::iterator iter = lvars.begin();
 	 iter != lvars.end(); ++iter) {
 	ID name = iter->first;
@@ -1040,8 +1033,8 @@
 	types.push_back(RubyObjTy);
 	types.push_back(PtrTy);
 	types.push_back(PtrPtrTy);
-	types.push_back(Type::Int32Ty);
-	FunctionType *ft = FunctionType::get(Type::VoidTy, types, true);
+	types.push_back(Int32Ty);
+	FunctionType *ft = FunctionType::get(VoidTy, types, true);
 	pushBindingFunc = cast<Function>
 	    (module->getOrInsertFunction("rb_vm_push_binding", ft));
     }
@@ -1059,7 +1052,7 @@
     }
 
     // Lvars.
-    params.push_back(ConstantInt::get(Type::Int32Ty, (int)lvars.size()));
+    params.push_back(ConstantInt::get(Int32Ty, (int)lvars.size()));
     for (std::map<ID, Value *>::iterator iter = lvars.begin();
 	 iter != lvars.end(); ++iter) {
 
@@ -1083,16 +1076,10 @@
 Instruction *
 RoxorAOTCompiler::gen_slot_cache(ID id)
 {
-    GlobalVariable *gvar = new GlobalVariable(
-	    Int32PtrTy,
-	    false,
-	    GlobalValue::InternalLinkage,
-	    Constant::getNullValue(Int32PtrTy),
-	    "",
-	    RoxorCompiler::module);
-
+    GlobalVariable *gvar = new GlobalVariable(*RoxorCompiler::module,
+	    Int32PtrTy, false, GlobalValue::InternalLinkage,
+	    Constant::getNullValue(Int32PtrTy), "");
     ivar_slots.push_back(gvar);
-
     return new LoadInst(gvar, "");
 }
 
@@ -1117,7 +1104,7 @@
 	slot = iter->second;
     }
 
-    Instruction *insn = slot->clone();
+    Instruction *insn = slot->clone(context);
     BasicBlock::InstListType &list = bb->getInstList();
     list.insert(list.end(), insn);
     return insn;
@@ -1148,7 +1135,7 @@
 	// void rb_vm_ivar_set(VALUE obj, ID name, VALUE val, int *slot_cache);
 	setIvarFunc = 
 	    cast<Function>(module->getOrInsertFunction("rb_vm_ivar_set",
-			Type::VoidTy, RubyObjTy, IntTy, RubyObjTy, Int32PtrTy,
+			VoidTy, RubyObjTy, IntTy, RubyObjTy, Int32PtrTy,
 			NULL)); 
     }
 
@@ -1172,16 +1159,15 @@
 	//	unsigned char dynamic_class);
 	cvarGetFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_cvar_get", 
-		    RubyObjTy, RubyObjTy, IntTy, Type::Int8Ty, Type::Int8Ty,
-		    NULL));
+		    RubyObjTy, RubyObjTy, IntTy, Int8Ty, Int8Ty, NULL));
     }
 
     std::vector<Value *> params;
 
     params.push_back(compile_current_class());
     params.push_back(compile_id(id));
-    params.push_back(ConstantInt::get(Type::Int8Ty, check ? 1 : 0));
-    params.push_back(ConstantInt::get(Type::Int8Ty, dynamic_class ? 1 : 0));
+    params.push_back(ConstantInt::get(Int8Ty, check ? 1 : 0));
+    params.push_back(ConstantInt::get(Int8Ty, dynamic_class ? 1 : 0));
 
     return compile_protected_call(cvarGetFunc, params);
 }
@@ -1194,7 +1180,7 @@
 	//	unsigned char dynamic_class);
 	cvarSetFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_cvar_set", 
-		    RubyObjTy, RubyObjTy, IntTy, RubyObjTy, Type::Int8Ty, NULL));
+		    RubyObjTy, RubyObjTy, IntTy, RubyObjTy, Int8Ty, NULL));
     }
 
     std::vector<Value *> params;
@@ -1202,7 +1188,7 @@
     params.push_back(compile_current_class());
     params.push_back(compile_id(name));
     params.push_back(val);
-    params.push_back(ConstantInt::get(Type::Int8Ty, dynamic_class ? 1 : 0));
+    params.push_back(ConstantInt::get(Int8Ty, dynamic_class ? 1 : 0));
 
     return CallInst::Create(cvarSetFunc, params.begin(),
 	    params.end(), "", bb);
@@ -1235,7 +1221,7 @@
 	//	unsigned char dynamic_class);
 	setConstFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_set_const",
-		    Type::VoidTy, RubyObjTy, IntTy, RubyObjTy, Type::Int8Ty,
+		    VoidTy, RubyObjTy, IntTy, RubyObjTy, Int8Ty,
 		    NULL));
     }
 
@@ -1253,7 +1239,7 @@
 	params.push_back(compile_id(node->nd_else->nd_mid));
     }
     params.push_back(val);
-    params.push_back(ConstantInt::get(Type::Int8Ty,
+    params.push_back(ConstantInt::get(Int8Ty,
 		dynamic_class && outer ? 1 : 0));
 
     CallInst::Create(setConstFunc, params.begin(), params.end(), "", bb);
@@ -1280,13 +1266,8 @@
 RoxorAOTCompiler::compile_nsobject(void)
 {
     if (cObject_gvar == NULL) {
-	cObject_gvar = new GlobalVariable(
-		RubyObjTy,
-		false,
-		GlobalValue::InternalLinkage,
-		zeroVal,
-		"NSObject",
-		RoxorCompiler::module);
+	cObject_gvar = new GlobalVariable(*RoxorCompiler::module, RubyObjTy,
+		false, GlobalValue::InternalLinkage, zeroVal, "NSObject");
 	class_gvars.push_back(cObject_gvar);
     }
     return new LoadInst(cObject_gvar, "", bb);
@@ -1302,13 +1283,9 @@
 RoxorAOTCompiler::compile_standarderror(void)
 {
     if (cStandardError_gvar == NULL) {
-	cStandardError_gvar = new GlobalVariable(
-		RubyObjTy,
-		false,
-		GlobalValue::InternalLinkage,
-		zeroVal,
-		"StandardError",
-		RoxorCompiler::module);
+	cStandardError_gvar = new GlobalVariable(*RoxorCompiler::module,
+		RubyObjTy, false, GlobalValue::InternalLinkage, zeroVal,
+		"StandardError");
 	class_gvars.push_back(cStandardError_gvar);
     }
     return new LoadInst(cStandardError_gvar, "", bb);
@@ -1327,13 +1304,8 @@
 
     GlobalVariable *gvar;
     if (iter == ids.end()) {
-	gvar = new GlobalVariable(
-		IntTy,
-		false,
-		GlobalValue::InternalLinkage,
-		ConstantInt::get(IntTy, 0),
-		"",
-		RoxorCompiler::module);
+	gvar = new GlobalVariable(*RoxorCompiler::module, IntTy, false,
+		GlobalValue::InternalLinkage, ConstantInt::get(IntTy, 0), "");
 	ids[id] = gvar;
     }
     else {
@@ -1357,17 +1329,17 @@
 	//	struct ccache *cache, ID id, unsigned char dynamic_class);
 	getConstFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_get_const", 
-		    RubyObjTy, RubyObjTy, Type::Int8Ty, PtrTy, IntTy, Type::Int8Ty,
+		    RubyObjTy, RubyObjTy, Int8Ty, PtrTy, IntTy, Int8Ty,
 		    NULL));
     }
 
     std::vector<Value *> params;
 
     params.push_back(outer);
-    params.push_back(ConstantInt::get(Type::Int8Ty, outer_given ? 0 : 1));
+    params.push_back(ConstantInt::get(Int8Ty, outer_given ? 0 : 1));
     params.push_back(compile_ccache(id));
     params.push_back(compile_id(id));
-    params.push_back(ConstantInt::get(Type::Int8Ty, dynamic_class ? 1 : 0));
+    params.push_back(ConstantInt::get(Int8Ty, dynamic_class ? 1 : 0));
 
     return compile_protected_call(getConstFunc, params);
 }
@@ -1447,8 +1419,8 @@
     // exception.
     Function *f = bb->getParent(); 
     BasicBlock *old_rescue_invoke_bb = rescue_invoke_bb;
-    BasicBlock *new_rescue_invoke_bb = BasicBlock::Create("rescue", f);
-    BasicBlock *merge_bb = BasicBlock::Create("merge", f);
+    BasicBlock *new_rescue_invoke_bb = BasicBlock::Create(context, "rescue", f);
+    BasicBlock *merge_bb = BasicBlock::Create(context, "merge", f);
     rescue_invoke_bb = new_rescue_invoke_bb;
 
     // Prepare arguments for the runtime.
@@ -1523,14 +1495,14 @@
 	// VALUE rb_vm_defined(VALUE self, int type, VALUE what, VALUE what2);
 	definedFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_defined",
-		    RubyObjTy, RubyObjTy, Type::Int32Ty, RubyObjTy, RubyObjTy,
+		    RubyObjTy, RubyObjTy, Int32Ty, RubyObjTy, RubyObjTy,
 		    NULL));
     }
 
     std::vector<Value *> params;
 
     params.push_back(self);
-    params.push_back(ConstantInt::get(Type::Int32Ty, type));
+    params.push_back(ConstantInt::get(Int32Ty, type));
     params.push_back(what1 == NULL ? nilVal : what1);
     params.push_back(what2 == NULL ? nilVal : what2);
 
@@ -1574,18 +1546,19 @@
 
     const int count = params.size();
 
-    params.insert(params.begin(), ConstantInt::get(Type::Int32Ty, count));
+    params.insert(params.begin(), ConstantInt::get(Int32Ty, count));
 
     if (newStringFunc == NULL) {
 	// VALUE rb_str_new_fast(int argc, ...)
 	std::vector<const Type *> types;
-	types.push_back(Type::Int32Ty);
+	types.push_back(Int32Ty);
 	FunctionType *ft = FunctionType::get(RubyObjTy, types, true);
 	newStringFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_str_new_fast", ft));
     }
 
-    return CallInst::Create(newStringFunc, params.begin(), params.end(), "", bb);
+    return CallInst::Create(newStringFunc, params.begin(), params.end(), "",
+	    bb);
 }
 
 Value *
@@ -1611,8 +1584,9 @@
     ++fargs_i; // skip sel
     Value *dvars_ary = fargs_i;
 
-    Value *index = ConstantInt::get(Type::Int32Ty, idx);
-    Value *slot = GetElementPtrInst::Create(dvars_ary, index, rb_id2name(name), bb);
+    Value *index = ConstantInt::get(Int32Ty, idx);
+    Value *slot = GetElementPtrInst::Create(dvars_ary, index, rb_id2name(name),
+	    bb);
     return new LoadInst(slot, "", bb);
 }
 
@@ -1623,7 +1597,7 @@
 	// void rb_vm_break(VALUE val);
 	breakFunc = cast<Function>(
 		module->getOrInsertFunction("rb_vm_break", 
-		    Type::VoidTy, RubyObjTy, NULL));
+		    VoidTy, RubyObjTy, NULL));
     }
     std::vector<Value *> params;
     params.push_back(val);
@@ -1637,11 +1611,11 @@
 	// void rb_vm_return_from_block(VALUE val, int id);
 	returnFromBlockFunc = cast<Function>(
 		module->getOrInsertFunction("rb_vm_return_from_block", 
-		    Type::VoidTy, RubyObjTy, Type::Int32Ty, NULL));
+		    VoidTy, RubyObjTy, Int32Ty, NULL));
     }
     std::vector<Value *> params;
     params.push_back(val);
-    params.push_back(ConstantInt::get(Type::Int32Ty, id));
+    params.push_back(ConstantInt::get(Int32Ty, id));
     compile_protected_call(returnFromBlockFunc, params);
 }
 
@@ -1657,25 +1631,25 @@
 	checkReturnFromBlockFunc = cast<Function>(
 		module->getOrInsertFunction(
 		    "rb_vm_check_return_from_block_exc", 
-		    RubyObjTy, PtrTy, Type::Int32Ty, NULL));
+		    RubyObjTy, PtrTy, Int32Ty, NULL));
     }
 
     std::vector<Value *> params;
     params.push_back(exception);
-    params.push_back(ConstantInt::get(Type::Int32Ty, id));
+    params.push_back(ConstantInt::get(Int32Ty, id));
     Value *val = CallInst::Create(checkReturnFromBlockFunc, params.begin(),
 	    params.end(), "", bb);
 
     Function *f = bb->getParent();
-    BasicBlock *ret_bb = BasicBlock::Create("ret", f);
-    BasicBlock *rethrow_bb  = BasicBlock::Create("rethrow", f);
-    Value *need_ret = new ICmpInst(ICmpInst::ICMP_NE, val,
-	    ConstantInt::get(RubyObjTy, Qundef), "", bb);
+    BasicBlock *ret_bb = BasicBlock::Create(context, "ret", f);
+    BasicBlock *rethrow_bb  = BasicBlock::Create(context, "rethrow", f);
+    Value *need_ret = new ICmpInst(*bb, ICmpInst::ICMP_NE, val,
+	    ConstantInt::get(RubyObjTy, Qundef));
     BranchInst::Create(ret_bb, rethrow_bb, need_ret, bb);
 
     bb = ret_bb;
     compile_landing_pad_footer(false);
-    ReturnInst::Create(val, bb);	
+    ReturnInst::Create(context, val, bb);	
 
     bb = rethrow_bb;
     compile_rethrow_exception();
@@ -1715,7 +1689,7 @@
 	    }
 	    else if (within_block) {
 		compile_break_val(val);
-		ReturnInst::Create(val, bb);
+		ReturnInst::Create(context, val, bb);
 	    }
 	    else {
 		rb_raise(rb_eLocalJumpError, "unexpected break");
@@ -1727,7 +1701,7 @@
 		BranchInst::Create(current_loop_begin_bb, bb);
 	    }
 	    else if (within_block) {
-		ReturnInst::Create(val, bb);
+		ReturnInst::Create(context, val, bb);
 	    }
 	    else {
 		rb_raise(rb_eLocalJumpError, "unexpected next");
@@ -1756,7 +1730,7 @@
 		    return_from_block = return_from_block_ids++;
 		}
 		compile_return_from_block(val, return_from_block);
-		ReturnInst::Create(val, bb);
+		ReturnInst::Create(context, val, bb);
 	    }
 	    else {
 		compile_simple_return(val);
@@ -1767,19 +1741,20 @@
     // To not complicate the compiler even more, let's be very lazy here and
     // continue on a dead branch. Hopefully LLVM is smart enough to eliminate
     // it at compilation time.
-    bb = BasicBlock::Create("DEAD", bb->getParent());
+    bb = BasicBlock::Create(context, "DEAD", bb->getParent());
 
     return val;
 }
 
-void RoxorCompiler::compile_simple_return(Value *val)
+void
+RoxorCompiler::compile_simple_return(Value *val)
 {
     if (ensure_bb != NULL) {
 	BranchInst::Create(ensure_bb, bb);
 	ensure_pn->addIncoming(val, bb);
     }
     else {
-	ReturnInst::Create(val, bb);
+	ReturnInst::Create(context, val, bb);
     }
 }
 
@@ -1868,10 +1843,10 @@
 		params.end(), "", bb);
 
 	Function *f = bb->getParent();
-	BasicBlock *typeok_bb = BasicBlock::Create("typeok", f);
-	BasicBlock *nocatch_bb  = BasicBlock::Create("nocatch", f);
-	Value *need_ret = new ICmpInst(ICmpInst::ICMP_EQ, eh_sel,
-		eh_typeid, "", bb);
+	BasicBlock *typeok_bb = BasicBlock::Create(context, "typeok", f);
+	BasicBlock *nocatch_bb  = BasicBlock::Create(context, "nocatch", f);
+	Value *need_ret = new ICmpInst(*bb, ICmpInst::ICMP_EQ, eh_sel,
+		eh_typeid);
 	BranchInst::Create(typeok_bb, nocatch_bb, need_ret, bb);
 
 	bb = nocatch_bb;
@@ -1900,7 +1875,7 @@
 	// void rb_vm_pop_exception(void);
 	popExceptionFunc = cast<Function>(
 		module->getOrInsertFunction("rb_vm_pop_exception", 
-		    Type::VoidTy, NULL));
+		    VoidTy, NULL));
     }
     CallInst::Create(popExceptionFunc, "", bb);
 }
@@ -1917,7 +1892,7 @@
 	// void __cxa_end_catch(void);
 	endCatchFunc = cast<Function>(
 		module->getOrInsertFunction("__cxa_end_catch",
-		    Type::VoidTy, NULL));
+		    VoidTy, NULL));
     }
     CallInst::Create(endCatchFunc, "", bb);
 }
@@ -1930,11 +1905,10 @@
 	if (rethrowFunc == NULL) {
 	    // void __cxa_rethrow(void);
 	    rethrowFunc = cast<Function>(
-		    module->getOrInsertFunction("__cxa_rethrow",
-			Type::VoidTy, NULL));
+		    module->getOrInsertFunction("__cxa_rethrow", VoidTy, NULL));
 	}
 	CallInst::Create(rethrowFunc, "", bb);
-	new UnreachableInst(bb);
+	new UnreachableInst(context, bb);
     }
     else {
 	BranchInst::Create(rescue_rethrow_bb, bb);
@@ -2068,10 +2042,10 @@
 	}
 
 	if (float_op) {
-	    res = new FCmpInst(predicate, leftVal, rightVal, "", bb);
+	    res = new FCmpInst(*bb, predicate, leftVal, rightVal);
 	}
 	else {
-	    res = new ICmpInst(predicate, leftVal, rightVal, "", bb);
+	    res = new ICmpInst(*bb, predicate, leftVal, rightVal);
 	}
 	res = SelectInst::Create(res, trueVal, falseVal, "", bb);
     }
@@ -2082,37 +2056,36 @@
 RoxorCompiler::compile_double_coercion(Value *val, Value *mask,
 	BasicBlock *fallback_bb, Function *f)
 {
-    Value *is_float = new ICmpInst(ICmpInst::ICMP_EQ,
-	    mask, threeVal, "", bb);
+    Value *is_float = new ICmpInst(*bb, ICmpInst::ICMP_EQ, mask, threeVal);
 
-    BasicBlock *is_float_bb = BasicBlock::Create("is_float", f);
-    BasicBlock *isnt_float_bb = BasicBlock::Create("isnt_float", f);
-    BasicBlock *merge_bb = BasicBlock::Create("merge", f);
+    BasicBlock *is_float_bb = BasicBlock::Create(context, "is_float", f);
+    BasicBlock *isnt_float_bb = BasicBlock::Create(context, "isnt_float", f);
+    BasicBlock *merge_bb = BasicBlock::Create(context, "merge", f);
 
     BranchInst::Create(is_float_bb, isnt_float_bb, is_float, bb);
 
     bb = is_float_bb;
     Value *is_float_val = BinaryOperator::CreateXor(val, threeVal, "", bb);
 #if __LP64__
-    is_float_val = new BitCastInst(is_float_val, Type::DoubleTy, "", bb);
+    is_float_val = new BitCastInst(is_float_val, DoubleTy, "", bb);
 #else
-    is_float_val = new BitCastInst(is_float_val, Type::FloatTy, "", bb);
-    is_float_val = new FPExtInst(is_float_val, Type::DoubleTy, "", bb);
+    is_float_val = new BitCastInst(is_float_val, FloatTy, "", bb);
+    is_float_val = new FPExtInst(is_float_val, DoubleTy, "", bb);
 #endif
     BranchInst::Create(merge_bb, bb);
 
     bb = isnt_float_bb;
-    Value *is_fixnum = new ICmpInst(ICmpInst::ICMP_EQ, mask, oneVal, "", bb);
-    BasicBlock *is_fixnum_bb = BasicBlock::Create("is_fixnum", f);
+    Value *is_fixnum = new ICmpInst(*bb, ICmpInst::ICMP_EQ, mask, oneVal);
+    BasicBlock *is_fixnum_bb = BasicBlock::Create(context, "is_fixnum", f);
     BranchInst::Create(is_fixnum_bb, fallback_bb, is_fixnum, bb);
 
     bb = is_fixnum_bb;
     Value *is_fixnum_val = BinaryOperator::CreateAShr(val, twoVal, "", bb);
-    is_fixnum_val = new SIToFPInst(is_fixnum_val, Type::DoubleTy, "", bb);
+    is_fixnum_val = new SIToFPInst(is_fixnum_val, DoubleTy, "", bb);
     BranchInst::Create(merge_bb, bb);
 
     bb = merge_bb;
-    PHINode *pn = PHINode::Create(Type::DoubleTy, "op_tmp", bb);
+    PHINode *pn = PHINode::Create(DoubleTy, "op_tmp", bb);
     pn->addIncoming(is_float_val, is_float_bb);
     pn->addIncoming(is_fixnum_val, is_fixnum_bb);
 
@@ -2134,9 +2107,9 @@
 
 	Function *f = bb->getParent();
 
-	BasicBlock *falseBB = BasicBlock::Create("", f);
-	BasicBlock *trueBB = BasicBlock::Create("", f);
-	BasicBlock *mergeBB = BasicBlock::Create("", f);
+	BasicBlock *falseBB = BasicBlock::Create(context, "", f);
+	BasicBlock *trueBB = BasicBlock::Create(context, "", f);
+	BasicBlock *mergeBB = BasicBlock::Create(context, "", f);
 
 	compile_boolean_test(val, trueBB, falseBB);
 
@@ -2175,14 +2148,14 @@
 	    // Both operands are symbol constants.
 	    if (sel == selEq || sel == selEqq || sel == selNeq) {
 		Value *is_redefined_val = new LoadInst(is_redefined, "", bb);
-		Value *isOpRedefined = new ICmpInst(ICmpInst::ICMP_EQ, 
-			is_redefined_val, ConstantInt::getFalse(), "", bb);
+		Value *isOpRedefined = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+			is_redefined_val, ConstantInt::getFalse(context));
 
 		Function *f = bb->getParent();
 
-		BasicBlock *thenBB = BasicBlock::Create("op_not_redefined", f);
-		BasicBlock *elseBB  = BasicBlock::Create("op_dispatch", f);
-		BasicBlock *mergeBB = BasicBlock::Create("op_merge", f);
+		BasicBlock *thenBB = BasicBlock::Create(context, "op_not_redefined", f);
+		BasicBlock *elseBB  = BasicBlock::Create(context, "op_dispatch", f);
+		BasicBlock *mergeBB = BasicBlock::Create(context, "op_merge", f);
 
 		BranchInst::Create(thenBB, elseBB, isOpRedefined, bb);
 		Value *thenVal = NULL;
@@ -2261,14 +2234,14 @@
 
 	    if (res_val != NULL) {
 		Value *is_redefined_val = new LoadInst(is_redefined, "", bb);
-		Value *isOpRedefined = new ICmpInst(ICmpInst::ICMP_EQ, 
-			is_redefined_val, ConstantInt::getFalse(), "", bb);
+		Value *isOpRedefined = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+			is_redefined_val, ConstantInt::getFalse(context));
 
 		Function *f = bb->getParent();
 
-		BasicBlock *thenBB = BasicBlock::Create("op_not_redefined", f);
-		BasicBlock *elseBB  = BasicBlock::Create("op_dispatch", f);
-		BasicBlock *mergeBB = BasicBlock::Create("op_merge", f);
+		BasicBlock *thenBB = BasicBlock::Create(context, "op_not_redefined", f);
+		BasicBlock *elseBB  = BasicBlock::Create(context, "op_dispatch", f);
+		BasicBlock *mergeBB = BasicBlock::Create(context, "op_merge", f);
 
 		BranchInst::Create(thenBB, elseBB, isOpRedefined, bb);
 		Value *thenVal = res_val;
@@ -2292,19 +2265,20 @@
 	else {
 	    // Either one or both is not a constant immediate.
 	    Value *is_redefined_val = new LoadInst(is_redefined, "", bb);
-	    Value *isOpRedefined = new ICmpInst(ICmpInst::ICMP_EQ, 
-		    is_redefined_val, ConstantInt::getFalse(), "", bb);
+	    Value *isOpRedefined = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+		    is_redefined_val, ConstantInt::getFalse(context));
 
 	    Function *f = bb->getParent();
 
 	    BasicBlock *not_redefined_bb =
-		BasicBlock::Create("op_not_redefined", f);
+		BasicBlock::Create(context, "op_not_redefined", f);
 	    BasicBlock *optimize_fixnum_bb =
-		BasicBlock::Create("op_optimize_fixnum", f);
+		BasicBlock::Create(context, "op_optimize_fixnum", f);
 	    BasicBlock *optimize_float_bb =
-		BasicBlock::Create("op_optimize_float", f);
-	    BasicBlock *dispatch_bb  = BasicBlock::Create("op_dispatch", f);
-	    BasicBlock *merge_bb = BasicBlock::Create("op_merge", f);
+		BasicBlock::Create(context, "op_optimize_float", f);
+	    BasicBlock *dispatch_bb =
+		BasicBlock::Create( context, "op_dispatch", f);
+	    BasicBlock *merge_bb = BasicBlock::Create(context, "op_merge", f);
 
 	    BranchInst::Create(not_redefined_bb, dispatch_bb, isOpRedefined,
 		    bb);
@@ -2324,23 +2298,23 @@
 	    }
 
 	    if (leftAndOp != NULL && rightAndOp != NULL) {
-		Value *leftIsFixnum = new ICmpInst(ICmpInst::ICMP_EQ,
-			leftAndOp, oneVal, "", bb);
+		Value *leftIsFixnum = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+			leftAndOp, oneVal);
 		BasicBlock *left_is_fixnum_bb =
-		    BasicBlock::Create("left_fixnum", f);
+		    BasicBlock::Create(context, "left_fixnum", f);
 		BranchInst::Create(left_is_fixnum_bb, optimize_float_bb,
 			leftIsFixnum, bb);
 
 		bb = left_is_fixnum_bb;
-		Value *rightIsFixnum = new ICmpInst(ICmpInst::ICMP_EQ,
-			rightAndOp, oneVal, "", bb);
+		Value *rightIsFixnum = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+			rightAndOp, oneVal);
 		BranchInst::Create(optimize_fixnum_bb, optimize_float_bb,
 			rightIsFixnum, bb);
 	    }
 	    else if (leftAndOp != NULL) {
 		if (rightImm.is_fixnum()) {
-		    Value *leftIsFixnum = new ICmpInst(ICmpInst::ICMP_EQ,
-			    leftAndOp, oneVal, "", bb);
+		    Value *leftIsFixnum = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+			    leftAndOp, oneVal);
 		    BranchInst::Create(optimize_fixnum_bb, optimize_float_bb,
 			    leftIsFixnum, bb);
 		}
@@ -2350,8 +2324,8 @@
 	    }
 	    else if (rightAndOp != NULL) {
 		if (leftImm.is_fixnum()) {
-		    Value *rightIsFixnum = new ICmpInst(ICmpInst::ICMP_EQ,
-			    rightAndOp, oneVal, "", bb);
+		    Value *rightIsFixnum = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+			    rightAndOp, oneVal);
 		    BranchInst::Create(optimize_fixnum_bb, optimize_float_bb,
 			    rightIsFixnum, bb);
 		}
@@ -2393,19 +2367,19 @@
 
 		// Is result fixable?
 		Value *fixnumMax = ConstantInt::get(IntTy, FIXNUM_MAX + 1);
-		Value *isFixnumMaxOk = new ICmpInst(ICmpInst::ICMP_SLT,
-			fix_op_res, fixnumMax, "", bb);
+		Value *isFixnumMaxOk = new ICmpInst(*bb, ICmpInst::ICMP_SLT,
+			fix_op_res, fixnumMax);
 
 		BasicBlock *fixable_max_bb =
-		    BasicBlock::Create("op_fixable_max", f);
+		    BasicBlock::Create(context, "op_fixable_max", f);
 
 		BranchInst::Create(fixable_max_bb, dispatch_bb, isFixnumMaxOk,
 			bb);
 
 		bb = fixable_max_bb;
 		Value *fixnumMin = ConstantInt::get(IntTy, FIXNUM_MIN);
-		Value *isFixnumMinOk = new ICmpInst(ICmpInst::ICMP_SGE,
-			fix_op_res, fixnumMin, "", bb);
+		Value *isFixnumMinOk = new ICmpInst(*bb, ICmpInst::ICMP_SGE,
+			fix_op_res, fixnumMin);
 
 		BranchInst::Create(merge_bb, dispatch_bb, isFixnumMinOk, bb);
 		fix_op_res = boxed_op_res;
@@ -2418,8 +2392,7 @@
 	    bb = optimize_float_bb;
 
 	    if (leftIsImmediateConst) {
-		unboxedLeft = ConstantFP::get(Type::DoubleTy,
-			leftImm.double_val());
+		unboxedLeft = ConstantFP::get(DoubleTy, leftImm.double_val());
 	    }
 	    else {
 		unboxedLeft = compile_double_coercion(leftVal, leftAndOp,
@@ -2427,8 +2400,7 @@
 	    }
 
 	    if (rightIsImmediateConst) {
-		unboxedRight = ConstantFP::get(Type::DoubleTy,
-			rightImm.double_val());
+		unboxedRight = ConstantFP::get(DoubleTy, rightImm.double_val());
 	    }
 	    else {
 		unboxedRight = compile_double_coercion(rightVal, rightAndOp,
@@ -2442,7 +2414,7 @@
 	    if (!result_is_predicate) {
 		// Box the float. 
 #if !__LP64__
-		flp_op_res = new FPTruncInst(flp_op_res, Type::FloatTy, "", bb);
+		flp_op_res = new FPTruncInst(flp_op_res, FloatTy, "", bb);
 #endif
 		flp_op_res = new BitCastInst(flp_op_res, RubyObjTy, "", bb);
 		flp_op_res = BinaryOperator::CreateOr(flp_op_res, threeVal,
@@ -2490,15 +2462,16 @@
 
 	if (sel == selLTLT) {
 	    opt_func = cast<Function>(module->getOrInsertFunction("rb_vm_fast_shift",
-			RubyObjTy, RubyObjTy, RubyObjTy, PtrTy, Type::Int1Ty, NULL));
+			RubyObjTy, RubyObjTy, RubyObjTy, PtrTy, Int1Ty, NULL));
 	}
 	else if (sel == selAREF) {
 	    opt_func = cast<Function>(module->getOrInsertFunction("rb_vm_fast_aref",
-			RubyObjTy, RubyObjTy, RubyObjTy, PtrTy, Type::Int1Ty, NULL));
+			RubyObjTy, RubyObjTy, RubyObjTy, PtrTy, Int1Ty, NULL));
 	}
 	else if (sel == selASET) {
 	    opt_func = cast<Function>(module->getOrInsertFunction("rb_vm_fast_aset",
-			RubyObjTy, RubyObjTy, RubyObjTy, RubyObjTy, PtrTy, Type::Int1Ty, NULL));
+			RubyObjTy, RubyObjTy, RubyObjTy, RubyObjTy, PtrTy,
+			Int1Ty, NULL));
 	}
 	else {
 	    abort();
@@ -2538,14 +2511,14 @@
 	GlobalVariable *is_redefined = GET_CORE()->redefined_op_gvar(sel, true);
 
 	Value *is_redefined_val = new LoadInst(is_redefined, "", bb);
-	Value *isOpRedefined = new ICmpInst(ICmpInst::ICMP_EQ, 
-		is_redefined_val, ConstantInt::getFalse(), "", bb);
+	Value *isOpRedefined = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+		is_redefined_val, ConstantInt::getFalse(context));
 
 	Function *f = bb->getParent();
 
-	BasicBlock *thenBB = BasicBlock::Create("op_not_redefined", f);
-	BasicBlock *elseBB = BasicBlock::Create("op_dispatch", f);
-	BasicBlock *mergeBB = BasicBlock::Create("op_merge", f);
+	BasicBlock *thenBB = BasicBlock::Create(context, "op_not_redefined", f);
+	BasicBlock *elseBB = BasicBlock::Create(context, "op_dispatch", f);
+	BasicBlock *mergeBB = BasicBlock::Create(context, "op_merge", f);
 
 	BranchInst::Create(thenBB, elseBB, isOpRedefined, bb);
 
@@ -2555,8 +2528,8 @@
 	new_params.push_back(params[1]);
 	new_params.push_back(compile_sel(new_sel));
 	new_params.push_back(params[3]);
-	new_params.push_back(ConstantInt::get(Type::Int8Ty, DISPATCH_FCALL));
-	new_params.push_back(ConstantInt::get(Type::Int32Ty, argc - 1));
+	new_params.push_back(ConstantInt::get(Int8Ty, DISPATCH_FCALL));
+	new_params.push_back(ConstantInt::get(Int32Ty, argc - 1));
 	for (int i = 0; i < argc - 1; i++) {
 	    new_params.push_back(params[7 + i]);
 	}
@@ -2746,8 +2719,8 @@
 		    str_len, CFHash((CFTypeRef)val));
 
 	    std::vector<Value *> idxs;
-	    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-	    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+	    idxs.push_back(ConstantInt::get(Int32Ty, 0));
+	    idxs.push_back(ConstantInt::get(Int32Ty, 0));
 	    Instruction *load = GetElementPtrInst::Create(str_gvar,
 		    idxs.begin(), idxs.end(), "", bb);
 
@@ -2755,13 +2728,13 @@
 		newString2Func = cast<Function>(
 			module->getOrInsertFunction(
 			    "rb_unicode_str_new",
-			    RubyObjTy, PointerType::getUnqual(Type::Int16Ty),
-			    Type::Int32Ty, NULL));
+			    RubyObjTy, PointerType::getUnqual(Int16Ty),
+			    Int32Ty, NULL));
 	    }
 
 	    std::vector<Value *> params;
 	    params.push_back(load);
-	    params.push_back(ConstantInt::get(Type::Int32Ty, str_len));
+	    params.push_back(ConstantInt::get(Int32Ty, str_len));
 
 	    return CallInst::Create(newString2Func, params.begin(),
 		    params.end(), "", bb);
@@ -2793,13 +2766,8 @@
     GlobalVariable *gvar = NULL;
 
     if (iter == literals.end()) {
-	gvar = new GlobalVariable(
-		RubyObjTy,
-		false,
-		GlobalValue::InternalLinkage,
-		nilVal,
-		"",
-		RoxorCompiler::module);
+	gvar = new GlobalVariable(*RoxorCompiler::module, RubyObjTy, false,
+		GlobalValue::InternalLinkage, nilVal, "");
 	literals[val] = gvar;
     }
     else {
@@ -2824,13 +2792,9 @@
     std::map<ID, GlobalVariable *>::iterator iter = global_entries.find(name);
     GlobalVariable *gvar = NULL;
     if (iter == global_entries.end()) {
-	gvar = new GlobalVariable(
-		PtrTy,
-		false,
-		GlobalValue::InternalLinkage,
-		Constant::getNullValue(PtrTy),
-		"",
-		RoxorCompiler::module);
+	gvar = new GlobalVariable(*RoxorCompiler::module, PtrTy, false,
+		GlobalValue::InternalLinkage, Constant::getNullValue(PtrTy),
+		"");
 	global_entries[name] = gvar;
     }
     else {
@@ -2864,7 +2828,7 @@
 	// void rb_vm_set_current_scope(VALUE mod, int scope)
 	setScopeFunc = cast<Function>(
 		module->getOrInsertFunction("rb_vm_set_current_scope",
-		    Type::VoidTy, RubyObjTy, Type::Int32Ty, NULL));
+		    VoidTy, RubyObjTy, Int32Ty, NULL));
     }
 
     std::vector<Value *> params;
@@ -2886,7 +2850,7 @@
 	    prepareIvarSlotFunc = cast<Function>(
 		    module->getOrInsertFunction(
 			"rb_vm_prepare_class_ivar_slot", 
-			Type::VoidTy, RubyObjTy, IntTy, Int32PtrTy, NULL));
+			VoidTy, RubyObjTy, IntTy, Int32PtrTy, NULL));
 	}
 	for (std::map<ID, Instruction *>::iterator iter
 		= ivar_slots_cache.begin();
@@ -2907,7 +2871,7 @@
 	    }
 	    params.push_back(id_val);
 
-	    Instruction *insn = ivar_slot->clone();
+	    Instruction *insn = ivar_slot->clone(context);
 	    list.insert(list_iter, insn);
 	    params.push_back(insn);
 
@@ -2934,17 +2898,19 @@
 	// void rb_vm_keep_vars(rb_vm_var_uses *uses, int lvars_size, ...)
 	std::vector<const Type *> types;
 	types.push_back(PtrTy);
-	types.push_back(Type::Int32Ty);
-	FunctionType *ft = FunctionType::get(Type::VoidTy, types, true);
+	types.push_back(Int32Ty);
+	FunctionType *ft = FunctionType::get(VoidTy, types, true);
 	keepVarsFunc = cast<Function>
 	    (module->getOrInsertFunction("rb_vm_keep_vars", ft));
     }
 
-    BasicBlock *notNullBB = BasicBlock::Create("not_null", startBB->getParent());
+    BasicBlock *notNullBB = BasicBlock::Create(context, "not_null",
+	    startBB->getParent());
 
     bb = startBB;
     Value *usesVal = new LoadInst(current_var_uses, "", bb);
-    Value *notNullCond = new ICmpInst(ICmpInst::ICMP_NE, usesVal, compile_const_pointer(NULL), "", bb);
+    Value *notNullCond = new ICmpInst(*bb, ICmpInst::ICMP_NE, usesVal,
+	    compile_const_pointer(NULL));
     // we only need to call keepVarsFunc if current_var_uses is not NULL
     BranchInst::Create(notNullBB, mergeBB, notNullCond, bb);
 
@@ -2966,7 +2932,7 @@
 	    vars_count++;
 	}
     }
-    params[1] = ConstantInt::get(Type::Int32Ty, vars_count);
+    params[1] = ConstantInt::get(Int32Ty, vars_count);
 
     CallInst::Create(keepVarsFunc, params.begin(), params.end(), "", bb);
 
@@ -3023,10 +2989,11 @@
 		}
 		FunctionType *ft = FunctionType::get(RubyObjTy, types, false);
 		Function *f = Function::Create(ft, GlobalValue::PrivateLinkage,
-			"", module);
+			"__ruby_scope", module);
 
-		RoxorFunctionAnnotation *old_func_annotation = func_annotation;
-		func_annotation = new RoxorFunctionAnnotation(f, fname);
+		RoxorScope *old_current_scope = current_scope;
+		current_scope = new RoxorScope(fname);
+		scopes[f] = current_scope;
 
 		BasicBlock *old_rescue_invoke_bb = rescue_invoke_bb;
 		BasicBlock *old_rescue_rethrow_bb = rescue_rethrow_bb;
@@ -3036,7 +3003,7 @@
 		BasicBlock *new_rescue_rethrow_bb = NULL;
 		rescue_invoke_bb = NULL;
 		rescue_rethrow_bb = NULL;
-		bb = BasicBlock::Create("MainBlock", f);
+		bb = BasicBlock::Create(context, "MainBlock", f);
 
 		std::map<ID, Value *> old_lvars = lvars;
 		lvars.clear();
@@ -3107,10 +3074,13 @@
 
 		    if (has_vars_to_save) {
 			current_var_uses = new AllocaInst(PtrTy, "", bb);
-			new StoreInst(compile_const_pointer(NULL), current_var_uses, bb);
+			new StoreInst(compile_const_pointer(NULL),
+				current_var_uses, bb);
 
-			new_rescue_invoke_bb = BasicBlock::Create("rescue_save_vars", f);
-			new_rescue_rethrow_bb = BasicBlock::Create("rescue_save_vars.rethrow", f);
+			new_rescue_invoke_bb = BasicBlock::Create(context,
+				"rescue_save_vars", f);
+			new_rescue_rethrow_bb = BasicBlock::Create(context,
+				"rescue_save_vars.rethrow", f);
 			rescue_invoke_bb = new_rescue_invoke_bb;
 			rescue_rethrow_bb = new_rescue_rethrow_bb;
 		    }
@@ -3158,7 +3128,7 @@
 
 		Value *val = NULL;
 		if (node->nd_body != NULL) {
-		    entry_bb = BasicBlock::Create("entry_point", f); 
+		    entry_bb = BasicBlock::Create(context, "entry_point", f); 
 		    BranchInst::Create(entry_bb, bb);
 		    bb = entry_bb;
 
@@ -3175,7 +3145,7 @@
 		    val = nilVal;
 		}
 
-		ReturnInst::Create(val, bb);
+		ReturnInst::Create(context, val, bb);
 
 		// the rethrows after the save of variables must be real rethrows
 		rescue_rethrow_bb = NULL;
@@ -3230,7 +3200,8 @@
 			}
 
 			bb = new_rescue_rethrow_bb;
-			BasicBlock *mergeBB = BasicBlock::Create("merge", f);
+			BasicBlock *mergeBB = BasicBlock::Create(context,
+				"merge", f);
 			compile_keep_vars(bb, mergeBB);
 
 			bb = mergeBB;
@@ -3276,13 +3247,13 @@
 		rescue_rethrow_bb = old_rescue_rethrow_bb;
 		rescue_invoke_bb = old_rescue_invoke_bb;
 
-		func_annotation = old_func_annotation;
 		bb = old_bb;
 		entry_bb = old_entry_bb;
 		lvars = old_lvars;
 		current_self = old_self;
 		current_var_uses = old_current_var_uses;
 		running_block = old_running_block;
+		current_scope = old_current_scope;
 
 		return cast<Value>(f);
 	    }
@@ -3380,19 +3351,21 @@
 		}
 
 
-		Value *falseCond = new ICmpInst(ICmpInst::ICMP_EQ, recvVal, falseVal, "", bb);
+		Value *falseCond = new ICmpInst(*bb, ICmpInst::ICMP_EQ,
+			recvVal, falseVal);
 
 		Function *f = bb->getParent();
 
-		BasicBlock *falseBB = BasicBlock::Create("", f);
-		BasicBlock *elseBB  = BasicBlock::Create("", f);
-		BasicBlock *trueBB = BasicBlock::Create("", f);
-		BasicBlock *mergeBB = BasicBlock::Create("", f);
+		BasicBlock *falseBB = BasicBlock::Create(context, "", f);
+		BasicBlock *elseBB  = BasicBlock::Create(context, "", f);
+		BasicBlock *trueBB = BasicBlock::Create(context, "", f);
+		BasicBlock *mergeBB = BasicBlock::Create(context, "", f);
 
 		BranchInst::Create(falseBB, trueBB, falseCond, bb);
 
 		bb = trueBB;
-		Value *nilCond = new ICmpInst(ICmpInst::ICMP_EQ, recvVal, nilVal, "", bb);
+		Value *nilCond = new ICmpInst(*bb, ICmpInst::ICMP_EQ, recvVal,
+			nilVal);
 		BranchInst::Create(falseBB, elseBB, nilCond, bb);
 
 		bb = falseBB;
@@ -3420,9 +3393,9 @@
 
 		Function *f = bb->getParent();
 
-		BasicBlock *notNilBB = BasicBlock::Create("", f);
-		BasicBlock *elseBB  = BasicBlock::Create("", f);
-		BasicBlock *mergeBB = BasicBlock::Create("", f);
+		BasicBlock *notNilBB = BasicBlock::Create(context, "", f);
+		BasicBlock *elseBB  = BasicBlock::Create(context, "", f);
+		BasicBlock *mergeBB = BasicBlock::Create(context, "", f);
 
 		compile_boolean_test(recvVal, notNilBB, elseBB);
 
@@ -3475,7 +3448,7 @@
 		params.push_back(recv);
 		params.push_back(compile_sel(sel));
 		params.push_back(compile_const_pointer(NULL));
-		params.push_back(ConstantInt::get(Type::Int8Ty, 0));
+		params.push_back(ConstantInt::get(Int8Ty, 0));
 
 		int argc = 0;
 		std::vector<Value *> arguments;
@@ -3485,7 +3458,7 @@
 			    arguments,
 			    &argc);
 		}
-		params.push_back(ConstantInt::get(Type::Int32Ty, argc));
+		params.push_back(ConstantInt::get(Int32Ty, argc));
 		for (std::vector<Value *>::iterator i = arguments.begin();
 			i != arguments.end(); ++i) {
 		    params.push_back(*i);
@@ -3509,9 +3482,9 @@
 		    // 0 means OR, 1 means AND
 		    Function *f = bb->getParent();
 
-		    touchedBB = BasicBlock::Create("", f);
-		    untouchedBB  = BasicBlock::Create("", f);
-		    mergeBB = BasicBlock::Create("merge", f);
+		    touchedBB = BasicBlock::Create(context, "", f);
+		    untouchedBB  = BasicBlock::Create(context, "", f);
+		    mergeBB = BasicBlock::Create(context, "merge", f);
 
 		    if (type == 0) {
 			compile_boolean_test(tmp, untouchedBB, touchedBB);
@@ -3534,8 +3507,8 @@
 		    params.push_back(tmp);
 		    params.push_back(compile_sel(sel));
 		    params.push_back(compile_const_pointer(NULL));
-		    params.push_back(ConstantInt::get(Type::Int8Ty, 0));
-		    params.push_back(ConstantInt::get(Type::Int32Ty, 1));
+		    params.push_back(ConstantInt::get(Int8Ty, 0));
+		    params.push_back(ConstantInt::get(Int32Ty, 1));
 		    params.push_back(compile_node(value));
 
 		    tmp2 = compile_optimized_dispatch_call(sel, 1, params);
@@ -3558,9 +3531,9 @@
 		params.push_back(recv);
 		params.push_back(compile_sel(sel));
 		params.push_back(compile_const_pointer(NULL));
-		params.push_back(ConstantInt::get(Type::Int8Ty, 0));
+		params.push_back(ConstantInt::get(Int8Ty, 0));
 		argc++;
-		params.push_back(ConstantInt::get(Type::Int32Ty, argc));
+		params.push_back(ConstantInt::get(Int32Ty, argc));
 		for (std::vector<Value *>::iterator i = arguments.begin();
 		     i != arguments.end(); ++i) {
 		    params.push_back(*i);
@@ -3609,9 +3582,8 @@
 		params.push_back(current_self);
 		params.push_back(compile_sel(selBackquote));
 		params.push_back(compile_const_pointer(NULL));
-		params.push_back(ConstantInt::get(Type::Int8Ty,
-			    DISPATCH_FCALL));
-		params.push_back(ConstantInt::get(Type::Int32Ty, 1));
+		params.push_back(ConstantInt::get(Int8Ty, DISPATCH_FCALL));
+		params.push_back(ConstantInt::get(Int32Ty, 1));
 		params.push_back(str);
 
 		return compile_dispatch_call(params);
@@ -3630,12 +3602,12 @@
 		if (newRegexpFunc == NULL) {
 		    newRegexpFunc = cast<Function>(module->getOrInsertFunction(
 				"rb_reg_new_str",
-				RubyObjTy, RubyObjTy, Type::Int32Ty, NULL));
+				RubyObjTy, RubyObjTy, Int32Ty, NULL));
 		}
 
 		std::vector<Value *> params;
 		params.push_back(val);
-		params.push_back(ConstantInt::get(Type::Int32Ty, flag));
+		params.push_back(ConstantInt::get(Int32Ty, flag));
 
 		return compile_protected_call(newRegexpFunc, params);
 	    }
@@ -3674,29 +3646,41 @@
 
 		Function *f = bb->getParent();
 
-		BasicBlock *leftNotFalseBB = BasicBlock::Create("left_not_false", f);
-		BasicBlock *leftNotTrueBB = BasicBlock::Create("left_not_true", f);
-		BasicBlock *leftTrueBB = BasicBlock::Create("left_is_true", f);
-		BasicBlock *rightNotFalseBB = BasicBlock::Create("right_not_false", f);
-		BasicBlock *rightTrueBB = BasicBlock::Create("right_is_true", f);
-		BasicBlock *failBB = BasicBlock::Create("fail", f);
-		BasicBlock *mergeBB = BasicBlock::Create("merge", f);
+		BasicBlock *leftNotFalseBB = BasicBlock::Create(context,
+			"left_not_false", f);
+		BasicBlock *leftNotTrueBB = BasicBlock::Create(context,
+			"left_not_true", f);
+		BasicBlock *leftTrueBB = BasicBlock::Create(context,
+			"left_is_true", f);
+		BasicBlock *rightNotFalseBB = BasicBlock::Create(context,
+			"right_not_false", f);
+		BasicBlock *rightTrueBB = BasicBlock::Create(context,
+			"right_is_true", f);
+		BasicBlock *failBB = BasicBlock::Create(context, "fail", f);
+		BasicBlock *mergeBB = BasicBlock::Create(context, "merge", f);
 
 		Value *leftVal = compile_node(left);
-		Value *leftNotFalseCond = new ICmpInst(ICmpInst::ICMP_NE, leftVal, falseVal, "", bb);
-		BranchInst::Create(leftNotFalseBB, leftNotTrueBB, leftNotFalseCond, bb);
+		Value *leftNotFalseCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			leftVal, falseVal);
+		BranchInst::Create(leftNotFalseBB, leftNotTrueBB,
+			leftNotFalseCond, bb);
 
 		bb = leftNotFalseBB;
-		Value *leftNotNilCond = new ICmpInst(ICmpInst::ICMP_NE, leftVal, nilVal, "", bb);
-		BranchInst::Create(leftTrueBB, leftNotTrueBB, leftNotNilCond, bb);
+		Value *leftNotNilCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			leftVal, nilVal);
+		BranchInst::Create(leftTrueBB, leftNotTrueBB, leftNotNilCond,
+			bb);
 
 		bb = leftNotTrueBB;
 		Value *rightVal = compile_node(right);
-		Value *rightNotFalseCond = new ICmpInst(ICmpInst::ICMP_NE, rightVal, falseVal, "", bb);
-		BranchInst::Create(rightNotFalseBB, failBB, rightNotFalseCond, bb);
+		Value *rightNotFalseCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			rightVal, falseVal);
+		BranchInst::Create(rightNotFalseBB, failBB, rightNotFalseCond,
+			bb);
 
 		bb = rightNotFalseBB;
-		Value *rightNotNilCond = new ICmpInst(ICmpInst::ICMP_NE, rightVal, nilVal, "", bb);
+		Value *rightNotNilCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			rightVal, nilVal);
 		BranchInst::Create(rightTrueBB, failBB, rightNotNilCond, bb);
 
 		BranchInst::Create(mergeBB, leftTrueBB);
@@ -3723,30 +3707,41 @@
 
 		Function *f = bb->getParent();
 
-		BasicBlock *leftNotFalseBB = BasicBlock::Create("left_not_false", f);
-		BasicBlock *leftTrueBB = BasicBlock::Create("left_is_true", f);
-		BasicBlock *rightNotFalseBB = BasicBlock::Create("right_not_false", f);
-		BasicBlock *leftFailBB = BasicBlock::Create("left_fail", f);
-		BasicBlock *rightFailBB = BasicBlock::Create("right_fail", f);
-		BasicBlock *successBB = BasicBlock::Create("success", f);
-		BasicBlock *mergeBB = BasicBlock::Create("merge", f);
+		BasicBlock *leftNotFalseBB = BasicBlock::Create(context,
+			"left_not_false", f);
+		BasicBlock *leftTrueBB = BasicBlock::Create(context,
+			"left_is_true", f);
+		BasicBlock *rightNotFalseBB = BasicBlock::Create(context,
+			"right_not_false", f);
+		BasicBlock *leftFailBB = BasicBlock::Create(context,
+			"left_fail", f);
+		BasicBlock *rightFailBB = BasicBlock::Create(context,
+			"right_fail", f);
+		BasicBlock *successBB = BasicBlock::Create(context, "success",
+			f);
+		BasicBlock *mergeBB = BasicBlock::Create(context, "merge", f);
 
 		Value *leftVal = compile_node(left);
-		Value *leftNotFalseCond = new ICmpInst(ICmpInst::ICMP_NE, leftVal, falseVal, "", bb);
-		BranchInst::Create(leftNotFalseBB, leftFailBB, leftNotFalseCond, bb);
+		Value *leftNotFalseCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			leftVal, falseVal);
+		BranchInst::Create(leftNotFalseBB, leftFailBB,
+			leftNotFalseCond, bb);
 
 		bb = leftNotFalseBB;
-		Value *leftNotNilCond = new ICmpInst(ICmpInst::ICMP_NE, leftVal, nilVal, "", bb);
+		Value *leftNotNilCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			leftVal, nilVal);
 		BranchInst::Create(leftTrueBB, leftFailBB, leftNotNilCond, bb);
 
 		bb = leftTrueBB;
 		Value *rightVal = compile_node(right);
-		Value *rightNotFalseCond = new ICmpInst(ICmpInst::ICMP_NE, rightVal, falseVal, "", bb);
+		Value *rightNotFalseCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			rightVal, falseVal);
 
 		BranchInst::Create(rightNotFalseBB, rightFailBB, rightNotFalseCond, bb);
 
 		bb = rightNotFalseBB;
-		Value *rightNotNilCond = new ICmpInst(ICmpInst::ICMP_NE, rightVal, nilVal, "", bb);
+		Value *rightNotNilCond = new ICmpInst(*bb, ICmpInst::ICMP_NE,
+			rightVal, nilVal);
 		BranchInst::Create(successBB, rightFailBB, rightNotNilCond, bb);
 
 		BranchInst::Create(mergeBB, successBB);
@@ -3769,9 +3764,9 @@
 
 		Function *f = bb->getParent();
 
-		BasicBlock *thenBB = BasicBlock::Create("then", f);
-		BasicBlock *elseBB  = BasicBlock::Create("else", f);
-		BasicBlock *mergeBB = BasicBlock::Create("merge", f);
+		BasicBlock *thenBB = BasicBlock::Create(context, "then", f);
+		BasicBlock *elseBB  = BasicBlock::Create(context, "else", f);
+		BasicBlock *mergeBB = BasicBlock::Create(context, "merge", f);
 
 		compile_boolean_test(condVal, thenBB, elseBB);
 
@@ -3823,7 +3818,7 @@
 				module->getOrInsertFunction(
 				    "rb_vm_define_class",
 				    RubyObjTy, IntTy, RubyObjTy, RubyObjTy,
-				    Type::Int32Ty, Type::Int8Ty, NULL));
+				    Int32Ty, Int8Ty, NULL));
 		    }
 
 		    std::vector<Value *> params;
@@ -3840,8 +3835,8 @@
 		    if (outer) {
 			flags |= DEFINE_OUTER;
 		    }
-		    params.push_back(ConstantInt::get(Type::Int32Ty, flags));
-		    params.push_back(ConstantInt::get(Type::Int8Ty,
+		    params.push_back(ConstantInt::get(Int32Ty, flags));
+		    params.push_back(ConstantInt::get(Int8Ty,
 				outer && dynamic_class ? 1 : 0));
 
 		    classVal = compile_protected_call(defineClassFunc, params);
@@ -3861,12 +3856,8 @@
 
 			GlobalVariable *old_class = current_opened_class;
 			current_opened_class = new GlobalVariable(
-				RubyObjTy,
-				false,
-				GlobalValue::InternalLinkage,
-				nilVal,
-				"current_opened_class",
-				RoxorCompiler::module);
+				*RoxorCompiler::module, RubyObjTy, false,
+				GlobalValue::InternalLinkage, nilVal, "");
 
 			bool old_current_module = current_module;
 
@@ -4058,7 +4049,7 @@
 			? DISPATCH_VCALL
 			: (nd_type(node) == NODE_FCALL)
 			    ? DISPATCH_FCALL : 0;	
-		params.push_back(ConstantInt::get(Type::Int8Ty, call_opt));
+		params.push_back(ConstantInt::get(Int8Ty, call_opt));
 
 		// Arguments.
 		int argc = 0;
@@ -4066,7 +4057,7 @@
 		    const int arity = mid == 0
 			? fargs_arity - 2 // skip dvars and current_block
 			: fargs_arity;
-		    params.push_back(ConstantInt::get(Type::Int32Ty, arity));
+		    params.push_back(ConstantInt::get(Int32Ty, arity));
 		    Function::ArgumentListType::iterator iter = fargs.begin();
 		    iter++; // skip self
 		    iter++; // skip sel
@@ -4092,14 +4083,14 @@
 		else if (real_args != NULL) {
 		    std::vector<Value *> arguments;
 		    compile_dispatch_arguments(real_args, arguments, &argc);
-		    params.push_back(ConstantInt::get(Type::Int32Ty, argc));
+		    params.push_back(ConstantInt::get(Int32Ty, argc));
 		    for (std::vector<Value *>::iterator i = arguments.begin();
 			 i != arguments.end(); ++i) {
 			params.push_back(*i);
 		    }
 		}
 		else {
-		    params.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    params.push_back(ConstantInt::get(Int32Ty, 0));
 		}
 
 		// Restore the block state.
@@ -4256,7 +4247,7 @@
 		if (newArrayFunc == NULL) {
 		    // VALUE rb_ary_new_fast(int argc, ...);
 		    std::vector<const Type *> types;
-		    types.push_back(Type::Int32Ty);
+		    types.push_back(Int32Ty);
 		    FunctionType *ft = FunctionType::get(RubyObjTy, types, true);
 		    newArrayFunc = cast<Function>(module->getOrInsertFunction("rb_ary_new_fast", ft));
 		}
@@ -4264,13 +4255,13 @@
 		std::vector<Value *> params;
 
 		if (nd_type(node) == NODE_ZARRAY) {
-		    params.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    params.push_back(ConstantInt::get(Int32Ty, 0));
 		}
 		else {
 		    const int count = node->nd_alen;
 		    NODE *n = node;
 		    
-		    params.push_back(ConstantInt::get(Type::Int32Ty, count));
+		    params.push_back(ConstantInt::get(Int32Ty, count));
 
 		    for (int i = 0; i < count; i++) {
 			assert(n->nd_head != NULL);
@@ -4288,7 +4279,7 @@
 		if (newHashFunc == NULL) {
 		    // VALUE rb_hash_new_fast(int argc, ...);
 		    std::vector<const Type *> types;
-		    types.push_back(Type::Int32Ty);
+		    types.push_back(Int32Ty);
 		    FunctionType *ft = FunctionType::get(RubyObjTy, types, true);
 		    newHashFunc = cast<Function>(module->getOrInsertFunction("rb_hash_new_fast", ft));
 		}
@@ -4301,7 +4292,7 @@
 		    assert(count % 2 == 0);
 		    NODE *n = node->nd_head;
 
-		    params.push_back(ConstantInt::get(Type::Int32Ty, count));
+		    params.push_back(ConstantInt::get(Int32Ty, count));
 
 		    for (int i = 0; i < count; i += 2) {
 			Value *key = compile_node(n->nd_head);
@@ -4314,7 +4305,7 @@
 		    }
 		}
 		else {
-		    params.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    params.push_back(ConstantInt::get(Int32Ty, 0));
 		}
 
 		return cast<Value>(CallInst::Create(newHashFunc, 
@@ -4380,8 +4371,8 @@
 		params.push_back(reTarget);
 		params.push_back(compile_sel(selEqTilde));
 		params.push_back(compile_const_pointer(NULL));
-		params.push_back(ConstantInt::get(Type::Int8Ty, 0));
-		params.push_back(ConstantInt::get(Type::Int32Ty, 1));
+		params.push_back(ConstantInt::get(Int8Ty, 0));
+		params.push_back(ConstantInt::get(Int32Ty, 1));
 		params.push_back(reSource);
 
 		return compile_dispatch_call(params);
@@ -4399,7 +4390,7 @@
 		if (valiasFunc == NULL) {
 		    // void rb_alias_variable(ID from, ID to);
 		    valiasFunc = cast<Function>(module->getOrInsertFunction("rb_alias_variable",
-				Type::VoidTy, IntTy, IntTy, NULL));
+				VoidTy, IntTy, IntTy, NULL));
 		}
 
 		std::vector<Value *> params;
@@ -4421,7 +4412,7 @@
 		    //	unsigned char dynamic_class);
 		    aliasFunc = cast<Function>(module->getOrInsertFunction(
 				"rb_vm_alias2",
-				Type::VoidTy, RubyObjTy, IntTy, IntTy, Type::Int8Ty,
+				VoidTy, RubyObjTy, IntTy, IntTy, Int8Ty,
 				NULL));
 		}
 
@@ -4430,7 +4421,7 @@
 		params.push_back(compile_current_class());
 		params.push_back(compile_id(node->u1.node->u1.node->u2.id));
 		params.push_back(compile_id(node->u2.node->u1.node->u2.id));
-		params.push_back(ConstantInt::get(Type::Int8Ty,
+		params.push_back(ConstantInt::get(Int8Ty,
 			    dynamic_class ? 1 : 0));
 
 		compile_protected_call(aliasFunc, params);
@@ -4500,8 +4491,7 @@
 		    undefFunc =
 			cast<Function>(module->getOrInsertFunction(
 				"rb_vm_undef",
-				Type::VoidTy, RubyObjTy, IntTy, Type::Int8Ty,
-				NULL));
+				VoidTy, RubyObjTy, IntTy, Int8Ty, NULL));
 		}
 
 		assert(node->u2.node != NULL);
@@ -4512,7 +4502,7 @@
 		std::vector<Value *> params;
 		params.push_back(compile_current_class());
 		params.push_back(compile_id(name_id));
-		params.push_back(ConstantInt::get(Type::Int8Ty,
+		params.push_back(ConstantInt::get(Int8Ty,
 			    dynamic_class ? 1 : 0));
 
 		compile_protected_call(undefFunc, params);
@@ -4542,11 +4532,11 @@
 		    // VALUE rb_vm_get_special(char code);
 		    getSpecialFunc =
 			cast<Function>(module->getOrInsertFunction("rb_vm_get_special",
-				RubyObjTy, Type::Int8Ty, NULL));
+				RubyObjTy, Int8Ty, NULL));
 		}
 
 		std::vector<Value *> params;
-		params.push_back(ConstantInt::get(Type::Int8Ty, code));
+		params.push_back(ConstantInt::get(Int8Ty, code));
 
 		return CallInst::Create(getSpecialFunc, params.begin(), params.end(), "", bb);
 	    }
@@ -4563,13 +4553,15 @@
 		Function *f = bb->getParent();
 
 		BasicBlock *old_begin_bb = begin_bb;
-		begin_bb = BasicBlock::Create("begin", f);
+		begin_bb = BasicBlock::Create(context, "begin", f);
 
 		BasicBlock *old_rescue_invoke_bb = rescue_invoke_bb;
 		BasicBlock *old_rescue_rethrow_bb = rescue_rethrow_bb;
-		BasicBlock *new_rescue_invoke_bb = BasicBlock::Create("rescue", f);
-		BasicBlock *new_rescue_rethrow_bb = BasicBlock::Create("rescue.rethrow", f);
-		BasicBlock *merge_bb = BasicBlock::Create("merge", f);
+		BasicBlock *new_rescue_invoke_bb =
+		    BasicBlock::Create(context, "rescue", f);
+		BasicBlock *new_rescue_rethrow_bb =
+		    BasicBlock::Create(context, "rescue.rethrow", f);
+		BasicBlock *merge_bb = BasicBlock::Create(context, "merge", f);
 
 		// Begin code.
 		BranchInst::Create(begin_bb, bb);
@@ -4581,7 +4573,7 @@
 		rescue_invoke_bb = old_rescue_invoke_bb;
 
 		if (node->nd_else != NULL) {
-		    BasicBlock *else_bb = BasicBlock::Create("else", f);
+		    BasicBlock *else_bb = BasicBlock::Create(context, "else", f);
 		    BranchInst::Create(else_bb, bb);
 		    bb = else_bb;
 		    not_rescued_val = compile_node(node->nd_else);
@@ -4636,7 +4628,7 @@
 		NODE *n = node;
 
 		Function *f = bb->getParent();
-		BasicBlock *merge_bb = BasicBlock::Create("merge", f);
+		BasicBlock *merge_bb = BasicBlock::Create(context, "merge", f);
 		BasicBlock *handler_bb = NULL;
 
 		std::vector<std::pair<Value *, BasicBlock *> > handlers;
@@ -4666,8 +4658,8 @@
 		    if (isEHActiveFunc == NULL) {
 			// bool rb_vm_is_eh_active(int argc, ...);
 			std::vector<const Type *> types;
-			types.push_back(Type::Int32Ty);
-			FunctionType *ft = FunctionType::get(Type::Int8Ty,
+			types.push_back(Int32Ty);
+			FunctionType *ft = FunctionType::get(Int8Ty,
 				types, true);
 			isEHActiveFunc = cast<Function>(
 				module->getOrInsertFunction(
@@ -4676,19 +4668,19 @@
 
 		    const int size = exceptions_to_catch.size();
 		    exceptions_to_catch.insert(exceptions_to_catch.begin(), 
-			    ConstantInt::get(Type::Int32Ty, size));
+			    ConstantInt::get(Int32Ty, size));
 
 		    Value *handler_active = CallInst::Create(isEHActiveFunc, 
 			    exceptions_to_catch.begin(), 
 			    exceptions_to_catch.end(), "", bb);
 
-		    Value *is_handler_active = new ICmpInst(ICmpInst::ICMP_EQ,
-			    handler_active,
-			    ConstantInt::get(Type::Int8Ty, 1), "", bb);
+		    Value *is_handler_active = new ICmpInst(*bb,
+			    ICmpInst::ICMP_EQ, handler_active,
+			    ConstantInt::get(Int8Ty, 1));
 		    
-		    handler_bb = BasicBlock::Create("handler", f);
+		    handler_bb = BasicBlock::Create(context, "handler", f);
 		    BasicBlock *next_handler_bb =
-			BasicBlock::Create("handler", f);
+			BasicBlock::Create(context, "handler", f);
 
 		    BranchInst::Create(handler_bb, next_handler_bb,
 			    is_handler_active, bb);
@@ -4753,17 +4745,22 @@
 		BasicBlock *old_ensure_bb = ensure_bb;
 		PHINode *old_ensure_pn = ensure_pn;
 		// the ensure for when the block is left with a return
-		BasicBlock *ensure_return_bb = BasicBlock::Create("ensure.for.return", f);
+		BasicBlock *ensure_return_bb = BasicBlock::Create(context,
+			"ensure.for.return", f);
 		// the ensure for when the block is left without using return
-		BasicBlock *ensure_normal_bb = BasicBlock::Create("ensure.no.return", f);
-		PHINode *new_ensure_pn = PHINode::Create(RubyObjTy, "ensure.phi", ensure_return_bb);
+		BasicBlock *ensure_normal_bb = BasicBlock::Create(context,
+			"ensure.no.return", f);
+		PHINode *new_ensure_pn = PHINode::Create(RubyObjTy,
+			"ensure.phi", ensure_return_bb);
 		ensure_pn = new_ensure_pn;
 		Value *val;
 
 		ensure_bb = ensure_return_bb;
 
-		BasicBlock *new_rescue_invoke_bb = BasicBlock::Create("rescue", f);
-		BasicBlock *new_rescue_rethrow_bb = BasicBlock::Create("rescue.rethrow", f);
+		BasicBlock *new_rescue_invoke_bb = BasicBlock::Create(context,
+			"rescue", f);
+		BasicBlock *new_rescue_rethrow_bb = BasicBlock::Create(context,
+			"rescue.rethrow", f);
 		BasicBlock *old_rescue_invoke_bb = rescue_invoke_bb;
 		BasicBlock *old_rescue_rethrow_bb = rescue_rethrow_bb;
 
@@ -4776,7 +4773,8 @@
 		rescue_invoke_bb = old_rescue_invoke_bb;
 		BranchInst::Create(ensure_normal_bb, bb);
 
-		if (new_rescue_invoke_bb->use_empty() && new_rescue_rethrow_bb->use_empty()) {
+		if (new_rescue_invoke_bb->use_empty()
+			&& new_rescue_rethrow_bb->use_empty()) {
 		    new_rescue_invoke_bb->eraseFromParent();
 		    new_rescue_rethrow_bb->eraseFromParent();
 		}
@@ -4829,10 +4827,11 @@
 
 		Function *f = bb->getParent();
 
-		BasicBlock *loopBB = BasicBlock::Create("loop", f);
-		BasicBlock *bodyBB = BasicBlock::Create("body", f);
-		BasicBlock *exitBB = BasicBlock::Create("loop_exit", f);
-		BasicBlock *afterBB = BasicBlock::Create("after", f);
+		BasicBlock *loopBB = BasicBlock::Create(context, "loop", f);
+		BasicBlock *bodyBB = BasicBlock::Create(context, "body", f);
+		BasicBlock *exitBB = BasicBlock::Create(context, "loop_exit",
+			f);
+		BasicBlock *afterBB = BasicBlock::Create(context, "after", f);
 
 		const bool first_pass_free = node->nd_state == 0;
 
@@ -4916,8 +4915,8 @@
 		    // call inside an exception handler, since return-from
 		    // -block is implemented using a C++ exception.
 		    Function *f = bb->getParent();
-		    rescue_invoke_bb = return_from_block_bb = BasicBlock::Create(
-			    "return-from-block", f);
+		    rescue_invoke_bb = return_from_block_bb =
+			BasicBlock::Create(context, "return-from-block", f);
 		}
 
 		current_loop_begin_bb = old_current_loop_begin_bb;
@@ -4952,8 +4951,8 @@
 
 		    params.push_back(compile_sel(selEach));
 		    params.push_back(compile_block_create(NULL));
-		    params.push_back(ConstantInt::get(Type::Int8Ty, 0));
-		    params.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    params.push_back(ConstantInt::get(Int8Ty, 0));
+		    params.push_back(ConstantInt::get(Int32Ty, 0));
 
 		    caller = compile_dispatch_call(params);
 		}
@@ -4981,7 +4980,7 @@
 		if (yieldFunc == NULL) {
 		    // VALUE rb_vm_yield_args(int argc, ...)
 		    std::vector<const Type *> types;
-		    types.push_back(Type::Int32Ty);
+		    types.push_back(Int32Ty);
 		    FunctionType *ft =
 			FunctionType::get(RubyObjTy, types, true);
 		    yieldFunc = cast<Function>(module->getOrInsertFunction(
@@ -4994,7 +4993,7 @@
 		    compile_dispatch_arguments(node->nd_head, params, &argc);
 		}
 		params.insert(params.begin(),
-			ConstantInt::get(Type::Int32Ty, argc));
+			ConstantInt::get(Int32Ty, argc));
 
 		Value *val = compile_protected_call(yieldFunc, params);
 
@@ -5006,17 +5005,18 @@
 		}
 
 		Value *broken = CallInst::Create(getBrokenFunc, "", bb);
-		Value *is_broken = new ICmpInst(ICmpInst::ICMP_NE, broken,
-			undefVal, "", bb);
+		Value *is_broken = new ICmpInst(*bb, ICmpInst::ICMP_NE, broken,
+			undefVal);
 
 		Function *f = bb->getParent();
-		BasicBlock *broken_bb = BasicBlock::Create("broken", f);
-		BasicBlock *next_bb = BasicBlock::Create("", f);
+		BasicBlock *broken_bb = BasicBlock::Create(context, "broken",
+			f);
+		BasicBlock *next_bb = BasicBlock::Create(context, "next", f);
 
 		BranchInst::Create(broken_bb, next_bb, is_broken, bb);
 
 		bb = broken_bb;
-		ReturnInst::Create(broken, bb);
+		ReturnInst::Create(context, broken, bb);
 		
 		bb = next_bb;
 		return val;
@@ -5045,7 +5045,7 @@
 	case NODE_CASE:
 	    {
 		Function *f = bb->getParent();
-		BasicBlock *caseMergeBB = BasicBlock::Create("case_merge", f);
+		BasicBlock *caseMergeBB = BasicBlock::Create(context, "case_merge", f);
 
 		PHINode *pn = PHINode::Create(RubyObjTy, "case_tmp",
 			caseMergeBB);
@@ -5064,7 +5064,7 @@
 		    NODE *valueNode = subnode->nd_head;
 		    assert(valueNode != NULL);
 
-		    BasicBlock *thenBB = BasicBlock::Create("then", f);
+		    BasicBlock *thenBB = BasicBlock::Create(context, "then", f);
 
 		    compile_when_arguments(valueNode, comparedToVal, thenBB);
 		    BasicBlock *nextWhenBB = bb;
@@ -5113,9 +5113,8 @@
 		params.push_back(compile_nsobject());
 		params.push_back(compile_sel(sel));
 		params.push_back(compile_block_create(NULL));
-		params.push_back(ConstantInt::get(Type::Int8Ty,
-			    DISPATCH_FCALL));
-		params.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		params.push_back(ConstantInt::get(Int8Ty, DISPATCH_FCALL));
+		params.push_back(ConstantInt::get(Int32Ty, 0));
 
 		current_block_func = old_current_block_func;
 		current_block_node = old_current_block_node;
@@ -5209,8 +5208,8 @@
 	    compile_const_global_string(rb_id2name(name));
 
 	std::vector<Value *> idxs;
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
 	Instruction *load = GetElementPtrInst::Create(const_gvar,
 		idxs.begin(), idxs.end(), "");
 
@@ -5244,8 +5243,8 @@
 	    compile_const_global_string(sel_getName(sel));
 
 	std::vector<Value *> idxs;
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
 	Instruction *load = GetElementPtrInst::Create(sel_gvar,
 		idxs.begin(), idxs.end(), "");
 
@@ -5270,7 +5269,7 @@
 
     Function *newRegexp2Func =
 	cast<Function>(module->getOrInsertFunction("rb_reg_new_retained",
-		    RubyObjTy, PtrTy, Type::Int32Ty, Type::Int32Ty, NULL));
+		    RubyObjTy, PtrTy, Int32Ty, Int32Ty, NULL));
 
     Function *getClassFunc =
 	cast<Function>(module->getOrInsertFunction("objc_getClass",
@@ -5292,8 +5291,8 @@
 			compile_const_global_string(class_getName((Class)val));
 
 		    std::vector<Value *> idxs;
-		    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-		    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    idxs.push_back(ConstantInt::get(Int32Ty, 0));
+		    idxs.push_back(ConstantInt::get(Int32Ty, 0));
 		    Instruction *load = GetElementPtrInst::Create(kname_gvar,
 			    idxs.begin(), idxs.end(), "");
 
@@ -5324,16 +5323,16 @@
 			    compile_const_global_string(re->str, re->len);
 
 			std::vector<Value *> idxs;
-			idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-			idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+			idxs.push_back(ConstantInt::get(Int32Ty, 0));
+			idxs.push_back(ConstantInt::get(Int32Ty, 0));
 			re_str = GetElementPtrInst::Create(rename_gvar,
 				idxs.begin(), idxs.end(), "");
 		    }
 
 		    std::vector<Value *> params;
 		    params.push_back(re_str);
-		    params.push_back(ConstantInt::get(Type::Int32Ty, re->len));
-		    params.push_back(ConstantInt::get(Type::Int32Ty,
+		    params.push_back(ConstantInt::get(Int32Ty, re->len));
+		    params.push_back(ConstantInt::get(Int32Ty,
 				re->ptr->options));
 
 		    Instruction *call = CallInst::Create(newRegexp2Func,
@@ -5355,8 +5354,8 @@
 			compile_const_global_string(symname);
 
 		    std::vector<Value *> idxs;
-		    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-		    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+		    idxs.push_back(ConstantInt::get(Int32Ty, 0));
+		    idxs.push_back(ConstantInt::get(Int32Ty, 0));
 		    Instruction *load = GetElementPtrInst::Create(symname_gvar,
 			    idxs.begin(), idxs.end(), "");
 
@@ -5450,8 +5449,8 @@
 	    compile_const_global_string(rb_id2name(name));
 
 	std::vector<Value *> idxs;
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
 	Instruction *load = GetElementPtrInst::Create(name_gvar,
 		idxs.begin(), idxs.end(), "");
 
@@ -5481,11 +5480,11 @@
 	GlobalVariable *gvar = *i;
 
 	GlobalVariable *str = compile_const_global_string(
-		gvar->getName().c_str());
+		gvar->getName().str().c_str());
 
 	std::vector<Value *> idxs;
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
 	Instruction *load = GetElementPtrInst::Create(str,
 		idxs.begin(), idxs.end(), "");
 
@@ -5508,8 +5507,8 @@
 	GlobalVariable *toplevel = compile_const_global_string("TopLevel");
 
 	std::vector<Value *> idxs;
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-	idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
+	idxs.push_back(ConstantInt::get(Int32Ty, 0));
 	Instruction *load = GetElementPtrInst::Create(toplevel,
 		idxs.begin(), idxs.end(), "");
 
@@ -5532,9 +5531,9 @@
 
 	GlobalVariable *gvar = *i;
 
-	Instruction *call = new MallocInst(Type::Int32Ty, "");
+	Instruction *call = new MallocInst(Int32Ty, "");
 	Instruction *assign1 =
-	    new StoreInst(ConstantInt::getSigned(Type::Int32Ty, -1), call, "");
+	    new StoreInst(ConstantInt::getSigned(Int32Ty, -1), call, "");
 	Instruction *assign2 = new StoreInst(call, gvar, "");
 
 	list.insert(list.begin(), assign2);
@@ -5556,11 +5555,11 @@
     Function::arg_iterator arg = f->arg_begin();
     current_self = arg++;
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     Value *val = compile_ivar_read(name);
 
-    ReturnInst::Create(val, bb);
+    ReturnInst::Create(context, val, bb);
 
     return f;
 }
@@ -5576,11 +5575,11 @@
     arg++; // sel
     Value *new_val = arg++; // 1st argument
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     Value *val = compile_ivar_assignment(name, new_val);
 
-    ReturnInst::Create(val, bb);
+    ReturnInst::Create(context, val, bb);
 
     return f;
 }
@@ -5896,7 +5895,7 @@
     if (getStructFieldsFunc == NULL) {
 	getStructFieldsFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_get_struct_fields",
-		    Type::VoidTy, RubyObjTy, RubyObjPtrTy, PtrTy, NULL));
+		    VoidTy, RubyObjTy, RubyObjPtrTy, PtrTy, NULL));
     }
 
     std::vector<Value *> params;
@@ -6046,7 +6045,7 @@
 		rb_vm_bs_boxed_t *bs_boxed = GET_CORE()->find_bs_struct(type);
 		if (bs_boxed != NULL) {
 		    Value *fields = new AllocaInst(RubyObjTy,
-			    ConstantInt::get(Type::Int32Ty,
+			    ConstantInt::get(Int32Ty,
 				bs_boxed->as.s->fields_count), "", bb);
 
 		    compile_get_struct_fields(val, fields, bs_boxed);
@@ -6058,15 +6057,15 @@
 
 			// Load field VALUE.
 			Value *fval = GetElementPtrInst::Create(fields,
-				ConstantInt::get(Type::Int32Ty, i), "", bb);
+				ConstantInt::get(Int32Ty, i), "", bb);
 			fval = new LoadInst(fval, "", bb);
 
 			// Get a pointer to the struct field. The extra 0 is
 			// needed because we are dealing with a pointer to the
 			// structure.
 			std::vector<Value *> slot_idx;
-			slot_idx.push_back(ConstantInt::get(Type::Int32Ty, 0));
-			slot_idx.push_back(ConstantInt::get(Type::Int32Ty, i));
+			slot_idx.push_back(ConstantInt::get(Int32Ty, 0));
+			slot_idx.push_back(ConstantInt::get(Int32Ty, i));
 			Value *fslot = GetElementPtrInst::Create(slot,
 				slot_idx.begin(), slot_idx.end(), "", bb);
 
@@ -6105,7 +6104,7 @@
     params.push_back(slot);
 
     Function *func = cast<Function>(module->getOrInsertFunction(
-		func_name, Type::VoidTy, RubyObjTy,
+		func_name, VoidTy, RubyObjTy,
 		PointerType::getUnqual(convert_type(type)), NULL));
 
     CallInst::Create(func, params.begin(), params.end(), "", bb);
@@ -6199,14 +6198,14 @@
     if (newStructFunc == NULL) {
 	std::vector<const Type *> types;
 	types.push_back(RubyObjTy);
-	types.push_back(Type::Int32Ty);
+	types.push_back(Int32Ty);
 	FunctionType *ft = FunctionType::get(RubyObjTy, types, true);
 
 	newStructFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_new_struct", ft));
     }
 
-    Value *argc = ConstantInt::get(Type::Int32Ty, fields.size());
+    Value *argc = ConstantInt::get(Int32Ty, fields.size());
     fields.insert(fields.begin(), argc);
     fields.insert(fields.begin(), klass);
 
@@ -6242,8 +6241,8 @@
 
     GlobalVariable *gvar = compile_const_global_string(type);
     std::vector<Value *> idxs;
-    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
-    idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
+    idxs.push_back(ConstantInt::get(Int32Ty, 0));
+    idxs.push_back(ConstantInt::get(Int32Ty, 0));
     Instruction *load = GetElementPtrInst::Create(gvar,
 	    idxs.begin(), idxs.end(), "", bb);
     params.push_back(load);
@@ -6272,8 +6271,8 @@
 
 	case _C_BOOL:
 	    {
-		Value *is_true = new ICmpInst(ICmpInst::ICMP_EQ, val,
-			ConstantInt::get(Type::Int8Ty, 1), "", bb);
+		Value *is_true = new ICmpInst(*bb, ICmpInst::ICMP_EQ, val,
+			ConstantInt::get(Int8Ty, 1));
 		return SelectInst::Create(is_true, trueVal, falseVal, "", bb);
 	    }
 
@@ -6315,7 +6314,7 @@
 	    break;
 
 	case _C_FLT:
-	    val = new FPExtInst(val, Type::DoubleTy, "", bb);
+	    val = new FPExtInst(val, DoubleTy, "", bb);
 	    // fall through	
 	case _C_DBL:
 	    val = new BitCastInst(val, RubyObjTy, "", bb);
@@ -6388,7 +6387,7 @@
 
     switch (*type) {
 	case _C_VOID:
-	    return Type::VoidTy;
+	    return VoidTy;
 
 	case _C_ID:
 	case _C_CLASS:
@@ -6402,33 +6401,33 @@
 	case _C_BOOL:
 	case _C_UCHR:
 	case _C_CHR:
-	    return Type::Int8Ty;
+	    return Int8Ty;
 
 	case _C_SHT:
 	case _C_USHT:
-	    return Type::Int16Ty;
+	    return Int16Ty;
 
 	case _C_INT:
 	case _C_UINT:
-	    return Type::Int32Ty;
+	    return Int32Ty;
 
 	case _C_LNG:
 	case _C_ULNG:
 #if __LP64__
-	    return Type::Int64Ty;
+	    return Int64Ty;
 #else
-	    return Type::Int32Ty;
+	    return Int32Ty;
 #endif
 
 	case _C_FLT:
-	    return Type::FloatTy;
+	    return FloatTy;
 
 	case _C_DBL:
-	    return Type::DoubleTy;
+	    return DoubleTy;
 
 	case _C_LNG_LNG:
 	case _C_ULNG_LNG:
-	    return Type::Int64Ty;
+	    return Int64Ty;
 
 	case _C_STRUCT_B:
 	    rb_vm_bs_boxed_t *bs_boxed = GET_CORE()->find_bs_struct(type);
@@ -6441,7 +6440,7 @@
 			const char *ftype = bs_boxed->as.s->fields[i].type;
 			s_types.push_back(convert_type(ftype));
 		    }
-		    bs_boxed->type = StructType::get(s_types);
+		    bs_boxed->type = StructType::get(context, s_types);
 		    assert(bs_boxed->type != NULL);
 		}
 		return bs_boxed->type;
@@ -6464,8 +6463,7 @@
 	// }
 	f = cast<Function>(module->getOrInsertFunction("",
 		    RubyObjTy,
-		    PtrTy, RubyObjTy, PtrTy, Type::Int32Ty, RubyObjPtrTy,
-		    NULL));
+		    PtrTy, RubyObjTy, PtrTy, Int32Ty, RubyObjPtrTy, NULL));
     }
     else {
 	// VALUE stub(IMP imp, int argc, VALUE *argv)
@@ -6474,11 +6472,11 @@
 	// }
 	f = cast<Function>(module->getOrInsertFunction("",
 		    RubyObjTy,
-		    PtrTy, Type::Int32Ty, RubyObjPtrTy,
+		    PtrTy, Int32Ty, RubyObjPtrTy,
 		    NULL));
     }
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     Function::arg_iterator arg = f->arg_begin();
     Value *imp_arg = arg++;
@@ -6499,7 +6497,7 @@
 	f_types.push_back(PointerType::getUnqual(ret_type));
 	sret = new AllocaInst(ret_type, "", bb);
 	params.push_back(sret);
-	ret_type = Type::VoidTy;
+	ret_type = VoidTy;
     }
 
     if (is_objc) {
@@ -6548,7 +6546,7 @@
 	    f_types.push_back(f_type);
 	}
 
-	Value *index = ConstantInt::get(Type::Int32Ty, given_argc);
+	Value *index = ConstantInt::get(Int32Ty, given_argc);
 	Value *slot = GetElementPtrInst::Create(argv_arg, index, "", bb);
 	Value *arg_val = new LoadInst(slot, "", bb);
 	Value *new_val_slot = new AllocaInst(llvm_type, "", bb);
@@ -6582,7 +6580,7 @@
     }
     GetFirstType(types, buf, sizeof buf);
     retval = compile_conversion_to_ruby(buf, convert_type(buf), retval);
-    ReturnInst::Create(retval, bb);
+    ReturnInst::Create(context, retval, bb);
 
     return f;
 }
@@ -6658,7 +6656,7 @@
 	// the ABI.
 	f_types.push_back(PointerType::getUnqual(f_ret_type));
 	f_sret_type = f_ret_type;
-	f_ret_type = Type::VoidTy;
+	f_ret_type = VoidTy;
     }
 
     // self
@@ -6691,7 +6689,7 @@
     Function *f = cast<Function>(module->getOrInsertFunction("", ft));
     Function::arg_iterator arg = f->arg_begin();
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     Value *sret = NULL;
     int sret_i = 0;
@@ -6743,17 +6741,17 @@
 	    "", bb);
 
     // Convert the return value into Objective-C type (if any).
-    if (f_ret_type != Type::VoidTy) {
+    if (f_ret_type != VoidTy) {
 	ret_val = compile_conversion_to_c(ret_type.c_str(), ret_val,
 		new AllocaInst(f_ret_type, "", bb));
-	ReturnInst::Create(ret_val, bb);
+	ReturnInst::Create(context, ret_val, bb);
     }
     else if (sret != NULL) {
 	compile_conversion_to_c(ret_type.c_str(), ret_val, sret);
-	ReturnInst::Create(bb);
+	ReturnInst::Create(context, bb);
     }
     else {
-	ReturnInst::Create(bb);
+	ReturnInst::Create(context, bb);
     }
 
     return f;
@@ -6767,7 +6765,7 @@
     //     return rb_vm_block_eval2(block, rcv, sel, argc, argv);
     // }
     Function *f = cast<Function>(module->getOrInsertFunction("",
-		RubyObjTy, RubyObjTy, PtrTy, Type::Int32Ty, RubyObjPtrTy,
+		RubyObjTy, RubyObjTy, PtrTy, Int32Ty, RubyObjPtrTy,
 		NULL));
     Function::arg_iterator arg = f->arg_begin();
     Value *rcv = arg++;
@@ -6775,15 +6773,15 @@
     Value *argc = arg++;
     Value *argv = arg++;
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     if (blockEvalFunc == NULL) {
 	// VALUE rb_vm_block_eval2(rb_vm_block_t *b, VALUE self, SEL sel,
 	//	int argc, const VALUE *argv)
 	blockEvalFunc = cast<Function>(module->getOrInsertFunction(
 		    "rb_vm_block_eval2",
-		    RubyObjTy, PtrTy, RubyObjTy, PtrTy, Type::Int32Ty,
-		    RubyObjPtrTy, NULL));
+		    RubyObjTy, PtrTy, RubyObjTy, PtrTy, Int32Ty, RubyObjPtrTy,
+		    NULL));
     }
     std::vector<Value *> params;
     params.push_back(compile_const_pointer(block));
@@ -6794,7 +6792,7 @@
 
     Value *retval = compile_protected_call(blockEvalFunc, params);
 
-    ReturnInst::Create(retval, bb);
+    ReturnInst::Create(context, retval, bb);
 
     return f;
 }
@@ -6808,7 +6806,7 @@
     Function::arg_iterator arg = f->arg_begin();
     Value *ocval = arg++;
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     const Type *llvm_type = convert_type(type); 
     ocval = new BitCastInst(ocval, PointerType::getUnqual(llvm_type), "", bb);
@@ -6816,7 +6814,7 @@
 
     Value *rval = compile_conversion_to_ruby(type, llvm_type, ocval);
 
-    ReturnInst::Create(rval, bb);
+    ReturnInst::Create(context, rval, bb);
 
     return f;
 }
@@ -6826,18 +6824,18 @@
 {
     // void foo(VALUE rval, void **ocval);
     Function *f = cast<Function>(module->getOrInsertFunction("",
-		Type::VoidTy, RubyObjTy, PtrTy, NULL));
+		VoidTy, RubyObjTy, PtrTy, NULL));
     Function::arg_iterator arg = f->arg_begin();
     Value *rval = arg++;
     Value *ocval = arg++;
 
-    bb = BasicBlock::Create("EntryBlock", f);
+    bb = BasicBlock::Create(context, "EntryBlock", f);
 
     const Type *llvm_type = convert_type(type);
     ocval = new BitCastInst(ocval, PointerType::getUnqual(llvm_type), "", bb);
     compile_conversion_to_c(type, rval, ocval);
 
-    ReturnInst::Create(bb);
+    ReturnInst::Create(context, bb);
 
     return f;
 }

Modified: MacRuby/trunk/compiler.h
===================================================================
--- MacRuby/trunk/compiler.h	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/compiler.h	2009-09-25 04:15:23 UTC (rev 2630)
@@ -27,21 +27,12 @@
 #define DEFINED_SUPER	6
 #define DEFINED_METHOD	7
 
-class RoxorFunctionAnnotation : public Annotation {
+class RoxorScope {
     public:
-	static AnnotationID id;
 	std::string path;
 	std::vector<unsigned int> dispatch_lines;
 
-	RoxorFunctionAnnotation(Function *function, const char *_path)
-	    : Annotation(RoxorFunctionAnnotation::id), path(_path) {
-		function->addAnnotation(this);
-	    }
-
-	static RoxorFunctionAnnotation *from_function(Function *function) {
-	    return (RoxorFunctionAnnotation *)
-		function->getAnnotation(RoxorFunctionAnnotation::id);
-	}
+	RoxorScope(const char *fname) : path(fname) {}
 };
 
 class RoxorCompiler {
@@ -79,6 +70,15 @@
 	bool is_dynamic_class(void) { return dynamic_class; }
 	void set_dynamic_class(bool flag) { dynamic_class = flag; }
 
+	RoxorScope *scope_for_function(Function *f) {
+	    std::map<Function *, RoxorScope *>::iterator i = scopes.find(f);
+	    return i == scopes.end() ? NULL : i->second;
+	}
+
+	void clear_scopes(void) {
+	    scopes.clear();
+	}
+
     protected:
 	const char *fname;
 	bool inside_eval;
@@ -88,6 +88,7 @@
 	std::map<ID, Instruction *> ivar_slots_cache;
 	std::map<std::string, GlobalVariable *> static_strings;
 	std::map<CFHashCode, GlobalVariable *> static_ustrings;
+	std::map<Function *, RoxorScope *> scopes;
 
 #if ROXOR_COMPILER_DEBUG
 	int level;
@@ -129,7 +130,7 @@
 	int return_from_block;
 	int return_from_block_ids;
 	PHINode *ensure_pn;
-	RoxorFunctionAnnotation *func_annotation;
+	RoxorScope *current_scope;
 
 	Function *dispatcherFunc;
 	Function *fastPlusFunc;
@@ -214,6 +215,15 @@
 	Constant *splatArgFollowsVal;
 	Constant *defaultScope;
 	Constant *publicScope;
+
+	const Type *VoidTy;
+	const Type *Int1Ty;
+	const Type *Int8Ty;
+	const Type *Int16Ty;
+	const Type *Int32Ty;
+	const Type *Int64Ty;
+	const Type *FloatTy;
+	const Type *DoubleTy;
 	const Type *RubyObjTy; 
 	const Type *RubyObjPtrTy;
 	const Type *RubyObjPtrPtrTy;
@@ -350,6 +360,8 @@
 	SEL mid_to_sel(ID mid, int arity);
 };
 
+#define context (RoxorCompiler::module->getContext())
+
 class RoxorAOTCompiler : public RoxorCompiler {
     public:
 	RoxorAOTCompiler(void);

Modified: MacRuby/trunk/include/ruby/config.h.in
===================================================================
--- MacRuby/trunk/include/ruby/config.h.in	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/include/ruby/config.h.in	2009-09-25 04:15:23 UTC (rev 2630)
@@ -1,8 +1,3 @@
-#define PACKAGE_NAME ""
-#define PACKAGE_TARNAME ""
-#define PACKAGE_VERSION ""
-#define PACKAGE_STRING ""
-#define PACKAGE_BUGREPORT ""
 #define NEXT_FAT_BINARY 1
 #define USE_BUILTIN_FRAME_ADDRESS 1
 #define _GNU_SOURCE 1
@@ -60,6 +55,9 @@
 #define TOKEN_PASTE(x,y) x##y
 #define STRINGIZE(expr) STRINGIZE0(expr)
 #define HAVE_STDARG_PROTOTYPES 1
+#if defined(NORETURN)
+#undef NORETURN
+#endif
 #define NORETURN(x) __attribute__ ((noreturn)) x
 #define DEPRECATED(x) __attribute__ ((deprecated)) x
 #define NOINLINE(x) __attribute__ ((noinline)) x

Modified: MacRuby/trunk/rakelib/builder.rb
===================================================================
--- MacRuby/trunk/rakelib/builder.rb	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/rakelib/builder.rb	2009-09-25 04:15:23 UTC (rev 2630)
@@ -83,7 +83,7 @@
 
 INSTALL_NAME = File.join(FRAMEWORK_USR_LIB, 'lib' + RUBY_SO_NAME + '.dylib')
 ARCHFLAGS = ARCHS.map { |a| '-arch ' + a }.join(' ')
-LLVM_MODULES = "core jit nativecodegen interpreter bitwriter"
+LLVM_MODULES = "core jit nativecodegen bitwriter"
 
 CC = '/usr/bin/gcc'
 CXX = '/usr/bin/g++'

Modified: MacRuby/trunk/vm.cpp
===================================================================
--- MacRuby/trunk/vm.cpp	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/vm.cpp	2009-09-25 04:15:23 UTC (rev 2630)
@@ -7,8 +7,7 @@
  */
 
 #define ROXOR_VM_DEBUG		0
-#define ROXOR_INTERPRET_EVAL	0
-#define ROXOR_COMPILER_DEBUG 	0
+#define ROXOR_COMPILER_DEBUG 	0	
 
 #include <llvm/Module.h>
 #include <llvm/DerivedTypes.h>
@@ -25,6 +24,7 @@
 #include <llvm/Target/TargetData.h>
 #include <llvm/Target/TargetMachine.h>
 #include <llvm/Target/TargetOptions.h>
+#include <llvm/Target/TargetSelect.h>
 #include <llvm/Transforms/Scalar.h>
 #include <llvm/Support/raw_ostream.h>
 #include <llvm/Intrinsics.h>
@@ -59,12 +59,15 @@
 
 struct RoxorFunction {
     Function *f;
+    RoxorScope *scope;
     unsigned char *start;
     unsigned char *end;
     void *imp;
 
-    RoxorFunction (Function *_f, unsigned char *_start, unsigned char *_end) {
+    RoxorFunction(Function *_f, RoxorScope *_scope, unsigned char *_start,
+	    unsigned char *_end) {
 	f = _f;
+	scope = _scope;
 	start = _start;
 	end = _end;
 	imp = NULL; 	// lazy
@@ -81,7 +84,7 @@
 	    mm = CreateDefaultMemManager(); 
 	}
 
-	struct RoxorFunction *find_function(unsigned char *addr) {
+	struct RoxorFunction *find_function(uint8_t *addr) {
 	     if (functions.empty()) {
 		return NULL;
 	     }
@@ -111,15 +114,19 @@
 	    mm->setMemoryExecutable(); 
 	}
 
-	unsigned char *allocateSpace(intptr_t Size, unsigned Alignment) { 
+	uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) { 
 	    return mm->allocateSpace(Size, Alignment); 
 	}
 
+	uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
+	    return mm->allocateGlobal(Size, Alignment);
+	}
+
 	void AllocateGOT(void) {
 	    mm->AllocateGOT();
 	}
 
-	unsigned char *getGOTBase() const {
+	uint8_t *getGOTBase() const {
 	    return mm->getGOTBase();
 	}
 
@@ -131,37 +138,43 @@
 	    return mm->getDlsymTable();
 	}
 
-	unsigned char *startFunctionBody(const Function *F, 
+	uint8_t *startFunctionBody(const Function *F, 
 		uintptr_t &ActualSize) {
 	    return mm->startFunctionBody(F, ActualSize);
 	}
 
-	unsigned char *allocateStub(const GlobalValue* F, 
+	uint8_t *allocateStub(const GlobalValue* F, 
 		unsigned StubSize, 
 		unsigned Alignment) {
 	    return mm->allocateStub(F, StubSize, Alignment);
 	}
 
-	void endFunctionBody(const Function *F, unsigned char *FunctionStart, 
-		unsigned char *FunctionEnd) {
+	void endFunctionBody(const Function *F, uint8_t *FunctionStart, 
+		uint8_t *FunctionEnd) {
 	    mm->endFunctionBody(F, FunctionStart, FunctionEnd);
-	    functions.push_back(new RoxorFunction(const_cast<Function *>(F), 
-			FunctionStart, FunctionEnd));
+	    Function *f = const_cast<Function *>(F);
+	    RoxorScope *s = RoxorCompiler::shared->scope_for_function(f);
+	    functions.push_back(new RoxorFunction(f, s, FunctionStart,
+			FunctionEnd));
 	}
 
 	void deallocateMemForFunction(const Function *F) {
 	    mm->deallocateMemForFunction(F);
 	}
 
-	unsigned char* startExceptionTable(const Function* F, 
+	uint8_t* startExceptionTable(const Function* F, 
 		uintptr_t &ActualSize) {
 	    return mm->startExceptionTable(F, ActualSize);
 	}
 
-	void endExceptionTable(const Function *F, unsigned char *TableStart, 
-		unsigned char *TableEnd, unsigned char* FrameRegister) {
+	void endExceptionTable(const Function *F, uint8_t *TableStart, 
+		uint8_t *TableEnd, uint8_t* FrameRegister) {
 	    mm->endExceptionTable(F, TableStart, TableEnd, FrameRegister);
 	}
+
+	void setPoisonMemory(bool poison) {
+	    mm->setPoisonMemory(poison);
+	}
 };
 
 #define FROM_GV(gv,t) ((t)(gv.IntVal.getZExtValue()))
@@ -200,18 +213,27 @@
 
     bs_parser = NULL;
 
+    //llvm_start_multithreaded();
+
     emp = new ExistingModuleProvider(RoxorCompiler::module);
     jmm = new RoxorJITManager;
-    ee = ExecutionEngine::createJIT(emp, 0, jmm, CodeGenOpt::None);
-    assert(ee != NULL);
 
+    InitializeNativeTarget();
+
+    std::string err;
+    ee = ExecutionEngine::createJIT(emp, &err, jmm, CodeGenOpt::None, false);
+    if (ee == NULL) {
+	fprintf(stderr, "error while creating JIT: %s\n", err.c_str());
+	abort();
+    }
+
     fpm = new FunctionPassManager(emp);
     fpm->add(new TargetData(*ee->getTargetData()));
 
+    // Do simple "peephole" optimizations and bit-twiddling optzns.
+    fpm->add(createInstructionCombiningPass());
     // Eliminate unnecessary alloca.
     fpm->add(createPromoteMemoryToRegisterPass());
-    // Do simple "peephole" optimizations and bit-twiddling optzns.
-    fpm->add(createInstructionCombiningPass());
     // Reassociate expressions.
     fpm->add(createReassociatePass());
     // Eliminate Common SubExpressions.
@@ -221,8 +243,13 @@
     // Eliminate tail calls.
     fpm->add(createTailCallEliminationPass());
 
-    iee = ExecutionEngine::create(emp, true);
-    assert(iee != NULL);
+#if USE_LLVM_INTERPRETER
+    iee = ExecutionEngine::create(emp, true, &err);
+    if (iee == NULL) {
+	fprintf(stderr, "error while creating Interpreter: %s\n", err.c_str());
+	abort();
+    }
+#endif
 
 #if ROXOR_VM_DEBUG
     functions_compiled = 0;
@@ -396,6 +423,7 @@
     return imp;
 }
 
+#if USE_LLVM_INTERPRETER
 VALUE
 RoxorCore::interpret(Function *func)
 {
@@ -404,6 +432,7 @@
     args.push_back(PTOGV(NULL));
     return (VALUE)iee->runFunction(func, args).IntVal.getZExtValue();
 }
+#endif
 
 bool
 RoxorCore::symbolize_call_address(void *addr, void **startp, char *path,
@@ -439,11 +468,9 @@
 
 	rb_vm_method_node_t *node = iter->second;
 
-	RoxorFunctionAnnotation *annotation = f == NULL
-	    ? NULL
-	    : RoxorFunctionAnnotation::from_function(f->f);
+	RoxorScope *scope = f == NULL ? NULL : f->scope;
 	if (ln != NULL) {
-	    if (annotation != NULL) {
+	    if (scope != NULL) {
 #if __LP64__
 		// So, we need to determine here which call to the dispatcher
 		// we are exactly, so that we can retrieve the appropriate
@@ -465,8 +492,8 @@
 		    p++;
 		}
 
-		if (i > 0 && i - 1 < annotation->dispatch_lines.size()) {
-		    *ln = annotation->dispatch_lines[i - 1];
+		if (i > 0 && i - 1 < scope->dispatch_lines.size()) {
+		    *ln = scope->dispatch_lines[i - 1];
 		}
 		else {
 		    *ln = 0;
@@ -481,8 +508,8 @@
 	    }
 	}
 	if (path != NULL) {
-	    if (annotation != NULL) {
-		strncpy(path, annotation->path.c_str(), path_len);
+	    if (scope != NULL) {
+		strncpy(path, scope->path.c_str(), path_len);
 	    }
 	    else {
 		strncpy(path, "core", path_len);
@@ -618,13 +645,11 @@
     GlobalVariable *gvar = NULL;
     if (iter == redefined_ops_gvars.end()) {
 	if (create) {
-	    gvar = new GlobalVariable(
-		    Type::Int1Ty,
+	    gvar = new GlobalVariable(*RoxorCompiler::module,
+		    Type::getInt1Ty(context),
 		    ruby_aot_compile ? true : false,
 		    GlobalValue::InternalLinkage,
-		    ConstantInt::getFalse(),
-		    "redefined",
-		    RoxorCompiler::module);
+		    ConstantInt::getFalse(context), "");
 	    assert(gvar != NULL);
 	    redefined_ops_gvars[sel] = gvar;
 	}
@@ -4808,7 +4833,7 @@
 	vm->pop_current_binding(false);
     }
 
-#if ROXOR_INTERPRET_EVAL
+#if USE_LLVM_INTERPRETER
     if (inside_eval) {
 	return GET_CORE()->interpret(function);
     }
@@ -4883,8 +4908,20 @@
     f->setName(init_function_name);
     GET_CORE()->optimize(f);
 
+    // Force a module verification.
+    if (verifyModule(*RoxorCompiler::module, PrintMessageAction)) {
+	printf("Error during module verification\n");
+	exit(1);
+    }
+
     // Dump the bitcode.
-    std::ofstream out(output);
+    std::string err;
+    raw_fd_ostream out(output, err, raw_fd_ostream::F_Binary);
+    if (!err.empty()) {
+	fprintf(stderr, "error when opening the output bitcode file: %s\n",
+		err.c_str());
+	abort();
+    }
     WriteBitcodeToFile(RoxorCompiler::module, out);
     out.close();
 }
@@ -5562,13 +5599,21 @@
     return NO; // TODO call old IMP
 }
 
+// We can't trust LLVM to pick the right target at runtime.
+#if __LP64__
+# define TARGET_TRIPLE "x86_64-apple-darwin"
+#else
+# define TARGET_TRIPLE "i386-apple-darwin"
+#endif
+
 extern "C"
 void 
 Init_PreVM(void)
 {
-    llvm::ExceptionHandling = true; // required!
+    llvm::DwarfExceptionHandling = true; // required!
 
-    RoxorCompiler::module = new llvm::Module("Roxor");
+    RoxorCompiler::module = new llvm::Module("Roxor", getGlobalContext());
+    RoxorCompiler::module->setTargetTriple(TARGET_TRIPLE);
     RoxorCore::shared = new RoxorCore();
     RoxorVM::main = new RoxorVM();
 

Modified: MacRuby/trunk/vm.h
===================================================================
--- MacRuby/trunk/vm.h	2009-09-24 23:24:17 UTC (rev 2629)
+++ MacRuby/trunk/vm.h	2009-09-25 04:15:23 UTC (rev 2630)
@@ -522,6 +522,10 @@
     READER(name, type) \
     WRITER(name, type)
 
+// Flip this switch to enable LLVM Interpreter. Note that this is still under
+// development.
+#define USE_LLVM_INTERPRETER 0
+
 // The Core class is a singleton, it's only created once and it's used by the
 // VMs. All calls to the Core are thread-safe, they acquire a shared lock.
 class RoxorCore {
@@ -533,7 +537,9 @@
 	ExistingModuleProvider *emp;
 	RoxorJITManager *jmm;
 	ExecutionEngine *ee;
+#if USE_LLVM_INTERPRETER
 	ExecutionEngine *iee;
+#endif
 	FunctionPassManager *fpm;
 
 	// Running threads.
@@ -622,7 +628,9 @@
 
 	void optimize(Function *func);
 	IMP compile(Function *func);
+#if USE_LLVM_INTERPRETER
 	VALUE interpret(Function *func);
+#endif
 
 	void load_bridge_support(const char *path, const char *framework_path,
 		int options);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090924/1bdabb67/attachment-0001.html>


More information about the macruby-changes mailing list