home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume5 / smallc / part2 / function.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.6 KB  |  147 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.  
  10. /*
  11.  *    begin a function
  12.  *
  13.  *    called from "parse", this routine tries to make a function out
  14.  *    of what follows
  15.  *    modified version.  p.l. woods
  16.  *
  17.  */
  18. int argtop;
  19. newfunc ()
  20. {
  21.     char    n[NAMESIZE], *ptr;
  22.     fexitlab = getlabel();
  23.  
  24.     if (!symname (n) ) {
  25.         error ("illegal function or declaration");
  26.         kill ();
  27.         return;
  28.     }
  29.     if (ptr = findglb (n)) {
  30.         if (ptr[IDENT] != FUNCTION)
  31.             multidef (n);
  32.         else if (ptr[OFFSET] == FUNCTION)
  33.             multidef (n);
  34.         else
  35.             ptr[OFFSET] = FUNCTION;
  36.     } else
  37.         addglb (n, FUNCTION, CINT, FUNCTION, PUBLIC);
  38.     prologue ();
  39.     if (!match ("("))
  40.         error ("missing open paren");
  41.     prefix ();
  42.     outstr (n);
  43.     col ();
  44.     nl ();
  45.     locptr = STARTLOC;
  46.     argstk = 0;
  47.     while (!match (")")) {
  48.         if (symname (n)) {
  49.             if (findloc (n))
  50.                 multidef (n);
  51.             else {
  52.                 addloc (n, 0, 0, argstk, AUTO);
  53.                 argstk = argstk + intsize();
  54.             }
  55.         } else {
  56.             error ("illegal argument name");
  57.             junk ();
  58.         }
  59.         blanks ();
  60.         if (!streq (line + lptr, ")")) {
  61.             if (!match (","))
  62.                 error ("expected comma");
  63.         }
  64.         if (endst ())
  65.             break;
  66.     }
  67.     stkp = 0;
  68.     argtop = argstk;
  69.     while (argstk) {
  70.         if (amatch ("register", 8)) {
  71.             if (amatch("char", 4)) 
  72.                 getarg(CCHAR);
  73.             else if (amatch ("int", 3))
  74.                 getarg(CINT);
  75.             else
  76.                 getarg(CINT);
  77.             ns();
  78.         } else if (amatch ("char", 4)) {
  79.             getarg (CCHAR);
  80.             ns ();
  81.         } else if (amatch ("int", 3)) {
  82.             getarg (CINT);
  83.             ns ();
  84.         } else {
  85.             error ("wrong number args");
  86.             break;
  87.         }
  88.     }
  89.     statement(YES);
  90.     printlabel(fexitlab);
  91.     col();
  92.     nl();
  93.     modstk (0);
  94.     gret ();
  95.     stkp = 0;
  96.     locptr = STARTLOC;
  97. }
  98.  
  99. /*
  100.  *    declare argument types
  101.  *
  102.  *    called from "newfunc", this routine add an entry in the local
  103.  *    symbol table for each named argument
  104.  *    completely rewritten version.  p.l. woods
  105.  *
  106.  */
  107. getarg (t)
  108. int    t;
  109. {
  110.     int    j, legalname, address;
  111.     char    n[NAMESIZE], c, *argptr;
  112.  
  113.     FOREVER {
  114.         if (argstk == 0)
  115.             return;
  116.         if (match ("*"))
  117.             j = POINTER;
  118.         else
  119.             j = VARIABLE;
  120.         if (!(legalname = symname (n)))
  121.             illname ();
  122.         if (match ("[")) {
  123.             while (inbyte () != ']')
  124.                 if (endst ())
  125.                     break;
  126.             j = POINTER;
  127.         }
  128.         if (legalname) {
  129.             if (argptr = findloc (n)) {
  130.                 argptr[IDENT] = j;
  131.                 argptr[TYPE] = t;
  132.                 address = argtop - glint(argptr);
  133.                 if (t == CCHAR && j == VARIABLE)
  134.                     address = address + byteoff();
  135.                 argptr[OFFSET] = (address) & 0xff;
  136.                 argptr[OFFSET + 1] = (address >> 8) & 0xff;
  137.             } else
  138.                 error ("expecting argument name");
  139.         }
  140.         argstk = argstk - intsize();
  141.         if (endst ())
  142.             return;
  143.         if (!match (","))
  144.             error ("expected comma");
  145.     }
  146. }
  147.