I have written a compiler that uses Inkwell to produce LLVM IR. When compiling a simple fizz_buzz program I have created the following llir. The llir when compiled and ran causes a segfault. Why is this? Thanks.
; ModuleID = 'main'
source_filename = "main"
@global_string = global [10 x i8] c"fizzbuzzA0"
@global_string.1 = global [6 x i8] c"fizzA0"
@global_string.2 = global [6 x i8] c"buzzA0"
declare void @printf(ptr, ...)
declare void @malloc(i32)
declare void @free(ptr)
define void @fizz_buzz(i32 %x) {
entry:
%x1 = alloca i32, align 4
store i32 %x, ptr %x1, align 4
%is_mod_three = alloca i32, align 4
%is_mod_five = alloca i32, align 4
br label %basic_block_0
basic_block_0: ; preds = %entry
%access_variable = load i32, ptr %x1, align 4
%int_to_int = sext i32 %access_variable to i64
%"$int_case" = srem i64 %int_to_int, 3
%int_cmp_zero_logical_not = icmp eq i64 %"$int_case", 0
store i1 %int_cmp_zero_logical_not, ptr %is_mod_three, align 1
%access_variable2 = load i32, ptr %x1, align 4
%int_to_int3 = sext i32 %access_variable2 to i64
%"$int_case4" = srem i64 %int_to_int3, 5
%int_cmp_zero_logical_not5 = icmp eq i64 %"$int_case4", 0
store i1 %int_cmp_zero_logical_not5, ptr %is_mod_five, align 1
%access_variable6 = load i32, ptr %is_mod_three, align 4
%access_variable7 = load i32, ptr %is_mod_five, align 4
%bitwise_and = and i32 %access_variable6, %access_variable7
%conv_to_i1_type = trunc i32 %bitwise_and to i1
br i1 %conv_to_i1_type, label %if_1_then, label %if_1_else
if_1_then: ; preds = %basic_block_0
call void (ptr, ...) @printf(ptr @global_string)
br label %if_1_end
if_1_else: ; preds = %basic_block_0
%access_variable8 = load i32, ptr %is_mod_three, align 4
%conv_to_i1_type9 = trunc i32 %access_variable8 to i1
br i1 %conv_to_i1_type9, label %if_2_then, label %if_2_else
if_2_then: ; preds = %if_1_else
call void (ptr, ...) @printf(ptr @global_string.1)
br label %if_2_end
if_2_else: ; preds = %if_1_else
call void (ptr, ...) @printf(ptr @global_string.2)
br label %if_2_end
if_2_end: ; preds = %if_2_else, %if_2_then
br label %if_1_end
if_1_end: ; preds = %if_2_end, %if_1_then
ret void
}
define i32 @main() {
entry:
%i = alloca i32, align 4
br label %basic_block_0
basic_block_0: ; preds = %entry
store i64 0, ptr %i, align 4
br label %loop_3
loop_3: ; preds = %loop_3_body, %basic_block_0
%access_variable = load i32, ptr %i, align 4
%int_to_int = sext i32 %access_variable to i64
%cmp_less_than = icmp slt i64 %int_to_int, 100
br i1 %cmp_less_than, label %loop_3_body, label %loop_3_end
loop_3_body: ; preds = %loop_3
%access_variable1 = load i32, ptr %i, align 4
call void @fizz_buzz(i32 %access_variable1)
%load_ptr = load i32, ptr %i, align 4
%inc_int = add i32 %load_ptr, 1
store i32 %inc_int, ptr %i, align 4
br label %loop_3
loop_3_end: ; preds = %loop_3
ret i32 0
}
i dont know what to try as i am new to llir.adskfja;lsdfj;lakdjsf;lkadsa;fkljas;ldfjka;ldjf;lakdjsf;lkads;flkjasd;fjka;lkdjsf;lakdjsf;lakdjsf;klajds;fkja;lsdfjk;alkdjsf;alkdjsf;lakdsf;lkads;fkljasdkjfa;lsdjkf;alkdjsf;lakdjsf;lkadjsflkadjsf;kl
Jake Snell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.