# HG changeset patch # User kono # Date 1319784087 -32400 # Node ID c5c4771a6be257d6a6b14091654fa1ee48bd0952 # Parent c8e3b90ba2d95e26c301d67875c1ca8719c94688 working on intel64 (Lion) diff -r c8e3b90ba2d9 -r c5c4771a6be2 .gdbinit --- a/.gdbinit Fri Oct 28 14:57:21 2011 +0900 +++ b/.gdbinit Fri Oct 28 15:41:27 2011 +0900 @@ -1,13 +1,15 @@ +tb main 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) +call (void)printf("rax=%16lx rbx=%16lx\nrcx=%16lx rdx=%16lx\nrsi=%16lx rdi=%16lx rbp=%16lx \nrsp=%16lx pc=%08lx\n",$rax,$rbx,$rcx,$rdx,$rsi,$rdi,$rbp,$rsp,$pc) end define si stepi regs -x/1i $eip +x/1i $pc end define ni nexti regs -x/1i $eip +x/1i $pc end + diff -r c8e3b90ba2d9 -r c5c4771a6be2 Makefile --- a/Makefile Fri Oct 28 14:57:21 2011 +0900 +++ b/Makefile Fri Oct 28 15:41:27 2011 +0900 @@ -1,7 +1,7 @@ TEST = s-imac64 CC = gcc -CFLAGS = -g -O -Wall +CFLAGS = -g -Wall YYFLAGS = -v COMPILER = s-compile.o s-token.o # COMPILER = s-tree-compile.o s-token.o diff -r c8e3b90ba2d9 -r c5c4771a6be2 print.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/print.c Fri Oct 28 15:41:27 2011 +0900 @@ -0,0 +1,15 @@ +int printf(const char *,...); + +void print(int i) +{ + printf("%d\n",i); +} + +int a = 3; +int b = 4; + +int main() +{ + + print(a+b); +} diff -r c8e3b90ba2d9 -r c5c4771a6be2 s-code-intel64-mac.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/s-code-intel64-mac.c Fri Oct 28 15:41:27 2011 +0900 @@ -0,0 +1,240 @@ +/* + Very Simple Code Generator for Intel x86 + $Id$ + */ + + #include "s-compile.h" + +int label = 0; +char *comments = "#####"; +static +char *intro[] = { + " .cstring\n", + "LC0:\n", + " .ascii \"%d\\n\\0\"\n" + " .text\n", + ".globl _print\n", + "_print:\n", + "LFB2:\n", + " pushq %rbp\n", + "LCFI0:\n", + " movq %rsp, %rbp\n", + "LCFI1:\n", + " subq $16, %rsp\n", + "LCFI2:\n", + " movl %edi, -4(%rbp)\n", + " movl -4(%rbp), %esi\n", + " leaq LC0(%rip), %rdi\n", + " movl $0, %eax\n", + " call _printf\n", + " leave\n", + " ret\n", + "LFE2:\n", + ".globl _main\n", + "_main:\n", + "LFB3:\n", + " pushq %rbp\n", + "LCFI3:\n", + " movq %rsp, %rbp\n", + "LCFI4:\n", + " subq $16, %rsp\n", + "LCFI5:\n", + "\n", + "\n", + NULL +}; + +static +char *ending[] = { + " movq %rax, %rdi\n", + " call _print\n", + " leave\n", + " ret\n", + "LFE3:\n", + ".comm _v,96,5\n", + " .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support\n", + "EH_frame1:\n", + " .set L$set$0,LECIE1-LSCIE1\n", + " .long L$set$0\n", + "LSCIE1:\n", + " .long 0x0\n", + " .byte 0x1\n", + " .ascii \"zR\\0\"\n", + " .byte 0x1\n", + " .byte 0x78\n", + " .byte 0x10\n", + " .byte 0x1\n", + " .byte 0x10\n", + " .byte 0xc\n", + " .byte 0x7\n", + " .byte 0x8\n", + " .byte 0x90\n", + " .byte 0x1\n", + " .align 3\n", + "LECIE1:\n", + " .globl _print.eh\n", + "_print.eh:\n", + "LSFDE1:\n", + " .set L$set$1,LEFDE1-LASFDE1\n", + " .long L$set$1\n", + "LASFDE1:\n", + " .long LASFDE1-EH_frame1\n", + " .quad LFB2-.\n", + " .set L$set$2,LFE2-LFB2\n", + " .quad L$set$2\n", + " .byte 0x0\n", + " .byte 0x4\n", + " .set L$set$3,LCFI0-LFB2\n", + " .long L$set$3\n", + " .byte 0xe\n", + " .byte 0x10\n", + " .byte 0x86\n", + " .byte 0x2\n", + " .byte 0x4\n", + " .set L$set$4,LCFI1-LCFI0\n", + " .long L$set$4\n", + " .byte 0xd\n", + " .byte 0x6\n", + " .align 3\n", + "LEFDE1:\n", + " .globl _main.eh\n", + "_main.eh:\n", + "LSFDE3:\n", + " .set L$set$5,LEFDE3-LASFDE3\n", + " .long L$set$5\n", + "LASFDE3:\n", + " .long LASFDE3-EH_frame1\n", + " .quad LFB3-.\n", + " .set L$set$6,LFE3-LFB3\n", + " .quad L$set$6\n", + " .byte 0x0\n", + " .byte 0x4\n", + " .set L$set$7,LCFI3-LFB3\n", + " .long L$set$7\n", + " .byte 0xe\n", + " .byte 0x10\n", + " .byte 0x86\n", + " .byte 0x2\n", + " .byte 0x4\n", + " .set L$set$8,LCFI4-LCFI3\n", + " .long L$set$8\n", + " .byte 0xd\n", + " .byte 0x6\n", + " .align 3\n", + "LEFDE3:\n", + " .subsections_via_symbols\n", + NULL +}; + +void +emit_push() +{ + printf("\tpushq %%rax\n"); +} + +void +emit_compare() +{ + printf("\tcmpq %%rax,(%%rsp)\n"); + printf("\t%s %%al\n","setg"); + printf("\tmovzbq %%al,%%rax\n"); + printf("\taddq $8,%%rsp\n"); +} + +void +emit_store( int d ) +{ + printf("\tmovq _v@GOTPCREL(%%rip), %%rcx\n"); + printf("\tmovq %%rax, %d(%%rcx)\n" ,d*8); +} + +void +emit_load(int d) +{ + printf("\tmovq _v@GOTPCREL(%%rip), %%rcx\n"); + printf("\tmovq %d(%%rcx),%%rax\n" ,d*8); +} + + +static +char *opcode[] = { + "", + "subq", + "addq", + "imulq", + "idivq", + "", + "", + "", + "", + "subq", + "idivq", +}; + +void +emit_calc(enum opcode op) +{ + if(op==O_DIV) { + printf("\tmovq %%rax,%%rbx\n"); + printf("\tpopq %%rax\n"); + printf("\tcltd\n"); + printf("\tidivq %%rbx\n"); + } else if(op==O_SUB) { + printf("\tpopq %%rbx\n"); + printf("\t%s %%rbx,%%rax\n",opcode[op]); + printf("\tnegq %%rax\n"); + } else { + printf("\tpopq %%rbx\n"); + printf("\t%s %%rbx,%%rax\n",opcode[op]); + } +} + +void +emit_value(d) +int d; +{ + printf("\tmovq $%d,%%rax\n",d); +} + +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("\tmovq %%rax, %%rdi\n"); + 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 */ diff -r c8e3b90ba2d9 -r c5c4771a6be2 s-compile.c --- a/s-compile.c Fri Oct 28 14:57:21 2011 +0900 +++ b/s-compile.c Fri Oct 28 15:41:27 2011 +0900 @@ -76,7 +76,6 @@ static void mexpr() { - int d; emit_comment(); term(); @@ -139,7 +138,6 @@ int main() { - int d; char buf[BUFSIZ]; emit_intro(); diff -r c8e3b90ba2d9 -r c5c4771a6be2 s-compile.h --- a/s-compile.h Fri Oct 28 14:57:21 2011 +0900 +++ b/s-compile.h Fri Oct 28 15:41:27 2011 +0900 @@ -38,4 +38,7 @@ extern void emit_comment(); extern void emit_intro(); extern void emit_ending(); +extern void emit_print(); +extern int token(); + diff -r c8e3b90ba2d9 -r c5c4771a6be2 s-token.c --- a/s-token.c Fri Oct 28 14:57:21 2011 +0900 +++ b/s-token.c Fri Oct 28 15:41:27 2011 +0900 @@ -40,7 +40,7 @@ break; } } - c && ptr--; + if (c!=0) ptr--; value = d; last_token = '0'; return last_token;