changeset 648:234bc5f79a11

alignement attribute (working)
author kono
date Fri, 17 Nov 2006 22:30:24 +0900
parents fe23fe842b82
children f1d71563a46a
files .gdbinit Changes mc-codegen.c mc-codegen.h mc-inline.c mc-macro.c mc-parse.c mc.h test/ps2.c
diffstat 9 files changed, 198 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/.gdbinit	Fri Nov 17 20:00:37 2006 +0900
+++ b/.gdbinit	Fri Nov 17 22:30:24 2006 +0900
@@ -57,4 +57,5 @@
 # run -DINLINE=inline test/tmp7.c
 # run -DINLINE=inline test/code-gen-all.c
 # run -s throw.c
-run -s test/simp.c
+# run -s test/simp.c
+run -s test/ps2.c
--- a/Changes	Fri Nov 17 20:00:37 2006 +0900
+++ b/Changes	Fri Nov 17 22:30:24 2006 +0900
@@ -9121,5 +9121,15 @@
 制御する必要がある。stack のalignment 以上には制御できない
 わけだけどね。
 
-
-
+attribute に出て来るidentifierって予約語じゃないんだよな。
+どう処理しようかな。最初の1文字とか... (本気か?!)
+
+まぁ、name space を専用に作るのが普通だろうとは思うが...
+
+順調に進んでいるけど。
+
+typedef されたsymbol に付いているattribute を引き継ぐ必要が
+ある。それは、どこで...
+
+
+
--- a/mc-codegen.c	Fri Nov 17 20:00:37 2006 +0900
+++ b/mc-codegen.c	Fri Nov 17 22:30:24 2006 +0900
@@ -2672,7 +2672,7 @@
 // define case label with default label for switch statement
 
 extern void
-def_label(int cslabel, int dlabel)
+df_label(int cslabel, int dlabel)
 {
     int fl;
 
@@ -3122,7 +3122,7 @@
 extern NMTBL *
 def(NMTBL *n,int ctmode)
 {
-    int sz,nsc,ndsp;
+    int sz,nsc,ndsp,align;
     int sbit_f = bit_field_disp;
     int type0 = type_value(type);
     bit_field_disp = 0;  // default is 0, recover only in bit-field
@@ -3324,8 +3324,14 @@
 	    ndsp = --disp;
 	} else {
 	    /* local variable alignment is done by new_lvar */
-	    nsc = LVAR;
-	    ndsp = new_lvar(sz);
+	    if ((align=attr_value(n,ALIGNED))) {
+		if (car(align)!=CONST) error(-1);
+		nsc = LVAR;
+		ndsp = new_lvar_align(sz,cadr(align));
+	    } else {
+		nsc = LVAR;
+		ndsp = new_lvar(sz);
+	    }
 	}
 	n->sc = nsc;
 	n->dsp = ndsp;
@@ -3342,6 +3348,19 @@
     return n;
 }
 
+extern char *
+nm(NMTBL *n) {
+    int e;
+    NMTBL *str; 
+    if (n->attr) {
+	if ((e=attr_value(n,ASM))) {
+	    if (car(e)!=STRING) error(-1);
+	    str = (NMTBL*)caddr(e);
+	    return str->nm;
+	}
+    }
+    return n->nm;
+}
 
 extern void
 emit_init_vars(void)
@@ -4774,14 +4793,14 @@
 static int lvar_list,lvar_free_list;
 
 extern int
