diff mc-nop-386.c @ 23:e605560e6146

Fix breaking edx,ecx on function call
author kono
date Tue, 04 Feb 2003 12:17:26 +0900
parents 3fa41d00f658
children 1df0bf7eb5da
line wrap: on
line diff
--- a/mc-nop-386.c	Mon Feb 03 23:43:12 2003 +0900
+++ b/mc-nop-386.c	Tue Feb 04 12:17:26 2003 +0900
@@ -225,6 +225,19 @@
     return 1;    
 }
 
+static
+int
+free_register_count(void)
+{
+    int i,count;
+    count = 0;
+    for(i=0;i<MAX_REGISTER;i++) {
+	if (! regs[i]) count++;
+    }
+    return count;    
+}
+
+
 void 
 gexpr_init(void)
 {
@@ -257,8 +270,8 @@
 	    break;
 	}
     }
-    if (real_v == -1)
-	error(-1);
+    /* if (real_v == -1)
+	error(-1); */
     return real_v;
 }
 
@@ -801,7 +814,15 @@
 {
     int e2,e3,e4,e5,nargs,t;
     NMTBL *n;
-
+    int save,saved;
+    if (free_register_count()<1) {
+        for(save = 0;save==dreg||save==creg;save++);
+	printf("\tpushl %s\n",register_name(save,0));
+        saved = 1;
+    } else {
+        save = get_register();
+        saved = 0;
+    }
     e2 = cadr(e1);
     nargs = 0;
     for (e3 = caddr(e1); e3; e3 = cadr(e3)) {	
@@ -831,14 +852,26 @@
     }
     if (car(e2) == FNAME) {	
 	n=(NMTBL *)cadr(e2);
-	use_register(creg,REG_EAX,0);
+    } else {	
+        use_register(creg,REG_ECX,0);
+	g_expr(e2);
+    }
+
+    use_register(creg,REG_EAX,0);
+    use_register(dreg,REG_EDX,0);  /* will be destroyed */
+    use_register(save,REG_ECX,0);  /* will be destroyed */
+
+    if (car(e2) == FNAME) {	
 	printf("\tcall\t%s\n",n->nm);
-    } else {	
-	g_expr(e2);
-	use_register(creg,REG_EAX,0);
+    } else {
 	printf("\tcall\t*%s\n",register_name(creg,0));
     }
     if (nargs) printf("\taddl $%d,%%esp\n",size_of_int*nargs);
+    if (saved) {
+	printf("\tpopl %s\n",register_name(save,0));
+    } else {
+        free_register(save);
+    }
 }
 
 int
@@ -1110,19 +1143,16 @@
     case REGISTER:
             g_expr(e4);
 	    if (creg!=cadr(e2))
-		printf("\t%s %s,%s\n",op,register_name(creg,byte),register_name(cadr(e2),0));
+		printf("\tmovl %s,%s\n",register_name(creg,0),register_name(cadr(e2),0));
             return;
     }
-    g_expr(e4);
+    g_expr(e2);
     emit_push();
-    g_expr(e2);
-    e2 = emit_pop(0);
+    use_data_reg(creg,0);
+    g_expr(e4);
     if (byte) use_data_reg(creg,1);
-    printf("\t%s %s,(%s)\n",op,register_name(e2,byte),register_name(creg,0));
-    free_register(creg);
-    creg = e2;
-    regs[creg]=1;
-    /* printf("\tmovl %s,%s\n",register_name(e2,0),register_name(creg,0)); */
+    e2 = emit_pop(0);
+    printf("\t%s %s,(%s)\n",op,register_name(creg,byte),register_name(e2,0));
     return;
 }
 
@@ -1466,8 +1496,8 @@
     printf("\tpushl %%ebp\n");
     printf("\tmovl %%esp,%%ebp\n");
     printf("\tpushl %%ebx\n");
-    printf("\tpushl %%ecx\n");
-    printf("\tpushl %%edx\n");
+    printf("\tpushl %%ecx\n");  /* we don't need this now */
+    printf("\tpushl %%edx\n");  /* we don't need this now */
     if (REGISTERS6) {
 	printf("\tpushl %%edi\n");
 	printf("\tpushl %%esi\n");