changeset 572:388baa7d4bee

more strict errors. Undeclared identifier Goto to function, calling code segement
author kono
date Fri, 13 Jan 2006 01:51:39 +0900
parents d78f78ff50d1
children 3de2ae36c7e5
files .gdbinit Changes mc-code-powerpc.c mc-codegen.c mc-parse.c mc.h test/conv1.c test/fact-a.c test/fact.c test/func_conv_err.c test/goto.c test/test1.c test/tmp1.c
diffstat 13 files changed, 201 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Thu Jan 12 13:56:44 2006 +0900
+++ b/.gdbinit	Fri Jan 13 01:51:39 2006 +0900
@@ -47,8 +47,9 @@
 # run -s test/too-long-argument.c
 # run -s test/strinit.c
 # run -DINLINE=inline test/scope.c
-run -DINLINE=inline test/code-gen-all.c
+# run -DINLINE=inline test/code-gen-all.c
 # run  -DINLINE=inline test/bitfield1.c
 # run -s test/linux_kernel.c.--- 
 # run -s test/stralign.c
 # run -s test/putenemy.c
+run -s test/func_conv_err.c
--- a/Changes	Thu Jan 12 13:56:44 2006 +0900
+++ b/Changes	Fri Jan 13 01:51:39 2006 +0900
@@ -8244,5 +8244,39 @@
 とすれば良かったわけね。まぁ、pexpr でひっかかりまくる
 可能性はあるが....
 
-
-
+Thu Jan 12 22:28:32 JST 2006
+
+code hoge()
+{
+    goto hoge();
+}
+
+int
+f()
+{
+}
+
+code hoga()
+{
+    f();
+
+    goto f();   // bad
+}
+
+main()
+{
+    hoge();     // bad
+}
+
+最初の方のエラーを出せないな。
+
+なんか、エラーにすると正しいものもエラーになってしまう。
+
+function として使われたかどうか、code として
+使われたかどうかのフラグが必要か。attribute?
+
+一回、goto f(); では function として処理しちゃっている
+から、構文解析レベルではチェックできないな。
+mode を変えないと。stmode?
+
+
--- a/mc-code-powerpc.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/mc-code-powerpc.c	Fri Jan 13 01:51:39 2006 +0900
@@ -8,8 +8,12 @@
 #include "mc-codegen.h"
 
 char *l_include_path[] = {
-    "/usr/include/",
-    "/usr/lib/gcc/i686-apple-darwin8/4.0.0/include/",
+ "/usr/local/include",
+ "/usr/lib/gcc/powerpc-apple-darwin8/4.0.0/include",
+ "/usr/lib/gcc/i686-apple-darwin8/4.0.0/include/",
+ "/usr/include",
+ "/System/Library/Frameworks",
+ "/Library/Frameworks",
     0
 };
 
--- a/mc-codegen.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/mc-codegen.c	Fri Jan 13 01:51:39 2006 +0900
@@ -250,9 +250,18 @@
 	code_string(e1,USE_CREG);
 	return ADDRESS;
     case FUNCTION:
+	if (car(e2)==FNAME&&is_code((NMTBL*)cadr(e2))) {
+	    // error(FNERR);
+	    jump(e1,0);
+	    return VOID;
+	} 
 	t = function(e1);
 	return t;
     case CODE:
+	if (car(e2)==FNAME&&is_code((NMTBL*)cadr(e2))) {
+	    // error(GTERR);
+	    return function(e1);
+	}
 	jump(e2,caddr(e1));
 	return VOID;
     case INLINE:
@@ -791,14 +800,14 @@
 is_code(NMTBL *fnptr)
 {
     int type = type_value(fnptr->ty);
-    return type==CODE|| (type>0 && car(type)==CODE);
+    return (type==CODE|| (type>0 && car(type)==CODE));
 }
 
 extern int 
 is_function(NMTBL *fnptr)
 {
     int type = type_value(fnptr->ty);
-    return type==FUNCTION || (type>0 && car(type)==FUNCTION);
+    return (type==FUNCTION || (type>0 && car(type)==FUNCTION));
 }
 
 extern int 
