changeset 2:b3f259ac34a1

args
author kono
date Thu, 13 Jan 2000 06:27:07 +0900
parents 0529f5abe9d0
children ca8e268dd7d4
files .gdbinit mc-nop-386.c mc-parse.c
diffstat 3 files changed, 83 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Thu Jan 13 02:48:11 2000 +0900
+++ b/.gdbinit	Thu Jan 13 06:27:07 2000 +0900
@@ -1,4 +1,4 @@
 tb main
 b error
 b errmsg
-r -s test/tmp7.c
+r -s test/tmp2.c
--- a/mc-nop-386.c	Thu Jan 13 02:48:11 2000 +0900
+++ b/mc-nop-386.c	Thu Jan 13 06:27:07 2000 +0900
@@ -63,6 +63,8 @@
 int error(int n);
 int size(int t);
 int list3(int e1, int e2, int e3);
+extern int scalar(int);
+extern int reverse0(int);
 
 #define TEXT_EMIT_MODE 0
 #define DATA_EMIT_MODE 1
@@ -761,21 +763,40 @@
     if (nargs) printf("\taddl $%d,%%esp\n",4*nargs);
 }
 
+int
+arg_size(int e3)
+{
+    int i,nargs,offset_list,e,t;
+
+    offset_list = 0;
+    /* we should use prototypes's type */
+    for (i = nargs = 0; e3;e3 =cadr(e3)) {
+	e = car(e3); t = caddr(e3);
+	if (i++ < MAX_REGISTER_VAR && scalar(t)) {
+	    offset_list = list3(REG_ESI+i,offset_list,e);
+	} else {
+	    nargs += (car(e3)==CHAR?int_size:size(t));
+	    offset_list = 
+		list3(-nargs+disp_offset+int_size,offset_list,e);
+	}
+    }
+    return offset_list;
+}
+
 void
 jump(int e1, int env)
 {
-    int i,e2,e3,e4,e5,nargs,regs;
+    int i,args,e2,e3,e4,e5,nargs,nargs0,regs;
     NMTBL *n,*code0;
     int new_disp;
     char *xrn;
 
+    /* We need three passes. Compute Stack size, Compute Arg, Copy it. */
     /* count number of args */
-    nargs = 0;
-    for (e3 = caddr(e1); e3;e3 =cadr(e3)) nargs++;
-    new_disp = -(nargs-MAX_REGISTER_VAR)*int_size;
-    if (new_disp > 0)
-	new_disp=0;
-/* printf("# new_disp %d,disp %d\n",new_disp,disp); */
+    args = caddr(e1);
+    reverse0(args);
+    nargs = arg_size(args);
+    new_disp = car(nargs);
     if (new_disp < disp) {
 	printf("\tlea %d(%%ebp),%%esp\n",new_disp+disp_offset);
     }
@@ -790,11 +811,11 @@
 	g_expr(e2);
 	emit_push();
     }
