changeset 37:412ad2e6c2a2

struct copy
author kono
date Mon, 10 Feb 2003 22:29:12 +0900
parents 9f89cef85b41
children d48d952da354
files .gdbinit mc-nop-386.c mc-parse.c mc-tree.c mc.h test/tmp7.c
diffstat 6 files changed, 132 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Mon Feb 10 10:07:00 2003 +0900
+++ b/.gdbinit	Mon Feb 10 22:29:12 2003 +0900
@@ -1,8 +1,6 @@
 tb main
 b error
 b errmsg
-b macro_function
-b macro_eval
 define regs 
 call fprintf(stderr,"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
@@ -15,4 +13,4 @@
 nexti
 x/1i $eip
 end
-r -s test/macro.c
+r -s test/tmp7.c
--- a/mc-nop-386.c	Mon Feb 10 10:07:00 2003 +0900
+++ b/mc-nop-386.c	Mon Feb 10 22:29:12 2003 +0900
@@ -55,6 +55,7 @@
 static void tosop(int op);
 static void div_cleanup(char *orn);
 static void use_register(int virt, int real, int move);
+static void emit_copy(char *from,char *to,int length,int offset);
 
 extern int error(int n);
 extern int fwdlabel(void);
@@ -564,6 +565,9 @@
 	use_register(creg,REG_EAX,1);
 	fwddef(e3);
 	return;
+    case RSTRUCT:
+	g_expr(e2);
+	return;
     case SASS: 
 	sassign(e1);
 	return;
@@ -793,7 +797,7 @@
 	printf("\tpushl %%edi\n");
 	printf("\tpushl %%esi\n");
 	printf("\tpushl %%ecx\n");
-	printf("\tleal 12(%%esp),%%di\n");
+	printf("\tleal 12(%%esp),%%edi\n");
 	if (rname[creg]!=REG_ESI)
 		printf("\tmovl %s,%%esi\n",register_name(creg,0));
 	printf("\tmovl $%d,%%ecx\n",length/size_of_int);
@@ -1107,15 +1111,15 @@
     char *xrn;
 
     /* structure assignment */
-    e2 = cadr(e1);
-    e3 = cadr(e2);
-    e4 = caddr(e1);
-    sz = size(cadr(e2));  /* cld rep movsb */
+    e2 = cadr(e1);  /* pointer variable to the struct */
+    e3 = cadr(e2);  /* offset of the variable (distination) */
+    e4 = caddr(e1); /* rright value (source) */
+    sz = cadddr(e1);  /* size of struct or union */
     g_expr(e4);
     emit_push();
     g_expr(e2);
     xrn = register_name(emit_pop(0),0);
-
+    emit_copy(xrn,register_name(creg,0),sz,0);
     return;
 }
 
--- a/mc-parse.c	Mon Feb 10 10:07:00 2003 +0900
+++ b/mc-parse.c	Mon Feb 10 22:29:12 2003 +0900
@@ -11,7 +11,7 @@
 static int binop(int op, int e1, int e2, int t1, int t2);
 static void compatible(int t1, int t2);
 static void decl(void);
-static void def(NMTBL *n);
+static NMTBL *def(NMTBL *n);
 static int digit(char c);
 static void docase(void);
 static void docomp(void);
@@ -85,12 +85,13 @@
 static int typespec(void);
 static int cexpr(int e);
 static void code_decl(NMTBL *n);
-static int macro_args(char **pcheapp,char **pchptr);
+static int macro_args(char **pcheapp,char* max,char **pchptr);
 static int macro_function(int macrop,char **pchptr,NMTBL *nptr,int history);
 static void local_define();
 static void local_undef();
 static int macro_eval(int macrop,char *body,int history);
 static char * append(int lists);
+static NMTBL *free_nptr();
 
 extern void display_ntable(NMTBL *n, char *s);
 extern void closing(void);
@@ -126,6 +127,8 @@
 
 extern void exit(int l);
 
+static int struct_return  = 0;
+
 int
 main(int argc, char **argv)
 {
@@ -667,15 +670,19 @@
     return 0;
 }
 
