changeset 304:9df8aa0497ea

fix line continuation and #macro in string.
author kono
date Tue, 08 Jun 2004 16:40:32 +0900
parents 88e20739e64c
children 117baacd1ed0
files .gdbinit Changes mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-code.h mc-parse.c mc.h stdio.h test/macro.c
diffstat 10 files changed, 141 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Mon Jun 07 22:57:41 2004 +0900
+++ b/.gdbinit	Tue Jun 08 16:40:32 2004 +0900
@@ -1,7 +1,7 @@
 tb main
 # run  -s test/arg.c
 # run  -s -ob00.s mc-parse.c
-run  -s -ob02.s mc-codegen.c
+run  -s -ob02.s mc-code-powerpc.c
 # run -s test/code-gen-all.c
 define regs 
 printf "pc =%08x lr =%08x r0 =%08x r1 =%08x r3= %08x r4= %08x\n",$pc,$lr,$r0,$r1,$r3,$r4
--- a/Changes	Mon Jun 07 22:57:41 2004 +0900
+++ b/Changes	Tue Jun 08 16:40:32 2004 +0900
@@ -4817,3 +4817,25 @@
 
 局所変数の初期化がレジスタ変数であるに関わらずスタックを
 初期化しているな。
+
+  gcc -E -c -DCONFIG_TASK_SIZE=4096 -DCONFIG_KERNEL_START=1 -D__KERNEL__=1 tcp_ipv4.c -I../../include > ~/src/device/test/struct_init.c
+
+kernel source は、まだ、早いんじゃない? asm とかあるし。
+でも、asm も難しそうではないけどね。
+
+stdarg は簡単なんだけど、初期化用の C source を持っていた方が
+良いね。macro だけでなく typedef もしたいよね。
+
+"test" "test" は、macro 中では使えない。っていうか使う必要ない?
+どうせ、あとで解釈されるから。
+
+hoge##name みたいなのの扱いだけど... hogefuga に変換されたあと、
+また、展開する必要があるんだよね。
+
+初期化ソースは、chptr に代入すると macro processing されない。
+macro は、getline で行われるから。うーん。
+
+うーん、line continuation と macro の関係は奥が深いな。
+getch が null を返すのを認めれば良いんだけど、他のところの
+影響が大きい。
+
--- a/mc-code-ia32.c	Mon Jun 07 22:57:41 2004 +0900
+++ b/mc-code-ia32.c	Tue Jun 08 16:40:32 2004 +0900
@@ -6,6 +6,8 @@
 #include "mc-codegen.h"
 #include "mc-code.h"
 
+char *init_src = 0;
+
 char *l_include_path[] = {
     "/usr/include/",
     "/usr/include/linux/",
--- a/mc-code-mips.c	Mon Jun 07 22:57:41 2004 +0900
+++ b/mc-code-mips.c	Tue Jun 08 16:40:32 2004 +0900
@@ -6,6 +6,8 @@
 #include "mc-code.h"
 #include "mc-codegen.h"
 
+char *init_src=0;
+
 char *l_include_path[] = {
     "/usr/include/",
     "/usr/lib/gcc-lib/mipsEEel-linux/2.95.2/include/",
--- a/mc-code-powerpc.c	Mon Jun 07 22:57:41 2004 +0900
+++ b/mc-code-powerpc.c	Tue Jun 08 16:40:32 2004 +0900
@@ -391,13 +391,23 @@
     lvar(e2);
 }
 
+char *init_src = "\
+#define __ppc__ 1\n\
+#define __BIG_ENDIAN__ 1\n\
+#define __STDC__ 1\n\
+#define __builtin_va_list int\n\
+";
+
 void
 code_init(void)
 {
+#if 0
     macro_define("__ppc__ 1\n");
     macro_define("__BIG_ENDIAN__ 1\n");
 //    macro_define("_BSD_CT_RUNE_T_ int\n");
-    macro_define("__STDC__ 1\n");
+//    macro_define("__STDC__ 1\n");
+    macro_define("__builtin_va_list int\n");
+#endif
     init_ptr_cache();
     regv_l(RET_LREGISTER) = RET_LREGISTER_L;
     regv_h(RET_LREGISTER) = RET_LREGISTER_H;
@@ -2651,8 +2661,13 @@
 void
 gen_comment(char *s)
 {
+    int c;
     if (chk) return;
-    printf("## %s",s);
+    printf("## ");
+    while((c=*s++)) {
+	putchar(c);
+	if (c=='\n') break;
+    }
 }
 
 void
--- a/mc-code.h	Mon Jun 07 22:57:41 2004 +0900
+++ b/mc-code.h	Tue Jun 08 16:40:32 2004 +0900
@@ -9,6 +9,7 @@
 
 extern char *l_include_path[];
 
+extern char *init_src;
 extern int size_of_int;
 extern int size_of_short;
 extern int size_of_float;
--- a/mc-parse.c	Mon Jun 07 22:57:41 2004 +0900
+++ b/mc-parse.c	Tue Jun 08 16:40:32 2004 +0900
@@ -115,6 +115,9 @@
 static
 char current_file_dir[LBUFSIZE];
 
+static char *chinput=0;
+static int in_quote=0;
+
 int
 main(int argc, char **argv)
 {
@@ -212,6 +215,7 @@
     fprintf(stderr,"%s:%d:%s\n",filep->name0,lineno,
 	(n==FILERR) ? "Can't open specified file" :
 	(n==DCERR) ? "Declaration syntax" :
+	(n==RDERR) ? "Redefined" :
 	(n==STERR) ? "Statement syntax" :
 	(n==EXERR) ? "Expression syntax" :
 	(n==CNERR) ? "Constant required" :
@@ -340,8 +344,6 @@
     reinit();
     filep=filestack;
     newfile();
-
-    getline();
     getch();
 }
 
@@ -398,6 +400,12 @@
     }
     opening(filep->name0);
     conv->open_(filep->name0);
+
+    if (init_src) {
+	// before reading any file, perform initialization source
+	chinput = init_src;
+    }
+    getline();
 }
 
 static void
@@ -962,8 +970,8 @@
 	  if (mode==ADECL) {  /* K&R arguments case */
 	    if (n->sc==LVAR && n->ty==INT);
 	    else if ( n->sc==REGISTER && n->ty==INT);
-	    else error(DCERR);
-	  } else error(DCERR);
+	    else error(RDERR);
+	  } else error(RDERR);
 	}
 	sz = size(n->ty = type);
     }
