home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / CCDL122.ZIP / SOURCE / DECL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-06  |  35.9 KB  |  1,106 lines

  1. /*
  2.  * 68K/386 32-bit C compiler.
  3.  *
  4.  * copyright (c) 1996, David Lindauer
  5.  * 
  6.  * This compiler is intended for educational use.  It may not be used
  7.  * for profit without the express written consent of the author.
  8.  *
  9.  * It may be freely redistributed, as long as this notice remains intact
  10.  * and sources are distributed along with any executables derived from them.
  11.  *
  12.  * The author is not responsible for damages, either direct or consequential,
  13.  * that may arise from use of this software.
  14.  *
  15.  * v1.5 August 1996
  16.  * David Lindauer, gclind01@starbase.spd.louisville.edu
  17.  *
  18.  * Credits to Mathew Brandt for original K&R C compiler
  19.  *
  20.  */
  21. #include        <stdio.h>
  22. #include        "expr.h"
  23. #include        "c.h"
  24. #include        "gen.h"
  25. #include        "cglbdec.h"
  26.  
  27. extern    SNODE *cbautoinithead;
  28. extern int prm_bss;
  29. extern int prm_packing;
  30. extern int prm_revbits;
  31. extern int prm_68020;
  32. extern HASHREC **globalhash;
  33. extern SYM *currentfunc;
  34. extern int prm_cplusplus;
  35. extern char * tn_unnamed;
  36.  
  37. char *Cstr = "C";
  38.  
  39. int mangleflag;
  40. int skm_declopenpa[] = { semicolon, comma,openpa, eq,0 };
  41. int skm_declclosepa[] = { semicolon, comma,closepa, eq,0 };
  42. int skm_declclosebr[] = { semicolon, comma,closebr, eq,0 };
  43. int skm_declend[] = { semicolon, comma, end, eq,0 };
  44. int skm_decl[] = {semicolon,kw_int, kw_long, kw_short, kw_char, kw_float,
  45.         kw_double, kw_struct, kw_union, kw_enum, kw_unsigned, kw_signed, kw_auto,
  46.         kw_extern, kw_static, kw_register, kw_typedef, id, kw_void, 0 };
  47.  
  48. TYP             *head = 0;
  49. TYP             *tail = 0;
  50. char            declid[100];
  51. TABLE           tagtable = {0,0};
  52. TYP             stdconst = { bt_long, 1, 0, 0, 0, -1, -1, 4, {0, 0}, 0, "stdconst",0};
  53.  
  54. static                    SYM *lastdecl;
  55. static                     int pcount=0;
  56. static int bittype = -1;
  57. static int curbit = 0;
  58. static int curofs = 0;
  59. static int manglelevel;
  60.  
  61. void declini(void)
  62. {
  63.     bittype = -1;
  64.     head = tail = 0;
  65.     declid[0] = 0;
  66.     tagtable.head = tagtable.tail = 0;
  67.     pcount = 0;
  68.     mangleflag = TRUE;
  69. }
  70. int     imax(int i,int j)
  71. {       return (i > j) ? i : j;
  72. }
  73.  
  74. char    *litlate(char *s)
  75. {       char    *p;
  76.         p = xalloc(strlen(s) + 1);
  77.         strcpy(p,s);
  78.         return p;
  79. }
  80.  
  81. TYP     *maketype(int bt,int siz)
  82. {       TYP     *tp;
  83.                 
  84.         tp = xalloc(sizeof(TYP));
  85.         tp->val_flag = 0;
  86.                 tp->bits = tp->startbit = -1;
  87.         tp->size = siz;
  88.         tp->type = bt;
  89.         tp->sname = 0;
  90.                 tp->cflags = 0;
  91.                 tp->uflags = 0;
  92.         tp->lst.head = tp->lst.tail = 0;
  93.                 tp->tdef = 0;
  94.         return tp;
  95. }
  96. TYP *cponetype(TYP *t)
  97. {                TYP *tp;
  98.     tp = xalloc(sizeof(TYP));
  99.     tp->type = t->type;
  100.     tp->val_flag = t->val_flag;
  101.     tp->cflags = t->cflags;
  102.     tp->bits = t->bits;
  103.     tp->startbit = t->startbit;
  104.     tp->size = t->size;
  105.     tp->lst = t->lst;
  106.     tp->btp = t->btp;
  107.     tp->sname = t->sname;
  108.     tp->uflags = t->uflags;
  109.     tp->tdef = t->tdef;
  110.     return tp;
  111. }
  112. TYP *copytype(TYP *itp, int flags)
  113. {
  114.     TYP *head, *tail;
  115.     head = tail = cponetype(itp);
  116.     while (itp->type == bt_pointer) {
  117.         tail = tail->btp = cponetype(itp->btp);
  118.         itp = itp->btp;
  119.     }
  120.     if (itp->type == bt_func || itp->type == bt_ptrfunc || itp->type == bt_ifunc) {
  121.             tail->btp = cponetype(itp);
  122.     }
  123.     else {
  124.         tail->cflags |= flags;
  125.     }
  126.     return head;
  127. }
  128. void     decl(TABLE *table,int flags)
  129. {
  130.                 SYM *sp;
  131.         switch (lastst) {
  132.                                 case kw_void:
  133.                         head = tail = maketype(bt_void,0);
  134.                                                 getsym();
  135.                                                 break;
  136.                 case kw_char:
  137.                         head = tail = maketype(bt_char,1);
  138.                         getsym();
  139.                         break;
  140.                 case kw_short:
  141.                         head = tail = maketype(bt_short,2);
  142.                         getsym();
  143.                         break;
  144.                 case kw_int: 
  145.                         head = tail = maketype(bt_long,4);
  146.                         getsym();
  147.                         break;
  148.                 case kw_unsigned:
  149.                                                 getsym();
  150.                                                 switch (lastst) {
  151.                                                     case kw_char:
  152.                                                         getsym();
  153.                                 head = tail = maketype(bt_unsignedchar,1);
  154.                                                         break;
  155.                                                     case kw_short:
  156.                                                         getsym();
  157.                                 head = tail = maketype(bt_unsignedshort,2);
  158.                                                         break;
  159.                                                     case kw_int:
  160.                                                     case kw_long:
  161.                                                         getsym();
  162.                                                     default:
  163.                                 head = tail = maketype(bt_unsigned,4);
  164.                                                         break;
  165.                                                 }
  166.                         break;
  167.                 case kw_signed:
  168.                                                 getsym();
  169.                                                 switch (lastst) {
  170.                                                     case kw_char:
  171.                                                         getsym();
  172.                                 head = tail = maketype(bt_char,1);
  173.                                                         break;
  174.                                                     case kw_short:
  175.                                                         getsym();
  176.                                 head = tail = maketype(bt_short,2);
  177.                                                         break;
  178.                                                     case kw_int:
  179.                                                     case kw_long:
  180.                                                         getsym();
  181.                                                     default:
  182.                                 head = tail = maketype(bt_long,4);
  183.                                                         break;
  184.                                                 }
  185.                         break;
  186.                 case id:                /* no type declarator */
  187.                                                 if ((sp = gsearch(lastid)) != 0 && sp->storage_class == sc_type) {
  188.                                                     head = tail = copytype(sp->tp,flags);
  189.                                                     getsym();
  190.                                                 }
  191.                                                 else
  192.                                head = tail = maketype(bt_long,4);
  193.                         break;
  194.                 case kw_float:
  195.                         head = tail = maketype(bt_float,4);
  196.                         getsym();
  197.                         break;
  198.                                 case kw_long:
  199.                                                 getsym();
  200.                                                 if (lastst!=kw_double) {
  201.                             head = tail = maketype(bt_long,4);
  202.                                                     break;
  203.                                                 }
  204.                         head = tail = maketype(bt_longdouble,12);
  205.                         getsym();
  206.                         break;
  207.                 case kw_double:
  208.                         head = tail = maketype(bt_double,8);
  209.                         getsym();
  210.                         break;
  211.                 case kw_enum:
  212.                         getsym();
  213.                         declenum(table);
  214.                         break;
  215.                 case kw_struct:
  216.                         getsym();
  217.                         declstruct(bt_struct,flags);
  218.                         break;
  219.                 case kw_union:
  220.                         getsym();
  221.                         declstruct(bt_union,flags);
  222.                         break;
  223.                 }
  224.     head->cflags |= flags;
  225.     head->uflags |= UF_CANASSIGN;
  226. }
  227.  
  228. void decl1(void)
  229. {       TYP     *temp1, *temp2, *temp3, *temp4;
  230. lp:
  231.         switch (lastst) {
  232.                                 case kw_const:
  233.                                     head->cflags |= DF_CONST;
  234.                                     getsym();
  235.                                     goto lp;
  236.                                 case kw_volatile:
  237.                                     head->cflags |= DF_VOL;
  238.                                     getsym();
  239.                                     goto lp;
  240.                 case id:
  241.                                                 strcpy(declid,lastid);
  242.                         getsym();
  243.                         decl2();
  244.                         break;
  245.                                 case and:
  246.                                                 getsym();
  247.                                                 if (prm_cplusplus) {
  248.                                                     decl1();
  249.                                                     if (head->type == bt_ref || head->type == bt_pointer)
  250.                                                         generror(ERR_CANTREF,0,0);
  251.                                                     else {
  252.                                                         temp1 = maketype(bt_ref,4);
  253.                                                         temp1->btp = head;
  254.                                                         head = temp1;
  255.                                                         if (tail == NULL)
  256.                                                             tail = head;
  257.                                                     }
  258.                                                 }
  259.                                                 else generror(ERR_NOREF,0,0);
  260.                                                 break;
  261.                 case star:
  262.                                                 head->uflags &= ~UF_CANASSIGN;
  263.                         temp1 = maketype(bt_pointer,4);
  264.                         temp1->btp = head;
  265.                         head = temp1;
  266.                                                 head->uflags |= UF_CANASSIGN;
  267.                         if(tail == NULL)
  268.                                 tail = head;
  269.                         getsym();
  270.                         decl1();
  271.                         break;
  272.                 case openpa:
  273.                         getsym();
  274.                           temp1 = head;
  275.                           temp2 = tail;
  276.                                                 if (lastst == star) {
  277.                                                     getsym();
  278.                                                      if (lastst != id && lastst != closepa)
  279.                                                         generror(ERR_IDEXPECT,0,skm_declclosepa);
  280.                                                     else {
  281.                                                         temp2 = head;
  282.                                   head = tail = maketype(bt_ptrfunc,4);
  283.                                   head->btp = temp2;
  284.                                                         if (lastst == id) {
  285.                                                             strcpy(declid,lastid);
  286.                                                             head->sname = declid;
  287.                                                             getsym();
  288.                                                             decl2();
  289.                                                         }
  290.                                                         if (needpunc(closepa,skm_declclosepa)) {
  291.                                                             char temp[40];
  292.                                                             strcpy(temp,declid);
  293.                                                             if (head->type == bt_func) {
  294.                                                                 declid[0] = 0;
  295.                                                                 temp1 = head;
  296.                                                                 head = head->btp;
  297.                                                             }
  298.                                                             else
  299.                                                                 temp1 = 0;
  300.                                                           if (needpunc(openpa,skm_declopenpa)) {
  301.                                                                 declfuncarg();
  302.                                                             }
  303.                                                             if (temp1) {
  304.                                                                 strcpy(declid,temp);
  305.                                                                 temp1->btp = head;
  306.                                                                 head = temp1;
  307.                                                             }
  308.                                                             if (lastst == begin) {
  309.                                                                 temp1->type = bt_ifunc;
  310.                                                                 break;
  311.                                                             }
  312.                                                         }
  313.                                                         else getsym();
  314.                                                     }
  315.                                                     
  316.                                                 }
  317.                                                 else {
  318.                               head = tail = NULL;
  319.                             decl1();
  320.                             needpunc(closepa,0);
  321.                             temp3 = head;
  322.                             temp4 = tail;
  323.                             head = temp1;
  324.                             tail = temp2;
  325.                             decl2();
  326.                             temp4->btp = head;
  327.                             if(temp4->type == bt_pointer &&
  328.                                     temp4->val_flag != 0 && head != NULL)
  329.                                     temp4->size *= head->size;
  330.                             head = temp3;
  331.                                                 }
  332.                         break;
  333.                 default:
  334.                         decl2();
  335.                         break;
  336.                 }
  337. }
  338.  
  339. void decl2(void)
  340. {       TYP     *temp1;
  341. lp:
  342.         switch (lastst) {
  343.                                 case kw_const:
  344.                                     head->cflags |= DF_CONST;
  345.                                     getsym();
  346.                                     goto lp;
  347.                                 case kw_volatile:
  348.                                     head->cflags |= DF_VOL;
  349.                                     getsym();
  350.                                     goto lp;
  351.                 case openbr:
  352.                                                 decl3();
  353.                                                 decl2();
  354.                         break;
  355.                 case openpa:
  356.                         getsym();
  357.                           temp1 = maketype(bt_func,0);
  358.                                                 head->uflags |= UF_DEFINED;
  359.                           temp1->val_flag = 1;
  360.                           temp1->btp = head;
  361.                           head = temp1;
  362.                                                 head->uflags |= UF_DEFINED;
  363.                                                 declfuncarg();
  364.                                                 if (lastst == begin) {
  365.                                                     temp1->type = bt_ifunc;
  366.                                                     break;
  367.                                                 }
  368.                         break;
  369.                                 case colon:
  370.                                         getsym();
  371.                                         if (head->type != bt_long && head->type != bt_unsigned
  372.                                                 && head->type != bt_short && head->type != bt_unsignedshort
  373.                                                 && head->type != bt_char && head->type != bt_unsignedchar)
  374.                                             generror(ERR_BFTYPE,0,0);
  375.                                         head->bits = intexpr(0);
  376.                                         break;
  377.                 }
  378. }
  379.  
  380. void decl3(void)
  381. {
  382.   TYP *temp1, *list[40];
  383.     int count = 0,i;
  384.     head->uflags |= UF_DEFINED;
  385.     head->uflags &= ~UF_CANASSIGN;
  386.     while(lastst == openbr) {
  387.                         getsym();
  388.                         temp1 = maketype(bt_pointer,0);  
  389.                         temp1->val_flag = 1;
  390.                                                 temp1->cflags = head->cflags &~DF_AUTOREG;
  391.                         if(lastst == closebr) {
  392.                                 temp1->size = 0;
  393.                                 getsym();
  394.                                 }
  395.                         else {
  396.                                 temp1->size = intexpr(0);
  397.                                 needpunc(closebr,skm_declclosebr);
  398.                                 }
  399.                                                 list[count++] = temp1;
  400.     }
  401.     if (head != NULL) {
  402.         list[count-1]->size *= head->size;
  403.         if (tail == NULL)
  404.             tail = head;
  405.     }
  406.     for (i=count-1; i>0; i--) {
  407.         list[i-1]->size *= list[i]->size;
  408.         list[count-1]->uflags |= UF_DEFINED;
  409.     }
  410.  
  411.     for (i=0; i < count-1; i++)
  412.         list[i]->btp = list[i+1];
  413.     list[count-1]->btp = head;
  414.  
  415.     head = list[0];
  416.     if (tail == NULL)
  417.         tail = list[count-1];
  418. }
  419. int     alignment(TYP *tp)
  420. {       switch(tp->type) {
  421.                 case bt_char: case bt_unsignedchar:  return AL_CHAR;
  422.                 case bt_short: case bt_unsignedshort: return AL_SHORT;
  423.                 case bt_long: case bt_unsigned: return AL_LONG;
  424.                 case bt_enum:           return AL_SHORT;
  425.                 case bt_pointer:
  426.                                 case bt_matchall:
  427.                         if(tp->val_flag)
  428.                                 return alignment(tp->btp);
  429.                         else
  430.                                 return AL_POINTER;
  431.                 case bt_float:          return AL_FLOAT;
  432.                 case bt_double:         return AL_DOUBLE;
  433.                                 case bt_longdouble:                return AL_LONGDOUBLE;
  434.                 case bt_struct:
  435.                 case bt_union:          return AL_STRUCT;
  436.                 default:                return AL_CHAR;
  437.                 }
  438. }
  439. int bitsize(int type)
  440. {
  441.     switch (type) {
  442.         case bt_char:
  443.         case bt_unsignedchar:
  444.             return 8;
  445.         case bt_short:
  446.         case bt_unsignedshort:
  447.             return 16;
  448.         case bt_long:
  449.         case bt_unsigned:
  450.             return 32;
  451.     }
  452.     return 0;
  453. }
  454. int     basedeclare(TABLE *table,int al,long ilc,int ztype, int flags)
  455. /*
  456.  *      process declarations of the form:
  457.  *
  458.  *              <type>  <decl>, <decl>...;
  459.  *
  460.  *      leaves the declarations in the symbol table pointed to by
  461.  *      table and returns the number of bytes declared. al is the
  462.  *      allocation type to assign, ilc is the initial location
  463.  *      counter. if al is sc_member then no initialization will
  464.  *      be processed. ztype should be bt_struct for normal and in
  465.  *      structure declarations and sc_union for in union declarations.
  466.  */
  467. {       SYM     *sp=0, *sp1;
  468.                 long *ilcp;
  469.         TYP     *dhead;
  470.         int     nbytes,*nbp,bssbytes,ufsave;
  471.         nbytes = 0;
  472.         dhead = head;
  473.                 ufsave = head->uflags;
  474.                 bssbytes = 0;
  475.         for(;;) {
  476. #ifdef i386
  477.                                 if (al == sc_abs)
  478.                                     al = sc_global;
  479. #endif
  480.                                 sp1 = 0;
  481.                 declid[0] = 0;
  482.                 decl1();
  483.                                 if (declid[0] == 0) {
  484.                                     if ((flags & DF_FUNCPARMS)) {
  485.                                         sprintf(declid,"**ARG%d**",pcount++);
  486.                                     }
  487.                                 }
  488.                 if( declid[0] != 0) {      /* otherwise just struct tag... */
  489.                     sp = xalloc(sizeof(SYM));
  490.                                         if (head->type == bt_func || head->type == bt_ifunc)
  491.                                             if (prm_cplusplus)
  492.                                                 if (!strcmp(declid,"_main")) {
  493.                                                     SYM *sp1;
  494.                                                     head->sname = sp->name = litlate(declid);
  495.                                                     sp1 = search(sp->name,&gsyms);
  496.                                                     if (sp1 && sp1->tp->type == bt_ifunc)
  497.                                                         generror(ERR_NOOVERMAIN,0,0);
  498.                                                 }
  499.                                                 else if (mangleflag)
  500.                                                     head->sname = sp->name = cppmangle(declid,head);
  501.                                                 else
  502.                                                     head->sname = sp->name = litlate(declid);
  503.                                             else 
  504.                                                 head->sname = sp->name = litlate(declid);
  505.                                         else
  506.                                             sp->name = litlate(declid);
  507.                                         sp->defalt = 0;
  508.                     sp->storage_class = al;
  509.                     sp->tp = head;
  510.                                         sp->extflag = FALSE;
  511.                                         sp->absflag = (al == sc_abs);
  512.                                         sp->intflag = flags &DF_INT;
  513.                                         if (al == sc_autoreg) {
  514.                                             if (head->size > 4) {
  515.                                                 sp->storage_class = sc_auto;
  516.                                                 gensymerror(ERR_ILLREGISTER,sp->name);
  517.                                             }
  518.                                             head->sname = sp->name;
  519.                                         }    
  520.                                         if (al != sc_type) {
  521.                                             if (!(flags & DF_GLOBAL) || sp->tp->type == bt_func || sp->tp->type == bt_ifunc) {
  522.                                                 nbp = &nbytes;
  523.                                                 ilcp = &ilc;
  524.                                             }
  525.                                             else {
  526.                                                 if (lastst != assign && prm_bss) {
  527.                                                     nbp = &bssbytes;
  528.                                                     ilcp = &lc_bss;
  529.                                                     bssseg();
  530.                                                 }
  531.                                                 else {
  532.                                                     nbp = &nbytes;
  533.                                                     ilcp = &ilc;
  534.                                                     dseg();
  535.                                                 }
  536.                                                 nl();
  537.                                             }
  538.                                             sp->addr = *ilcp;
  539.                                             if (al != sc_member || head->bits == -1 || bittype != head->type || head->bits +curbit > bitsize(head->type)) {
  540.                                                 int align=alignment(head);
  541.                                                 if (al != sc_auto) {
  542. #ifdef i386
  543.                                                     if (prm_packing)
  544.                                                         align = 1;
  545. #else
  546.                                                     if (prm_packing) 
  547.                                                         if (!prm_68020)
  548.                                                             align = 1;
  549.                                                         else
  550.                                                             if (align > 2) align = 2;
  551. #endif
  552.                                                 }
  553.                                                 bittype = head->type;
  554.                                                 curbit = 0;
  555.                            while( (*ilcp + *nbp) % align) {
  556.                                 if( al != sc_member &&
  557.                                         al != sc_external &&
  558.                                         al != sc_auto && al != sc_autoreg 
  559.                                                                                 && al != sc_memberreg && al != sc_abs) {
  560.                                         genbyte(0);
  561.                                                                                 nl();
  562.                                         }
  563.                                 ++(*nbp);
  564.                                 }
  565.                                                 curofs = *ilcp+*nbp;
  566.                         if( al != sc_static)
  567.                             if( ztype == bt_union)
  568.                                 sp->value.i = *ilcp;
  569.                             else if( al != sc_auto && al != sc_autoreg)
  570.                                 sp->value.i = *ilcp + *nbp;
  571.                             else
  572.                                 sp->value.i = -(*ilcp + *nbp + head->size);
  573.                         if(ztype == bt_union)
  574.                               *nbp = imax(*nbp,sp->tp->size);
  575.                         else if(al != sc_external)
  576.                               *nbp += sp->tp->size;
  577.                                             }
  578.                                             if (head->bits != -1) {
  579.                                                 if (al != sc_member || head->bits +curbit > bitsize(head->type)) {
  580.                                                     generror(ERR_BFILLEGAL,0,0);
  581.                                                     head->bits = -1;
  582.                                                 }
  583.                                                 else {
  584.                                                     sp->value.i = curofs;
  585.                                                     if (prm_revbits)
  586.                                                         head->startbit = head->size*8-curbit-head->bits;
  587.                                                     else
  588.                                                         head->startbit = curbit;
  589.                                                     curbit+=head->bits;
  590.                                                 }
  591.                                             }
  592.                                             if (sp->absflag &&( sp->tp->type == bt_func || sp->tp->type == bt_ifunc))
  593.                                                 gensymerror(ERR_ILLCLASS,lastid);
  594.                                             if (sp->intflag &&( sp->tp->type != bt_func && sp->tp->type != bt_ifunc))
  595.                                                 gensymerror(ERR_ILLCLASS,lastid);
  596.                       if( sp->tp->type == bt_func && 
  597.                             sp->storage_class == sc_global || sp->storage_class == sc_member)
  598. #ifndef i386          
  599.                             sp->storage_class = sc_external;
  600. #else                 
  601.                             sp->storage_class = sc_externalfunc;
  602. #endif
  603.                                             
  604.                                           sp1 = search(sp->name,table);
  605.                                             if (sp->name[0] == '@' && !sp1)
  606.                                                 funcrefinsert(declid,sp->name,head,table);
  607.                          if (!sp1 || (sp1->tp->type != bt_func && sp1->tp->type != bt_ifunc)) {
  608.                                                 if (!sp1 || (al != sc_external && sp1->storage_class != sc_externalfunc&& sp1->storage_class != sc_external) 
  609.                                                     || ((al==sc_external || sp1->storage_class == sc_externalfunc || sp1->storage_class == sc_external) && !checktype(sp->tp,sp1->tp)))
  610.                              insert(sp,table);                                                                       
  611.                                                 else
  612.                                                     if (sp1->storage_class == sc_external || sp1->storage_class == sc_externalfunc) {
  613.                                                         sp1->storage_class = sp->storage_class;
  614.                                                         sp1->tp = sp->tp;
  615.                                                         sp1->value.i = sp->value.i;
  616.                                                     }
  617.                                             }
  618.                                             else 
  619. join:
  620.                                             if (sp1->tp->type == bt_func && sp->tp->type == bt_ifunc){
  621.                                                 if (sp1->storage_class == sc_external || sp1->storage_class == sc_externalfunc)
  622.                                                     sp1->storage_class = sc_global;
  623.                                             }
  624.                          if( sp->tp->type == bt_ifunc) { /* function body follows */
  625.                                                          sp1->tp->type = bt_ifunc;
  626.                              funcbody(sp);
  627.                                                          lc_bss += bssbytes;
  628.                                                          lastdecl = sp1;
  629.                              return nbytes;
  630.                              }
  631.                          if( (al == sc_global || al == sc_static) &&
  632.                                sp->tp->type != bt_func && sp->tp->type != bt_ifunc
  633.                                                         && !(flags & DF_FUNCPARMS)) {
  634.                                                     if (sp->tp->type == bt_ref && lastst != assign)
  635.                                                         gensymerror(ERR_REFMUSTINIT,lastid);
  636.                           doinit(sp);
  637.                                             }
  638.                                             else 
  639.                                                 if (al == sc_auto || al == sc_autoreg) {
  640.                                                     if (prm_cplusplus &&(flags & DF_FUNCPARMS)) {
  641.                                                         dodefaultinit(sp);
  642.                                                     }
  643.                                                     else{
  644.                                                         if (sp->tp->type == bt_ref && lastst != assign)
  645.                                                             gensymerror(ERR_REFMUSTINIT,lastid);
  646.                                                     }
  647.                                                      doautoinit(sp);
  648.                                                 }
  649.                                         }
  650.                                         else if (sp->tp->type == bt_func || sp->tp->type == bt_ifunc) {
  651.                                                         gensymerror(ERR_ILLTYPE,declid);
  652.                                                         expskim(skm_declclosepa);
  653.                                                     }
  654.                                                     else {
  655.                                                         sp->tp = copytype(sp->tp,0);
  656.                                                         sp->tp->sname = sp->name;
  657.                             insert(sp,table);
  658.                                                     }
  659.                 }
  660.                                 if (!(flags & DF_FUNCPARMS)) {
  661.                     if(lastst == semicolon)
  662.                         break;
  663.                                     dhead = copytype(dhead,0);
  664.                     needpunc(comma,0);
  665.                                 }
  666.                                 else {
  667.                     if(lastst == comma)
  668.                                         break;
  669.                                     lc_bss += bssbytes;
  670.                                     if (sp1)
  671.                                         lastdecl = sp1;
  672.                                     else
  673.                                         lastdecl = sp;
  674.                   return(nbytes);
  675.                                 }
  676.                 if(declbegin(lastst) == 0)
  677.                         break;
  678.                 head = dhead;
  679.                                 head->uflags = ufsave;
  680.                 }
  681.         getsym();
  682.                 lc_bss += bssbytes;
  683.                 if (sp1)
  684.                     lastdecl = sp1;
  685.                 else
  686.                     lastdecl = sp;
  687.         return nbytes;
  688. }
  689. int     declare(TABLE *table,int al,long ilc,int ztype, int flags)
  690. /*
  691.  *      process declarations of the form:
  692.  *
  693.  *              <type>  <decl>, <decl>...;
  694.  *
  695.  *      leaves the declarations in the symbol table pointed to by
  696.  *      table and returns the number of bytes declared. al is the
  697.  *      allocation type to assign, ilc is the initial location
  698.  *      counter. if al is sc_member then no initialization will
  699.  *      be processed. ztype should be bt_struct for normal and in
  700.  *      structure declarations and sc_union for in union declarations.
  701.  */
  702. {
  703.         decl(table,flags);
  704.                 return(basedeclare(table,al,ilc,ztype,flags));
  705. }
  706. int     declare2(SYM *sp, TABLE *table,int al,long ilc,int ztype, int flags)
  707. /*
  708.  *      process declarations of the form:
  709.  *
  710.  *              <type>  <decl>, <decl>...;
  711.  *
  712.  *      leaves the declarations in the symbol table pointed to by
  713.  *      table and returns the number of bytes declared. al is the
  714.  *      allocation type to assign, ilc is the initial location
  715.  *      counter. if al is sc_member then no initialization will
  716.  *      be processed. ztype should be bt_struct for normal and in
  717.  *      structure declarations and sc_union for in union declarations.
  718.  */
  719. {
  720.                 tail = head = sp->tp;
  721.                 if (tail)
  722.                     while (tail->btp)
  723.                         tail = tail->btp;
  724.                 getsym();
  725.                 return(basedeclare(table,al,ilc,ztype,flags));
  726. }
  727.  
  728. int     declbegin(int st)
  729. {       return st == star || st == id || st == openpa ||
  730.                 st == openbr;
  731. }
  732.  
  733. void declenum(TABLE *table)
  734. {       SYM     *sp;
  735.         TYP     *tp;
  736.                 char *nm;
  737.         if( lastst == id) {
  738.                 if((sp = search(nm = litlate(lastid),&tagtable)) == 0) {
  739.                         sp = xalloc(sizeof(SYM));
  740.                         sp->tp = xalloc(sizeof(TYP));
  741.                         sp->tp->type = bt_enum;
  742.                         sp->tp->size = 2;
  743.                         sp->tp->lst.head = sp->tp->btp = 0;
  744.                         sp->storage_class = sc_type;
  745.                         sp->name = nm;
  746.                         sp->tp->sname = sp->name;
  747.                                                 sp->tp->bits = sp->tp->startbit = -1;
  748.                         getsym();
  749.                         if( lastst != begin)
  750.                                 generror(ERR_PUNCT,begin,skm_declend);
  751.                         else    {
  752.                                 insert(sp,&tagtable);
  753.                                 getsym();
  754.                                 enumbody(table);
  755.                                 }
  756.                         }
  757.                 else
  758.                         getsym();
  759.                 head = sp->tp;
  760.                 }
  761.         else    {
  762.                 tp = xalloc(sizeof(TYP));
  763.                 tp->type = bt_enum;
  764.                 tp->lst.head = tp->btp = 0;
  765.                                 tp->size = 2;
  766.                                 tp->bits = tp->startbit = -1;
  767.                                 tp->sname = 0;
  768.                 if( lastst != begin)
  769.                         generror(ERR_PUNCT,begin,skm_declend);
  770.                 else    {
  771.                         getsym();
  772.                         enumbody(table);
  773.                         }
  774.                 head = tp;
  775.                 }
  776. }
  777.  
  778. void enumbody(TABLE *table)
  779. {       int     evalue;
  780.         SYM     *sp;
  781.         evalue = 0;
  782.         while(lastst == id) {
  783.                 sp = xalloc(sizeof(SYM));
  784.                 sp->value.i = evalue++;
  785.                 sp->name = litlate(lastid);
  786.                 sp->storage_class = sc_const;
  787.                 sp->tp = &stdconst;
  788.                 insert(sp,table);
  789.                 getsym();
  790.                                 if (lastst == assign) {
  791.                                         getsym();
  792.                                         evalue = sp->value.i = intexpr(0);
  793.                                         evalue++;
  794.                                 }
  795.                 if( lastst == comma)
  796.                         getsym();
  797.                 else if(lastst != end)
  798.                         break;
  799.                 }
  800.         needpunc(end,skm_declend);
  801. }
  802.  
  803. void declstruct(int ztype, int flags)
  804. /*
  805.  *      declare a structure or union type. ztype should be either
  806.  *      bt_struct or bt_union.
  807.  */
  808. {       SYM     *sp;
  809.         TYP     *tp;
  810.                 char *nm = litlate(lastid);
  811.         if(lastst == id) {
  812.                 if((sp = search(nm,&tagtable)) == 0) {
  813.                         sp = xalloc(sizeof(SYM));
  814.                         sp->name = nm;
  815.                         sp->tp = xalloc(sizeof(TYP));
  816.                         sp->tp->type = ztype;
  817.                         sp->tp->lst.head = 0;
  818.                         sp->storage_class = sc_type;
  819.                         sp->tp->sname = sp->name;
  820.                                                 sp->tp->cflags = flags;
  821.                                                 sp->tp->uflags = UF_DEFINED;
  822.                         getsym();
  823.                         if(lastst != begin)
  824.                                         generror(ERR_PUNCT,begin,skm_declend);
  825.                         else    {
  826.                                 insert(sp,&tagtable);
  827.                                 getsym();
  828.                                 structbody(sp->tp,sp->name,ztype);
  829.                                 }
  830.                         }
  831.                 else
  832.                         getsym();
  833.                                 if (flags & (DF_CONST | DF_VOL))
  834.                                     head = copytype(sp->tp,flags);
  835.                                 else
  836.                     head = sp->tp;
  837.                                 head->bits = head->startbit = -1;
  838.                 }
  839.         else    {
  840.                 tp = xalloc(sizeof(TYP));
  841.                 tp->type = ztype;
  842.                                 tp->cflags = flags;
  843.                                 tp->uflags = UF_DEFINED;
  844.                 tp->sname = 0;
  845.                 tp->lst.head = 0;
  846.                                 tp->bits = tp->startbit =-1;
  847.                 if( lastst != begin)
  848.                            generror(ERR_PUNCT,begin,skm_declend);
  849.                 else    {
  850.                         getsym();
  851.                         structbody(tp,tn_unnamed,ztype);
  852.                         }
  853.                 head = tp;
  854.                 }
  855. }
  856.  
  857. void structbody(TYP *tp,char  *name,int ztype)
  858. {       int     slc;
  859.         slc = 0;
  860.         tp->val_flag = 1;
  861.                 head->uflags &= ~UF_CANASSIGN;
  862.         while( lastst != end) {
  863.                                 int flags=0;
  864.                                 while (lastst == kw_const || lastst == kw_volatile) {
  865.                                     if (lastst == kw_const)
  866.                                         flags |= DF_CONST;
  867.                                     else
  868.                                         flags |= DF_VOL;
  869.                                     getsym();
  870.                                 }
  871.                 if(ztype == bt_struct)
  872.                         slc += declare(&tp->lst,sc_member,slc,ztype,flags);
  873.                 else
  874.                         slc = imax(slc,declare(&tp->lst,sc_member,0,ztype,flags));
  875.                                 if (tp->lst.tail->tp->type == bt_ref)
  876.                                     genclasserror(ERR_REFNOCONS,name,tp->lst.tail->name);
  877.                                 tp->lst.tail->tp->uflags |= UF_DEFINED;
  878.                 }
  879.                 if (!prm_packing)
  880.             tp->size = (slc+3)&0xfffffffcL;
  881.                 else
  882.                     tp->size = slc;
  883.         getsym();
  884. }
  885. void check_used(void)
  886. {
  887.                 int i;
  888.                 SYM *sp;
  889.                     for (i=0; i < HASHTABLESIZE; i++) {
  890.                         if ((sp=(SYM *) globalhash[i]) != 0) {
  891.                             while (sp) {
  892.                                 if (sp->storage_class == sc_static)
  893.                                     if (sp->tp->type == bt_func)
  894.                                         gensymerror(ERR_NOSTATICFUNC,sp->name);
  895.                                     else
  896.                                         if (!(sp->tp->uflags & UF_USED))
  897.                                             if (sp->tp->type == bt_ifunc || sp->tp->type == bt_func)
  898.                                                 gensymerror(ERR_FUNCUNUSED,sp->name);
  899.                                             else
  900.                                                 gensymerror(ERR_STATICSYMUNUSED,sp->name);
  901.                                 sp = sp->next;
  902.                             }
  903.                         }
  904.                     }
  905. }
  906. void compile(void)
  907. /*
  908.  *      main compiler routine. this routine parses all of the
  909.  *      declarations using declare which will call funcbody as
  910.  *      functions are encountered.
  911.  */
  912. {       while(lastst != eof) {
  913.                 dodecl(sc_global);
  914.                                 if (lastst != eof) {
  915.                                     generror(ERR_DECLEXPECT,0,0);
  916.                                     getsym();
  917.                                 }
  918.         }
  919.                 check_used();
  920.         dumplits();
  921. }
  922.  
  923. void dodecl(int defclass)
  924. {
  925.                 SYM *sp;
  926.                 int flags = 0;
  927.                 long val;
  928.                 char *nm;
  929.                 cbautoinithead = 0;
  930.     
  931.         for(;;) {
  932.             switch(lastst) {
  933.                                 case kw_typedef:
  934.                                                 getsym();
  935.                                                 if (defclass == sc_global)
  936.                                                     declare(&gsyms,sc_type,0,bt_struct, 0);
  937.                                                 else
  938.                                                     declare(&lsyms,sc_type,0,bt_struct, 0);
  939.                                                 break;
  940.                 case kw_register:
  941.                         if( defclass != sc_auto || flags & DF_VOL) {
  942.                           gensymerror(ERR_ILLCLASS,lastid);
  943.                             getsym();
  944.                                                 }
  945.                                                 else  {
  946.                             getsym();
  947.                                 lc_auto +=
  948.                                 declare(&lsyms,sc_autoreg,lc_auto,bt_struct,flags | DF_AUTOREG);
  949.                                                 };
  950.                                         break;
  951.                                 case kw_volatile:
  952.                                         flags |= DF_VOL;
  953.                                         getsym();
  954.                                         continue;
  955.                                 case kw_const:
  956.                                         flags |= DF_CONST;
  957.                                         getsym();
  958.                                         continue;
  959.                 case id:
  960.                                         if (defclass == sc_auto)
  961.                                                 if (!(((sp = search(nm = litlate(lastid),&gsyms)) != 0 && sp->storage_class == sc_type)
  962.                                                      || ((sp = search(nm,&lsyms)) != 0 && sp->storage_class == sc_type)))
  963.                                                     return;
  964.                 case kw_char: case kw_int: case kw_short: case kw_unsigned:
  965.                 case kw_long: case kw_struct: case kw_union: case kw_signed:
  966.                 case kw_enum: case kw_void:
  967.                 case kw_float: case kw_double:
  968.                     if( defclass == sc_global)
  969.                         lc_static +=
  970.                             declare(&gsyms,sc_global,lc_static,bt_struct,flags | DF_GLOBAL);
  971.                     else if( defclass == sc_auto)
  972.                         lc_auto +=
  973.                             declare(&lsyms,sc_auto,lc_auto,bt_struct,flags);
  974.                     else
  975.                         declare(&lsyms,sc_auto,0,bt_struct,flags);
  976.                     break;
  977.                 case kw_static:
  978.                         if( defclass == sc_member) {
  979.                            gensymerror(ERR_ILLCLASS,lastid);
  980.                                                         getsym();
  981.                                                         break;
  982.                                                 }
  983.                                                 getsym();
  984.                                                 if( defclass == sc_auto)
  985.                                                     lc_static += 
  986.                                                         declare(&lsyms,sc_static,lc_static,bt_struct,flags | DF_GLOBAL);
  987.                                                 else
  988.                                                     lc_static +=
  989.                                                         declare(&gsyms,sc_static,lc_static,bt_struct,flags | DF_GLOBAL);
  990.                         break;
  991.                 case kw_extern: {
  992.                                                 int thismangle = FALSE;
  993.                         getsym();
  994.                                                 if (prm_cplusplus && lastst == sconst) {
  995.                                                     if (!strcmp(laststr,Cstr)) {
  996.                                                         mangleflag = FALSE;
  997.                                                         manglelevel++;
  998.                                                         getsym();
  999.                                                         if (lastst == begin) {
  1000.                                                             getsym();
  1001.                                                           thismangle = FALSE;
  1002.                                                         }
  1003.                                                         else thismangle = TRUE;
  1004.                                                     }
  1005.                                                 }
  1006.                         if( defclass == sc_member) {
  1007.                             gensymerror(ERR_ILLCLASS,lastid);
  1008.                                                         break;
  1009.                                                 }
  1010.                         ++global_flag;
  1011.                         declare(&gsyms,sc_external,0,bt_struct,flags);
  1012.                         --global_flag;
  1013.                                                 if (thismangle && !--manglelevel)
  1014.                                                     mangleflag = TRUE;
  1015.                                                 }
  1016.                         break;
  1017.                                 case end:
  1018.                                                 if (prm_cplusplus && manglelevel) {
  1019.                                                     mangleflag = (!--manglelevel);
  1020.                                                     getsym();
  1021.                                                     continue;
  1022.                                                 }
  1023.                                                 return;
  1024.                                 case kw__interrupt:
  1025.                                                 flags |= DF_INT;
  1026.                                                 getsym();
  1027.                                                 continue;
  1028.                                 case kw__abs:
  1029.                                                 ++global_flag;
  1030.                                                 getsym();
  1031.                                                 if (lastst != openpa) {
  1032.                                                     generror(ERR_PUNCT,openpa,0);
  1033.                                                 }
  1034.                                                 else {
  1035.                                                     getsym();
  1036.                                                     val = intexpr(0);
  1037.                                                     if (lastst != closepa)
  1038.                                                         generror(ERR_PUNCT,closepa,skm_declclosepa);
  1039.                                                 }
  1040.                                                 getsym();
  1041.                                                 declare(&gsyms,sc_abs,val,bt_struct,flags);
  1042.                                                 --global_flag;
  1043.                                                 break;
  1044.                 default:
  1045.                         return;
  1046.                 }
  1047.                         flags = 0;
  1048.             }
  1049. }
  1050. void doargdecl(int defclass, char *names[], int *nparms, TABLE *table, int inline)
  1051. {
  1052.                 SYM *sp;
  1053.                 int flags = inline ? DF_FUNCPARMS : 0;
  1054.         for(;;) {
  1055.             switch(lastst) {
  1056.                                 case kw_const:
  1057.                                     flags |= DF_CONST;
  1058.                                     getsym();
  1059.                                     continue;
  1060.                                 case ellipse: {
  1061.                                     sprintf(declid,"**ELLIPSE%d**",pcount++);
  1062.                                     sp = xalloc(sizeof(SYM));
  1063.                                     sp->name = litlate(declid);
  1064.                   sp->storage_class = sc_auto;
  1065.                                     sp->tp = maketype(bt_ellipse,0);
  1066.                                     sp->tp->uflags |= UF_DEFINED | UF_USED;
  1067.                                     insert(sp,table);
  1068.                                     getsym();
  1069.                                     goto exit;
  1070.                                 }
  1071.                                 case id:
  1072.                 case kw_char: case kw_int: case kw_short: case kw_unsigned:
  1073.                 case kw_long: case kw_struct: case kw_union: case kw_signed:
  1074.                 case kw_enum: case kw_void:
  1075.                 case kw_float: case kw_double:
  1076.                     declare(table,sc_auto,0,bt_struct,flags);
  1077.                     break;
  1078.                 case kw_static:
  1079.                                 case kw_auto:
  1080.                                 case kw_register:
  1081.                         gensymerror(ERR_ILLCLASS,lastid);
  1082.                         getsym();
  1083.                                                 continue;
  1084.                 default:
  1085.                                                 goto exit;
  1086.                 }
  1087.                             if (inline) {
  1088.                                 names[(*nparms)++] = litlate(declid);
  1089.                             }
  1090.                         
  1091.                 flags &= ~DF_CONST;
  1092.                 }
  1093. exit:
  1094.         if (prm_cplusplus) {
  1095.             SYM *sp = table->head;
  1096.             int found = FALSE;
  1097.             while (sp) {
  1098.                 if (sp->defalt)
  1099.                     found = TRUE;
  1100.                 else 
  1101.                     if (found)
  1102.                         gensymerror(ERR_MISSINGDEFAULT,sp->name);
  1103.                 sp = sp->next;
  1104.             }
  1105.         }
  1106. }