-void
+NMTBL *
 def(NMTBL *n)
 {
     int sz,nsc,ndsp,t;
 
+    if (n==0) {
+	n=free_nptr();
+	n->nm = "_";
+    }
     nsc=ndsp=0;
     if(car(type)==FUNCTION) {	
 	fcheck(n);
-	return;
+	return n;
     }
     if (n->sc!=EMPTY && 
 	    !(n->sc==GVAR&&n->dsp==EXTRN) &&
@@ -713,7 +720,7 @@
 	    /* gpc is incremented by emit_data */
 	} else 
 	    gpc +=sz;
-	return;
+	return n;
     case GSDECL:
 	nsc = FIELD;
 	ndsp = disp;
@@ -738,7 +745,7 @@
 		    error(-1);
 		} 
 	    } 
-	    return;
+	    return n;
 	}
 	nptr->sc = LVAR;
 	if(type==CHAR) {
@@ -761,7 +768,8 @@
 	    } else
 		error(TYERR);
 	}
-	return;
+	return n;
+    case STAT: /* of course this is wrong */
     case LDECL:
 	if (stmode==REGISTER && reg_var <=MAX_REGISTER_VAR) {
 	    if(!scalar(type)) /* non integer register type ... */
@@ -779,7 +787,7 @@
 	if(sym==ASS) {	
 	    decl_data(type,n,0);
 	}
-	return;
+	return n;
     case LSDECL:
 	nsc = FIELD;
 	ndsp = disp;
@@ -801,6 +809,7 @@
 	n->dsp = EXTRN;
     else
 	n->dsp = ndsp;
+    return n;
 }
 
 void
@@ -1007,6 +1016,9 @@
 void
 fdecl(NMTBL *n)
 {
+    NMTBL str_ret;
+    int t;
+
     enter(n->nm);
     fnptr=n;
     retlabel=fwdlabel();
@@ -1020,6 +1032,16 @@
 	stmode=0;
 	decl(); getsym();
     }
+    if (((t=car(fnptr->ty))==STRUCT||t==UNION)) {
+	/* this extra dummy arguments are set at calling sequence */
+	str_ret.nm = "str_ret";
+	type = t;
+	def(&str_ret);
+	if ((t=size(type))==-1) error(TYERR);
+	else struct_return = list2(list2(LVAR,str_ret.dsp),size(type));
+    } else {
+	struct_return = 0;
+    }
     disp=0;
     init_vars=0;
     /* local variable declaration */
@@ -1364,7 +1386,7 @@
 void
 doreturn(void)
 {
-    int slfree;
+    int slfree,e;
 
     if(getsym()==SM) {	
 	getsym();
@@ -1372,7 +1394,18 @@
 	return;
     }
     slfree=lfree;
-    gexpr(expr());
+    if (struct_return) {
+	e = expr();
+	if ((car(type)==STRUCT || car(type)==UNION)&&
+		size(type)==cadr(struct_return)) {
+	    e = list4(SASS,car(struct_return),rvalue(e),size(type));
+	    gexpr(e);
+	} else {
+	    error(TYERR);
+	}
+    } else {
+	gexpr(expr());
+    }
     lfree=slfree;
     checksym(SM);
     retpending = 1;
@@ -1924,7 +1957,17 @@
 	    type=list2(POINTER,cadr(type));
 	    if(car(e)==INDIRECT) return cadr(e);
 	    return list2(ADDRESS,e);
-	} else if(t==STRUCT || t==UNION) { return e;
+	} else if(t==STRUCT || t==UNION) { 
+	    t = cadr(type); /* size */
+	    switch(car(e)) {
+	    case GVAR:
+		return(list3(RSTRUCT,list3(RGVAR,cadr(e),caddr(e)),t));
+	    case LVAR:
+		return(list3(RSTRUCT,list2(RLVAR,cadr(e)),t));
+	    case INDIRECT:
+		return(list3(RSTRUCT,list2(RINDIRECT,cadr(e)),t));
+	    default: return list3(RSTRUCT,e,t);
+	    }
 	} else if(t!=POINTER) error(TYERR);
     }
     switch(car(e)) {
@@ -2111,7 +2154,13 @@
     checksym(RPAR);
     if(car(t)!=CODE) {
 	t=cadr(t);
-	if(t==CHAR) type= INT;else type=t;
+	if(t==CHAR) type= INT;
+	else if(car(t)==STRUCT||car(t)==UNION) {
+	    type = t;
+	    e = list2(LVAR,def(0)->dsp);
+	    arglist=list3(e,arglist,type);
+	    return list3(COMMA,list3(FUNCTION,e1,arglist),rvalue(e));
+	} else type=t;
     }
     return list3(FUNCTION,e1,arglist);
 }
