Mercurial > hg > CbC > old > device
changeset 185:427008a06333
comment
author | kono |
---|---|
date | Mon, 01 Dec 2003 03:24:30 +0900 |
parents | 4bf42b2df5d7 |
children | 6391432ca002 |
files | mc-code-powerpc.c mc-parse.c test/enum.c |
diffstat | 3 files changed, 192 insertions(+), 111 deletions(-) [+] |
line wrap: on
line diff
--- a/mc-code-powerpc.c Sun Nov 30 23:56:21 2003 +0900 +++ b/mc-code-powerpc.c Mon Dec 01 03:24:30 2003 +0900 @@ -303,6 +303,7 @@ macro_define("__ppc__ 1\n"); macro_define("__BIG_ENDIAN__ 1\n"); // macro_define("_BSD_CT_RUNE_T_ int\n"); + macro_define("__STDC__ 1\n"); init_ptr_cache(); }
--- a/mc-parse.c Sun Nov 30 23:56:21 2003 +0900 +++ b/mc-parse.c Mon Dec 01 03:24:30 2003 +0900 @@ -301,12 +301,9 @@ gfree=ilabel=1; labelno=2; lfree=HEAPSIZE; - filep=filestack; codegen_init(); macro_define("__micro_c__ 1\n"); - // macro_define("__pure2\n"); - // macro_define("__dead2\n"); macro_define("__restrict\n"); macro_define("__micro_c__ 1\n"); #ifdef __APPLE__ @@ -323,6 +320,7 @@ { reinit(); + filep=filestack; newfile(); getline(); @@ -483,7 +481,6 @@ n=decl0(); reverse(t); if(n == &null_nptr) error(DCERR); - /* if(args) error(DCERR); */ conv->return_type_(type,n,1); def(n); } @@ -572,7 +569,6 @@ if(sym==LONG) { getsym(0); t=LONGLONG; - // t=INT; } else if(sym==DOUBLE) { getsym(0); t=DOUBLE; @@ -637,7 +633,7 @@ n= &null_nptr; } while(1) { - if(sym==LBRA) { + if(sym==LBRA) { /* array */ if(getsym(0)==RBRA) { getsym(0); if(mode==ADECL) { @@ -653,15 +649,14 @@ checksym(RBRA); type=list3(ARRAY,t,i); } - } else if(sym==LPAR) { + } else if(sym==LPAR) { /* function or code segment */ if(mode==GDECL) { mode=ADECL;getsym(0);mode=GDECL; /* ??? */ } else getsym(0); n->dsp=0; if(stmode==EXTRN) n->sc=EXTRN; - if(stmode==STATIC) n->sc=STATIC; - // else n->sc=EXTRN; /* this is odd... */ + else if(stmode==STATIC) n->sc=STATIC; if (type==CODE) { n->ty=CODE; if(sym==RPAR) { @@ -678,8 +673,9 @@ } type=glist3(FUNCTION,type,t); } - // n->ty=type; - /* in GDECL mode dsp contains real parameter, if not, + /* Do not set n->ty here. It could be K&R style arguments or + struct field names */ + /* in GDECL n->dsp contains real parameter, if not, it contains arg type list. Real parameter list is compatible with arg type list. See def/ADECL */ if (mode!=GDECL) @@ -757,6 +753,8 @@ return argtypes; } +/* reverse modifies type also */ + static void reverse(int t1) { @@ -807,6 +805,7 @@ if(t==ENUM) return size_of_int; error(DCERR); } + /* type represented in a list */ if(car(t)==STRUCT||car(t)==UNION) { if(cadr(t)==-1) error(DCERR); return(cadr(t)); @@ -830,6 +829,8 @@ return disp -= sz; } +/* temporal local variable free list */ + static int lvar_list,lvar_free_list; int @@ -897,30 +898,31 @@ } nsc=ndsp=0; if (stmode==EXTRN||mode==GDECL) - n->ty = type; + n->ty = type; /* must be in global table/heap */ if(type>0&&(car(type)==FUNCTION || car(type)==CODE)) { - if (/* stmode==EXTRN ||*/ (mode==GDECL)) { + if ((mode==GDECL)) { fcheck(n); return n; + /* function and code segement are defined using fdecl/code_decl */ + /* in decl() */ } } - if (n->sc!=EMPTY && !(n->sc==EXTRN||n->sc==EXTRN1||n->sc==STATIC)) { - if (mode==ADECL) { - if (n->sc==LVAR && n->ty==INT); - else if ( n->sc==REGISTER && n->ty==INT); - else error(DCERR); - } else if (mode==GSDECL||mode==LSDECL) { -// if (n->sc==FIELD && n->dsp==disp); -// else error(DCERR); - } else if (mode==GUDECL||mode==LUDECL) { -// if (n->sc==FIELD && n->dsp==0); -// else error(DCERR); - } else error(DCERR); + if (mode==GSDECL||mode==LSDECL|| mode==GUDECL||mode==LUDECL) { + /* Struct fileds name lists are in the struct type or tag. */ + /* Only name in the table is used. */ + fields = list4(type,fields,(int)(n->nm),disp); + sz = size(type); + } else { + if (n->sc!=EMPTY && !(n->sc==EXTRN||n->sc==EXTRN1||n->sc==STATIC)) { + /* redefined case */ + 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); + } + sz = size(n->ty = type); } - if (mode==GSDECL||mode==LSDECL) - sz = size(type); - else - sz = size(n->ty = type); switch(mode) { case GDECL: gen_gdecl(n->nm,gpc); @@ -951,25 +953,14 @@ } else gpc +=sz; return n; - case GSDECL: - fields = list4(type,fields,(int)(n->nm),disp); + case GSDECL: case LSDECL: disp += sz; return n; - case GUDECL: - fields = list4(type,fields,(int)(n->nm),disp); - if (disp < sz) disp = sz; - return n; - case LSDECL: - fields = list4(type,fields,(int)(n->nm),disp); - disp += sz; - return n; - case LUDECL: - fields = list4(type,fields,(int)(n->nm),disp); + case GUDECL: case LUDECL: if (disp < sz) disp = sz; return n; case GTDECL: nsc = TYPE; - // if (gnptr!=n) error(-1); gtypedefed=glist2((int)gnptr,gtypedefed); break; case LTDECL: @@ -984,21 +975,19 @@ if(type==CHAR||type==UCHAR) { if (n->dsp==0) { n->dsp = args; - if (endian) - n->dsp += size_of_int-1; + if (endian) n->dsp += size_of_int-1; } args += size_of_int; } else if(type==SHORT||type==USHORT) { if (n->dsp==0) { n->dsp = args; - if (endian) - n->dsp += size_of_int-size_of_short; + if (endian) n->dsp += size_of_int-size_of_short; } args += size_of_int; } else { /* if (n->dsp==0) (argument list in ADECL is useless, type list can be found in type ) */ - n->dsp = args; + n->dsp = args; args += sz; } cadddr(fnptr->dsp)=sz; @@ -1007,7 +996,7 @@ n->ty = type; } return n; - case STAT: /* of course this is wrong */ + case STAT: /* return (struct hoge)f() case? */ case LDECL: if (stmode==REGISTER) { if(scalar(type)) { @@ -1190,6 +1179,14 @@ } #endif +/* + struct/union + tag ... struct/union name + nptr0->sc = TAG; + nptr0->ty = list4(...) + type ... list4(STRUCT,disp,fields,(int)nptr0); + filed ... assoc list defined in def(); + */ static int sdecl(int s) { @@ -1236,7 +1233,6 @@ type0 = list4(s,disp,fields,0); } else error(DCERR); - /* print_fields(fields,"def"); */ stypedecl=1; disp=sdisp; @@ -1245,6 +1241,10 @@ return type0; } +/* + enum + */ + static int edecl() { @@ -1281,6 +1281,9 @@ return type; } +/* code sgement + simpler than fdecl, because it does not have return value. + */ static void code_decl(NMTBL *n) { @@ -1336,6 +1339,10 @@ static NMTBL *tmp_struct; +/* local decl can be used, after {} */ +/* but it's lexical scope remains after {} */ +/* my be in for(int i=....) not yet */ + static void local_decl() { @@ -1353,6 +1360,8 @@ conv->localvar_end_(); } +/* function define */ + static void fdecl(NMTBL *n) { @@ -1361,7 +1370,7 @@ fnptr=n; retlabel=fwdlabel(); retcont = 0; - tmp_struct = 0; + tmp_struct = 0; /* a = f().filed */ reg_var=0; n->ty = type; @@ -1376,7 +1385,7 @@ } } else fnptr->dsp=reverse0(fnptr->dsp); - fdecl_struct(fnptr->ty); + fdecl_struct(fnptr->ty); /* insert extra argument for struct passing */ disp=0; arg_register(fnptr); typedefed=0; @@ -1928,6 +1937,8 @@ checksym(COLON); } +/* numerical type conversion */ + int double_value(int e2,int type) { @@ -1970,6 +1981,9 @@ error(TYERR); return e2; } +/* assign statement */ + +/* keep type */ int assign_expr0(int e1,int e2,int t,int type0) { @@ -1982,6 +1996,8 @@ return e1; } +/* with converion (will destroy type global variable) */ + int assign_expr(int e1,int e2,int t,int type) { if(t==VOID) @@ -2018,6 +2034,8 @@ } } +/* C expression */ + int expr(int noconv) { @@ -2054,7 +2072,8 @@ getsym(0); e2=rvalue(expr1()); return assign_expr(e1,e2,t,type); - case RSHIFT+AS: case LSHIFT+AS: case BAND+AS: case EOR+AS: case BOR+AS: case MOD+AS: + case RSHIFT+AS: case LSHIFT+AS: case BAND+AS: + case EOR+AS: case BOR+AS: case MOD+AS: no_float = 1; case ADD+AS: case SUB+AS: case MUL+AS: case DIV+AS: conv->op_(sym); @@ -2186,7 +2205,7 @@ int e1,e2,t; e1=expr6(); - while(sym==BOR) { /* & */ + while(sym==BOR) { /* | */ conv->op_(sym); e1=rvalue(e1); t=type; @@ -2325,6 +2344,8 @@ return e1; } +/* unary operators */ + static int expr13(void) { @@ -2440,6 +2461,9 @@ return e; } e=expr14(); + + /* postfix unary operators */ + if((op=sym)==INC||op==DEC) { conv->postfix_(sym); lcheck(e); @@ -2489,6 +2513,8 @@ return expr16(e1); } +/* term */ + static int expr14(void) { @@ -2624,6 +2650,8 @@ return expr16(e1); } +/* post fix binary operator (struct . -> or array[] */ + static int expr16(int e1) { @@ -2651,6 +2679,8 @@ return e1; } +/* right value , get the value of the variable */ + static int rvalue(int e) { @@ -2759,6 +2789,8 @@ return(list2(INDIRECT,e)); } +/* filed name search */ + static int search_struct_type(int t,char *name,int *dsp) { @@ -2811,6 +2843,8 @@ return e; } +/* binary floating compuation */ + #define DTYPE(dop) (dop==DOP?DOUBLE:FLOAT) static int @@ -2904,6 +2938,8 @@ return fdbinop(op, e1, e2, t1, t2,FOP); } +/* binary integer compuation */ + static int binop(int op, int e1, int e2, int t1, int t2) { @@ -3040,6 +3076,8 @@ return(list3(type==UNSIGNED?op+US:op,e1,e2)); } +/* coarse for function/code segments arguments */ + int correct_type(int e,int t) { @@ -3064,7 +3102,8 @@ { int t,arglist,e,sz,argtypes,at,ftype; - /* function call */ + /* function call target */ + if(car(type)==POINTER) { if (car(cadr(type))==FUNCTION||car(cadr(type))==CODE) { e1=rvalue(e1); @@ -3076,6 +3115,9 @@ } ftype = type; conv->funcall_(type); + + /* function argments */ + argtypes = caddr(type); if (!integral(t=cadr(type))&&(car(t)==STRUCT||car(t)==UNION)) { /* skip return struct pointer */ @@ -3099,7 +3141,10 @@ conv->funcall_args_(); if(car(t)==CODE) return list4(FUNCTION,e1,arglist,ftype); - type = cadr(ftype); /* return type */ + + /* return type */ + + type = cadr(ftype); if(type==CHAR) type=INT; else if(car(type)==STRUCT||car(type)==UNION) { /* make temporaly struct for return value */ @@ -3209,24 +3254,62 @@ extern double strtod(const char *nptr, char **endptr); +static void +get_name() +{ + int i = 0; + hash = 0; + name = namebuf; + while (alpha(ch) || digit(ch)) { + if (i < LBUFSIZE-1) + hash=(((7*hash)&0xfffffff) ^ (name[i++]=ch)); + getch(); + } + name[i++] = '\0'; +} + +static void +macro_expansion(NMTBL *nptrm) +{ + int i = mode; + int macrop = 0; + int slfree = lfree; + mode = STAT; + + macropp = macro_buf; + if (nptrm->sc == FMACRO) { + macrop=macro_function(macrop,&chptr,nptrm,0); + } else { + macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0); + } + macropp = macro_buf; + mappend(reverse0(macrop)); + macropp[-1] ='\n'; + *macropp =0; + lfree = slfree; + if (lsrc && !asmf && nptrm->sc==FMACRO) gen_comment(macro_buf); + macropp[-1] =0; + if (macro_buf[0]==0) { + mode = i; + return; + } + chptrsave = list2((int)chptr,chptrsave); + chsave = list2(chptr[-1],chsave); + chptr = macro_buf; + ch = *chptr++; + mode = i; +} + static int getsym(int sc) { NMTBL *nptr0,*nptr1,*nptrm; - int i,slfree,macrop,d; + int d; char *scheapp; char c; if (alpha(skipspc())) { - i = hash = 0; - name = namebuf; - while (alpha(ch) || digit(ch)) { - if (i < LBUFSIZE-1) - hash=(((7*hash)&0xfffffff) ^ (name[i++]=ch)); - getch(); - } - name[i++] = '\0'; -/* printf("# hash %u %s\n",hash,name); */ + get_name(); nptrm=msearch(name); if (mode==MDECL) { @@ -3243,35 +3326,10 @@ } if ((nptrm->sc==MACRO&&neqname((char *)car(nptrm->dsp),name)) || (nptrm->sc==FMACRO&&skipspc()=='(')) { - i = mode; - mode = STAT; - macrop = 0; - slfree = lfree; - macropp = macro_buf; - if (nptrm->sc == FMACRO) { - macrop=macro_function(macrop,&chptr,nptrm,0); - } else { - macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0); - } - macropp = macro_buf; - mappend(reverse0(macrop)); - macropp[-1] ='\n'; - *macropp =0; - lfree = slfree; - if (lsrc && !asmf && nptrm->sc==FMACRO) gen_comment(macro_buf); - macropp[-1] =0; - if (macro_buf[0]==0) { - mode = i; - return getsym(0); - } - chptrsave = list2((int)chptr,chptrsave); - chsave = list2(chptr[-1],chsave); - chptr = macro_buf; - ch = *chptr++; - mode = i; + macro_expansion(nptrm); return getsym(0); } - + /* global variable name table */ nptr0 = gsearch(sc); if (nptr0->sc == RESERVE) return sym = nptr0->dsp; sym = IDENT; @@ -3281,13 +3339,18 @@ mode==GTDECL || mode==TOP || mode==GEDECL) { return sym; } + + /* local variable name table */ nptr1=lsearch(nptr0->nm,sc); if (mode==STAT) { if (nptr1->sc == EMPTY) return sym; } nptr=nptr1; return sym; + } else if (digit(ch)||ch=='.') { + /* numerical */ + symval=0; d=0; scheapp = cheapp; if(ch=='.') { @@ -3306,6 +3369,7 @@ *cheapp++ = '.'; /* .0 case */ } else if (ch == '0') { if (getch() == 'x' || ch == 'X') { + /* hexadicimal */ while(1) { if(digit(getch())) symval=symval*16+ch-'0'; @@ -3317,6 +3381,7 @@ } return sym=CONST; } else if (ch!='.'&&ch!='L') { + /* octal */ while (digit(ch)) { symval=symval*8+ch-'0';getch(); } @@ -3341,6 +3406,7 @@ } return sym=CONST; } + /* floating point case */ while(digit(ch)|| ch=='.'||ch=='e') { *cheapp++ = ch; getch(); @@ -3364,6 +3430,7 @@ getstring(); return sym= STRING; } + /* 2 letters literal */ c=ch; getch(); switch(c) { @@ -3574,6 +3641,23 @@ return nptr; } +void +extern_define(char *s,int d,int type,int use) +{ + NMTBL *nptr0; + int i; + + hash=0; name=namebuf; i=0; + while((name[i++] = *s)) { + hash=(((7*hash)&0xfffffff) ^ (*s)); s++; + } + if (cheapp+i >= cheap+CHEAPSIZE) error(STRERR); + name[i++] = 0; + (nptr0 = gsearch(0))->sc = EXTRN; + nptr0->dsp = d; nptr0->ty=type; + if (use) extrn_use(nptr0); +} + static void copy(NMTBL *nptr, char *s) @@ -3695,6 +3779,10 @@ return p; } +/* preprocessor part */ + +/* file inclusion */ + static FILE * getfname(void) { @@ -3731,6 +3819,8 @@ return ( (filep+1)->fcb = fp ); } +/* line input and conversion */ + static int macro_if_depth ; static int macro_if_current ; static int macro_if_skip ; @@ -3761,6 +3851,10 @@ } while(macro_if_skip || linebuf[0] == '#'); } +/* preprocessor directive */ + +/* line continuation \\ */ + static void check_macro_eof() { @@ -3919,22 +4013,7 @@ return 1; } -void -extern_define(char *s,int d,int type,int use) -{ - NMTBL *nptr0; - int i; - - hash=0; name=namebuf; i=0; - while((name[i++] = *s)) { - hash=(((7*hash)&0xfffffff) ^ (*s)); s++; - } - if (cheapp+i >= cheap+CHEAPSIZE) error(STRERR); - name[i++] = 0; - (nptr0 = gsearch(0))->sc = EXTRN; - nptr0->dsp = d; nptr0->ty=type; - if (use) extrn_use(nptr0); -} +/* macro interpreter */ void macro_define(char *macro) @@ -3995,12 +4074,10 @@ } } *cheapp++ = '\0'; -//fprintf(stderr,"before: *%s*\n",(char*)car(nptr->dsp)); while(cheapp>scheapp&&(*cheapp=='\n'||*cheapp==0)) cheapp--; *++cheapp = '\0'; cheapp++; if (cheapp >= cheap+CHEAPSIZE) /* too late? */ error(STRERR); -//fprintf(stderr,"after : *%s*\n",(char*)car(nptr->dsp)); // fprintf(stderr,"%s\n",(char *)car(nptr->dsp)); mode=i; } @@ -4216,6 +4293,8 @@ return macrop; } +/* node management (cdr coding ) */ + int dlist2(int e1, double d1) {