Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnguyen1618 committed Feb 5, 2019
1 parent ee66611 commit 7884bb6
Show file tree
Hide file tree
Showing 14 changed files with 1,296 additions and 155 deletions.
Binary file modified bin/compiler
Binary file not shown.
313 changes: 277 additions & 36 deletions llvm_byte_code/test/binary_sort.tig-opt.ll

Large diffs are not rendered by default.

381 changes: 318 additions & 63 deletions llvm_byte_code/test/binary_sort.tig-opt.s

Large diffs are not rendered by default.

352 changes: 315 additions & 37 deletions llvm_byte_code/test/binary_sort.tig.ll

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions llvm_byte_code/test/record_esc.tig-opt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; ModuleID = 'llvm_byte_code/test/record_esc.tig.ll'
source_filename = "Tiger jit"

define i32 @main() local_unnamed_addr {
entry:
%frame_pointer = alloca { i32, { i32, i8* }* }
%root = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
store { i32, i8* }* null, { i32, i8* }** %root
ret i32 0
}

define void @f({ i32, { i32, i8* }* }*) local_unnamed_addr {
entry:
%frame_pointer = alloca { { i32, { i32, i8* }* }* }
%arg_address = getelementptr { { i32, { i32, i8* }* }* }, { { i32, { i32, i8* }* }* }* %frame_pointer, i32 0, i32 0
store { i32, { i32, i8* }* }* %0, { i32, { i32, i8* }* }** %arg_address
%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 5, i32* %Element
%Element2 = getelementptr { i32, i8* }, { i32, i8* }* %record_init, i32 0, i32 1
store i8* null, i8** %Element2
ret void
}

declare noalias i8* @malloc(i32) local_unnamed_addr
36 changes: 36 additions & 0 deletions llvm_byte_code/test/record_esc.tig-opt.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.text
.file "Tiger jit"
.globl main # -- Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main
.cfi_startproc
# %bb.0: # %entry
movq $0, -8(%rsp)
xorl %eax, %eax
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
pushq %rax
.cfi_def_cfa_offset 16
movq %rdi, (%rsp)
movl $16, %edi
callq malloc
movl $5, (%rax)
movq $0, 8(%rax)
popq %rax
retq
.Lfunc_end1:
.size f, .Lfunc_end1-f
.cfi_endproc
# -- End function

.section ".note.GNU-stack","",@progbits
77 changes: 77 additions & 0 deletions llvm_byte_code/test/record_esc.tig.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
; ModuleID = 'Tiger jit'
source_filename = "Tiger jit"

declare void @tig_print_int(i32)

declare void @tig_print(i8*)

declare void @print_arr_int_ele(i32)

declare i32* @tig_init_array(i32, i32)

declare i32* @tig_init_record(i32)

declare i32 @tig_array_length(i8*)

declare i32 @tig_nillable(i8*)

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

declare void @tig_check_null_pointer(i8*, i8*)

declare i32 @tig_random(i32)

declare void @tig_exit(i32)

declare void @tig_flush()

declare i8* @tig_getchar()

declare i32 @tig_ord(i8*)

declare i8* @tig_chr(i32)

declare i32 @tig_string_cmp(i8*, i8*)

declare i32 @tig_size(i8*)

declare i8* @tig_substring(i8*, i32, i32)

declare i8* @tig_concat(i8*, i8*)

declare i32 @tig_not(i32)

declare void @assert_equal_int(i32, i32)

declare void @assert_equal_string(i8*, i8*)

define i32 @main() {
entry:
%frame_pointer = alloca { i32, { i32, i8* }* }
%root = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
store { i32, i8* }* null, { i32, i8* }** %root
ret i32 0

break_loop: ; No predecessors!
ret i32 0
}

define void @f({ i32, { i32, i8* }* }*) {
entry:
%frame_pointer = alloca { { i32, { i32, i8* }* }* }
%arg_address = getelementptr { { i32, { i32, i8* }* }* }, { { i32, { i32, i8* }* }* }* %frame_pointer, i32 0, i32 0
store { i32, { i32, i8* }* }* %0, { i32, { i32, i8* }* }** %arg_address
%fp_addr_in_sl = getelementptr { { i32, { i32, i8* }* }* }, { { i32, { i32, i8* }* }* }* %frame_pointer, i32 0, i32 0
%fp_addr = load { i32, { i32, i8* }* }*, { i32, { i32, i8* }* }** %fp_addr_in_sl
%root = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %fp_addr, i32 0, i32 1
%root1 = load { i32, i8* }*, { i32, i8* }** %root
%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 5, i32* %Element
%Element2 = getelementptr { i32, i8* }, { i32, i8* }* %record_init, i32 0, i32 1
store i8* null, i8** %Element2
ret void
}

