Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions code_gen/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ LDFLAGS=`llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native
all: codegen

codegen: main.o error.o parser.o reader.o scanner.o semantics.o symbol_table.o token.o code_gen.o
# ${CC} main.o error.o parser.o reader.o scanner.o semantics.o symbol_table.o token.o
$(LD) main.o error.o parser.o reader.o scanner.o semantics.o symbol_table.o token.o code_gen.o $(LDFLAGS) -o codegen.out

codegen.bc: codegen
./codegen.out tests/test-array.src
./codegen.out tests/test-loop.src

codegen.ll: codegen.bc
llvm-dis codegen.bc
Expand Down
Binary file removed code_gen/codegen.bc
Binary file not shown.
1 change: 0 additions & 1 deletion code_gen/codegen.ll
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ define void @putstring(i8* %val) {
putstring:
%putstring1 = call i32 (...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @format_str.3, i32 0, i32 0), i8* %val)
ret void
}

define void @square_array(i32*, i32*) {
square_array:
Expand Down
8 changes: 4 additions & 4 deletions code_gen/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void throw_error(ErrorCode code, int lineNo, int columnNo) {
}

void missing_token(TokenType type, int lineNo, int columnNo) {
printf("%d:%d: Parse Erorr! Expecting %s\n", lineNo, columnNo, print_token_type(type));
printf("%d:%d: Parse Error! Expecting %s\n", lineNo, columnNo, print_token_type(type));
exit(0);
}

Expand All @@ -76,15 +76,15 @@ Comment the function body to print logging info
Uncomment to silence
**/
void assert_scanner(const char *mesg) {
// printf("%s", mesg);
printf("%s", mesg);
}

void assert_parser(const char *mesg) {
// printf("%s", mesg);
printf("%s", mesg);
}

void assert_symbol_table(const char *mesg) {
// printf("%s", mesg);
printf("%s", mesg);
}

void assert_semantics(const char *mesg) {
Expand Down
19 changes: 19 additions & 0 deletions code_gen/llvm-tests/recursion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Run with llvm-gcc -S -emit-llvm recursion.c for LLVM IR code
#include <stdio.h>

void recursion(int level) {
if (level >= 3) {
printf("It's time to return\n");
return;
}
// } else {
printf("Recursion level: %d\n", level);
level++;
recursion(level);
// return;
// }
}

int main() {
recursion(0);
}
51 changes: 51 additions & 0 deletions code_gen/llvm-tests/recursion.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; ModuleID = 'recursion.c'
source_filename = "recursion.c"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.13.0"

@.str = private unnamed_addr constant [21 x i8] c"It's time to return\0A\00", align 1
@.str.1 = private unnamed_addr constant [21 x i8] c"Recursion level: %d\0A\00", align 1

; Function Attrs: noinline nounwind optnone ssp uwtable
define void @recursion(i32) #0 {
%2 = alloca i32, align 4
store i32 %0, i32* %2, align 4
%3 = load i32, i32* %2, align 4
%4 = icmp sge i32 %3, 3
br i1 %4, label %5, label %7

; <label>:5: ; preds = %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i32 0, i32 0))
br label %13

; <label>:7: ; preds = %1
%8 = load i32, i32* %2, align 4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i32 0, i32 0), i32 %8)
%10 = load i32, i32* %2, align 4
%11 = add nsw i32 %10, 1
store i32 %11, i32* %2, align 4
%12 = load i32, i32* %2, align 4
call void @recursion(i32 %12)
br label %13

; <label>:13: ; preds = %7, %5
ret void
}

declare i32 @printf(i8*, ...) #1

; Function Attrs: noinline nounwind optnone ssp uwtable
define i32 @main() #0 {
call void @recursion(i32 0)
ret i32 0
}

attributes #0 = { noinline nounwind optnone ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{!"Apple LLVM version 9.1.0 (clang-902.0.39.2)"}
13 changes: 10 additions & 3 deletions code_gen/llvm-tests/test-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ int global_i;
float global_f;
int i;

void square_array(int array[10], int result[10], int param_i, char *s) {
void square_array(int array[10], int *result, int param_i, char *s, float *param_f) {
int i;
i = 0;
global_i = 0;
global_f = 2.5;
i = global_i;
float f;
*param_f = global_f;
f = *param_f;

for ( ; i < 10; i = i + 1)
result[i] = array[i] * array[i];
i = param_i + 1;
Expand All @@ -20,12 +27,12 @@ int main() {
int numbers[10];
int res[10];
// int i;
float f;
char *s;
i = 0;
for (; i < 10; i = i + 1)
numbers[i] = i;
square_array(numbers, res, 0, s);
float f;
square_array(numbers, res, 0, s, &f);
i = 0;
for (; i < 10; i = i + 1) {
printf("%d\n", res[i]);
Expand Down
117 changes: 65 additions & 52 deletions code_gen/llvm-tests/test-array.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,75 @@ source_filename = "test-array.c"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.13.0"

@global_i = common global i32 0, align 4
@global_f = common global float 0.000000e+00, align 4
@i = common global i32 0, align 4
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
@.str.1 = private unnamed_addr constant [2 x i8] c" \00", align 1
@global_numbers = common global [10 x i32] zeroinitializer, align 16
@global_res = common global [10 x i32] zeroinitializer, align 16
@global_i = common global i32 0, align 4
@global_f = common global float 0.000000e+00, align 4

; Function Attrs: noinline nounwind optnone ssp uwtable
define void @square_array(i32*, i32*, i32, i8*) #0 {
%5 = alloca i32*, align 8
define void @square_array(i32*, i32*, i32, i8*, float*) #0 {
%6 = alloca i32*, align 8
%7 = alloca i32, align 4
%8 = alloca i8*, align 8
%9 = alloca i32, align 4
store i32* %0, i32** %5, align 8
store i32* %1, i32** %6, align 8
store i32 %2, i32* %7, align 4
store i8* %3, i8** %8, align 8
store i32 0, i32* %9, align 4
br label %10

; <label>:10: ; preds = %29, %4
%11 = load i32, i32* %9, align 4
%12 = icmp slt i32 %11, 10
br i1 %12, label %13, label %32

; <label>:13: ; preds = %10
%14 = load i32*, i32** %5, align 8
%15 = load i32, i32* %9, align 4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %14, i64 %16
%18 = load i32, i32* %17, align 4
%19 = load i32*, i32** %5, align 8
%20 = load i32, i32* %9, align 4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22, align 4
%24 = mul nsw i32 %18, %23
%25 = load i32*, i32** %6, align 8
%26 = load i32, i32* %9, align 4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
store i32 %24, i32* %28, align 4
br label %29

; <label>:29: ; preds = %13
%30 = load i32, i32* %9, align 4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9, align 4
br label %10

; <label>:32: ; preds = %10
%33 = load i32, i32* %7, align 4
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9, align 4
%7 = alloca i32*, align 8
%8 = alloca i32, align 4
%9 = alloca i8*, align 8
%10 = alloca float*, align 8
%11 = alloca i32, align 4
%12 = alloca float, align 4
store i32* %0, i32** %6, align 8
store i32* %1, i32** %7, align 8
store i32 %2, i32* %8, align 4
store i8* %3, i8** %9, align 8
store float* %4, float** %10, align 8
store i32 0, i32* %11, align 4
store i32 0, i32* @global_i, align 4
store float 2.500000e+00, float* @global_f, align 4
%13 = load i32, i32* @global_i, align 4
store i32 %13, i32* %11, align 4
%14 = load float, float* @global_f, align 4
%15 = load float*, float** %10, align 8
store float %14, float* %15, align 4
%16 = load float*, float** %10, align 8
%17 = load float, float* %16, align 4
store float %17, float* %12, align 4
br label %18

; <label>:18: ; preds = %37, %5
%19 = load i32, i32* %11, align 4
%20 = icmp slt i32 %19, 10
br i1 %20, label %21, label %40

; <label>:21: ; preds = %18
%22 = load i32*, i32** %6, align 8
%23 = load i32, i32* %11, align 4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
%26 = load i32, i32* %25, align 4
%27 = load i32*, i32** %6, align 8
%28 = load i32, i32* %11, align 4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30, align 4
%32 = mul nsw i32 %26, %31
%33 = load i32*, i32** %7, align 8
%34 = load i32, i32* %11, align 4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
store i32 %32, i32* %36, align 4
br label %37

; <label>:37: ; preds = %21
%38 = load i32, i32* %11, align 4
%39 = add nsw i32 %38, 1
store i32 %39, i32* %11, align 4
br label %18

; <label>:40: ; preds = %18
%41 = load i32, i32* %8, align 4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %11, align 4
ret void
}

Expand All @@ -67,8 +80,8 @@ define i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca [10 x i32], align 16
%3 = alloca [10 x i32], align 16
%4 = alloca float, align 4
%5 = alloca i8*, align 8
%4 = alloca i8*, align 8
%5 = alloca float, align 4
store i32 0, i32* %1, align 4
store i32 0, i32* @i, align 4
br label %6
Expand All @@ -95,8 +108,8 @@ define i32 @main() #0 {
; <label>:17: ; preds = %6
%18 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i32 0, i32 0
%19 = getelementptr inbounds [10 x i32], [10 x i32]* %3, i32 0, i32 0
%20 = load i8*, i8** %5, align 8
call void @square_array(i32* %18, i32* %19, i32 0, i8* %20)
%20 = load i8*, i8** %4, align 8
call void @square_array(i32* %18, i32* %19, i32 0, i8* %20, float* %5)
store i32 0, i32* @i, align 4
br label %21

Expand Down
Loading