Skip to content

Instantly share code, notes, and snippets.

@nanki
Created January 2, 2010 22:37
Show Gist options
  • Save nanki/267706 to your computer and use it in GitHub Desktop.
Save nanki/267706 to your computer and use it in GitHub Desktop.
diff --git a/ext/llvm_basicblock.cpp b/ext/llvm_basicblock.cpp
index e9937b1..83b5bc4 100644
--- a/ext/llvm_basicblock.cpp
+++ b/ext/llvm_basicblock.cpp
@@ -133,7 +133,7 @@ llvm_builder_malloc(VALUE self, VALUE rtype, VALUE rsize) {
const Type *type;
Data_Get_Struct(rtype, Type, type);
- Value *size = ConstantInt::get(Type::Int32Ty, FIX2INT(rsize));
+ Value *size = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), FIX2INT(rsize));
Instruction *v = builder->CreateMalloc(type, size);
return llvm_instruction_wrap(v);
}
@@ -153,7 +153,7 @@ llvm_builder_alloca(VALUE self, VALUE rtype, VALUE rsize) {
const Type* type;
Data_Get_Struct(rtype, Type, type);
- Value *size = ConstantInt::get(Type::Int32Ty, FIX2INT(rsize));
+ Value *size = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), FIX2INT(rsize));
Instruction *v = builder->CreateAlloca(type, size);
return Data_Wrap_Struct(cLLVMAllocationInst, NULL, NULL, v);
}
@@ -286,7 +286,7 @@ llvm_builder_extract_element(VALUE self, VALUE rv, VALUE ridx) {
VALUE
llvm_builder_get_global(VALUE self) {
- GlobalVariable *g = new GlobalVariable(Type::Int64Ty, false, GlobalValue::ExternalLinkage, 0, "shakalaka");
+ GlobalVariable *g = new GlobalVariable(getGlobalContext(), Type::getInt64Ty(getGlobalContext()), false, GlobalValue::ExternalLinkage, 0, "shakalaka");
return llvm_value_wrap(g);
}
diff --git a/ext/llvm_function.cpp b/ext/llvm_function.cpp
index acd6fa1..b8ab482 100644
--- a/ext/llvm_function.cpp
+++ b/ext/llvm_function.cpp
@@ -9,7 +9,7 @@ llvm_function_wrap(Function *f) {
VALUE
llvm_function_create_block(VALUE self) {
- BasicBlock *bb = BasicBlock::Create("bb", LLVM_FUNCTION(self));
+ BasicBlock *bb = BasicBlock::Create(getGlobalContext(), "bb", LLVM_FUNCTION(self));
return llvm_basic_block_wrap(bb);
}
diff --git a/ext/llvm_module.cpp b/ext/llvm_module.cpp
index a300f1e..56fe95b 100644
--- a/ext/llvm_module.cpp
+++ b/ext/llvm_module.cpp
@@ -3,7 +3,8 @@
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Support/MemoryBuffer.h"
-#include <fstream>
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
#include <sstream>
extern "C" {
@@ -16,7 +17,7 @@ llvm_module_allocate(VALUE klass) {
VALUE
llvm_module_initialize(VALUE self, VALUE rname) {
Check_Type(rname, T_STRING);
- DATA_PTR(self) = new Module(StringValuePtr(rname));
+ DATA_PTR(self) = new Module(StringValuePtr(rname), getGlobalContext());
return self;
}
@@ -44,7 +45,7 @@ llvm_module_global_variable(VALUE self, VALUE rtype, VALUE rinitializer) {
Module *m = LLVM_MODULE(self);
Type *type = LLVM_TYPE(rtype);
Constant *initializer = (Constant*)DATA_PTR(rinitializer);
- GlobalVariable *gv = new GlobalVariable(type, true, GlobalValue::InternalLinkage, initializer, "", m);
+ GlobalVariable *gv = new GlobalVariable(getGlobalContext(), type, true, GlobalValue::InternalLinkage, initializer, "", m);
return llvm_value_wrap(gv);
}
@@ -98,7 +99,7 @@ llvm_execution_engine_get(VALUE klass, VALUE module) {
ExistingModuleProvider *MP = new ExistingModuleProvider(m);
if(EE == NULL) {
- EE = ExecutionEngine::create(MP, false);
+ EE = ExecutionEngine::create(MP, true);
} else {
EE->addModuleProvider(MP);
}
@@ -125,11 +126,12 @@ VALUE
llvm_module_read_assembly(VALUE self, VALUE assembly) {
Check_Type(assembly, T_STRING);
- ParseError e;
+ SMDiagnostic e;
Module *module = ParseAssemblyString(
StringValuePtr(assembly),
- LLVM_MODULE(self),
- &e
+ NULL,
+ e,
+ getGlobalContext()
);
//TODO How do we handle errors?
return Data_Wrap_Struct(cLLVMModule, NULL, NULL, module);
@@ -140,7 +142,7 @@ llvm_module_read_bitcode(VALUE self, VALUE bitcode) {
Check_Type(bitcode, T_STRING);
MemoryBuffer *buf = MemoryBuffer::getMemBufferCopy(RSTRING(bitcode)->ptr,RSTRING(bitcode)->ptr+RSTRING(bitcode)->len);
- Module *module = ParseBitcodeFile(buf);
+ Module *module = ParseBitcodeFile(buf, getGlobalContext());
delete buf;
return Data_Wrap_Struct(cLLVMModule, NULL, NULL, module);
}
@@ -152,9 +154,11 @@ llvm_module_write_bitcode(VALUE self, VALUE file_name) {
// Don't really know how to handle c++ streams well,
// dumping all into string buffer and then saving
- std::ofstream file;
- file.open(StringValuePtr(file_name));
+ std::string err;
+ raw_fd_ostream file(StringValuePtr(file_name), true, true, err);
+
WriteBitcodeToFile(LLVM_MODULE(self), file); // Convert value into a string.
+ file.close();
return Qtrue;
}
@@ -182,6 +186,9 @@ llvm_execution_engine_run_function(int argc, VALUE *argv, VALUE klass) {
/* For tests: assume no args, return uncoverted int and turn it into fixnum */
VALUE llvm_execution_engine_run_autoconvert(VALUE klass, VALUE func) {
std::vector<GenericValue> args;
+
+ CHECK_TYPE(func, cLLVMFunction);
+
GenericValue v = EE->runFunction(LLVM_FUNCTION(func), args);
VALUE val = INT2NUM(v.IntVal.getZExtValue());
return val;
diff --git a/ext/llvm_value.cpp b/ext/llvm_value.cpp
index b5f7a0d..c144353 100644
--- a/ext/llvm_value.cpp
+++ b/ext/llvm_value.cpp
@@ -12,9 +12,8 @@ llvm_value_name(VALUE self) {
Data_Get_Struct(self, Value, v);
if(v->hasName()) {
- const char *name = v->getNameStart();
- int len = v->getNameLen();
- return rb_str_new(name, len);
+ StringRef name = v->getName();
+ return rb_str_new(name.data(), name.size());
} else {
return Qnil;
}
@@ -24,7 +23,8 @@ VALUE
llvm_value_set_name(VALUE self, VALUE rname) {
Value *v;
Data_Get_Struct(self, Value, v);
- v->setName(RSTRING_PTR(rname), RSTRING_LEN(rname));
+ StringRef name(RSTRING_PTR(rname), RSTRING_LEN(rname));
+ v->setName(name);
return rname;
}
@@ -70,12 +70,12 @@ llvm_value_get_constant(VALUE self, VALUE type, VALUE v) {
VALUE
llvm_value_get_float_constant(VALUE self, VALUE v) {
- return llvm_value_wrap(ConstantFP::get(Type::FloatTy, RFLOAT(v)->value));
+ return llvm_value_wrap(ConstantFP::get(Type::getFloatTy(getGlobalContext()), RFLOAT(v)->value));
}
VALUE
llvm_value_get_double_constant(VALUE self, VALUE v) {
- return llvm_value_wrap(ConstantFP::get(Type::DoubleTy, RFLOAT(v)->value));
+ return llvm_value_wrap(ConstantFP::get(Type::getDoubleTy(getGlobalContext()), RFLOAT(v)->value));
}
VALUE
@@ -94,9 +94,9 @@ VALUE
llvm_value_get_immediate_constant(VALUE self, VALUE v) {
const IntegerType* type;
if(sizeof(VALUE) == 4) {
- type = Type::Int32Ty;
+ type = Type::getInt32Ty(getGlobalContext());
} else {
- type = Type::Int64Ty;
+ type = Type::getInt64Ty(getGlobalContext());
}
return llvm_value_wrap(ConstantInt::get(type, (long)v));
}
@@ -181,7 +181,7 @@ llvm_type_struct(VALUE self, VALUE rtypes, VALUE rpacked) {
Data_Get_Struct(v, Type, t);
types.push_back(t);
}
- StructType *s = StructType::get(types);
+ StructType *s = StructType::get(getGlobalContext(), types);
return Data_Wrap_Struct(cLLVMStructType, NULL, NULL, s);
}
@@ -232,22 +232,22 @@ llvm_type_type_id(VALUE self) {
}
void init_types() {
- rb_define_const(cLLVMType, "Int1Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int1Ty)));
- rb_define_const(cLLVMType, "Int8Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int8Ty)));
- rb_define_const(cLLVMType, "Int16Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int16Ty)));
- rb_define_const(cLLVMType, "Int32Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int32Ty)));
- rb_define_const(cLLVMType, "Int64Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int64Ty)));
- rb_define_const(cLLVMType, "VoidTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::VoidTy)));
- rb_define_const(cLLVMType, "LabelTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::LabelTy)));
- rb_define_const(cLLVMType, "FloatTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::FloatTy)));
- rb_define_const(cLLVMType, "DoubleTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::DoubleTy)));
+ rb_define_const(cLLVMType, "Int1Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt1Ty(getGlobalContext()))));
+ rb_define_const(cLLVMType, "Int8Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt8Ty(getGlobalContext()))));
+ rb_define_const(cLLVMType, "Int16Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt16Ty(getGlobalContext()))));
+ rb_define_const(cLLVMType, "Int32Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt32Ty(getGlobalContext()))));
+ rb_define_const(cLLVMType, "Int64Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt64Ty(getGlobalContext()))));
+ rb_define_const(cLLVMType, "VoidTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getVoidTy(getGlobalContext()))));
+ rb_define_const(cLLVMType, "LabelTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getLabelTy(getGlobalContext()))));
+ rb_define_const(cLLVMType, "FloatTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getFloatTy(getGlobalContext()))));
+ rb_define_const(cLLVMType, "DoubleTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getDoubleTy(getGlobalContext()))));
// Figure out details of the target machine
const IntegerType *machine_word_type;
if(sizeof(void*) == 4) {
- machine_word_type = Type::Int32Ty;
+ machine_word_type = Type::getInt32Ty(getGlobalContext());
} else {
- machine_word_type = Type::Int64Ty;
+ machine_word_type = Type::getInt64Ty(getGlobalContext());
}
rb_define_const(cLLVMRuby, "MACHINE_WORD", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(machine_word_type)));
}
diff --git a/lib/llvm.rb b/lib/llvm.rb
index 7b95053..c00330c 100644
--- a/lib/llvm.rb
+++ b/lib/llvm.rb
@@ -30,7 +30,7 @@ end
module LLVM
# enum llvm::Type::TypeID
- VoidTyID, FloatTyID, DoubleTyID, X86_FP80TyID, FP128TyID, PPC_FP128TyID, LabelTyID, IntegerTyID,
+ VoidTyID, FloatTyID, DoubleTyID, X86_FP80TyID, FP128TyID, PPC_FP128TyID, LabelTyID, MetadataTyID, IntegerTyID,
FunctionTyID, StructTyID, ArrayTyID, PointerTyID, OpaqueTyID, VectorTyID = (0..13).to_a
class Builder
diff --git a/test/test_basic.rb b/test/test_basic.rb
index 84ed793..a068fa8 100644
--- a/test/test_basic.rb
+++ b/test/test_basic.rb
@@ -317,7 +317,7 @@ class BasicTests < Test::Unit::TestCase
function_tester(666) do |f|
b = f.create_block.builder
vt = Type.vector(MACHINE_WORD, 3)
- vp = b.alloca(vt, 0)
+ vp = b.alloca(vt, 1)
v = b.load(vp)
v2 = b.insert_element(v, 666.llvm(MACHINE_WORD), 0.llvm(Type::Int32Ty))
r = b.extract_element(v2, 0.llvm(Type::Int32Ty))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment