Skip to content

Commit

Permalink
fix bug function return nil, fix bug array of nil
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnguyen1618 committed Feb 2, 2019
1 parent 9ac8a8e commit d2d0cae
Show file tree
Hide file tree
Showing 8 changed files with 531 additions and 12 deletions.
Binary file added bin/compiler
Binary file not shown.
134 changes: 134 additions & 0 deletions llvm_byte_code/test/array_of_rec.tig-opt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
; ModuleID = 'llvm_byte_code/test/array_of_rec.tig.ll'
source_filename = "Tiger jit"

@0 = private unnamed_addr constant [49 x i8] c"test/array_of_rec.tig::12.25: Array out of bound\00"
@1 = private unnamed_addr constant [49 x i8] c"test/array_of_rec.tig::13.26: Array out of bound\00"
@2 = private unnamed_addr constant [49 x i8] c"test/array_of_rec.tig::14.26: Array out of bound\00"

declare i32 @tig_nillable(i8*) local_unnamed_addr

declare void @tig_check_array_bound(i8*, i32, i8*) local_unnamed_addr

declare void @assert_equal_int(i32, i32) local_unnamed_addr

define i32 @main() local_unnamed_addr {
entry:
%frame_pointer = alloca { i32 }
%malloccall = tail call i8* @malloc(i32 ptrtoint ({ i32, i8* }* getelementptr ({ i32, i8* }, { i32, i8* }* null, i32 1) to i32))
%record_init = bitcast i8* %malloccall to { i32, i8* }*
%Element = getelementptr { i32, i8* }, { i32, i8* }* %record_init, i32 0, i32 0
store i32 1, i32* %Element
%Element1 = getelementptr { i32, i8* }, { i32, i8* }* %record_init, i32 0, i32 1
store i8* null, i8** %Element1
%0 = call { i32, i8* }* @f({ i32 }* %frame_pointer)
%malloccall2 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i1** getelementptr (i1*, i1** null, i32 1) to i32), i32 5))
%array_init = bitcast i8* %malloccall2 to { i32, i8* }**
br label %test

test: ; preds = %loop, %entry
%i.0 = phi i32 [ 0, %entry ], [ %add_tmp, %loop ]
%lt_tmp = icmp slt i32 %i.0, 5
%bool_tmp = zext i1 %lt_tmp to i32
%cond = icmp eq i32 %bool_tmp, 1
br i1 %cond, label %loop, label %end

loop: ; preds = %test
%Element5 = getelementptr { i32, i8* }*, { i32, i8* }** %array_init, i32 %i.0
store { i32, i8* }* %0, { i32, i8* }** %Element5
%add_tmp = add i32 %i.0, 1
br label %test

end: ; preds = %test
%malloccall6 = tail call i8* @malloc(i32 ptrtoint ({ i32, { i32, i8* }** }* getelementptr ({ i32, { i32, i8* }** }, { i32, { i32, i8* }** }* null, i32 1) to i32))
%array_wrapper = bitcast i8* %malloccall6 to { i32, { i32, i8* }** }*
%array_info = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper, i32 0, i32 0
store i32 5, i32* %array_info
%array_info7 = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper, i32 0, i32 1
store { i32, i8* }** %array_init, { i32, i8* }*** %array_info7
%malloccall8 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i1** getelementptr (i1*, i1** null, i32 1) to i32), i32 5))
%array_init9 = bitcast i8* %malloccall8 to { i32, i8* }**
br label %test11

test11: ; preds = %loop12, %end
%i10.0 = phi i32 [ 0, %end ], [ %add_tmp20, %loop12 ]
%lt_tmp15 = icmp slt i32 %i10.0, 5
%bool_tmp16 = zext i1 %lt_tmp15 to i32
%cond17 = icmp eq i32 %bool_tmp16, 1
br i1 %cond17, label %loop12, label %end13

loop12: ; preds = %test11
%Element19 = getelementptr { i32, i8* }*, { i32, i8* }** %array_init9, i32 %i10.0
store { i32, i8* }* null, { i32, i8* }** %Element19
%add_tmp20 = add i32 %i10.0, 1
br label %test11

