changeset 547:9defd9459016

*** empty log message ***
author kono
date Tue, 03 Jan 2006 12:02:26 +0900
parents 9cdd602cc050
children 650717de878b
files Changes mc-codegen.c mc-codegen.h mc-parse.c mc-parse.h test/strinit.c
diffstat 6 files changed, 222 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/Changes	Mon Jan 02 17:37:15 2006 +0900
+++ b/Changes	Tue Jan 03 12:02:26 2006 +0900
@@ -7777,3 +7777,9 @@
 
 linux が通らなくなってるね。struct field のネストね。通したはずなんだが。
 
+getsym で構造体のデータがlocal defineされてしまう。SFDINIT modeか。
+LDECL だけで定義するんじゃないのか? 大域変数の場合もあるけどね。
+
+decl_data で、構造体の中身の代入には assign_data で代入文が
+出てしまうらしい。でも、それでもemit_dataに飛ぶ理由は不明だな。
+
--- a/mc-codegen.c	Mon Jan 02 17:37:15 2006 +0900
+++ b/mc-codegen.c	Tue Jan 03 12:02:26 2006 +0900
@@ -765,9 +765,11 @@
 is_const(int e)
 {
     switch(car(e)) {
+	case ADDRESS:
+	    e = cadr(e);
+	    return (car(e)==GVAR||car(e)==FNAME||car(e)==LVAR);
+	case STRING:
 	case CONST: case LCONST: case FCONST: case DCONST:
-	// case ADDRESS:   what happen?
-	// case POINTER:
 	return 1;
     default:
 	return 0;
@@ -1194,39 +1196,65 @@
     return 0;
 }
 
-extern int
-is_memory(int e1)
+static int
+is_memory0(int e1,int *global)
 {
+    *global=0;
     switch(car(e1)) {
-    case CRLVAR  :
     case CRGVAR  :
     case CURGVAR :
+	*global=1;
+    case CRLVAR  :
+    case CURLVAR  :
 	return 1;
-    case SURGVAR:
     case SRGVAR :
+    case SURGVAR:
+	*global=1;
+    case SRLVAR :
+    case SURLVAR :
 	return size_of_short;
-    case LVAR :
+    // case GVAR :
+    case RGVAR:
+	*global=1;
+    // case LVAR :
     case RLVAR:
-    case GVAR :
     case REGISTER :
-    case RGVAR:
 	return size_of_int;
+    case FRGVAR :
+	*global=1;
     case FRLVAR  :
-    case FRGVAR :
     case FREGISTER :
 	return size_of_float;
+    case DRGVAR  :
+	*global=1;
     case DRLVAR  :
-    case DRGVAR  :
     case DREGISTER  :
 	return size_of_double;
     case LRGVAR :
+    case LURGVAR :
+	*global=1;
     case LRLVAR :
+    case LURLVAR :
     case LREGISTER:
 	return size_of_longlong;
     }
     return 0;
 }
 
+extern int
+is_memory(int e1)
+{
+    int global;
+    return is_memory0(e1,&global);
+}
+
+extern int
+is_local_memory(int e1)
+{
+    int global;
+    return is_memory0(e1,&global) && !global;
+}
+
 static int
 check_source(int source,int e)
 {
@@ -3018,10 +3046,9 @@
 	}
     }
     if(mode==GDECL) {
+	if (!is_const(e)) error(INERR);
  	emit_data(e,t,n);
-    } else if(mode==STADECL) {
- 	emit_data(e,t,n);
-    } else if(mode==LDECL) {
+    } else if(mode==LDECL || (mode==STADECL&&local_nptr&&(n=local_nptr))) {
 	if (t==EMPTY) {
 	    /* empty space in partial initialization */
 	    return offset+cadr(e);
@@ -3035,6 +3062,9 @@
 	    list3(n->sc,n->dsp+offset,(int)n),
 	e,t,type);
 	init_vars = list2(ass,init_vars);
+    } else if(mode==STADECL) {
+	if (!is_const(e)) error(INERR);
+	else emit_data(e,t,n);
     } else if(mode==SFDINIT) {
 	decl_str_init=insert_ascend(decl_str_init,
 		list4(offset,0,e,t),str_init_eq);
@@ -3087,6 +3117,7 @@
     if ((sz=(offset1+sz-offset))>0)
         assign_data(list2(CONST,sz),EMPTY,n,offset0);
     decl_str_init = 0;
+    local_nptr = 0;
     mode=smode;
 }
 
