Skip to content

Commit

Permalink
Added AssemblySyntaxError exception which is raised when assembly can…
Browse files Browse the repository at this point in the history
…'t be parsed by read_assembly() method.

Updated assembly source code in test_value exception since it doesn't compile by llvm 2.9 any more.
  • Loading branch information
dubik committed Sep 26, 2011
1 parent b7082ad commit 2335bbd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
22 changes: 17 additions & 5 deletions ext/llvm_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ VALUE
llvm_module_get_function(VALUE self, VALUE name) {
Check_Type(name, T_STRING);
Module *m = LLVM_MODULE(self);

Function *f = NULL;

if(m)
f = m->getFunction(StringValuePtr(name));
return llvm_function_wrap(f);
f = m->getFunction(StringValuePtr(name));

return llvm_function_wrap(f);
}

VALUE
Expand Down Expand Up @@ -163,16 +166,25 @@ VALUE
llvm_module_read_assembly(VALUE self, VALUE assembly) {
Check_Type(assembly, T_STRING);

SMDiagnostic e;
const char * asmString = StringValuePtr(assembly);

SMDiagnostic e;
Module *module = ParseAssemblyString(
asmString,
0,
e,
getGlobalContext()
getGlobalContext()
);
//TODO How do we handle errors?

if(!module) {
VALUE exception = rb_exc_new2(cLLVMAssemblySyntaxError, e.getMessage().c_str());
rb_iv_set(exception, "@line", INT2NUM(e.getLineNo()));
rb_iv_set(exception, "@column", INT2NUM(e.getColumnNo()));
rb_iv_set(exception, "@line_contents", rb_str_new2(e.getLineContents().c_str()));
rb_iv_set(exception, "@filename", rb_str_new2(""));
rb_exc_raise(exception);
}

return Data_Wrap_Struct(cLLVMModule, NULL, NULL, module);
}

Expand Down
8 changes: 8 additions & 0 deletions ext/llvmruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ VALUE cLLVMPhi = Qnil;
VALUE cLLVMBinaryOps = Qnil;
VALUE cLLVMPassManager = Qnil;
VALUE cLLVMExecutionEngine = Qnil;
VALUE cLLVMAssemblySyntaxError = Qnil;

#define HANDLE_TERM_INST(Num, Opcode, Klass) VALUE cLLVM##Klass;
#define HANDLE_MEMORY_INST(Num, Opcode, Klass) VALUE cLLVM##Klass;
Expand Down Expand Up @@ -192,6 +193,8 @@ void Init_llvmruby() {
cLLVMPassManager = rb_define_class_under(cLLVMRuby, "PassManager", rb_cObject);
cLLVMExecutionEngine = rb_define_class_under(cLLVMRuby, "ExecutionEngine", rb_cObject);

cLLVMAssemblySyntaxError = rb_define_class_under(cLLVMRuby, "AssemblySyntaxError", rb_eSyntaxError);

init_types();
rb_define_module_function(cLLVMType, "pointer", llvm_type_pointer, 1);
rb_define_module_function(cLLVMType, "struct", llvm_type_struct, 1);
Expand Down Expand Up @@ -308,6 +311,11 @@ void Init_llvmruby() {
rb_define_module_function(cLLVMExecutionEngine, "run_function", llvm_execution_engine_run_function, -1);
rb_define_module_function(cLLVMExecutionEngine, "run_autoconvert", llvm_execution_engine_run_autoconvert, 1);

rb_define_attr(cLLVMAssemblySyntaxError, "line", 1, 0);
rb_define_attr(cLLVMAssemblySyntaxError, "column", 1, 0);
rb_define_attr(cLLVMAssemblySyntaxError, "line_contents", 1, 0);
rb_define_attr(cLLVMAssemblySyntaxError, "filename", 1, 0);

/*
printf("sizeof long: %d\n", (int)sizeof(long));
printf("sizeof ptr: %d\n", (int)sizeof(long*));
Expand Down
1 change: 1 addition & 0 deletions ext/llvmruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern VALUE cLLVMFunctionType;
extern VALUE cLLVMInstruction;
extern VALUE cLLVMBinaryOperator;
extern VALUE cLLVMAllocationInst;
extern VALUE cLLVMAssemblySyntaxError;

#define HANDLE_TERM_INST(Num, Opcode, Klass) extern VALUE cLLVM##Klass;
#define HANDLE_MEMORY_INST(Num, Opcode, Klass) extern VALUE cLLVM##Klass;
Expand Down
30 changes: 15 additions & 15 deletions test/test_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ class ValueTests < Test::Unit::TestCase

def setup
@assembly=<<-EOF
; ModuleID = 'constants.o'
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
target triple = "i386-apple-darwin9"
@CONST_PI = constant float 0x400921FA00000000 ; <float*> [#uses=0]
define float @circle_area(float %radius) nounwind {
entry:
%tmp2 = mul float %radius, 0x400921FA00000000 ; <float> [#uses=1]
%tmp4 = mul float %tmp2, %radius ; <float> [#uses=1]
ret float %tmp4
; ModuleID = '/tmp/webcompile/_14635_0.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-unknown-linux-gnu"
@CONST_PI = constant float 0x400921FA00000000, align 4
define float @circle_area(float %radius) nounwind readnone {
%1 = fmul float %radius, 0x400921FA00000000
%2 = fmul float %1, %radius
ret float %2
}
define i32 @add_42(i32 %x) nounwind {
entry:
%tmp2 = add i32 %x, 42 ; <i32> [#uses=1]
ret i32 %tmp2
define i32 @add_42(i32 %x) nounwind readnone {
%1 = add nsw i32 %x, 42
ret i32 %1
}
EOF

@m = LLVM::Module.read_assembly(@assembly)
@m = LLVM::Module.read_assembly(@assembly)
end

def test_check_value_is_constant
add42 = @m.get_function("add_42")
assert(add42)
assert_equal(2, add42.get_basic_block_list.count)
ins = add42.get_basic_block_list[0].get_instruction_list
add_ins = ins[0]
assert_equal(false,add_ins.get_operand(0).is_constant())
Expand Down

0 comments on commit 2335bbd

Please sign in to comment.