changeset 367:4efbb07f556b

global variable offset
author kono
date Wed, 07 Jul 2004 20:50:07 +0900
parents 2d510935c37d
children be56772ab12a
files Changes mc-code-powerpc.c mc-code.h mc-codegen.c mc-parse.c
diffstat 5 files changed, 100 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/Changes	Wed Jul 07 17:34:32 2004 +0900
+++ b/Changes	Wed Jul 07 20:50:07 2004 +0900
@@ -5553,3 +5553,5 @@
 list2(GVAR,nptr) なんだけど、これを、list2(GVAR,nptr,0)
 にすれば良い? まぁねぇ。あんまり使われないとは思うけどね。
 実際、出るコードは変わらないし。
+
+結構、複雑。複雑すぎるんじゃないの?
--- a/mc-code-powerpc.c	Wed Jul 07 17:34:32 2004 +0900
+++ b/mc-code-powerpc.c	Wed Jul 07 20:50:07 2004 +0900
@@ -1108,30 +1108,65 @@
     printf("L_%d:\n",labelno);
 }
 
+static void 
+code_add(int reg,int offset,int r)
+{
+    char *crn = register_name(reg);
+    char *rrn = register_name(r);
+    if (offset==0) {
+	if(r!=reg)
+	    printf("\tmr %s,%s\n",crn,rrn);
+    } else if (offset<-32768||32767<offset) {
+	printf("\tla   %s,ha16(%d)(%s)\n",crn,offset,rrn);
+	printf("\taddi %s,%s,lo16(%d)\n",crn,crn,offset);
+    } else
+	printf("\taddi %s,%s,%d\n",crn,rrn,offset);
+}
+
+static void 
+code_ld(char *ld,int reg,int offset,int r)
+{
+    char *crn = register_name(reg);
+    char *rrn = register_name(r);
+    if (offset<-32768||32767<offset) {
+	printf("\tla   %s,ha16(%d)(%s)\n",crn,offset,rrn);
+	printf("\t%s %s,%s,lo16(%d)\n",ld,crn,crn,offset);
+    } else
+	printf("\t%s %s,%d(%s)\n",ld,crn,offset,rrn);
+}
+
+static void 
+code_ldf(char *ld,char *crn,int offset,int r)
+{
+    char *rrn = register_name(r);
+    int reg;
+    char *lrn;
+    if (offset<-32768||32767<offset) {
+	lrn = register_name(reg = get_register());
+	printf("\tla   %s,ha16(%d)(%s)\n",lrn,offset,rrn);
+	printf("\t%s %s,%s,lo16(%d)\n",ld,crn,lrn,offset);
+	free_register(reg);
+    } else
+	printf("\t%s %s,%d(%s)\n",ld,crn,offset,rrn);
+}
+
 void
 code_gvar(int e1,int reg) {
-    int r;
     use_int(reg);
-    r = get_ptr_cache((NMTBL*)cadr(e1));
-    if(r!=reg)
-	printf("\tmr %s,%s\n",register_name(reg),register_name(r));
+    code_add(reg,cadr(e1),get_ptr_cache((NMTBL*)caddr(e1)));
     return;
 }
 
 void
 code_rgvar(int e1,int reg) {
     use_int(reg);
-    printf("\tlwz %s,0(%s)\n",register_name(reg),
-                             register_name(get_ptr_cache((NMTBL*)cadr(e1))));
+    code_ld("lwz",reg,cadr(e1),get_ptr_cache((NMTBL*)caddr(e1)));
 }
 
 void
 code_crgvar(int e1,int reg,int sign,int sz){
-    char *crn;
     use_int(reg);
-    crn = register_name(reg);
-    printf("\t%s %s,0(%s)\n",cload(sz),crn,
-                             register_name(get_ptr_cache((NMTBL*)cadr(e1))));
+    code_ld(cload(sz),reg,cadr(e1),get_ptr_cache((NMTBL*)caddr(e1)));
     cext(sign,sz,reg);
 }
 
