home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Source / GNU / cctools / as / symbols.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-31  |  13.5 KB  |  487 lines

  1. /* symbols.c -symbol table-
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "as.h"
  22. #include "hash.h"
  23. #include "obstack.h"        /* For "symbols.h" */
  24. #include "struc-symbol.h"
  25. #include "symbols.h"
  26. #include "frags.h"
  27.  
  28. #ifndef WORKING_DOT_WORD
  29. extern int new_broken_words;
  30. #endif
  31.  
  32. #ifndef NeXT    /* the .dump/.load feature (sy_hash used in read.c) */
  33. static
  34. #endif NeXT    /* the .dump/.load feature */
  35. struct hash_control *
  36. sy_hash;            /* symbol-name => struct symbol pointer */
  37.  
  38.                 /* Below are commented in "symbols.h". */
  39. unsigned int local_bss_counter;
  40. symbolS * symbol_rootP;
  41. symbolS * symbol_lastP;
  42. symbolS    abs_symbol;
  43. struct obstack    notes;
  44.  
  45.  
  46.  
  47. symbolS * symbol_find();    /* Keep C compiler happy. */
  48.  
  49. /*
  50.  * Un*x idea of local labels. They are made by "n:" where n
  51.  * is any decimal digit. Refer to them with
  52.  *  "nb" for previous (backward) n:
  53.  *  or "nf" for next (forward) n:.
  54.  *
  55.  * Like Un*x AS, we have one set of local label counters for entire assembly,
  56.  * not one set per (sub)segment like in most assemblers. This implies that
  57.  * one can refer to a label in another segment, and indeed some crufty
  58.  * compilers have done just that.
  59.  *
  60.  * I document the symbol names here to save duplicating words elsewhere.
  61.  * The mth occurence of label n: is turned into the symbol "Ln^Am" where
  62.  * n is a digit and m is a decimal number. "L" makes it a label discarded
  63.  * unless debugging and "^A"('\1') ensures no ordinary symbol SHOULD get the
  64.  * same name as a local label symbol. The first "4:" is "L4^A1" - the m
  65.  * numbers begin at 1.
  66.  */
  67.  
  68. typedef short unsigned int
  69. local_label_countT;
  70.  
  71. static local_label_countT
  72. local_label_counter[10];
  73.  
  74. static                /* Returned to caller, then copied. */
  75.   char symbol_name_build[12];    /* used for created names ("4f") */
  76.  
  77. #ifdef SUN_ASM_SYNTAX
  78. int local_label_defined[10];
  79. #endif
  80.  
  81.  
  82. void
  83. symbol_begin()
  84. {
  85.   symbol_lastP = NULL;
  86.   symbol_rootP = NULL;        /* In case we have 0 symbols (!!) */
  87.   sy_hash = hash_new();
  88.   bzero ((char *)(& abs_symbol), sizeof(abs_symbol));
  89.   abs_symbol . sy_type = N_ABS;    /* Can't initialise a union. Sigh. */
  90.   bzero ((char *)(local_label_counter), sizeof(local_label_counter) );
  91.   local_bss_counter = 0;
  92. }
  93.  
  94. /*
  95.  *            local_label_name()
  96.  *
  97.  * Caller must copy returned name: we re-use the area for the next name.
  98.  */
  99.  
  100. char *                /* Return local label name. */
  101. local_label_name(n, augend)
  102.      register int n;    /* we just saw "n:", "nf" or "nb" : n a digit */
  103.      register int augend; /* 0 for nb, 1 for n:, nf */
  104. {
  105.   register char *    p;
  106.   register char *    q;
  107.   char symbol_name_temporary[10]; /* build up a number, BACKWARDS */
  108.  
  109.   know( n >= 0 );
  110.   know( augend == 0 || augend == 1 );
  111.   p = symbol_name_build;
  112.   * p ++ = 'L';
  113.   * p ++ = n + '0';        /* Make into ASCII */
  114.   * p ++ = 1;            /* ^A */
  115.   n = local_label_counter [ n ] + augend;
  116.                 /* version number of this local label */
  117.   /*
  118.    * Next code just does sprintf( {}, "%d", n);
  119.    * It is more elegant to do the next part recursively, but a procedure
  120.    * call for each digit emitted is considered too costly.
  121.    */
  122.   q = symbol_name_temporary;
  123.   for (*q++=0; n; q++)        /* emits NOTHING if n starts as 0 */
  124.     {
  125.       know(n>0);        /* We expect n > 0 always */
  126.       *q = n % 10 + '0';
  127.       n /= 10;
  128.     }
  129.   while ( * p ++ = * -- q )
  130.     {
  131.     }
  132.   /* The label, as a '\0' ended string, starts at symbol_name_build. */
  133.   return (symbol_name_build);
  134. }
  135.  
  136.  
  137. void
  138. local_colon (n)
  139.      int        n;    /* just saw "n:" */
  140. {
  141.   local_label_counter [n] ++;
  142. #ifdef SUN_ASM_SYNTAX
  143.   local_label_defined[n]=1;
  144. #endif
  145.   colon (local_label_name (n, 0));
  146. }
  147.  
  148. /*
  149.  *            symbol_new()
  150.  *
  151.  * Return a pointer to a new symbol.
  152.  * Die if we can't make a new symbol.
  153.  * Fill in the symbol's values.
  154.  * Add symbol to end of symbol chain.
  155.  *
  156.  *
  157.  * Please always call this to create a new symbol.
  158.  *
  159.  * Changes since 1985: Symbol names may not contain '\0'. Sigh.
  160.  */
  161.  
  162. symbolS *
  163. symbol_new (name, type, other, desc, value, frag)
  164.      char *        name;    /* We copy this: OK to alter your copy. */
  165.      unsigned char    type;    /* As in <a.out.h>. */
  166.      char        other;    /* As in <a.out.h>. */
  167.      short int        desc;    /* As in <a.out.h>. */
  168.      valueT        value;    /* As in <a.out.h>, often an address. */
  169.                 /* Often used as offset from frag address. */
  170.      struct frag *    frag;    /* For sy_frag. */
  171. {
  172.   register symbolS *        symbolP;
  173.   register char *        preserved_copy_of_name;
  174.   register unsigned int        name_length;
  175.            char *        p;
  176.  
  177.   name_length = strlen(name) + 1;
  178.   obstack_grow(¬es,name,name_length);
  179.   p=obstack_finish(¬es);
  180.   /* obstack_1done( ¬es, name, name_length, &p ); */
  181.   preserved_copy_of_name = p;
  182.   p=obstack_alloc(¬es,sizeof(struct symbol));
  183.   /* obstack_1blank( ¬es, sizeof(struct symbol), &p ); */
  184.   symbolP            = (symbolS *) p;
  185.   symbolP -> sy_name        = preserved_copy_of_name;
  186.   symbolP -> sy_type        = type;
  187.   symbolP -> sy_other        = other;
  188.   symbolP -> sy_desc        = desc;
  189.   symbolP -> sy_value        = value;
  190.   symbolP -> sy_frag        = frag;
  191.   symbolP -> sy_next        = NULL;    /* End of chain. */
  192.   symbolP -> sy_forward        = NULL; /* JF */
  193. #ifdef SUSPECT
  194.   symbolP -> sy_name_offset    = ~ 0; /* Impossible offset catches errors. */
  195.   symbolP -> sy_number        = ~ 0; /* Ditto. */
  196. #endif
  197.   /*
  198.    * Link to end of symbol chain.
  199.    */
  200.   if (symbol_lastP)
  201.     {
  202.       symbol_lastP -> sy_next = symbolP;
  203.     }
  204.   else
  205.     {
  206.       symbol_rootP = symbolP;
  207.     }
  208.   symbol_lastP = symbolP;
  209.  
  210.   return (symbolP);
  211. }
  212.  
  213. /*
  214.  *            colon()
  215.  *
  216.  * We have just seen "<name>:".
  217.  * Creates a struct symbol unless it already exists.
  218.  *
  219.  * Gripes if we are redefining a symbol incompatibly (and ignores it).
  220.  *
  221.  */
  222. void
  223. colon (sym_name)        /* just seen "x:" - rattle symbols & frags */
  224.      register char *  sym_name; /* symbol name, as a cannonical string */
  225.                 /* We copy this string: OK to alter later. */
  226. {
  227.   register struct symbol * symbolP; /* symbol we are working with */
  228.  
  229. #ifdef SUN_ASM_SYNTAX
  230.   /* Sun local labes go out of scope whenever a non-local symbol is
  231.      defined.  */
  232.  
  233.   if(*sym_name !='L')
  234.     bzero((void *)local_label_defined,sizeof(local_label_defined));
  235. #endif
  236.  
  237. #ifndef WORKING_DOT_WORD
  238.   if(new_broken_words) {
  239.     struct broken_word *a;
  240.     int possible_bytes;
  241.     fragS *frag_tmp;
  242.     char *frag_opcode;
  243.     extern md_short_jump_size;
  244.     extern md_long_jump_size;
  245.  
  246.     possible_bytes=md_short_jump_size+new_broken_words*md_long_jump_size;
  247.     frag_tmp=frag_now;
  248.     frag_opcode=frag_var(rs_broken_word,possible_bytes,possible_bytes,(relax_substateT)0,(symbolS *)broken_words,(long int)0,(char *)0);
  249.  
  250.     /* We want to store the pointer to where to insert the jump table in the
  251.        fr_opcode of the rs_broken_word frag.  This requires a little hackery */
  252.     while(frag_tmp && (frag_tmp->fr_type!=rs_broken_word || frag_tmp->fr_opcode))
  253.       frag_tmp=frag_tmp->fr_next;
  254.     know(frag_tmp);
  255.     frag_tmp->fr_opcode=frag_opcode;
  256.     new_broken_words = 0;
  257.  
  258.     for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
  259.       a->dispfrag=frag_tmp;
  260.   }
  261. #endif
  262.   if (symbolP = symbol_table_lookup( sym_name ))
  263.     {
  264. #ifdef    VMS
  265.       /*
  266.        *    If the new symbol is .comm AND it has a size of zero,
  267.        *    we ignore it (i.e. the old symbol overrides it)
  268.        */
  269.       if ((seg_N_TYPE [(int) now_seg] == (N_UNDF | N_EXT)) &&
  270.       ((obstack_next_free(& frags) - frag_now -> fr_literal) == 0))
  271.         return;
  272.       /*
  273.        *    If the old symbol is .comm and it has a size of zero,
  274.        *    we override it with the new symbol value.
  275.        */
  276.       if ((symbolP -> sy_type == (N_UNDF | N_EXT)) &&
  277.       (symbolP->sy_value == 0)) {
  278.           symbolP -> sy_frag  = frag_now;
  279.           symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  280.           symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  281. #ifdef Mach_O
  282.           symbolP -> sy_other = seg_n_sect(now_seg, now_subseg);
  283. #endif Mach_O
  284.           return;
  285.       }
  286. #endif    /* VMS */
  287.       /*
  288.        *    Now check for undefined symbols
  289.        */
  290.       if ((symbolP -> sy_type & N_TYPE) == N_UNDF)
  291.     {
  292.       if(   symbolP -> sy_other == 0
  293.          && symbolP -> sy_desc  == 0
  294.          && symbolP -> sy_value == 0)
  295.         {
  296.           symbolP -> sy_frag  = frag_now;
  297.           symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  298.           know( N_UNDF == 0 );
  299.           symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  300. #ifdef Mach_O
  301.           symbolP -> sy_other = seg_n_sect(now_seg, now_subseg);
  302. #endif Mach_O
  303. #ifdef NeXT    /* generate stabs for debugging assembly code */
  304.           if(flagseen['g'])
  305.           make_stab_for_symbol(symbolP);
  306. #endif NeXT    /* generate stabs for debugging assembly code */
  307.         }
  308.       else
  309.         {
  310. #ifdef    VMS
  311.           /*
  312.            *    There are still several cases to check:
  313.            *        A .comm/.lcomm symbol being redefined as
  314.            *            initialized data is OK
  315.            *        A .comm/.lcomm symbol being redefined with
  316.            *            a larger size is also OK
  317.            */
  318.           char New_Type = seg_N_TYPE [(int) now_seg];
  319.           if (((symbolP->sy_type == (N_UNDF | N_EXT)) ||
  320.            (symbolP->sy_type == N_BSS)) &&
  321.           (((New_Type & ~N_EXT) == N_DATA) ||
  322.            (New_Type == symbolP->sy_type))) {
  323.             /*
  324.              *    Select which of the 2 cases this is
  325.              */
  326.             if (New_Type == symbolP->sy_type) {
  327.                 /*
  328.                  *    If the new size is larger we just
  329.                  *    change its value.  If the new size
  330.                  *    is smaller, we ignore this symbol
  331.                  */
  332.                 if (symbolP->sy_value <
  333.                     (obstack_next_free(& frags) -
  334.                         frag_now -> fr_literal)) {
  335.                       symbolP -> sy_value = 
  336.                     obstack_next_free(& frags) -
  337.                         frag_now -> fr_literal;
  338.                 }
  339.             } else {
  340.                 /*
  341.                  *    It is a .comm/.lcomm being converted
  342.                  *    to initialized data.
  343.                  */
  344.                 symbolP -> sy_frag  = frag_now;
  345.                 symbolP -> sy_value = obstack_next_free(& frags) - frag_now -> fr_literal;
  346.                 symbolP -> sy_type |= seg_N_TYPE [(int) now_seg]; /* keep N_EXT bit */
  347.             }
  348.           } else {
  349. #endif    /* VMS */
  350.           as_fatal( "Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
  351.               sym_name,
  352.               seg_name [(int) N_TYPE_seg [symbolP -> sy_type & N_TYPE]],
  353.               symbolP -> sy_other, symbolP -> sy_desc,
  354.               symbolP -> sy_value);
  355. #ifdef    VMS
  356.         }
  357. #endif    /* VMS */
  358.         }
  359.     }
  360.       else
  361.     {
  362.       as_fatal("Symbol %s already defined.",sym_name);
  363.     }
  364.     }
  365.   else
  366.     {
  367.       symbolP = symbol_new (sym_name,
  368.                 (unsigned char)(seg_N_TYPE [(int) now_seg]),
  369. #ifdef Mach_O
  370.                 seg_n_sect(now_seg, now_subseg),
  371. #else !defined(Mach_O)
  372.                 0,
  373. #endif Mach_O
  374.                 0,
  375.                 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
  376.                 frag_now);
  377.       symbol_table_insert (symbolP);
  378. #ifdef NeXT    /* generate stabs for debugging assembly code */
  379.       if(flagseen['g'])
  380.       make_stab_for_symbol(symbolP);
  381. #endif NeXT    /* generate stabs for debugging assembly code */
  382.     }
  383. }
  384.  
  385.  
  386. /*
  387.  *            symbol_table_insert()
  388.  *
  389.  * Die if we can't insert the symbol.
  390.  *
  391.  */
  392.  
  393. void
  394. symbol_table_insert (symbolP)
  395.      struct symbol *    symbolP;
  396. {
  397.   register char *    error_string;
  398.  
  399.   know( symbolP );
  400.   know( symbolP -> sy_name );
  401.   if ( * (error_string = hash_jam (sy_hash, symbolP -> sy_name, (char *)symbolP)))
  402.     {
  403.       as_fatal( "Inserting \"%s\" into symbol table failed: %s",
  404.           symbolP -> sy_name, error_string);
  405.     }
  406. }
  407.  
  408. /*
  409.  *            symbol_find_or_make()
  410.  *
  411.  * If a symbol name does not exist, create it as undefined, and insert
  412.  * it into the symbol table. Return a pointer to it.
  413.  */
  414. symbolS *
  415. symbol_find_or_make (name)
  416.      char *    name;
  417. {
  418.   register symbolS *    symbolP;
  419.  
  420.   symbolP = symbol_table_lookup (name);
  421.   if (symbolP == NULL)
  422.     {
  423.       symbolP = symbol_new (name, N_UNDF, 0, 0, 0, & zero_address_frag);
  424.       symbol_table_insert (symbolP);
  425.     }
  426.   return (symbolP);
  427. }
  428.  
  429. /*
  430.  *            symbol_find()
  431.  * 
  432.  * Implement symbol table lookup.
  433.  * In:    A symbol's name as a string: '\0' can't be part of a symbol name.
  434.  * Out:    NULL if the name was not in the symbol table, else the address
  435.  *    of a struct symbol associated with that name.
  436.  */
  437.  
  438. symbolS *
  439. symbol_find (name)
  440.      char *    name;
  441. {
  442.   return ( (symbolS *) hash_find( sy_hash, name ));
  443. }
  444.  
  445. #ifdef NeXT    /* generate stabs for debugging assembly code */
  446. /*
  447.  * make_stab_for_symbol() is called when -g is present for a label that is
  448.  * being defined.  If the label is a text label and in the (__TEXT,__text)
  449.  * section and not a local label create a stab for it.
  450.  * 
  451.  * See the detailed comments about stabs in read_a_source_file() for a
  452.  * description of what is going on here.
  453.  */
  454. make_stab_for_symbol(symbolP)
  455. symbolS *symbolP;
  456. {
  457.     symbolS *stab;
  458.     int stabnamelen;
  459.     char *stabname;
  460.     extern unsigned int logical_input_line;
  461.  
  462.     if(symbolP->sy_name[0] == 'L' ||
  463.        ((symbolP->sy_type && N_TYPE) == N_TEXT && symbolP->sy_other == 1) )
  464.         return;
  465.  
  466.     stabnamelen = strlen(symbolP->sy_name) + sizeof(":f3");
  467.     stabname = xmalloc(stabnamelen);
  468.     strcpy(stabname, symbolP->sy_name);
  469.     if(symbolP->sy_type & N_EXT)
  470.         strcat(stabname, ":F3");
  471.     else
  472.         strcat(stabname, ":f3");
  473.     
  474.     stab = symbol_new(
  475.         stabname,
  476.         36 /* N_FUN */,
  477.         1 /* n_sect */,
  478.         logical_input_line /* n_desc, line number */,
  479.         symbolP->sy_value,
  480.         symbolP->sy_frag);
  481.     free(stabname);
  482. }
  483. #endif NeXT    /* generate stabs for debugging assembly code */
  484.  
  485.  
  486. /* end: symbols.c */
  487.