Mercurial > hg > Members > kono > compiler-examples
changeset 27:c4e2ca100ab9 default tip
llvm worked
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 25 Oct 2024 17:10:11 +0900 |
parents | 8550260c18fa |
children | |
files | CMakeLists.txt README.md s-code-llvm.c |
diffstat | 3 files changed, 62 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Fri Oct 18 16:06:51 2024 +0900 +++ b/CMakeLists.txt Fri Oct 25 17:10:11 2024 +0900 @@ -22,8 +22,8 @@ # add_executable(s-intel ${COMPILER} s-code-intel.c ) # add_executable(s-intel-r ${COMPILER} s-code-intel-r.c ) # add_executable(s-imac ${COMPILER} s-code-intel-mac.c ) -add_executable(s-imac64 ${COMPILER} s-code-intel64-mac.c ) -add_executable(s-imac64-r ${COMPILER} s-code-intel64-mac-r.c ) +# add_executable(s-imac64 ${COMPILER} s-code-intel64-mac.c ) +# add_executable(s-imac64-r ${COMPILER} s-code-intel64-mac-r.c ) add_executable(s-llvm ${COMPILER} s-code-llvm.c) # add_executable(s-sparc ${COMPILER} s-code-sparc.c ) # add_executable(s-rs6k ${COMPILER} s-code-rs6k.c )
--- a/README.md Fri Oct 18 16:06:51 2024 +0900 +++ b/README.md Fri Oct 25 17:10:11 2024 +0900 @@ -8,3 +8,12 @@ cmake -DCMAKE_MAKE_PROGRAM=make ``` +# run on lldb + +``` + lldb ./s-llvm + (lldb) b main + (lldb) process launch -i s-input.txt +``` + +
--- a/s-code-llvm.c Fri Oct 18 16:06:51 2024 +0900 +++ b/s-code-llvm.c Fri Oct 25 17:10:11 2024 +0900 @@ -12,13 +12,13 @@ "; ModuleID = 'tmp.c'\n", "source_filename = \"tmp.c\"\n", "target datalayout = \"e-m:o-i64:64-i128:128-n32:64-S128\"\n", - "target triple = \"arm64-apple-macosx14.0.0"\n", + "target triple = \"arm64-apple-macosx14.0.0\"\n", "\n", "@a = global i64 4, align 8\n", "@b = global i64 3, align 8\n", "@p = global i64 3, align 8\n", - "@.str = private unnamed_addr constant [5 x i8] c\"%ld\0A\00\", align 1\n", - "@v = common global [100 x i64] zeroinitializer, align 8\n", + "@.str = private unnamed_addr constant [5 x i8] c\"%ld\\0A\\00\", align 1\n", + "@variable = common global [100 x i32] zeroinitializer, align 8\n", "\n", "; Function Attrs: noinline nounwind optnone ssp uwtable(sync)\n", "define void @print(i64 noundef %0) #0 {\n", @@ -47,19 +47,8 @@ static char *ending[] = { - " store i64 %6, ptr @a, align 8\n", - " %7 = load i64, ptr @a, align 8\n", - " call void @print(i64 noundef %7)\n", - " %8 = load i64, ptr @p, align 8\n", - " %9 = getelementptr inbounds [100 x i64], ptr @v, i64 0, i64 %8\n", - " store i64 3, ptr %9, align 8\n", - " %10 = load i64, ptr @p, align 8\n", - " %11 = getelementptr inbounds [100 x i64], ptr @v, i64 0, i64 %10\n", - " %12 = load i64, ptr %11, align 8\n", - " call void @print(i64 noundef %12)\n", - " %13 = load i64, ptr @a, align 8\n", - " %14 = trunc i64 %13 to i32\n", - " ret i32 %14\n", + // " %7 = add i32 0, 0\n", + // " ret i32 %7\n", "}\n", "\n", "attributes #0 = { noinline nounwind optnone ssp uwtable(sync) \"frame-pointer\"=\"non-leaf\" \"min-legal-vector-width\"=\"0\" \"no-trapping-math\"=\"true\" \"probe-stack\"=\"__chkstk_darwin\" \"stack-protector-buffer-size\"=\"8\" \"target-cpu\"=\"apple-m1\" \"target-features\"=\"+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8a,+zcm,+zcz\" }\n", @@ -83,14 +72,14 @@ static int xreg; /* operand register */ static int reg_sp; /* REGister Stack-Pointer */ -#define MAX_MAX 20 +#define MAX_MAX 200000 static int regs[MAX_MAX]; /* 使われているレジスタを示すフラグ */ static int reg_stack[MAX_MAX]; /* 実際のレジスタの領域 */ -const int MAX_REGISTER=20; +const int MAX_REGISTER=100000; -static int -get_register() +static int +get_register() { /* 使われていないレジスタを調べる */ int i; for(i=0;i<MAX_REGISTER;i++) { @@ -102,9 +91,9 @@ return -1; /* 空いている場所がないなら、それを表す -1 を返す */ } -static void +static void free_register(int i) { /* いらなくなったレジスタを開放 */ - regs[i]=0; + // regs[i]=0; } /* @@ -113,25 +102,22 @@ { int i; for(i=0;i<MAX_REGISTER;i++) { - if (! regs[i]) { - return 0; + if (! regs[i]) { + return 0; } } - return 1; + return 1; } */ -void -emit_init() +void +emit_init() { - int i; - for(i=0;i<MAX_REGISTER;i++) regs[i]=0; - creg = get_register(); reg_sp = 0; } -static int -pop_register() +static int +pop_register() { /* レジスタから値を取り出す */ int i; @@ -142,22 +128,22 @@ return i; } else { lreg = i; - regs[i]=0; + // regs[i]=0; return lreg; } } -void +void emit_push() { - int new_reg; - new_reg = get_register(); - if(new_reg<0) { /* もうレジスタがない */ - printf("Compilation error: too complex expression\n"); - } else { + // int new_reg; + // new_reg = get_register(); + // if(new_reg<0) { /* もうレジスタがない */ + // printf("Compilation error: too complex expression\n"); + // } else { reg_stack[reg_sp++] = creg; /* push するかわりにレジスタを使う */ - creg = new_reg; - } + // creg = new_reg; + // } } int @@ -170,12 +156,15 @@ void -emit_compare() +emit_compare() { int oreg; oreg = emit_pop(); - printf("\t%%%d = icmp sgt i32 %%%d %%%d\n",creg,oreg,oreg); - printf("\t%%%d = zext i1 %%%d to i32\n",creg,creg); + creg = get_register(); + int creg0 = creg ; + printf("\t%%%d = icmp sgt i32 %%%d, %%%d\n",creg,oreg,oreg); + creg = get_register(); + printf("\t%%%d = zext i1 %%%d to i32\n",creg,creg0); } void @@ -183,8 +172,8 @@ { int tmp = get_register(); - printf("\t%%%d = getelementptr inbounds [48 x i32]* @variable, i32 0, i64 %d\n",tmp,assign); - printf("\tstore i32 %%%d i32* %%%d, align 4\n",creg,tmp); + printf("\t%%%d = getelementptr inbounds [48 x i32], ptr @variable, i32 0, i32 %d\n",tmp,assign); + printf("\tstore i32 %%%d, ptr %%%d, align 4\n",creg,tmp); free_register(tmp); } @@ -193,8 +182,9 @@ emit_load(int d) { int tmp = get_register(); - printf("\t%%%d = getelementptr inbounds [48 x i32]* @variable, i32 0, i64 %d\n", tmp, d); - printf("\t%%%d = load i32* %%%d, align 4\n", creg, tmp); + printf("\t%%%d = getelementptr inbounds [48 x i32], ptr @variable, i32 0, i32 %d\n", tmp, d); + creg = get_register(); + printf("\t%%%d = load i32, ptr %%%d, align 4\n", creg, tmp); free_register(tmp); } @@ -220,6 +210,7 @@ { int oreg; oreg = emit_pop(); + creg = get_register(); if(op==O_DIV) { printf("\t%%%d = %s %%%d, %%%d\n",creg,opcode[op],oreg,oreg); } else if(op==O_SUB) { @@ -232,7 +223,9 @@ void emit_value(int d) { - printf("\t%%%d = i32 %d\n",creg,d); + creg = get_register(); + printf("\t%%%d = add i32 0, %d\n",creg, d); + // printf("\t%%%d = %d\n",creg,d); } void @@ -253,7 +246,9 @@ void emit_print() { - printf("\t%%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str, i32 %%%d, i32 0))\n", creg); + // printf("\t%%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str, i32 %%%d, i32 0))\n", creg); + printf("\tcall i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %%%d)\n", creg); + creg = get_register(); } @@ -264,11 +259,17 @@ for(iptr=intro;*iptr;iptr++) { printf("%s",*iptr); } + int i ; + for(i=0;i<MAX_REGISTER;i++) regs[i]=0; + for(i=0;i<7;i++) regs[i]=1; } void emit_ending() { + creg = get_register(); + printf("\t%%%d = add i32 0, 0\n",creg); + printf("\tret i32 %%%d\n",creg); char **iptr; for(iptr=ending;*iptr;iptr++) { printf("%s",*iptr);