@@ -1406,14 +1441,11 @@
 void
 code_cmp_crgvar(int e1,int reg,int sz,int label,int cond) {
     int r;
-    char *crn;
     use_int(reg);
-    crn = register_name(reg);
-    r = get_ptr_cache((NMTBL*)cadr(e1));
-    printf("\t%s %s,0(%s)\n",cload(sz),crn,register_name(r));
+    code_ld(cload(sz),reg,cadr(e1),get_ptr_cache((NMTBL*)caddr(e1)));
     cext(0,sz,r);
     inc_cmpflag();
-    printf("\tcmpwi cr%d,%s,0\n",cmpflag,crn);
+    printf("\tcmpwi cr%d,%s,0\n",cmpflag,register_name(reg));
     jcond(label,cond);
 }
 
@@ -1433,12 +1465,8 @@
 
 void
 code_cmp_rgvar(int e1,int reg,int label,int cond) {
-    int r;
-    char *crn;
     use_int(reg);
-    crn = register_name(reg);
-    r = get_ptr_cache((NMTBL*)cadr(e1));
-    printf("\tlwz %s,0(%s)\n",crn,register_name(r));
+    code_ld("lwz",reg,cadr(e1),get_ptr_cache((NMTBL*)caddr(e1)));
     code_cmp_register(reg,label,cond);
 }
 
@@ -2289,13 +2317,11 @@
 }
 #endif
 
-
 void
 code_assign_gvar(int e2,int creg,int byte) {
-    int r;
     use_int(creg);
-    r = get_ptr_cache((NMTBL*)cadr(e2));
-    code_assign(r,byte,creg);
+    code_ldf(cstore(byte),register_name(creg),cadr(e2),
+	get_ptr_cache((NMTBL*)caddr(e2)));
 }
 
 void
@@ -2304,13 +2330,7 @@
     use_int(creg);
     crn=register_name(creg);
     lvar_intro(e2);
-    if (byte==1) {
-	printf("\tstb %s,",crn);
-    } else if (byte==SIZE_OF_SHORT) {
-	printf("\tsth %s,",crn);
-    } else {
-	printf("\tstw %s,",crn);
-    }
+    printf("\t%s  %s,",cstore(byte),crn);
     lvar(e2);
 }
 
@@ -2330,13 +2350,7 @@
     use_int(creg);
     crn=register_name(creg);
 
-    if (byte==1) {
-	printf("\tstb %s,0(%s)\n",crn,drn);
-    } else if (byte==SIZE_OF_SHORT) {
-	printf("\tsth %s,0(%s)\n",crn,drn);
-    } else {
-	printf("\tstw %s,0(%s)\n",crn,drn);
-    }
+    printf("\t%s %s,0(%s)\n",cstore(byte),crn,drn);
 }
 
 
@@ -2992,10 +3006,13 @@
 }
 
 extern void
-emit_address(char *s)
+emit_address(char *s,int offset)
 {
     data_mode(0);
-    printf("\t.long _%s\n",s);
+    if (offset) 
+	printf("\t.long _%s+%d\n",s,offset);
+    else 
+	printf("\t.long _%s\n",s);
 }
 
 extern void
@@ -3216,10 +3233,9 @@
 void
 code_dassign_gvar(int e2,int freg,int d)
 { 
-    int r;
     use_float(d,freg);
-    r = get_ptr_cache((NMTBL*)cadr(e2));
-    printf("\t%s %s,0(%s)\n",fstore(d),fregister_name(freg),register_name(r));
+    code_ldf(fstore(d),fregister_name(freg),cadr(e2),
+	get_ptr_cache((NMTBL*)caddr(e2)));
 }
 
 void
@@ -3490,10 +3506,9 @@
 void
 code_drgvar(int e2,int d,int freg)
 { 
-    int r;
-    r = get_ptr_cache((NMTBL*)cadr(e2));
     use_float(d,freg);
-    printf("\t%s %s,0(%s)\n",fload(d),fregister_name(freg),register_name(r));
+    code_ldf(fload(d),fregister_name(freg),cadr(e2),
+	get_ptr_cache((NMTBL*)caddr(e2)));
 }
 
 
