changeset 782:003067098032

code argument offset in caller and callee
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 20 Nov 2010 10:55:31 +0900
parents b8cb4e1ac922
children feeb9b9f8236
files Changes mc-code-arm.c mc-code-i64.c mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-code-spu.c mc-code.h mc-codegen.c mc-codegen.h mc-parse.c
diffstat 11 files changed, 134 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/Changes	Fri Nov 19 18:57:36 2010 +0900
+++ b/Changes	Sat Nov 20 10:55:31 2010 +0900
@@ -9879,6 +9879,18 @@
 
 string の長さを無制限にするのは難しそう。でも、i64.c でははみ出てる。
 
-
-
-
+Fri Nov 19 23:56:10 JST 2010
+
+code_ofset_diapl0 を 0 にしないと、code segment 側の呼び出しがずれる。
+
+function からの goto と、code からの goto の offset は、異なる。
+
+Sat Nov 20 09:45:56 JST 2010
+
+そうか。code_decl/pcode_decl で、argument offset を逆転させているので、
+       caller argument  と callee argument
+の code_arg_alignment が合わなくなるのね。code_code_arg_alignment ってのを
+作るべき?
+
+
+
--- a/mc-code-arm.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-code-arm.c	Sat Nov 20 10:55:31 2010 +0900
@@ -6584,9 +6584,9 @@
 #endif
 
 extern int 
-code_arg_alignment(int args,NMTBL *n, int type0,int sz)
-{
-    return code_arg_alignment0(args,n, type0,sz);
+code_arg_alignment(int args,NMTBL *n, int type0,int sz,int is_code)
+{
+    return code_arg_alignment0(args,n, type0,sz,is_code);
 }
 
 
--- a/mc-code-i64.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-code-i64.c	Sat Nov 20 10:55:31 2010 +0900
@@ -527,7 +527,7 @@
 #define arg_offset1  (0)
 #define ARG_LVAR_OFFSET 0x10000000
 
-#define code_disp_offset0 (4)
+#define code_disp_offset0 (0)              // should be zero
 // disp_offset
 int disp_offset = code_disp_offset0;
 
@@ -5158,7 +5158,7 @@
 }
 
 extern int 
