home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 338_01 / decl.c < prev    next >
Text File  |  1979-12-31  |  17KB  |  487 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *  all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. TYP             *head = 0;
  26. TYP             *tail = 0;
  27. char            *declid = 0;
  28. TABLE           tagtable = {0,0};
  29. TYP             stdconst = { bt_long, 1, 4, {0, 0}, 0, "stdconst"};
  30.  
  31. int     imax(i,j)
  32. int     i,j;
  33. {       return (i > j) ? i : j;
  34. }
  35.  
  36. char    *litlate(s)
  37. char    *s;
  38. {       char    *p;
  39.         p = xalloc(strlen(s) + 1);
  40.         strcpy(p,s);
  41.         return p;
  42. }
  43.  
  44. TYP     *maketype(bt,siz)
  45. int     bt, siz;
  46. {       TYP     *tp;
  47.         tp = xalloc(sizeof(TYP));
  48.         tp->val_flag = 0;
  49.         tp->size = siz;
  50.         tp->type = bt;
  51.         tp->sname = 0;
  52.         tp->lst.head = 0;
  53.         return tp;
  54. }
  55.  
  56. int     decl(table)
  57. TABLE   *table;
  58. {       switch (lastst) {
  59.                 case kw_char:
  60.                         head = tail = maketype(bt_char,1);
  61.                         getsym();
  62.                         break;
  63.                 case kw_short:
  64.                         head = tail = maketype(bt_short,2);
  65.                         getsym();
  66.                         break;
  67.                 case kw_int: case kw_long:
  68.                         head = tail = maketype(bt_long,4);
  69.                         getsym();
  70.                         break;
  71.                 case kw_unsigned:
  72.                         head = tail = maketype(bt_unsigned,4);
  73.                         getsym();
  74.                         if( lastst == kw_int )
  75.                                 getsym();
  76.                         break;
  77.                 case id:                /* no type declarator */
  78.                         head = tail = maketype(bt_long,4);
  79.                         break;
  80.                 case kw_float:
  81.                         head = tail = maketype(bt_float,4);
  82.                         getsym();
  83.                         break;
  84.                 case kw_double:
  85.                         head = tail = maketype(bt_double,8);
  86.                         getsym();
  87.                         break;
  88.                 case kw_enum:
  89.                         getsym();
  90.                         declenum(table);
  91.                         break;
  92.                 case kw_struct:
  93.                         getsym();
  94.                         declstruct(bt_struct);
  95.                         break;
  96.                 case kw_union:
  97.                         getsym();
  98.                         declstruct(bt_union);
  99.                         break;
  100.                 }
  101. }
  102.  
  103. decl1()
  104. {       TYP     *temp1, *temp2, *temp3, *temp4;
  105.         switch (lastst) {
  106.                 case id:
  107.                         declid = litlate(lastid);
  108.                         getsym();
  109.                         decl2();
  110.                         break;
  111.                 case star:
  112.                         temp1 = maketype(bt_pointer,4);
  113.                         temp1->btp = head;
  114.                         head = temp1;
  115.                         if(tail == NULL)
  116.                                 tail = head;
  117.                         getsym();
  118.                         decl1();
  119.                         break;
  120.                 case openpa:
  121.                         getsym();
  122.                         temp1 = head;
  123.                         temp2 = tail;
  124.                         head = tail = NULL;
  125.                         decl1();
  126.                         needpunc(closepa);
  127.                         temp3 = head;
  128.                         temp4 = tail;
  129.                         head = temp1;
  130.                         tail = temp2;
  131.                         decl2();
  132.                         temp4->btp = head;
  133.                         if(temp4->type == bt_pointer &&
  134.                                 temp4->val_flag != 0 && head != NULL)
  135.                                 temp4->size *= head->size;
  136.                         head = temp3;
  137.                         break;
  138.                 default:
  139.                         decl2();
  140.                         break;
  141.                 }
  142. }
  143.  
  144. decl2()
  145. {       TYP     *temp1;
  146.         switch (lastst) {
  147.                 case openbr:
  148.                         getsym();
  149.                         temp1 = maketype(bt_pointer,0);
  150.                         temp1->val_flag = 1;
  151.                         temp1->btp = head;
  152.                         if(lastst == closebr) {
  153.                                 temp1->size = 0;
  154.                                 getsym();
  155.                                 }
  156.                         else if(head != NULL) {
  157.                                 temp1->size = intexpr() * head->size;
  158.                                 needpunc(closebr);
  159.                                 }
  160.                         else {
  161.                                 temp1->size = intexpr();
  162.                                 needpunc(closebr);
  163.                                 }
  164.                         head = temp1;
  165.                         if( tail == NULL)
  166.                                 tail = head;
  167.                         decl2();
  168.                         break;
  169.                 case openpa:
  170.                         getsym();
  171.                         temp1 = maketype(bt_func,0);
  172.                         temp1->val_flag = 1;
  173.                         temp1->btp = head;
  174.                         head = temp1;
  175.                         if( lastst == closepa) {
  176.                                 getsym();
  177.                                 if(lastst == begin)
  178.                                         temp1->type = bt_ifunc;
  179.                                 }
  180.                         else
  181.                                 temp1->type = bt_ifunc;
  182.                         break;
  183.                 }
  184. }
  185.  
  186. int     alignment(tp)
  187. TYP     *tp;
  188. {       switch(tp->type) {
  189.                 case bt_char:           return AL_CHAR;
  190.                 case bt_short:          return AL_SHORT;
  191.                 case bt_long:           return AL_LONG;
  192.                 case bt_enum:           return AL_SHORT;
  193.                 case bt_pointer:
  194.                         if(tp->val_flag)
  195.                                 return alignment(tp->btp);
  196.                         else
  197.                                 return AL_POINTER;
  198.                 case bt_float:          return AL_FLOAT;
  199.                 case bt_double:         return AL_DOUBLE;
  200.                 case bt_struct:
  201.                 case bt_union:          return AL_STRUCT;
  202.                 default:                return AL_CHAR;
  203.                 }
  204. }
  205.  
  206. int     declare(table,al,ilc,ztype)
  207. /*
  208.  *      process declarations of the form:
  209.  *
  210.  *              <type>  <decl>, <decl>...;
  211.  *
  212.  *      leaves the declarations in the symbol table pointed to by
  213.  *      table and returns the number of bytes declared. al is the
  214.  *      allocation type to assign, ilc is the initial location
  215.  *      counter. if al is sc_member then no initialization will
  216.  *      be processed. ztype should be bt_struct for normal and in
  217.  *      structure declarations and sc_union for in union declarations.
  218.  */
  219. TABLE           *table;
  220. int               al;
  221. int             ilc;
  222. int               ztype;
  223. {       SYM     *sp, *sp1;
  224.         TYP     *dhead;
  225.         int     nbytes;
  226.         nbytes = 0;
  227.         decl(table);
  228.         dhead = head;
  229.         for(;;) {
  230.                 declid = 0;
  231.                 decl1();
  232.                 if( declid != 0) {      /* otherwise just struct tag... */
  233.                         sp = xalloc(sizeof(SYM));
  234.                         sp->name = declid;
  235.                         sp->storage_class = al;
  236.                         while( (ilc + nbytes) % alignment(head)) {
  237.                                 if( al != sc_member &&
  238.                                         al != sc_external &&
  239.                                         al != sc_auto) {
  240.                                         dseg();
  241.                                         genbyte(0);
  242.                                         }
  243.                                 ++nbytes;
  244.                                 }
  245.                         if( al == sc_static)
  246.                                 sp->value.i = nextlabel++;
  247.                         else if( ztype == bt_union)
  248.                                 sp->value.i = ilc;
  249.                         else if( al != sc_auto )
  250.                                 sp->value.i = ilc + nbytes;
  251.                         else
  252.                                 sp->value.i = -(ilc + nbytes + head->size);
  253.                         sp->tp = head;
  254.                         if( sp->tp->type == bt_func && 
  255.                                 sp->storage_class == sc_global )
  256.                                 sp->storage_class = sc_external;
  257.                         if(ztype == bt_union)
  258.                                 nbytes = imax(nbytes,sp->tp->size);
  259.                         else if(al != sc_external)
  260.                                 nbytes += sp->tp->size;
  261.                         if( sp->tp->type == bt_ifunc &&
  262.                                 (sp1 = search(sp->name,table->head)) != 0 &&
  263.                                 sp1->tp->type == bt_func )
  264.                                 {
  265.                                 sp1->tp = sp->tp;
  266.                                 sp1->storage_class = sp->storage_class;
  267.                                 sp1->value.i = sp->value.i;
  268.                                 sp = sp1;
  269.                                 }
  270.                         else
  271.                                 insert(sp,table);
  272.                         if( sp->tp->type == bt_ifunc) { /* function body follows */
  273.                                 funcbody(sp);
  274.                                 return nbytes;
  275.                                 }
  276.                         if( (al == sc_global || al == sc_static) &&
  277.                                 sp->tp->type != bt_func)
  278.                                 doinit(sp);
  279.                         }
  280.                 if(lastst == semicolon)
  281.                         break;
  282.                 needpunc(comma);
  283.                 if(declbegin(lastst) == 0)
  284.                         break;
  285.                 head = dhead;
  286.                 }
  287.         getsym();
  288.         return nbytes;
  289. }
  290.  
  291. int     declbegin(st)
  292. int               st;
  293. {       return st == star || st == id || st == openpa ||
  294.                 st == openbr;
  295. }
  296.  
  297. declenum(table)
  298. TABLE   *table;
  299. {       SYM     *sp;
  300.         TYP     *tp;
  301.         int     evalue;
  302.         if( lastst == id) {
  303.                 if((sp = search(lastid,tagtable.head)) == 0) {
  304.                         sp = xalloc(sizeof(SYM));
  305.                         sp->tp = xalloc(sizeof(TYP));
  306.                         sp->tp->type = bt_enum;
  307.                         sp->tp->size = 2;
  308.                         sp->tp->lst.head = sp->tp->btp = 0;
  309.                         sp->storage_class = sc_type;
  310.                         sp->name = litlate(lastid);
  311.                         sp->tp->sname = sp->name;
  312.                         getsym();
  313.                         if( lastst != begin)
  314.                                 error(ERR_INCOMPLETE);
  315.                         else    {
  316.                                 insert(sp,&tagtable);
  317.                                 getsym();
  318.                                 enumbody(table);
  319.                                 }
  320.                         }
  321.                 else
  322.                         getsym();
  323.                 head = sp->tp;
  324.                 }
  325.         else    {
  326.                 tp = xalloc(sizeof(tp));
  327.                 tp->type = bt_short;
  328.                 if( lastst != begin)
  329.                         error(ERR_INCOMPLETE);
  330.                 else    {
  331.                         getsym();
  332.                         enumbody(table);
  333.                         }
  334.                 head = tp;
  335.                 }
  336. }
  337.  
  338. enumbody(table)
  339. TABLE   *table;
  340. {       int     evalue;
  341.         SYM     *sp;
  342.         evalue = 0;
  343.         while(lastst == id) {
  344.                 sp = xalloc(sizeof(SYM));
  345.                 sp->value.i = evalue++;
  346.                 sp->name = litlate(lastid);
  347.                 sp->storage_class = sc_const;
  348.                 sp->tp = &stdconst;
  349.                 insert(sp,table);
  350.                 getsym();
  351.                 if( lastst == comma)
  352.                         getsym();
  353.                 else if(lastst != end)
  354.                         break;
  355.                 }
  356.         needpunc(end);
  357. }
  358.  
  359. declstruct(ztype)
  360. /*
  361.  *      declare a structure or union type. ztype should be either
  362.  *      bt_struct or bt_union.
  363.  */
  364. int               ztype;
  365. {       SYM     *sp;
  366.         TYP     *tp;
  367.         int     slc;
  368.         if(lastst == id) {
  369.                 if((sp = search(lastid,tagtable.head)) == 0) {
  370.                         sp = xalloc(sizeof(SYM));
  371.                         sp->name = litlate(lastid);
  372.                         sp->tp = xalloc(sizeof(TYP));
  373.                         sp->tp->type = ztype;
  374.                         sp->tp->lst.head = 0;
  375.                         sp->storage_class = sc_type;
  376.                         sp->tp->sname = sp->name;
  377.                         getsym();
  378.                         if(lastst != begin)
  379.                                 error(ERR_INCOMPLETE);
  380.                         else    {
  381.                                 insert(sp,&tagtable);
  382.                                 getsym();
  383.                                 structbody(sp->tp,ztype);
  384.                                 }
  385.                         }
  386.                 else
  387.                         getsym();
  388.                 head = sp->tp;
  389.                 }
  390.         else    {
  391.                 tp = xalloc(sizeof(TYP));
  392.                 tp->type = ztype;
  393.                 tp->sname = 0;
  394.                 tp->lst.head = 0;
  395.                 if( lastst != begin)
  396.                         error(ERR_INCOMPLETE);
  397.                 else    {
  398.                         getsym();
  399.                         structbody(tp,ztype);
  400.                         }
  401.                 head = tp;
  402.                 }
  403. }
  404.  
  405. structbody(tp,ztype)
  406. TYP             *tp;
  407. int               ztype;
  408. {       int     slc;
  409.         slc = 0;
  410.         tp->val_flag = 1;
  411.         while( lastst != end) {
  412.                 if(ztype == bt_struct)
  413.                         slc += declare(&(tp->lst),sc_member,slc,ztype);
  414.                 else
  415.                         slc = imax(slc,declare(&tp->lst,sc_member,0,ztype));
  416.                 }
  417.         tp->size = slc;
  418.         getsym();
  419. }
  420.  
  421. compile()
  422. /*
  423.  *      main compiler routine. this routine parses all of the
  424.  *      declarations using declare which will call funcbody as
  425.  *      functions are encountered.
  426.  */
  427. {       while(lastst != eof) {
  428.                 dodecl(sc_global);
  429.                 if( lastst != eof)
  430.                         getsym();
  431.                 }
  432.         dumplits();
  433. }
  434.  
  435. dodecl(defclass)
  436. int               defclass;
  437. {       int     size;
  438.         for(;;) {
  439.             switch(lastst) {
  440.                 case kw_register:
  441.                         getsym();
  442.                         if( defclass != sc_auto && defclass != sc_member )
  443.                                 error(ERR_ILLCLASS);
  444.                         goto do_decl;
  445.                 case id:
  446.                         if(defclass == sc_auto)
  447.                                 return;
  448. /*                      else fall through to declare    */
  449.                 case kw_char: case kw_int: case kw_short: case kw_unsigned:
  450.                 case kw_long: case kw_struct: case kw_union:
  451.                 case kw_enum: case kw_void:
  452.                 case kw_float: case kw_double:
  453. do_decl:            if( defclass == sc_global)
  454.                         lc_static +=
  455.                             declare(&gsyms,sc_global,lc_static,bt_struct);
  456.                     else if( defclass == sc_auto)
  457.                         lc_auto +=
  458.                             declare(&lsyms,sc_auto,lc_auto,bt_struct);
  459.                     else
  460.                         declare(&lsyms,sc_auto,0,bt_struct);
  461.                     break;
  462.                 case kw_static:
  463.                         getsym();
  464.                         if( defclass == sc_member)
  465.                             error(ERR_ILLCLASS);
  466.                         if( defclass == sc_auto )
  467.                             lc_static += 
  468.                                 declare(&lsyms,sc_static,lc_static,bt_struct);
  469.                         else
  470.                             lc_static +=
  471.                                 declare(&gsyms,sc_static,lc_static,bt_struct);
  472.                         break;
  473.                 case kw_extern:
  474.                         getsym();
  475.                         if( defclass == sc_member)
  476.                             error(ERR_ILLCLASS);
  477.                         ++global_flag;
  478.                         declare(&gsyms,sc_external,0,bt_struct);
  479.                         --global_flag;
  480.                         break;
  481.                 default:
  482.                         return;
  483.                 }
  484.             }
  485. }
  486.  
  487.