changeset 682:639db8597a58

decl_data optimization local_struct_static should be abandoned
author kono
date Mon, 01 Oct 2007 17:54:47 +0900
parents e16b34f2b386
children 9708820f2b8a
files .gdbinit mc-codegen.c mc-codegen.h mc-inline.c mc-parse.c test/strinit.c
diffstat 6 files changed, 137 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Mon Oct 01 01:01:47 2007 +0900
+++ b/.gdbinit	Mon Oct 01 17:54:47 2007 +0900
@@ -14,5 +14,7 @@
 end
 b errmsg
 # r -s test/tmp7.c
- r -s test/code-gen-all.c
+# r -s test/code-gen-all.c
 # r -s mc-code-powerpc.c
+# r -s test/strinit.c
+r -s -DINLINE=inline test/strinit.c
--- a/mc-codegen.c	Mon Oct 01 01:01:47 2007 +0900
+++ b/mc-codegen.c	Mon Oct 01 17:54:47 2007 +0900
@@ -61,6 +61,7 @@
 static void remove0(int *parent,int e) ;
 static void sassign(int e1);
 static int gen_decl_data(int e);
+static int gen_decl_data0(NMTBL *nptr0,int target_type,int init,int offset);
 
 #if FLOAT_CODE
 
@@ -291,7 +292,9 @@
 	jump(e2,caddr(e1));
 	return VOID;
     case ARRAY:
-	if (chk==2) {
+	if (chk==2) { // for generation check (?)
+	    indop(e1);
+	    t = type;
 	    g_expr0(e2);
 	    g_expr0(caddr(e1));
 	    code_gexpr(e1);
@@ -1732,6 +1735,39 @@
 }
 #endif
 
+static int
+lvalue_opt(int e0)
+{
+    int e,e1;
+
+    if (car(e0)!=ADD) return e0;
+    if (car(e=caddr(e0))!=CONST) return e0;
+    e1 = cadr(e0);
+    if(car(e1)==ADDRESS) {
+	switch (car(cadr(e1))) {
+	case GVAR:
+	    return(list2(ADDRESS,
+		list3(GVAR,cadr(cadr(e1))+cadr(e),caddr(cadr(e1)))));
+	case LVAR:
+	    return(list2(ADDRESS,
+		list3(car(cadr(e1)),cadr(cadr(e1))+cadr(e),
+			caddr(cadr(e1)))));
+	case INDIRECT:
+	case PERIOD:
+	case ARROW:
+	    return(list3(ADD,e1,e));
+	default:
+	    error(-1); // ?
+	}
+    } else if(car(e1)==GVAR) {
+	return(list3(GVAR,cadr(e1)+cadr(e),caddr(e1)));
+    } else if(car(e1)==LVAR) {
+	return(list3(LVAR,cadr(e1)+cadr(e),0));
+    }
+    error(-1); // ?
+    return e0;
+}
+
 static void
 sassign(int e1)
 {
@@ -1751,6 +1787,27 @@
 	    return;
 	}
     }
+    e2 = lvalue_opt(e2);
+
+    if (car(e4)==DECL_DATA && (car(e2)==GVAR || car(e2)==LVAR)) {
+	// we can do optimize any other type of destination ...
+	// but gen_decl_data0 requires nptr (of course wrong decision)
+	NMTBL nptr1;
+	int t = caddr(e4);
+	int sinit_vars = init_vars; init_vars = 0;
+	nptr1.sc = car(e2);
+	nptr1.dsp = cadr(e2);
+	nptr1.ty = t;
+	nptr1.nm = 0;
+	nptr1.next = 0;
+	nptr1.attr = 0;
+	gen_decl_data0(&nptr1,t,e4,0);
+	emit_init_vars();
+	init_vars = sinit_vars;
+	return;
+    }
+
+
     g_expr(e4);
     emit_push();
     g_expr(e2);
@@ -1820,6 +1877,7 @@
     byte=(car(e1) == CASS)?1:(car(e1) == SASS)?size_of_short:0;
     /*    e2=e4 */
     e2 = cadr(e1);
+    e2 = lvalue_opt(e2);
     e4 = caddr(e1);e5=car(e4);
     if (is_same_type(e2,e4)&&cadr(e2)==cadr(e4)) {
 	if (use) g_expr(e4);
@@ -3532,7 +3590,6 @@
     error(INERR);
 }
 
-static int gen_decl_data0(NMTBL *nptr0,int target_type,int init,int offset);
 
 extern int
 gen_delayed_decl_data(NMTBL *n,int offset)