@@ -1465,9 +1474,7 @@
     e2 = cadr(e1);
     if (car(e2) == FNAME) {	
 	code0=(NMTBL *)cadr(e2);
-	if (!is_code(code0)) {
-	    error(TYERR); return;
-	}
+	// if (!is_code(code0)) { error(TYERR); return; }
     } else {	/* indirect */
 	g_expr(e2);
 	emit_push();
@@ -3371,9 +3378,13 @@
     if (n->sc==EMPTY) {
 	n->sc=EXTRN;
 	n->ty=type;
-    } else if(is_code(n)) compatible(cadr(n->ty),cadr(type));
-    else if(is_function(n)) compatible(cadr(n->ty),cadr(type));
-    else {
+    } else if(is_code(n)) {
+	// if (car(type0)!=CODE) error(TYERR);
+	compatible(cadr(n->ty),cadr(type));
+    } else if(is_function(n)) {
+	// if (car(type0)!=FUNCTION) error(TYERR);
+	compatible(cadr(n->ty),cadr(type));
+    } else {
 	error(DCERR);
     }
 }
--- a/mc-parse.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/mc-parse.c	Fri Jan 13 01:51:39 2006 +0900
@@ -354,16 +354,20 @@
     case CNERR:  // "Constant required" :
     case CHERR:  // "Illegal character" :
     case MCERR:  // "Macro syntax" :
-    case INCERR:  // "Include syntax" :
+    case INCERR: // "Include syntax" :
     case TYERR:  // "Type mismatch" :
     case LVERR:  // "Lvalue required" :
     case UDERR:  // "Undeclared identifier" :
-    case OPTION:  // "Illegal option" :
+    case OPTION: // "Illegal option" :
     case INERR:  // "bad initialization" :
     case AGERR:  // "wrong number of arguments" :
     case CODE_ERR:  // "goto is necessary" :
     case ILERR:  // "inline error" :
     case SIERR:  // "non brace in struct init error" :
+    case GTERR:  
+    case FNERR:  
+    case UCERR:  
+    case UFERR:  
     return 0;
     }
     return 1;
@@ -426,6 +430,10 @@
 	(n==CODE_ERR) ? "goto is necessary" :
 	(n==ILERR) ? "inline error" :
 	(n==SIERR) ? "warning: missing braces around initializer" :
+	(n==GTERR) ? "cannot goto to a function" :
+	(n==FNERR) ? "calling a code segement, use goto" :
+	(n==UCERR) ? "already used as code segement" :
+	(n==UFERR) ? "already used as function" :
 	(n==CSERR) ? "no excutable code in switch" :
 	"Bug of compiler");
     errmsg();