declare noalias i8* @malloc(i32)
51 changes: 51 additions & 0 deletions llvm_byte_code/test/record_nil_esc.tig-opt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; ModuleID = 'llvm_byte_code/test/record_nil_esc.tig.ll'
source_filename = "Tiger jit"

@0 = private unnamed_addr constant [54 x i8] c"test/record_nil_esc.tig::8.19: Nil pointer exception!\00"

declare i32 @tig_nillable(i8*) local_unnamed_addr

declare void @tig_check_null_pointer(i8*, 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, { i32, i8* }* }
%root = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
store { i32, i8* }* null, { i32, i8* }** %root
%root1 = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
%root2 = load { i32, i8* }*, { i32, i8* }** %root1
%0 = bitcast { i32, i8* }* %root2 to i8*
%1 = call i32 @tig_nillable(i8* %0)
call void @assert_equal_int(i32 %1, i32 1)
call void @f({ i32, { i32, i8* }* }* %frame_pointer)
%root3 = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
%root4 = load { i32, i8* }*, { i32, i8* }** %root3
%2 = bitcast { i32, i8* }* %root4 to i8*
call void @tig_check_null_pointer(i8* %2, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @0, i32 0, i32 0))
%element = getelementptr { i32, i8* }, { i32, i8* }* %root4, i32 0, i32 0
%field_var = load i32, i32* %element
call void @assert_equal_int(i32 %field_var, i32 5)
ret i32 0
}

define void @f({ i32, { i32, i8* }* }*) local_unnamed_addr {
entry:
%frame_pointer = alloca { { i32, { i32, i8* }* }* }
%arg_address = getelementptr { { i32, { i32, i8* }* }* }, { { i32, { i32, i8* }* }* }* %frame_pointer, i32 0, i32 0
store { i32, { i32, i8* }* }* %0, { i32, { i32, i8* }* }** %arg_address
%fp_addr_in_sl = getelementptr { { i32, { i32, i8* }* }* }, { { i32, { i32, i8* }* }* }* %frame_pointer, i32 0, i32 0
%fp_addr = load { i32, { i32, i8* }* }*, { i32, { i32, i8* }* }** %fp_addr_in_sl
%root = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %fp_addr, i32 0, i32 1
%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 5, i32* %Element
%Element1 = getelementptr { i32, i8* }, { i32, i8* }* %record_init, i32 0, i32 1
store i8* null, i8** %Element1
store { i32, i8* }* %record_init, { i32, i8* }** %root
ret void
}

declare noalias i8* @malloc(i32) local_unnamed_addr
70 changes: 70 additions & 0 deletions llvm_byte_code/test/record_nil_esc.tig-opt.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
.text
.file "Tiger jit"
.globl main # -- Begin function main
.p2align 4, 0x90
.type main,@function
main: # @main
.cfi_startproc
# %bb.0: # %entry
pushq %rbx
.cfi_def_cfa_offset 16
subq $16, %rsp
.cfi_def_cfa_offset 32
.cfi_offset %rbx, -16
movq $0, 8(%rsp)
xorl %edi, %edi
callq tig_nillable
movl $1, %esi
movl %eax, %edi
callq assert_equal_int
movq %rsp, %rdi
callq f
movq 8(%rsp), %rbx
movl $.L__unnamed_1, %esi
movq %rbx, %rdi
callq tig_check_null_pointer
movl (%rbx), %edi
movl $5, %esi
callq assert_equal_int
xorl %eax, %eax
addq $16, %rsp
popq %rbx
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
pushq %rbx
.cfi_def_cfa_offset 16
subq $16, %rsp
.cfi_def_cfa_offset 32
.cfi_offset %rbx, -16
movq %rdi, %rbx
movq %rbx, 8(%rsp)
movl $16, %edi
callq malloc
movl $5, (%rax)
movq $0, 8(%rax)
movq %rax, 8(%rbx)
addq $16, %rsp
popq %rbx
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/record_nil_esc.tig::8.19: Nil pointer exception!"
.size .L__unnamed_1, 54


.section ".note.GNU-stack","",@progbits
92 changes: 92 additions & 0 deletions llvm_byte_code/test/record_nil_esc.tig.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
; ModuleID = 'Tiger jit'
source_filename = "Tiger jit"