@@ -4249,7 +4306,7 @@
 {
     int dsp = 0;
     int type0;
-    int e1;
+    int e1 = 0;
 
     if (inmode || chk) {
 	e1 = list4(ind?ARROW:PERIOD,e,(int)nptr,type);
@@ -4675,30 +4732,7 @@
 	type=t1;
 	if (car(e)==CONST && cadr(e)==0)
 	    return(e1);
-	if (car(e)==CONST) {
-	    if(car(e1)==ADDRESS) {
-		switch (car(cadr(e1))) {
-		case GVAR:
-		    return(list2(ADDRESS,
-			list3(GVAR,cadr(cadr(e1))+cadr(e),caddr(cadr(e1)))));
-		case LVAR:
-		    return(list2(ADDRESS,
-			list3(car(cadr(e1)),cadr(cadr(e1))+cadr(e),
-				caddr(cadr(e1)))));
-		case INDIRECT:
-		case PERIOD:
-		case ARROW:
-		    return(list3(ADD,e1,e));
-		default:
-		    error(-1); // ?
-		}
-	    } else if(car(e1)==GVAR) {
-		return(list3(GVAR,cadr(e1)+cadr(e),caddr(e1)));
-	    } else if(car(e1)==LVAR) {
-		return(list3(LVAR,cadr(e1)+cadr(e),0));
-	    }
-	}
-	return(list3(ADD,e1,e));
+	return(lvalue_opt(list3(ADD,e1,e)));
     }
     if(op==SUB) {
 	if(integral(t1)) {
@@ -4803,6 +4837,20 @@
     return 0;
 }
 
+extern int
+skip_cast(int e)
+{
+    int t,t1;
+    if (car(e)==CAST) {
+	t  = caddr(e);
+	t1 = cadddr(e);
+	if ( type_compatible(t, t1) ) {
+	    return cadr(e);
+	}
+    } 
+    return e;
+}
+
 /* coarse for function/code segments arguments */
 
 extern int
--- a/mc-codegen.h	Mon Oct 01 01:01:47 2007 +0900
+++ b/mc-codegen.h	Mon Oct 01 17:54:47 2007 +0900
@@ -53,6 +53,7 @@
 extern int cexpr(int e);
 extern int cond(int t,int e1,int e2,int e3);
 extern int correct_type(int e,int t);
+extern int skip_cast(int e);
 extern int csvalue();
 extern int function_type(int e1,int *dots);
 extern int gexpr(int e1,int use0);
--- a/mc-inline.c	Mon Oct 01 01:01:47 2007 +0900
+++ b/mc-inline.c	Mon Oct 01 17:54:47 2007 +0900
@@ -841,8 +841,9 @@
     decl_str_init = 0;
     target_type = type_value(target_type);
 
+    e = cadr(init);
     if (car(init)==DECL_DATA) {
-	switch( car(e=cadr(init))) {
+	switch(car(e)) {
 	case DECL_DATA_LIST: 
 	    offset = pdecl_data_list(var,e,target_type,offset);
 	    break;
--- a/mc-parse.c	Mon Oct 01 01:01:47 2007 +0900
+++ b/mc-parse.c	Mon Oct 01 17:54:47 2007 +0900
@@ -1670,6 +1670,7 @@
     int t0,t1=0,e,i,mode_save,lc=0;
     int offset0;
 
+    emit_init_vars();
     conv->decl_data_();
     t0 = type_value(t);
     mode_save = mode;
@@ -4007,7 +4008,7 @@
 #if LOCAL_STRUCT_INIT_STATIC 
 		    local_struct_static(nptr0); 
 #else
-		    decl_data_field(type,nptr0,0,0);
+		    decl_data_field(type,nptr0,0);
 #endif
 		    e1 = list3(RSTRUCT,list3(
 			nptr0->sc,nptr0->dsp,(int)nptr0),e1);
--- a/test/strinit.c	Mon Oct 01 01:01:47 2007 +0900
+++ b/test/strinit.c	Mon Oct 01 17:54:47 2007 +0900
@@ -115,7 +115,7 @@
 } __attribute__((__aligned__(32/8)));
 
 
-INLINE static __inline__ __attribute__((always_inline)) __attribute__((always_inline)) 
+INLINE static /* __inline__ __attribute__((always_inline)) __attribute__((always_inline))  */
 void ip_route_connect(/* struct rtable **rp, */ u32 dst,
                                    u32 src, u32 tos, int oif, u8 protocol,
                                    u16 sport, u16 dport /*, struct sock *sk */)
@@ -129,7 +129,7 @@
                                        { .sport = sport,
                                          .dport = dport } } };
 
-	printf("#0166:%d %d\n",fl.oif,fl.uli_u.ports.dport);
+	printf("#0131:%x %x\n",fl.oif,fl.uli_u.ports.dport);
 }
 
 INLINE static 
@@ -147,8 +147,29 @@
                                        { .sport = sport,
                                          .dport = dport } } };
 
-	printf("#0184:%d %d\n",fl.oif,fl.uli_u.ports.dport);
-	printf("#0185:%d %d\n",fl.iif,fl.nl_u.ip4_u.saddr);
+	printf("#0149:%x %x\n",fl.oif,fl.uli_u.ports.dport);
+	printf("#0150:%x %x\n",fl.iif,fl.nl_u.ip4_u.saddr);
+}
+
+INLINE static 
+void ip_route_connect1(/* struct rtable **rp, */ u32 dst,
+                                   u32 src, u32 tos, int oif, u8 protocol,
+                                   u16 sport, u16 dport /*, struct sock *sk */)
+{
+        struct flowi fl, *flp; 
+      fl = (struct flowi){ .oif = oif,
+			    .iif = 55,
+                            .nl_u = { .ip4_u = { .daddr = dst,
+                                                 .saddr = 66,
+                                                 .tos = tos } },
+                            .proto = protocol,
+                            .uli_u = { .ports =
+                                       { .sport = sport,
+                                         .dport = dport } } };
+       flp = &fl;
+
+	printf("#01490:%x %x\n",flp->oif,flp->uli_u.ports.dport);
+	printf("#01500:%x %x\n",flp->iif,flp->nl_u.ip4_u.saddr);
 }
 
 INLINE void
