home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / vbcc / declaration.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-17  |  44.6 KB  |  1,476 lines

  1. /*  $VER: vbcc (declaration.c) V0.4     */
  2.  
  3. #include "vbc.h"
  4.  
  5. static char FILE_[]=__FILE__;
  6.  
  7. #define PARAMETER 8
  8. #define OLDSTYLE 16
  9.  
  10. struct const_list *initialization(struct Typ *,int,int);
  11. int test_assignment(struct Typ *,np);
  12. int return_sc,return_reg,has_return;
  13.  
  14. extern int float_used;
  15. extern void optimize(long,struct Var *);
  16.  
  17. int settyp(int typnew, int typold)
  18. /* Unterroutine fuer declaration_specifiers().              */
  19. {
  20.   static int warned_long_double;
  21.   if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);
  22.   if(typold==LONG&&typnew==FLOAT){ error(203); return(DOUBLE);}
  23.   if(typold==LONG&&typnew==DOUBLE){
  24.     if(!warned_long_double){error(204);warned_long_double=1;}
  25.     return(DOUBLE);
  26.   }
  27.   if(typold!=0&&typnew!=INT){error(47);return(typnew);}
  28.   if(typold==0&&typnew==INT) return(INT);
  29.   if(typold==0) return(typnew);
  30.   if(typold==SHORT||typold==LONG) return(typold);
  31.   error(48);
  32.   return(typnew);
  33. }
  34.  
  35. #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)
  36. #define XSIGNED 16384
  37.  
  38. struct Typ *declaration_specifiers(void)
  39. /* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      */
  40. /* parst z.B. unsigned int, struct bla etc.               */
  41. {
  42.   int typ=0,type_qualifiers=0,notdone,storage_class,hard_reg;
  43.   char *merk,*imerk,sident[MAXI],sbuff[MAXI];
  44.   struct Typ *new=mymalloc(TYPS),*t,*ts;
  45.   struct struct_declaration *ssd;
  46.   struct struct_list (*sl)[];
  47.   size_t slsz;
  48.   struct Var *v;
  49.   storage_class=hard_reg=0;
  50.   new->next=0; new->exact=0;
  51.   do{
  52.     killsp();merk=s;cpbez(buff,0);notdone=0;
  53.     if(DEBUG&2) printf("ts: %s\n",buff);
  54.     if(!strcmp("struct",buff)) notdone=STRUCT;
  55.     if(!strcmp("union",buff)) notdone=UNION;
  56.     if(notdone!=0){
  57.       killsp();
  58.       if(*s!='{'){
  59.     cpbez(sident,1);
  60.     killsp();
  61.     ssd=find_struct(sident,0);
  62.     if(ssd&&*s=='{'&&find_struct(sident,nesting)&&ssd->count>0) error(13,sident);
  63.     if(!ssd||((*s=='{'||*s==';')&&!find_struct(sident,nesting))){
  64.       typ=settyp(notdone,typ);
  65.       ssd=mymalloc(sizeof(*ssd));
  66.       ssd->count=0;
  67.       new->exact=ssd=add_sd(ssd);
  68.       add_struct_identifier(sident,ssd);
  69.     }else{
  70.       new->exact=ssd;
  71.       typ=settyp(new->flags=notdone,typ);
  72.     }
  73.       }else{
  74.     *sident=0;
  75.     typ=settyp(notdone,typ);
  76.     ssd=mymalloc(sizeof(*ssd));
  77.     ssd->count=0;
  78.     new->exact=ssd=add_sd(ssd);
  79.       }
  80.       if(*s=='{'){
  81.     s++;
  82.     killsp();
  83.     slsz=SLSIZE;
  84.     sl=mymalloc(slsz*sizeof(struct struct_list));
  85.     ssd->count=0;
  86.     imerk=ident;
  87.     ts=declaration_specifiers();
  88.     while(*s!='}'&&ts){
  89.       ident=sbuff;
  90.       t=declarator(clone_typ(ts));
  91.       killsp();
  92.       if(*s==':'){
  93.         /*  bitfields werden hier noch ignoriert    */
  94.         np tree;
  95.         if((ts->flags&NQ)!=INT) error(51);
  96.         s++;killsp();tree=assignment_expression();
  97.         if(type_expression(tree)){
  98.           if(tree->flags!=CEXPR) error(52);
  99.           if((tree->ntyp->flags&NQ)<CHAR||(tree->ntyp->flags&NQ)>LONG) error(52);
  100.         }
  101.         if(tree) free_expression(tree);
  102.       }else{
  103.         if(*ident==0) error(53);
  104.       }
  105.       if(type_uncomplete(t)){
  106.         error(14,sbuff);
  107.         freetyp(t);
  108.         break;
  109.       }
  110.       if((t->flags&NQ)==FUNKT)
  111.         error(15,sbuff);
  112.       
  113.       if(*ident!=0){
  114.         int i=ssd->count;
  115.         while(--i>=0)
  116.           if(!strcmp((*sl)[i].identifier,ident))
  117.         error(16,ident);
  118.       }
  119.       (*sl)[ssd->count].styp=t;
  120.       (*sl)[ssd->count].identifier=add_identifier(ident,strlen(ident));
  121.       ssd->count++;
  122.       if(ssd->count>=slsz-1){
  123.         slsz+=SLSIZE;
  124.         sl=realloc(sl,slsz*sizeof(struct struct_list));
  125.         if(!sl){error(12);raus();}
  126.       }
  127.       killsp();
  128.       if(*s==',') {s++;killsp();continue;}
  129.       if(*s!=';') error(54); else s++;
  130.       killsp();
  131.       if(*s!='}'){
  132.         if(ts) freetyp(ts);
  133.         ts=declaration_specifiers();killsp();
  134.       }
  135.     }
  136.     if(ts) freetyp(ts);
  137.     if(ssd->count==0) error(55);
  138.     ident=imerk;
  139.     add_sl(ssd,sl);
  140.     free(sl);
  141.     if(*s!='}') error(56); else s++;
  142.     new->flags=notdone|type_qualifiers;
  143.       }
  144.       notdone=1;
  145.     }
  146.     if(!strcmp("enum",buff)){
  147.       /*  enumerations; die Namen werden leider noch ignoriert    */
  148.       killsp();notdone=1;
  149.       if(*s!='{'){cpbez(buff,1);killsp();}
  150.       if(*s=='{'){
  151.     zlong val; struct Var *v; struct Typ *t;
  152.     val=l2zl(0L);
  153.     s++;killsp();
  154.     while(*s!='}'){
  155.       cpbez(sident,1);killsp();
  156.       if(*sident==0) {error(56);break;}
  157.       t=mymalloc(TYPS);
  158.       t->flags=CONST|INT;
  159.       t->next=0;
  160.       if(find_var(sident,nesting)) error(17,sident);
  161.       v=add_var(sident,t,AUTO,0); /*  AUTO hier klug? */
  162.       if(*s=='='){
  163.         s++;killsp();
  164.         v->clist=initialization(v->vtyp,0,0);
  165.         val=zi2zl(v->clist->val.vint);killsp();
  166.       }else{
  167.         v->clist=mymalloc(CLS);
  168.         v->clist->val.vint=val;
  169.         v->clist->next=v->clist->other=0;
  170.         v->clist->tree=0;
  171.       }
  172.       vlong=l2zl(1L);val=zladd(val,vlong);
  173.       v->vtyp->flags=CONST|ENUM;
  174.       if(*s!='}'&&*s!=',') {error(56);break;}
  175.       if(*s==',') s++;
  176.       killsp();
  177.       if(*s=='}') {s++; break;}
  178.     }
  179.       }
  180.       killsp();
  181.       typ=settyp(INT,typ);*buff=0;
  182.     }
  183.     if(!strcmp("void",buff)) {typ=settyp(VOID,typ);notdone=1;}
  184.     if(!strcmp("char",buff)) {typ=settyp(CHAR,typ);notdone=1;}
  185.     if(!strcmp("short",buff)) {typ=settyp(SHORT,typ);notdone=1;}
  186.     if(!strcmp("int",buff)) {typ=settyp(INT,typ);notdone=1;}
  187.     if(!strcmp("long",buff)) {typ=settyp(LONG,typ);notdone=1;}
  188.     if(!strcmp("float",buff)) {typ=settyp(FLOAT,typ);notdone=1;}
  189.     if(!strcmp("double",buff)) {typ=settyp(DOUBLE,typ);notdone=1;}
  190.     if(!strcmp("const",buff)){
  191.       if(type_qualifiers&CONST) error(58);
  192.       type_qualifiers|=CONST;notdone=1;
  193.     }
  194.     if(!strcmp("volatile",buff)){
  195.       if(type_qualifiers&VOLATILE) error(58);
  196.       type_qualifiers|=VOLATILE;notdone=1;
  197.     }
  198.     if(!strcmp("unsigned",buff)){
  199.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  200.       notdone=1;type_qualifiers|=UNSIGNED;
  201.     }
  202.     if(!strcmp("signed",buff)){
  203.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  204.       notdone=1;type_qualifiers|=XSIGNED;
  205.     }
  206.     if(!strcmp("auto",buff)) {dsc;storage_class=AUTO;notdone=1;}
  207.     if(!strcmp("register",buff)){dsc;storage_class=REGISTER;notdone=1;}
  208.     if(!strcmp("static",buff)) {dsc;storage_class=STATIC;notdone=1;}
  209.     if(!strcmp("extern",buff)) {dsc;storage_class=EXTERN;notdone=1;}
  210.     if(!strcmp("typedef",buff)) {dsc;storage_class=TYPEDEF;notdone=1;}
  211.     if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)){
  212.       char *d;int f=0;
  213.       killsp(); if(*s=='(') s++; else error(151);
  214.       killsp(); if(*s=='\"') s++; else error(74);
  215.       d=buff;
  216.       while(*s&&*s!='\"'){
  217.     if(d-buff-2>MAXI){
  218.       if(!f){ error(206,MAXI);f=1;}
  219.     }else *d++=*s;
  220.     s++;
  221.       }
  222.       *d=0;
  223.       if(*s=='\"') s++; else error(74);
  224.       killsp(); if(*s==')') s++; else error(59);
  225.       for(hard_reg=1;hard_reg<=MAXR;hard_reg++){
  226.     if(!strcmp(buff,regnames[hard_reg])) break;
  227.       }
  228.       if(hard_reg>MAXR){ hard_reg=0;error(220,buff);}
  229.       notdone=1;
  230.     }
  231.     
  232.     if(!notdone&&*buff&&typ==0&&!(type_qualifiers&(XSIGNED|UNSIGNED))){
  233.       v=find_var(buff,0);
  234.       if(v&&v->storage_class==TYPEDEF){
  235.     free(new);
  236.     new=clone_typ(v->vtyp);
  237.     typ=settyp(new->flags,typ);
  238.     notdone=1;
  239.       }
  240.     }
  241.     if(DEBUG&2) printf("typ:%d\n",typ);
  242.   }while(notdone);
  243.   s=merk;killsp();
  244.   return_sc=storage_class;
  245.   return_reg=hard_reg;
  246.   if(typ==0){
  247.     if(storage_class==0&&type_qualifiers==0) {free(new);return(0);}
  248.     typ=INT;
  249.   }
  250.   if(type_qualifiers&(XSIGNED|UNSIGNED))
  251.     if(typ!=INT&&typ!=CHAR&&typ!=LONG&&typ!=SHORT)
  252.       error(58);
  253.   if(DEBUG&2) printf("ts finish:%s\n",s);
  254.   new->flags=typ|type_qualifiers;
  255.   return(new);
  256. }
  257.  
  258. struct Typ *declarator(struct Typ *a)
  259. /* Erzeugt einen neuen Typ, auf Basis des Typs a.           */
  260. /* a wird hiermit verkettet.                                */
  261. {
  262.   struct Typ *t;
  263.   killsp();*ident=0;
  264.   t=direct_declarator(pointer(a));
  265.   if(!a) {if(t) freetyp(t);return(0);} else return(t);
  266. }
  267. struct Typ *pointer(struct Typ *a)
  268. /* Unterroutine fuer declaration(), behandelt Zeiger auf Typ.   */
  269. {
  270.   struct Typ *t;char *merk;int notdone;
  271.   if(!a) return(0);
  272.   killsp();
  273.   while(*s=='*'){
  274.     s++;
  275.     t=mymalloc(TYPS);
  276.     t->flags=POINTER;
  277.     t->next=a;
  278.     a=t;
  279.     do{
  280.       killsp();
  281.       merk=s;cpbez(buff,0);
  282.       notdone=0;
  283.       if(!strcmp("const",buff)) {a->flags|=CONST;notdone=1;}
  284.       if(!strcmp("volatile",buff)) {a->flags|=VOLATILE;notdone=1;}
  285.     }while(notdone);
  286.     s=merk;
  287.   }
  288.   return(a);
  289. }
  290.  
  291. struct Typ *direct_declarator(struct Typ *a)
  292. /*  Unterroutine zu declarator()                    */
  293. /*  behandelt [],(funkt),(dekl).                     */
  294. {
  295.   struct Typ *rek=0,*merk,*p,*t,*first,*last=0;
  296.   struct struct_declaration *fsd;
  297.   struct struct_list (*sl)[];
  298.   size_t slsz;
  299.   char *imerk,fbuff[MAXI];
  300.   killsp();
  301.   if(!isalpha((unsigned char)*s)&&*s!='_'&&*s!='('&&*s!='[') return(a);
  302.   if(isalpha((unsigned char)*s)||*s=='_'){
  303.     cpbez(ident,1);
  304.     if(!a) return(0);
  305.   }else if(*s=='('&&a){
  306.     /* Rekursion */
  307.     imerk=s; s++; killsp();
  308.     if(*s!=')'&&*ident==0&&!declaration(0)){
  309.       merk=a;
  310.       rek=declarator(a);
  311.       if(*s!=')') error(59); else s++;
  312.     }else s=imerk;
  313.   }
  314.   if(!a)return(0);
  315.   killsp();
  316.   while(*s=='['||*s=='('){
  317.     if(*s=='['){
  318.       s++;
  319.       killsp();
  320.       p=mymalloc(TYPS);
  321.       p->flags=ARRAY;
  322.       p->next=0;
  323.       if(*s==']'){
  324.     p->size=l2zl(0L);
  325.       }else{
  326.     np tree;
  327.     tree=expression();
  328.     if(!type_expression(tree)){
  329.       /*                    error("incorrect constant expression");*/
  330.     }else{
  331.       if(tree->sidefx) error(60);
  332.       if(tree->flags!=CEXPR||(tree->ntyp->flags&NQ)<CHAR||(tree->ntyp->flags&NQ)>LONG){
  333.         error(19);
  334.       }else{
  335.         eval_constn(tree);
  336.         p->size=vlong;
  337.         if(zleqto(p->size,l2zl(0L))) {error(61);p->size=l2zl(1L);}
  338.       }
  339.     }
  340.     free_expression(tree);
  341.       }
  342.       if(*s!=']') error(62); else s++;
  343.       if(last){
  344.     last->next=p;
  345.     last=p;
  346.       }else{
  347.     first=last=p;
  348.       }
  349.     }
  350.     if(*s=='('){
  351.       int komma,oldstyle=0;
  352. #ifdef HAVE_REGPARMS
  353.       struct reg_handle reg_handle=empty_reg_handle;
  354.       struct Typ rpointer={POINTER};
  355.       if(!freturn(a)){
  356.     rpointer.next=a;
  357.     if(!reg_parm(®_handle,&rpointer)) ierror(0);
  358.       }
  359. #endif
  360.       s++;
  361.       killsp();
  362.       fsd=mymalloc(sizeof(*fsd));
  363.       slsz=SLSIZE;
  364.       sl=mymalloc(sizeof(struct struct_list)*slsz);
  365.       fsd->count=0;
  366.       imerk=ident;komma=0;
  367.       enter_block();
  368.       while(*s!=')'&&*s!='.'){
  369.     int hard_reg;
  370.     ident=fbuff;*fbuff=0;komma=0;
  371.     t=declaration_specifiers();
  372.     hard_reg=return_reg;
  373.     t=declarator(t);
  374.     if(!t){
  375.       oldstyle=1;
  376.       if(*ident==0) {error(20);break;}
  377.     }
  378.     if(fsd->count){
  379.       if((t&&!(*sl)[fsd->count-1].styp)||
  380.          (!t&&(*sl)[fsd->count-1].styp))
  381.         error(63);
  382.     }
  383.     if(!return_sc) return_sc=AUTO;
  384.     if(return_sc!=AUTO&&return_sc!=REGISTER)
  385.       {error(21);return_sc=AUTO;}
  386.     (*sl)[fsd->count].styp=t;
  387.     (*sl)[fsd->count].storage_class=return_sc;
  388.     if(hard_reg&&!regok(hard_reg,t->flags,0)) error(217,regnames[hard_reg]);
  389.     (*sl)[fsd->count].identifier=add_identifier(ident,strlen(ident));
  390.     if(t){
  391.       if(((*sl)[fsd->count].styp->flags&NQ)==VOID&&fsd->count!=0)
  392.         error(22);
  393.       /*  Arrays in Zeiger umwandeln  */
  394.       if(((*sl)[fsd->count].styp->flags&NQ)==ARRAY)
  395.         (*sl)[fsd->count].styp->flags=POINTER;
  396.       /*  Funktionen in Zeiger auf Funktionen umwandeln   */
  397.       if(((*sl)[fsd->count].styp->flags&NQ)==FUNKT){
  398.         struct Typ *new;
  399.         new=mymalloc(TYPS);
  400.         new->flags=POINTER;
  401.         new->next=(*sl)[fsd->count].styp;
  402.         (*sl)[fsd->count].styp=new;
  403.       }      
  404.     }
  405. #ifdef HAVE_REGPARMS
  406.     if(t) (*sl)[fsd->count].reg=reg_parm(®_handle,t);
  407.     if(hard_reg) (*sl)[fsd->count].reg=hard_reg;
  408. #else
  409.     (*sl)[fsd->count].reg=hard_reg;
  410. #endif
  411.     fsd->count++;
  412.     if(fsd->count>=slsz-2){     /*  eins Reserve fuer VOID  */
  413.       slsz+=SLSIZE;
  414.       sl=realloc(sl,slsz*sizeof(struct struct_list));
  415.       if(!sl){error(12);raus();}
  416.     }
  417.     killsp(); /* Hier Syntaxpruefung strenger machen */
  418.     if(*s==',') {s++;komma=1; killsp();}
  419.       }
  420.       ident=imerk;
  421.       if((*s!='.'||*(s+1)!='.'||*(s+2)!='.')||!komma){
  422.     if(fsd->count>0&&(!(*sl)[fsd->count-1].styp||((*sl)[fsd->count-1].styp->flags&NQ)!=VOID)){
  423.       (*sl)[fsd->count].styp=mymalloc(TYPS);
  424.       (*sl)[fsd->count].styp->flags=VOID;
  425.       (*sl)[fsd->count].styp->next=0;
  426.       (*sl)[fsd->count].identifier=empty;
  427.       fsd->count++;
  428.     }
  429.       }else if(komma){
  430.     s+=3;komma=0;
  431.     if(oldstyle) error(221);
  432.       }
  433.       p=mymalloc(TYPS);
  434.       p->flags=FUNKT;
  435.       p->next=0;
  436.       {
  437.     int m=nesting;
  438.     nesting=0;
  439.     p->exact=add_sd(fsd);
  440.     add_sl(fsd,sl);
  441.     free(sl);
  442.     nesting=m;
  443.       }
  444.       killsp();
  445.       if(komma) error(59);
  446.       if(*s!=')') error(59); else s++;
  447.       killsp();
  448.       if(*s==','||*s==';'||*s==')'||*s=='=') leave_block();
  449.       if(last){
  450.     last->next=p;
  451.     last=p;
  452.       }else{
  453.     first=last=p;
  454.       }
  455.     }
  456.     killsp();
  457.   }
  458.   if(last){last->next=a;last=a;a=first;}
  459.   if(rek!=0&&rek!=merk){
  460.     /* Zweite Liste anhaengen */
  461.     p=rek;
  462.     while(p->next!=merk) p=p->next;
  463.     if(p) p->next=a; else ierror(0);
  464.     return(rek);
  465.   }
  466.   return(a);
  467. }
  468. int declaration(int offset)
  469. /*  Testet, ob eine Typangabe kommt. Wenn offset!=0 ist,    */
  470. /*  muss s auf '(' zeigen und es wird getestet, ob nach der */
  471. /*  Klammer eine Typangabe kommt.                           */
  472. /*  In jedem Fall zeigt s danach wieder auf dieselbe Stelle */
  473. /*  im Source.                                              */
  474. {
  475.   char *merk=s,buff[MAXI];
  476.   struct Var *v;
  477.   if(offset){
  478.     s++;
  479.     read_new_line=0;
  480.     if(DEBUG&1) printf("cleared read_new_line\n");
  481.     killsp();
  482.     if(read_new_line){  /*  es kam eine neue Zeile  */
  483.       memmove(s+1,s,MAXINPUT);
  484.       *s='(';
  485.       if(DEBUG&1) printf("look-ahead: %s|\n",s);
  486.       merk=s;
  487.       s++;
  488.       cpbez(buff,0);
  489.     }else{
  490.       if(DEBUG&1) printf("read_new_line unchanged\n");
  491.       cpbez(buff,0);
  492.     }
  493.   }else{
  494.     cpbez(buff,0);
  495.   }
  496.   s=merk;
  497.   if(!strcmp("auto",buff)) return(1);
  498.   if(!strcmp("char",buff)) return(1);
  499.   if(!strcmp("const",buff)) return(1);
  500.   if(!strcmp("double",buff)) return(1);
  501.   if(!strcmp("enum",buff)) return(1);
  502.   if(!strcmp("extern",buff)) return(1);
  503.   if(!strcmp("float",buff)) return(1);
  504.   if(!strcmp("int",buff)) return(1);
  505.   if(!strcmp("long",buff)) return(1);
  506.   if(!strcmp("register",buff)) return(1);
  507.   if(!strcmp("short",buff)) return(1);
  508.   if(!strcmp("signed",buff)) return(1);
  509.   if(!strcmp("static",buff)) return(1);
  510.   if(!strcmp("struct",buff)) return(1);
  511.   if(!strcmp("typedef",buff)) return(1);
  512.   if(!strcmp("union",buff)) return(1);
  513.   if(!strcmp("unsigned",buff)) return(1);
  514.   if(!strcmp("void",buff)) return(1);
  515.   if(!strcmp("volatile",buff)) return(1);
  516.   if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)) return(1);
  517.   v=find_var(buff,0);
  518.   if(v&&v->storage_class==TYPEDEF) return(1);
  519.   return(0);
  520. }
  521. void add_sl(struct struct_declaration *sd,struct struct_list (*sl)[])
  522. /*  Fuegt ein struct_list-Array in eine struct_declaration ein.     */
  523. /*  Das Array muss mind. sd->count Elements haben und wird kopiert. */
  524. {
  525.   size_t sz=sizeof(struct struct_list)*sd->count;
  526.   sd->sl=mymalloc(sz);
  527.   memcpy(sd->sl,sl,sz);
  528. }
  529. struct struct_declaration *add_sd(struct struct_declaration *new)
  530. /*  Fuegt eine struct Declaration in Liste ein.     */
  531. {
  532.   new->next=0;
  533.   if(first_sd[nesting]==0){
  534.     first_sd[nesting]=last_sd[nesting]=new;
  535.   }else{
  536.     last_sd[nesting]->next=new;
  537.     last_sd[nesting]=new;
  538.   }
  539.   return(new);
  540. }
  541. void free_sd(struct struct_declaration *p)
  542. /*  Gibt eine struct_declaration-List inkl. struct_lists und    */
  543. /*  allen Typen jeder struct_list frei, nicht aber identifier.  */
  544. {
  545.   int i;struct struct_declaration *merk;
  546.   while(p){
  547.     merk=p->next;
  548.     for(i=0;i<p->count;i++) if((*p->sl)[i].styp) freetyp((*p->sl)[i].styp);
  549.     if(p->count>0) free(p->sl);
  550.     free(p);
  551.     p=merk;
  552.   }
  553. }
  554. char *add_identifier(char *identifier,int length)
  555. /*  Kopiert identifier an sicheren Ort, der spaeter zentral     */
  556. /*  freigegeben werden kann.                                    */
  557. /*  Sollte noch einbauen, dass ueberprueft wird, ob schon       */
  558. /*  vorhanden und dann nicht zweimal speichern.                 */
  559. {
  560.   struct identifier_list *new;
  561.   if((*identifier==0&&length==0)||identifier==empty) return(empty);
  562.   new=mymalloc(sizeof(struct identifier_list));
  563.   new->identifier=mymalloc(length+1);
  564.   memcpy(new->identifier,identifier,length+1);
  565.   new->next=0;new->length=length;
  566.   if(last_ilist[nesting]){
  567.     last_ilist[nesting]->next=new;
  568.     last_ilist[nesting]=new;
  569.   }else{
  570.     last_ilist[nesting]=first_ilist[nesting]=new;
  571.   }
  572.   return(new->identifier);
  573. }
  574. void free_ilist(struct identifier_list *p)
  575. /*  Gibt eine verkettete identifier_liste und saemtliche darin  */
  576. /*  gespeicherten Identifier frei.                              */
  577. {
  578.   struct identifier_list *merk;
  579.   while(p){
  580.     merk=p->next;
  581.     if(p->identifier) free(p->identifier);
  582.     free(p);
  583.     p=merk;
  584.   }
  585. }
  586. int type_uncomplete(struct Typ *p)
  587. /*  Testet, ob Typ unvollstaendig ist. Momentan gelten nur      */
  588. /*  unvollstaendige Strukturen und Arrays von solchen als       */
  589. /*  unvollstaendig, aber keine Zeiger oder Funktionen darauf.   */
  590. {
  591.   struct struct_declaration *sd;
  592.   if(!p){ierror(0);return(0);}
  593.   if((p->flags&NQ)==STRUCT||(p->flags&NQ)==UNION)
  594.     if(p->exact->count<=0) return(1);
  595.   if((p->flags&NQ)==ARRAY){
  596.     if(zlleq(p->size,l2zl(0L))) return(1);
  597.     if(type_uncomplete(p->next)) return(1);
  598.   }
  599.   return(0);
  600. }
  601. void add_struct_identifier(char *identifier,struct struct_declaration *sd)
  602. /*  Erzeugt neuen struct_identifier, fuegt ihn in Liste an und  */
  603. /*  vervollstaendigt unvollstaendige Typen dieser Struktur.     */
  604. {
  605.   struct struct_identifier *new;
  606. /*    struct Typ *t;*/
  607.   if(DEBUG&1) printf("add_si %s (nesting=%d)->%p\n",identifier,nesting,(void *)sd);
  608.   new=mymalloc(sizeof(struct struct_identifier));
  609.   new->identifier=add_identifier(identifier,strlen(identifier));
  610.   new->sd=sd; new->next=0;
  611.   if(first_si[nesting]==0){
  612.     first_si[nesting]=new;last_si[nesting]=new;
  613.   }else{
  614.     last_si[nesting]->next=new;last_si[nesting]=new;
  615.   }
  616. }
  617. void free_si(struct struct_identifier *p)
  618. /*  Gibt eine struct_identifier-Liste frei, aber nicht die      */
  619. /*  identifiers und struct_declarations.                        */
  620. {
  621.   struct struct_identifier *merk;
  622.   while(p){
  623.     merk=p->next;
  624.     free(p);
  625.     p=merk;
  626.   }
  627. }
  628. struct struct_declaration *find_struct(char *identifier,int endnesting)
  629. /*  Sucht angegebene Strukturdefinition und liefert             */
  630. /*  entsprechende struct_declaration.                           */
  631. {
  632.   struct struct_identifier *si; int i;
  633.   for(i=nesting;i>=endnesting;i--){
  634.     si=first_si[i];
  635.     while(si){
  636.       if(!strcmp(si->identifier,identifier)){
  637.     if(DEBUG&1) printf("found struct tag <%s> at nesting %d->%p\n",identifier,i,(void *)si->sd);
  638.     return(si->sd);
  639.       }
  640.       si=si->next;
  641.     }
  642.   }
  643.   if(DEBUG&1) printf("didn't find struct tag <%s>\n",identifier);
  644.   return(0);
  645. }
  646. struct Var *add_tmp_var(struct Typ *t)
  647. {
  648.   return add_var(empty,t,AUTO,0);
  649. }
  650. struct Var *add_var(char *identifier, struct Typ *t, int storage_class,struct const_list *clist)
  651. /*  Fuegt eine Variable mit Typ in die var_list ein.            */
  652. /*  In der storage_class werden die Flags PARAMETER und evtl.   */
  653. /*  OLDSTYLE und REGPARM erkannt.                               */
  654. {
  655.   struct Var *new;int f;
  656.   struct struct_declaration *sd;
  657.   static zlong paroffset;
  658.   zlong al;
  659.   /*if(*identifier==0) return;*/ /* sollte woanders bemaekelt werden */
  660.   if(DEBUG&2) printf("add_var(): %s\n",identifier);
  661.   if((t->flags&NQ)==FUNKT&&((t->next->flags&NQ)==ARRAY||(t->next->flags&NQ)==FUNKT))
  662.     error(25);
  663.   new=mymalloc(sizeof(struct Var));
  664.   new->identifier=add_identifier(identifier,strlen(identifier));
  665.   new->clist=clist;
  666.   new->vtyp=t;
  667.   new->storage_class=storage_class&7;
  668.   new->reg=0;
  669.   new->next=0;
  670.   new->flags=0;
  671.   new->fi=0;
  672.   new->nesting=nesting;
  673.   /*    if((storage_class&7)==STATIC||(storage_class&7)==EXTERN) new->flags=USEDASSOURCE|USEDASDEST;*/
  674.   if(DEBUG&2) printf("storage_class=%d\n",storage_class);
  675.   if(storage_class&PARAMETER) new->flags|=USEDASDEST;
  676.   if(storage_class®PARM) {new->flags|=REGPARM;storage_class&=~PARAMETER;}
  677.   if(DEBUG&2) printf("storage_class=%d\n",storage_class);
  678.   if(DEBUG&2) printf("max_offset=%ld\n",max_offset);
  679.   if((storage_class&7)==REGISTER) new->priority=registerpri; else new->priority=0;
  680.   if(last_var[nesting]){
  681.     new->offset=zladd(last_var[nesting]->offset,szof(last_var[nesting]->vtyp));
  682.     last_var[nesting]->next=new;
  683.     last_var[nesting]=new;
  684.   }else{
  685.     new->offset=l2zl(0L);
  686.     paroffset=l2zl(0L);;
  687.     first_var[nesting]=last_var[nesting]=new;
  688.   }
  689.   f=t->flags&NQ;
  690.   if((storage_class&7)==AUTO||(storage_class&7)==REGISTER){
  691.     if(DEBUG&2) printf("auto\n");
  692.     if(type_uncomplete(t)&&(t->flags&NQ)!=ARRAY) error(202,identifier);
  693.     /*  das noch ueberpruefen   */
  694.     if((c_flags_val[0].l&2)&&nesting==1&&!(storage_class&PARAMETER)){
  695.       new->offset=max_offset;
  696.     }else{
  697.       if(storage_class&PARAMETER){
  698.     new->offset=paroffset;
  699.       }else{
  700.     new->offset=local_offset[nesting];
  701.       }
  702.     }
  703.     al=falign(t);
  704.     new->offset=zlmult(zldiv(zladd(new->offset,zlsub(al,l2zl(1L))),al),al);
  705.     if(storage_class&PARAMETER){
  706.       new->offset=zlmult(zldiv(zladd(new->offset,zlsub(maxalign,l2zl(1L))),maxalign),maxalign);
  707.       if(f>=CHAR&&f<=SHORT){
  708.     /*  Integer-Erweiterungen fuer alle Funktionsparameter  */
  709.     paroffset=zladd(new->offset,sizetab[INT]);
  710.       }else{
  711.     if(f==FLOAT&&(storage_class&OLDSTYLE)){
  712.       /*  Bei alten Funktionen werden FLOAT als DOUBLE uebergeben */
  713.       new->offset=zlmult(zldiv(zladd(new->offset,zlsub(align[DOUBLE],l2zl(1L))),align[DOUBLE]),align[DOUBLE]);
  714.       paroffset=zladd(new->offset,sizetab[DOUBLE]);
  715.     }else{
  716.       paroffset=zladd(new->offset,szof(new->vtyp));
  717.     }
  718.       }
  719.     }else{
  720.       local_offset[nesting]=zladd(new->offset,szof(new->vtyp));
  721.     }
  722.     
  723.     if(!(storage_class&PARAMETER))
  724.       if(zlleq(max_offset,local_offset[nesting])) max_offset=local_offset[nesting];
  725.     if(DEBUG&2) printf("max_offset=%ld\n",max_offset);
  726.   }
  727.   if((storage_class&7)==STATIC) new->offset=l2zl((long)++label);
  728.   if(storage_class&PARAMETER){
  729.     
  730.     if(DEBUG&2) printf("parameter\n");
  731.     
  732.     if(f>=CHAR&&f<=SHORT&&!zlleq(sizetab[INT],sizetab[f])){
  733.       if(BIGENDIAN){
  734.     new->offset=zladd(new->offset,zlsub(sizetab[INT],sizetab[f]));
  735.       }else{
  736.     if(!LITTLEENDIAN)
  737.       ierror(0);
  738.       }
  739.     }
  740.     if((storage_class&OLDSTYLE)&&f==FLOAT){
  741.       /*  Bei alten Funktionen werden DOUBLE nach FLOAT konvertiert   */
  742.       struct IC *conv=mymalloc(ICS);
  743.       conv->code=CONVDOUBLE;
  744.       conv->typf=FLOAT;
  745.       conv->q1.flags=VAR|DONTREGISTERIZE;
  746.       conv->z.flags=VAR;
  747.       conv->q2.flags=0;
  748.       conv->q1.v=conv->z.v=new;
  749.       conv->q1.val.vlong=conv->z.val.vlong=l2zl(0);
  750.       add_IC(conv);
  751.       new->flags|=CONVPARAMETER;
  752.     }
  753.     new->offset=zlsub(l2zl(0L),zladd(maxalign,new->offset));
  754.   }
  755.   if((storage_class&7)==EXTERN){
  756.     if(!strcmp("fprintf",identifier)) new->flags|=PRINTFLIKE;
  757.     if(!strcmp("printf",identifier))  new->flags|=PRINTFLIKE;
  758.     if(!strcmp("sprintf",identifier)) new->flags|=PRINTFLIKE;
  759.     if(!strcmp("fscanf",identifier))  new->flags|=SCANFLIKE;
  760.     if(!strcmp("scanf",identifier))   new->flags|=SCANFLIKE;
  761.     if(!strcmp("sscanf",identifier))  new->flags|=SCANFLIKE;
  762.   }
  763.   return(new);
  764. }
  765. void free_var(struct Var *p)
  766. /*  Gibt Variablenliste inkl. Typ, aber ohne Identifier frei.   */
  767. {
  768.   struct Var *merk;
  769.   while(p){
  770.     merk=p->next;
  771.     if(!(p->flags&USEDASADR)&&(p->storage_class==AUTO||p->storage_class==REGISTER)){
  772.       if(*p->identifier&&!(p->flags&USEDASDEST)&&(p->vtyp->flags&NQ)<=POINTER) error(64,p->identifier);
  773.       if(*p->identifier&&!(p->flags&USEDASSOURCE)&&(p->vtyp->flags&NQ)<=POINTER) error(65,p->identifier);
  774.     }
  775.     if(DEBUG&2) printf("free_var %s, pri=%d\n",p->identifier,p->priority);
  776.     if(p->vtyp) freetyp(p->vtyp);
  777.     if(p->clist) free_clist(p->clist);
  778.     if(p->fi){
  779.       if(DEBUG&2) printf("free_fi of function %s\n",p->identifier);
  780.       free_fi(p->fi);
  781.       if(DEBUG&2) printf("end free_fi of function %s\n",p->identifier);
  782.     }
  783.     free(p);
  784.     p=merk;
  785.   }
  786. }
  787. struct Var *find_var(char *identifier,int endnesting)
  788. /*  Sucht Variable mit Bezeichner und liefert Zeiger zurueck    */
  789. /*  es werden nur Variablen der Bloecke endnesting-nesting      */
  790. /*  durchsucht.                                                 */
  791. {
  792.   int i;struct Var *v;
  793.   if(*identifier==0||identifier==0) return(0);
  794.   for(i=nesting;i>=endnesting;i--){
  795.     v=first_var[i];
  796.     while(v){
  797.       if(!strcmp(v->identifier,identifier)) return(v);
  798.       v=v->next;
  799.     }
  800.   }
  801.   return(0);
  802. }
  803. void var_declaration(void)
  804. /*  Bearbeitet eine Variablendeklaration und erzeugt alle       */
  805. /*  noetigen Strukturen.                                        */
  806. {
  807.   struct Typ *ts,*t,*old=0,*om=0;char *imerk,vident[MAXI];
  808.   int mdef=0,makeint=0,notdone,storage_class,msc,extern_flag,isfunc,
  809.     had_decl,hard_reg,mhr;
  810.   struct Var *v;
  811.   ts=declaration_specifiers();notdone=1;
  812.   storage_class=return_sc;hard_reg=return_reg;
  813.   if(storage_class==EXTERN) extern_flag=1; else extern_flag=0;
  814.   killsp();
  815.   if(*s==';'){
  816.     if(storage_class||((ts->flags&NQ)!=STRUCT&&(ts->flags&NQ)!=UNION&&(ts->flags&NQ)!=INT))
  817.       error(36);
  818.     freetyp(ts);s++;killsp();
  819.     return;
  820.   }
  821.   if(nesting==0&&(storage_class==AUTO||storage_class==REGISTER))
  822.     {error(66);storage_class=EXTERN;}
  823.   if(!ts){
  824.     if(nesting<=1){
  825.       ts=mymalloc(TYPS);
  826.       ts->flags=INT;ts->next=0;
  827.       makeint=1;
  828.       if(!storage_class) storage_class=EXTERN;
  829.       error(67);
  830.     }else{
  831.       ierror(0);return;
  832.     }
  833.   }
  834.   if(storage_class==0){
  835.     if(nesting==0) storage_class=EXTERN; else storage_class=AUTO;
  836.   }
  837.   msc=storage_class;mhr=hard_reg;
  838.   while(notdone){
  839.     int oldnesting=nesting;
  840.     imerk=ident;ident=vident;*vident=0;  /* merken von ident hier vermutlich */
  841.     storage_class=msc;hard_reg=mhr;
  842.     if(old) {freetyp(old);old=0;}
  843.     t=declarator(clone_typ(ts));
  844.     if((t->flags&NQ)!=FUNKT) isfunc=0;
  845.     else {isfunc=1;if(storage_class!=STATIC) storage_class=EXTERN;}
  846.     ident=imerk;                    /* nicht unbedingt noetig ?         */
  847.     if(!*vident){
  848.       free(ts);free(t);
  849.       error(36);return;
  850.     }
  851.     v=find_var(vident,oldnesting);
  852.     if(v){
  853.       had_decl=1;
  854.       if(storage_class==TYPEDEF){
  855.     error(226,v->identifier);
  856.       }else{
  857.     if(nesting>0&&(v->flags&DEFINED)&&!extern_flag&&!isfunc){
  858.       error(27,vident);
  859.     }else{
  860.       if(t&&v->vtyp&&!compare_pointers(v->vtyp,t,255)){
  861.         error(68,vident);
  862.       }
  863.       if((storage_class!=v->storage_class&&!extern_flag)||hard_reg!=v->reg)
  864.         error(28,v->identifier);
  865.       if(!isfunc&&!extern_flag) v->flags|=TENTATIVE;
  866.     }
  867.     if(!isfunc){
  868.       v->vtyp=t;
  869.     }else{
  870.       om=v->vtyp;
  871.       if(t->exact->count>0) {old=v->vtyp;v->vtyp=t;}
  872.     }
  873.       }
  874.     }else{
  875.       had_decl=0;
  876.       if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>0) nesting--;
  877.       v=add_var(vident,t,storage_class,0);
  878.       v->reg=hard_reg;
  879.       if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>=0) nesting++;
  880.       if(!v) ierror(0);
  881.       else{
  882.     if(!isfunc&&!extern_flag){
  883.       v->flags|=TENTATIVE;
  884.       if(nesting>0) v->flags|=DEFINED;
  885.     }
  886.       }
  887.       om=0;
  888.     }
  889.     killsp();
  890.     /*  Inline-Assembler-Code in Funktionsdeklarationen */
  891.     if(*s=='='&&(v->vtyp->flags&NQ)==FUNKT&&!(c_flags[7]&USEDFLAG)){
  892.       np tree;
  893.       s++;killsp();
  894.       tree=string_expression();
  895.       if(!tree||tree->flags!=STRING) error(42);
  896.       else{
  897.     int l;struct const_list *cl;
  898.     if(!v->fi) v->fi=new_fi();
  899.     cl=tree->cl;l=0;
  900.     while(cl){
  901.       l++;
  902.       cl=cl->next;
  903.     }
  904.     v->fi->inline_asm=mymalloc(l);
  905.     cl=tree->cl;l=0;
  906.     while(cl){
  907.       v->fi->inline_asm[l]=zl2l(zc2zl(cl->other->val.vchar));
  908.       l++;
  909.       cl=cl->next;
  910.     }
  911.       }
  912.       if(tree) free_expression(tree);
  913.       killsp();
  914.     }           
  915.     /*  Initialisierung von Variablen bei Deklaration   */
  916.     if(*s=='='){
  917.       s++;killsp();
  918.       if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN&&strcmp("main",v->identifier))
  919.     error(168,v->identifier);
  920.       if(v->flags&DEFINED) {if(nesting==0) error(30,v->identifier);}
  921.       else v->flags|=DEFINED;
  922.       if(v->storage_class==TYPEDEF) error(114,v->identifier);
  923.       if(extern_flag){
  924.     if(nesting==0)
  925.       error(118,v->identifier);
  926.     else
  927.       error(207,v->identifier);
  928.     if(v->storage_class!=EXTERN){ error(77);v->storage_class=EXTERN;}
  929.       }
  930.       v->clist=initialization(v->vtyp,v->storage_class==AUTO||v->storage_class==REGISTER,0);
  931.       if(v->clist){
  932.     if((v->vtyp->flags&NQ)==ARRAY&&zleqto(v->vtyp->size,l2zl(0L))){
  933.       struct const_list *p=v->clist;
  934.       while(p){v->vtyp->size=zladd(v->vtyp->size,l2zl(1L));p=p->next;}
  935.       if(v->storage_class==AUTO||v->storage_class==REGISTER){
  936.         local_offset[nesting]=zladd(local_offset[nesting],szof(v->vtyp));
  937.         if(zlleq(max_offset,local_offset[nesting])) max_offset=local_offset[nesting];
  938.       }
  939.     }
  940.     if(v->storage_class==AUTO||v->storage_class==REGISTER){
  941.       struct IC *new;
  942.       /*  Initialisierung von auto-Variablen  */
  943.       new=mymalloc(ICS);
  944.       new->code=ASSIGN;
  945.       new->typf=v->vtyp->flags;
  946.       new->q2.flags=0;
  947.       new->q2.val.vlong=szof(v->vtyp);
  948.       new->z.flags=VAR;
  949.       new->z.v=v;
  950.       new->z.val.vlong=l2zl(0L);
  951.       if(v->clist->tree){
  952.         /*  einzelner Ausdruck  */
  953.         gen_IC(v->clist->tree,0,0);
  954.         convert(v->clist->tree,v->vtyp->flags&NU);
  955.         new->q1=v->clist->tree->o;
  956.         /*                        v->clist=0;*/
  957.       }else{
  958.         /*  Array etc.  */
  959.         struct Var *nv;
  960.         nv=add_var(empty,clone_typ(v->vtyp),STATIC,v->clist);
  961.         nv->flags|=DEFINED;
  962.         nv->vtyp->flags|=CONST;
  963.         /*                        v->clist=0;*/
  964.         new->q1.flags=VAR;
  965.         new->q1.v=nv;
  966.         new->q1.val.vlong=l2zl(0L);
  967.       }
  968.       add_IC(new);
  969.       /*                    if(v->clist&&v->clist->tree){free_expression(v->clist->tree);v->clist->tree=0;}*/
  970.     }else if(c_flags[19]&USEDFLAG){
  971.       /*  Ohne Optimierung gleich erzeugen; das ist noch  */
  972.       /*  etwas von der genauen Implementierung der Liste */
  973.       /*  der Variablen abhaengig.                        */
  974.       struct Var *merk=v->next;
  975.       v->next=0;
  976.       gen_vars(v);
  977.       v->next=merk;
  978.       v->clist=0;
  979.     }
  980.       }
  981.     }else{
  982.       if((v->flags&DEFINED)&&type_uncomplete(v->vtyp)) error(202,v->identifier);
  983.       if((v->vtyp->flags&CONST)&&(v->storage_class==AUTO||v->storage_class==REGISTER))
  984.     error(119,v->identifier);
  985.     }
  986.     if(*s==',') {s++;killsp();mdef=1;} else notdone=0;
  987.   }
  988.   freetyp(ts);
  989.   if(!mdef&&t&&(t->flags&NQ)==FUNKT&&*s!=';'){
  990.     /*  Funktionsdefinition                                     */
  991.     int i,oldstyle=0;
  992. #ifdef HAVE_REGPARMS
  993.     struct reg_handle reg_handle;
  994. #endif
  995.     fline=line;
  996.     if(DEBUG&1) printf("Funktionsdefinition!\n");
  997.     {int i;
  998.     for(i=1;i<=MAXR;i++) {regs[i]=regused[i]=regsa[i];regsbuf[i]=0;}
  999.     }
  1000.     cur_func=v->identifier;
  1001.     if(only_inline==2) only_inline=0;
  1002.     if(nesting<1) ierror(0);
  1003.     if(nesting>1) error(32);
  1004.     if(v->flags&DEFINED) error(33,v->identifier);
  1005.     else v->flags|=DEFINED;
  1006.     if(storage_class!=EXTERN&&storage_class!=STATIC) error(34);
  1007.     if(extern_flag) error(120);
  1008.     if(storage_class==EXTERN&&!strcmp(v->identifier,"main")&&(!t->next||t->next->flags!=INT)) error(121);
  1009.     if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN&&strcmp("main",v->identifier))
  1010.       error(168,v->identifier);
  1011.     while(*s!='{'){
  1012.       /*  alter Stil  */
  1013.       struct Typ *nt=declaration_specifiers();notdone=1;oldstyle=OLDSTYLE;
  1014.       if(!ts) {error(35);}
  1015.       while(notdone){
  1016.     int found=0;
  1017.     imerk=ident;ident=vident;*vident=0;
  1018.     ts=declarator(clone_typ(nt));
  1019.     ident=imerk;
  1020.     if(!ts) {error(36);}
  1021.     else{
  1022.       for(i=0;i<t->exact->count;i++){
  1023.         if(!strcmp((*t->exact->sl)[i].identifier,vident)){
  1024.           found=1;
  1025.           if((*t->exact->sl)[i].styp){
  1026.         error(69,vident);
  1027.         freetyp((*t->exact->sl)[i].styp);
  1028.           }
  1029.           /*  typ[] in *typ   */
  1030.           if((ts->flags&NQ)==ARRAY) ts->flags=POINTER;
  1031.           /*  typ() in *typ() */
  1032.           if((ts->flags&NQ)==FUNKT){
  1033.         struct Typ *new=mymalloc(TYPS);
  1034.         new->flags=POINTER;
  1035.         new->next=ts;
  1036.         ts=new;
  1037.           }
  1038.           if(!return_sc) return_sc=AUTO;
  1039.           if(return_sc!=AUTO&&return_sc!=REGISTER)
  1040.         {error(122);return_sc=AUTO;}
  1041.           (*t->exact->sl)[i].storage_class=return_sc;
  1042.           (*t->exact->sl)[i].reg=return_reg;
  1043.           if(return_reg) error(219);
  1044.           (*t->exact->sl)[i].styp=ts;
  1045.         }
  1046.       }
  1047.     }
  1048.     if(!found) {error(37,vident);}
  1049.     killsp();
  1050.     if(*s==',') {s++;killsp();} else notdone=0;
  1051.       }
  1052.       if(nt) freetyp(nt);
  1053.       if(*s==';'){s++;killsp();
  1054.       }else{
  1055.     error(54);
  1056.     while(*s!='{'&&*s!=';'){s++;killsp();}
  1057.       }
  1058.     }
  1059.     if(t->exact->count==0){
  1060.       struct struct_list sl[1];
  1061.       if(DEBUG&1) printf("prototype converted to (void)\n");
  1062.       t->exact->count=1;
  1063.       sl[0].identifier=empty;
  1064.       sl[0].storage_class=AUTO;
  1065.       sl[0].styp=mymalloc(TYPS);
  1066.       sl[0].styp->flags=VOID;
  1067.       sl[0].styp->next=0;
  1068.       nesting--;
  1069.       add_sl(t->exact,&sl);
  1070.       nesting++;
  1071.     }
  1072.     if(om&&!compare_sd(om->exact,t->exact))
  1073.       error(123);
  1074.     nocode=0;currentpri=1;
  1075.     /*        enter_block();*/
  1076.     local_offset[1]=l2zl(0L);
  1077.     return_var=0;
  1078.     if(!v->vtyp) ierror(0);
  1079. #ifdef HAVE_REGPARMS
  1080.     reg_handle=empty_reg_handle;
  1081. #endif
  1082.     if(v->vtyp->next->flags==VOID) return_typ=0;
  1083.     else{
  1084.       return_typ=v->vtyp->next;
  1085.       if(!freturn(return_typ)){
  1086.     /*  Parameter fuer die Rueckgabe von Werten, die nicht in einem */
  1087.     /*  Register sind.                                              */
  1088.     struct Typ *rt=mymalloc(TYPS);int reg;
  1089.     rt->flags=POINTER;rt->next=return_typ;
  1090. #ifdef HAVE_REGPARMS
  1091.     reg=reg_parm(®_handle,rt);
  1092.     if(!reg) ierror(0);
  1093.     return_var=add_var(empty,clone_typ(rt),AUTO|PARAMETER|REGPARM|oldstyle,0);
  1094.     return_var->reg=reg;
  1095. #else
  1096.     return_var=add_var(empty,clone_typ(rt),AUTO|PARAMETER|oldstyle,0);
  1097. #endif
  1098.     return_var->flags|=DEFINED;
  1099.     free(rt);
  1100.       }
  1101.     }
  1102.     first_ic=last_ic=0;ic_count=0;max_offset=l2zl(0L);
  1103.     for(i=0;i<t->exact->count;i++){
  1104.       if(!(*t->exact->sl)[i].styp&&*(*t->exact->sl)[i].identifier){
  1105.     struct Typ *nt;
  1106.     nt=mymalloc(TYPS);
  1107.     nt->flags=INT; nt->next=0;
  1108.     (*t->exact->sl)[i].styp=nt;
  1109.     (*t->exact->sl)[i].storage_class=AUTO;
  1110.     (*t->exact->sl)[i].reg=0;
  1111.     error(124);
  1112.       }
  1113.       if(*(*t->exact->sl)[i].identifier){
  1114.     struct Var *tmp;int sc;
  1115.     sc=((*t->exact->sl)[i].storage_class|PARAMETER|oldstyle);
  1116. #ifdef HAVE_REGPARMS
  1117.     if(!t->exact->sl) ierror(0);
  1118.     if(!(*t->exact->sl)[i].styp) ierror(0);
  1119.     (*t->exact->sl)[i].reg=reg_parm(®_handle,(*t->exact->sl)[i].styp);
  1120. #endif
  1121.     if((*t->exact->sl)[i].reg) sc|=REGPARM;
  1122.     tmp=add_var((*t->exact->sl)[i].identifier,clone_typ((*t->exact->sl)[i].styp),sc,0);
  1123.     tmp->reg=(*t->exact->sl)[i].reg;
  1124.     tmp->flags|=DEFINED;
  1125.     if(oldstyle){
  1126.       freetyp((*t->exact->sl)[i].styp);
  1127.       (*t->exact->sl)[i].styp=0; /*  Prototype entfernen */
  1128.     }
  1129.       }
  1130.     }
  1131.     if(oldstyle) t->exact->count=0; /*  Prototype entfernen */
  1132.     /*        local_offset[1]=l2zl(0L);*/
  1133.     return_label=++label;
  1134.     v->flags|=GENERATED;
  1135.     function_calls=0;float_used=0;has_return=0;goto_used=0;
  1136.     compound_statement();
  1137.     if((v->vtyp->next->flags&NQ)!=VOID&&!has_return){
  1138.       if(strcmp(v->identifier,"main")) error(173,v->identifier);
  1139.       else error(174,v->identifier);
  1140.     }
  1141. #if 0
  1142.     {int i;
  1143.     for(i=1;i<=MAXR;i++) if(regs[i]!=regsa[i]) {printf("Register %s:\n",regnames[i]);ierror(0);}
  1144.     }
  1145. #endif
  1146.         gen_label(return_label);
  1147.         if(first_ic&&errors==0){
  1148.       if((c_flags[2]&USEDFLAG)&&ic1){fprintf(ic1,"function %s\n",v->identifier); pric(ic1,first_ic);}
  1149.       vl1=first_var[0];
  1150.       vl2=first_var[1];
  1151.       vl3=merk_varf;
  1152.       optimize(c_flags_val[0].l,v);
  1153.       if((c_flags[3]&USEDFLAG)&&ic2){fprintf(ic2,"function %s\n",v->identifier); pric(ic2,first_ic);}
  1154.       if(out&&!only_inline&&!(c_flags[5]&USEDFLAG)){
  1155.         gen_code(out,first_ic,v,max_offset);
  1156.       }
  1157.       /*            if(DEBUG&8192){fprintf(ic2,"function %s, after gen_code\n",v->identifier); pric(ic2,first_ic);}*/
  1158.       free_IC(first_ic);
  1159.       first_ic=last_ic=0;
  1160.         }
  1161.         if(v->fi&&v->fi->first_ic){
  1162.       struct Var *vp;
  1163.       if(DEBUG&1) printf("leave block %d (inline-version)\n",nesting);
  1164.       if(nesting!=1) ierror(0);
  1165.       if(merk_varl) merk_varl->next=first_var[nesting]; else merk_varf=first_var[nesting];
  1166.       if(last_var[nesting]) merk_varl=last_var[nesting];
  1167.       if(merk_sil) merk_sil->next=first_si[nesting]; else merk_sif=first_si[nesting];
  1168.       if(last_si[nesting]) merk_sil=last_si[nesting];
  1169.       if(merk_sdl) merk_sdl->next=first_sd[nesting]; else merk_sdf=first_sd[nesting];
  1170.       if(last_sd[nesting]) merk_sdl=last_sd[nesting];
  1171.       if(merk_ilistl) merk_ilistl->next=first_ilist[nesting]; else merk_ilistf=first_ilist[nesting];
  1172.       if(last_ilist[nesting]) merk_ilistl=last_ilist[nesting];
  1173.       
  1174.       if(merk_varf&&!only_inline) gen_vars(merk_varf);
  1175.       if(first_llist) free_llist(first_llist);
  1176.       if(first_clist) free_clist(first_clist);
  1177.       if(merk_sif) free_si(merk_sif);
  1178. /*  struct-declarations erst ganz am Schluss loeschen. Um zu vermeiden,     */
  1179. /*  dass struct-declarations in Prototypen frei werden und dann eine        */
  1180. /*  spaetere struct, dieselbe Adresse bekommt und dadurch gleich wird.      */
  1181. /*  Nicht sehr schoen - wenn moeglich noch mal aendern.                     */
  1182. /*            if(merk_sdf) free_sd(merk_sdf);*/
  1183.             /*  hier noch was ueberlegen    */
  1184. /*            if(merk_ilistf) free_ilist(merk_ilistf);*/
  1185.       nesting--;
  1186.       v->fi->vars=merk_varf;
  1187. /*            v->fi->vars=first_var[1];*/
  1188.             /*  keine echten Parameter=>keine negativen Offsets */
  1189. /*            vp=first_var[1];*/
  1190.       vp=merk_varf;
  1191.       while(vp){
  1192.         if(vp->storage_class==AUTO||vp->storage_class==REGISTER){
  1193.           if(!zlleq(l2zl(0L),vp->offset)){
  1194.         vp->offset=l2zl(0L);
  1195.         if(DEBUG&1024) printf("converted parameter <%s>(%ld) for inlining\n",vp->identifier,(long)zl2l(vp->offset));
  1196.           }else vp->offset=l2zl(4L);  /*  Dummy, da recalc_offsets?   */
  1197.         }
  1198.         vp=vp->next;
  1199.       }
  1200.         }else{
  1201.       leave_block();
  1202.         }
  1203.         if(only_inline==2) only_inline=0;
  1204.         cur_func="oops, I forgot it";
  1205.   }else{
  1206.     if(makeint) error(125);
  1207.     if(*s==';') s++; else error(54);
  1208.     if((t->flags&NQ)==FUNKT&&t->exact){
  1209.       struct struct_declaration *sd=t->exact;int i,f;
  1210.       for(f=0,i=0;i<sd->count;i++)
  1211.     if(!(*sd->sl)[i].styp){error(126);f=1;}
  1212.       if(f){
  1213.     for(i=0;i<sd->count;i++) if((*sd->sl)[i].styp) freetyp((*sd->sl)[i].styp);
  1214.     sd->count=0;
  1215.       }
  1216.     }
  1217.   }
  1218.   if(old) freetyp(old);
  1219. }
  1220. int compare_pointers(struct Typ *a,struct Typ *b,int qual)
  1221. /*  Vergleicht, ob Typ beider Typen gleich ist, const/volatile      */
  1222. /*  werden laut ANSI nicht beruecksichtigt.                         */
  1223. {
  1224.   struct struct_declaration *sd;
  1225.   int af=a->flags&qual,bf=b->flags&qual;
  1226.   if(af!=bf) return(0);
  1227.   af&=NQ;bf&=NQ;
  1228.   if(af==FUNKT){
  1229.     if(a->exact->count&&!compare_sd(a->exact,b->exact)) return(0);
  1230.   }
  1231.   if(af==STRUCT||af==UNION){
  1232.     if(a->exact!=b->exact) return(0);
  1233.   }
  1234.   if(af==ARRAY){
  1235.     if(!zleqto(a->size,l2zl(0L))&&!zleqto(b->size,l2zl(0L))&&!zleqto(a->size,b->size)) return(0);
  1236.   }
  1237.   if(a->next==0&&b->next!=0) return(0);
  1238.   if(a->next!=0&&b->next==0) return(0);
  1239.   if(a->next==0&&b->next==0) return(1);
  1240.   return(compare_pointers(a->next,b->next,qual));
  1241. }
  1242. int compare_sd(struct struct_declaration *a,struct struct_declaration *b)
  1243. /*  Vergleicht, ob zwei struct_declarations identisch sind          */
  1244. /*  Wird nur nur fuer Prototypen benutzt, leere Liste immer gleich. */
  1245. {
  1246.   int i;
  1247.   if(!a->count||!b->count) return(1);
  1248.   if(a->count!=b->count) return(0);
  1249.   for(i=0;i<a->count;i++)
  1250.     if((*a->sl)[i].styp&&(*b->sl)[i].styp&&!compare_pointers((*a->sl)[i].styp,(*b->sl)[i].styp,255)) return(0);
  1251.   return(1);
  1252. }
  1253. void free_clist(struct const_list *p)
  1254. /*  Gibt clist frei.                                        */
  1255. {
  1256.   struct const_list *merk;
  1257.   return;
  1258.   while(p){
  1259.     merk=p->next;
  1260.     if(p->other) free_clist(p->other);
  1261.     if(p->tree) free_expression(p->tree);
  1262.     free(p);
  1263.     p=merk;
  1264.   }
  1265. }
  1266. void gen_clist(FILE *,struct Typ *,struct const_list *);
  1267.  
  1268. void gen_vars(struct Var *v)
  1269. /*  Generiert Variablen.                                    */
  1270. {
  1271.   int mode,al;struct Var *p;
  1272.   if(errors!=0||(c_flags[5]&USEDFLAG)) return;
  1273.   for(mode=0;mode<3;mode++){
  1274.     int i,flag;
  1275.     for(p=v;p;p=p->next){
  1276.       if(DEBUG&2) printf("gen_var(): %s\n",p->identifier);
  1277.       if(p->storage_class==STATIC||p->storage_class==EXTERN){
  1278.     if(!(p->flags&GENERATED)){
  1279.       if(p->storage_class==EXTERN&&!(p->flags&(USEDASSOURCE|USEDASDEST))&&!(p->flags&(TENTATIVE|DEFINED))) continue;
  1280.       /*  erst konstante initialisierte Daten */
  1281.       if(mode==0){
  1282.         if(!p->clist) continue;
  1283.         if(!(p->vtyp->flags&(CONST|STRINGCONST))){
  1284.           struct Typ *t=p->vtyp;int f=0;
  1285.           do{
  1286.         if(t->flags&(CONST|STRINGCONST)) break;
  1287.         if((t->flags&NQ)!=ARRAY){f=1;break;}
  1288.         t=t->next;
  1289.           }while(1);
  1290.           if(f) continue;
  1291.         }
  1292.       }
  1293.       /*  dann initiolisierte */
  1294.       if(mode==1&&!p->clist) continue;
  1295.       /*  und dann der Rest   */
  1296.       if(mode==2&&p->clist) continue;
  1297.       if(!(p->flags&(TENTATIVE|DEFINED))){
  1298.         gen_var_head(out,p);
  1299.         if(p->storage_class==STATIC) error(127,p->identifier);
  1300.         continue;
  1301.       }else{
  1302.         gen_align(out,falign(p->vtyp));
  1303.       }
  1304.       gen_var_head(out,p);
  1305.       if(!p->clist){
  1306.         if(type_uncomplete(p->vtyp)) error(202,p->identifier);
  1307.         gen_ds(out,szof(p->vtyp),p->vtyp);
  1308.       }else{
  1309.         gen_clist(out,p->vtyp,p->clist);
  1310.       }
  1311.       p->flags|=GENERATED;
  1312.     }
  1313.       }
  1314.     }   
  1315.   }
  1316. }
  1317. void gen_clist(FILE *f,struct Typ *t,struct const_list *cl)
  1318. /*  Generiert dc fuer const_list.                           */
  1319. {
  1320.   int i;zlong sz;
  1321.   if((t->flags&NQ)==ARRAY){
  1322.     for(sz=l2zl(0L);!zlleq(t->size,sz)&&cl;sz=zladd(sz,l2zl(1L)),cl=cl->next){
  1323.       if(!cl->other){ierror(0);return;}
  1324.       gen_clist(f,t->next,cl->other);
  1325.     }
  1326.     if(!zlleq(t->size,sz)) gen_ds(f,zlmult(zlsub(t->size,sz),szof(t->next)),t->next);
  1327.     return;
  1328.   }
  1329.   if((t->flags&NQ)==UNION){
  1330.     gen_clist(f,(*t->exact->sl)[0].styp,cl);
  1331.     sz=zlsub(szof(t),szof((*t->exact->sl)[0].styp));
  1332.     if(!zleqto(sz,l2zl(0L))) gen_ds(f,sz,0);
  1333.     return;
  1334.   }
  1335.   if((t->flags&NQ)==STRUCT){
  1336.     zlong al;int fl;struct Typ *st;
  1337.     sz=l2zl(0L);
  1338.     for(i=0;i<t->exact->count&&cl;i++){
  1339.       if(!cl->other){ierror(0);return;}
  1340.       st=(*t->exact->sl)[i].styp;
  1341.       al=falign(st);
  1342.       if(!zleqto(zlmod(sz,al),l2zl(0L))){
  1343.     gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1344.     sz=zladd(sz,zlsub(al,zlmod(sz,al)));
  1345.       }
  1346.       if(!(*t->exact->sl)[i].identifier) ierror(0);
  1347.       if((*t->exact->sl)[i].identifier[0]){
  1348.     gen_clist(f,st,cl->other);
  1349.     cl=cl->next;
  1350.       }else{
  1351.     gen_ds(f,szof(st),0); /* sollte unnamed bitfield sein */
  1352.       }
  1353.       sz=zladd(sz,szof(st));
  1354.     }
  1355.     for(;i<t->exact->count;i++){
  1356.       st=(*t->exact->sl)[i].styp;
  1357.       al=falign(st);
  1358.       if(!zleqto(zlmod(sz,al),l2zl(0L))){
  1359.     gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1360.     sz+=zladd(sz,zlsub(al,zlmod(sz,al)));
  1361.       }
  1362.       gen_ds(f,szof((*t->exact->sl)[i].styp),(*t->exact->sl)[i].styp);
  1363.       sz=zladd(sz,szof(st));
  1364.     }
  1365.     al=falign(t);
  1366.     if(!zleqto(zlmod(sz,al),l2zl(0L)))
  1367.       gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1368.     return;
  1369.   }
  1370.   if(cl->tree) cl->tree->o.am=0;
  1371.   gen_dc(f,t->flags&NU,cl);
  1372. }
  1373. struct const_list *initialization(struct Typ *t,int noconst,int level)
  1374. /*  Traegt eine Initialisierung in eine const_list ein.         */
  1375. {
  1376.   struct const_list *first,*cl,**prev;np tree,tree2;int bracket;zlong i;
  1377.   int f=t->flags&NQ;
  1378.   if(f==FUNKT){error(42);return(0);}
  1379.   if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1380.   if(f==ARRAY){
  1381.     if(*s=='\"'&&t->next&&(t->next->flags&NQ)==CHAR){
  1382.       killsp();
  1383.       tree=string_expression();
  1384.       first=tree->cl;
  1385.       free_expression(tree);
  1386.     }else{
  1387.       prev=0;
  1388.       if(level==0&&!bracket) error(157);
  1389.       for(i=l2zl(0L);(zleqto(t->size,l2zl(0L))||!zlleq(t->size,i))&&*s!='}';i=zladd(i,l2zl(1L))){
  1390.     if(!zlleq(i,0)){
  1391.       if(*s==','){s++;killsp();} else break;
  1392.       if(*s=='}') break;
  1393.     }
  1394.     cl=mymalloc(CLS);
  1395.     cl->next=0;cl->tree=0;
  1396.     cl->other=initialization(t->next,0,level+1);
  1397.     killsp();
  1398.     if(prev) *prev=cl; else first=cl;
  1399.     prev=&cl->next;
  1400.       }
  1401.     }
  1402.   }else if(f==STRUCT&&(bracket||!noconst)){
  1403.     if(t->exact->count<=0)
  1404.       {error(43);return(0);}
  1405.     prev=0;
  1406.     if(level==0&&!bracket) error(157);
  1407.     for(i=l2zl(0L);!zlleq(t->exact->count,i)&&*s!='}';i=zladd(i,l2zl(1L))){
  1408.       if((*t->exact->sl)[zl2l(i)].identifier[0]==0) {continue;} /* unnamed bitfield */
  1409.       if(!zlleq(i,0)){
  1410.     if(*s==','){s++;killsp();} else break;
  1411.     if(*s=='}') break;
  1412.       }
  1413.       cl=mymalloc(CLS);
  1414.       cl->next=0;cl->tree=0;
  1415.       cl->other=initialization((*t->exact->sl)[zl2l(i)].styp,0,level+1);
  1416.       if(prev) *prev=cl; else first=cl;
  1417.       prev=&cl->next;
  1418.     }
  1419.   }else if(f==UNION&&(bracket||!noconst)){
  1420.     if(t->exact->count<=0)
  1421.       {error(44);return(0);}
  1422.     first=initialization((*t->exact->sl)[0].styp,0,level+1);
  1423.   }else{
  1424.     tree2=tree=assignment_expression();
  1425.     if(!tree){error(45);return(0);}
  1426.     if(!type_expression(tree)){free_expression(tree); return(0);}
  1427.     tree=makepointer(tree);
  1428.     test_assignment(t,tree);
  1429.     if(!noconst){
  1430.       /*  nur Konstanten erlaubt (bei Arrays/Strukturen etc. oder static) */
  1431.       if(tree->flags!=CEXPR){
  1432.     while(tree->flags==CAST) tree=tree->left;
  1433.     if(tree->flags==ADDRESS||tree->flags==ADDRESSS||tree->flags==ADDRESSA){
  1434.       gen_IC(tree,0,0);
  1435.       if(!(tree->o.flags&VARADR)){
  1436.         /*  hier fehlen noch viele Pruefungen   */
  1437.         free_expression(tree);error(46);
  1438.         return(0);
  1439.       }
  1440.       first=mymalloc(CLS);
  1441.       first->next=first->other=0;
  1442.       first->tree=tree;
  1443.       killsp();
  1444.     }else{
  1445.       free_expression(tree);error(46);
  1446.       return(0);
  1447.     }
  1448.       }else{
  1449.     first=mymalloc(CLS);
  1450.     first->next=first->other=0;
  1451.     first->tree=0;
  1452.     eval_constn(tree);
  1453.     tree->ntyp->flags=t->flags;
  1454.     insert_const(tree);
  1455.     first->val=tree->val;
  1456.     free_expression(tree2);
  1457.     killsp();
  1458.       }
  1459.     }else{
  1460.       /*  auch anderes erlaubt    */
  1461.       first=mymalloc(CLS);
  1462.       first->next=first->other=0;
  1463.       first->tree=tree;
  1464.       killsp();
  1465.     }
  1466.   }
  1467.   if(bracket){
  1468.     if(*s==','){s++;killsp();}
  1469.     if(*s=='}'){s++;killsp();} else error(128);
  1470.   }
  1471.   return(first);
  1472. }
  1473.  
  1474.  
  1475.  
  1476.