diff --git a/README.rdoc b/README.rdoc index a824f7c66..b00d95896 100644 --- a/README.rdoc +++ b/README.rdoc @@ -21,11 +21,12 @@ http://macruby.org * 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 diff --git a/bin/rubyc b/bin/rubyc index 5f83930ce..d80783034 100644 --- a/bin/rubyc +++ b/bin/rubyc @@ -101,7 +101,7 @@ class Compiler # 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}") diff --git a/bridgesupport.cpp b/bridgesupport.cpp index e5333d099..3bc307e4b 100644 --- a/bridgesupport.cpp +++ b/bridgesupport.cpp @@ -92,7 +92,7 @@ RoxorCompiler::compile_check_arity(Value *given, Value *requested) // void rb_vm_check_arity(int given, int requested); checkArityFunc = cast(module->getOrInsertFunction( "rb_vm_check_arity", - Type::VoidTy, Type::Int32Ty, Type::Int32Ty, NULL)); + VoidTy, Int32Ty, Int32Ty, NULL)); } std::vector params; @@ -118,12 +118,12 @@ RoxorCompiler::compile_set_struct(Value *rcv, int field, Value *val) // void rb_vm_set_struct(VALUE rcv, int field, VALUE val); setStructFunc = cast(module->getOrInsertFunction( "rb_vm_set_struct", - Type::VoidTy, RubyObjTy, Type::Int32Ty, RubyObjTy, NULL)); + VoidTy, RubyObjTy, Int32Ty, RubyObjTy, NULL)); } std::vector 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 @@ RoxorCompiler::compile_bs_struct_writer(rb_vm_bs_boxed_t *bs_boxed, int field) 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 @@ RoxorCompiler::compile_bs_struct_writer(rb_vm_bs_boxed_t *bs_boxed, int field) compile_set_struct(self, field, val); - ReturnInst::Create(val, bb); + ReturnInst::Create(context, val, bb); return f; } @@ -162,7 +162,7 @@ RoxorCompiler::compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed) { // VALUE foo(VALUE self, SEL sel, int argc, VALUE *argv); Function *f = cast(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 @@ RoxorCompiler::compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed) 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 @@ RoxorCompiler::compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed) std::vector 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 @@ RoxorCompiler::compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed) 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 @@ RoxorCompiler::compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed) 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 @@ RoxorCompiler::compile_bs_struct_new(rb_vm_bs_boxed_t *bs_boxed) 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 @@ RoxorCompiler::compile_ffi_function(void *stub, void *imp, int argc) FunctionType *ft = FunctionType::get(RubyObjTy, f_types, false); Function *f = cast(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 @@ RoxorCompiler::compile_ffi_function(void *stub, void *imp, int argc) 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 @@ RoxorCompiler::compile_ffi_function(void *stub, void *imp, int argc) "", 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 @@ RoxorCompiler::compile_ffi_function(void *stub, void *imp, int argc) // 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; } diff --git a/compiler.cpp b/compiler.cpp index 3b9599e0c..352999ff6 100644 --- a/compiler.cpp +++ b/compiler.cpp @@ -24,9 +24,6 @@ extern "C" const char *ruby_node_name(int node); llvm::Module *RoxorCompiler::module = NULL; RoxorCompiler *RoxorCompiler::shared = NULL; -AnnotationID RoxorFunctionAnnotation::id = - AnnotationManager::getID("RoxorFunction"); - RoxorCompiler::RoxorCompiler(void) { fname = ""; @@ -60,6 +57,7 @@ RoxorCompiler::RoxorCompiler(void) return_from_block = -1; return_from_block_ids = 0; ensure_pn = NULL; + current_scope = NULL; dispatcherFunc = NULL; fastPlusFunc = NULL; @@ -133,12 +131,19 @@ RoxorCompiler::RoxorCompiler(void) 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 @@ RoxorCompiler::RoxorCompiler(void) 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 @@ RoxorCompiler::RoxorCompiler(void) 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 @@ inline ICmpInst * 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 @@ RoxorCompiler::compile_protected_call(Function *func, 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 @@ RoxorCompiler::compile_single_when_argument(NODE *arg, Value *comparedToVal, Bas 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 @@ RoxorCompiler::compile_single_when_argument(NODE *arg, Value *comparedToVal, Bas } 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 @@ RoxorCompiler::compile_when_arguments(NODE *args, Value *comparedToVal, BasicBlo 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 @@ RoxorCompiler::compile_when_arguments(NODE *args, Value *comparedToVal, BasicBlo } 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 @@ RoxorCompiler::compile_when_splat(Value *comparedToVal, Value *splatVal) // VALUE comparedTo, VALUE splat) whenSplatFunc = cast (module->getOrInsertFunction("rb_vm_when_splat", - RubyObjTy, PtrTy, Type::Int1Ty, + RubyObjTy, PtrTy, Int1Ty, RubyObjTy, RubyObjTy, NULL)); } @@ -478,20 +491,16 @@ RoxorCompiler::compile_const_global_ustring(const UniChar *str, 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 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 @@ RoxorCompiler::compile_const_global_string(const char *str, 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 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 @@ RoxorCompiler::compile_get_mcache(Value *sel, bool super) // void *rb_vm_get_call_cache2(SEL sel, unsigned char super); getCacheFunc = cast(module->getOrInsertFunction( - "rb_vm_get_call_cache2", PtrTy, PtrTy, Type::Int8Ty, + "rb_vm_get_call_cache2", PtrTy, PtrTy, Int8Ty, NULL)); } std::vector 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 @@ RoxorAOTCompiler::compile_mcache(SEL sel, bool super) GlobalVariable *gvar; std::map::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 @@ RoxorAOTCompiler::compile_ccache(ID name) 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 @@ RoxorAOTCompiler::compile_sel(SEL sel, bool add_to_bb) std::map::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 @@ RoxorCompiler::compile_arity(rb_vm_arity_t &arity) 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 @@ RoxorCompiler::compile_prepare_method(Value *classVal, Value *sel, prepareMethodFunc = cast(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 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 @@ RoxorAOTCompiler::compile_prepare_method(Value *classVal, Value *sel, prepareMethodFunc = cast(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 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 @@ RoxorAOTCompiler::compile_prepare_method(Value *classVal, Value *sel, 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 @@ RoxorCompiler::compile_dispatch_call(std::vector ¶ms) 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 (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 @@ RoxorCompiler::compile_attribute_assign(NODE *node, Value *extra_val) 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::iterator i = args.begin(); i != args.end(); ++i) { @@ -852,19 +845,19 @@ RoxorCompiler::compile_multiple_assignment(NODE *node, Value *val) // VALUE rb_vm_masgn_get_elem_before_splat(VALUE ary, int offset); masgnGetElemBeforeSplatFunc = cast(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(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(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 @@ RoxorCompiler::compile_multiple_assignment(NODE *node, Value *val) for (int i = 0; l != NULL; ++i) { std::vector 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 @@ RoxorCompiler::compile_multiple_assignment(NODE *node, Value *val) if (splat != NULL && splat != (NODE *)-1) { std::vector 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 @@ RoxorCompiler::compile_multiple_assignment(NODE *node, Value *val) for (int i = 0; l != NULL; ++i) { std::vector 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 @@ RoxorCompiler::compile_block_create(NODE *node) // int dvars_size, ...); std::vector 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 (module->getOrInsertFunction("rb_vm_prepare_block", ft)); @@ -999,7 +992,7 @@ RoxorCompiler::compile_block_create(NODE *node) && 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 @@ RoxorCompiler::compile_block_create(NODE *node) ? 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::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::iterator iter = lvars.begin(); iter != lvars.end(); ++iter) { ID name = iter->first; @@ -1040,8 +1033,8 @@ RoxorCompiler::compile_binding(void) 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 (module->getOrInsertFunction("rb_vm_push_binding", ft)); } @@ -1059,7 +1052,7 @@ RoxorCompiler::compile_binding(void) } // Lvars. - params.push_back(ConstantInt::get(Type::Int32Ty, (int)lvars.size())); + params.push_back(ConstantInt::get(Int32Ty, (int)lvars.size())); for (std::map::iterator iter = lvars.begin(); iter != lvars.end(); ++iter) { @@ -1083,16 +1076,10 @@ RoxorCompiler::gen_slot_cache(ID id) 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 @@ RoxorCompiler::compile_slot_cache(ID id) 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 @@ RoxorCompiler::compile_ivar_assignment(ID vid, Value *val) // void rb_vm_ivar_set(VALUE obj, ID name, VALUE val, int *slot_cache); setIvarFunc = cast(module->getOrInsertFunction("rb_vm_ivar_set", - Type::VoidTy, RubyObjTy, IntTy, RubyObjTy, Int32PtrTy, + VoidTy, RubyObjTy, IntTy, RubyObjTy, Int32PtrTy, NULL)); } @@ -1172,16 +1159,15 @@ RoxorCompiler::compile_cvar_get(ID id, bool check) // unsigned char dynamic_class); cvarGetFunc = cast(module->getOrInsertFunction( "rb_vm_cvar_get", - RubyObjTy, RubyObjTy, IntTy, Type::Int8Ty, Type::Int8Ty, - NULL)); + RubyObjTy, RubyObjTy, IntTy, Int8Ty, Int8Ty, NULL)); } std::vector 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 @@ RoxorCompiler::compile_cvar_assignment(ID name, Value *val) // unsigned char dynamic_class); cvarSetFunc = cast(module->getOrInsertFunction( "rb_vm_cvar_set", - RubyObjTy, RubyObjTy, IntTy, RubyObjTy, Type::Int8Ty, NULL)); + RubyObjTy, RubyObjTy, IntTy, RubyObjTy, Int8Ty, NULL)); } std::vector params; @@ -1202,7 +1188,7 @@ RoxorCompiler::compile_cvar_assignment(ID name, Value *val) 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 @@ RoxorCompiler::compile_constant_declaration(NODE *node, Value *val) // unsigned char dynamic_class); setConstFunc = cast(module->getOrInsertFunction( "rb_vm_set_const", - Type::VoidTy, RubyObjTy, IntTy, RubyObjTy, Type::Int8Ty, + VoidTy, RubyObjTy, IntTy, RubyObjTy, Int8Ty, NULL)); } @@ -1253,7 +1239,7 @@ RoxorCompiler::compile_constant_declaration(NODE *node, Value *val) 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 @@ inline Value * 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 @@ inline Value * 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 @@ RoxorAOTCompiler::compile_id(ID id) 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 @@ RoxorCompiler::compile_const(ID id, Value *outer) // struct ccache *cache, ID id, unsigned char dynamic_class); getConstFunc = cast(module->getOrInsertFunction( "rb_vm_get_const", - RubyObjTy, RubyObjTy, Type::Int8Ty, PtrTy, IntTy, Type::Int8Ty, + RubyObjTy, RubyObjTy, Int8Ty, PtrTy, IntTy, Int8Ty, NULL)); } std::vector 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 @@ RoxorCompiler::compile_defined_expression(NODE *node) // 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 @@ RoxorCompiler::compile_defined_expression(NODE *node) // VALUE rb_vm_defined(VALUE self, int type, VALUE what, VALUE what2); definedFunc = cast(module->getOrInsertFunction( "rb_vm_defined", - RubyObjTy, RubyObjTy, Type::Int32Ty, RubyObjTy, RubyObjTy, + RubyObjTy, RubyObjTy, Int32Ty, RubyObjTy, RubyObjTy, NULL)); } std::vector 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 @@ RoxorCompiler::compile_dstr(NODE *node) 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 types; - types.push_back(Type::Int32Ty); + types.push_back(Int32Ty); FunctionType *ft = FunctionType::get(RubyObjTy, types, true); newStringFunc = cast(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 @@ RoxorCompiler::compile_dvar_slot(ID name) ++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 @@ RoxorCompiler::compile_break_val(Value *val) // void rb_vm_break(VALUE val); breakFunc = cast( module->getOrInsertFunction("rb_vm_break", - Type::VoidTy, RubyObjTy, NULL)); + VoidTy, RubyObjTy, NULL)); } std::vector params; params.push_back(val); @@ -1637,11 +1611,11 @@ RoxorCompiler::compile_return_from_block(Value *val, int id) // void rb_vm_return_from_block(VALUE val, int id); returnFromBlockFunc = cast( module->getOrInsertFunction("rb_vm_return_from_block", - Type::VoidTy, RubyObjTy, Type::Int32Ty, NULL)); + VoidTy, RubyObjTy, Int32Ty, NULL)); } std::vector 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 @@ RoxorCompiler::compile_return_from_block_handler(int id) checkReturnFromBlockFunc = cast( module->getOrInsertFunction( "rb_vm_check_return_from_block_exc", - RubyObjTy, PtrTy, Type::Int32Ty, NULL)); + RubyObjTy, PtrTy, Int32Ty, NULL)); } std::vector 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 @@ RoxorCompiler::compile_jump(NODE *node) } 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 @@ RoxorCompiler::compile_jump(NODE *node) 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 @@ RoxorCompiler::compile_jump(NODE *node) 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 @@ RoxorCompiler::compile_jump(NODE *node) // 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 @@ RoxorCompiler::compile_landing_pad_header(const std::type_info &eh_type) 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 @@ RoxorCompiler::compile_pop_exception(void) // void rb_vm_pop_exception(void); popExceptionFunc = cast( module->getOrInsertFunction("rb_vm_pop_exception", - Type::VoidTy, NULL)); + VoidTy, NULL)); } CallInst::Create(popExceptionFunc, "", bb); } @@ -1917,7 +1892,7 @@ RoxorCompiler::compile_landing_pad_footer(bool pop_exception) // void __cxa_end_catch(void); endCatchFunc = cast( module->getOrInsertFunction("__cxa_end_catch", - Type::VoidTy, NULL)); + VoidTy, NULL)); } CallInst::Create(endCatchFunc, "", bb); } @@ -1930,11 +1905,10 @@ RoxorCompiler::compile_rethrow_exception(void) if (rethrowFunc == NULL) { // void __cxa_rethrow(void); rethrowFunc = cast( - 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 @@ RoxorCompiler::optimized_immediate_op(SEL sel, Value *leftVal, Value *rightVal, } 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 @@ Value * 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, // 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, } 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, } 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, // 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, } 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, if (sel == selLTLT) { opt_func = cast(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(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(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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, 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 @@ RoxorCompiler::compile_optimized_dispatch_call(SEL sel, int argc, 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 @@ RoxorCompiler::compile_literal(VALUE val) str_len, CFHash((CFTypeRef)val)); std::vector 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 @@ RoxorCompiler::compile_literal(VALUE val) newString2Func = cast( module->getOrInsertFunction( "rb_unicode_str_new", - RubyObjTy, PointerType::getUnqual(Type::Int16Ty), - Type::Int32Ty, NULL)); + RubyObjTy, PointerType::getUnqual(Int16Ty), + Int32Ty, NULL)); } std::vector 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 @@ RoxorAOTCompiler::compile_immutable_literal(VALUE val) 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 @@ RoxorAOTCompiler::compile_global_entry(NODE *node) std::map::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 @@ RoxorCompiler::compile_set_current_scope(Value *klass, Value *scope) // void rb_vm_set_current_scope(VALUE mod, int scope) setScopeFunc = cast( module->getOrInsertFunction("rb_vm_set_current_scope", - Type::VoidTy, RubyObjTy, Type::Int32Ty, NULL)); + VoidTy, RubyObjTy, Int32Ty, NULL)); } std::vector params; @@ -2886,7 +2850,7 @@ RoxorCompiler::compile_ivar_slots(Value *klass, prepareIvarSlotFunc = cast( module->getOrInsertFunction( "rb_vm_prepare_class_ivar_slot", - Type::VoidTy, RubyObjTy, IntTy, Int32PtrTy, NULL)); + VoidTy, RubyObjTy, IntTy, Int32PtrTy, NULL)); } for (std::map::iterator iter = ivar_slots_cache.begin(); @@ -2907,7 +2871,7 @@ RoxorCompiler::compile_ivar_slots(Value *klass, } 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 @@ RoxorCompiler::compile_keep_vars(BasicBlock *startBB, BasicBlock *mergeBB) // void rb_vm_keep_vars(rb_vm_var_uses *uses, int lvars_size, ...) std::vector 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 (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 @@ RoxorCompiler::compile_keep_vars(BasicBlock *startBB, BasicBlock *mergeBB) 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 @@ RoxorCompiler::compile_node(NODE *node) } 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 @@ RoxorCompiler::compile_node(NODE *node) 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 old_lvars = lvars; lvars.clear(); @@ -3107,10 +3074,13 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) } 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 @@ RoxorCompiler::compile_node(NODE *node) 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(f); } @@ -3380,19 +3351,21 @@ RoxorCompiler::compile_node(NODE *node) } - 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 arguments; @@ -3485,7 +3458,7 @@ RoxorCompiler::compile_node(NODE *node) arguments, &argc); } - params.push_back(ConstantInt::get(Type::Int32Ty, argc)); + params.push_back(ConstantInt::get(Int32Ty, argc)); for (std::vector::iterator i = arguments.begin(); i != arguments.end(); ++i) { params.push_back(*i); @@ -3509,9 +3482,9 @@ RoxorCompiler::compile_node(NODE *node) // 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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::iterator i = arguments.begin(); i != arguments.end(); ++i) { params.push_back(*i); @@ -3609,9 +3582,8 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) if (newRegexpFunc == NULL) { newRegexpFunc = cast(module->getOrInsertFunction( "rb_reg_new_str", - RubyObjTy, RubyObjTy, Type::Int32Ty, NULL)); + RubyObjTy, RubyObjTy, Int32Ty, NULL)); } std::vector 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) module->getOrInsertFunction( "rb_vm_define_class", RubyObjTy, IntTy, RubyObjTy, RubyObjTy, - Type::Int32Ty, Type::Int8Ty, NULL)); + Int32Ty, Int8Ty, NULL)); } std::vector params; @@ -3840,8 +3835,8 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) ? 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) else if (real_args != NULL) { std::vector 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::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 @@ RoxorCompiler::compile_node(NODE *node) if (newArrayFunc == NULL) { // VALUE rb_ary_new_fast(int argc, ...); std::vector types; - types.push_back(Type::Int32Ty); + types.push_back(Int32Ty); FunctionType *ft = FunctionType::get(RubyObjTy, types, true); newArrayFunc = cast(module->getOrInsertFunction("rb_ary_new_fast", ft)); } @@ -4264,13 +4255,13 @@ RoxorCompiler::compile_node(NODE *node) std::vector 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 @@ RoxorCompiler::compile_node(NODE *node) if (newHashFunc == NULL) { // VALUE rb_hash_new_fast(int argc, ...); std::vector types; - types.push_back(Type::Int32Ty); + types.push_back(Int32Ty); FunctionType *ft = FunctionType::get(RubyObjTy, types, true); newHashFunc = cast(module->getOrInsertFunction("rb_hash_new_fast", ft)); } @@ -4301,7 +4292,7 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) } } else { - params.push_back(ConstantInt::get(Type::Int32Ty, 0)); + params.push_back(ConstantInt::get(Int32Ty, 0)); } return cast(CallInst::Create(newHashFunc, @@ -4380,8 +4371,8 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) if (valiasFunc == NULL) { // void rb_alias_variable(ID from, ID to); valiasFunc = cast(module->getOrInsertFunction("rb_alias_variable", - Type::VoidTy, IntTy, IntTy, NULL)); + VoidTy, IntTy, IntTy, NULL)); } std::vector params; @@ -4421,7 +4412,7 @@ RoxorCompiler::compile_node(NODE *node) // unsigned char dynamic_class); aliasFunc = cast(module->getOrInsertFunction( "rb_vm_alias2", - Type::VoidTy, RubyObjTy, IntTy, IntTy, Type::Int8Ty, + VoidTy, RubyObjTy, IntTy, IntTy, Int8Ty, NULL)); } @@ -4430,7 +4421,7 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) undefFunc = cast(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 @@ RoxorCompiler::compile_node(NODE *node) std::vector 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 @@ RoxorCompiler::compile_node(NODE *node) // VALUE rb_vm_get_special(char code); getSpecialFunc = cast(module->getOrInsertFunction("rb_vm_get_special", - RubyObjTy, Type::Int8Ty, NULL)); + RubyObjTy, Int8Ty, NULL)); } std::vector 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 > handlers; @@ -4666,8 +4658,8 @@ RoxorCompiler::compile_node(NODE *node) if (isEHActiveFunc == NULL) { // bool rb_vm_is_eh_active(int argc, ...); std::vector 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( module->getOrInsertFunction( @@ -4676,19 +4668,19 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) // 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) if (yieldFunc == NULL) { // VALUE rb_vm_yield_args(int argc, ...) std::vector types; - types.push_back(Type::Int32Ty); + types.push_back(Int32Ty); FunctionType *ft = FunctionType::get(RubyObjTy, types, true); yieldFunc = cast(module->getOrInsertFunction( @@ -4994,7 +4993,7 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) } 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorCompiler::compile_node(NODE *node) 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) compile_const_global_string(rb_id2name(name)); std::vector 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) compile_const_global_string(sel_getName(sel)); std::vector 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) Function *newRegexp2Func = cast(module->getOrInsertFunction("rb_reg_new_retained", - RubyObjTy, PtrTy, Type::Int32Ty, Type::Int32Ty, NULL)); + RubyObjTy, PtrTy, Int32Ty, Int32Ty, NULL)); Function *getClassFunc = cast(module->getOrInsertFunction("objc_getClass", @@ -5292,8 +5291,8 @@ RoxorAOTCompiler::compile_main_function(NODE *node) compile_const_global_string(class_getName((Class)val)); std::vector 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) compile_const_global_string(re->str, re->len); std::vector 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 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) compile_const_global_string(symname); std::vector 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) compile_const_global_string(rb_id2name(name)); std::vector 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) GlobalVariable *gvar = *i; GlobalVariable *str = compile_const_global_string( - gvar->getName().c_str()); + gvar->getName().str().c_str()); std::vector 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) GlobalVariable *toplevel = compile_const_global_string("TopLevel"); std::vector 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 @@ RoxorAOTCompiler::compile_main_function(NODE *node) 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 @@ RoxorCompiler::compile_read_attr(ID name) 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 @@ RoxorCompiler::compile_write_attr(ID name) 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 @@ RoxorCompiler::compile_get_struct_fields(Value *val, Value *buf, if (getStructFieldsFunc == NULL) { getStructFieldsFunc = cast(module->getOrInsertFunction( "rb_vm_get_struct_fields", - Type::VoidTy, RubyObjTy, RubyObjPtrTy, PtrTy, NULL)); + VoidTy, RubyObjTy, RubyObjPtrTy, PtrTy, NULL)); } std::vector params; @@ -6046,7 +6045,7 @@ RoxorCompiler::compile_conversion_to_c(const char *type, Value *val, 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 @@ RoxorCompiler::compile_conversion_to_c(const char *type, Value *val, // 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 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 @@ RoxorCompiler::compile_conversion_to_c(const char *type, Value *val, params.push_back(slot); Function *func = cast(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 @@ RoxorCompiler::compile_new_struct(Value *klass, std::vector &fields) if (newStructFunc == NULL) { std::vector types; types.push_back(RubyObjTy); - types.push_back(Type::Int32Ty); + types.push_back(Int32Ty); FunctionType *ft = FunctionType::get(RubyObjTy, types, true); newStructFunc = cast(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 @@ RoxorCompiler::compile_new_pointer(const char *type, Value *val) GlobalVariable *gvar = compile_const_global_string(type); std::vector 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 @@ RoxorCompiler::compile_conversion_to_ruby(const char *type, 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 @@ RoxorCompiler::compile_conversion_to_ruby(const char *type, 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 @@ RoxorCompiler::convert_type(const char *type) switch (*type) { case _C_VOID: - return Type::VoidTy; + return VoidTy; case _C_ID: case _C_CLASS: @@ -6402,33 +6401,33 @@ RoxorCompiler::convert_type(const char *type) 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 @@ RoxorCompiler::convert_type(const char *type) 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 @@ RoxorCompiler::compile_stub(const char *types, int argc, bool is_objc) // } f = cast(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 @@ RoxorCompiler::compile_stub(const char *types, int argc, bool is_objc) // } f = cast(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 @@ RoxorCompiler::compile_stub(const char *types, int argc, bool is_objc) 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 @@ RoxorCompiler::compile_stub(const char *types, int argc, bool is_objc) 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 @@ RoxorCompiler::compile_stub(const char *types, int argc, bool is_objc) } 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 @@ RoxorCompiler::compile_objc_stub(Function *ruby_func, IMP ruby_imp, // 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 @@ RoxorCompiler::compile_objc_stub(Function *ruby_func, IMP ruby_imp, Function *f = cast(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 @@ RoxorCompiler::compile_objc_stub(Function *ruby_func, IMP ruby_imp, "", 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 @@ RoxorCompiler::compile_block_caller(rb_vm_block_t *block) // return rb_vm_block_eval2(block, rcv, sel, argc, argv); // } Function *f = cast(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 @@ RoxorCompiler::compile_block_caller(rb_vm_block_t *block) 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(module->getOrInsertFunction( "rb_vm_block_eval2", - RubyObjTy, PtrTy, RubyObjTy, PtrTy, Type::Int32Ty, - RubyObjPtrTy, NULL)); + RubyObjTy, PtrTy, RubyObjTy, PtrTy, Int32Ty, RubyObjPtrTy, + NULL)); } std::vector params; params.push_back(compile_const_pointer(block)); @@ -6794,7 +6792,7 @@ RoxorCompiler::compile_block_caller(rb_vm_block_t *block) Value *retval = compile_protected_call(blockEvalFunc, params); - ReturnInst::Create(retval, bb); + ReturnInst::Create(context, retval, bb); return f; } @@ -6808,7 +6806,7 @@ RoxorCompiler::compile_to_rval_convertor(const char *type) 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 @@ RoxorCompiler::compile_to_rval_convertor(const char *type) 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 @@ RoxorCompiler::compile_to_ocval_convertor(const char *type) { // void foo(VALUE rval, void **ocval); Function *f = cast(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; } diff --git a/compiler.h b/compiler.h index 7f3a1b888..ca1bdb692 100644 --- a/compiler.h +++ b/compiler.h @@ -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 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 @@ class RoxorCompiler { 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::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 @@ class RoxorCompiler { std::map ivar_slots_cache; std::map static_strings; std::map static_ustrings; + std::map scopes; #if ROXOR_COMPILER_DEBUG int level; @@ -129,7 +130,7 @@ class RoxorCompiler { 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 @@ class RoxorCompiler { 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 @@ class RoxorCompiler { SEL mid_to_sel(ID mid, int arity); }; +#define context (RoxorCompiler::module->getContext()) + class RoxorAOTCompiler : public RoxorCompiler { public: RoxorAOTCompiler(void); diff --git a/include/ruby/config.h.in b/include/ruby/config.h.in index 156513976..bdadff34a 100644 --- a/include/ruby/config.h.in +++ b/include/ruby/config.h.in @@ -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 diff --git a/rakelib/builder.rb b/rakelib/builder.rb index 790f66236..d626964a2 100644 --- a/rakelib/builder.rb +++ b/rakelib/builder.rb @@ -83,7 +83,7 @@ def do_option(name, default) 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++' diff --git a/vm.cpp b/vm.cpp index a2804aefc..2678ff5c4 100644 --- a/vm.cpp +++ b/vm.cpp @@ -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 #include @@ -25,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -59,12 +59,15 @@ VALUE rb_cTopLevel = 0; 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 @@ class RoxorJITManager : public JITMemoryManager { 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 @@ class RoxorJITManager : public JITMemoryManager { 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 @@ class RoxorJITManager : public JITMemoryManager { 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(F), - FunctionStart, FunctionEnd)); + Function *f = const_cast(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 @@ RoxorCore::RoxorCore(void) 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())); - // Eliminate unnecessary alloca. - fpm->add(createPromoteMemoryToRegisterPass()); // Do simple "peephole" optimizations and bit-twiddling optzns. fpm->add(createInstructionCombiningPass()); + // Eliminate unnecessary alloca. + fpm->add(createPromoteMemoryToRegisterPass()); // Reassociate expressions. fpm->add(createReassociatePass()); // Eliminate Common SubExpressions. @@ -221,8 +243,13 @@ RoxorCore::RoxorCore(void) // 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 @@ RoxorCore::compile(Function *func) return imp; } +#if USE_LLVM_INTERPRETER VALUE RoxorCore::interpret(Function *func) { @@ -404,6 +432,7 @@ RoxorCore::interpret(Function *func) 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 @@ RoxorCore::symbolize_call_address(void *addr, void **startp, char *path, 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 @@ RoxorCore::symbolize_call_address(void *addr, void **startp, char *path, 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 @@ RoxorCore::symbolize_call_address(void *addr, void **startp, char *path, } } 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 @@ RoxorCore::redefined_op_gvar(SEL sel, bool create) 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 @@ rb_vm_run(const char *fname, NODE *node, rb_vm_binding_t *binding, 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 @@ rb_vm_aot_compile(NODE *node) 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 @@ resolveInstanceMethod_imp(void *self, SEL sel, SEL name) 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(); diff --git a/vm.h b/vm.h index 3afdcb399..04d3f3518 100644 --- a/vm.h +++ b/vm.h @@ -522,6 +522,10 @@ class RoxorJITManager; 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 @@ class RoxorCore { ExistingModuleProvider *emp; RoxorJITManager *jmm; ExecutionEngine *ee; +#if USE_LLVM_INTERPRETER ExecutionEngine *iee; +#endif FunctionPassManager *fpm; // Running threads. @@ -622,7 +628,9 @@ class RoxorCore { 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);