@0 = private unnamed_addr constant [54 x i8] c"test/record_nil_esc.tig::8.19: Nil pointer exception!\00"

declare void @tig_print_int(i32)

declare void @tig_print(i8*)

declare void @print_arr_int_ele(i32)

declare i32* @tig_init_array(i32, i32)

declare i32* @tig_init_record(i32)

declare i32 @tig_array_length(i8*)

declare i32 @tig_nillable(i8*)

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

declare void @tig_check_null_pointer(i8*, i8*)

declare i32 @tig_random(i32)

declare void @tig_exit(i32)

declare void @tig_flush()

declare i8* @tig_getchar()

declare i32 @tig_ord(i8*)

declare i8* @tig_chr(i32)

declare i32 @tig_string_cmp(i8*, i8*)

declare i32 @tig_size(i8*)

declare i8* @tig_substring(i8*, i32, i32)

declare i8* @tig_concat(i8*, i8*)

declare i32 @tig_not(i32)

declare void @assert_equal_int(i32, i32)

declare void @assert_equal_string(i8*, i8*)

define i32 @main() {
entry:
%frame_pointer = alloca { i32, { i32, i8* }* }
%root = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
store { i32, i8* }* null, { i32, i8* }** %root
%root1 = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
%root2 = load { i32, i8* }*, { i32, i8* }** %root1
%0 = bitcast { i32, i8* }* %root2 to i8*
%1 = call i32 @tig_nillable(i8* %0)
call void @assert_equal_int(i32 %1, i32 1)
call void @f({ i32, { i32, i8* }* }* %frame_pointer)
%root3 = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %frame_pointer, i32 0, i32 1
%root4 = load { i32, i8* }*, { i32, i8* }** %root3
%2 = bitcast { i32, i8* }* %root4 to i8*
call void @tig_check_null_pointer(i8* %2, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @0, i32 0, i32 0))
%element = getelementptr { i32, i8* }, { i32, i8* }* %root4, i32 0, i32 0
%field_var = load i32, i32* %element
call void @assert_equal_int(i32 %field_var, i32 5)
ret i32 0

break_loop: ; No predecessors!
ret i32 0
}

define void @f({ i32, { i32, i8* }* }*) {
entry:
%frame_pointer = alloca { { i32, { i32, i8* }* }* }
%arg_address = getelementptr { { i32, { i32, i8* }* }* }, { { i32, { i32, i8* }* }* }* %frame_pointer, i32 0, i32 0
store { i32, { i32, i8* }* }* %0, { i32, { i32, i8* }* }** %arg_address
%fp_addr_in_sl = getelementptr { { i32, { i32, i8* }* }* }, { { i32, { i32, i8* }* }* }* %frame_pointer, i32 0, i32 0
%fp_addr = load { i32, { i32, i8* }* }*, { i32, { i32, i8* }* }** %fp_addr_in_sl
%root = getelementptr { i32, { i32, i8* }* }, { i32, { i32, i8* }* }* %fp_addr, i32 0, i32 1
%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 5, i32* %Element
%Element1 = getelementptr { i32, i8* }, { i32, i8* }* %record_init, i32 0, i32 1
store i8* null, i8** %Element1
store { i32, i8* }* %record_init, { i32, i8* }** %root
ret void
}

declare noalias i8* @malloc(i32)
16 changes: 9 additions & 7 deletions src/link.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,21 @@ let rec trans_dec (
A.VarDec { name; typ; init; pos; escape; order }
) =
let rhs_type = trans_exp (v_env, t_env, init) in
if !escape
then
begin
escape_vars := rhs_type :: !escape_vars;
order := !current_counter;
increase_counter()
end;
let add_esc_type esc_type =
if !escape
then
escape_vars := esc_type :: !escape_vars;
order := !current_counter;
increase_counter()
in
match typ with
Some (s, p) ->
begin
match S.look(t_env, s) with
| Some lhs_type ->
let new_entry = E.VarEntry{ty = lhs_type; access = Translate.dummy_access} in
let new_v_env = S.enter(v_env, name, new_entry) in
add_esc_type lhs_type;
{
v_env = new_v_env;
t_env = t_env;
Expand All @@ -83,6 +84,7 @@ let rec trans_dec (
end
| None ->
let new_entry = E.VarEntry{ty = rhs_type; access = Translate.dummy_access} in
add_esc_type rhs_type;
{
v_env = S.enter(v_env, name, new_entry);
t_env = t_env
Expand Down
Loading

0 comments on commit 7884bb6

Please sign in to comment.