home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / c-tools / vbcc / declaration.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-11  |  51.4 KB  |  1,379 lines

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