end13: ; preds = %test11
%malloccall21 = tail call i8* @malloc(i32 ptrtoint ({ i32, { i32, i8* }** }* getelementptr ({ i32, { i32, i8* }** }, { i32, { i32, i8* }** }* null, i32 1) to i32))
%array_wrapper22 = bitcast i8* %malloccall21 to { i32, { i32, i8* }** }*
%array_info23 = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper22, i32 0, i32 0
store i32 5, i32* %array_info23
%array_info24 = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper22, i32 0, i32 1
store { i32, i8* }** %array_init9, { i32, i8* }*** %array_info24
%malloccall26 = tail call i8* @malloc(i32 mul (i32 ptrtoint (i1** getelementptr (i1*, i1** null, i32 1) to i32), i32 5))
%array_init27 = bitcast i8* %malloccall26 to { i32, i8* }**
br label %test29

test29: ; preds = %loop30, %end13
%i28.0 = phi i32 [ 0, %end13 ], [ %add_tmp38, %loop30 ]
%lt_tmp33 = icmp slt i32 %i28.0, 5
%bool_tmp34 = zext i1 %lt_tmp33 to i32
%cond35 = icmp eq i32 %bool_tmp34, 1
br i1 %cond35, label %loop30, label %end31

loop30: ; preds = %test29
%Element37 = getelementptr { i32, i8* }*, { i32, i8* }** %array_init27, i32 %i28.0
store { i32, i8* }* null, { i32, i8* }** %Element37
%add_tmp38 = add i32 %i28.0, 1
br label %test29

end31: ; preds = %test29
%malloccall39 = tail call i8* @malloc(i32 ptrtoint ({ i32, { i32, i8* }** }* getelementptr ({ i32, { i32, i8* }** }, { i32, { i32, i8* }** }* null, i32 1) to i32))
%array_wrapper40 = bitcast i8* %malloccall39 to { i32, { i32, i8* }** }*
%array_info41 = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper40, i32 0, i32 0
store i32 5, i32* %array_info41
%array_info42 = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper40, i32 0, i32 1
store { i32, i8* }** %array_init27, { i32, i8* }*** %array_info42
%1 = bitcast { i32, { i32, i8* }** }* %array_wrapper to i8*
call void @tig_check_array_bound(i8* %1, i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @0, i32 0, i32 0))
%array_pointer = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper, i32 0, i32 1
%arr_addr = load { i32, i8* }**, { i32, i8* }*** %array_pointer
%arr_ele_addr = getelementptr { i32, i8* }*, { i32, i8* }** %arr_addr, i32 1
%arr_ele = load { i32, i8* }*, { i32, i8* }** %arr_ele_addr
%2 = bitcast { i32, i8* }* %arr_ele to i8*
%3 = call i32 @tig_nillable(i8* %2)
call void @assert_equal_int(i32 %3, i32 1)
%4 = bitcast { i32, { i32, i8* }** }* %array_wrapper22 to i8*
call void @tig_check_array_bound(i8* %4, i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @1, i32 0, i32 0))
%array_pointer45 = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper22, i32 0, i32 1
%arr_addr46 = load { i32, i8* }**, { i32, i8* }*** %array_pointer45
%arr_ele_addr47 = getelementptr { i32, i8* }*, { i32, i8* }** %arr_addr46, i32 1
%arr_ele48 = load { i32, i8* }*, { i32, i8* }** %arr_ele_addr47
%5 = bitcast { i32, i8* }* %arr_ele48 to i8*
%6 = call i32 @tig_nillable(i8* %5)
call void @assert_equal_int(i32 %6, i32 1)
%7 = bitcast { i32, { i32, i8* }** }* %array_wrapper40 to i8*
call void @tig_check_array_bound(i8* %7, i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @2, i32 0, i32 0))
%array_pointer50 = getelementptr { i32, { i32, i8* }** }, { i32, { i32, i8* }** }* %array_wrapper40, i32 0, i32 1
%arr_addr51 = load { i32, i8* }**, { i32, i8* }*** %array_pointer50
%arr_ele_addr52 = getelementptr { i32, i8* }*, { i32, i8* }** %arr_addr51, i32 1
%arr_ele53 = load { i32, i8* }*, { i32, i8* }** %arr_ele_addr52
%8 = bitcast { i32, i8* }* %arr_ele53 to i8*
%9 = call i32 @tig_nillable(i8* %8)
call void @assert_equal_int(i32 %9, i32 1)
ret i32 0
}

declare noalias i8* @malloc(i32) local_unnamed_addr

