diff --git a/test/simple.sf b/test/simple.sf index 6bdd22f..209b013 100644 --- a/test/simple.sf +++ b/test/simple.sf @@ -2,4 +2,4 @@ -main = 2 +main = -12321 diff --git a/test/simple_goal.ll b/test/simple_goal.ll index 33dddd7..095a731 100644 --- a/test/simple_goal.ll +++ b/test/simple_goal.ll @@ -1,7 +1,11 @@ ; External declaration of the puts function declare i32 @puts(i8* nocapture) nounwind +declare i64 @llvm.abs.i64(i64, i1 immarg) -define [22 x i8] @i64ToString(i64 %val) { +define [21 x i8] @i64ToString(i64 %val_org) { + %val = alloca i64 + store i64 %val_org, i64* %val + %val_copy = add i64 0, %val_org ; https://stackoverflow.com/a/7123710 ; an algorithm for translating ints to strings ; s = '' @@ -18,43 +22,67 @@ define [22 x i8] @i64ToString(i64 %val) { ; return sign + s ; allocate memory for the string, and store the temp variable into it - %string_ptr = alloca [22 x i8] - store [22 x i8] c"Tjena banane hellosa\0A\00", ptr %string_ptr + %string_ptr = alloca [21 x i8] + store [21 x i8] c"\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00", ptr %string_ptr ; create a pointer to the array %array_index_ptr = alloca i32 - store i32 0, ptr %array_index_ptr + store i32 0, i32* %array_index_ptr - ; get the value of the first element in the array + br label %while_point + while_point: + %val.tmp = load i64, i64* %val; load i64, i64* %val + %val.1 = call i64 @llvm.abs.i64(i64 %val.tmp, i1 true) + %tmp = load i32, ptr %array_index_ptr + %array_pointer.1 = getelementptr [21 x i8], [21 x i8]* %string_ptr, i32 0, i32 %tmp + %array_index_ptr.1 = add i32 %tmp, 1 + store i32 %array_index_ptr.1, ptr %array_index_ptr + + ; this should not work, but it does + %remainder = srem i64 %val.1, 10 + %remainder_tmp = alloca i64 + store i64 %remainder, i64* %remainder_tmp + %remainder_tmp8 = bitcast i64* %remainder_tmp to i8* + %remainder8 = load i8, i8* %remainder_tmp8 + + %i = sdiv i64 %val.1, 10 + %char = add i8 48, %remainder8 + store i8 %char, i8* %array_pointer.1 ; update string! + store i64 %i, i64* %val + %while_condition = icmp eq i64 %i, 0 + + br i1 %while_condition, label %while_break, label %while_point + while_break: + + ; get last_pointer %array_index_ptr.0 = load i32, i32* %array_index_ptr - %p = getelementptr [22 x i8], [22 x i8]* %string_ptr, i32 0, i32 %array_index_ptr.0 - + %array_pointer.0 = getelementptr [21 x i8], [21 x i8]* %string_ptr, i32 0, i32 %array_index_ptr.0 + ; check if p is below 0 - %condition = icmp slt i64 %val, 0 + %condition = icmp slt i64 %val_copy, 0 br i1 %condition, label %negative_check_true, label %negative_check_false negative_check_true: - store i8 45, i8* %p + store i8 45, i8* %array_pointer.0 br label %negative_check_done negative_check_false: - store i8 43, i8* %p + ; store i8 43, i8* %array_pointer.0 br label %negative_check_done negative_check_done: - - ; iterate over the next nums - + %noop = add i32 0, 0 ; load the result and return it - %res = load [22 x i8],[22 x i8]* %string_ptr - ret [22 x i8] %res + %res = load [21 x i8],[21 x i8]* %string_ptr + ret [21 x i8] %res } ; Definition of main function define i32 @main() { ; i32()* - %val = add i64 1, 0 + ; %val = add i64 -123456789, 0 + %val = add i64 -1234, 0 - %print_res = call [22 x i8] @i64ToString(i64 %val) - %ptr = alloca [22 x i8] - store [22 x i8] %print_res, ptr %ptr + %print_res = call [21 x i8] @i64ToString(i64 %val) + %ptr = alloca [21 x i8] + store [21 x i8] %print_res, ptr %ptr call i32 @puts(i8* %ptr) diff --git a/test/simple_goal.s b/test/simple_goal.s new file mode 100644 index 0000000..c90b572 --- /dev/null +++ b/test/simple_goal.s @@ -0,0 +1,120 @@ + .text + .file "simple_goal.ll" + .globl i64ToString # -- Begin function i64ToString + .p2align 4, 0x90 + .type i64ToString,@function +i64ToString: # @i64ToString + .cfi_startproc +# %bb.0: + pushq %rbp + .cfi_def_cfa_offset 16 + pushq %r15 + .cfi_def_cfa_offset 24 + pushq %r14 + .cfi_def_cfa_offset 32 + pushq %r13 + .cfi_def_cfa_offset 40 + pushq %r12 + .cfi_def_cfa_offset 48 + pushq %rbx + .cfi_def_cfa_offset 56 + subq $40, %rsp + .cfi_def_cfa_offset 96 + .cfi_offset %rbx, -56 + .cfi_offset %r12, -48 + .cfi_offset %r13, -40 + .cfi_offset %r14, -32 + .cfi_offset %r15, -24 + .cfi_offset %rbp, -16 + movq %rsi, %r12 + movq %rdi, %r14 + movabsq $7810763617093968238, %rax # imm = 0x6C656820656E616E + movq %rax, 22(%rsp) + movabsq $7017206772232710740, %rax # imm = 0x616220616E656A54 + movq %rax, 14(%rsp) + movw $10, 34(%rsp) + movl $1634955116, 30(%rsp) # imm = 0x61736F6C + movl $0, 36(%rsp) + leaq 14(%rsp), %rax + testq %rsi, %rsi + js .LBB0_1 +# %bb.2: # %negative_check_false + movb $43, (%rax) + jmp .LBB0_3 +.LBB0_1: # %negative_check_true + movb $45, (%rax) +.LBB0_3: # %negative_check_done + movabsq $7378697629483820647, %r13 # imm = 0x6666666666666667 + leaq 14(%rsp), %r15 + .p2align 4, 0x90 +.LBB0_4: # %while_point + # =>This Inner Loop Header: Depth=1 + movq %r12, %rax + imulq %r13 + movq %rdx, %rbx + movq %rdx, %rbp + shrq $63, %rbp + sarq $2, %rbx + movq %r15, %rdi + callq puts@PLT + addq %rbp, %rbx + jne .LBB0_4 +# %bb.5: # %while_break + movq 14(%rsp), %rax + movq 22(%rsp), %rcx + movl 30(%rsp), %edx + movzwl 34(%rsp), %esi + movw %si, 20(%r14) + movl %edx, 16(%r14) + movq %rcx, 8(%r14) + movq %rax, (%r14) + movq %r14, %rax + addq $40, %rsp + .cfi_def_cfa_offset 56 + popq %rbx + .cfi_def_cfa_offset 48 + popq %r12 + .cfi_def_cfa_offset 40 + popq %r13 + .cfi_def_cfa_offset 32 + popq %r14 + .cfi_def_cfa_offset 24 + popq %r15 + .cfi_def_cfa_offset 16 + popq %rbp + .cfi_def_cfa_offset 8 + retq +.Lfunc_end0: + .size i64ToString, .Lfunc_end0-i64ToString + .cfi_endproc + # -- End function + .globl main # -- Begin function main + .p2align 4, 0x90 + .type main,@function +main: # @main + .cfi_startproc +# %bb.0: + subq $56, %rsp + .cfi_def_cfa_offset 64 + leaq 12(%rsp), %rdi + movl $200, %esi + callq i64ToString@PLT + movzwl 32(%rsp), %eax + movl 28(%rsp), %ecx + movq 12(%rsp), %rdx + movq 20(%rsp), %rsi + movq %rdx, 34(%rsp) + movq %rsi, 42(%rsp) + movl %ecx, 50(%rsp) + movw %ax, 54(%rsp) + leaq 34(%rsp), %rdi + callq puts@PLT + xorl %eax, %eax + addq $56, %rsp + .cfi_def_cfa_offset 8 + retq +.Lfunc_end1: + .size main, .Lfunc_end1-main + .cfi_endproc + # -- End function + .section ".note.GNU-stack","",@progbits