--- a/mc-codegen.h	Mon Jan 02 17:37:15 2006 +0900
+++ b/mc-codegen.h	Tue Jan 03 12:02:26 2006 +0900
@@ -90,6 +90,7 @@
 extern int g_expr_u(int e1);
 extern int is_const(int e);
 extern int is_memory(int e);
+extern int is_local_memory(int e1);
 extern int is_code(NMTBL *fnptr);
 extern int is_function(NMTBL *fnptr);
 extern int is_inline(NMTBL *fnptr);
--- a/mc-parse.c	Mon Jan 02 17:37:15 2006 +0900
+++ b/mc-parse.c	Tue Jan 03 12:02:26 2006 +0900
@@ -27,57 +27,78 @@
 static int lfree_type_limit;
 
 NMTBL null_nptr;
-NMTBL *fnptr;
-NMTBL *nptr,*gnptr;
-static NMTBL *htable0[GSYMS];
+NMTBL *fnptr;                    // current compiling function
+NMTBL *nptr,*gnptr;              // current name ptr
+NMTBL *local_nptr;               // local variable initialzation
+
+static NMTBL *htable0[GSYMS];    // hash table
 NMTBL **htable = htable0;
+
+// string pool
 struct cheap *cheap,*cheap0;
-
 static struct cheap *nptr_pool,*nptr_pool0;
 static NMTBL *free_nptr_list;
 
-static int current_scope;
-int attribute;
-
-int inline_funcs;
-
+static int current_scope;         // scope unwinder
+
+int attribute;                    // current attribte list
+
+int inline_funcs;                 // list of inline functions (for pfdecl)
+
+// current linebuffer and pointer
 char linebuf[LBUFSIZE];
 char *chptr;
-int args,init_vars;
-int *heap;
+int ch;                           // pre-read character
+int chptrsave,chsave;             // linebuffer stack (for macro)
+static struct cheap * new_cheap(); // increment cheap
+
+int chk;
+int args;                         // function argument list
+int init_vars;                    // list of local variable initialize
+/*
+     heap 0---global----gfree->  <-lfree--local----TOP (heapsize)
+       lfree is reset in top level
+ */
+int *heap;                        // heap pointer
+int lfree;                        // local heap top
+static int gfree;                 // global heap top
 int heapsize = HEAPSIZE;
-int asmf;
+int asmf;                         // in asm (no longer used)
 int bit_field_disp;
-int blabel,dlabel;
-int ch;
-int chk;
-int chptrsave; 
-int chsave; 
-int cslabel,control;
+int blabel,dlabel;                // break label, default label
+int clabel;                       // continue label
+int cslabel;                      // case label list
 int csvalue1;
+int cslist;
+int control;                      // execution will be reached here
 int debug; 
-int fields;
-int glineno; 
-int gtypedefed;
-int in_comment; 
-int in_quote; 
-int labelno,gpc,disp;
-int lastexp;
-int lfree; 
-int lineno; 
-int lsrc; 
-int retlabel,retpending,retcont;
-int pending_jmp;
-int struct_return;
-int sym,type,mode,stmode,ctmode,inmode;
-int typedefed;
-int decl_str_init;
-int parse;
-
-NMTBL *local_static_list,*global_list;
-
+int fields;                       // struct field list
+int gtypedefed;                   // global typedef list (for mc-tree.c)
+int typedefed;                    // local  typedef list (for mc-tree.c)
+int in_comment;                   // getsym mode
+int in_quote;                     // getsym mode
+int labelno;                      // label number
+int disp;                         // local variable offset
+int gpc;                          // global variable count (nolonger used)
+static int stat_no;               // local static unique number
+int lastexp;                      // delayed last expression in a statement
+int lineno;                       // lineno in a file
+int glineno;                      // total lineno
+int lsrc;                         //   source listing flag
+int retlabel,retpending,retcont;  // return label
+int pending_jmp;                  // previous jump (not yet generated)
+int struct_return;                //    function returns struct
+int sym,type;                     // current symbol and type
+int mode,stmode,ctmode,inmode;    // mode, storage mode, constant mode, inline
+int decl_str_init;                //   delayed local struct initialize
+int parse;                        // parse tree list for inline
+
+NMTBL *local_static_list,*global_list;   // list of static, global variable
+
+//  file pointers
 struct {int fd,ln;char *name0;int inc;FILE *fcb;} *filep,filestack[FILES];
 