@@ -855,8 +863,10 @@
         stypedecl=sd;
 	if (type<0) error(DCERR);
 	else if (car(type)==CODE) {
+	    if (is_function(n)) error(UFERR);
 	    code_decl(n); return;
 	} else if (car(type)==FUNCTION) {
+	    if (is_code(n)) error(UCERR);
 	    fdecl(n); return;
 	} else error(DCERR);
     } else {
@@ -1149,7 +1159,7 @@
 	    if(stmode==EXTRN) n->sc=EXTRN;
 	    else if(stmode==STATIC) n->sc=STATIC;
 	    if (type==CODE) {
-		n->ty=CODE;
+		// n->ty=CODE;
 		if(sym==RPAR) {
 		    getsym(0);arg=0;
 		} else {
@@ -2773,16 +2783,47 @@
     retpending = 1;
 }
 
+/*   CbC continuation */
+static void
+dojump(int e1,int env)
+{
+    int e2 = cadr(e1);
+    NMTBL *nptr0;
+    conv->jump_(env);
+    if (car(e2) == FNAME) {
+	nptr0=(NMTBL *)cadr(e2);
+	if (nptr0->sc==EMPTY)
+	    nptr0->sc = EXTRN1;
+	else if(nptr0->sc==FUNCTION)
+	    nptr0->sc = CODE;
+	if (nptr0->ty>0&&car(nptr0->ty)==FUNCTION)
+	    error(GTERR);
+	    // car(nptr0->ty)=CODE;
+    }
+    if (inmode) {
+	parse = list3(ST_GOTO,parse,list3(CODE,e1,env));
+    } else {
+	gexpr(list3(CODE,e1,env),0);
+    }
+    control=0;
+    conv->sm_();
+    checksym(SM);
+    return;
+}
+
 static void
 dogoto(void)
 {
     NMTBL *nptr0;
-    int t,e1,e2,env;
+    int t,e1,env;
+    int sstmode=stmode;
 
     checkret();
     conv->goto_();
+    stmode=CODE;
     getsym(0);
     e1 = expr(0);
+    stmode=sstmode;
     t=car(e1);
     if (type==VOID) {
 	/* indirect goto */
@@ -2826,27 +2867,9 @@
     } else {
 	env = 0;
     }
-    if (t==FUNCTION) {
+    if (t==CODE) {
         /*   CbC continuation */
-	conv->jump_(env);
-	e2 = cadr(e1);
-	if (car(e2) == FNAME) {
-	    nptr0=(NMTBL *)cadr(e2);
-	    if (nptr0->sc==EMPTY)
-		nptr0->sc = EXTRN1;
-	    else if(nptr0->sc==FUNCTION)
-		nptr0->sc = CODE;
-	    if (nptr0->ty>0&&car(nptr0->ty)==FUNCTION)
-		car(nptr0->ty)=CODE;
-	}
-	if (inmode) {
-	    parse = list3(ST_GOTO,parse,list3(CODE,e1,env));
-	} else {
-	    gexpr(list3(CODE,e1,env),0);
-	}
-	control=0;
-	conv->sm_();
-	checksym(SM);
+	dojump(e1,env);
 	return;
     }
     error(STERR);
@@ -3417,12 +3440,18 @@
 	return e;
     case LAND: /* &&p  gcc extension label value */
 	getsym(0);
+	if (sym!=IDENT) error(TYERR);
+#if 0
 	e = expr13();
 	type = car(e);
 	if (type!=FNAME) {
 	    error(TYERR); 
 	}
         nptr1 = (NMTBL *)cadr(e);
+#else
+	nptr1 = nptr;
+	getsym(0);
+#endif
         type = nptr1->sc;
         if (type==EMPTY||type==EXTRN1||type==EXTRN) {
             nptr1->sc=EMPTY;
@@ -3557,7 +3586,7 @@
 	    break;
 	case EMPTY:
 	    if(getsym(0)==LPAR) {
-		type= glist3(FUNCTION,INT,0);
+		type= glist3(stmode==CODE?CODE:FUNCTION,INT,0);
                 nptr->sc = EXTRN1;
 		nptr->ty= type;
 		extrn_use(nptr);
@@ -3568,6 +3597,8 @@
 		e1= list2(CONST,0);
 		break;
 	    } else {
+		if (stmode!=CODE) // goto statement
+		    error(UDERR);
 		nptr->sc = EXTRN1;
                 type = nptr->ty= glist3(FUNCTION,INT,0);
 		e1=list2(FNAME,(int)nptr);
@@ -3827,7 +3858,7 @@
 	    type0 = type_value(type);
 	} /* else error */
     }
-    if(integral(type0)||type0<0|| ((car(type0)!=FUNCTION)&&(car(type0)!=CODE))) {
+    if(integral(type0)||type0<0||((car(type0)!=FUNCTION)&&(car(type0)!=CODE))){
 	error(TYERR);
     }
     ftype = type;
@@ -3866,10 +3897,13 @@
     }
     checksym(RPAR);
     conv->funcall_args_();
-    if(t<0 && t==CODE) {
+    if(t==CODE) {
+	if (stmode!=CODE) error(FNERR);
 	// code segment has no return type
 	type = ftype;
-	return list4(FUNCTION,e1,arglist,ftype); // should be CODE?
+	return list4(CODE,e1,arglist,ftype); // should be CODE?
+    } else if (stmode==CODE) {
+	// error(GTERR);
     }
 
     /* return type */
@@ -3898,7 +3932,7 @@
 	    return list4(INLINE,e1,arglist,ftype);
 	}
     }
-    return list4(FUNCTION,e1,arglist,ftype);
+    return list4(stmode==CODE?CODE:FUNCTION,e1,arglist,ftype);
 }
 
 static int
--- a/mc.h	Thu Jan 12 13:56:44 2006 +0900
+++ b/mc.h	Fri Jan 13 01:51:39 2006 +0900
@@ -469,7 +469,11 @@
 #define AGERR   29
 #define ILERR   30
 #define CSERR   31
-#define SIERR   32
+#define GTERR   32
+#define FNERR   33
+#define UCERR   34
+#define UFERR   35
+#define SIERR   36
 
 /* error number end */
 
@@ -521,7 +525,7 @@
 extern void free_nptr(NMTBL *n);
 extern NMTBL *get_nptr();
 
-#if 1
+#if 0
 extern int heapsize;
 #define CHECK_HEAP(b) ({int _k=(int)(b);if(_k>heapsize||_k<0)error(-1);_k;})
 #else
--- a/test/conv1.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/test/conv1.c	Fri Jan 13 01:51:39 2006 +0900
@@ -67,6 +67,8 @@
     goto (c->ret)(k+4+j,sp);
 }
 
+code g_h1(int j,stack sp);
+
 code g(int i,stack sp) { // Caller
     struct f_g0_interface *c = 
 	(struct f_g0_interface *)(sp -= sizeof(struct f_g0_interface));
--- a/test/fact-a.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/test/fact-a.c	Fri Jan 13 01:51:39 2006 +0900
@@ -15,6 +15,8 @@
     }
 }
 