@@ -3822,13 +3830,13 @@
 		else break;
 	    }
 	    return sym=CONST;
-	} else if (ch!='.'&&ch!='L') {
+	} else if (ch!='.'&&ch!='L'&&ch!='U') {
 	    /* octal */
 	    while (digit(ch)) {
 		symval=symval*8+ch-'0';getch();
 	    }
 	    return sym=CONST;
-	} else if (ch=='L') {  /* 0L or 0LL case */
+	} else if (ch=='L'||ch=='U') {  /* 0L or 0LL case */
 	    if (getch()=='L') {
 #if LONGLONG_CODE
 		getch();
@@ -3850,18 +3858,26 @@
 	    *cheapp++ = ch;
 	    symval=symval*10+ch-'0';getch();
 	}
-	if (!(ch=='.'||ch=='e'||ch=='L')) {
+	if (!(ch=='.'||ch=='e'||ch=='L'||ch=='U')) {
 	    cheapp=scheapp;
 	    return sym=CONST;
 	}
     }
+    if (!d && ch=='U') {
+	getch();
+	if (ch=='L')  {
+	    getch();
+	    return sym=CONST; // unsigned constant
+	} else
+	    error(CHERR);
+    }
     if (!d && ch=='L') {
 	getch();
 	if (ch=='L') {
 #if LONGLONG_CODE
 	    getch(); /*LONGLONG*/
 	    *cheapp++ = 0;
-	    lsymval = strtoll(scheapp,0,0);
+	    lsymval = strtoll(scheapp,0,0);  // unsigned should be considered
 	    cheapp=scheapp;
 	    return sym=LCONST;
 #else
@@ -4201,15 +4217,21 @@
 static void
 getstring(void)
 {
-    getch();
     symval = 0;
     sptr = cheapp;
-    while (ch != '"') {
-	*cheapp++ = escape();
-	symval++;
-	if (cheapp >= cheap+CHEAPSIZE) error(STRERR);
+    while (ch == '"') {
+	in_quote = 1;
+	getch();
+	while (ch != '"') {
+	    *cheapp++ = escape();
+	    symval++;
+	    if (cheapp >= cheap+CHEAPSIZE) error(STRERR);
+	}
+	in_quote = 0;
+	getch();
+	skipspc();
     }
-    getch();
+    in_quote = 0;
     *cheapp++ = '\0';
     symval++;
 }
@@ -4281,12 +4303,15 @@
 	case '\\':
 	    return '\\';
 	case '\n':
+	    if (ch=='"') {
+		return 0;
+	    }
 	    return escape();
 	default:
 	    return c;
 	}
     }
-    if (c == '\n') error(EXERR);
+    // if (c == '\n') error(EXERR);
     getch();
     return c;
 }
@@ -4389,23 +4414,36 @@
     int c;
 
     do {
-	lineno++;
-	glineno++;
-	chptr=linebuf;
-	i=0;
-	while ((*chptr++ = c = getc(filep->fcb)) != '\n') {
-	    if (++i > LBUFSIZE-2) error(LNERR);
-	    if (c==EOF) {
-		    error(EOFERR);
-		    --chptr;
+	if (chinput) {
+	    if (! *chinput) {
+		chinput=0;
+		continue;
+	    }
+	    chptr=chinput;
+	    while((c=*chinput++)&&(c!='\n'));
+	    if (lsrc && !asmf && !macro_if_skip) gen_comment(chinput);
+	    if (*(chptr = linebuf) == '#' && !in_comment && !in_quote) {
+		if (macro_processing()) return;
+	    }
+	} else {
+	    lineno++;
+	    glineno++;
+	    chptr=linebuf;
+	    i=0;
+	    while ((*chptr++ = c = getc(filep->fcb)) != '\n') {
+		if (++i > LBUFSIZE-2) error(LNERR);
+		if (c==EOF) {
+			error(EOFERR);
+			--chptr;
+		}
+	    }
+	    *chptr = '\0';
+	    if (lsrc && !asmf && !macro_if_skip) gen_comment(linebuf);
+	    if (*(chptr = linebuf) == '#' && !in_comment && !in_quote) {
+		if (macro_processing()) return;
 	    }
 	}
-	*chptr = '\0';
-	if (lsrc && !asmf && !macro_if_skip) gen_comment(linebuf);
-	if (*(chptr = linebuf) == '#' && !in_comment) {
-	    if (macro_processing()) return;
-	}
-    } while(macro_if_skip || linebuf[0] == '#');
+    } while(!in_quote && (macro_if_skip || linebuf[0] == '#'));
 }
 
 /* preprocessor directive */
--- a/mc.h	Mon Jun 07 22:57:41 2004 +0900
+++ b/mc.h	Tue Jun 08 16:40:32 2004 +0900
@@ -375,25 +375,26 @@
 
 #define FILERR	1
 #define DCERR	2
-#define STERR	3
-#define EXERR	4
-#define CNERR	5
-#define CHERR	6
-#define GSERR	7
-#define LSERR	8
-#define STRERR	9
-#define LNERR	10
-#define EOFERR	11
-#define MCERR	12
-#define INCERR	13
-#define HPERR	14
-#define TYERR	15
-#define LVERR	16
-#define UDERR	17
-#define OPTION	18
-#define REG_ERR	19
-#define CODE_ERR	20
-#define MSERR   21
+#define RDERR	3
+#define STERR	4
+#define EXERR	5
+#define CNERR	6
+#define CHERR	7
+#define GSERR	8
+#define LSERR	9
+#define STRERR	10
+#define LNERR	11
+#define EOFERR	12
+#define MCERR	13
+#define INCERR	14
+#define HPERR	15
+#define TYERR	16
+#define LVERR	17
+#define UDERR	18
+#define OPTION	19
+#define REG_ERR	20
+#define CODE_ERR	21
+#define MSERR   22
 /* error number end */
 
 #define GSYMS	9000
@@ -427,6 +428,8 @@
 EXTERN int retlabel,retpending,retcont;
 EXTERN int arglist;
 
+extern char *init_src;
+
 extern int size_of_int;
 extern int size_of_short;
 extern int size_of_float;
--- a/stdio.h	Mon Jun 07 22:57:41 2004 +0900
+++ b/stdio.h	Tue Jun 08 16:40:32 2004 +0900
@@ -49,7 +49,7 @@
 
 typedef int SIZE_T_;
 typedef /*long*/ int fpos_t;
-typedef void *__gnuc_va_list;
+// typedef void *__gnuc_va_list;
  
 void	 clearerr(FILE *);
 int	 fclose(FILE *);
@@ -101,12 +101,12 @@
 int	 sprintf(char *, const char *, ...);
 int	 sscanf(const char *, const char *, ...);
 int	 ungetc(int, FILE *);
-int	 vfprintf(FILE *, const char *, __gnuc_va_list);
+// int	 vfprintf(FILE *, const char *, __gnuc_va_list);
 int	 vfscanf();
-int	 vprintf(const char *, __gnuc_va_list);
+// int	 vprintf(const char *, __gnuc_va_list);
 int	 vscanf();
 int	 vsnprintf();
-int	 vsprintf(char *, const char *, __gnuc_va_list);
+// int	 vsprintf(char *, const char *, __gnuc_va_list);
 int	 vsscanf();
 char	*ctermid(char *);
 FILE	*fdopen(int, const char *);
--- a/test/macro.c	Mon Jun 07 22:57:41 2004 +0900
+++ b/test/macro.c	Tue Jun 08 16:40:32 2004 +0900
@@ -8,6 +8,8 @@
 int i;
 TEST
 
+char *a = "test" "test";
+
 #define TEST0 33
 #define TEST111 TEST0