TC-L Samples
Starting from a typed AST, generate the LLVM IR instructions using the LLVM framework.
let
var answer := 42
in
answer := 51
end
$ tc --llvm-display the-answer-llvm.tig
; ModuleID = 'tc'
source_filename = "tc"
target triple = "i386-pc-linux-gnu"
; Function Attrs: inlinehint nounwind
declare void @tc_print(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_err(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_int(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_flush() #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_getchar() #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_ord(i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_chr(i32) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_size(i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_streq(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_strcmp(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_substring(i8*, i32, i32) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_concat(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_not(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_exit(i32) #0
; Function Attrs: nounwind
define void @tc_main() #1 {
entry__main:
%answer_17 = alloca i32, align 4
store i32 42, i32* %answer_17, align 4
store i32 51, i32* %answer_17, align 4
ret void
}
attributes #0 = { inlinehint nounwind }
attributes #1 = { nounwind }
$ echo $?
0
let
function add(x: int, y: int) : int = x + y
in
print_int(add(1,(add(2, 3)))); print("\n")
end
$ tc --llvm-display add-llvm.tig
; ModuleID = 'tc'
source_filename = "tc"
target triple = "i386-pc-linux-gnu"
@string = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
; Function Attrs: inlinehint nounwind
declare void @tc_print(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_err(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_int(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_flush() #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_getchar() #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_ord(i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_chr(i32) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_size(i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_streq(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_strcmp(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_substring(i8*, i32, i32) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_concat(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_not(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_exit(i32) #0
; Function Attrs: nounwind
define void @tc_main() #1 {
entry__main:
%call_add_19 = call i32 @add_19(i32 2, i32 3)
%call_add_191 = call i32 @add_19(i32 1, i32 %call_add_19)
call void @tc_print_int(i32 %call_add_191)
call void @tc_print(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @string, i32 0, i32 0))
ret void
}
; Function Attrs: nounwind
define internal i32 @add_19(i32 %x_17, i32 %y_18) #1 {
entry_add_19:
%y_182 = alloca i32, align 4
%x_171 = alloca i32, align 4
store i32 %x_17, i32* %x_171, align 4
store i32 %y_18, i32* %y_182, align 4
%x_173 = load i32, i32* %x_171, align 4
%y_184 = load i32, i32* %y_182, align 4
%addtmp = add i32 %x_173, %y_184
ret i32 %addtmp
}
attributes #0 = { inlinehint nounwind }
attributes #1 = { nounwind }
$ echo $?
0
Once your compiler is complete, you can produce an actual LLVM IR output and compile it with Clang to produce a real executable program.
$ tc --llvm-runtime-display --llvm-display add-llvm.tig
; ModuleID = 'tc'
source_filename = "tc"
target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128"
target triple = "i386-pc-linux-gnu"
%struct._IO_FILE = type { i32, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, %struct._IO_marker*, %struct._IO_FILE*, i32, i32, i32, i16, i8, [1 x i8], i8*, i64, %struct._IO_codecvt*, %struct._IO_wide_data*, %struct._IO_FILE*, i8*, i32, i32, [40 x i8] }
%struct._IO_marker = type opaque
%struct._IO_codecvt = type opaque
%struct._IO_wide_data = type opaque
@string = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
@.str = private unnamed_addr constant [29 x i8] c"chr: character out of range\0A\00", align 1
@stderr = external global %struct._IO_FILE*, align 4
@consts = internal global [512 x i8] zeroinitializer, align 1
@.str.1 = private unnamed_addr constant [36 x i8] c"substring: arguments out of bounds\0A\00", align 1
@stdin = external global %struct._IO_FILE*, align 4
@.str.2 = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@.str.3 = private unnamed_addr constant [3 x i8] c"%s\00", align 1
@.str.4 = private unnamed_addr constant [3 x i8] c"%d\00", align 1
@stdout = external global %struct._IO_FILE*, align 4
; Function Attrs: nounwind
define void @tc_main() #0 {
entry__main:
%call_add_19 = call i32 @add_19(i32 2, i32 3)
%call_add_191 = call i32 @add_19(i32 1, i32 %call_add_19)
call void @tc_print_int(i32 %call_add_191)
call void @tc_print(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @string, i32 0, i32 0))
ret void
}
; Function Attrs: nounwind
define internal i32 @add_19(i32 %x_17, i32 %y_18) #0 {
entry_add_19:
%y_182 = alloca i32, align 4
%x_171 = alloca i32, align 4
store i32 %x_17, i32* %x_171, align 4
store i32 %y_18, i32* %y_182, align 4
%x_173 = load i32, i32* %x_171, align 4
%y_184 = load i32, i32* %y_182, align 4
%addtmp = add i32 %x_173, %y_184
ret i32 %addtmp
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32* @tc_init_array(i32 noundef %0, i32 noundef %1) #1 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
%5 = alloca i32*, align 4
%6 = alloca i32, align 4
store i32 %0, i32* %3, align 4
store i32 %1, i32* %4, align 4
%7 = load i32, i32* %3, align 4
%8 = mul i32 %7, 4
%9 = call noalias i8* @malloc(i32 noundef %8) #0
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %5, align 4
store i32 0, i32* %6, align 4
br label %11
11: ; preds = %20, %2
%12 = load i32, i32* %6, align 4
%13 = load i32, i32* %3, align 4
%14 = icmp ult i32 %12, %13
br i1 %14, label %15, label %23
15: ; preds = %11
%16 = load i32, i32* %4, align 4
%17 = load i32*, i32** %5, align 4
%18 = load i32, i32* %6, align 4
%19 = getelementptr inbounds i32, i32* %17, i32 %18
store i32 %16, i32* %19, align 4
br label %20
20: ; preds = %15
%21 = load i32, i32* %6, align 4
%22 = add i32 %21, 1
store i32 %22, i32* %6, align 4
br label %11
23: ; preds = %11
%24 = load i32*, i32** %5, align 4
ret i32* %24
}
; Function Attrs: nounwind
declare noalias i8* @malloc(i32 noundef) #2
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @tc_not(i32 noundef %0) #1 {
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
%3 = load i32, i32* %2, align 4
%4 = icmp ne i32 %3, 0
%5 = xor i1 %4, true
%6 = zext i1 %5 to i32
ret i32 %6
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @tc_exit(i32 noundef %0) #1 {
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
%3 = load i32, i32* %2, align 4
call void @exit(i32 noundef %3) #6
unreachable
}
; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i8* @tc_chr(i32 noundef %0) #1 {
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
%3 = load i32, i32* %2, align 4
%4 = icmp sle i32 0, %3
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* %2, align 4
%7 = icmp sle i32 %6, 255
br i1 %7, label %11, label %8
8: ; preds = %5, %1
%9 = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 4
%10 = call i32 @fputs(i8* noundef getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i32 0, i32 0), %struct._IO_FILE* noundef %9)
call void @exit(i32 noundef 120) #6
unreachable
11: ; preds = %5
%12 = load i32, i32* %2, align 4
%13 = mul nsw i32 %12, 2
%14 = getelementptr inbounds i8, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @consts, i32 0, i32 0), i32 %13
ret i8* %14
}
declare i32 @fputs(i8* noundef, %struct._IO_FILE* noundef) #4
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i8* @tc_concat(i8* noundef %0, i8* noundef %1) #1 {
%3 = alloca i8*, align 4
%4 = alloca i8*, align 4
%5 = alloca i8*, align 4
%6 = alloca i32, align 4
%7 = alloca i32, align 4
%8 = alloca i32, align 4
%9 = alloca i32, align 4
%10 = alloca i8*, align 4
store i8* %0, i8** %4, align 4
store i8* %1, i8** %5, align 4
%11 = load i8*, i8** %4, align 4
%12 = call i32 @strlen(i8* noundef %11) #7
store i32 %12, i32* %6, align 4
%13 = load i8*, i8** %5, align 4
%14 = call i32 @strlen(i8* noundef %13) #7
store i32 %14, i32* %7, align 4
%15 = load i32, i32* %6, align 4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = load i8*, i8** %5, align 4
store i8* %18, i8** %3, align 4
br label %69
19: ; preds = %2
%20 = load i32, i32* %7, align 4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i8*, i8** %4, align 4
store i8* %23, i8** %3, align 4
br label %69
24: ; preds = %19
store i32 0, i32* %8, align 4
%25 = load i32, i32* %6, align 4
%26 = load i32, i32* %7, align 4
%27 = add i32 %25, %26
store i32 %27, i32* %9, align 4
%28 = load i32, i32* %9, align 4
%29 = add nsw i32 %28, 1
%30 = call noalias i8* @malloc(i32 noundef %29) #0
store i8* %30, i8** %10, align 4
store i32 0, i32* %8, align 4
br label %31
31: ; preds = %43, %24
%32 = load i32, i32* %8, align 4
%33 = load i32, i32* %6, align 4
%34 = icmp ult i32 %32, %33
br i1 %34, label %35, label %46
35: ; preds = %31
%36 = load i8*, i8** %4, align 4
%37 = load i32, i32* %8, align 4
%38 = getelementptr inbounds i8, i8* %36, i32 %37
%39 = load i8, i8* %38, align 1
%40 = load i8*, i8** %10, align 4
%41 = load i32, i32* %8, align 4
%42 = getelementptr inbounds i8, i8* %40, i32 %41
store i8 %39, i8* %42, align 1
br label %43
43: ; preds = %35
%44 = load i32, i32* %8, align 4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8, align 4
br label %31
46: ; preds = %31
store i32 0, i32* %8, align 4
br label %47
47: ; preds = %61, %46
%48 = load i32, i32* %8, align 4
%49 = load i32, i32* %7, align 4
%50 = icmp ult i32 %48, %49
br i1 %50, label %51, label %64
51: ; preds = %47
%52 = load i8*, i8** %5, align 4
%53 = load i32, i32* %8, align 4
%54 = getelementptr inbounds i8, i8* %52, i32 %53
%55 = load i8, i8* %54, align 1
%56 = load i8*, i8** %10, align 4
%57 = load i32, i32* %8, align 4
%58 = load i32, i32* %6, align 4
%59 = add i32 %57, %58
%60 = getelementptr inbounds i8, i8* %56, i32 %59
store i8 %55, i8* %60, align 1
br label %61
61: ; preds = %51
%62 = load i32, i32* %8, align 4
%63 = add nsw i32 %62, 1
store i32 %63, i32* %8, align 4
br label %47
64: ; preds = %47
%65 = load i8*, i8** %10, align 4
%66 = load i32, i32* %9, align 4
%67 = getelementptr inbounds i8, i8* %65, i32 %66
store i8 0, i8* %67, align 1
%68 = load i8*, i8** %10, align 4
store i8* %68, i8** %3, align 4
br label %69
69: ; preds = %64, %22, %17
%70 = load i8*, i8** %3, align 4
ret i8* %70
}
; Function Attrs: nounwind readonly willreturn
declare i32 @strlen(i8* noundef) #5
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @tc_ord(i8* noundef %0) #1 {
%2 = alloca i32, align 4
%3 = alloca i8*, align 4
%4 = alloca i32, align 4
store i8* %0, i8** %3, align 4
%5 = load i8*, i8** %3, align 4
%6 = call i32 @strlen(i8* noundef %5) #7
store i32 %6, i32* %4, align 4
%7 = load i32, i32* %4, align 4
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2, align 4
br label %15
10: ; preds = %1
%11 = load i8*, i8** %3, align 4
%12 = getelementptr inbounds i8, i8* %11, i32 0
%13 = load i8, i8* %12, align 1
%14 = sext i8 %13 to i32
store i32 %14, i32* %2, align 4
br label %15
15: ; preds = %10, %9
%16 = load i32, i32* %2, align 4
ret i32 %16
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @tc_size(i8* noundef %0) #1 {
%2 = alloca i8*, align 4
store i8* %0, i8** %2, align 4
%3 = load i8*, i8** %2, align 4
%4 = call i32 @strlen(i8* noundef %3) #7
ret i32 %4
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i8* @tc_substring(i8* noundef %0, i32 noundef %1, i32 noundef %2) #1 {
%4 = alloca i8*, align 4
%5 = alloca i8*, align 4
%6 = alloca i32, align 4
%7 = alloca i32, align 4
%8 = alloca i32, align 4
%9 = alloca i8*, align 4
%10 = alloca i32, align 4
store i8* %0, i8** %5, align 4
store i32 %1, i32* %6, align 4
store i32 %2, i32* %7, align 4
%11 = load i8*, i8** %5, align 4
%12 = call i32 @strlen(i8* noundef %11) #7
store i32 %12, i32* %8, align 4
%13 = load i32, i32* %6, align 4
%14 = icmp sle i32 0, %13
br i1 %14, label %15, label %24
15: ; preds = %3
%16 = load i32, i32* %7, align 4
%17 = icmp sle i32 0, %16
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i32, i32* %6, align 4
%20 = load i32, i32* %7, align 4
%21 = add nsw i32 %19, %20
%22 = load i32, i32* %8, align 4
%23 = icmp ule i32 %21, %22
br i1 %23, label %27, label %24
24: ; preds = %18, %15, %3
%25 = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 4
%26 = call i32 @fputs(i8* noundef getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i32 0, i32 0), %struct._IO_FILE* noundef %25)
call void @exit(i32 noundef 120) #6
unreachable
27: ; preds = %18
%28 = load i32, i32* %7, align 4
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i8*, i8** %5, align 4
%32 = load i32, i32* %6, align 4
%33 = getelementptr inbounds i8, i8* %31, i32 %32
%34 = load i8, i8* %33, align 1
%35 = sext i8 %34 to i32
%36 = mul nsw i32 %35, 2
%37 = getelementptr inbounds i8, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @consts, i32 0, i32 0), i32 %36
store i8* %37, i8** %4, align 4
br label %64
38: ; preds = %27
%39 = load i32, i32* %7, align 4
%40 = add nsw i32 %39, 1
%41 = call noalias i8* @malloc(i32 noundef %40) #0
store i8* %41, i8** %9, align 4
store i32 0, i32* %10, align 4
br label %42
42: ; preds = %56, %38
%43 = load i32, i32* %10, align 4
%44 = load i32, i32* %7, align 4
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %59
46: ; preds = %42
%47 = load i8*, i8** %5, align 4
%48 = load i32, i32* %6, align 4
%49 = load i32, i32* %10, align 4
%50 = add nsw i32 %48, %49
%51 = getelementptr inbounds i8, i8* %47, i32 %50
%52 = load i8, i8* %51, align 1
%53 = load i8*, i8** %9, align 4
%54 = load i32, i32* %10, align 4
%55 = getelementptr inbounds i8, i8* %53, i32 %54
store i8 %52, i8* %55, align 1
br label %56
56: ; preds = %46
%57 = load i32, i32* %10, align 4
%58 = add nsw i32 %57, 1
store i32 %58, i32* %10, align 4
br label %42
59: ; preds = %42
%60 = load i8*, i8** %9, align 4
%61 = load i32, i32* %7, align 4
%62 = getelementptr inbounds i8, i8* %60, i32 %61
store i8 0, i8* %62, align 1
%63 = load i8*, i8** %9, align 4
store i8* %63, i8** %4, align 4
br label %64
64: ; preds = %59, %30
%65 = load i8*, i8** %4, align 4
ret i8* %65
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @tc_strcmp(i8* noundef %0, i8* noundef %1) #1 {
%3 = alloca i8*, align 4
%4 = alloca i8*, align 4
store i8* %0, i8** %3, align 4
store i8* %1, i8** %4, align 4
%5 = load i8*, i8** %3, align 4
%6 = load i8*, i8** %4, align 4
%7 = call i32 @strcmp(i8* noundef %5, i8* noundef %6) #7
ret i32 %7
}
; Function Attrs: nounwind readonly willreturn
declare i32 @strcmp(i8* noundef, i8* noundef) #5
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @tc_streq(i8* noundef %0, i8* noundef %1) #1 {
%3 = alloca i8*, align 4
%4 = alloca i8*, align 4
store i8* %0, i8** %3, align 4
store i8* %1, i8** %4, align 4
%5 = load i8*, i8** %3, align 4
%6 = load i8*, i8** %4, align 4
%7 = call i32 @strcmp(i8* noundef %5, i8* noundef %6) #7
%8 = icmp eq i32 %7, 0
%9 = zext i1 %8 to i32
ret i32 %9
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i8* @tc_getchar() #1 {
%1 = alloca i8*, align 4
%2 = alloca i32, align 4
%3 = load %struct._IO_FILE*, %struct._IO_FILE** @stdin, align 4
%4 = call i32 @getc(%struct._IO_FILE* noundef %3)
store i32 %4, i32* %2, align 4
%5 = load i32, i32* %2, align 4
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %8
7: ; preds = %0
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i32 0, i32 0), i8** %1, align 4
br label %12
8: ; preds = %0
%9 = load i32, i32* %2, align 4
%10 = mul nsw i32 %9, 2
%11 = getelementptr inbounds i8, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @consts, i32 0, i32 0), i32 %10
store i8* %11, i8** %1, align 4
br label %12
12: ; preds = %8, %7
%13 = load i8*, i8** %1, align 4
ret i8* %13
}
declare i32 @getc(%struct._IO_FILE* noundef) #4
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @tc_print(i8* noundef %0) #1 {
%2 = alloca i8*, align 4
store i8* %0, i8** %2, align 4
%3 = load i8*, i8** %2, align 4
%4 = call i32 (i8*, ...) @printf(i8* noundef getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* noundef %3)
ret void
}
declare i32 @printf(i8* noundef, ...) #4
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @tc_print_err(i8* noundef %0) #1 {
%2 = alloca i8*, align 4
store i8* %0, i8** %2, align 4
%3 = load %struct._IO_FILE*, %struct._IO_FILE** @stderr, align 4
%4 = load i8*, i8** %2, align 4
%5 = call i32 (%struct._IO_FILE*, i8*, ...) @fprintf(%struct._IO_FILE* noundef %3, i8* noundef getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* noundef %4)
ret void
}
declare i32 @fprintf(%struct._IO_FILE* noundef, i8* noundef, ...) #4
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @tc_print_int(i32 noundef %0) #1 {
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
%3 = load i32, i32* %2, align 4
%4 = call i32 (i8*, ...) @printf(i8* noundef getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i32 0, i32 0), i32 noundef %3)
ret void
}
; Function Attrs: noinline nounwind optnone uwtable
define dso_local void @tc_flush() #1 {
%1 = load %struct._IO_FILE*, %struct._IO_FILE** @stdout, align 4
%2 = call i32 @fflush(%struct._IO_FILE* noundef %1)
ret void
}
declare i32 @fflush(%struct._IO_FILE* noundef) #4
; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @main() #1 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
store i32 0, i32* %1, align 4
store i32 0, i32* %2, align 4
br label %3
3: ; preds = %15, %0
%4 = load i32, i32* %2, align 4
%5 = icmp slt i32 %4, 512
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = load i32, i32* %2, align 4
%8 = sdiv i32 %7, 2
%9 = trunc i32 %8 to i8
%10 = load i32, i32* %2, align 4
%11 = getelementptr inbounds [512 x i8], [512 x i8]* @consts, i32 0, i32 %10
store i8 %9, i8* %11, align 1
%12 = load i32, i32* %2, align 4
%13 = add nsw i32 %12, 1
%14 = getelementptr inbounds [512 x i8], [512 x i8]* @consts, i32 0, i32 %13
store i8 0, i8* %14, align 1
br label %15
15: ; preds = %6
%16 = load i32, i32* %2, align 4
%17 = add nsw i32 %16, 2
store i32 %17, i32* %2, align 4
br label %3
18: ; preds = %3
call void bitcast (void ()* @tc_main to void (i32)*)(i32 noundef 0)
ret i32 0
}
attributes #0 = { nounwind }
attributes #1 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="i686" "target-features"="+cx8,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="i686" "target-features"="+cx8,+x87" "tune-cpu"="generic" }
attributes #3 = { noreturn nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="i686" "target-features"="+cx8,+x87" "tune-cpu"="generic" }
attributes #4 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="i686" "target-features"="+cx8,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind readonly willreturn "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="i686" "target-features"="+cx8,+x87" "tune-cpu"="generic" }
attributes #6 = { noreturn nounwind }
attributes #7 = { nounwind readonly willreturn }
!llvm.ident = !{!0}
!llvm.module.flags = !{!1, !2, !3, !4, !5, !6}
!0 = !{!"Debian clang version 14.0.6"}
!1 = !{i32 1, !"NumRegisterParameters", i32 0}
!2 = !{i32 1, !"wchar_size", i32 4}
!3 = !{i32 7, !"PIC Level", i32 2}
!4 = !{i32 7, !"PIE Level", i32 2}
!5 = !{i32 7, !"uwtable", i32 1}
!6 = !{i32 7, !"frame-pointer", i32 2}
$ echo $?
0
$ tc --llvm-runtime-display --llvm-display add-llvm.tig > add-llvm.ll
$ echo $?
0
$ clang -m32 -oadd-llvm add-llvm.ll
$ echo $?
0
$ ./add-llvm
6
$ echo $?
0
if 1
then 1
else 2
$ tc --llvm-display if.tig
; ModuleID = 'tc'
source_filename = "tc"
target triple = "i386-pc-linux-gnu"
; Function Attrs: inlinehint nounwind
declare void @tc_print(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_err(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_int(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_flush() #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_getchar() #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_ord(i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_chr(i32) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_size(i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_streq(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_strcmp(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_substring(i8*, i32, i32) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_concat(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_not(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_exit(i32) #0
; Function Attrs: nounwind
define void @tc_main() #1 {
entry__main:
br i1 true, label %then, label %else
then: ; preds = %entry__main
br label %ifend
else: ; preds = %entry__main
br label %ifend
ifend: ; preds = %else, %then
%iftmp = phi i32 [ 1, %then ], [ 2, %else ]
ret void
}
attributes #0 = { inlinehint nounwind }
attributes #1 = { nounwind }
$ echo $?
0
while 1
do
print("toto")
$ tc --llvm-display while.tig
; ModuleID = 'tc'
source_filename = "tc"
target triple = "i386-pc-linux-gnu"
@string = private unnamed_addr constant [5 x i8] c"toto\00", align 1
; Function Attrs: inlinehint nounwind
declare void @tc_print(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_err(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_int(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_flush() #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_getchar() #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_ord(i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_chr(i32) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_size(i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_streq(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_strcmp(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_substring(i8*, i32, i32) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_concat(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_not(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_exit(i32) #0
; Function Attrs: nounwind
define void @tc_main() #1 {
entry__main:
br label %test
test: ; preds = %body, %entry__main
br i1 true, label %body, label %afterloop
body: ; preds = %test
call void @tc_print(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @string, i32 0, i32 0))
br label %test
afterloop: ; preds = %test
ret void
}
attributes #0 = { inlinehint nounwind }
attributes #1 = { nounwind }
$ echo $?
0
let
type arrtype = array of int
var arr1 : arrtype := arrtype [10] of 0
in
arr1[2]
end
$ tc --llvm-display array.tig
; ModuleID = 'tc'
source_filename = "tc"
target triple = "i386-pc-linux-gnu"
; Function Attrs: inlinehint nounwind
declare void @tc_print(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_err(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_int(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_flush() #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_getchar() #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_ord(i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_chr(i32) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_size(i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_streq(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_strcmp(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_substring(i8*, i32, i32) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_concat(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_not(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_exit(i32) #0
; Function Attrs: nounwind
define void @tc_main() #1 {
entry__main:
%arr1_18 = alloca i32*, align 8
%init_array_call = call i32* @tc_init_array(i32 10, i32 0)
store i32* %init_array_call, i32** %arr1_18, align 8
%arr1_181 = load i32*, i32** %arr1_18, align 8
%subscriptptr = getelementptr i32, i32* %arr1_181, i32 2
%subscriptvar = load i32, i32* %subscriptptr, align 4
ret void
}
declare i32* @tc_init_array(i32, i32)
attributes #0 = { inlinehint nounwind }
attributes #1 = { nounwind }
$ echo $?
0
/* a record type and a record variable */
let
type rectype = {name : string, age : int}
var rec1 : rectype := rectype {name="Nobody", age=1000}
in
rec1.name := "Somebody";
rec1
end
$ tc --llvm-display record.tig
; ModuleID = 'tc'
source_filename = "tc"
target triple = "i386-pc-linux-gnu"
%0 = type { i8*, i32 }
@string = private unnamed_addr constant [7 x i8] c"Nobody\00", align 1
@string.1 = private unnamed_addr constant [9 x i8] c"Somebody\00", align 1
; Function Attrs: inlinehint nounwind
declare void @tc_print(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_err(i8*) #0
; Function Attrs: inlinehint nounwind
declare void @tc_print_int(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_flush() #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_getchar() #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_ord(i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_chr(i32) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_size(i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_streq(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_strcmp(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_substring(i8*, i32, i32) #0
; Function Attrs: inlinehint nounwind
declare i8* @tc_concat(i8*, i8*) #0
; Function Attrs: inlinehint nounwind
declare i32 @tc_not(i32) #0
; Function Attrs: inlinehint nounwind
declare void @tc_exit(i32) #0
; Function Attrs: nounwind
define void @tc_main() #1 {
entry__main:
%rec1_18 = alloca %0*, align 8
%malloccall = tail call i8* @malloc(i32 ptrtoint (%0* getelementptr (%0, %0* null, i32 1) to i32))
%malloccall2 = bitcast i8* %malloccall to %0*
%fieldinit_name = getelementptr inbounds %0, %0* %malloccall2, i32 0, i32 0
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @string, i32 0, i32 0), i8** %fieldinit_name, align 8
%fieldinit_age = getelementptr inbounds %0, %0* %malloccall2, i32 0, i32 1
store i32 1000, i32* %fieldinit_age, align 4
store %0* %malloccall2, %0** %rec1_18, align 8
%rec1_183 = load %0*, %0** %rec1_18, align 8
%fieldptr_name = getelementptr inbounds %0, %0* %rec1_183, i32 0, i32 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @string.1, i32 0, i32 0), i8** %fieldptr_name, align 8
%rec1_184 = load %0*, %0** %rec1_18, align 8
ret void
}
declare noalias i8* @malloc(i32)
attributes #0 = { inlinehint nounwind }
attributes #1 = { nounwind }
$ echo $?
0