+//  function for Recursive decent nodes
 static NMTBL *decl0(void),*decl1(void);
 static int append3(int p,int a1,int a2);
 static int expr0(void);
@@ -144,7 +165,7 @@
 static void qualifiers();
 static void attributes();
 
-static struct cheap * new_cheap();
+// current value of constant symbol
 
 #if FLOAT_CODE
 static double dsymval;
@@ -153,30 +174,24 @@
 static long long  lsymval;
 #endif
 static int symval;
-static int gfree;
-
-static int sdecl_f = 1;
-static int stypedecl;
+
+static int sdecl_f = 1;        // in struct defenition (for converter)
+static int stypedecl;          // in type defenition (for converter)
 
 static Converter *conv = &null_converter;
 /* Converter *conv = &c_converter; */
 
-static char *ccout = 0;
-
+static char *ccout = 0;        // output file name
+
+//  include file search path
 #define MAX_INCLUDE_PATH_COUNT 10
 char *include_path[MAX_INCLUDE_PATH_COUNT];
 int include_path_count;
 extern char *l_include_path[];
-
-static
-char current_file_dir[LBUFSIZE];
+static char current_file_dir[LBUFSIZE];   // keep track include file directory
 
 static int ac,ac2;
 static char **av;
-int cslist;
-int clabel;
-static int ilabel;
-static int stat_no;
 
 int
 main(int argc, char **argv)
@@ -409,7 +424,7 @@
     gpc=glineno=0;
     if (!heap) heap = (int *)malloc(heapsize*sizeof(int));
     if (!heap) error(MMERR);
-    gfree=ilabel=1;
+    gfree=1;
     labelno=2;
     set_lfree(heapsize);
 }
@@ -1325,7 +1340,7 @@
     a[] = {,,,,};
  */
 
-#define LOCAL_STRUCT_INIT_STATIC 0
+#define LOCAL_STRUCT_INIT_STATIC 1
 
 static void
 decl_data_field(int type,NMTBL *n,int offset)
@@ -1367,6 +1382,8 @@
     mode = mode_save;
 }
 
+
+#if LOCAL_STRUCT_INIT_STATIC 
 static void
 local_struct_static(NMTBL *n)
 {
@@ -1376,20 +1393,25 @@
     // uninitialized part should be 0.
     // local var init cannot postponed because of assign_expr0/type
     //  if initialization contains expressions,
-    //  we cannot do it in STADECL
+    //  we cannot do it in STADECL, but we can assign later in this mode
+    if (local_nptr)  error(-1);
+    local_nptr = n;  // will be clear in flush
+
     nptr0=new_static_name("__lstruct",'_');
     nptr0->next = local_static_list; local_static_list = nptr0;
     nptr0->sc = STATIC;
     nptr0->ty = t = type;
     mode=STADECL;
     decl_data_field(type,nptr0,offset);
-    flush_delayed_decl_data(nptr0);
+    // do struct assignment before flushed assignment expression
     init_vars = list2(
 	list4(STASS,list3(LVAR,n->dsp+offset,0),
 	    list3(RSTRUCT,list3(GVAR,0,(int)nptr0),sz),sz),
 	init_vars);
+    flush_delayed_decl_data(nptr0);
     mode = smode;
 }
