changeset 363:f3f2b7906d50

label extension
author kono
date Tue, 06 Jul 2004 17:04:31 +0900
parents 5cf12c7a3274
children c29eebf3eaf4
files .gdbinit Changes Makefile mc-code-ia32.c mc-code-mips.c mc-code-powerpc.c mc-code.h mc-codegen.c mc-codegen.h mc-macro.c mc-parse.c mc.h test/scope.c
diffstat 13 files changed, 264 insertions(+), 105 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Mon Jul 05 14:40:20 2004 +0900
+++ b/.gdbinit	Tue Jul 06 17:04:31 2004 +0900
@@ -4,7 +4,7 @@
 # run  -s nkf203/nkf.c
 # run  -s -ob01.s mc-switch.c
 # run  -s l.c
-run  -s test/macro.c
+run  -s test/scope.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 Jul 05 14:40:20 2004 +0900
+++ b/Changes	Tue Jul 06 17:04:31 2004 +0900
@@ -5506,3 +5506,13 @@
 Mon Jul  5 14:11:07 JST 2004
 
 local label ね。まぁ、簡単なんだけど...
+
+lazy flag で、
+     do if 
+       make l = listN(IF,cond,then,else)
+       if(lazy)
+            return l;
+       else
+            eval_if l;
+     eval_if
+みたいな感じ?
--- a/Makefile	Mon Jul 05 14:40:20 2004 +0900
+++ b/Makefile	Tue Jul 06 17:04:31 2004 +0900
@@ -1,4 +1,4 @@
-CC = gcc 
+CC = gcc
 # -O3
 CFLAGS = -g -Wall -I.
 BASE=0
@@ -104,7 +104,7 @@
 #	-./$(MC) -Itest/ -s $(TARGET).c
 
 check: $(MC) $(TARGET).c
-	-gcc $(TARGET).c -o b.out $(MLIB)
+	-gcc  -std=gnu99 $(TARGET).c -o b.out $(MLIB)
 	-./b.out > $(TARGET).gcc.out
 	-./$(MC) -s $(TARGET).c
 	-gcc $(TARGET).s $(MLIB)
--- a/mc-code-ia32.c	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-code-ia32.c	Tue Jul 06 17:04:31 2004 +0900
@@ -833,6 +833,12 @@
     regv[creg]=1;
 }
 
+void
+code_label_value(int label,int reg) {
+    use_int(reg);
+    printf("\tleal _%d,%s\n",label,register_name(reg,0));
+    regv[creg]=1;
+}
 
 void
 code_const(int e2,int creg) {
@@ -841,7 +847,6 @@
     regv[creg]=1;
 }
 
-
 void
 code_neg(int creg) {
     use_int(creg);
--- a/mc-code-mips.c	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-code-mips.c	Tue Jul 06 17:04:31 2004 +0900
@@ -1261,6 +1261,12 @@
     return;
 }
 
+void
+code_label_value(int label,int reg) {
+    use_int(reg);
+    printf("\tla %s,L_%d\n",register_name(reg),label);
+    return;
+}
 
 void
 code_const(int e2,int reg) {
@@ -1270,7 +1276,6 @@
     printf("\tli %s,%d\n",crn,e2);
 }
 
-
 void
 code_neg(int creg) {
     use_int(creg);
--- a/mc-code-powerpc.c	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-code-powerpc.c	Tue Jul 06 17:04:31 2004 +0900
@@ -1199,6 +1199,15 @@
     return;
 }
 