-new_lvar0(int sz)
+new_lvar0(int sz, int align)
 {
     disp-=sz;
-#if 1
-    if (sz>=4 && (disp & (4-1))) { // alignment
+    if (align) {
+	disp &= ~(align-1);
+    } else if (sz>=4 && (disp & (4-1))) { // alignment 4
 	disp &= ~(4-1);
     }
-#endif
     return disp;
 }
 
@@ -4789,14 +4808,15 @@
     Allocate new local variable in flat scope
  */
 
+
 extern int
-new_lvar(int size)
+new_lvar_align(int size,int align)
 {
     int lvar,plvar;
 
     /* Can we reuse previously freed local variable? */ 
     for (plvar = 0,lvar = lvar_free_list;lvar;lvar = cadr(lvar)) {
-        if (caddr(lvar)==size) {
+        if (caddr(lvar)==size && (~align|| car(lvar)%align==0)) {
             if (plvar) cadr(plvar) = cadr(lvar);
             else lvar_free_list = cadr(lvar);
             break;
@@ -4804,7 +4824,7 @@
         plvar = lvar;
     }
     if (!lvar) {
-        lvar_list = glist3((lvar=new_lvar0(size)),lvar_list,size);
+        lvar_list = glist3((lvar=new_lvar0(size,align)),lvar_list,size);
     } else {
         cadr(lvar) = lvar_list; lvar_list = lvar;
         lvar = car(lvar_list);
@@ -4812,6 +4832,11 @@
     return lvar;
 }
 
+extern int
+new_lvar(int size) {
+    return new_lvar_align(size,0);
+}
+
 /*
     Free the allocated local variable. 	It may be reused again.
  */
--- a/mc-codegen.h	Fri Nov 17 20:00:37 2006 +0900
+++ b/mc-codegen.h	Fri Nov 17 22:30:24 2006 +0900
@@ -59,7 +59,7 @@
 extern int indop(int e);
 extern int integral(int t);
 extern int new_lvar(int size);
-extern int new_lvar0(int sz);
+extern int new_lvar0(int sz, int align);
 extern int rvalue(int e);
 extern int rvalue_t(int e,int t);
 extern int search_struct_type(int type,char *name,int *dsp);
@@ -74,7 +74,7 @@
 extern void codegen_init();      /* called only once */
 extern void codegen_reinit();    /* called for each file */
 extern void data_closing(NMTBL *n);
-extern void def_label(int cslabel, int dlabel);
+extern void df_label(int cslabel, int dlabel);
 extern void emit_init_vars(void);
 extern void fcheck(NMTBL *n);
 extern void fdecl_struct(int fntype);
@@ -134,6 +134,9 @@
 
 extern int ilog(int i);
 
+extern char * nm(NMTBL *n) ;
+extern int new_lvar_align(int size,int align);
+
 /* used by mc-tree */
 
 extern NMTBL str_ret;
@@ -142,4 +145,6 @@
 extern int rop_dual(int op);
 extern int copy_expr(int e);
 
+
+
 /* end */
--- a/mc-inline.c	Fri Nov 17 20:00:37 2006 +0900
+++ b/mc-inline.c	Fri Nov 17 22:30:24 2006 +0900
@@ -226,12 +226,12 @@
 	if (control) gen_jmp(blabel);
 	genswitch(cslist,cslabel);
     } else if (!cslist) {
-	if(dlabel) def_label(cslabel,dlabel);
+	if(dlabel) df_label(cslabel,dlabel);
 	else fwddef(cslabel);
     }
 #else
     if (!(cst && cslist)) {
-	if(dlabel) def_label(cslabel,dlabel);
+	if(dlabel) df_label(cslabel,dlabel);
 	else fwddef(cslabel);
     }
 #endif
--- a/mc-macro.c	Fri Nov 17 20:00:37 2006 +0900
+++ b/mc-macro.c	Fri Nov 17 22:30:24 2006 +0900
@@ -337,7 +337,7 @@
 getline(void)
 {
     int i;
-    int c;
+    int c = 0;
     char num[10]; // for 32bit
     char *p;
 
--- a/mc-parse.c	Fri Nov 17 20:00:37 2006 +0900
+++ b/mc-parse.c	Fri Nov 17 22:30:24 2006 +0900
@@ -174,7 +174,7 @@
 static void getstring(void);
 static void init(void);
 static void newfile(void);
-static void reserve(char *s, int d);
+static void reserve(char *s, int d, int sc);
 static void reverse(int t1);
 static void set_converter(char *s);
 static int escape(void);
@@ -191,8 +191,10 @@
 static void top_init();
 static void qualifiers();
 static void attributes();
+static void set_attributes(NMTBL *n);
 static void macro_convert();
 extern void sym_print(int,FILE *);
+static void copy_attributes(NMTBL *n) ;
 
 // current value of constant symbol
 
@@ -564,70 +566,73 @@
 
     heap_init();
 
-    reserve("int",INT);
-    reserve("void",VOID);
-    reserve("char",CHAR);
-    reserve("const",KONST);
-    reserve("__const__",KONST);
-    reserve("struct",STRUCT);
-    reserve("union",UNION);
-    reserve("unsigned",UNSIGNED);
-    reserve("signed",SIGNED);
-    reserve("static",STATIC);
-    reserve("goto",GOTO);
-    reserve("return",RETURN);
-    reserve("break",BREAK);
-    reserve("continue",CONTINUE);
-    reserve("if",IF);
-    reserve("else",ELSE);
-    reserve("for",FOR);
-    reserve("do",DO);
-    reserve("while",WHILE);
-    reserve("switch",SWITCH);
-    reserve("case",CASE);
-    reserve("default",DEFAULT);
-    reserve("typedef",TYPEDEF);
-    reserve("sizeof",SIZEOF);
-    reserve("long",LONG);
-    reserve("short",SHORT);
-    reserve("extern",EXTRN);
-    reserve("defined",DEFINED);
-    reserve("register",REGISTER);
+    reserve("int",INT,RESERVE);
+    reserve("void",VOID,RESERVE);
+    reserve("char",CHAR,RESERVE);
+    reserve("const",KONST,RESERVE);
+    reserve("__const__",KONST,RESERVE);
+    reserve("struct",STRUCT,RESERVE);
+    reserve("union",UNION,RESERVE);
+    reserve("unsigned",UNSIGNED,RESERVE);
+    reserve("signed",SIGNED,RESERVE);
+    reserve("static",STATIC,RESERVE);
+    reserve("goto",GOTO,RESERVE);
+    reserve("return",RETURN,RESERVE);
+    reserve("break",BREAK,RESERVE);
+    reserve("continue",CONTINUE,RESERVE);
+    reserve("if",IF,RESERVE);
+    reserve("else",ELSE,RESERVE);
+    reserve("for",FOR,RESERVE);
+    reserve("do",DO,RESERVE);
+    reserve("while",WHILE,RESERVE);
+    reserve("switch",SWITCH,RESERVE);
+    reserve("case",CASE,RESERVE);
+    reserve("default",DEFAULT,RESERVE);
+    reserve("typedef",TYPEDEF,RESERVE);
+    reserve("sizeof",SIZEOF,RESERVE);
+    reserve("long",LONG,RESERVE);
+    reserve("short",SHORT,RESERVE);
+    reserve("extern",EXTRN,RESERVE);
+    reserve("defined",DEFINED,RESERVE);
+    reserve("register",REGISTER,RESERVE);
 #ifdef USE_CODE_KEYWORD
-    reserve("code",CODE);
+    reserve("code",CODE,RESERVE);
 #endif
-    reserve("__code",CODE);
-    reserve("environment",ENVIRONMENT);
-    reserve("float",FLOAT);
-    reserve("double",DOUBLE);
-    reserve("inline",INLINE);
-    reserve("enum",ENUM);
-    reserve("volatile",VOLATILE);
-    reserve("__volatile__",VOLATILE);
-    reserve("restrict",RESTRICT);
-    reserve("typeof",TYPEOF);
-    reserve("__typeof__",TYPEOF);
-    reserve("__builtin_alloca",ALLOCA);
-    reserve("__builtin_constant_p",BUILTINP);
-    reserve("__builtin_expect",BUILTIN_EXPECT);
-    reserve("__builtin_fabs",BUILTIN_FABS);
-    reserve("__builtin_fabsf",BUILTIN_FABSF);
-    reserve("__builtin_fabsl",BUILTIN_FABSL);
-    reserve("__builtin_inf",BUILTIN_INF);
-    reserve("__builtin_inff",BUILTIN_INFF);
-    reserve("__builtin_infl",BUILTIN_INFL);
-    reserve("__attribute__",ATTRIBUTE);
-    reserve("__attribute",ATTRIBUTE);
-    reserve("__label__",LABEL);
-    reserve("__FILE__",C_FILE);
-    reserve("__FUNCTION__",C_FUNCTION);
-    reserve("__func__",C_FUNCTION);
-    reserve("__LINE__",C_LINE);
+    reserve("__code",CODE,RESERVE);
+    reserve("environment",ENVIRONMENT,RESERVE);
+    reserve("float",FLOAT,RESERVE);
+    reserve("double",DOUBLE,RESERVE);
+    reserve("inline",INLINE,RESERVE);
+    reserve("enum",ENUM,RESERVE);
+    reserve("volatile",VOLATILE,RESERVE);
+    reserve("__volatile__",VOLATILE,RESERVE);
+    reserve("restrict",RESTRICT,RESERVE);
+    reserve("typeof",TYPEOF,RESERVE);
+    reserve("__typeof__",TYPEOF,RESERVE);
+    reserve("__builtin_alloca",ALLOCA,RESERVE);
+    reserve("__builtin_constant_p",BUILTINP,RESERVE);
+    reserve("__builtin_expect",BUILTIN_EXPECT,RESERVE);
+    reserve("__builtin_fabs",BUILTIN_FABS,RESERVE);
+    reserve("__builtin_fabsf",BUILTIN_FABSF,RESERVE);
+    reserve("__builtin_fabsl",BUILTIN_FABSL,RESERVE);
+    reserve("__builtin_inf",BUILTIN_INF,RESERVE);
+    reserve("__builtin_inff",BUILTIN_INFF,RESERVE);
+    reserve("__builtin_infl",BUILTIN_INFL,RESERVE);
+    reserve("__attribute__",ATTRIBUTE,RESERVE);
+    reserve("__attribute",ATTRIBUTE,RESERVE);
+    reserve("__label__",LABEL,RESERVE);
+    reserve("__FILE__",C_FILE,RESERVE);
+    reserve("__FUNCTION__",C_FUNCTION,RESERVE);
+    reserve("__func__",C_FUNCTION,RESERVE);
+    reserve("__LINE__",C_LINE,RESERVE);
 #if ASM_CODE
-    reserve("asm",ASM);
-    reserve("__asm",ASM);  // ?
-    reserve("__asm__",ASM);
+    reserve("asm",ASM,RESERVE);
+    reserve("__asm",ASM,RESERVE);  // ?
+    reserve("__asm__",ASM,RESERVE);
 #endif
+    // attributes ( in different name space )
+    reserve("aligned",ALIGNED,ATTRIBUTE);
+    reserve("noreturn",NORETURN,ATTRIBUTE);
 
     codegen_reinit();
     macro_define("__restrict\n"); 
@@ -778,14 +783,13 @@
  */
 
 static void
-reserve(char *s, int d)
+reserve(char *s, int d, int sc)
 {
     NMTBL *nptr;
 
 
-    (nptr = name_space_search(get_name(s,0,DEF),d?0:MACRO))->sc = RESERVE;
+    (nptr = name_space_search(get_name(s,0,DEF),d?0:MACRO))->sc = sc;
     if (d==0) {
-	nptr->sc = MACRO;
 	nptr->dsp = (int)""; nptr->ty=0;
     } else {
 	nptr->dsp = d;
@@ -896,7 +900,10 @@
 	    error(DCERR); return;
 	}
     }
-    while (sym==ATTRIBUTE||sym==ASM) { getsym(0); attributes(); }
+    while (sym==ATTRIBUTE||sym==ASM) { 
+	int sym0 = sym; getsym(0); attributes(sym0); 
+	set_attributes(n); attribute = 0;
+    }
     if(sym==LC || ( sym!=SM && sym!=COMMA && sym!=ASS)) {
 	/* function body */
 	if (mode!=GDECL) {
@@ -907,13 +914,16 @@
 	if (type<0) error(DCERR);
 	else if (car(type)==CODE) {
 	    if (is_function(n)) error(UFERR);
+	    set_attributes(n); attribute = 0;
 	    code_decl(n); return;
 	} else if (car(type)==FUNCTION) {
 	    if (is_code(n)) error(UCERR);
+	    set_attributes(n); attribute = 0;
 	    fdecl(n); return;
 	} else error(DCERR);
     } else {
 	conv->return_type_(type,n,sd);
+	set_attributes(n); attribute = 0;
 	n = def(n,ctmode);
 	if (inmode && (mode==LDECL||mode==LLDECL)) { 
 	    parse = list4(ST_DECL,parse,(int)n,list3(mode,stmode,ctmode));
@@ -934,6 +944,7 @@
 		    error(DCERR);
 	    }
 	    conv->return_type_(type,n,1);
+	    set_attributes(n); attribute = 0;
 	    def(n,ctmode);
 	    if (inmode && mode==LDECL) {
 		parse = list4(ST_DECL,parse,(int)n,list3(mode,stmode,ctmode));
@@ -953,28 +964,43 @@
 }
 
 static void
-attributes()
+attributes(int attr)
 {
-    int sattribute;
+    int smode = mode; mode=ATTRIBUTE;
     checksym(LPAR);
+    if (attr==ASM) {
+	//  int r __asm("r0") 
+	getsym(ATTRIBUTE);
+	if (sym==STRING) {
+	    attribute = list3(ASM,attribute,(int)nptr);
+	}
+	mode = smode;
+	checksym(RPAR);
+	return;
+    }
     while(sym!=RPAR) {
 	if (sym==LPAR) {
-	    sattribute = attribute;
-	    attribute = 0;
-	    attributes();
-	    attribute = list3(ATTRIBUTE,sattribute,attribute);
+	    attributes(0);
 	} else if (sym==IDENT) {
 	    attribute = list3(IDENT,attribute,(int)nptr);
-	    getsym(0);
+	    getsym(ATTRIBUTE);
 	} else if (sym==STRING) {
-	    attribute = list3(STRING,attribute,
-		list3(STRING,(int)nptr,nptr->dsp));
-	    getsym(0);
+	    attribute = list3(STRING,attribute,(int)nptr);
+	    getsym(ATTRIBUTE);
 	} else {
-	    attribute = list3(sym,attribute,symval);
-	    getsym(0);
+	    attribute = list3(sym,attribute,0);
+	    getsym(ATTRIBUTE);
+	    if (sym==LPAR) {
+		getsym(ATTRIBUTE);
+		while(sym!=RPAR) {
+		    caddr(attribute) = list3(sym,caddr(attribute),symval);
+		    getsym(ATTRIBUTE);
+		}
+		getsym(ATTRIBUTE);
+	    }
 	}
     }
+    mode = smode;
     getsym(0);
 }
 
@@ -994,7 +1020,7 @@
 	    break;
 	case ATTRIBUTE:
 	    getsym(0);
-	    attributes();
+	    attributes(ATTRIBUTE);
 	    continue;
 	case INLINE:
 	    inmode = INLINE;
@@ -1125,9 +1151,11 @@
 	    if (nptr->sc==TYPE) {
 		t=nptr->ty;
 		typedefed=glist2((int)nptr,typedefed);
+		copy_attributes(nptr);
 		getsym(0);
 		break;
 	    } else if(nptr->sc==EMPTY && gnptr->sc==TYPE) {
+		// ???
 		getsym(0);
 		break;
 	    }
@@ -2619,7 +2647,7 @@
 	    genswitch(cslist,cslabel);
 	} else if (!cslist) {
 	    if(dlabel) {
-		def_label(cslabel,dlabel);
+		df_label(cslabel,dlabel);
 		cslist=1;
 	    } else {
 	    // no matched value, but some statement may haave control
@@ -2630,7 +2658,7 @@
 	}
 #else
 	if (!(cst && cslit)) {
-	    if(dlabel) def_label(cslabel,dlabel);
+	    if(dlabel) df_label(cslabel,dlabel);
 	    else fwddef(cslabel);
 	}
 #endif
@@ -4619,7 +4647,10 @@
 	}
         /* global variable name table */
 	nptr0 = name_space_search(nlist,sc);
-	if (nptr0->sc == RESERVE) return sym = nptr0->dsp;
+	if (nptr0->sc == RESERVE) 
+	    return sym = nptr0->dsp;
+	if (mode==ATTRIBUTE && nptr0->sc == ATTRIBUTE) 
+	    return sym = nptr0->dsp;
 	sym = IDENT;
 	gnptr=nptr=nptr0;
 
@@ -5420,6 +5451,27 @@
     return;
 }
 
+/*
+    transfer attribtes() value to nptr;
+ */
+
+static void
+set_attributes(NMTBL *n) {
+    int e;
+    for(e = attribute; e ; e = cadr(e)) {
+	set_attr(n,car(e),caddr(e));
+    }
+}
+
+static void
+copy_attributes(NMTBL *n) {
+    int attr;
+    for(attr=n->attr;attr;attr=cadr(attr)) {
+	attribute = list3(car(attr),attribute,caddr(attr));
+    }
+}
+
+
 extern void
 display_ntable(NMTBL *n, char *s)
 {
--- a/mc.h	Fri Nov 17 20:00:37 2006 +0900
+++ b/mc.h	Fri Nov 17 22:30:24 2006 +0900
@@ -110,6 +110,9 @@
 #define C_FUNCTION     (-63)
 #define C_LINE     (-64)
 
+#define ALIGNED     (-65)
+#define NORETURN     (-66)
+
 /* reserved word end */
 
 #define EMPTY   (-99)
--- a/test/ps2.c	Fri Nov 17 20:00:37 2006 +0900
+++ b/test/ps2.c	Fri Nov 17 22:30:24 2006 +0900
@@ -99,10 +99,11 @@
 main(int ac, char *av[])
 {
     FMATRIX m;
+    int aligned = 10;
 
     m[1][1] = 0.5;
     ps2_vu0_unit_matrix(m);
-    printf("%g\n",m[1][1]);
+    printf("%g %d\n",m[1][1],aligned);
     align16(2,m,m);
     align16_1(2,m,m);
     graphic_ObjNode_draw( &p, m);