home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / icont / lmem.c < prev    next >
C/C++ Source or Header  |  2000-10-06  |  7KB  |  277 lines

  1. /*
  2.  * lmem.c -- memory initialization and allocation; also parses arguments.
  3.  */
  4.  
  5. #include "link.h"
  6. #include "tproto.h"
  7. #include "tglobals.h"
  8.  
  9. /*
  10.  * Prototypes.
  11.  */
  12.  
  13. static struct    lfile *alclfile    (char *name);
  14.  
  15. void dumplfiles(void);
  16.  
  17. #ifdef MultipleRuns
  18.    static void    freelfile    (struct lfile *p);
  19. #endif                    /* MultipleRuns */
  20.  
  21. /*
  22.  * Memory initialization
  23.  */
  24.  
  25. struct gentry **lghash;        /* hash area for global table */
  26. struct ientry **lihash;        /* hash area for identifier table */
  27. struct fentry **lfhash;        /* hash area for field table */
  28.  
  29. struct lentry *lltable;        /* local table */
  30. struct centry *lctable;        /* constant table */
  31. struct ipc_fname *fnmtbl;    /* table associating ipc with file name */
  32. struct ipc_line *lntable;    /* table associating ipc with line number */
  33.  
  34. char *lsspace;            /* string space */
  35. word *labels;            /* label table */
  36. char *codeb;            /* generated code space */
  37.  
  38. struct ipc_fname *fnmfree;    /* free pointer for ipc/file name table */
  39. struct ipc_line *lnfree;    /* free pointer for ipc/line number table */
  40. word lsfree;            /* free index for string space */
  41. char *codep;            /* free pointer for code space */
  42.  
  43. struct fentry *lffirst;        /* first field table entry */
  44. struct fentry *lflast;        /* last field table entry */
  45. struct gentry *lgfirst;        /* first global table entry */
  46. struct gentry *lglast;        /* last global table entry */
  47.  
  48. #ifdef MultipleRuns
  49.    extern word pc;
  50.    extern int fatals;
  51.    extern int nlflag;
  52.    extern int lstatics;
  53.    extern int nfields;
  54. #endif                    /* MultipleRuns */
  55.  
  56. /*
  57.  * linit - scan the command line arguments and initialize data structures.
  58.  */
  59. void linit()
  60.    {
  61.    struct gentry **gp;
  62.    struct ientry **ip;
  63.    struct fentry **fp;
  64.  
  65.    llfiles = NULL;        /* Zero queue of files to link. */
  66.  
  67.    /*
  68.     * Allocate the various data structures that are used by the linker.
  69.     */
  70.    lghash   = (struct gentry **) tcalloc(ghsize, sizeof(struct gentry *));
  71.    lihash   = (struct ientry **) tcalloc(ihsize, sizeof(struct ientry *));
  72.    lfhash   = (struct fentry **) tcalloc(fhsize, sizeof(struct fentry *));
  73.  
  74.    lltable  = (struct lentry *) tcalloc(lsize, sizeof(struct lentry));
  75.    lctable  = (struct centry *) tcalloc(csize, sizeof(struct centry));
  76.  
  77.    lnfree = lntable  = (struct ipc_line*)tcalloc(nsize,sizeof(struct ipc_line));
  78.  
  79.    lsspace = (char *) tcalloc(stsize, sizeof(char));
  80.    lsfree = 0;
  81.  
  82.    fnmtbl = (struct ipc_fname *) tcalloc(fnmsize, sizeof(struct ipc_fname));
  83.    fnmfree = fnmtbl;
  84.  
  85.    labels  = (word *) tcalloc(maxlabels, sizeof(word));
  86.    codep = codeb = (char *) tcalloc(maxcode, 1);
  87.  
  88.    lffirst = NULL;
  89.    lflast = NULL;
  90.    lgfirst = NULL;
  91.    lglast = NULL;
  92.  
  93.    /*
  94.     * Zero out the hash tables.
  95.     */
  96.    for (gp = lghash; gp < &lghash[ghsize]; gp++)
  97.       *gp = NULL;
  98.    for (ip = lihash; ip < &lihash[ihsize]; ip++)
  99.       *ip = NULL;
  100.    for (fp = lfhash; fp < &lfhash[fhsize]; fp++)
  101.       *fp = NULL;
  102.  
  103. #ifdef MultipleRuns
  104.    /*
  105.     * Initializations required for repeated program runs.
  106.     */
  107.    pc = 0;                /* In lcode.c    */
  108.    nrecords = 0;            /* In lglob.c    */
  109.  
  110.    #ifdef EventMon
  111.       colmno = 0;            /* In link.c    */
  112.    #endif                /* EventMon */
  113.  
  114.    lineno = 0;                /* In link.c    */
  115.    fatals = 0;                /* In link.c    */
  116.    nlflag = 0;                /* In llex.c    */
  117.    lstatics = 0;            /* In lsym.c    */
  118.    nfields = 0;                /* In lsym.c    */
  119. #endif                    /* MultipleRuns */
  120.  
  121.    /*
  122.     * Install "main" as a global variable in order to insure that it
  123.     *  is the first global variable.  iconx/start.s depends on main
  124.     *  being global number 0.
  125.     */
  126.    putglobal(instid("main"), F_Global, 0, 0);
  127.    }
  128.  
  129. #ifdef DeBugLinker
  130. /*
  131.  * dumplfiles - print the list of files to link.  Used for debugging only.
  132.  */
  133.  
  134. void dumplfiles()
  135.    {
  136.    struct lfile *p,*lfls;
  137.  
  138.    fprintf(stderr,"lfiles:\n");
  139.    lfls = llfiles;
  140.    while (p = getlfile(&lfls))
  141.        fprintf(stderr,"'%s'\n",p->lf_name);
  142.    fflush(stderr);
  143.    }
  144. #endif                    /* DeBugLinker */
  145.  
  146. /*
  147.  * alsolink - create an lfile structure for the named file and add it to the
  148.  *  end of the list of files (llfiles) to generate link instructions for.
  149.  */
  150. void alsolink(name)
  151. char *name;
  152.    {
  153.    struct lfile *nlf, *p;
  154.    char file[MaxFileName];
  155.  
  156.    if (!pathfind(file, ipath, name, U1Suffix))
  157.      quitf("cannot resolve reference to file '%s'",name);
  158.  
  159.    nlf = alclfile(file);
  160.    if (llfiles == NULL) {
  161.       llfiles = nlf;
  162.       }
  163.    else {
  164.       p = llfiles;
  165.       while (p->lf_link != NULL) {
  166.         if (strcmp(p->lf_name,file) == 0)
  167.            return;
  168.         p = p->lf_link;
  169.         }
  170.       if (strcmp(p->lf_name,file) == 0)
  171.         return;
  172.       p->lf_link = nlf;
  173.       }
  174.    }
  175.  
  176. /*
  177.  * getlfile - return a pointer (p) to the lfile structure pointed at by lptr
  178.  *  and move lptr to the lfile structure that p points at.  That is, getlfile
  179.  *  returns a pointer to the current (wrt. lptr) lfile and advances lptr.
  180.  */
  181. struct lfile *getlfile(lptr)
  182. struct lfile **lptr;
  183.    {
  184.    struct lfile *p;
  185.  
  186.    if (*lptr == NULL)
  187.       return (struct lfile *)NULL;
  188.    else {
  189.       p = *lptr;
  190.       *lptr = p->lf_link;
  191.       return p;
  192.       }
  193.    }
  194.  
  195. /*
  196.  * alclfile - allocate an lfile structure for the named file, fill
  197.  *  in the name and return a pointer to it.
  198.  */
  199. static struct lfile *alclfile(name)
  200. char *name;
  201.    {
  202.    struct lfile *p;
  203.  
  204.    p = (struct lfile *) alloc(sizeof(struct lfile));
  205.    p->lf_link = NULL;
  206.    p->lf_name = salloc(name);
  207.    return p;
  208.    }
  209.  
  210. #ifdef MultipleRuns
  211. /*
  212.  * freelfile - free memory of an lfile structure.
  213.  */
  214. static void freelfile(p)
  215. struct lfile *p;
  216.    {
  217.    free((char *)p->lf_name);
  218.    free((char *) p);
  219.    }
  220. #endif                    /* MultipleRuns */
  221.  
  222. /*
  223.  * lmfree - free memory used by the linker
  224.  */
  225. void lmfree()
  226.    {
  227.    struct fentry *fp, *fp1;
  228.    struct gentry *gp, *gp1;
  229.    struct rentry *rp, *rp1;
  230.    struct ientry *ip, *ip1;
  231.    int i;
  232.  
  233.    for (i = 0; i < ihsize; ++i)
  234.       for (ip = lihash[i]; ip != NULL; ip = ip1) {
  235.            ip1 = ip->i_blink;
  236.            free((char *)ip);
  237.            }
  238.  
  239.    free((char *) lghash);   lghash = NULL;
  240.    free((char *) lihash);   lihash = NULL;
  241.    free((char *) lfhash);   lfhash = NULL;
  242.    free((char *) lltable);   lltable = NULL;
  243.    free((char *) lctable);   lctable = NULL;
  244.    free((char *) lntable);   lntable = NULL;
  245.    free((char *) lsspace);   lsspace = NULL;
  246.    free((char *) fnmtbl);   fnmtbl = NULL;
  247.    free((char *) labels);   labels = NULL;
  248.    free((char *) codep);   codep = NULL;
  249.  
  250.    for (fp = lffirst; fp != NULL; fp = fp1) {
  251.       for(rp = fp->f_rlist; rp != NULL; rp = rp1) {
  252.          rp1 = rp->r_link;
  253.          free((char *)rp);
  254.          }
  255.       fp1 = fp->f_nextentry;
  256.       free((char *)fp);
  257.       }
  258.    lffirst = NULL;
  259.    lflast = NULL;
  260.  
  261.    for (gp = lgfirst; gp != NULL; gp = gp1) {
  262.       gp1 = gp->g_next;
  263.       free((char *)gp);
  264.       }
  265.    lgfirst = NULL;
  266.    lglast = NULL;
  267.  
  268. #ifdef MultipleRuns
  269.    for (lf = llfiles; lf != NULL; lf = nlf) {
  270.       nlf = lf->lf_link;
  271.       freelfile(lf);
  272.       }
  273.    llfiles = NULL;
  274. #endif                    /* MultipleRuns */
  275.  
  276.    }
  277.