-code_arg_alignment(int args,NMTBL *n, int type0,int sz)
+code_arg_alignment(int args,NMTBL *n, int type0,int sz, int is_code)
 {
     if(type0==CHAR||type0==UCHAR) {
         if (n->dsp==0) {
--- a/mc-code-ia32.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-code-ia32.c	Sat Nov 20 10:55:31 2010 +0900
@@ -4859,9 +4859,9 @@
 }
 
 extern int 
-code_arg_alignment(int args,NMTBL *n, int type0,int sz)
+code_arg_alignment(int args,NMTBL *n, int type0,int sz, int is_code)
 {
-    return code_arg_alignment0(args,n, type0,sz);
+    return code_arg_alignment0(args,n, type0,sz, is_code);
 }
 
 
--- a/mc-code-mips.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-code-mips.c	Sat Nov 20 10:55:31 2010 +0900
@@ -5901,9 +5901,9 @@
 #endif
 
 extern int 
-code_arg_alignment(int args,NMTBL *n, int type0,int sz)
-{
-    return code_arg_alignment0(args,n, type0,sz);
+code_arg_alignment(int args,NMTBL *n, int type0,int sz, int is_code)
+{
+    return code_arg_alignment0(args,n, type0,sz, is_code);
 }
 
 
--- a/mc-code-powerpc.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-code-powerpc.c	Sat Nov 20 10:55:31 2010 +0900
@@ -6781,9 +6781,9 @@
 #endif
 
 extern int 
-code_arg_alignment(int args,NMTBL *n, int type0,int sz)
-{
-    return code_arg_alignment0(args,n, type0,sz);
+code_arg_alignment(int args,NMTBL *n, int type0,int sz, int is_code)
+{
+    return code_arg_alignment0(args,n, type0,sz, is_code);
 }
 
 
--- a/mc-code-spu.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-code-spu.c	Sat Nov 20 10:55:31 2010 +0900
@@ -5048,7 +5048,7 @@
 #endif
 
 extern int
-code_arg_alignment(int args,NMTBL *n, int type0,int sz)
+code_arg_alignment(int args,NMTBL *n, int type0,int sz, int is_code)
 {
     if(type0==CHAR||type0==UCHAR) {
         if (n->dsp==0) {
--- a/mc-code.h	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-code.h	Sat Nov 20 10:55:31 2010 +0900
@@ -88,7 +88,7 @@
 extern void jmp(int l);
 extern int not_simple_p(int l);
 extern void code_save_stacks();
-extern int code_arg_alignment(int disp0,NMTBL *n,int type0,int sz);
+extern int code_arg_alignment(int disp0,NMTBL *n,int type0,int sz, int is_code);
 extern int code_lvar_alignment(int disp0,NMTBL *n,int type0,int sz);
 
 extern int get_register_var(NMTBL *n);
--- a/mc-codegen.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-codegen.c	Sat Nov 20 10:55:31 2010 +0900
@@ -108,6 +108,11 @@
     init_free_lvar_list();
 }
 
+/**
+    make register argments
+    and save it into memory (sigh...)
+    we should postpone code_save_argument_register
+ */
 extern void 
 arg_register(NMTBL *fnptr)
 {
@@ -1472,6 +1477,7 @@
 // maximum size of struct divide (don't make it large)
 
 #define ASSIGN_STRUCT_DIVIDE 40
+#define ARG_OFFSET_CODE 1
 
 extern void
 jump(int e1, int env)
@@ -1488,6 +1494,7 @@
     /* e1 = list4(FUNCTION,code_segment,arglist,ftype); */
 
     if (env) {
+        error(-1); // not supported
 	envreg = get_register_var(0);
 	g_expr_u(assign_expr0(envreg,env,int_type,int_type));
     }
@@ -1526,9 +1533,12 @@
 	    target=list5(list3n(LVAR,0,0), target,ty,e2,0);
 	}
         /* keep arg space for register variables */
-        // NMTBL n;
-	// arg_size = code_arg_alignment(arg_size, &n, ty, sz);
+#if ARG_OFFSET_CODE
+        NMTBL n;
+	arg_size = code_arg_alignment(arg_size, &n, ty, sz,1);
+#else
         arg_size += sz;
+#endif
 #if DEBUG_PARALLEL_ASSIGN
 if (lsrc)printf("## target %d ty %d+%d sz %d\n",car(car(target)),ty,cadr(car(target)),sz);
 #endif
@@ -1549,19 +1559,27 @@
     /*  複雑な式を前もって計算しておく     */
     /*  必要なら局所変数を用いる。         */
     /*  局所変数へのオフセットを覚えておく */
-    // int arg_offset = 0;
+#if ARG_OFFSET_CODE
+    NMTBL n;
+    n.dsp = 0;
+    int arg_offset = 0;
+    target = reverse0(target); 
+#endif
     for (e2 = target; e2; e2 = cadr(e2)) {	
 	t0=car(e2); s0=cadddr(e2);
 	sz=size(ty=caddr(e2));
+#if ARG_OFFSET_CODE
+	/* ここで、書込先アドレスを決める */
+	arg_offset = code_arg_alignment(arg_offset, &n, ty, sz,1);
 	if(car(t0)==LVAR) {
-	    /* ここで、書込先アドレスを決める */
-	    if (envreg) error(-1);
-	    // NMTBL n;
-	    // arg_offset = code_arg_alignment(arg_offset, &n, ty, sz);
-	    // cadr(t0) = arg_offset - n.dsp;
+	    cadr(t0) = n.dsp;
+	}
+#else
+	if(car(t0)==LVAR) {
 	    cadr(t0)=-arg_size;    // disp_offset?!
 	}
         arg_size-=sz;
+#endif
 #ifdef SAVE_ALL_NON_MEMORY
 	if (!is_simple(car(s0))) {
 #else
@@ -1576,7 +1594,7 @@
         } else if (is_same_type(t0,s0)) {
             if(cadr(t0)==cadr(s0)) {
 		if(is_writable(s0)) {
-		    caddddr(e2)=list3(s0,0,sz);
+		    caddddr(e2)=list3(s0,0,sz); // これなんだっけ?
 		    continue;
 		} else
 		    error(-1);
@@ -1607,8 +1625,7 @@
 		    case 2:
 		    case 3: caddr(e2) = USHORT; r = size_of_short; break;
 		    case 4: if (lp64) { caddr(e2) = UNSIGNED; r = size_of_int; break; }
-		    default: if (lp64) { caddr(e2) = ULONGLONG; r = int_size; break; }
-		             caddr(e2) = UNSIGNED; r = size_of_int;
+		    default: caddr(e2) = int_unsigned; r = size_of_int;
 		    }
 		    if (e4==int_size) e3=cadr(e2);
 		    car(e2) =  list3n(LVAR,cadr(t0)+e4,0);
@@ -3499,7 +3516,7 @@
 	    n->dsp = args++;
 	    n->sc = IVAR;
 	} else {
-	    args = code_arg_alignment(args,n,type0,sz);
+	    args = code_arg_alignment(args,n,type0,sz, is_code(fnptr));
 	}
 
 	caddr(fnptr->dsp)=sz;
@@ -3555,34 +3572,68 @@
 
 // standard 32bit alignment 
 
+// for code
 extern int
-code_arg_alignment0(int args,NMTBL *n, int type0,int sz)
+code_arg_alignment1(int offset,NMTBL *n, int type0,int sz, int is_code)
 {
     if(type0==CHAR||type0==UCHAR) {
         if (n->dsp==0) {
-            n->dsp = args;
+            n->dsp = -offset;
             if (endian) n->dsp += size_of_int-1;
         }
-        args += size_of_int;
+        offset += size_of_int;
     } else if(type0==SHORT||type0==USHORT) {
         if (n->dsp==0) {
-            n->dsp = args;
+            n->dsp = -offset;
             if (endian) n->dsp += size_of_int-size_of_short;
         }
-        args += size_of_int;
+        offset += size_of_int;
     } else if(type0>0&&(car(type0)==UNION||car(type0)==STRUCT)) {
         /* alignment in struct in argument */
         /* should be GCD of member alignment */
         /* __attribute(alignment(16)) is ignored in argments */
-        n->dsp = args;
-        args += align(sz,size_of_int);
+        n->dsp = -offset;
+        offset += align(sz,size_of_int);
     } else {
         /* if (n->dsp==0) (argument list in ADECL is useless, type
            list can be found in type ) */
-        n->dsp = args;
-        args += sz; 
-    }
-    return args;
+        n->dsp = -offset;
+        offset += sz; 
+    }
+    return offset;
+}
+
+// for function
+extern int
+code_arg_alignment0(int offset,NMTBL *n, int type0,int sz, int is_code)
+{
+    if (is_code) return code_arg_alignment1(offset,n,type0,sz,is_code);
+
+    if(type0==CHAR||type0==UCHAR) {
+        if (n->dsp==0) {
+            n->dsp = offset;
+            if (endian) n->dsp += size_of_int-1;
+        }
+        offset += size_of_int;
+    } else if(type0==SHORT||type0==USHORT) {
+        if (n->dsp==0) {
+            n->dsp = offset;
+            if (endian) n->dsp += size_of_int-size_of_short;
+        }
+        offset += size_of_int;
+    } else if(type0>0&&(car(type0)==UNION||car(type0)==STRUCT)) {
+        /* alignment in struct in argument */
+        /* should be GCD of member alignment */
+        /* __attribute(alignment(16)) is ignored in argments */
+        n->dsp = offset;
+        offset += align(sz,size_of_int);
+    } else {
+        /* if (n->dsp==0) (argument list in ADECL is useless, type
+           list can be found in type ) */
+        n->dsp = offset;
+        offset += sz; 
+    }
+    return offset;
 }
 
 // standard 32bit alignment for local variable
@@ -3605,9 +3656,9 @@
 
 // for mc-parse.c
 extern int
-arg_alignment(int args,NMTBL *n, int type0,int sz)
+arg_alignment(int args,NMTBL *n, int type0,int sz, int is_code)
 {
-    return code_arg_alignment(args,n, type0,sz);
+    return code_arg_alignment(args,n, type0,sz, is_code);
 }
 
 extern char *
--- a/mc-codegen.h	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-codegen.h	Sat Nov 20 10:55:31 2010 +0900
@@ -104,7 +104,7 @@
 extern void gen_label_call(int l);
 extern void flush_delayed_decl_data(int v);
 extern void jump(int e,int env);
-extern int arg_alignment(int disp0,NMTBL *n,int type0,int sz);
+extern int arg_alignment(int disp0,NMTBL *n,int type0,int sz, int is_code);
 
 
 /* used by mc-inline */
@@ -145,7 +145,7 @@
 extern int get_ptr_cache(NMTBL *nptr);
 
 // standard 32bit alignment
-extern int code_arg_alignment0(int disp0,NMTBL *n,int type0,int sz);
+extern int code_arg_alignment0(int disp0,NMTBL *n,int type0,int sz, int is_code);
 extern int code_lvar_alignment0(int disp0,NMTBL *n,int type0,int sz);
 
 extern int ilog(int i);
--- a/mc-parse.c	Fri Nov 19 18:57:36 2010 +0900
+++ b/mc-parse.c	Sat Nov 20 10:55:31 2010 +0900
@@ -2038,13 +2038,29 @@
     conv->localvar_end_();
 }
 
+extern void
+code_arguments_fix(NMTBL *fnptr)
+{
+#if 0
+    /* reverse all argument offset (with size) */
+    int t;
+    int arglist = fnptr->dsp;
+    for(t=arglist;t;t=cadr(t)) {
+	NMTBL *n=ncadddr(t);
+	if(n->sc==LVAR)
+	    n->dsp = -n->dsp-caddr(t);
+    }
+#endif
+    arg_register(fnptr);
+}
+
 /* code sgement
      simpler than fdecl, because it does not have return value.
  */
 static void
 code_decl(NMTBL *n)
 {
-    int t,arglist;
+    int arglist;
     int sinmode = 0;
     int arg_disp;
 
@@ -2085,16 +2101,8 @@
 	fnptr->dsp = arg_reorder(arglist,fnptr->dsp);
 	// fnptr->dsp = reverse0(fnptr->dsp);
     }
-    if (!inmode) {
-	/* reverse all argument offset (with size) */
-	arglist = fnptr->dsp;
-	for(t=arglist;t;t=cadr(t)) {
-	    n=ncadddr(t);
-	    if(n->sc==LVAR)
-		n->dsp = -n->dsp-caddr(t);
-	}
-	arg_register(fnptr);
-    }
+    if (!inmode) 
+	code_arguments_fix(fnptr);
     arg_disp = args;
 
     typedefed=0;
@@ -2242,7 +2250,7 @@
 /*     calcurate argument offset here */
 
 static int
-copy_arg(int arg) 
+copy_arg(int arg, int is_code) 
 {
     NMTBL *a,*n1;
     int offset=0;
@@ -2261,7 +2269,7 @@
 
 	t = type_value(n1->ty);
 	sz = size(t);
-	offset = arg_alignment(offset,n1,t,sz);
+	offset = arg_alignment(offset,n1,t,sz, is_code);
 
 	nargs=list4n(car(arg),nargs,caddr(arg),n1);
     }
@@ -2306,7 +2314,7 @@
     fnptr->next=0;
     
     // make copied called function argment
-    nargs = copy_arg(n->dsp);
+    nargs = copy_arg(n->dsp, 0);
 
     // fdecl_struct(fnptr->ty);  already done by fdecl before
     fnptr->dsp=reverse0(nargs);
@@ -2348,8 +2356,8 @@
 pcode_decl(NMTBL *n)
 {
     int e;
-    int arg,nargs,cargs,t;
-    NMTBL *a,*n1;
+    int arg,nargs,cargs;
+    NMTBL *a;
 
     if (has_attr(n,GENERATED)) return;
     set_attr(n,GENERATED,0);
@@ -2376,18 +2384,12 @@
     fnptr->next=0;
     
     // make copied called function argment
-    nargs = copy_arg(n->dsp);
+    nargs = copy_arg(n->dsp,1);
 
     // fdecl_struct(fnptr->ty);  already done by fdecl before
     fnptr->dsp=reverse0(nargs);
 
-    /* reverse all argument offset (with size) */
-    arg = fnptr->dsp;
-    for(t=arg;t;t=cadr(t)) {
-	n1=ncadddr(t);
-	if(n1->sc==LVAR)
-	    n1->dsp = -n1->dsp-caddr(t);
-    }
+    code_arguments_fix(fnptr);
     
     retcont = 0;
     tmp_struct = 0;