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