@@ -2119,7 +2168,8 @@
 int
 typeid(int s)
 {
-    return (integral(s) || s==CODE || s==SHORT || s==LONG || s==STRUCT || s==UNION ||
+    return (integral(s) || s==CODE || s==SHORT || 
+	s==LONG || s==STRUCT || s==UNION ||
     (s==IDENT && nptr->sc==TYPE));
 }
 
@@ -2401,6 +2451,28 @@
     return('0'<=c&&c<='9');
 }
 
+int dummy_count = 0;
+
+NMTBL *
+free_nptr()
+{
+    NMTBL *nptr,*iptr;
+
+    iptr=nptr= &ntable[hash % GSYMS];
+    while(nptr->sc!=0) {	
+	if (++nptr== &ntable[GSYMS]) 
+	    nptr=ntable;
+	if (nptr==iptr) error(GSERR);
+    }
+    copy(nptr,"_00000");
+    dummy_count++;
+    if (dummy_count>999) error(STRERR);
+    nptr->nm[5]='0'+dummy_count%10;
+    nptr->nm[4]='0'+(dummy_count/10)%10;
+    nptr->nm[3]='0'+(dummy_count/100)%10;
+    nptr->sc=EMPTY;
+    return nptr;
+}
 
 NMTBL *
 gsearch(void)
@@ -2760,7 +2832,7 @@
     args = 0;
     if (ch=='(') {
 	nptr->sc = FMACRO;
-	args = macro_args(&cheapp,&chptr);
+	args = macro_args(&cheapp,cheap+CHEAPSIZE,&chptr);
     } else {
 	nptr->sc = MACRO;
 	nptr->ty = -1; 
@@ -2781,7 +2853,7 @@
 }
 
 int 
