changeset 20:d71b36deaa9d

fix div in intel64
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 28 Oct 2016 17:50:32 +0900
parents 0a1507c12f9a
children 06763fed18e4
files Makefile s-code-intel64-mac-r.c s-code-intel64-mac.c s-yacc.y
diffstat 4 files changed, 51 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Fri Oct 28 15:35:31 2016 +0900
+++ b/Makefile	Fri Oct 28 17:50:32 2016 +0900
@@ -1,12 +1,12 @@
-TEST = s-imac64-r
+TEST = s-imac64
 
 CC = clang
 # CFLAGS = -g -O  -Wall
 CFLAGS = -g -O0  -Wall
 YYFLAGS = -v
 YACC = bison
-# COMPILER = s-trecompile.o s-token.o
-COMPILER = s-tree-compile.o s-token.o
+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
--- a/s-code-intel64-mac-r.c	Fri Oct 28 15:35:31 2016 +0900
+++ b/s-code-intel64-mac-r.c	Fri Oct 28 17:50:32 2016 +0900
@@ -162,7 +162,7 @@
 
 const int	MAX_REGISTER=10;
 
-static char    *reg_name[] = {"%rax","%rbx","%rcx","%rdx","%rsi", "%rdi", 
+static char    *reg_name[] = {"%rax","%rbx","%rcx","%rdx", "%rsi", "%rdi", 
                       "%r8", "%r9", "%r10", "%r12", "%r13", "%r14", "%r15",
                       "%rip", "%rbp", "%rsp" };
 
@@ -316,15 +316,36 @@
     char *orn;
     orn = emit_pop();
     if(op==O_DIV) {
-        //   crn -> rax
-        //   rdx !!!
-        printf("\tcltd\n");
-        printf("\tidivq %s\n",orn);
+        if (orn[2]=='a') {
+            printf("\tcltd\n");
+            printf("\tidivq %s\n",crn);
+            printf("\txchg %s,%%rax\n",crn);
+        } else if ( crn[2]=='a' ) {
+            printf("\txchg %s,%%rax\n",crn);
+            printf("\tcltd\n");
+            printf("\tidivq %s\n",orn);
+            printf("\txchg %s,%%rax\n",crn);
+        } else {
+            printf("\txchg %s,%%rax\n",orn);
+            printf("\tcltd\n");
+            printf("\tidivq %s\n",crn);
+            printf("\txchg %s,%%rax\n",crn);
+        }
     } else if(op==O_DIV_R) {
-        printf("\txchg %s,%%rax\n",crn);
-        printf("\tcltd\n");
-        printf("\tidivq %s\n",crn);
-        crn = "%rax";
+        if (crn[2]=='a') {
+            printf("\tcltd\n");
+            printf("\tidivq %s\n",orn);
+        } else if ( orn[2]=='a' ) {
+            printf("\txchg %s,%%rax\n",crn);
+            printf("\tcltd\n");
+            printf("\tidivq %s\n",crn);
+            printf("\txchg %s,%%rax\n",crn);
+        } else {
+            printf("\txchg %s,%%rax\n",crn);
+            printf("\tcltd\n");
+            printf("\tidivq %s\n",orn);
+            printf("\txchg %s,%%rax\n",crn);
+        }
     } else if(op==O_SUB) {
         printf("\t%s %s,%s\n",opcode[op],orn,crn);
         printf("\tnegq %s\n",crn);
--- a/s-code-intel64-mac.c	Fri Oct 28 15:35:31 2016 +0900
+++ b/s-code-intel64-mac.c	Fri Oct 28 17:50:32 2016 +0900
@@ -175,19 +175,22 @@
 void
 emit_calc(enum opcode op)
 {
-    if(op==O_DIV || op==O_DIV_R ) {
-    printf("\tmovq %%rax,%%rbx\n");
-    printf("\tpopq %%rax\n");
-    // printf("\tmovq $0,%%rdx\n");
-    printf("\tcltd\n");
-    printf("\tidivq %%rbx\n");
+    if(op==O_DIV ) {
+        printf("\tmovq %%rax,%%rbx\n");
+        printf("\tpopq %%rax\n");
+        printf("\tcltd\n");
+        printf("\tidivq %%rbx\n");
+    } else if( op==O_DIV_R ) {
+        printf("\tpopq %%rbx\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");
+        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]);
+        printf("\tpopq %%rbx\n");
+        printf("\t%s %%rbx,%%rax\n",opcode[op]);
     }
 }
 
--- a/s-yacc.y	Fri Oct 28 15:35:31 2016 +0900
+++ b/s-yacc.y	Fri Oct 28 17:50:32 2016 +0900
@@ -8,6 +8,7 @@
 #include "s-compile.h"
 
 void yyerror(char *);
+int yylex();
 
 typedef struct node {
     struct node *left;
@@ -16,6 +17,8 @@
     int value;
 } node,*nodeptr;
 
+static int optimize = 0;
+
 #define YYSTYPE nodeptr
 
 static node *new_node();
@@ -66,7 +69,7 @@
 node *right;
 {
     node *d;
-    if ((left  &&  left->type =='0') &&
+    if (optimize && (left  &&  left->type =='0') &&
         (right && right->type =='0')) {
 	switch(type) {
 	case '>':