4655 lines · 110.9 KB · C
RawDownload
/*  $VER: vbcc (declaration.c) V0.8     */ #include <string.h> #include "vbcc_cpp.h"#include "vbc.h" static char FILE_[]=__FILE__; #define PLAIN_STORAGE_CLASS 7#define PARAMETER 8#define OLDSTYLE 16 void dynamic_init(struct Var *v,struct Typ *t,struct const_list *cl,zmax of,int noconst);int test_assignment(struct Typ *,np);int return_sc,return_reg,has_return,return_inline;char *return_vattr;static int did_return_label;#ifdef HAVE_TARGET_ATTRIBUTESunsigned long return_tattr;#endifzmax init_dyn_sz,init_const_sz;int init_dyn_cnt,init_const_cnt; void init_sl(struct struct_list *sl); #ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed */#endif extern np gen_libcall(char *,np,struct Typ *,np,struct Typ *);extern int float_used;extern void optimize(long,struct Var *); static void clear_main_ret(void){  if(c99&&!strcmp(cur_func,"main")&&return_typ&&ISSCALAR(return_typ->flags)){    /* in c99, main returns 0 if it falls from back */    struct IC *new=new_IC();    new->code=SETRETURN;    new->q1.val.vmax=l2zm(0L);    eval_const(&new->q1.val,MAXINT);    insert_const(&new->q1.val,return_typ->flags&NU);    new->q1.flags=KONST;    new->typf=return_typ->flags;    new->q2.val.vmax=szof(return_typ);    new->q2.flags=new->z.flags=0;    new->z.reg=freturn(return_typ);    add_IC(new);  }} static char *get_string(void)/* Liest Stringkonstante und liefert Wert als String (malloced) */{  np tree;int l;char *p;  struct const_list *cl;  killsp();  if(ctok->type!=T_STRING) {error(74);return 0;}  tree=string_expression();  if(!tree||tree->flags!=STRING){    error(229);    return 0;  }  cl=tree->cl;l=0;  while(cl){    l++;    cl=cl->next;  }  p=mymalloc(l);  cl=tree->cl;l=0;  while(cl){    p[l]=zm2l(zc2zm(cl->other->val.vchar));    l++;    cl=cl->next;  }  if(tree) free_expression(tree);  killsp();  return p;} /* checks whether string is a valid vector type *//* returns the dimension */static int check_vect(char *s,char *base){  int i=strlen(base),dim;  if(strncmp(s,base,i)) return 0;  dim=s[i];  if(dim=='2'||dim=='3'||dim=='4'||dim=='8'){    if(s[i+1]==0)      return dim-'0';    else      return 0;  }  if(s[i]=='1'&&s[i+1]=='6'&&s[i+2]==0)    return 16;  return 0;} int settyp(int typnew, int typold)/* Unterroutine fuer declaration_specifiers().              */{  if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);  if(typold==LONG&&typnew==FLOAT){ error(203); return DOUBLE;}  if(typold==LONG&&typnew==DOUBLE) return LDOUBLE;  if(c99&&typold==LONG&&typnew==LONG) return LLONG;  if(typold!=0&&typnew!=INT){error(47);return(typnew);}  if(typold==0&&typnew==INT) return(INT);  if(typold==0) return(typnew);  if(typold==SHORT||typold==LONG||typold==LLONG) return(typold);  error(48);  return(typnew);} #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)#define XSIGNED 16384 #ifdef HAVE_MISRA/* removed */#endif struct Typ *declaration_specifiers(void)/* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      *//* parst z.B. unsigned int, struct bla etc.               */{  int typ=0,type_qualifiers=0,notdone,storage_class,hard_reg,have_inline;#ifdef HAVE_ECPP/* removed *//* removed *//* removed */#endif  char *imerk,sident[MAXI],sbuff[MAXI],*attr=0,*vattr=0;  struct Typ *new=new_typ(),*t,*ts;  struct struct_declaration *ssd;  struct struct_list (*sl)[];  size_t slsz;  struct Var *v;  int dim;#ifdef HAVE_TARGET_ATTRIBUTES  unsigned long tattr=0;#endif  storage_class=hard_reg=have_inline=0;  do{    killsp();notdone=0;    if(ctok->type==NAME){      if(!strcmp("struct",ctok->name)||!strcmp("union",ctok->name)||(ecpp&&!strcmp("class",ctok->name))){        if(!strcmp("struct",ctok->name)) notdone=STRUCT;        if(!strcmp("union",ctok->name)) {          notdone=UNION;#ifdef HAVE_MISRA/* removed */#endif        }#ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed *//* removed */#endif        next_token();        killsp();        if(ctok->type!=LBRA){          cpbez(sident,1);          next_token();          killsp();          ssd=find_struct(sident,0);#ifdef HAVE_MISRA/* removed */#endif          if(ssd&&ctok->type==LBRA&&find_struct(sident,nesting)&&ssd->count>0) error(13,sident);          if(!ssd||((ctok->type==LBRA||ctok->type==SEMIC)&&!find_struct(sident,nesting))){            typ=settyp(notdone,typ);            ssd=mymalloc(sizeof(*ssd));            ssd->count=0;            new->exact=ssd=add_sd(ssd,notdone);            if(!ecpp)add_struct_identifier(sident,ssd);#ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed */#endif          }else{            new->exact=ssd;            typ=settyp(new->flags=notdone,typ);          }        }else{          *sident=0;          typ=settyp(notdone,typ);          ssd=mymalloc(sizeof(*ssd));          ssd->count=0;          new->exact=ssd=add_sd(ssd,notdone);        }        if(ssd->typ!=notdone) error(230,sident);#ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed */#endif                if(ctok->type==LBRA){          int bfoffset,bfsize,flex_array=0,cbfo=0;          zmax off=l2zm(0L);          next_token();          killsp();          slsz=SLSIZE;          imerk=ident;            sl=mymalloc(slsz*sizeof(struct struct_list));            ssd->count=0;#ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed */#endif          ts=declaration_specifiers();          while(ctok->type!=RBRA&&ts){            if(flex_array) error(231);            ident=sbuff;            t=declarator(clone_typ(ts));            killsp();            bfsize=bfoffset=-1;            if(ctok->type==T_COLON){              np tree;              if((ts->flags&NQ)!=INT) {                error(51);#ifdef HAVE_MISRA/* removed */#endif              }              next_token();killsp();tree=assignment_expression();               if(type_expression(tree)){                int tsize;                if(tree->flags!=CEXPR) error(52);                if(!ISINT(tree->ntyp->flags)) error(52);                eval_const(&tree->val,tree->ntyp->flags);                bfsize=(int)zm2l(vmax);                tsize=(int)zm2l(zmmult(sizetab[t->flags&NQ],char_bit));                if(bfsize<0||bfsize>tsize||(bfsize==0&&*ident)){                  error(332);bfsize=1;                }                if(bfsize+cbfo>tsize){                  bfoffset=cbfo=0;                }else{                  bfoffset=cbfo;                }                if(bfsize!=0)                  cbfo+=bfsize;                else                  cbfo=0;              }#ifdef HAVE_MISRA/* removed *//* removed *//* removed */#endif              if(tree) free_expression(tree);            }else{              if(!ecpp&&*ident==0) error(53);              cbfo=0;            }#ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed */#endif            if(type_uncomplete(t)){              if(!c99||notdone!=STRUCT||flex_array||(t->flags&NQ)!=ARRAY||type_uncomplete(t->next)){                error(14,sbuff);                freetyp(t);                break;              }else{                flex_array=1;              }            }	    if(ISARRAY(t->flags)&&t->dsize){	      error(352,sbuff);	    }            if(ISFUNC(t->flags)){              if(!ecpp){                error(15,sbuff);              }            }            if(*ident!=0){              int i=ssd->count;              while(--i>=0)                if(!strcmp((*sl)[i].identifier,ident))                  error(16,ident);            }#ifdef HAVE_MISRA/* removed */#endif#ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed */#endif	          (*sl)[ssd->count].bfoffset=bfoffset;            (*sl)[ssd->count].bfsize=bfsize;            (*sl)[ssd->count].styp=t;            if(!ecpp) (*sl)[ssd->count].identifier=add_identifier(ident,strlen(ident));#ifdef HAVE_ECPP/* removed *//* removed *//* removed */#endif            if(pack_align>0&&pack_align<falign(t))              (*sl)[ssd->count].align=pack_align;            else              (*sl)[ssd->count].align=falign(t);            {              zmax m,al=(*sl)[ssd->count].align;              zmax sz=szof(t);              m=off;              if((zmeqto(al,l2zm(0L))||zmeqto(sz,l2zm(0L)))&&!flex_array){                if (!ecpp){                  error(316,ident);                }              }else{                off=zmmult(zmdiv(zmadd(off,zmsub(al,l2zm(1L))),al),al);                if(!zmeqto(m,off)) error(306,ident);                if(!flex_array){                  m=zmmult(zmdiv(zmadd(off,zmsub(sz,l2zm(1L))),sz),sz);                  if(!zmeqto(m,off)) error(307,ident);                  off=zmadd(off,sz);                }              }            }            ssd->count++;            if(ssd->count>=slsz-1){              slsz+=SLSIZE;              sl=myrealloc(sl,slsz*sizeof(struct struct_list));            }            killsp();            if(ctok->type==COMMA) {next_token();killsp();continue;}            if(ctok->type!=SEMIC) error(54); else next_token();            killsp();#ifdef HAVE_ECPP/* removed */#endif            if(ctok->type!=RBRA){              if(ts) freetyp(ts);              ts=declaration_specifiers();killsp();            }          }          if(ts) freetyp(ts);          if(ssd->count==0) error(55);#ifdef HAVE_ECPP/* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed *//* removed */

Lines 1–500 of 4655.