home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / sym_link.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  4KB  |  127 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1991 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. #define LINKER 1
  7. #include <stdio.h>
  8. #include "defines.h"
  9. #include "nasm.h"
  10. #include "debug.h"
  11. #include "labels.h"
  12.  
  13. extern label huge *h_global[SEP], huge *t_global[SEP], huge *l_global;
  14. extern int        u_global;
  15. extern lword      l_hash;
  16.  
  17. extern char    err_defined[], warn_0fwd[], warn_equfwd[], is_where[];
  18. extern char    *str_alloc();
  19.  
  20. /* ---------------------------------------------------------- */
  21. /*                  Find a label by it's name                 */
  22. /* ---------------------------------------------------------- */
  23. label *find_label( s)
  24. register char  *s;
  25. {
  26.    register label huge  *p;
  27.    register lword       hash;
  28.    register int         res;
  29.  
  30.    ENTER("find_label");
  31.  
  32.    l_hash = hash = calc_hash( s + 1);
  33.    p = h_global[ u_global = is_where[*s]];
  34.    while( p && (p->hash < hash ||
  35.           (p->hash == hash && (res = strcmp( p->name, s)) < 0)))
  36.       p = p->next;
  37.    l_global = p;
  38.    if( ! found(p, hash) || res)
  39.    {
  40.       LEAVE();
  41.       return( 0);
  42.    }
  43.    LEAVE();
  44.    return( p);
  45. }
  46.  
  47. /* ---------------------------------------------------------- */
  48. /*         This routine is called when a forward label        */
  49. /*         is encountered:                                    */
  50. /*    -- Note that the tables have already been searched --   */
  51. /* e.g.:                                                      */
  52. /*             sta foo                                        */
  53. /* not:  foo   lda foo                                        */
  54. /* not:  foo:  =  56                                          */
  55. /* not:  foo   *=$600                                         */
  56. /* ---------------------------------------------------------- */
  57. label *enter_llabel( s, val, type)
  58. char              *s;
  59. register word     val;
  60. word              type;
  61. {
  62.    register label huge  *p;
  63.  
  64.    ENTER("enter forward label");
  65. #if DEBUG
  66.    fprintf( ESTREAM, "with %s as name, and some value $%X\n", s, val);
  67. #endif
  68.    (p = lab_alloc())->name = s;
  69.    p->refs = 0;
  70.    p->hash = l_hash;
  71.    p->val  = val;
  72.    p->type = type;
  73.    einlinker( h_global, t_global, l_global, u_global, p);
  74.    LEAVE();
  75.    return( p);
  76. }
  77.  
  78.  
  79. void  define( name, value)
  80. char  *name;
  81. word  value;
  82. {
  83. #if ! VERSION
  84.    name = strcpy( str_alloc( strlen( name) + 1), name);
  85. #endif   
  86.    if( ! find_label( name))
  87.       (void) enter_llabel( name, value, L_NORMAL);
  88.    else
  89.       nserror("Label has been already defined", name);
  90. }
  91.  
  92.  
  93. lerr_undefs( flag)
  94. {
  95.    register label huge  *p;
  96.    register int         i, j, f;
  97.  
  98.    ENTER("lerr_undefs");
  99.    for( i = f = 0; i < SEP;)
  100.       if( p = h_global[i++])
  101.          do
  102.             if( p->refs)
  103.             {
  104.                if( ! f)
  105.                {
  106.                   if( flag)
  107.                      return( 1);
  108.                   f = 1;
  109.                   j = 0;
  110.                   putc( '\n', ESTREAM);
  111.                   nerror( "Some labels remain undefined");
  112.                }
  113.                if( ++j == 8)
  114.                {
  115.                   j = 0;
  116.                   putc( '\n', ESTREAM);
  117.                }
  118.                fprintf( ESTREAM, "%-8.8s ", p->name);
  119.             }
  120.          while( p = p->next);
  121.    if( f && ! flag && j)
  122.       putc( '\n', ESTREAM);
  123.    LEAVE();
  124.    return( 0);
  125. }
  126.  
  127.