Mercurial > hg > Members > kono > compiler-examples
changeset 18:230d5ca49f9f
fix
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 28 Oct 2016 15:16:55 +0900 |
parents | e5c8532ab38d |
children | 0a1507c12f9a |
files | Makefile s-code-intel64-mac-r.c s-code-intel64-mac.c s-code-llvm.c |
diffstat | 4 files changed, 71 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Fri Oct 18 15:51:35 2013 +0900 +++ b/Makefile Fri Oct 28 15:16:55 2016 +0900 @@ -1,15 +1,18 @@ -TEST = s-imac64 +TEST = s-imac64-r CC = clang # CFLAGS = -g -O -Wall CFLAGS = -g -O0 -Wall YYFLAGS = -v -# COMPILER = s-compile.o s-token.o -COMPILER = s-tree-compile.o s-token.o +YACC = bison +COMPILER = s-compile.o s-token.o +# COMPILER = s-tree-compile.o s-token.o # COMPILER = s-yacc.o s-token.o # TARGET = token calc s-calc s-prefix s-rpn s-09 s-intel s-intel-r s-sparc s-rs6k s-m68k s-ppc s-imac -TARGET = token calc s-calc s-calc-left s-prefix s-rpn s-intel s-intel-r s-sparc s-ppc s-imac s-imac64 # s-imac64-r s-llvm +TARGET = token calc s-calc s-calc-left s-prefix s-rpn s-intel s-intel-r s-sparc s-ppc s-imac s-imac64 s-imac64-r # s-llvm +TARGET = s-llvm + all: $(TARGET) token: token.o s-token.o @@ -57,7 +60,8 @@ $(CC) $(CFLAGS) -o $@ $^ s-yacc.o: s-yacc.y $(YACC) $(YYFLAGS) s-yacc.y - mv y.tab.c s-yacc.c + # mv y.tab.c s-yacc.c + mv s-yacc.tab.c s-yacc.c $(CC) -c s-yacc.c test: $(TEST) @@ -65,6 +69,11 @@ $(CC) s-output.s ./a.out +testll: $(TEST) + ./$(TEST) < s-input.txt > s-output.ll + $(CC) s-output.ll + ./a.out + MC09EMU = ../emulator/util09/v09 MC09ASM = ../emulator/util09/a09 test-09: s-09 @@ -81,3 +90,23 @@ # DO NOT DELETE +s-calc.o: s-compile.h +s-code-09.o: s-compile.h +s-code-intel-mac.o: s-compile.h +s-code-intel-r.o: s-compile.h +s-code-intel.o: s-compile.h +s-code-intel64-mac-r.o: s-compile.h +s-code-intel64-mac.o: s-compile.h +s-code-llvm.o: s-compile.h +s-code-m68k.o: s-compile.h +s-code-ppc.o: s-compile.h +s-code-print.o: s-compile.h +s-code-rs6k-r.o: s-compile.h +s-code-rs6k.o: s-compile.h +s-code-sparc.o: s-compile.h +s-compile.o: s-compile.h +s-prefix.o: s-compile.h +s-token.o: s-compile.h +s-tree-compile.o: s-compile.h +s-yacc.o: s-compile.h +token.o: s-compile.h
--- a/s-code-intel64-mac-r.c Fri Oct 18 15:51:35 2013 +0900 +++ b/s-code-intel64-mac-r.c Fri Oct 28 15:16:55 2016 +0900 @@ -48,6 +48,7 @@ char *ending[] = { " movq %rax, %rdi\n", " call _print\n", + " movq $0, %rax\n", " leave\n", " ret\n", "LFE3:\n", @@ -161,11 +162,11 @@ const int MAX_REGISTER=10; -static char *reg_name[] = {"%rax","%rbx","%rcx","%rdx","%rsi", "%rdi", "%rbp", - "%r07", "%r08", "%r09", "%r10", "%r12", "%r13", "%r14", "%r15", - "%rsp"}; +static char *reg_name[] = {"%rax","%rbx","%rcx","%rdx","%rsi", "%rdi", + "%r8", "%r9", "%r10", "%r12", "%r13", "%r14", "%r15", + "%rip", "%rbp", "%rsp" }; -static char * +char * regster(i) int i; { @@ -190,7 +191,7 @@ regs[i]=0; } -static int +int register_full() { int i; @@ -280,7 +281,7 @@ { int tmp = get_register(); printf("\tmovq _v@GOTPCREL(%%rip), %s\n",reg_name[tmp] ); - printf("\tmovq %s, %d(%s)\n" ,crn, d*8, reg_name[tmp]); + printf("\tmovq %s, %d(%s)\n" ,crn, assign*8, reg_name[tmp]); free_register(tmp); } @@ -289,7 +290,7 @@ { int tmp = get_register(); printf("\tmovq _v@GOTPCREL(%%rip), %s\n",reg_name[tmp]); - printf("\tmovq %d(%s),%s\n" ,reg_name[tmp],d*8,crn); + printf("\tmovq %d(%s),%s\n" ,d*8, reg_name[tmp],crn); free_register(tmp); } @@ -320,10 +321,10 @@ printf("\tcltd\n"); printf("\tidivq %s\n",orn); } else if(op==O_SUB) { - printf("\t%s %s,%s\n",orn,opcode[op],crn); + printf("\t%s %s,%s\n",opcode[op],orn,crn); printf("\tnegq %s\n",crn); } else { - printf("\t%s %s,%s\n",orn,opcode[op],crn); + printf("\t%s %s,%s\n",opcode[op],orn,crn); } } @@ -364,6 +365,7 @@ for(iptr=intro;*iptr;iptr++) { printf("%s",*iptr); } + emit_init(); } void
--- a/s-code-intel64-mac.c Fri Oct 18 15:51:35 2013 +0900 +++ b/s-code-intel64-mac.c Fri Oct 28 15:16:55 2016 +0900 @@ -3,7 +3,7 @@ $Id$ */ - #include "s-compile.h" +#include "s-compile.h" int label = 0; char *comments = "#####"; @@ -48,6 +48,7 @@ char *ending[] = { " movq %rax, %rdi\n", " call _print\n", + " movq $0,%rax\n", " leave\n", " ret\n", "LFE3:\n",
--- a/s-code-llvm.c Fri Oct 18 15:51:35 2013 +0900 +++ b/s-code-llvm.c Fri Oct 28 15:16:55 2016 +0900 @@ -10,7 +10,7 @@ static char *intro[] = { "target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128\"\n", - "target triple = \"x86_64-apple-macosx10.8.0\"\n", + "target triple = \"x86_64-apple-macosx10.10.0\"\n", "@.str = private unnamed_addr constant [5 x i8] c\"\0A = \00\", align 1\n", "define i32 @main() nounwind uwtable ssp {\n", "entry:\n", @@ -22,12 +22,26 @@ " ret i32 0\n", "}\n", "declare i32 @printf(i8*, ...)\n", + "; Function Attrs: nounwind ssp uwtable", + "define i32 @main() #0 {", + " %1 = load i32* @a, align 4", + " %2 = load i32* @b, align 4", + " %3 = add nsw i32 %1, %2", + " call void @print(i32 %3)", + " ret i32 0", + "}", + "", + "attributes #0 = { nounwind ssp uwtable \"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\" \"stack-protector-buffer-size\"=\"8\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }", + "attributes #1 = { \"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\" \"stack-protector-buffer-size\"=\"8\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }", + "", + "!llvm.ident = !{!0}", + "", + "!0 = metadata !{metadata !\"Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)\"}", NULL }; static int creg; /* current register */ static int lreg; /* operand register */ -static int dreg; /* temporary register */ static int xreg; /* operand register */ static int reg_sp; /* REGister Stack-Pointer */ @@ -55,6 +69,7 @@ regs[i]=0; } +/* static int register_full() { @@ -66,6 +81,7 @@ } return 1; } +*/ void emit_init() @@ -130,8 +146,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); + 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); free_register(tmp); } @@ -141,7 +157,7 @@ { 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 = load i32* %%%d, align 4\n", creg, tmp); free_register(tmp); } @@ -168,11 +184,11 @@ int oreg; oreg = emit_pop(); if(op==O_DIV) { - printf("\t%%%d = %s %%%d, %%%d\n",creg,opcode[op],oreg); + printf("\t%%%d = %s %%%d, %%%d\n",creg,opcode[op],oreg,oreg); } else if(op==O_SUB) { - printf("\t%%%d = %s %%%d, %%%d\n",creg,opcode[op],oreg); + printf("\t%%%d = %s %%%d, %%%d\n",creg,opcode[op],oreg,oreg); } else { - printf("\t%%%d = %s %%%d, %%%d\n",creg,opcode[op],oreg); + printf("\t%%%d = %s %%%d, %%%d\n",creg,opcode[op],oreg,oreg); } }