home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 109.lha / PD_C / src / Func.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-20  |  3.5 KB  |  140 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. extern char     *litlate();
  26. extern SYM    *search();
  27. extern struct snode *compound();
  28.  
  29. /*      function compilation routines           */
  30.  
  31. funcbody(sp)
  32. /*
  33.  *      funcbody starts with the current symbol being either
  34.  *      the first parameter id or the begin for the local
  35.  *      block. If begin is the current symbol then funcbody
  36.  *      assumes that the function has no parameters.
  37.  */
  38. SYM     *sp;
  39. {       char    *names[20];             /* 20 parameters maximum */
  40.         int     nparms, poffset, i, size;
  41.         SYM     *sp1, *makeint();
  42.         global_flag = 0;
  43.         poffset = 8;            /* size of return block */
  44.         nparms = 0;
  45.         if(lastst == id) {              /* declare parameters */
  46.                 while(lastst == id) {
  47.                         names[nparms++] = litlate(lastid);
  48.                         getsym();
  49.                         if( lastst == comma)
  50.                                 getsym();
  51.                         else
  52.                                 break;
  53.                         }
  54.                 needpunc(closepa);
  55.                 dodecl(sc_member);      /* declare parameters */
  56.                 for(i = 0;i < nparms;++i) {
  57.                         if( (sp1 = search(names[i],lsyms.head)) == 0)
  58.                                 sp1 = makeint(names[i]);
  59.             if ( sp1->tp->type == bt_pointer )
  60.                size = 4;
  61.             else
  62.                size = sp1->tp->size;
  63.             sp1->value.i = poffset;
  64.             if( size < 4 )
  65.                {
  66.                 if ( size == 1)
  67.                   ++sp1->value.i;
  68.                 poffset += 2;
  69.                 }
  70.             else
  71.                poffset += size;
  72.             sp1->storage_class = sc_auto;
  73.         }
  74.         }
  75.         if(lastst != begin)
  76.                 error(ERR_BLOCK);
  77.         else    {
  78.                 cseg();
  79.                 gen_strlab(sp->name);
  80.                 block();
  81.                 funcbottom();
  82.                 }
  83.         global_flag = 1;
  84. }
  85.  
  86. SYM     *makeint(name)
  87. char    *name;
  88. {       SYM     *sp;
  89.         TYP     *tp;
  90.         sp = xalloc(sizeof(SYM));
  91.         tp = xalloc(sizeof(TYP));
  92.         tp->type = bt_long;
  93.         tp->size = 4;
  94.         tp->btp = tp->lst.head = 0;
  95.         tp->sname = 0;
  96.         sp->name = name;
  97.         sp->storage_class = sc_auto;
  98.         sp->tp = tp;
  99.         insert(sp,&lsyms);
  100.         return sp;
  101. }
  102.  
  103. check_table(head)
  104. SYM     *head;
  105. {      char *err;
  106.  
  107.        err = (char *)"*** UNDEFINED LABEL - %s\n";
  108.        while( head != 0 ) 
  109.      {
  110.            if( head->storage_class == sc_ulabel )
  111.          {
  112.         if ( Options.List )
  113.                    fprintf(list, err, head->name);
  114.         printf( err, head->name );
  115.          }
  116.            head = head->next;
  117.      }
  118. }
  119.  
  120. funcbottom()
  121. {       nl();
  122.         check_table(lsyms.head);
  123.         lc_auto = 0;
  124.     if ( Options.List )
  125.            fprintf(list,"\n\n*** local symbol table ***\n\n");
  126.         list_table(&lsyms,0);
  127.     if ( Options.List )
  128.            fprintf(list,"\n\n\n");
  129.         release_local();        /* release local symbols */
  130. }
  131.  
  132. block()
  133. {       needpunc(begin);
  134.         dodecl(sc_auto);
  135. cseg();
  136.         genfunc(compound());
  137.         flush_peep();
  138. }
  139.  
  140.