+void
+code_label_value(int label,int reg) {
+    char *crn;
+    use_int(reg);
+    crn = register_name(reg);
+    printf("\taddis %s,r31,ha16(L_%d-L_%d)\n",crn,label,code_base);
+    printf("\tla %s,lo16(L_%d-L_%d)(%s)\n",crn,label,code_base,crn);
+    return;
+}
 
 void
 code_const(int e2,int reg) {
--- a/mc-code.h	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-code.h	Tue Jul 06 17:04:31 2004 +0900
@@ -116,6 +116,7 @@
 extern void code_set_return_register(int mode);
 extern void global_table(void);
 extern void code_label(int labelno);
+extern void code_label_value(int labelno,int reg);
 
 extern int code_const_op_p(int op,int v); 
 extern void oprtc(int op,int reg,int v);
--- a/mc-codegen.c	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-codegen.c	Tue Jul 06 17:04:31 2004 +0900
@@ -219,6 +219,9 @@
     case FNAME:
 	code_fname((NMTBL *)(e2),USE_CREG);
 	return ADDRESS;
+    case LABEL:
+	code_label_value(e2,USE_CREG);
+	return ADDRESS;
     case CONST:  /* 代入する値が0でも特別な処理はしない */
 	code_const(e2,USE_CREG);
 	return INT;
@@ -2442,8 +2445,8 @@
 	nsc = TYPE;
 	break;
     case LLDECL:
-	nsc = BLABEL;
-	n->dsp = backdef();
+	nsc = FLABEL;
+	n->dsp = fwdlabel();
 	break;
     case ADECL:
 	if(!integral(type)&&(car(type)==FUNCTION||car(type)==CODE)) {
@@ -3665,6 +3668,13 @@
     jmp(l);
 }
 
+extern void
+gen_indirect_goto(int e1)
+{
+    g_expr(e1);
+    code_indirect_jmp(USE_CREG);
+}
+
 extern int
 make_mask(int from,int to)
 {
--- a/mc-codegen.h	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-codegen.h	Tue Jul 06 17:04:31 2004 +0900
@@ -62,6 +62,7 @@
 extern void gen_enter(char *name);
 extern void gen_enter1();
 extern void gen_jmp(int l);
+extern void gen_indirect_goto(int e1);
 extern void gen_leave(int control, char *name);
 extern void init_free_lvar_list();
 extern void lcheck(int e);
--- a/mc-macro.c	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-macro.c	Tue Jul 06 17:04:31 2004 +0900
@@ -27,6 +27,11 @@
      printf("%s",s);
 }
 
+/*
+     replace macro term into cheap.
+     ## concatenation requires repeated replace.
+ */
+
 extern void
 macro_expansion(NMTBL *nptrm)
 {
@@ -76,6 +81,10 @@
 
 /* file inclusion */
 
+/*
+    file name concatenation
+ */
+
 static char *
 expand_file_name(char *path,char *name)
 {
@@ -91,6 +100,16 @@
     return p;
 }
 
+/*
+      get file name
+            <name> =>   name
+                        current_file_name_dir / name
+                        libdir / name
+            "name" =>   name
+                        current_file_name_dir / name
+                        include_path / name
+ */
+
 static FILE *
 getfname(void)
 {
@@ -145,6 +164,11 @@
 static int macro_if_current ;
 static int macro_if_skip ;
 
+/* there may extra non-terminate comment after #if/#else directive */
+/*      #endif /* hoge						   */
+/*           */
+/*                                                                 */
+
 static int
 skip_rest_of_line()
 {
@@ -175,6 +199,11 @@
     return 0;
 }
 
+/*
+     getline from chptr or chinput (for internal source)
+        with macro processing 
+ */
+
 static int next_eof;
 
 extern void
@@ -244,6 +273,8 @@
     chptr[c+2] = 0;
 }
 
+/*   #if hoge case */
+
 static void
 macro_if()
 {
@@ -435,6 +466,8 @@
     ch = chsave;
 }
 
+/* macro define from chptr */
+
 static void
 macro_define0()
 {
@@ -620,6 +653,11 @@
     nptr0->nm = value;
 }
 
+/*
+    Evaluate macro string.
+    reuslt:   list2("replaced string",next)
+ */
+
 static int
 macro_eval(int macrop,char *body0,int history)
 {
@@ -708,6 +746,10 @@
     return macrop;
 }
 
+/*
+    cancat list2("string",next) into cheap.
+    result overwrited by next cheap allocation
+ */
 
 static char *
 mappend(int lists,char **result)
--- a/mc-parse.c	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc-parse.c	Tue Jul 06 17:04:31 2004 +0900
@@ -1710,6 +1710,7 @@
 	mode=STAT;
 	checkret();
 	emit_init_vars();
+	getsym(0);
     } else if(sym!=SM) {
 	checkret();
 	gexpr(expr(0),0);
@@ -1967,6 +1968,13 @@
     getsym(0);
     e1 = expr(0);
     t=car(e1);
+    if (type==VOID) {
+	if (car(e1)==RINDIRECT) {
+	    gen_indirect_goto(cadr(e1));
+	} else error(TYERR);
+	checksym(SM);
+	return;
+    }
     if (t==FNAME) {
 	nptr0 = (NMTBL *)cadr(e1);
 	t = nptr0->sc;
@@ -2350,6 +2358,7 @@
 expr13(void)
 {
     int e,op,dir;
+    NMTBL *nptr1;
 
     switch (op = sym) {
     case INC: case DEC:
@@ -2503,6 +2512,23 @@
 	e=list2(CONST,size(type));
 	type=INT;
 	return e;
+    case LAND: /* &&p  gcc extension label value */
+	getsym(0);
+	e = expr13();
+	type = car(e);
+	if (type!=FNAME) {
+	    error(TYERR); 
+	}
+        nptr1 = (NMTBL *)cadr(e);
+        type = nptr1->sc;
+        if (type==EMPTY||type==EXTRN1||type==EXTRN) {
+            nptr1->sc=EMPTY;
+            nptr1=l_top_search(nptr->nm,0);
+            nptr1->sc = FLABEL;
+            nptr1->dsp = fwdlabel();
+        }
+	type = list2(POINTER,VOID);
+	return list2(LABEL,nptr1->dsp);
     }
     e=expr14();
 
--- a/mc.h	Mon Jul 05 14:40:20 2004 +0900
+++ b/mc.h	Tue Jul 06 17:04:31 2004 +0900
@@ -68,8 +68,7 @@
 #define ULONGLONG       (-56)
 #define VOLATILE        (-57)
 #define TYPEOF  (-58)
-#define LABEL   (-59)
-#define ASM     (-60)
+#define ASM     (-59)
 
 /* reserved word end */
 
@@ -144,25 +143,26 @@
 #define LCONST  (LOP+CONST)
 #define STRING 	8
 #define FNAME  	9
+#define LABEL  	10
 
-#define NULLARY_ARGS(i) (i==REGISTER||i==DREGISTER||i==FREGISTER||i==LREGISTER||(GVAR<=(i%SOP)&&(i%SOP)<=FNAME))
+#define NULLARY_ARGS(i) (i==REGISTER||i==DREGISTER||i==FREGISTER||i==LREGISTER||(GVAR<=(i%SOP)&&(i%SOP)<=LABEL))
 
 /* unary  argments */
 
-#define ADDRESS	10
-#define MINUS  	11
-#define LNOT   	12
-#define BNOT   	13
-#define INC    	14
-#define POSTINC	15
-#define UPOSTINC       	16
-#define PREINC 	17
-#define UPREINC	18
-#define POSTDEC	19
-#define UPOSTDEC       	20
-#define PREDEC 	21
-#define UPREDEC	22
-#define DEC    	23
+#define ADDRESS	11
+#define MINUS  	12
+#define LNOT   	13
+#define BNOT   	14
+#define INC    	15
+#define POSTINC	16
+#define UPOSTINC       	17
+#define PREINC 	18
+#define UPREINC	19
+#define POSTDEC	20
+#define UPOSTDEC       	21
+#define PREDEC 	22
+#define UPREDEC	23
+#define DEC    	24
 #define CPOSTINC (COP+POSTINC)
 #define CUPOSTINC (COP+UPOSTINC)
 #define CPREINC (COP+PREINC)
@@ -187,9 +187,9 @@
 #define LPREINC (LOP+PREINC)
 #define LUPOSTINC       (LOP+UPOSTINC)
 #define LUPREINC        (LOP+UPREINC)
-#define INDIRECT       	24
-#define RINDIRECT      	25
-#define URINDIRECT     	26
+#define INDIRECT       	25
+#define RINDIRECT      	26
+#define URINDIRECT     	27
 #define CRINDIRECT      (COP+RINDIRECT)
 #define CURINDIRECT     (COP+URINDIRECT)
 #define SRINDIRECT      (SOP+RINDIRECT)
@@ -198,58 +198,58 @@
 #define DRINDIRECT      (DOP+RINDIRECT)
 #define LRINDIRECT      (LOP+RINDIRECT)
 #define LURINDIRECT     (LOP+URINDIRECT)
-#define RSTRUCT	27
-#define ALLOCA 	28
-#define BIT_FIELD 	29
-#define RBIT_FIELD 	30
-#define BPREINC 	31
-#define BPOSTINC 	32
-#define CONV   	33
+#define RSTRUCT	28
+#define ALLOCA 	29
+#define BIT_FIELD 	30
+#define RBIT_FIELD 	31
+#define BPREINC 	32
+#define BPOSTINC 	33
+#define CONV   	34
 
 #define UNARY_ARGS(i) (ADDRESS<=(i%SOP)&&(i%SOP)<=CONV)
 
 /* binary  argments */
 
-#define MUL    	34
-#define UMUL   	35
-#define DIV    	36
-#define UDIV   	37
-#define MOD    	38
-#define UMOD   	39
-#define ADD    	40
-#define SUB    	41
-#define CMP    	42      
-#define RSHIFT 	43
-#define URSHIFT	44
-#define LSHIFT 	45
-#define ULSHIFT	46
-#define GT     	47
-#define UGT    	48
-#define GE     	49
-#define UGE    	50
-#define LT     	51
-#define ULT    	52
-#define LE     	53
-#define ULE    	54
-#define EQ     	55
-#define NEQ    	56
-#define BAND   	57
-#define EOR    	58
-#define BOR    	59
-#define LAND   	60
-#define LOR    	61
-#define ASS    	62
-#define UCMP   	63
-#define UCMPGE 	64
-#define CMPGE  	65
-#define CMPEQ  	66
-#define CMPNEQ 	67
-#define ASSOP  	68
-#define COMMA  	69
+#define MUL    	35
+#define UMUL   	36
+#define DIV    	37
+#define UDIV   	38
+#define MOD    	39
+#define UMOD   	40
+#define ADD    	41
+#define SUB    	42
+#define CMP    	43      
+#define RSHIFT 	44
+#define URSHIFT	45
+#define LSHIFT 	46
+#define ULSHIFT	47
+#define GT     	48
+#define UGT    	49
+#define GE     	50
+#define UGE    	51
+#define LT     	52
+#define ULT    	53
+#define LE     	54
+#define ULE    	55
+#define EQ     	56
+#define NEQ    	57
+#define BAND   	58
+#define EOR    	59
+#define BOR    	60
+#define LAND   	61
+#define LOR    	62
+#define ASS    	63
+#define UCMP   	64
+#define UCMPGE 	65
+#define CMPGE  	66
+#define CMPEQ  	67
+#define CMPNEQ 	68
+#define ASSOP  	69
+#define COMMA  	70
 
-#define CASS   	70
-#define CASSOP 	71
-#define CUASSOP	72
+#define CASS   	71
+#define CASSOP 	72
+#define CUASSOP	73
 
 #define SASS    (SOP+CASS)
 #define SASSOP (SOP+CASSOP)
@@ -305,18 +305,18 @@
 #define LEOR    (LOP+EOR)
 #define LBOR    (LOP+BOR)
 
-#define BASS   	73
-#define BASSOP 	74
-#define BFD_REPL 	75
+#define BASS   	74
+#define BASSOP 	75
+#define BFD_REPL 	76
 
-#define STASS  	76
+#define STASS  	77
 
 
 #define BINARY_ARGS(i) (MUL<=(i%SOP)&&(i%SOP)<=STASS)
 
 /* tarnary  argments */
 
-#define COND   	77
+#define COND   	78
 #define SCOND   (SOP+COND)
 #define DCOND   (DOP+COND)
 #define FCOND   (FOP+COND)
@@ -326,35 +326,35 @@
 
 /* not appeared as tags */
 
-#define LPAR   	78
-#define RPAR   	79
-#define LBRA   	80
-#define RBRA   	81
-#define LC     	82
-#define RC     	83
-#define COLON  	84
-#define SM     	85
-#define PERIOD 	86
-#define ARROW  	87
-#define CNAME  	88
+#define LPAR   	79
+#define RPAR   	80
+#define LBRA   	81
+#define RBRA   	82
+#define LC     	83
+#define RC     	84
+#define COLON  	85
+#define SM     	86
+#define PERIOD 	87
+#define ARROW  	88
+#define CNAME  	89
 
-#define I2C  	89
-#define I2S  	90
-#define I2I    	91
-#define I2U    	92
-#define I2D    	93
-#define I2F    	94
-#define I2LL   	95
-#define I2ULL  	96
+#define I2C  	90
+#define I2S  	91
+#define I2I    	92
+#define I2U    	93
+#define I2D    	94
+#define I2F    	95
+#define I2LL   	96
+#define I2ULL  	97
 
-#define U2UC  	97
-#define U2US  	98
-#define U2I    	99
-#define U2U    	100
-#define U2D    	101
-#define U2F    	102
-#define U2LL   	103
-#define U2ULL  	104
+#define U2UC  	98
+#define U2US  	99
+#define U2I    	100
+#define U2U    	101
+#define U2D    	102
+#define U2F    	103
+#define U2LL   	104
+#define U2ULL  	105
 
 
 #define D2I     (DOP+I2I)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/scope.c	Tue Jul 06 17:04:31 2004 +0900
@@ -0,0 +1,50 @@
+/* 
+
+    gcc -g -std=gnu99 test/scope.c
+
+ */
+
+#include <stdio.h>
+
+void
+f(int *i,void *label)
+{
+    static int k = 3;
+    {
+	static int k = 3;
+	*i =  k++;
+    }
+    k--;
+    printf("f %d\n",k);
+} 
+
+int
+main()
+{
+    int i,k;
+    void *exit = &&exit0;
+    i = 3;k=10;
+    for(int k=0;k<10;k++) { 
+	int i;
+	i = 4;
+	if (({
+	    __label__ exit0; 
+	    int i; 
+		f(&i,&&exit0); 
+		if (i==5) 
+		    goto exit0;
+		printf("2nd inner %d %d %0x\n",i,k,&&exit1==exit);
+	    exit0:
+	    i;
+	    })!=7) {
+	    goto exit1;
+	} else
+	    goto *exit;
+exit1:
+	printf("inner %d %d %0x\n",i,k,&&exit1==exit);
+    }
+    k++;
+exit0:
+    printf("outer %d %d %0x\n",i,k,&&exit0==exit);
+    return 0;
+}