home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / c68k_src / func.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-10  |  3.4 KB  |  126 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 question
  18. s
  19.  *    to:
  20.  *
  21.  *        Matthew Brandt
  22.  *        Box 920337
  23.  *        Norcross, Ga 30092
  24.  */
  25.  
  26. /*      function compilation routines           */
  27.  
  28. funcbody(sp)
  29. /*
  30.  *      funcbody starts with the current symbol being either
  31.  *      the first parameter id or the begin for the local
  32.  *      block. If begin is the current symbol then funcbody
  33.  *      assumes that the function has no parameters.
  34.  */
  35. SYM     *sp;
  36. {       char    *names[20];             /* 20 parameters maximum */
  37.         int     nparms, poffset, i;
  38.         SYM     *sp1, *makeint();
  39.         global_flag = 0;
  40.         poffset = 8;            /* size of return block */
  41.         nparms = 0;
  42.         if(lastst == id) {              /* declare parameters */
  43.                 while(lastst == id) {
  44.                         names[nparms++] = litlate(lastid);
  45.                         getsym();
  46.                         if( lastst == comma)
  47.                                 getsym();
  48.                         else
  49.                                 break;
  50.                         }
  51.                 needpunc(closepa);
  52.                 dodecl(sc_member);      /* declare parameters */
  53.                 for(i = 0;i < nparms;++i) {
  54.                         if( (sp1 = search(names[i],lsyms.head)) == 0)
  55.                                 sp1 = makeint(names[i]);
  56.                         if( sp1->tp->size < 4 )
  57.                         {
  58.                             sp1->value.i = poffset +
  59.  (4 - sp1->tp->size);
  60.                             poffset += 4;
  61.                         }
  62.                         else
  63.                         {
  64.                             sp1->value.i = poffset;
  65.                             poffset += sp1->tp->size
  66. ;
  67.                         }
  68.                         sp1->storage_class = sc_auto;
  69.                     }
  70.                 }
  71.         if(lastst != begin)
  72.                 error(ERR_BLOCK);
  73.         else    {
  74.                 cseg();
  75.                 gen_strlab(sp->name);
  76.                 block();
  77.                 funcbottom();
  78.                 }
  79.         global_flag = 1;
  80. }
  81.  
  82. SYM     *makeint(name)
  83. char    *name;
  84. {       SYM     *sp;
  85.         TYP     *tp;
  86.         sp = xalloc(sizeof(SYM));
  87.         tp = xalloc(sizeof(TYP));
  88.         tp->type = bt_long;
  89.         tp->size = 4;
  90.         tp->btp = tp->lst.head = 0;
  91.         tp->sname = 0;
  92.         sp->name = name;
  93.         sp->storage_class = sc_auto;
  94.         sp->tp = tp;
  95.         insert(sp,&lsyms);
  96.         return sp;
  97. }
  98.  
  99. check_table(head)
  100. SYM     *head;
  101. {       while( head != 0 ) {
  102.                 if( head->storage_class == sc_ulabel )
  103.                         fprintf(list,"*** UNDEFINED LABEL - %s\n",head->name);
  104.                 head = head->next;
  105.                 }
  106. }
  107.  
  108. funcbottom()
  109. {       nl();
  110.         check_table(lsyms.head);
  111.         lc_auto = 0;
  112.         fprintf(list,"\n\n*** local symbol table ***\n\n");
  113.         list_table(&lsyms,0);
  114.         fprintf(list,"\n\n\n");
  115.         release_local();        /* release local symbols */
  116. }
  117.  
  118. block()
  119. {       needpunc(begin);
  120.         dodecl(sc_auto);
  121.         cseg();
  122.         genfunc(compound());
  123.         flush_peep();
  124. }
  125.  
  126.