-macro_args(char **pcheapp,char **pchptr)
+macro_args(char **pcheapp,char *maxcheap,char **pchptr)
 {
     int c;
     int in_quote = 0;
@@ -2792,6 +2864,7 @@
     int args = list2((int)cheapp,0);
     for(;;) {
         *cheapp++ = c = *chptr++;
+	if (cheapp >= maxcheap) error(MCERR);
 	if (!c)  {
 	    chptr--;
 	    error(MCERR);
@@ -2869,7 +2942,7 @@
     char *macro;
 
     sargs = args = cadr(nptr->dsp);
-    values = macro_args(&macropp,pchptr);
+    values = macro_args(&macropp,macro_buf+MACROSIZE,pchptr);
     evalues = 0;
     while(values) {
 	evalues = list2(macro_eval(0,(char *)car(values),history),evalues);
@@ -2927,6 +3000,7 @@
     c = 1;
     macrop = list2((int)macropp,macrop);
     while(c && (*macropp++ = c = *body++)) {
+	if (macropp>macro_buf+MACROSIZE) error(STRERR);
 	if (in_quote) {
 	    if (c=='\\') {
 		*macropp++ = c = *body++;
@@ -3055,6 +3129,7 @@
     char *p;
     char *result = macropp;
     while(lists) {
+	if (macropp>macro_buf+MACROSIZE) error(STRERR);
 	p = (char *)car(lists);
 	while((*macropp++=*p++)) if (p[-1]=='\n') macropp[-1]=' ';
 	macropp--;
@@ -3074,4 +3149,13 @@
     fprintf(stderr,"nptr->nm %s\n",n->nm); 
 }
 
+void c0(int d)  { fprintf(stderr,"heap[%d]=%d\n",d,car(d)); }
+void c1(int d)  { fprintf(stderr,"heap[%d]=%d\n",d,cadr(d)); }
+void c2(int d)  { fprintf(stderr,"heap[%d]=%d\n",d,caddr(d)); }
+void c3(int d)  { fprintf(stderr,"heap[%d]=%d\n",d,cadddr(d)); }
+void cc0(int d) { fprintf(stderr,"heap[%d]=%s\n",d,(char *)car(d)); }
+void cc1(int d) { fprintf(stderr,"heap[%d]=%s\n",d,(char *)cadr(d)); }
+void cc2(int d) { fprintf(stderr,"heap[%d]=%s\n",d,(char *)caddr(d)); }
+void cc3(int d) { fprintf(stderr,"heap[%d]=%s\n",d,(char *)cadddr(d)); }
+
 /* end */
--- a/mc-tree.c	Mon Feb 10 10:07:00 2003 +0900
+++ b/mc-tree.c	Mon Feb 10 22:29:12 2003 +0900
@@ -56,6 +56,10 @@
     {(-39),"CODE",""},
     {(-40),"ENVIRONMENT",""},
     {(-41),"DEFINED",""},
+    {(-42),"KONST",""},
+    {(-43),"FMACRO",""},
+    {(-44),"LMACRO",""},
+
     {1,"GVAR","vs"},
     {2,"RGVAR","vs"},
     {3,"CRGVAR","vs"},
@@ -122,7 +126,9 @@
     {64,"SM",""},
     {65,"PERIOD",""},
     {66,"ARROW",""},
-    {66,"CNAME",""},
+    {67,"CNAME",""},
+    {68,"SASS",""},
+    {69,"DOTS",""},
     {100,"AS",""}
 };
 
--- a/mc.h	Mon Feb 10 10:07:00 2003 +0900
+++ b/mc.h	Mon Feb 10 22:29:12 2003 +0900
@@ -141,6 +141,7 @@
 #define CNAME	67
 #define SASS	68
 #define DOTS	69
+#define RSTRUCT	70
 
 #define US	1
 #define AS	100
--- a/test/tmp7.c	Mon Feb 10 10:07:00 2003 +0900
+++ b/test/tmp7.c	Mon Feb 10 22:29:12 2003 +0900
@@ -20,6 +20,10 @@
 void
 main2(struct aa a1)
 {
+/* main2 a1.a[99] 0 */
+/* main2 a1.a[55] 0 */
+/* main2 a1.a[99] 55 */
+/* main2 a1.a[55] 44 */
 	printf("main2 a1.a[99] %d\n",a1.a[99]);
 	printf("main2 a1.a[55] %d\n",a1.a[55]);
 }
@@ -27,6 +31,8 @@
 void
 main1(struct aa a1)
 {
+/* main1 a1.a[0] 0 */
+/* main1 a1.a[0] 44 */
     printf("main1 a1.a[0] %d\n",a1.a[0]);
     a1.a[55]=44;
     if (a1.a[0]) 
@@ -50,19 +56,24 @@
     bbb = main0();
     j = main0().a[55];
     printf("%d==3\n",q.b);
+/* 3==3 */
     r = q;
     printf("%d==3\n",r.b);
+/* 3==3 */
     (*r.c)(aaa);
     printf("main aaa.a[55] %d\n",aaa.a[55]);
+/* main aaa.a[55] 123 */
     bbb.a[0]=44; 
     aaa = bbb;
     aaa.a[99]=55;  /* core dump? */
     (*r.c)(aaa);
+/* main aaa.a[55] 0 */
     printf("main aaa.a[55] %d\n",aaa.a[55]); 
     p = av[0];
     if (!p) return;
     i = p[1];
     p = p+i;
+/* 47 ./a.out */
     printf("%d %s\n",i,p-i);
 }
 
@@ -70,3 +81,5 @@
 tmp()
 {
 }
+
+