home *** CD-ROM | disk | FTP | other *** search
/ messroms.de / 2007-01-13_www.messroms.de.zip / VZ200 / TOOLS / ZCCSRC.ZIP / scc / function.c < prev    next >
C/C++ Source or Header  |  2000-03-01  |  3KB  |  177 lines

  1. /*    File function.c: 2.1 (83/03/20,16:02:04) */
  2. /*% cc -O -c %
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "defs.h"
  8. #include "data.h"
  9. #include "proto.h"
  10.  
  11. /*
  12.  *    begin a function
  13.  *
  14.  *    called from "parse", this routine tries to make a function out
  15.  *    of what follows
  16.  *    modified version.  p.l. woods
  17.  *
  18.  */
  19. int argtop;
  20.  
  21. void newfunc(void)
  22. {
  23.     char n[NAMESIZE], *ptr;
  24.     int i;
  25.  
  26.     fexitlab = getlabel();
  27.  
  28.     if (!symname(n))
  29.     {
  30.         error("illegal function or declaration");
  31.         kill();
  32.         return;
  33.     }
  34.     if (NULL!=(ptr = findglb(n)))
  35.     {
  36.         if (ptr[IDENT] != FUNCTION)
  37.             multidef(n);
  38.         else
  39.         if (ptr[OFFSET] == FUNCTION)
  40.             multidef(n);
  41.         else
  42.             ptr[OFFSET] = FUNCTION;
  43.     }
  44.     else
  45.         addglb(n, FUNCTION, CINT, FUNCTION, PUBLIC);
  46.     if (!match("("))
  47.         error("missing open paren");
  48.     prefix();
  49.     outstr(n);
  50.     col();
  51.     nl();
  52.     prologue();
  53.     locptr = STARTLOC;
  54.     argstk = 0;
  55.     while (!match(")"))
  56.     {
  57.         if (symname(n))
  58.         {
  59.             if (findloc(n))
  60.                 multidef(n);
  61.             else
  62.             {
  63.                 addloc(n, 0, 0, argstk, AUTO);
  64.                 argstk = argstk + intsize();
  65.             }
  66.         }
  67.         else
  68.         {
  69.             error("illegal argument name");
  70.             junk();
  71.         }
  72.         blanks();
  73.         if (!streq(line + lptr, ")"))
  74.         {
  75.             if (!match(","))
  76.                 error("expected comma");
  77.         }
  78.         if (endst())
  79.             break;
  80.     }
  81.     stkp = 0;
  82.     argtop = argstk;
  83.     while (argstk)
  84.     {
  85.         if (amatch("register", 8))
  86.         {
  87.             if (amatch("char", 4))
  88.                 getarg(CCHAR);
  89.             else
  90.             if (amatch("int", 3))
  91.                 getarg(CINT);
  92.             else
  93.                 getarg(CINT);
  94.             ns();
  95.         }
  96.         else
  97.         if (amatch("char", 4))
  98.         {
  99.             getarg(CCHAR);
  100.             ns();
  101.         }
  102.         else
  103.         if (amatch("int", 3))
  104.         {
  105.             getarg(CINT);
  106.             ns();
  107.         }
  108.         else
  109.         {
  110.             error("wrong number args");
  111.             break;
  112.         }
  113.     }
  114.     i = frameix ? frame() : 0;
  115.     statement(YES);
  116.     printlabel(fexitlab);
  117.     col();
  118.     nl();
  119.     modstk(-i);                           /* don't adjust for frame pointer here */
  120.     gret();
  121.     stkp = 0;
  122.     locptr = STARTLOC;
  123. }
  124.  
  125. /*
  126.  *    declare argument types
  127.  *
  128.  *    called from "newfunc", this routine add an entry in the local
  129.  *    symbol table for each named argument
  130.  *    completely rewritten version.  p.l. woods
  131.  *
  132.  */
  133. void getarg(int t)
  134. {
  135.     int j, legalname, address;
  136.     char n[NAMESIZE], *argptr;
  137.  
  138.     for (;;)
  139.     {
  140.         if (argstk == 0)
  141.             return;
  142.         if (match("*"))
  143.             j = POINTER;
  144.         else
  145.             j = VARIABLE;
  146.         if (!(legalname = symname(n)))
  147.             illname();
  148.         if (match("["))
  149.         {
  150.             while (inbyte() != ']')
  151.                 if (endst())
  152.                     break;
  153.             j = POINTER;
  154.         }
  155.         if (legalname)
  156.         {
  157.             if ((argptr = findloc(n)))
  158.             {
  159.                 argptr[IDENT] = j;
  160.                 argptr[TYPE] = t;
  161.                 address = argtop - glint(argptr);
  162.                 if (t == CCHAR && j == VARIABLE)
  163.                     address = address + byteoff();
  164.                 argptr[OFFSET] = (address) & 0xff;
  165.                 argptr[OFFSET + 1] = (address >> 8) & 0xff;
  166.             }
  167.             else
  168.                 error("expecting argument name");
  169.         }
  170.         argstk = argstk - intsize();
  171.         if (endst())
  172.             return;
  173.         if (!match(","))
  174.             error("expected comma");
  175.     }
  176. }
  177.