define { i32, i8* }* @f({ i32 }*) local_unnamed_addr {
entry:
%frame_pointer = alloca { { i32 }* }
%arg_address = getelementptr { { i32 }* }, { { i32 }* }* %frame_pointer, i32 0, i32 0
store { i32 }* %0, { i32 }** %arg_address
ret { i32, i8* }* null
}
161 changes: 161 additions & 0 deletions llvm_byte_code/test/array_of_rec.tig-opt.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
.text
.file "Tiger jit"
.globl main # -- Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main
.cfi_startproc
# %bb.0: # %entry
pushq %r15
.cfi_def_cfa_offset 16
pushq %r14
.cfi_def_cfa_offset 24
pushq %r12
.cfi_def_cfa_offset 32
pushq %rbx
.cfi_def_cfa_offset 40
pushq %rax
.cfi_def_cfa_offset 48
.cfi_offset %rbx, -40
.cfi_offset %r12, -32
.cfi_offset %r14, -24
.cfi_offset %r15, -16
movl $16, %edi
callq malloc
movl $1, (%rax)
movq $0, 8(%rax)
movq %rsp, %rdi
callq f
movq %rax, %rbx
movl $40, %edi
callq malloc
movq %rax, %r15
xorl %eax, %eax
cmpl $4, %eax
jg .LBB0_3
.p2align 4, 0x90
.LBB0_2: # %loop
# =>This Inner Loop Header: Depth=1
movq %rbx, (%r15,%rax,8)
incq %rax
cmpl $4, %eax
jle .LBB0_2
.LBB0_3: # %end
movl $16, %edi
callq malloc
movq %rax, %r14
movl $5, (%r14)
movq %r15, 8(%r14)
movl $40, %edi
callq malloc
movq %rax, %rbx
xorl %eax, %eax
cmpl $4, %eax
jg .LBB0_6
.p2align 4, 0x90
.LBB0_5: # %loop12
# =>This Inner Loop Header: Depth=1
movq $0, (%rbx,%rax,8)
incq %rax
cmpl $4, %eax
jle .LBB0_5
.LBB0_6: # %end13
movl $16, %edi
callq malloc
movq %rax, %r15
movl $5, (%r15)
movq %rbx, 8(%r15)
movl $40, %edi
callq malloc
movq %rax, %rbx
xorl %eax, %eax
cmpl $4, %eax
jg .LBB0_9
.p2align 4, 0x90
.LBB0_8: # %loop30
# =>This Inner Loop Header: Depth=1
movq $0, (%rbx,%rax,8)
incq %rax
cmpl $4, %eax
jle .LBB0_8
.LBB0_9: # %end31
movl $16, %edi
callq malloc
movq %rax, %r12
movl $5, (%r12)
movq %rbx, 8(%r12)
movl $1, %esi
movl $.L__unnamed_1, %edx
movq %r14, %rdi
callq tig_check_array_bound
movq 8(%r14), %rax
movq 8(%rax), %rdi
callq tig_nillable
movl $1, %esi
movl %eax, %edi
callq assert_equal_int
movl $1, %esi
movl $.L__unnamed_2, %edx
movq %r15, %rdi
callq tig_check_array_bound
movq 8(%r15), %rax
movq 8(%rax), %rdi
callq tig_nillable
movl $1, %esi
movl %eax, %edi
callq assert_equal_int
movl $1, %esi
movl $.L__unnamed_3, %edx
movq %r12, %rdi
callq tig_check_array_bound
movq 8(%r12), %rax
movq 8(%rax), %rdi
callq tig_nillable
movl $1, %esi
movl %eax, %edi
callq assert_equal_int
xorl %eax, %eax
addq $8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
.Lfunc_end0:
.size main, .Lfunc_end0-main
.cfi_endproc
# -- End function
.globl f # -- Begin function f
.p2align 4, 0x90
.type f,@function
f: # @f
.cfi_startproc
# %bb.0: # %entry
movq %rdi, -8(%rsp)
xorl %eax, %eax
retq
.Lfunc_end1:
.size f, .Lfunc_end1-f
.cfi_endproc
# -- End function
.type .L__unnamed_1,@object # @0
.section .rodata.str1.16,"aMS",@progbits,1
.p2align 4
.L__unnamed_1:
.asciz "test/array_of_rec.tig::12.25: Array out of bound"
.size .L__unnamed_1, 49

.type .L__unnamed_2,@object # @1
.p2align 4
.L__unnamed_2:
.asciz "test/array_of_rec.tig::13.26: Array out of bound"
.size .L__unnamed_2, 49

.type .L__unnamed_3,@object # @2
.p2align 4
.L__unnamed_3:
.asciz "test/array_of_rec.tig::14.26: Array out of bound"
.size .L__unnamed_3, 49


.section ".note.GNU-stack","",@progbits
Loading

0 comments on commit d2d0cae

Please sign in to comment.