changeset 886:9a4b92984cf1

inline struct initialization in an expression
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 05 Apr 2014 23:04:38 +0900
parents 1a027275743d
children b519139c25e2
files mc-codegen.c mc-codegen.h mc-inline.c test/strinit.c
diffstat 4 files changed, 30 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/mc-codegen.c	Sat Apr 05 21:12:43 2014 +0900
+++ b/mc-codegen.c	Sat Apr 05 23:04:38 2014 +0900
@@ -3962,6 +3962,29 @@
 //
 // local variable initialization
 //
+extern void
+zfill(int var, int offset, int sz)
+{
+    int fill = sz;
+    if (fill>63) {
+        // call bzero 
+    }
+    while(fill>0) {   // should consider alignment
+        int t = UCHAR;
+        if (fill>=size_of_longlong) t = ULONGLONG;
+        else if (fill>=size_of_int) t = UNSIGNED;
+        else if (fill>=size_of_short) t = USHORT;
+        fill -= size(t);
+        int ass = assign_expr0(
+            (offset+fill)?
+                lp64?list3(LADD,var,llist2(LCONST,offset+fill)):    // binop?
+                     list3(ADD,var,list2(CONST,offset+fill)):
+                var,
+            list2(CONST,0),t,t); // already correct_typed
+        init_vars = list2(ass,init_vars);
+    }
+}
+
 
 extern int
 assign_data(int e, int t, int v,int offset)
@@ -3999,6 +4022,7 @@
     case STAT:               // inline case
         if (t==EMPTY) {
             /* empty space in partial initialization */
+            zfill(v,offset,cadr(e));
             return offset+cadr(e);
         }
         /* If this is a local declared constant, we don't have to assign.
--- a/mc-codegen.h	Sat Apr 05 21:12:43 2014 +0900
+++ b/mc-codegen.h	Sat Apr 05 23:04:38 2014 +0900
@@ -49,6 +49,7 @@
 
 extern NMTBL * def(NMTBL *n,int ctmode);
 extern int arg_reorder(int arg,int new_arg);
+extern void zfill(int var, int offset, int sz);
 extern int assign_data(int e, int t, int v,int offset);
 extern int assign_expr(int e1,int e2,int t);
 extern int assign_expr0(int e1,int e2,int t,int type0);
--- a/mc-inline.c	Sat Apr 05 21:12:43 2014 +0900
+++ b/mc-inline.c	Sat Apr 05 23:04:38 2014 +0900
@@ -741,6 +741,7 @@
     }
     if (t==EMPTY) {
         /* empty space in partial initialization */
+        zfill(var,offset,cadr(e));
         return offset+cadr(e);
     }
     type = t;
@@ -833,33 +834,6 @@
     return offset;
 }
 
-#if 0
-static void
-pzfill(int var, int offset, int sz)
-{
-    int c0 = list2(CONST,0);
-    int l0 = llist2(LCONST,0L);
-    while(sz>0) {
-       int t,c;
-       switch(sz) {
-       case 1: t = UCHAR; c = c0; break;
-       case 2: t = USHORT; c = c0; break;
-       case 4: t = UNSIGNED; c = c0; break;
-       default: t = ULONGLONG; c = l0; break;
-       }
-       int ass = assign_expr0(
-               offset?
-                   lp64?list3(LADD,var,llist2(LCONST,offset)):    // binop?
-                        list3(ADD,var,list2(CONST,offset)):
-                   var,
-               c ,t,t); // already correct_typed
-       init_vars = list2(ass,init_vars);
-       sz -= size(t);
-       offset += size(t);
-    }
-}
-#endif
-
 static int
 pdecl_data_field(int var,int init,int target_type,int offset)
 {
--- a/test/strinit.c	Sat Apr 05 21:12:43 2014 +0900
+++ b/test/strinit.c	Sat Apr 05 23:04:38 2014 +0900
@@ -224,8 +224,11 @@
 
 INLINE int main8() {
      struct arg a = {11,22,33};
+     struct arg b = {.fuga=44,.aho=55};
      f(a);
+     f(b);
      f((struct arg){.fuga = 3,.aho=5});
+     f((struct arg){.hage = 3});
      printf("#0226:%d %d\n",a.aho,((struct arg){.aho=120, .hage=55}).aho); return 0;
 }
 
@@ -256,7 +259,7 @@
     printf("#0253:1: %ld\n",temp1.b);
     printf("#0254:2: %ld\n",temp2.c);
     printf("#0255:2: %ld\n",temp2.e);
-    printf("#0256:2: %ld\n",temp2.b);
+    printf("#0256:2: %ld\n",temp2.b); // non explict initialized  value
     printf("#0257:2: %d\n",(void*)temp3.c==b);
     printf("#0258:2: %d\n",temp3.c==(long)b);
     printf("#0259:2: %d\n",temp3.a==(long)&b);