Permalink
Browse files

Implement LLVM::Value#type, LLVM::Type#to_s, LLVM::Type::type_id and …

…Ruby constants for llvm::Type::TypeID enum.
  • Loading branch information...
1 parent bd6fff4 commit c70593096d078193a19bb13a1959ea29bc135792 @macournoyer macournoyer committed Oct 24, 2008
Showing with 41 additions and 0 deletions.
  1. +22 −0 ext/llvm_value.cpp
  2. +6 −0 ext/llvmruby.c
  3. +4 −0 lib/llvm.rb
  4. +9 −0 test/test_basic.rb
View
@@ -28,6 +28,14 @@ llvm_value_set_name(VALUE self, VALUE rname) {
return rname;
}
+VALUE
+llvm_value_type(VALUE self) {
+ Value *v;
+ Data_Get_Struct(self, Value, v);
+ const Type *t = v->getType();
+ return Data_Wrap_Struct(cLLVMType, NULL, NULL, (void*) t);;
+}
+
VALUE
llvm_value_num_uses(VALUE self) {
Value *v;
@@ -147,6 +155,20 @@ llvm_type_function(int argc, VALUE *argv, VALUE self) {
return Data_Wrap_Struct(cLLVMFunctionType, NULL, NULL, ftype);
}
+VALUE
+llvm_type_to_s(VALUE self) {
+ Type *type;
+ Data_Get_Struct(self, Type, type);
+ return rb_str_new2(type->getDescription().c_str());
+}
+
+VALUE
+llvm_type_type_id(VALUE self) {
+ Type *type;
+ Data_Get_Struct(self, Type, type);
+ return INT2FIX((int) type->getTypeID());
+}
+
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)));
View
@@ -37,6 +37,8 @@ VALUE llvm_type_array(VALUE, VALUE, VALUE);
VALUE llvm_type_vector(VALUE, VALUE, VALUE);
VALUE llvm_type_function2(VALUE, VALUE);
VALUE llvm_type_function(VALUE, VALUE, VALUE);
+VALUE llvm_type_to_s(VALUE);
+VALUE llvm_type_type_id(VALUE);
void init_instructions();
@@ -120,6 +122,7 @@ VALUE llvm_value_get_immediate_constant(VALUE);
VALUE llvm_value_get_struct_constant(int, VALUE*, VALUE);
VALUE llvm_value_name(VALUE);
VALUE llvm_value_set_name(VALUE, VALUE);
+VALUE llvm_value_type(VALUE);
VALUE llvm_value_num_uses(VALUE);
VALUE llvm_value_used_in_basic_block(VALUE, VALUE);
VALUE llvm_value_replace_all_uses_with(VALUE, VALUE);
@@ -180,6 +183,8 @@ void Init_llvmruby() {
rb_define_module_function(cLLVMType, "array", llvm_type_array, 2);
rb_define_module_function(cLLVMType, "vector", llvm_type_vector, 2);
rb_define_module_function(cLLVMType, "function", llvm_type_function, -1);
+ rb_define_method(cLLVMType, "to_s", llvm_type_to_s, 0);
+ rb_define_method(cLLVMType, "type_id", llvm_type_type_id, 0);
rb_define_module_function(cLLVMValue, "get_constant", llvm_value_get_constant, 2);
rb_define_module_function(cLLVMValue, "get_float_constant", llvm_value_get_float_constant, 1);
@@ -188,6 +193,7 @@ void Init_llvmruby() {
rb_define_module_function(cLLVMValue, "get_struct_constant", llvm_value_get_struct_constant, -1);
rb_define_method(cLLVMValue, "name", llvm_value_name, 0);
rb_define_method(cLLVMValue, "name=", llvm_value_set_name, 1);
+ rb_define_method(cLLVMValue, "type", llvm_value_type, 0);
rb_define_method(cLLVMValue, "num_uses", llvm_value_num_uses, 0);
rb_define_method(cLLVMValue, "used_in_basic_block?", llvm_value_used_in_basic_block, 1);
rb_define_method(cLLVMValue, "replace_all_uses_with", llvm_value_replace_all_uses_with, 1);
View
@@ -29,6 +29,10 @@ def immediate
end
module LLVM
+ # enum llvm::Type::TypeID
+ VoidTyID, FloatTyID, DoubleTyID, X86_FP80TyID, FP128TyID, PPC_FP128TyID, LabelTyID, IntegerTyID,
+ FunctionTyID, StructTyID, ArrayTyID, PointerTyID, OpaqueTyID, VectorTyID = (0..13).to_a
+
class Builder
def self.add_bin_op(op)
define_method(op.downcase) do |x, y|
View
@@ -1,4 +1,5 @@
require 'test/unit'
+$:.unshift File.dirname(__FILE__) + "/../ext"
require 'llvm'
include LLVM
@@ -328,4 +329,12 @@ def test_pass_manager_run
m = LLVM::Module.new('test')
assert PassManager.new.run(m)
end
+
+ def test_type_to_s
+ assert_equal "i32", 2.llvm.type.to_s
+ end
+
+ def test_type_type_id
+ assert_equal IntegerTyID, 2.llvm.type.type_id
+ end
end

0 comments on commit c705930

Please sign in to comment.