Guest User

Untitled

a guest
Mar 17th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.78 KB | None | 0 0
  1. diff --git a/ext/llvm_basicblock.cpp b/ext/llvm_basicblock.cpp
  2. index e9937b1..83b5bc4 100644
  3. --- a/ext/llvm_basicblock.cpp
  4. +++ b/ext/llvm_basicblock.cpp
  5. @@ -133,7 +133,7 @@ llvm_builder_malloc(VALUE self, VALUE rtype, VALUE rsize) {
  6. const Type *type;
  7. Data_Get_Struct(rtype, Type, type);
  8.  
  9. - Value *size = ConstantInt::get(Type::Int32Ty, FIX2INT(rsize));
  10. + Value *size = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), FIX2INT(rsize));
  11. Instruction *v = builder->CreateMalloc(type, size);
  12. return llvm_instruction_wrap(v);
  13. }
  14. @@ -153,7 +153,7 @@ llvm_builder_alloca(VALUE self, VALUE rtype, VALUE rsize) {
  15. const Type* type;
  16. Data_Get_Struct(rtype, Type, type);
  17.  
  18. - Value *size = ConstantInt::get(Type::Int32Ty, FIX2INT(rsize));
  19. + Value *size = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), FIX2INT(rsize));
  20. Instruction *v = builder->CreateAlloca(type, size);
  21. return Data_Wrap_Struct(cLLVMAllocationInst, NULL, NULL, v);
  22. }
  23. @@ -286,7 +286,7 @@ llvm_builder_extract_element(VALUE self, VALUE rv, VALUE ridx) {
  24.  
  25. VALUE
  26. llvm_builder_get_global(VALUE self) {
  27. - GlobalVariable *g = new GlobalVariable(Type::Int64Ty, false, GlobalValue::ExternalLinkage, 0, "shakalaka");
  28. + GlobalVariable *g = new GlobalVariable(getGlobalContext(), Type::getInt64Ty(getGlobalContext()), false, GlobalValue::ExternalLinkage, 0, "shakalaka");
  29. return llvm_value_wrap(g);
  30. }
  31.  
  32. diff --git a/ext/llvm_function.cpp b/ext/llvm_function.cpp
  33. index acd6fa1..b8ab482 100644
  34. --- a/ext/llvm_function.cpp
  35. +++ b/ext/llvm_function.cpp
  36. @@ -9,7 +9,7 @@ llvm_function_wrap(Function *f) {
  37.  
  38. VALUE
  39. llvm_function_create_block(VALUE self) {
  40. - BasicBlock *bb = BasicBlock::Create("bb", LLVM_FUNCTION(self));
  41. + BasicBlock *bb = BasicBlock::Create(getGlobalContext(), "bb", LLVM_FUNCTION(self));
  42. return llvm_basic_block_wrap(bb);
  43. }
  44.  
  45. diff --git a/ext/llvm_module.cpp b/ext/llvm_module.cpp
  46. index a300f1e..56fe95b 100644
  47. --- a/ext/llvm_module.cpp
  48. +++ b/ext/llvm_module.cpp
  49. @@ -3,7 +3,8 @@
  50. #include "llvm/Bitcode/ReaderWriter.h"
  51. #include "llvm/Analysis/Verifier.h"
  52. #include "llvm/Support/MemoryBuffer.h"
  53. -#include <fstream>
  54. +#include "llvm/Support/SourceMgr.h"
  55. +#include "llvm/Support/raw_ostream.h"
  56. #include <sstream>
  57.  
  58. extern "C" {
  59. @@ -16,7 +17,7 @@ llvm_module_allocate(VALUE klass) {
  60. VALUE
  61. llvm_module_initialize(VALUE self, VALUE rname) {
  62. Check_Type(rname, T_STRING);
  63. - DATA_PTR(self) = new Module(StringValuePtr(rname));
  64. + DATA_PTR(self) = new Module(StringValuePtr(rname), getGlobalContext());
  65. return self;
  66. }
  67.  
  68. @@ -44,7 +45,7 @@ llvm_module_global_variable(VALUE self, VALUE rtype, VALUE rinitializer) {
  69. Module *m = LLVM_MODULE(self);
  70. Type *type = LLVM_TYPE(rtype);
  71. Constant *initializer = (Constant*)DATA_PTR(rinitializer);
  72. - GlobalVariable *gv = new GlobalVariable(type, true, GlobalValue::InternalLinkage, initializer, "", m);
  73. + GlobalVariable *gv = new GlobalVariable(getGlobalContext(), type, true, GlobalValue::InternalLinkage, initializer, "", m);
  74. return llvm_value_wrap(gv);
  75. }
  76.  
  77. @@ -98,7 +99,7 @@ llvm_execution_engine_get(VALUE klass, VALUE module) {
  78. ExistingModuleProvider *MP = new ExistingModuleProvider(m);
  79.  
  80. if(EE == NULL) {
  81. - EE = ExecutionEngine::create(MP, false);
  82. + EE = ExecutionEngine::create(MP, true);
  83. } else {
  84. EE->addModuleProvider(MP);
  85. }
  86. @@ -125,11 +126,12 @@ VALUE
  87. llvm_module_read_assembly(VALUE self, VALUE assembly) {
  88. Check_Type(assembly, T_STRING);
  89.  
  90. - ParseError e;
  91. + SMDiagnostic e;
  92. Module *module = ParseAssemblyString(
  93. StringValuePtr(assembly),
  94. - LLVM_MODULE(self),
  95. - &e
  96. + NULL,
  97. + e,
  98. + getGlobalContext()
  99. );
  100. //TODO How do we handle errors?
  101. return Data_Wrap_Struct(cLLVMModule, NULL, NULL, module);
  102. @@ -140,7 +142,7 @@ llvm_module_read_bitcode(VALUE self, VALUE bitcode) {
  103. Check_Type(bitcode, T_STRING);
  104.  
  105. MemoryBuffer *buf = MemoryBuffer::getMemBufferCopy(RSTRING(bitcode)->ptr,RSTRING(bitcode)->ptr+RSTRING(bitcode)->len);
  106. - Module *module = ParseBitcodeFile(buf);
  107. + Module *module = ParseBitcodeFile(buf, getGlobalContext());
  108. delete buf;
  109. return Data_Wrap_Struct(cLLVMModule, NULL, NULL, module);
  110. }
  111. @@ -152,9 +154,11 @@ llvm_module_write_bitcode(VALUE self, VALUE file_name) {
  112.  
  113. // Don't really know how to handle c++ streams well,
  114. // dumping all into string buffer and then saving
  115. - std::ofstream file;
  116. - file.open(StringValuePtr(file_name));
  117. + std::string err;
  118. + raw_fd_ostream file(StringValuePtr(file_name), true, true, err);
  119. +
  120. WriteBitcodeToFile(LLVM_MODULE(self), file); // Convert value into a string.
  121. + file.close();
  122. return Qtrue;
  123. }
  124.  
  125. @@ -182,6 +186,9 @@ llvm_execution_engine_run_function(int argc, VALUE *argv, VALUE klass) {
  126. /* For tests: assume no args, return uncoverted int and turn it into fixnum */
  127. VALUE llvm_execution_engine_run_autoconvert(VALUE klass, VALUE func) {
  128. std::vector<GenericValue> args;
  129. +
  130. + CHECK_TYPE(func, cLLVMFunction);
  131. +
  132. GenericValue v = EE->runFunction(LLVM_FUNCTION(func), args);
  133. VALUE val = INT2NUM(v.IntVal.getZExtValue());
  134. return val;
  135. diff --git a/ext/llvm_value.cpp b/ext/llvm_value.cpp
  136. index b5f7a0d..c144353 100644
  137. --- a/ext/llvm_value.cpp
  138. +++ b/ext/llvm_value.cpp
  139. @@ -12,9 +12,8 @@ llvm_value_name(VALUE self) {
  140. Data_Get_Struct(self, Value, v);
  141.  
  142. if(v->hasName()) {
  143. - const char *name = v->getNameStart();
  144. - int len = v->getNameLen();
  145. - return rb_str_new(name, len);
  146. + StringRef name = v->getName();
  147. + return rb_str_new(name.data(), name.size());
  148. } else {
  149. return Qnil;
  150. }
  151. @@ -24,7 +23,8 @@ VALUE
  152. llvm_value_set_name(VALUE self, VALUE rname) {
  153. Value *v;
  154. Data_Get_Struct(self, Value, v);
  155. - v->setName(RSTRING_PTR(rname), RSTRING_LEN(rname));
  156. + StringRef name(RSTRING_PTR(rname), RSTRING_LEN(rname));
  157. + v->setName(name);
  158. return rname;
  159. }
  160.  
  161. @@ -70,12 +70,12 @@ llvm_value_get_constant(VALUE self, VALUE type, VALUE v) {
  162.  
  163. VALUE
  164. llvm_value_get_float_constant(VALUE self, VALUE v) {
  165. - return llvm_value_wrap(ConstantFP::get(Type::FloatTy, RFLOAT(v)->value));
  166. + return llvm_value_wrap(ConstantFP::get(Type::getFloatTy(getGlobalContext()), RFLOAT(v)->value));
  167. }
  168.  
  169. VALUE
  170. llvm_value_get_double_constant(VALUE self, VALUE v) {
  171. - return llvm_value_wrap(ConstantFP::get(Type::DoubleTy, RFLOAT(v)->value));
  172. + return llvm_value_wrap(ConstantFP::get(Type::getDoubleTy(getGlobalContext()), RFLOAT(v)->value));
  173. }
  174.  
  175. VALUE
  176. @@ -94,9 +94,9 @@ VALUE
  177. llvm_value_get_immediate_constant(VALUE self, VALUE v) {
  178. const IntegerType* type;
  179. if(sizeof(VALUE) == 4) {
  180. - type = Type::Int32Ty;
  181. + type = Type::getInt32Ty(getGlobalContext());
  182. } else {
  183. - type = Type::Int64Ty;
  184. + type = Type::getInt64Ty(getGlobalContext());
  185. }
  186. return llvm_value_wrap(ConstantInt::get(type, (long)v));
  187. }
  188. @@ -181,7 +181,7 @@ llvm_type_struct(VALUE self, VALUE rtypes, VALUE rpacked) {
  189. Data_Get_Struct(v, Type, t);
  190. types.push_back(t);
  191. }
  192. - StructType *s = StructType::get(types);
  193. + StructType *s = StructType::get(getGlobalContext(), types);
  194. return Data_Wrap_Struct(cLLVMStructType, NULL, NULL, s);
  195. }
  196.  
  197. @@ -232,22 +232,22 @@ llvm_type_type_id(VALUE self) {
  198. }
  199.  
  200. void init_types() {
  201. - rb_define_const(cLLVMType, "Int1Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int1Ty)));
  202. - rb_define_const(cLLVMType, "Int8Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int8Ty)));
  203. - rb_define_const(cLLVMType, "Int16Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int16Ty)));
  204. - rb_define_const(cLLVMType, "Int32Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int32Ty)));
  205. - rb_define_const(cLLVMType, "Int64Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::Int64Ty)));
  206. - rb_define_const(cLLVMType, "VoidTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::VoidTy)));
  207. - rb_define_const(cLLVMType, "LabelTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::LabelTy)));
  208. - rb_define_const(cLLVMType, "FloatTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::FloatTy)));
  209. - rb_define_const(cLLVMType, "DoubleTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::DoubleTy)));
  210. + rb_define_const(cLLVMType, "Int1Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt1Ty(getGlobalContext()))));
  211. + rb_define_const(cLLVMType, "Int8Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt8Ty(getGlobalContext()))));
  212. + rb_define_const(cLLVMType, "Int16Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt16Ty(getGlobalContext()))));
  213. + rb_define_const(cLLVMType, "Int32Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt32Ty(getGlobalContext()))));
  214. + rb_define_const(cLLVMType, "Int64Ty", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(Type::getInt64Ty(getGlobalContext()))));
  215. + rb_define_const(cLLVMType, "VoidTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getVoidTy(getGlobalContext()))));
  216. + rb_define_const(cLLVMType, "LabelTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getLabelTy(getGlobalContext()))));
  217. + rb_define_const(cLLVMType, "FloatTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getFloatTy(getGlobalContext()))));
  218. + rb_define_const(cLLVMType, "DoubleTy", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<Type*>(Type::getDoubleTy(getGlobalContext()))));
  219.  
  220. // Figure out details of the target machine
  221. const IntegerType *machine_word_type;
  222. if(sizeof(void*) == 4) {
  223. - machine_word_type = Type::Int32Ty;
  224. + machine_word_type = Type::getInt32Ty(getGlobalContext());
  225. } else {
  226. - machine_word_type = Type::Int64Ty;
  227. + machine_word_type = Type::getInt64Ty(getGlobalContext());
  228. }
  229. rb_define_const(cLLVMRuby, "MACHINE_WORD", Data_Wrap_Struct(cLLVMType, NULL, NULL, const_cast<IntegerType*>(machine_word_type)));
  230. }
  231. diff --git a/lib/llvm.rb b/lib/llvm.rb
  232. index 7b95053..c00330c 100644
  233. --- a/lib/llvm.rb
  234. +++ b/lib/llvm.rb
  235. @@ -30,7 +30,7 @@ end
  236.  
  237. module LLVM
  238. # enum llvm::Type::TypeID
  239. - VoidTyID, FloatTyID, DoubleTyID, X86_FP80TyID, FP128TyID, PPC_FP128TyID, LabelTyID, IntegerTyID,
  240. + VoidTyID, FloatTyID, DoubleTyID, X86_FP80TyID, FP128TyID, PPC_FP128TyID, LabelTyID, MetadataTyID, IntegerTyID,
  241. FunctionTyID, StructTyID, ArrayTyID, PointerTyID, OpaqueTyID, VectorTyID = (0..13).to_a
  242.  
  243. class Builder
  244. diff --git a/test/test_basic.rb b/test/test_basic.rb
  245. index 84ed793..a068fa8 100644
  246. --- a/test/test_basic.rb
  247. +++ b/test/test_basic.rb
  248. @@ -317,7 +317,7 @@ class BasicTests < Test::Unit::TestCase
  249. function_tester(666) do |f|
  250. b = f.create_block.builder
  251. vt = Type.vector(MACHINE_WORD, 3)
  252. - vp = b.alloca(vt, 0)
  253. + vp = b.alloca(vt, 1)
  254. v = b.load(vp)
  255. v2 = b.insert_element(v, 666.llvm(MACHINE_WORD), 0.llvm(Type::Int32Ty))
  256. r = b.extract_element(v2, 0.llvm(Type::Int32Ty))
Add Comment
Please, Sign In to add comment