home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / c68k_src / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-10  |  5.4 KB  |  184 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 question
  17. s
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. doinit(sp)
  26. SYM     *sp;
  27. {       dseg();                 /* initialize into data segment */
  28.         nl();                   /* start a new line in object */
  29.         if(sp->storage_class == sc_static)
  30.                 put_label(sp->value.i);
  31.         else
  32.                 gen_strlab(sp->name);
  33.         if( lastst != assign)
  34.                 genstorage(sp->tp->size);
  35.         else    {
  36.                 getsym();
  37.                 inittype(sp->tp);
  38.                 }
  39.         endinit();
  40. }
  41.  
  42. int     inittype(tp)
  43. TYP     *tp;
  44. {       int     nbytes;
  45.         switch(tp->type) {
  46.  
  47.                 case bt_char:
  48.                         nbytes = initchar();
  49.                         break;
  50.                 case bt_short:
  51.                 case bt_enum:
  52.                         nbytes = initshort();
  53.                         break;
  54.                 case bt_pointer:
  55.                         if( tp->val_flag)
  56.                                 nbytes = initarray(tp);
  57.                         else
  58.                                 nbytes = initpointer();
  59.                         break;
  60.                 case bt_long:
  61.                         nbytes = initlong();
  62.                         break;
  63.                 case bt_struct:
  64.                         nbytes = initstruct(tp);
  65.                         break;
  66.                 default:
  67.                         error(ERR_NOINIT);
  68.                         nbytes = 0;
  69.                 }
  70.         return nbytes;
  71. }
  72.  
  73. initarray(tp)
  74. TYP     *tp;
  75. {       int     nbytes;
  76.         char    *p;
  77.         nbytes = 0;
  78.         if( lastst == begin) {
  79.                 getsym();               /* skip past the brace */
  80.                 while(lastst != end) {
  81.                         nbytes += inittype(tp->btp);
  82.                         if( lastst == comma)
  83.                                 getsym();
  84.                         else if( lastst != end)
  85.                                 error(ERR_PUNCT);
  86.                         }
  87.                 getsym();               /* skip closing brace */
  88.                 }
  89.         else if( lastst == sconst && tp->btp->type == bt_char) {
  90.                 nbytes = strlen(laststr) + 1;
  91.                 p = laststr;
  92.                 while( *p )
  93.                         genbyte(*p++);
  94.                 genbyte(0);
  95.                 getsym();
  96.                 }
  97.         else if( lastst != semicolon)
  98.                 error(ERR_ILLINIT);
  99.         if( nbytes < tp->size) {
  100.                 genstorage( tp->size - nbytes);
  101.                 nbytes = tp->size;
  102.                 }
  103.         else if( tp->size != 0 && nbytes > tp->size)
  104.                 error(ERR_INITSIZE);    /* too many initializers */
  105.         return nbytes;
  106. }
  107.  
  108. initstruct(tp)
  109. TYP     *tp;
  110. {       SYM     *sp;
  111.         int     nbytes;
  112.         needpunc(begin);
  113.         nbytes = 0;
  114.         sp = tp->lst.head;      /* start at top of symbol table */
  115.         while(sp != 0) {
  116.                 while(nbytes < sp->value.i)     /* align properly */
  117.                         nbytes += genbyte(0);
  118.                 nbytes += inittype(sp->tp);
  119.                 if( lastst == comma)
  120.                         getsym();
  121.                 else if(lastst == end)
  122.                         break;
  123.                 else
  124.                         error(ERR_PUNCT);
  125.                 sp = sp->next;
  126.                 }
  127.         if( nbytes < tp->size)
  128.                 genstorage( tp->size - nbytes);
  129.         needpunc(end);
  130.         return tp->size;
  131. }
  132.  
  133. initchar()
  134. {       genbyte(intexpr());
  135.         return 1;
  136. }
  137.  
  138. initshort()
  139. {       genword(intexpr());
  140.         return 2;
  141. }
  142.  
  143. initlong()
  144. {       genlong(intexpr());
  145.         return 4;
  146. }
  147.  
  148. initpointer()
  149. {       SYM     *sp;
  150.         if(lastst == and) {     /* address of a variable */
  151.                 getsym();
  152.                 if( lastst != id)
  153.                         error(ERR_IDEXPECT);
  154.                 else if( (sp = gsearch(lastid)) == 0)
  155.                         error(ERR_UNDEFINED);
  156.                 else    {
  157.                         getsym();
  158.                         if( lastst == plus || lastst == minus)
  159.                                 genref(sp,intexpr());
  160.                         else
  161.                                 genref(sp,0);
  162.                         if( sp->storage_class == sc_auto)
  163.                                 error(ERR_NOINIT);
  164.                         }
  165.                 }
  166.         else if(lastst == sconst) {
  167.                 gen_labref(stringlit(laststr));
  168.                 getsym();
  169.                 }
  170.         else
  171.                 genlong(intexpr());
  172.         endinit();
  173.         return 4;       /* pointers are 4 bytes long */
  174. }
  175.  
  176. endinit()
  177. {       if( lastst != comma && lastst != semicolon && lastst != end) {
  178.                 error(ERR_PUNCT);
  179.                 while( lastst != comma && lastst != semicolon && lastst != end)
  180.                         getsym();
  181.                 }
  182. }
  183.  
  184.