@@ -3508,7 +3523,6 @@
 void
 code_cmp_drgvar(int e2,int reg,int d,int label,int cond)
 { 
-    int r;
     char *frn;
     int g=get_dregister(d);
     char *grn=fregister_name(g);
@@ -3516,8 +3530,8 @@
     use_float(d,reg);
     frn=fregister_name(reg);
 
-    r = get_ptr_cache((NMTBL*)cadr(e2));
-    printf("\t%s %s,0(%s)\n",fload(1),grn,register_name(r));
+    code_ldf(fload(1),grn,cadr(e2),
+	get_ptr_cache((NMTBL*)caddr(e2)));
     inc_cmpflag();
     printf("\tfcmpu cr%d,%s,%s\n",cmpflag,frn,grn);
     free_register(g);
@@ -3963,8 +3977,10 @@
 code_lassign_gvar(int e2,int creg)
 {
     int r;
-    r = get_ptr_cache((NMTBL*)cadr(e2));
-    code_lassign(r,creg);
+    use_longlong(creg);
+    r = get_ptr_cache((NMTBL*)caddr(e2));
+    code_ldf(cstore(0),lregister_name_high(creg),cadr(e2),r);
+    code_ldf(cstore(0),lregister_name_low(creg),cadr(e2)+SIZE_OF_INT,r);
 }
 
 void
@@ -4033,9 +4049,9 @@
     use_longlong(creg);
     crn_h = lregister_name_high(creg);
     crn_l = lregister_name_low(creg);
-    r = get_ptr_cache((NMTBL*)cadr(e1));
-    printf("\tlwz %s,0(%s)\n",crn_h,register_name(r));
-    printf("\tlwz %s,%d(%s)\n",crn_l,SIZE_OF_INT,register_name(r));
+    r = get_ptr_cache((NMTBL*)caddr(e1));
+    code_ldf(cload(0),crn_h,cadr(e1),r);
+    code_ldf(cload(0),crn_l,cadr(e1)+SIZE_OF_INT,r);
 }
 
 void
--- a/mc-code.h	Wed Jul 07 17:34:32 2004 +0900
+++ b/mc-code.h	Wed Jul 07 20:50:07 2004 +0900
@@ -130,7 +130,7 @@
 extern void emit_longlong(int e);
 extern void emit_double(int e);
 extern void emit_float(int e);
-extern void emit_address(char *s);
+extern void emit_address(char *s,int offset);
 extern void emit_label(int labelno);
 extern int emit_string_label();
 extern void ascii(char *s);
--- a/mc-codegen.c	Wed Jul 07 17:34:32 2004 +0900
+++ b/mc-codegen.c	Wed Jul 07 20:50:07 2004 +0900
@@ -1048,17 +1048,23 @@
 	    return 1;
 	}
     } else if (ce1==GVAR) {
+	return 0;
+#if 0
 	switch(ce2) {
 	    case RGVAR: case CRGVAR: case FRGVAR: case DRGVAR:
 	    case SRGVAR: case SURGVAR: case CURGVAR:
 	    return 1;
 	}
+#endif
     } else if (ce2==GVAR) {
+	return 0;
+#if 0
 	switch(ce1) {
 	    case RGVAR: case CRGVAR: case FRGVAR: case DRGVAR:
 	    case SRGVAR: case SURGVAR: case CURGVAR: case LRGVAR:
 	    return 1;
 	}
+#endif
     }
     return 0;
 }
@@ -2589,12 +2595,14 @@
 	    return;
 	case ADDRESS:
 	    if (car(cadr(e))==GVAR)
-		emit_address(((NMTBL *)cadr(cadr(e)))->nm);
+		emit_address(((NMTBL *)caddr(cadr(e)))->nm,cadr(cadr(e)));
 	    else error(INERR);
 	    return;
 	case FNAME:
+	    emit_address(((NMTBL *)cadr(e))->nm,0);
+	    return;
 	case GVAR:
-	    emit_address(((NMTBL *)cadr(e))->nm);
+	    emit_address(((NMTBL *)caddr(e))->nm,0);
 	    return;
 	case STRING:
 	    if (car(n->ty)!=ARRAY || cadr(n->ty)!=CHAR) {
@@ -2892,7 +2900,7 @@
     }
     switch(car(e)) {
     case GVAR:
-	return(list2(RGVAR+op,cadr(e)));
+	return(list3(RGVAR+op,cadr(e),caddr(e)));
     case LVAR:
 	return(list2(RLVAR+op,cadr(e)));
     case INDIRECT:
@@ -2984,7 +2992,8 @@
     if(dsp) {
 	switch(car(e)) {
 	case GVAR:
-	    e=list2(INDIRECT,list3(ADD,e,list2(CONST,dsp)));
+	    // e=list2(INDIRECT,list3(ADD,e,list2(CONST,dsp)));
+	    e=list3(GVAR,cadr(e)+dsp,caddr(e));
 	    break;
 	case LVAR:
 	    e=list2(LVAR,cadr(e) + dsp);
@@ -3346,9 +3355,16 @@
 	type=t1;
 	if (car(e)==CONST && cadr(e)==0)
 	    return(e1);
-	if(car(e1)==ADDRESS&&car(e)==CONST&&car(cadr(e1))!=GVAR)
-	    return(list2(ADDRESS,list2(car(cadr(e1)),
-		cadr(cadr(e1))+cadr(e))));
+	if (car(e)==CONST) {
+	    if(car(e1)==ADDRESS&&car(cadr(e1))!=GVAR) {
+		return(list2(ADDRESS,list2(car(cadr(e1)),
+		    cadr(cadr(e1))+cadr(e))));
+	    } else if(car(e1)==GVAR) {
+		return(list3(GVAR,cadr(e1)+cadr(e),caddr(e1)));
+	    } else if(car(e1)==LVAR) {
+		return(list2(LVAR,cadr(e1)+cadr(e)));
+	    }
+	}
 	return(list3(ADD,e1,e));
     }
     if(op==SUB) {
@@ -3361,6 +3377,7 @@
 	if(integral(t2)) {
 	    e=binop(MUL,e2,list2(CONST,size(cadr(t1))),t2,INT);
 	    type=t1;
+	    if (car(e)==CONST) error(-1);
 	    return(list3(SUB,e1,e));
 	}
 	if(car(t2)!=POINTER)
--- a/mc-parse.c	Wed Jul 07 17:34:32 2004 +0900
+++ b/mc-parse.c	Wed Jul 07 20:50:07 2004 +0900
@@ -1099,7 +1099,7 @@
 	decl_data_field(type,nptr0,offset);
 	init_vars = list2(
 	    list4(STASS,list2(LVAR,n->dsp+offset),
-		list3(RSTRUCT,list2(GVAR,(int)nptr0),e),e),
+		list3(RSTRUCT,list3(GVAR,0,(int)nptr0),e),e),
 	    init_vars);
 	return;
     }
@@ -2614,7 +2614,7 @@
 		return fname(nptr);
 	    }
 	case GVAR:
-	    e1=list2(GVAR,(int)nptr);
+	    e1=list3(GVAR,0,(int)nptr);
 	    type=nptr->ty;
 	    getsym(0);
 	    extrn_use(nptr);
@@ -2738,7 +2738,7 @@
 		mode=STADECL;
 		decl_data_field(type,nptr0,0);
 		checksym(RC);
-		e1 = list3(RSTRUCT,list2(GVAR,(int)nptr0),e1);
+		e1 = list3(RSTRUCT,list3(GVAR,0,(int)nptr0),e1);
 		mode = smode;
 		return e1;
 	    }