home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 204_01 / init.c < prev    next >
Text File  |  1979-12-31  |  6KB  |  183 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6. /*
  7.  *    68000 C compiler
  8.  *
  9.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  10.  *  all commercial rights reserved.
  11.  *
  12.  *    This compiler is intended as an instructive tool for personal use. Any
  13.  *    use for profit without the written consent of the author is prohibited.
  14.  *
  15.  *    This compiler may be distributed freely for non-commercial use as long
  16.  *    as this notice stays intact. Please forward any enhancements or questions
  17.  *    to:
  18.  *
  19.  *        Matthew Brandt
  20.  *        Box 920337
  21.  *        Norcross, Ga 30092
  22.  */
  23.  
  24. doinit(sp)
  25. SYM     *sp;
  26. {       dseg();                 /* initialize into data segment */
  27.         nl();                   /* start a new line in object */
  28.         if(sp->storage_class == sc_static)
  29.                 put_label(sp->value.i);
  30.         else
  31.                 gen_strlab(sp->name);
  32.         if( lastst != assign)
  33.                 genstorage(sp->tp->size);
  34.         else    {
  35.                 getsym();
  36.                 inittype(sp->tp);
  37.                 }
  38.         endinit();
  39. }
  40.  
  41. int     inittype(tp)
  42. TYP     *tp;
  43. {       int     nbytes;
  44.         switch(tp->type) {
  45.  
  46.                 case bt_char:
  47.                         nbytes = initchar();
  48.                         break;
  49.                 case bt_short:
  50.                 case bt_enum:
  51.                         nbytes = initshort();
  52.                         break;
  53.                 case bt_pointer:
  54.                         if( tp->val_flag)
  55.                                 nbytes = initarray(tp);
  56.                         else
  57.                                 nbytes = initpointer();
  58.                         break;
  59.                 case bt_long:
  60.                         nbytes = initlong();
  61.                         break;
  62.                 case bt_struct:
  63.                         nbytes = initstruct(tp);
  64.                         break;
  65.                 default:
  66.                         error(ERR_NOINIT);
  67.                         nbytes = 0;
  68.                 }
  69.         return nbytes;
  70. }
  71.  
  72. initarray(tp)
  73. TYP     *tp;
  74. {       int     nbytes;
  75.         char    *p;
  76.         nbytes = 0;
  77.         if( lastst == begin) {
  78.                 getsym();               /* skip past the brace */
  79.                 while(lastst != end) {
  80.                         nbytes += inittype(tp->btp);
  81.                         if( lastst == comma)
  82.                                 getsym();
  83.                         else if( lastst != end)
  84.                                 error(ERR_PUNCT);
  85.                         }
  86.                 getsym();               /* skip closing brace */
  87.                 }
  88.         else if( lastst == sconst && tp->btp->type == bt_char) {
  89.                 nbytes = strlen(laststr) + 1;
  90.                 p = laststr;
  91.                 while( *p )
  92.                         genbyte(*p++);
  93.                 genbyte(0);
  94.                 getsym();
  95.                 }
  96.         else if( lastst != semicolon)
  97.                 error(ERR_ILLINIT);
  98.         if( nbytes < tp->size) {
  99.                 genstorage( tp->size - nbytes);
  100.                 nbytes = tp->size;
  101.                 }
  102.         else if( tp->size != 0 && nbytes > tp->size)
  103.                 error(ERR_INITSIZE);    /* too many initializers */
  104.         return nbytes;
  105. }
  106.  
  107. initstruct(tp)
  108. TYP     *tp;
  109. {       SYM     *sp;
  110.         int     nbytes;
  111.         needpunc(begin);
  112.         nbytes = 0;
  113.         sp = tp->lst.head;      /* start at top of symbol table */
  114.         while(sp != 0) {
  115.                 while(nbytes < sp->value.i)     /* align properly */
  116.                         nbytes += genbyte(0);
  117.                 nbytes += inittype(sp->tp);
  118.                 if( lastst == comma)
  119.                         getsym();
  120.                 else if(lastst == end)
  121.                         break;
  122.                 else
  123.                         error(ERR_PUNCT);
  124.                 sp = sp->next;
  125.                 }
  126.         if( nbytes < tp->size)
  127.                 genstorage( tp->size - nbytes);
  128.         needpunc(end);
  129.         return tp->size;
  130. }
  131.  
  132. initchar()
  133. {       genbyte(intexpr());
  134.         return 1;
  135. }
  136.  
  137. initshort()
  138. {       genword(intexpr());
  139.         return 2;
  140. }
  141.  
  142. initlong()
  143. {       genlong(intexpr());
  144.         return 4;
  145. }
  146.  
  147. initpointer()
  148. {       SYM     *sp;
  149.         if(lastst == and) {     /* address of a variable */
  150.                 getsym();
  151.                 if( lastst != id)
  152.                         error(ERR_IDEXPECT);
  153.                 else if( (sp = gsearch(lastid)) == 0)
  154.                         error(ERR_UNDEFINED);
  155.                 else    {
  156.                         getsym();
  157.                         if( lastst == plus || lastst == minus)
  158.                                 genref(sp,intexpr());
  159.                         else
  160.                                 genref(sp,0);
  161.                         if( sp->storage_class == sc_auto)
  162.                                 error(ERR_NOINIT);
  163.                         }
  164.                 }
  165.         else if(lastst == sconst) {
  166.                 gen_labref(stringlit(laststr));
  167.                 getsym();
  168.                 }
  169.         else
  170.                 genlong(intexpr());
  171.         endinit();
  172.         return 4;       /* pointers are 4 bytes long */
  173. }
  174.  
  175. endinit()
  176. {       if( lastst != comma && lastst != semicolon && lastst != end) {
  177.                 error(ERR_PUNCT);
  178.                 while( lastst != comma && lastst != semicolon && lastst != end)
  179.                         getsym();
  180.                 }
  181. }
  182.  
  183.