Mercurial > hg > Members > kono > compiler-examples
changeset 8:2735d0b637b6
Intel Mac version
author | kono |
---|---|
date | Fri, 09 Nov 2007 15:33:30 +0900 |
parents | fbb9cf5e483f |
children | bd295bb268a4 |
files | .gdbinit .gdbinit.i386 .gdbinit.ppc Makefile s-code-intel-mac.c |
diffstat | 5 files changed, 238 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/.gdbinit Fri Nov 17 18:31:08 2006 +0900 +++ b/.gdbinit Fri Nov 09 15:33:30 2007 +0900 @@ -1,38 +1,13 @@ -b expr -b aexpr -b mexpr -b term - define regs -printf "pc =%08x lr =%08x r0 =%08x r1 =%08x r3= %08x r4= %08x\n",$pc,$lr,$r0,$r1,$r3,$r4 -printf "r10=%08x r11=%08x r12=%08x r13=%08x r14=%08x r15=%08x\n",$r10,$r11,$r12,$r13,$r14,$r15 -end -define fregs -printf "f1=%g f2=%g f3=%g f4=%g f5=%g f6=%g\n",$f1,$f2,$f3,$f4,$f5,$f6 -printf "f10=%g f11=%g f12=%g f13=%g f14=%g f15=%g\n",$f10,$f11,$f12,$f13,$f14,$f15 -end -define allreg -printf "pc =%08x lr =%08x r0 =%08x r1 =%08x r3= %08x r4= %08x\n",$pc,$lr,$r0,$r1,$r3,$r4 -printf "r5=%08x r6=%08x r7=%08x r8=%08x r9=%08x\n",$r5,$r6,$r7,$r8,$r9 -printf "r10=%08x r11=%08x r12=%08x r13=%08x r14=%08x r15=%08x\n",$r10,$r11,$r12,$r13,$r14,$r15 -printf "r16=%08x r17=%08x r18=%08x r19=%08x r20=%08x r21=%08x\n",$r10,$r11,$r12,$r13,$r14,$r15 -printf "f1=%g f2=%g f3=%g f4=%g f5=%g f6=%g\n",$f1,$f2,$f3,$f4,$f5,$f6 -printf "f10=%g f11=%g f12=%g f13=%g f14=%g f15=%g\n",$f10,$f11,$f12,$f13,$f14,$f15 -printf "f20=%g f21=%g f22=%g f23=%g f24=%g f25=%g\n",$f20,$f21,$f22,$f23,$f24,$f25 -end -define sh -regs -x/20i $pc-36 +call (void)printf("eax=%08x ebx=%08x ecx=%08x edx=%08x\nesi=%08x edi=%08x ebp=%08x esp=%08x eip=%08x\n",$eax,$ebx,$ecx,$edx,$esi,$edi,$ebp,$esp,$eip) end define si stepi regs -x/1i $pc +x/1i $eip end define ni nexti regs -x/1i $pc +x/1i $eip end -# b errmsg -tb main
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gdbinit.i386 Fri Nov 09 15:33:30 2007 +0900 @@ -0,0 +1,13 @@ +define regs +call (void)printf("eax=%08x ebx=%08x ecx=%08x edx=%08x\nesi=%08x edi=%08x ebp=%08x esp=%08x eip=%08x\n",$eax,$ebx,$ecx,$edx,$esi,$edi,$ebp,$esp,$eip) +end +define si +stepi +regs +x/1i $eip +end +define ni +nexti +regs +x/1i $eip +end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gdbinit.ppc Fri Nov 09 15:33:30 2007 +0900 @@ -0,0 +1,38 @@ +b expr +b aexpr +b mexpr +b term + +define regs +printf "pc =%08x lr =%08x r0 =%08x r1 =%08x r3= %08x r4= %08x\n",$pc,$lr,$r0,$r1,$r3,$r4 +printf "r10=%08x r11=%08x r12=%08x r13=%08x r14=%08x r15=%08x\n",$r10,$r11,$r12,$r13,$r14,$r15 +end +define fregs +printf "f1=%g f2=%g f3=%g f4=%g f5=%g f6=%g\n",$f1,$f2,$f3,$f4,$f5,$f6 +printf "f10=%g f11=%g f12=%g f13=%g f14=%g f15=%g\n",$f10,$f11,$f12,$f13,$f14,$f15 +end +define allreg +printf "pc =%08x lr =%08x r0 =%08x r1 =%08x r3= %08x r4= %08x\n",$pc,$lr,$r0,$r1,$r3,$r4 +printf "r5=%08x r6=%08x r7=%08x r8=%08x r9=%08x\n",$r5,$r6,$r7,$r8,$r9 +printf "r10=%08x r11=%08x r12=%08x r13=%08x r14=%08x r15=%08x\n",$r10,$r11,$r12,$r13,$r14,$r15 +printf "r16=%08x r17=%08x r18=%08x r19=%08x r20=%08x r21=%08x\n",$r10,$r11,$r12,$r13,$r14,$r15 +printf "f1=%g f2=%g f3=%g f4=%g f5=%g f6=%g\n",$f1,$f2,$f3,$f4,$f5,$f6 +printf "f10=%g f11=%g f12=%g f13=%g f14=%g f15=%g\n",$f10,$f11,$f12,$f13,$f14,$f15 +printf "f20=%g f21=%g f22=%g f23=%g f24=%g f25=%g\n",$f20,$f21,$f22,$f23,$f24,$f25 +end +define sh +regs +x/20i $pc-36 +end +define si +stepi +regs +x/1i $pc +end +define ni +nexti +regs +x/1i $pc +end +# b errmsg +tb main
--- a/Makefile Fri Nov 17 18:31:08 2006 +0900 +++ b/Makefile Fri Nov 09 15:33:30 2007 +0900 @@ -2,9 +2,10 @@ CC = gcc CFLAGS = -g -# COMPILER = s-compile.o s-token.o +YYFLAGS = -v +COMPILER = s-compile.o s-token.o # COMPILER = s-tree-compile.o s-token.o -COMPILER = s-yacc.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 all: $(TARGET) @@ -33,6 +34,8 @@ $(CC) $(CFLAGS) -o $@ $^ s-intel-r: $(COMPILER) s-code-intel-r.o $(CC) $(CFLAGS) -o $@ $^ +s-imac: $(COMPILER) s-code-intel-mac.o + $(CC) $(CFLAGS) -o $@ $^ s-sparc: $(COMPILER) s-code-sparc.o $(CC) $(CFLAGS) -o $@ $^ s-rs6k: $(COMPILER) s-code-rs6k.o @@ -42,7 +45,7 @@ s-m68k: $(COMPILER) s-code-m68k.o $(CC) $(CFLAGS) -o $@ $^ s-yacc.o: s-yacc.y - $(YACC) s-yacc.y + $(YACC) $(YYFLAGS) s-yacc.y mv y.tab.c s-yacc.c $(CC) -c s-yacc.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/s-code-intel-mac.c Fri Nov 09 15:33:30 2007 +0900 @@ -0,0 +1,178 @@ +/* + Very Simple Code Generator for Intel x86 + $Id$ + */ + +#include "s-compile.h" + +int label = 0; +char *comments = "#####"; + +static +char *intro[] = { +".globl _variable\n", +" .data\n", +" .align 2\n", +"_variable:\n", +" .space 512\n", +".globl _main\n", +"_main:\n", +" pushl %ebp\n", +" movl %esp, %ebp\n", +" pushl %ebx\n", +" subl $20, %esp\n", +" call ___i686.get_pc_thunk.bx\n", +"\"L00000000002$pb\":\n", +" leal _variable-\"L00000000002$pb\"(%ebx), %esi\n", + NULL +}; + +static +char *ending[] = { +" addl $20, %esp\n", +" popl %ebx\n", +" popl %ebp\n", +" ret\n", +"LC0:\n", +" .ascii \"%d\\12\\0\"\n", +" .text\n", +".globl _print\n", +"_print:\n", +" pushl %ebp\n", +" movl %esp, %ebp\n", +" pushl %ebx\n", +" subl $20, %esp\n", +" call ___i686.get_pc_thunk.bx\n", +"\"L00000000001$pb\":\n", +" movl %eax, 4(%esp)\n", +" leal LC0-\"L00000000001$pb\"(%ebx), %eax\n", +" movl %eax, (%esp)\n", +" call L_printf$stub\n", +" addl $20, %esp\n", +" popl %ebx\n", +" popl %ebp\n", +" ret\n", +" .section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5\n", +"L_printf$stub:\n", +" .indirect_symbol _printf\n", +" hlt ; hlt ; hlt ; hlt ; hlt\n", +" .subsections_via_symbols\n", +" .section __TEXT,__textcoal_nt,coalesced,pure_instructions\n", +".weak_definition ___i686.get_pc_thunk.bx\n", +".private_extern ___i686.get_pc_thunk.bx\n", +"___i686.get_pc_thunk.bx:\n", +" movl (%esp), %ebx\n", +" ret\n", + NULL +}; + +void +emit_push() +{ + printf("\tpushl %%eax\n"); +} + +void +emit_compare() +{ + printf("\tcmpl %%eax,(%%esp)\n"); + printf("\t%s %%al\n","setg"); + printf("\tmovzbl %%al,%%eax\n"); + printf("\taddl $4,%%esp\n"); +} + +void +emit_store(assign) +int assign; +{ + printf("\tmovl %%eax,%d(%%esi)\n",assign*4); +} + +static +char *opcode[] = { + "", + "subl", + "addl", + "imull", + "idiv", + "", + "", + "", + "", + "subl", + "idiv", +}; + +void +emit_calc(enum opcode op) +{ + if(op==O_DIV) { + printf("\tmovl %%eax,%%ebx\n"); + printf("\tpopl %%eax\n"); + printf("\tcltd\n"); + printf("\tidiv %%ebx\n",op); + } else if(op==O_SUB) { + printf("\tpopl %%ebx\n"); + printf("\t%s %%ebx,%%eax\n",opcode[op]); + printf("\tnegl %%eax\n"); + } else { + printf("\tpopl %%ebx\n"); + printf("\t%s %%ebx,%%eax\n",opcode[op]); + } +} + +void +emit_value(d) +int d; +{ + printf("\tmovl $%d,%%eax\n",d); +} + +void +emit_load(d) +int d; +{ + printf("\tmovl %d(%%esi),%%eax\n",d*4); +} + +void +emit_comment() +{ + if (before < ptr) { + putchar('#'); putchar('#'); putchar(' '); + while (before < ptr) { + if(*before && *before!='\n') { + putchar(*before); + } + before++; + } + putchar('\n'); + } +} + +void +emit_print() +{ + printf("\tcall _print\n"); +} + + +void +emit_intro() +{ + char **iptr; + for(iptr=intro;*iptr;iptr++) { + printf("%s",*iptr); + } +} + +void +emit_ending() +{ + char **iptr; + for(iptr=ending;*iptr;iptr++) { + printf("%s",*iptr); + } +} + +/* end */