-    /* compute arguments */
-    regs = nargs-1;
-    i=0;
-    for (e3 = caddr(e1); e3;e3 =cadr(e3),i++) {
-	n=(NMTBL *)(e5=(cadr(e4 = car(e3))));
+    /* compute arguments in reverse order */
+    regs = 0;
+    nargs;
+    for (e3=nargs; e3;e3 =cadr(e3)) {
+	n=(NMTBL *)(e5=(cadr(e4 = caddr(e3))));
 	switch(car(e4)) {
 	case FNAME:
 	    printf("\tlea %s,%s\n",n->nm,crn);
@@ -804,75 +825,68 @@
 	    g_expr(e5);
 	    emit_push();
 	    break;
-/*
-printf("# cadr(e4) %d ==disp_offset+(-1-i)*int_size %d\n", cadr(e4), disp_offset+(-2-i)*int_size);
- */
 	case RLVAR:
 	case CRLVAR:
-	    if (env==0 && cadr(e4)==disp_offset+(-1-i)*int_size) {
-		/* Same positioned local variable. No need to copy */
-		reg_stack[reg_sp++] = -2;
-		--regs;
-		continue;
+	    if (env==0) {
+		 if (e5<0 && e5+disp_offset==nargs) {
+		/* The same positioned local variable. No need to copy */
+		    reg_stack[reg_sp++] = -2;
+		}
 	    }
 	case REGISTER:
-	    if (rname[cadr(e4)]==REG_ESI+regs) {
+	    if (rname[e5]==REG_ESI+regs) {
 		/* Same register variable. No need to copy */
-		reg_stack[reg_sp++] = cadr(e4);
-		--regs;
-		continue;
+		reg_stack[reg_sp++] = e5;
 	    }
+	    break;
 	default:
 	    g_expr(e4);
 	    emit_push();
 	}
-	--regs;
+	regs++;
     }
-    /* save frame pointer before jump in fuction */
-    if (fnptr->sc==FUNCTION) {
-	printf("\tmovl %%ebp,%d(%%ebp)\n",disp_offset);
-    } 
     if (env) {
-	/* change frame pointer */
+	/* change the frame pointer */
 	g_expr(env);
 	printf("\tmovl %s,%%ebp\n",crn);
     }
     /* copy arguments to destination environment if necessary */
-    for(i=0;i<nargs;i++) {
-    	if (i>=MAX_REGISTGER_VAR) {
+    reverse0(args);
+    for (e3=args; e3;e3 =cadr(e3)) {
+	if ((e4=car(e3))>0)  {
+	    /* register case */
+	    if (reg_stack[--reg_sp]>=REG_ESI) {
+		/* the same registger */
+	    } else {
+		if(reg_stack[reg_sp]<0) {
+		    printf("\tpopl %s\n",reg_name[rname[e4]]);
+		} else {
+		    printf("\tmovl %s,%s\n",
+			    reg_name[rname[reg_stack[reg_sp]]],
+			    reg_name[rname[e4]]);
+		    free_register(reg_stack[reg_sp]);
+		}
+	    }
+	} else {
+	    /* local variable case */
 	    if (reg_stack[reg_sp-1]== -2) {
 		/* same positioned variable */
 		reg_sp--;
 	    } else {
 		xrn=emit_pop(0);
-		printf("\tmovl %s,%d(%%ebp)\n",xrn, 
-		    disp_offset+(-(nargs-i))*int_size);
-	    }
-	} else if (reg_stack[--reg_sp]>=REG_ESI) {
-	    /* the same registger */
-	    ;
-	} else {
-	    if(reg_stack[reg_sp]<0) {
-		printf("\tpopl %s\n",reg_name[rname[REG_ESI+i]]);
-	    } else {
-		printf("\tmovl %s,%s\n",
-			reg_name[rname[reg_stack[reg_sp]]],
-			reg_name[rname[REG_ESI+i]]);
-		free_register(reg_stack[reg_sp]);
+		printf("\tmovl %s,%d(%%ebp)\n",xrn, e4);
 	    }
 	}
     }
     if (car(e2) != FNAME) {	
 	xrn=emit_pop(0);
     }
-/* printf("# new_disp %d,disp %d\n",new_disp,disp); */
+    if (fnptr->sc==FUNCTION) {
+	printf("\tlea %d(%%ebp),%%ebp\n",disp_offset+int_size);
+    } 
     if (new_disp>disp) {
 	/* shrink stack if necessary */
-	printf("\tlea %d(%%ebp),%%esp\n",new_disp+disp_offset);
-    } 
-    if (fnptr->sc==FUNCTION) {
-	/* link operation for leave, %ebp must point saved old %ebp */
-	printf("\tlea %d(%%ebp),%%ebp\n",disp_offset);
+	printf("\tlea %d(%%ebp),%%esp\n",new_disp);
     } 
     if (car(e2) == FNAME) {	
 	printf("\tjmp %s\n",code0->nm);
@@ -1264,7 +1278,6 @@
 code_enter(char *name)
 {
     disp_offset = code_disp_offset;
-    emit_init();
     printf("\t.align 4\n");
     if (stmode!=STATIC)
 	printf(".globl %s\n",name);
@@ -1293,7 +1306,6 @@
 enter(char *name)
 {
     disp_offset = func_disp_offset;
-    emit_init();
     printf("\t.align 2\n");
     if (stmode!=STATIC)
 	printf(".globl %s\n",name);
--- a/mc-parse.c	Thu Jan 13 02:48:11 2000 +0900
+++ b/mc-parse.c	Thu Jan 13 06:27:07 2000 +0900
@@ -70,10 +70,10 @@
 static void newfile(void);
 static int postequ(int s1, int s2);
 static void reverse(int t1);
-static int reverse0(int t1);
+int reverse0(int t1);
 static int rplacad(int e, int n);
 static int rvalue(int e);
-static int scalar(int t);
+int scalar(int t);
 static int sdecl(int s);
 static int skipspc(void);
 static void statement(void);
@@ -146,8 +146,10 @@
 	if ( (obuf = fopen(ccout,"w")) == NULL ) error(FILERR);
     init();
     while(1) {	
-	for (nptr = &ntable[GSYMS],i=LSYMS; i--;)
+	for (nptr = &ntable[GSYMS],i=LSYMS; i--;) {
 	    (nptr++)->sc = 0;
+	}
+	emit_init();
 	mode=TOP;
 	lfree= HEAPSIZE;
 	while(getsym()==SM);
@@ -522,10 +524,8 @@
     if(mode!=GDECL && mode!=ADECL) error(DCERR);
     mode=ADECL;
     args= 0;
-printf("# %s\n",n->nm);
     for(;;) {	
 	if(sym==IDENT && nptr->sc!=TYPE) {
-printf("# %d\n",n->ty);
 	    rplacad(n->ty,list2(INT,cadr(n->ty)));
 	    if (stmode==REGISTER && reg_var < MAX_REGISTER_VAR) {
 		nptr->ty = INT;
@@ -543,7 +543,6 @@
 	    if(sym==RPAR) break;
 	} else {
 	    if(sym==DOTS) {
-printf("# %d\n",n->ty);
 		rplacad(n->ty,list2(INT,cadr(n->ty)));
 		getsym();
 		break;
@@ -561,7 +560,6 @@
 		    def(arg);
 		}
 	    }
-printf("# %d\n",n->ty);
 	    rplacad(n->ty,list2(INT,cadr(n->ty)));
 	    if(sym==RPAR) break;
 	} 
@@ -688,10 +686,14 @@
 	if (stmode==REGISTER && reg_var <MAX_REGISTER_VAR) {
 	    if (type!=CHAR && !scalar(type)) 
 		error(TYERR);
-	    nsc = REGISTER;
+	    nptr->sc = REGISTER;
 	    reg_var++;
-	    ndsp = get_register_var();
-	    break;
+	    if (nptr->dsp==0) {
+		if ((nptr->dsp = get_register_var())<0) {
+		    error(-1);
+		} 
+	    } 
+	    return;
 	}
 	nptr->sc = LVAR;
 	if(type==CHAR) {
@@ -721,7 +723,8 @@
 		error(DCERR);
 	    nsc = REGISTER;
 	    reg_var++;
-	    ndsp = get_register_var();
+	    if ((ndsp = get_register_var())<0)
+		error(-1);
 	} else {
 	    nsc = LVAR;
 	    ndsp = (disp -= sz);
@@ -1777,7 +1780,7 @@
 	getsym();
 	break;
     case ENVIRONMENT:
-	type=list2(POINTER,INT);
+	type=list2(POINTER,VOID);
 	e1=list2(ENVIRONMENT,0);
 	getsym();
 	break;
@@ -2021,7 +2024,7 @@
     getsym();
     arglist=0;
     while(sym!=RPAR) {	
-	arglist=list2(rvalue(expr1()),arglist);
+	arglist=list3(rvalue(expr1()),arglist,type);
 	if(sym!=COMMA) break;
 	getsym();
     }