@@ -157,6 +178,7 @@
     ip_route_connect0(1,2,3,4,5,6,7);
     ip_route_connect(1,2,3,4,5,6,7);
     ip_route_connect(11,12,13,14,15,16,17);
+    ip_route_connect1(1,2,3,4,5,6,7);
 }
 
 struct st_child {
@@ -175,16 +197,16 @@
 {
   struct st_parent st1 = {'A',{1,2,'E'},5,6};
   struct st_child st2 = {10,11,'F'};
-  printf("st1.ch   = %c\n",st1.ch);
-  printf("st1.s1.a = %d\n",st1.s1.a);
-  printf("st1.s1.b = %d\n",st1.s1.b);
-  printf("st1.s1.c = %c\n",st1.s1.c);
-  printf("st1.i    = %d\n",st1.i);
-  printf("st1.j    = %d\n\n",st1.j);
+  printf("#0177:st1.ch   = %c\n",st1.ch);
+  printf("#0178:st1.s1.a = %d\n",st1.s1.a);
+  printf("#0179:st1.s1.b = %d\n",st1.s1.b);
+  printf("#0180:st1.s1.c = %c\n",st1.s1.c);
+  printf("#0181:st1.i    = %d\n",st1.i);
+  printf("#0182:st1.j    = %d\n\n",st1.j);
 
-  printf("st2.a = %d\n",st2.a);
-  printf("st2.b = %d\n",st2.b);
-  printf("st2.c = %c\n",st2.c);
+  printf("#0184:st2.a = %d\n",st2.a);
+  printf("#0185:st2.b = %d\n",st2.b);
+  printf("#0186:st2.c = %c\n",st2.c);
   
   return;
 
@@ -195,12 +217,12 @@
 struct arg { int hage, fuga; int aho; };
 
 INLINE void f(struct arg h) {
-    printf("%d %d %d\n",h.hage,h.fuga,h.aho);
+    printf("#0197:%d %d %d\n",h.hage,h.fuga,h.aho);
 }
 
 INLINE int main8() {
      f((struct arg){.fuga = 3,.aho=5});
-     printf("%d\n",((struct arg){.aho=120, .hage=55}).aho);
+     printf("#0202:%d\n",((struct arg){.aho=120, .hage=55}).aho);
 }
 
 int
@@ -224,23 +246,23 @@
        .c = (int)&temp4,
     };
 
-    printf("#0070:1: %d\n",temp1.a);
-    printf("#0071:1: %d\n",temp1.e);
-    printf("#0072:1: %d\n",temp1.b);
-    printf("#0073:2: %d\n",temp2.c);
-    printf("#0074:2: %d\n",temp2.e);
-    printf("#0075:2: %d\n",temp2.b);
-    printf("#0076:2: %d\n",(void*)temp3.c==b);
-    printf("#0077:2: %d\n",temp3.c==(int)b);
-    printf("#0078:2: %d\n",temp3.a==(int)&b);
-    printf("#0079:2: %d\n",temp4.m.j);
-    printf("#0080:2: %d\n",temp5.m.j);
-    printf("#0081:2: %d\n",temp11.m.j);
-    printf("#0082:2: %d\n",temp12.m.k);
-    printf("#0083:2: %d\n",temp12.m.j);
-    printf("#0084:2: %d\n",temp11.c==(int)&b);
-    printf("#0085:2: %d\n",temp8.e);
-    printf("#0086:2: %d\n",temp6.e);
+    printf("#0226:1: %d\n",temp1.a);
+    printf("#0227:1: %d\n",temp1.e);
+    printf("#0228:1: %d\n",temp1.b);
+    printf("#0229:2: %d\n",temp2.c);
+    printf("#0230:2: %d\n",temp2.e);
+    printf("#0231:2: %d\n",temp2.b);
+    printf("#0232:2: %d\n",(void*)temp3.c==b);
+    printf("#0233:2: %d\n",temp3.c==(int)b);
+    printf("#0234:2: %d\n",temp3.a==(int)&b);
+    printf("#0235:2: %d\n",temp4.m.j);
+    printf("#0236:2: %d\n",temp5.m.j);
+    printf("#0237:2: %d\n",temp11.m.j);
+    printf("#0238:2: %d\n",temp12.m.k);
+    printf("#0239:2: %d\n",temp12.m.j);
+    printf("#0240:2: %d\n",temp11.c==(int)&b);
+    printf("#0241:2: %d\n",temp8.e);
+    printf("#0242:2: %d\n",temp6.e);
     main7();
     main8();
     linux_kernel();