+code print(int n,int result,int orig,code(*print)(),code (*exit1)(),void*exit1env);
+
 int main( int ac, char *av[])
 {
     int n;
@@ -23,7 +25,7 @@
     goto factorial(n,1,n,print,return,environment);
 }
 
-code print(int n,int result,int orig,code(*print)(),(*exit1)(),void*exit1env)
+code print(int n,int result,int orig,code(*print)(),code (*exit1)(),void*exit1env)
 {
     printf("#0027:%d! = %d\n",orig, result);
     goto (*exit1)(0),exit1env;
--- a/test/fact.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/test/fact.c	Fri Jan 13 01:51:39 2006 +0900
@@ -1,5 +1,5 @@
 #include "stdio.h"
-
+code print();
 int
 main(ac,av)
 int ac;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/func_conv_err.c	Fri Jan 13 01:51:39 2006 +0900
@@ -0,0 +1,42 @@
+
+code hoge()
+{
+    goto hoge(); // ok
+}
+
+int
+f()
+{
+    return 1;
+}
+
+code hoga(int i)
+{
+    f();   // ok
+    h();   // ok h() is a function
+    if (i)
+	goto f();  // bad
+    else
+	goto g(i);  // ok g() is a code segement
+}
+
+int
+g(int i)
+{                     // g is already used as code bad
+    k();
+    if (i) 
+	goto h();    // bad
+    // should complain.... no return value
+}
+
+code k()    
+{                    // bad k is already used as function
+    goto hoge();       // ok
+}
+
+int
+main()
+{
+    hoge();    // bad
+}
+
--- a/test/goto.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/test/goto.c	Fri Jan 13 01:51:39 2006 +0900
@@ -5,20 +5,12 @@
 code a3(int i,code conv());
 code a4(int i,code conv());
 code a5(int i,code conv());
+code a6();
+code a7();
+code a8();
+code a9();
 
-char*
-print_conv(code conv())
-{
-    if(conv==a2) return "a2";
-    if(conv==a3) return "a3";
-    if(conv==a4) return "a4";
-    if(conv==a5) return "a5";
-    if(conv==a6) return "a6";
-    if(conv==a7) return "a7";
-    if(conv==a8) return "a8";
-    if(conv==a9) return "a9";
-    else return "xx";
-}
+char* print_conv(code conv());
 
 code (*exit0)(int);
 void *env;
@@ -85,3 +77,17 @@
     conv = a2;
     goto conv(1,a3);
 }
+
+char*
+print_conv(code conv())
+{
+    if(conv==a2) return "a2";
+    if(conv==a3) return "a3";
+    if(conv==a4) return "a4";
+    if(conv==a5) return "a5";
+    if(conv==a6) return "a6";
+    if(conv==a7) return "a7";
+    if(conv==a8) return "a8";
+    if(conv==a9) return "a9";
+    else return "xx";
+}
--- a/test/test1.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/test/test1.c	Fri Jan 13 01:51:39 2006 +0900
@@ -64,6 +64,7 @@
 	int jj;
 };
 
+code f1(int i,void *sp) ;
 code f0(int i,int j,code(*exit2)(), void *exit2env,void *sp)
 {
 	struct f0_save *c;
--- a/test/tmp1.c	Thu Jan 12 13:56:44 2006 +0900
+++ b/test/tmp1.c	Fri Jan 13 01:51:39 2006 +0900
@@ -2,6 +2,7 @@
 
 code (*ret)();
 void *env;
+code exit1(int ac);
 
 main0(ac,av)
 int ac;