+#endif
 
 /*
     data structure initialization
@@ -1426,7 +1448,7 @@
 	mode = mode_save;
  	//if(car(e)!=CONST && t==CHAR)
  	//    error(TYERR);
-	// correct_type is too weak, should hanle ADDRESS/ARRAY
+	// correct_type is too weak, should handle ADDRESS/ARRAY
 	//   assign_data call rvalue, this is a duplicate rvalue call (correct?)
 	if (!scalar(type) && (type>0 && (car(type)!=ADDRESS && car(type)!=ARRAY)))
 	    e = correct_type(rvalue(e),t0);
--- a/mc-parse.h	Mon Jan 02 17:37:15 2006 +0900
+++ b/mc-parse.h	Tue Jan 03 12:02:26 2006 +0900
@@ -18,7 +18,7 @@
 extern NMTBL *nptr;         /* current name table */
 extern NMTBL *gnptr;        /* currrent global name table (unsed?) */
 extern NMTBL *fnptr;        /* current function name table */
-extern int gtypedefed;      /* global typedef list */
+extern NMTBL *local_nptr;   /* initialzing local variable nptr */
 extern int retlabel;        /* return label */
 extern int retpending;      /* jump to return label suspended */
 extern int retcont;         /* continuation label */
@@ -87,6 +87,7 @@
 
 /* used in mc-tree.c */
 
+extern int gtypedefed;      /* global typedef list */
 extern int typedefed;        /* accumlated typedef name */
 
 /* global variable end */
--- a/test/strinit.c	Mon Jan 02 17:37:15 2006 +0900
+++ b/test/strinit.c	Tue Jan 03 12:02:26 2006 +0900
@@ -31,6 +31,8 @@
 struct temp temp4 = { 1,2,3,4,5,6,7};
 struct temp temp7 = { 1,2,3,4,{5,6},7};
 
+void
+linux_kernel();
 
 int
 main()
@@ -55,5 +57,92 @@
     printf("#0050:2: %d\n",temp3.a==(int)&b);
     printf("#0051:2: %d\n",temp4.m.j);
     printf("#0051:2: %d\n",temp5.m.j);
+    linux_kernel();
     return 0;
 }
+
+typedef unsigned int __u32;
+typedef unsigned short __u16;
+typedef unsigned char __u8;
+typedef unsigned int u32;
+typedef unsigned short u16;
+typedef unsigned char u8;
+struct in6_addr { char hoge[128]; };
+
+struct flowi {
+        int oif;
+        int iif;
+
+        union {
+                struct {
+                        __u32 daddr;
+                        __u32 saddr;
+                        __u32 fwmark;
+                        __u8 tos;
+                        __u8 scope;
+                } ip4_u;
+
+                struct {
+                        struct in6_addr daddr;
+                        struct in6_addr saddr;
+                        __u32 flowlabel;
+                } ip6_u;
+
+                struct {
+                        __u16 daddr;
+                        __u16 saddr;
+                        __u32 fwmark;
+                        __u8 scope;
+                } dn_u;
+        } nl_u;
+        __u8 proto;
+        __u8 flags;
+        union {
+                struct {
+                        __u16 sport;
+                        __u16 dport;
+                } ports;
+
+                struct {
+                        __u8 type;
+                        __u8 code__;
+                } icmpt;
+
+                struct {
+                        __u16 sport;
+                        __u16 dport;
+
+                        __u8 objnum;
+                        __u8 objnamel;
+                        __u8 objname[16];
+                } dnports;
+
+                __u32 spi;
+        } uli_u;
+} __attribute__((__aligned__(32/8)));
+
+
+static __inline__ __attribute__((always_inline)) __attribute__((always_inline)) 
+int ip_route_connect(/* struct rtable **rp, */ u32 dst,
+                                   u32 src, u32 tos, int oif, u8 protocol,
+                                   u16 sport, u16 dport /*, struct sock *sk */)
+{
+        struct flowi fl = { .oif = oif,
+                            .nl_u = { .ip4_u = { .daddr = dst,
+                                                 .saddr = src,
+                                                 .tos = tos } },
+                            .proto = protocol,
+                            .uli_u = { .ports =
+                                       { .sport = sport,
+                                         .dport = dport } } };
+
+	printf("%d %d\n",fl.oif,fl.uli_u.ports.dport);
+}
+
+void
+linux_kernel()
+{
+    ip_route_connect(1,2,3,4,5,6,7);
+}
+
+// end