* Makefile.in (autoconf-install): New target.
[deliverable/binutils-gdb.git] / gas / symbols.c
1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
3 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
23
24 #include <ctype.h>
25
26 #include "as.h"
27
28 #include "obstack.h" /* For "symbols.h" */
29 #include "subsegs.h"
30
31 /* This is non-zero if symbols are case sensitive, which is the
32 default. */
33 int symbols_case_sensitive = 1;
34
35 #ifndef WORKING_DOT_WORD
36 extern int new_broken_words;
37 #endif
38
39 /* symbol-name => struct symbol pointer */
40 static struct hash_control *sy_hash;
41
42 /* Below are commented in "symbols.h". */
43 symbolS *symbol_rootP;
44 symbolS *symbol_lastP;
45 symbolS abs_symbol;
46
47 #ifdef DEBUG_SYMS
48 #define debug_verify_symchain verify_symbol_chain
49 #else
50 #define debug_verify_symchain(root, last) ((void) 0)
51 #endif
52
53 struct obstack notes;
54
55 static void fb_label_init PARAMS ((void));
56 static long dollar_label_instance PARAMS ((long));
57 static long fb_label_instance PARAMS ((long));
58
59 /* symbol_new()
60
61 Return a pointer to a new symbol. Die if we can't make a new
62 symbol. Fill in the symbol's values. Add symbol to end of symbol
63 chain.
64
65 This function should be called in the general case of creating a
66 symbol. However, if the output file symbol table has already been
67 set, and you are certain that this symbol won't be wanted in the
68 output file, you can call symbol_create. */
69
70 symbolS *
71 symbol_new (name, segment, valu, frag)
72 const char *name;
73 segT segment;
74 valueT valu;
75 fragS *frag;
76 {
77 symbolS *symbolP = symbol_create (name, segment, valu, frag);
78
79 /*
80 * Link to end of symbol chain.
81 */
82 #ifdef BFD_ASSEMBLER
83 {
84 extern int symbol_table_frozen;
85 if (symbol_table_frozen)
86 abort ();
87 }
88 #endif
89 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
90
91 return symbolP;
92 }
93
94 symbolS *
95 symbol_create (name, segment, valu, frag)
96 const char *name; /* It is copied, the caller can destroy/modify */
97 segT segment; /* Segment identifier (SEG_<something>) */
98 valueT valu; /* Symbol value */
99 fragS *frag; /* Associated fragment */
100 {
101 unsigned int name_length;
102 char *preserved_copy_of_name;
103 symbolS *symbolP;
104
105 name_length = strlen (name) + 1; /* +1 for \0 */
106 obstack_grow (&notes, name, name_length);
107 preserved_copy_of_name = obstack_finish (&notes);
108 #ifdef STRIP_UNDERSCORE
109 if (preserved_copy_of_name[0] == '_')
110 preserved_copy_of_name++;
111 #endif
112
113 #ifdef tc_canonicalize_symbol_name
114 preserved_copy_of_name =
115 tc_canonicalize_symbol_name (preserved_copy_of_name);
116 #endif
117
118 if (! symbols_case_sensitive)
119 {
120 unsigned char *s;
121
122 for (s = (unsigned char *) preserved_copy_of_name; *s != '\0'; s++)
123 if (islower (*s))
124 *s = toupper (*s);
125 }
126
127 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
128
129 /* symbol must be born in some fixed state. This seems as good as any. */
130 memset (symbolP, 0, sizeof (symbolS));
131
132 #ifdef BFD_ASSEMBLER
133 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
134 if (symbolP->bsym == NULL)
135 as_perror ("%s", "bfd_make_empty_symbol");
136 symbolP->bsym->udata.p = (PTR) symbolP;
137 #endif
138 S_SET_NAME (symbolP, preserved_copy_of_name);
139
140 S_SET_SEGMENT (symbolP, segment);
141 S_SET_VALUE (symbolP, valu);
142 symbol_clear_list_pointers (symbolP);
143
144 symbolP->sy_frag = frag;
145 #ifndef BFD_ASSEMBLER
146 symbolP->sy_number = ~0;
147 symbolP->sy_name_offset = (unsigned int) ~0;
148 #endif
149
150 obj_symbol_new_hook (symbolP);
151
152 #ifdef tc_symbol_new_hook
153 tc_symbol_new_hook (symbolP);
154 #endif
155
156 return symbolP;
157 }
158 \f
159
160 /*
161 * colon()
162 *
163 * We have just seen "<name>:".
164 * Creates a struct symbol unless it already exists.
165 *
166 * Gripes if we are redefining a symbol incompatibly (and ignores it).
167 *
168 */
169 symbolS *
170 colon (sym_name) /* just seen "x:" - rattle symbols & frags */
171 const char *sym_name; /* symbol name, as a cannonical string */
172 /* We copy this string: OK to alter later. */
173 {
174 register symbolS *symbolP; /* symbol we are working with */
175
176 /* Sun local labels go out of scope whenever a non-local symbol is
177 defined. */
178 if (LOCAL_LABELS_DOLLAR)
179 {
180 int local;
181
182 #ifdef BFD_ASSEMBLER
183 local = bfd_is_local_label_name (stdoutput, sym_name);
184 #else
185 local = LOCAL_LABEL (sym_name);
186 #endif
187
188 if (! local)
189 dollar_label_clear ();
190 }
191
192 #ifndef WORKING_DOT_WORD
193 if (new_broken_words)
194 {
195 struct broken_word *a;
196 int possible_bytes;
197 fragS *frag_tmp;
198 char *frag_opcode;
199
200 extern const int md_short_jump_size;
201 extern const int md_long_jump_size;
202 possible_bytes = (md_short_jump_size
203 + new_broken_words * md_long_jump_size);
204
205 frag_tmp = frag_now;
206 frag_opcode = frag_var (rs_broken_word,
207 possible_bytes,
208 possible_bytes,
209 (relax_substateT) 0,
210 (symbolS *) broken_words,
211 (offsetT) 0,
212 NULL);
213
214 /* We want to store the pointer to where to insert the jump table in the
215 fr_opcode of the rs_broken_word frag. This requires a little
216 hackery. */
217 while (frag_tmp
218 && (frag_tmp->fr_type != rs_broken_word
219 || frag_tmp->fr_opcode))
220 frag_tmp = frag_tmp->fr_next;
221 know (frag_tmp);
222 frag_tmp->fr_opcode = frag_opcode;
223 new_broken_words = 0;
224
225 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
226 a->dispfrag = frag_tmp;
227 }
228 #endif /* WORKING_DOT_WORD */
229
230 if ((symbolP = symbol_find (sym_name)) != 0)
231 {
232 #ifdef RESOLVE_SYMBOL_REDEFINITION
233 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
234 return symbolP;
235 #endif
236 /*
237 * Now check for undefined symbols
238 */
239 if (!S_IS_DEFINED (symbolP))
240 {
241 if (S_GET_VALUE (symbolP) == 0)
242 {
243 symbolP->sy_frag = frag_now;
244 #ifdef OBJ_VMS
245 S_SET_OTHER(symbolP, const_flag);
246 #endif
247 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
248 S_SET_SEGMENT (symbolP, now_seg);
249 #ifdef N_UNDF
250 know (N_UNDF == 0);
251 #endif /* if we have one, it better be zero. */
252
253 }
254 else
255 {
256 /*
257 * There are still several cases to check:
258 * A .comm/.lcomm symbol being redefined as
259 * initialized data is OK
260 * A .comm/.lcomm symbol being redefined with
261 * a larger size is also OK
262 *
263 * This only used to be allowed on VMS gas, but Sun cc
264 * on the sparc also depends on it.
265 */
266
267 if (((!S_IS_DEBUG (symbolP)
268 && !S_IS_DEFINED (symbolP)
269 && S_IS_EXTERNAL (symbolP))
270 || S_GET_SEGMENT (symbolP) == bss_section)
271 && (now_seg == data_section
272 || now_seg == S_GET_SEGMENT (symbolP)))
273 {
274 /*
275 * Select which of the 2 cases this is
276 */
277 if (now_seg != data_section)
278 {
279 /*
280 * New .comm for prev .comm symbol.
281 * If the new size is larger we just
282 * change its value. If the new size
283 * is smaller, we ignore this symbol
284 */
285 if (S_GET_VALUE (symbolP)
286 < ((unsigned) frag_now_fix ()))
287 {
288 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
289 }
290 }
291 else
292 {
293 /* It is a .comm/.lcomm being converted to initialized
294 data. */
295 symbolP->sy_frag = frag_now;
296 #ifdef OBJ_VMS
297 S_SET_OTHER(symbolP, const_flag);
298 #endif
299 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
300 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
301 }
302 }
303 else
304 {
305 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
306 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
307 sym_name,
308 segment_name (S_GET_SEGMENT (symbolP)),
309 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
310 (long) S_GET_VALUE (symbolP));
311 #else
312 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
313 sym_name,
314 segment_name (S_GET_SEGMENT (symbolP)),
315 (long) S_GET_VALUE (symbolP));
316 #endif
317 }
318 } /* if the undefined symbol has no value */
319 }
320 else
321 {
322 /* Don't blow up if the definition is the same */
323 if (!(frag_now == symbolP->sy_frag
324 && S_GET_VALUE (symbolP) == frag_now_fix ()
325 && S_GET_SEGMENT (symbolP) == now_seg))
326 as_fatal ("Symbol %s already defined.", sym_name);
327 } /* if this symbol is not yet defined */
328
329 }
330 else
331 {
332 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
333 frag_now);
334 #ifdef OBJ_VMS
335 S_SET_OTHER (symbolP, const_flag);
336 #endif /* OBJ_VMS */
337
338 symbol_table_insert (symbolP);
339 } /* if we have seen this symbol before */
340
341 if (mri_common_symbol != NULL)
342 {
343 /* This symbol is actually being defined within an MRI common
344 section. This requires special handling. */
345 symbolP->sy_value.X_op = O_symbol;
346 symbolP->sy_value.X_add_symbol = mri_common_symbol;
347 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
348 symbolP->sy_frag = &zero_address_frag;
349 S_SET_SEGMENT (symbolP, expr_section);
350 symbolP->sy_mri_common = 1;
351 }
352
353 #ifdef tc_frob_label
354 tc_frob_label (symbolP);
355 #endif
356
357 return symbolP;
358 }
359 \f
360
361 /*
362 * symbol_table_insert()
363 *
364 * Die if we can't insert the symbol.
365 *
366 */
367
368 void
369 symbol_table_insert (symbolP)
370 symbolS *symbolP;
371 {
372 register const char *error_string;
373
374 know (symbolP);
375 know (S_GET_NAME (symbolP));
376
377 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
378 {
379 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
380 S_GET_NAME (symbolP), error_string);
381 } /* on error */
382 } /* symbol_table_insert() */
383 \f
384 /*
385 * symbol_find_or_make()
386 *
387 * If a symbol name does not exist, create it as undefined, and insert
388 * it into the symbol table. Return a pointer to it.
389 */
390 symbolS *
391 symbol_find_or_make (name)
392 const char *name;
393 {
394 register symbolS *symbolP;
395
396 symbolP = symbol_find (name);
397
398 if (symbolP == NULL)
399 {
400 symbolP = symbol_make (name);
401
402 symbol_table_insert (symbolP);
403 } /* if symbol wasn't found */
404
405 return (symbolP);
406 } /* symbol_find_or_make() */
407
408 symbolS *
409 symbol_make (name)
410 CONST char *name;
411 {
412 symbolS *symbolP;
413
414 /* Let the machine description default it, e.g. for register names. */
415 symbolP = md_undefined_symbol ((char *) name);
416
417 if (!symbolP)
418 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
419
420 return (symbolP);
421 } /* symbol_make() */
422
423 /*
424 * symbol_find()
425 *
426 * Implement symbol table lookup.
427 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
428 * Out: NULL if the name was not in the symbol table, else the address
429 * of a struct symbol associated with that name.
430 */
431
432 symbolS *
433 symbol_find (name)
434 CONST char *name;
435 {
436 #ifdef STRIP_UNDERSCORE
437 return (symbol_find_base (name, 1));
438 #else /* STRIP_UNDERSCORE */
439 return (symbol_find_base (name, 0));
440 #endif /* STRIP_UNDERSCORE */
441 } /* symbol_find() */
442
443 symbolS *
444 symbol_find_base (name, strip_underscore)
445 CONST char *name;
446 int strip_underscore;
447 {
448 if (strip_underscore && *name == '_')
449 name++;
450
451 #ifdef tc_canonicalize_symbol_name
452 {
453 char *copy;
454
455 copy = (char *) alloca (strlen (name) + 1);
456 strcpy (copy, name);
457 name = tc_canonicalize_symbol_name (copy);
458 }
459 #endif
460
461 if (! symbols_case_sensitive)
462 {
463 unsigned char *copy;
464
465 copy = (unsigned char *) alloca (strlen (name) + 1);
466 name = (const char *) copy;
467 for (; *copy != '\0'; copy++)
468 if (islower (*copy))
469 *copy = toupper (*copy);
470 }
471
472 return ((symbolS *) hash_find (sy_hash, name));
473 }
474
475 /*
476 * Once upon a time, symbols were kept in a singly linked list. At
477 * least coff needs to be able to rearrange them from time to time, for
478 * which a doubly linked list is much more convenient. Loic did these
479 * as macros which seemed dangerous to me so they're now functions.
480 * xoxorich.
481 */
482
483 /* Link symbol ADDME after symbol TARGET in the chain. */
484 void
485 symbol_append (addme, target, rootPP, lastPP)
486 symbolS *addme;
487 symbolS *target;
488 symbolS **rootPP;
489 symbolS **lastPP;
490 {
491 if (target == NULL)
492 {
493 know (*rootPP == NULL);
494 know (*lastPP == NULL);
495 addme->sy_next = NULL;
496 #ifdef SYMBOLS_NEED_BACKPOINTERS
497 addme->sy_previous = NULL;
498 #endif
499 *rootPP = addme;
500 *lastPP = addme;
501 return;
502 } /* if the list is empty */
503
504 if (target->sy_next != NULL)
505 {
506 #ifdef SYMBOLS_NEED_BACKPOINTERS
507 target->sy_next->sy_previous = addme;
508 #endif /* SYMBOLS_NEED_BACKPOINTERS */
509 }
510 else
511 {
512 know (*lastPP == target);
513 *lastPP = addme;
514 } /* if we have a next */
515
516 addme->sy_next = target->sy_next;
517 target->sy_next = addme;
518
519 #ifdef SYMBOLS_NEED_BACKPOINTERS
520 addme->sy_previous = target;
521 #endif /* SYMBOLS_NEED_BACKPOINTERS */
522
523 debug_verify_symchain (symbol_rootP, symbol_lastP);
524 }
525
526 /* Set the chain pointers of SYMBOL to null. */
527 void
528 symbol_clear_list_pointers (symbolP)
529 symbolS *symbolP;
530 {
531 symbolP->sy_next = NULL;
532 #ifdef SYMBOLS_NEED_BACKPOINTERS
533 symbolP->sy_previous = NULL;
534 #endif
535 }
536
537 #ifdef SYMBOLS_NEED_BACKPOINTERS
538 /* Remove SYMBOLP from the list. */
539 void
540 symbol_remove (symbolP, rootPP, lastPP)
541 symbolS *symbolP;
542 symbolS **rootPP;
543 symbolS **lastPP;
544 {
545 if (symbolP == *rootPP)
546 {
547 *rootPP = symbolP->sy_next;
548 } /* if it was the root */
549
550 if (symbolP == *lastPP)
551 {
552 *lastPP = symbolP->sy_previous;
553 } /* if it was the tail */
554
555 if (symbolP->sy_next != NULL)
556 {
557 symbolP->sy_next->sy_previous = symbolP->sy_previous;
558 } /* if not last */
559
560 if (symbolP->sy_previous != NULL)
561 {
562 symbolP->sy_previous->sy_next = symbolP->sy_next;
563 } /* if not first */
564
565 debug_verify_symchain (*rootPP, *lastPP);
566 }
567
568 /* Link symbol ADDME before symbol TARGET in the chain. */
569 void
570 symbol_insert (addme, target, rootPP, lastPP)
571 symbolS *addme;
572 symbolS *target;
573 symbolS **rootPP;
574 symbolS **lastPP;
575 {
576 if (target->sy_previous != NULL)
577 {
578 target->sy_previous->sy_next = addme;
579 }
580 else
581 {
582 know (*rootPP == target);
583 *rootPP = addme;
584 } /* if not first */
585
586 addme->sy_previous = target->sy_previous;
587 target->sy_previous = addme;
588 addme->sy_next = target;
589
590 debug_verify_symchain (*rootPP, *lastPP);
591 }
592
593 #endif /* SYMBOLS_NEED_BACKPOINTERS */
594
595 void
596 verify_symbol_chain (rootP, lastP)
597 symbolS *rootP;
598 symbolS *lastP;
599 {
600 symbolS *symbolP = rootP;
601
602 if (symbolP == NULL)
603 return;
604
605 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
606 {
607 #ifdef SYMBOLS_NEED_BACKPOINTERS
608 assert (symbolP->sy_next->sy_previous == symbolP);
609 #else
610 /* Walk the list anyways, to make sure pointers are still good. */
611 ;
612 #endif /* SYMBOLS_NEED_BACKPOINTERS */
613 }
614
615 assert (lastP == symbolP);
616 }
617
618 void
619 verify_symbol_chain_2 (sym)
620 symbolS *sym;
621 {
622 symbolS *p = sym, *n = sym;
623 #ifdef SYMBOLS_NEED_BACKPOINTERS
624 while (symbol_previous (p))
625 p = symbol_previous (p);
626 #endif
627 while (symbol_next (n))
628 n = symbol_next (n);
629 verify_symbol_chain (p, n);
630 }
631
632 /* Resolve the value of a symbol. This is called during the final
633 pass over the symbol table to resolve any symbols with complex
634 values. */
635
636 void
637 resolve_symbol_value (symp)
638 symbolS *symp;
639 {
640 int resolved;
641
642 if (symp->sy_resolved)
643 return;
644
645 resolved = 0;
646
647 if (symp->sy_resolving)
648 {
649 as_bad ("Symbol definition loop encountered at %s",
650 S_GET_NAME (symp));
651 S_SET_VALUE (symp, (valueT) 0);
652 resolved = 1;
653 }
654 else
655 {
656 offsetT left, right, val;
657 segT seg_left, seg_right;
658
659 symp->sy_resolving = 1;
660
661 /* Simplify addition or subtraction of a constant by folding the
662 constant into X_add_number. */
663 if (symp->sy_value.X_op == O_add
664 || symp->sy_value.X_op == O_subtract)
665 {
666 resolve_symbol_value (symp->sy_value.X_add_symbol);
667 resolve_symbol_value (symp->sy_value.X_op_symbol);
668 if (S_GET_SEGMENT (symp->sy_value.X_op_symbol) == absolute_section)
669 {
670 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
671 if (symp->sy_value.X_op == O_add)
672 symp->sy_value.X_add_number += right;
673 else
674 symp->sy_value.X_add_number -= right;
675 symp->sy_value.X_op = O_symbol;
676 symp->sy_value.X_op_symbol = NULL;
677 }
678 else if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
679 == absolute_section)
680 && symp->sy_value.X_op == O_add)
681 {
682 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
683 symp->sy_value.X_add_symbol = symp->sy_value.X_op_symbol;
684 symp->sy_value.X_add_number += left;
685 symp->sy_value.X_op = O_symbol;
686 symp->sy_value.X_op_symbol = NULL;
687 }
688 }
689
690 switch (symp->sy_value.X_op)
691 {
692 case O_absent:
693 S_SET_VALUE (symp, 0);
694 /* Fall through. */
695 case O_constant:
696 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
697 if (S_GET_SEGMENT (symp) == expr_section)
698 S_SET_SEGMENT (symp, absolute_section);
699 resolved = 1;
700 break;
701
702 case O_symbol:
703 resolve_symbol_value (symp->sy_value.X_add_symbol);
704
705 if (symp->sy_mri_common)
706 {
707 /* This is a symbol inside an MRI common section. The
708 relocation routines are going to handle it specially.
709 Don't change the value. */
710 S_SET_VALUE (symp, symp->sy_value.X_add_number);
711 resolved = symp->sy_value.X_add_symbol->sy_resolved;
712 break;
713 }
714
715 if (symp->sy_value.X_add_number == 0)
716 copy_symbol_attributes (symp, symp->sy_value.X_add_symbol);
717
718 /* If we have equated this symbol to an undefined symbol, we
719 keep X_op set to O_symbol, and we don't change
720 X_add_number. This permits the routine which writes out
721 relocation to detect this case, and convert the
722 relocation to be against the symbol to which this symbol
723 is equated. */
724 if (! S_IS_DEFINED (symp->sy_value.X_add_symbol)
725 || S_IS_COMMON (symp->sy_value.X_add_symbol))
726 {
727 symp->sy_value.X_op = O_symbol;
728 S_SET_SEGMENT (symp,
729 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
730 }
731 else
732 {
733 S_SET_VALUE (symp,
734 (symp->sy_value.X_add_number
735 + symp->sy_frag->fr_address
736 + S_GET_VALUE (symp->sy_value.X_add_symbol)));
737 if (S_GET_SEGMENT (symp) == expr_section
738 || S_GET_SEGMENT (symp) == undefined_section)
739 S_SET_SEGMENT (symp,
740 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
741 }
742
743 resolved = symp->sy_value.X_add_symbol->sy_resolved;
744 break;
745
746 case O_uminus:
747 case O_bit_not:
748 case O_logical_not:
749 resolve_symbol_value (symp->sy_value.X_add_symbol);
750 if (symp->sy_value.X_op == O_uminus)
751 val = - S_GET_VALUE (symp->sy_value.X_add_symbol);
752 else if (symp->sy_value.X_op == O_logical_not)
753 val = ! S_GET_VALUE (symp->sy_value.X_add_symbol);
754 else
755 val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol);
756 S_SET_VALUE (symp,
757 (val
758 + symp->sy_value.X_add_number
759 + symp->sy_frag->fr_address));
760 if (S_GET_SEGMENT (symp) == expr_section
761 || S_GET_SEGMENT (symp) == undefined_section)
762 S_SET_SEGMENT (symp, absolute_section);
763 resolved = symp->sy_value.X_add_symbol->sy_resolved;
764 break;
765
766 case O_multiply:
767 case O_divide:
768 case O_modulus:
769 case O_left_shift:
770 case O_right_shift:
771 case O_bit_inclusive_or:
772 case O_bit_or_not:
773 case O_bit_exclusive_or:
774 case O_bit_and:
775 case O_add:
776 case O_subtract:
777 case O_eq:
778 case O_ne:
779 case O_lt:
780 case O_le:
781 case O_ge:
782 case O_gt:
783 case O_logical_and:
784 case O_logical_or:
785 resolve_symbol_value (symp->sy_value.X_add_symbol);
786 resolve_symbol_value (symp->sy_value.X_op_symbol);
787 seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
788 seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
789 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
790 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
791
792 /* Subtraction is permitted if both operands are in the same
793 section. Otherwise, both operands must be absolute. We
794 already handled the case of addition or subtraction of a
795 constant above. This will probably need to be changed
796 for an object file format which supports arbitrary
797 expressions, such as IEEE-695. */
798 if ((seg_left != absolute_section
799 || seg_right != absolute_section)
800 && (symp->sy_value.X_op != O_subtract
801 || seg_left != seg_right))
802 {
803 char *file;
804 unsigned int line;
805
806 if (expr_symbol_where (symp, &file, &line))
807 {
808 if (seg_left == undefined_section)
809 as_bad_where (file, line,
810 "undefined symbol %s in operation",
811 S_GET_NAME (symp->sy_value.X_add_symbol));
812 if (seg_right == undefined_section)
813 as_bad_where (file, line,
814 "undefined symbol %s in operation",
815 S_GET_NAME (symp->sy_value.X_op_symbol));
816 if (seg_left != undefined_section
817 && seg_right != undefined_section)
818 as_bad_where (file, line, "invalid section for operation");
819 }
820 else
821 {
822 if (seg_left == undefined_section)
823 as_bad ("undefined symbol %s in operation setting %s",
824 S_GET_NAME (symp->sy_value.X_add_symbol),
825 S_GET_NAME (symp));
826 if (seg_right == undefined_section)
827 as_bad ("undefined symbol %s in operation setting %s",
828 S_GET_NAME (symp->sy_value.X_op_symbol),
829 S_GET_NAME (symp));
830 if (seg_left != undefined_section
831 && seg_right != undefined_section)
832 as_bad ("invalid section for operation setting %s",
833 S_GET_NAME (symp));
834 }
835 }
836
837 switch (symp->sy_value.X_op)
838 {
839 case O_multiply: val = left * right; break;
840 case O_divide: val = left / right; break;
841 case O_modulus: val = left % right; break;
842 case O_left_shift: val = left << right; break;
843 case O_right_shift: val = left >> right; break;
844 case O_bit_inclusive_or: val = left | right; break;
845 case O_bit_or_not: val = left |~ right; break;
846 case O_bit_exclusive_or: val = left ^ right; break;
847 case O_bit_and: val = left & right; break;
848 case O_add: val = left + right; break;
849 case O_subtract: val = left - right; break;
850 case O_eq: val = left == right ? ~ (offsetT) 0 : 0;
851 case O_ne: val = left != right ? ~ (offsetT) 0 : 0;
852 case O_lt: val = left < right ? ~ (offsetT) 0 : 0;
853 case O_le: val = left <= right ? ~ (offsetT) 0 : 0;
854 case O_ge: val = left >= right ? ~ (offsetT) 0 : 0;
855 case O_gt: val = left > right ? ~ (offsetT) 0 : 0;
856 case O_logical_and: val = left && right; break;
857 case O_logical_or: val = left || right; break;
858 default: abort ();
859 }
860 S_SET_VALUE (symp,
861 (symp->sy_value.X_add_number
862 + symp->sy_frag->fr_address
863 + val));
864 if (S_GET_SEGMENT (symp) == expr_section
865 || S_GET_SEGMENT (symp) == undefined_section)
866 S_SET_SEGMENT (symp, absolute_section);
867 resolved = (symp->sy_value.X_add_symbol->sy_resolved
868 && symp->sy_value.X_op_symbol->sy_resolved);
869 break;
870
871 case O_register:
872 case O_big:
873 case O_illegal:
874 /* Give an error (below) if not in expr_section. We don't
875 want to worry about expr_section symbols, because they
876 are fictional (they are created as part of expression
877 resolution), and any problems may not actually mean
878 anything. */
879 break;
880 }
881 }
882
883 /* Don't worry if we can't resolve an expr_section symbol. */
884 if (resolved)
885 symp->sy_resolved = 1;
886 else if (S_GET_SEGMENT (symp) != expr_section)
887 {
888 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp));
889 symp->sy_resolved = 1;
890 }
891 }
892
893 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
894 They are *really* local. That is, they go out of scope whenever we see a
895 label that isn't local. Also, like fb labels, there can be multiple
896 instances of a dollar label. Therefor, we name encode each instance with
897 the instance number, keep a list of defined symbols separate from the real
898 symbol table, and we treat these buggers as a sparse array. */
899
900 static long *dollar_labels;
901 static long *dollar_label_instances;
902 static char *dollar_label_defines;
903 static long dollar_label_count;
904 static unsigned long dollar_label_max;
905
906 int
907 dollar_label_defined (label)
908 long label;
909 {
910 long *i;
911
912 know ((dollar_labels != NULL) || (dollar_label_count == 0));
913
914 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
915 if (*i == label)
916 return dollar_label_defines[i - dollar_labels];
917
918 /* if we get here, label isn't defined */
919 return 0;
920 } /* dollar_label_defined() */
921
922 static long
923 dollar_label_instance (label)
924 long label;
925 {
926 long *i;
927
928 know ((dollar_labels != NULL) || (dollar_label_count == 0));
929
930 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
931 if (*i == label)
932 return (dollar_label_instances[i - dollar_labels]);
933
934 /* If we get here, we haven't seen the label before, therefore its instance
935 count is zero. */
936 return 0;
937 }
938
939 void
940 dollar_label_clear ()
941 {
942 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
943 }
944
945 #define DOLLAR_LABEL_BUMP_BY 10
946
947 void
948 define_dollar_label (label)
949 long label;
950 {
951 long *i;
952
953 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
954 if (*i == label)
955 {
956 ++dollar_label_instances[i - dollar_labels];
957 dollar_label_defines[i - dollar_labels] = 1;
958 return;
959 }
960
961 /* if we get to here, we don't have label listed yet. */
962
963 if (dollar_labels == NULL)
964 {
965 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
966 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
967 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
968 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
969 dollar_label_count = 0;
970 }
971 else if (dollar_label_count == dollar_label_max)
972 {
973 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
974 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
975 dollar_label_max * sizeof (long));
976 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
977 dollar_label_max * sizeof (long));
978 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
979 } /* if we needed to grow */
980
981 dollar_labels[dollar_label_count] = label;
982 dollar_label_instances[dollar_label_count] = 1;
983 dollar_label_defines[dollar_label_count] = 1;
984 ++dollar_label_count;
985 }
986
987 /*
988 * dollar_label_name()
989 *
990 * Caller must copy returned name: we re-use the area for the next name.
991 *
992 * The mth occurence of label n: is turned into the symbol "Ln^Am"
993 * where n is the label number and m is the instance number. "L" makes
994 * it a label discarded unless debugging and "^A"('\1') ensures no
995 * ordinary symbol SHOULD get the same name as a local label
996 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
997 *
998 * fb labels get the same treatment, except that ^B is used in place of ^A.
999 */
1000
1001 char * /* Return local label name. */
1002 dollar_label_name (n, augend)
1003 register long n; /* we just saw "n$:" : n a number */
1004 register int augend; /* 0 for current instance, 1 for new instance */
1005 {
1006 long i;
1007 /* Returned to caller, then copied. used for created names ("4f") */
1008 static char symbol_name_build[24];
1009 register char *p;
1010 register char *q;
1011 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1012
1013 know (n >= 0);
1014 know (augend == 0 || augend == 1);
1015 p = symbol_name_build;
1016 *p++ = 'L';
1017
1018 /* Next code just does sprintf( {}, "%d", n); */
1019 /* label number */
1020 q = symbol_name_temporary;
1021 for (*q++ = 0, i = n; i; ++q)
1022 {
1023 *q = i % 10 + '0';
1024 i /= 10;
1025 }
1026 while ((*p = *--q) != '\0')
1027 ++p;
1028
1029 *p++ = 1; /* ^A */
1030
1031 /* instance number */
1032 q = symbol_name_temporary;
1033 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1034 {
1035 *q = i % 10 + '0';
1036 i /= 10;
1037 }
1038 while ((*p++ = *--q) != '\0');;
1039
1040 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1041 return symbol_name_build;
1042 }
1043
1044 /*
1045 * Sombody else's idea of local labels. They are made by "n:" where n
1046 * is any decimal digit. Refer to them with
1047 * "nb" for previous (backward) n:
1048 * or "nf" for next (forward) n:.
1049 *
1050 * We do a little better and let n be any number, not just a single digit, but
1051 * since the other guy's assembler only does ten, we treat the first ten
1052 * specially.
1053 *
1054 * Like someone else's assembler, we have one set of local label counters for
1055 * entire assembly, not one set per (sub)segment like in most assemblers. This
1056 * implies that one can refer to a label in another segment, and indeed some
1057 * crufty compilers have done just that.
1058 *
1059 * Since there could be a LOT of these things, treat them as a sparse array.
1060 */
1061
1062 #define FB_LABEL_SPECIAL (10)
1063
1064 static long fb_low_counter[FB_LABEL_SPECIAL];
1065 static long *fb_labels;
1066 static long *fb_label_instances;
1067 static long fb_label_count;
1068 static long fb_label_max;
1069
1070 /* this must be more than FB_LABEL_SPECIAL */
1071 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1072
1073 static void
1074 fb_label_init ()
1075 {
1076 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1077 } /* fb_label_init() */
1078
1079 /* add one to the instance number of this fb label */
1080 void
1081 fb_label_instance_inc (label)
1082 long label;
1083 {
1084 long *i;
1085
1086 if (label < FB_LABEL_SPECIAL)
1087 {
1088 ++fb_low_counter[label];
1089 return;
1090 }
1091
1092 if (fb_labels != NULL)
1093 {
1094 for (i = fb_labels + FB_LABEL_SPECIAL;
1095 i < fb_labels + fb_label_count; ++i)
1096 {
1097 if (*i == label)
1098 {
1099 ++fb_label_instances[i - fb_labels];
1100 return;
1101 } /* if we find it */
1102 } /* for each existing label */
1103 }
1104
1105 /* if we get to here, we don't have label listed yet. */
1106
1107 if (fb_labels == NULL)
1108 {
1109 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1110 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1111 fb_label_max = FB_LABEL_BUMP_BY;
1112 fb_label_count = FB_LABEL_SPECIAL;
1113
1114 }
1115 else if (fb_label_count == fb_label_max)
1116 {
1117 fb_label_max += FB_LABEL_BUMP_BY;
1118 fb_labels = (long *) xrealloc ((char *) fb_labels,
1119 fb_label_max * sizeof (long));
1120 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1121 fb_label_max * sizeof (long));
1122 } /* if we needed to grow */
1123
1124 fb_labels[fb_label_count] = label;
1125 fb_label_instances[fb_label_count] = 1;
1126 ++fb_label_count;
1127 }
1128
1129 static long
1130 fb_label_instance (label)
1131 long label;
1132 {
1133 long *i;
1134
1135 if (label < FB_LABEL_SPECIAL)
1136 {
1137 return (fb_low_counter[label]);
1138 }
1139
1140 if (fb_labels != NULL)
1141 {
1142 for (i = fb_labels + FB_LABEL_SPECIAL;
1143 i < fb_labels + fb_label_count; ++i)
1144 {
1145 if (*i == label)
1146 {
1147 return (fb_label_instances[i - fb_labels]);
1148 } /* if we find it */
1149 } /* for each existing label */
1150 }
1151
1152 /* We didn't find the label, so this must be a reference to the
1153 first instance. */
1154 return 0;
1155 }
1156
1157 /*
1158 * fb_label_name()
1159 *
1160 * Caller must copy returned name: we re-use the area for the next name.
1161 *
1162 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1163 * where n is the label number and m is the instance number. "L" makes
1164 * it a label discarded unless debugging and "^B"('\2') ensures no
1165 * ordinary symbol SHOULD get the same name as a local label
1166 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1167 *
1168 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1169
1170 char * /* Return local label name. */
1171 fb_label_name (n, augend)
1172 long n; /* we just saw "n:", "nf" or "nb" : n a number */
1173 long augend; /* 0 for nb, 1 for n:, nf */
1174 {
1175 long i;
1176 /* Returned to caller, then copied. used for created names ("4f") */
1177 static char symbol_name_build[24];
1178 register char *p;
1179 register char *q;
1180 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1181
1182 know (n >= 0);
1183 know (augend == 0 || augend == 1);
1184 p = symbol_name_build;
1185 *p++ = 'L';
1186
1187 /* Next code just does sprintf( {}, "%d", n); */
1188 /* label number */
1189 q = symbol_name_temporary;
1190 for (*q++ = 0, i = n; i; ++q)
1191 {
1192 *q = i % 10 + '0';
1193 i /= 10;
1194 }
1195 while ((*p = *--q) != '\0')
1196 ++p;
1197
1198 *p++ = 2; /* ^B */
1199
1200 /* instance number */
1201 q = symbol_name_temporary;
1202 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1203 {
1204 *q = i % 10 + '0';
1205 i /= 10;
1206 }
1207 while ((*p++ = *--q) != '\0');;
1208
1209 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1210 return (symbol_name_build);
1211 } /* fb_label_name() */
1212
1213 /*
1214 * decode name that may have been generated by foo_label_name() above. If
1215 * the name wasn't generated by foo_label_name(), then return it unaltered.
1216 * This is used for error messages.
1217 */
1218
1219 char *
1220 decode_local_label_name (s)
1221 char *s;
1222 {
1223 char *p;
1224 char *symbol_decode;
1225 int label_number;
1226 int instance_number;
1227 char *type;
1228 const char *message_format = "\"%d\" (instance number %d of a %s label)";
1229
1230 if (s[0] != 'L')
1231 return s;
1232
1233 for (label_number = 0, p = s + 1; isdigit (*p); ++p)
1234 label_number = (10 * label_number) + *p - '0';
1235
1236 if (*p == 1)
1237 type = "dollar";
1238 else if (*p == 2)
1239 type = "fb";
1240 else
1241 return s;
1242
1243 for (instance_number = 0, p++; isdigit (*p); ++p)
1244 instance_number = (10 * instance_number) + *p - '0';
1245
1246 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1247 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1248
1249 return symbol_decode;
1250 }
1251
1252 /* Get the value of a symbol. */
1253
1254 valueT
1255 S_GET_VALUE (s)
1256 symbolS *s;
1257 {
1258 if (!s->sy_resolved && !s->sy_resolving && s->sy_value.X_op != O_constant)
1259 resolve_symbol_value (s);
1260 if (s->sy_value.X_op != O_constant)
1261 {
1262 static symbolS *recur;
1263
1264 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1265 may call S_GET_VALUE. We use a static symbol to avoid the
1266 immediate recursion. */
1267 if (recur == s)
1268 return (valueT) s->sy_value.X_add_number;
1269 recur = s;
1270 if (! s->sy_resolved
1271 || s->sy_value.X_op != O_symbol
1272 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1273 as_bad ("Attempt to get value of unresolved symbol %s",
1274 S_GET_NAME (s));
1275 recur = NULL;
1276 }
1277 return (valueT) s->sy_value.X_add_number;
1278 }
1279
1280 /* Set the value of a symbol. */
1281
1282 void
1283 S_SET_VALUE (s, val)
1284 symbolS *s;
1285 valueT val;
1286 {
1287 s->sy_value.X_op = O_constant;
1288 s->sy_value.X_add_number = (offsetT) val;
1289 s->sy_value.X_unsigned = 0;
1290 }
1291
1292 void
1293 copy_symbol_attributes (dest, src)
1294 symbolS *dest, *src;
1295 {
1296 #ifdef BFD_ASSEMBLER
1297 /* In an expression, transfer the settings of these flags.
1298 The user can override later, of course. */
1299 #define COPIED_SYMFLAGS (BSF_FUNCTION)
1300 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1301 #endif
1302
1303 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1304 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1305 #endif
1306 }
1307
1308 #ifdef BFD_ASSEMBLER
1309
1310 int
1311 S_IS_EXTERNAL (s)
1312 symbolS *s;
1313 {
1314 flagword flags = s->bsym->flags;
1315
1316 /* sanity check */
1317 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
1318 abort ();
1319
1320 return (flags & BSF_GLOBAL) != 0;
1321 }
1322
1323 int
1324 S_IS_WEAK (s)
1325 symbolS *s;
1326 {
1327 return (s->bsym->flags & BSF_WEAK) != 0;
1328 }
1329
1330 int
1331 S_IS_COMMON (s)
1332 symbolS *s;
1333 {
1334 return bfd_is_com_section (s->bsym->section);
1335 }
1336
1337 int
1338 S_IS_DEFINED (s)
1339 symbolS *s;
1340 {
1341 return s->bsym->section != undefined_section;
1342 }
1343
1344 int
1345 S_IS_DEBUG (s)
1346 symbolS *s;
1347 {
1348 if (s->bsym->flags & BSF_DEBUGGING)
1349 return 1;
1350 return 0;
1351 }
1352
1353 int
1354 S_IS_LOCAL (s)
1355 symbolS *s;
1356 {
1357 flagword flags = s->bsym->flags;
1358 const char *name;
1359
1360 /* sanity check */
1361 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
1362 abort ();
1363
1364 if (bfd_get_section (s->bsym) == reg_section)
1365 return 1;
1366
1367 name = S_GET_NAME (s);
1368 return (name != NULL
1369 && ! S_IS_DEBUG (s)
1370 && (strchr (name, '\001')
1371 || strchr (name, '\002')
1372 || (! flag_keep_locals
1373 && (bfd_is_local_label (stdoutput, s->bsym)
1374 || (flag_mri
1375 && name[0] == '?'
1376 && name[1] == '?')))));
1377 }
1378
1379 int
1380 S_IS_EXTERN (s)
1381 symbolS *s;
1382 {
1383 return S_IS_EXTERNAL (s);
1384 }
1385
1386 int
1387 S_IS_STABD (s)
1388 symbolS *s;
1389 {
1390 return S_GET_NAME (s) == 0;
1391 }
1392
1393 CONST char *
1394 S_GET_NAME (s)
1395 symbolS *s;
1396 {
1397 return s->bsym->name;
1398 }
1399
1400 segT
1401 S_GET_SEGMENT (s)
1402 symbolS *s;
1403 {
1404 return s->bsym->section;
1405 }
1406
1407 void
1408 S_SET_SEGMENT (s, seg)
1409 symbolS *s;
1410 segT seg;
1411 {
1412 s->bsym->section = seg;
1413 }
1414
1415 void
1416 S_SET_EXTERNAL (s)
1417 symbolS *s;
1418 {
1419 if ((s->bsym->flags & BSF_WEAK) != 0)
1420 {
1421 /* Let .weak override .global. */
1422 return;
1423 }
1424 s->bsym->flags |= BSF_GLOBAL;
1425 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
1426 }
1427
1428 void
1429 S_CLEAR_EXTERNAL (s)
1430 symbolS *s;
1431 {
1432 if ((s->bsym->flags & BSF_WEAK) != 0)
1433 {
1434 /* Let .weak override. */
1435 return;
1436 }
1437 s->bsym->flags |= BSF_LOCAL;
1438 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1439 }
1440
1441 void
1442 S_SET_WEAK (s)
1443 symbolS *s;
1444 {
1445 s->bsym->flags |= BSF_WEAK;
1446 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
1447 }
1448
1449 void
1450 S_SET_NAME (s, name)
1451 symbolS *s;
1452 char *name;
1453 {
1454 s->bsym->name = name;
1455 }
1456 #endif /* BFD_ASSEMBLER */
1457
1458 void
1459 symbol_begin ()
1460 {
1461 symbol_lastP = NULL;
1462 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
1463 sy_hash = hash_new ();
1464
1465 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
1466 #ifdef BFD_ASSEMBLER
1467 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1468 abs_symbol.bsym = bfd_abs_section.symbol;
1469 #endif
1470 #else
1471 /* Can't initialise a union. Sigh. */
1472 S_SET_SEGMENT (&abs_symbol, absolute_section);
1473 #endif
1474 abs_symbol.sy_value.X_op = O_constant;
1475 abs_symbol.sy_frag = &zero_address_frag;
1476
1477 if (LOCAL_LABELS_FB)
1478 fb_label_init ();
1479 }
1480
1481 \f
1482 int indent_level;
1483
1484 #if 0
1485
1486 static void
1487 indent ()
1488 {
1489 printf ("%*s", indent_level * 4, "");
1490 }
1491
1492 #endif
1493
1494 void
1495 print_symbol_value_1 (file, sym)
1496 FILE *file;
1497 symbolS *sym;
1498 {
1499 const char *name = S_GET_NAME (sym);
1500 if (!name || !name[0])
1501 name = "(unnamed)";
1502 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
1503 if (sym->sy_frag != &zero_address_frag)
1504 fprintf (file, " frag %lx", (long) sym->sy_frag);
1505 if (sym->written)
1506 fprintf (file, " written");
1507 if (sym->sy_resolved)
1508 fprintf (file, " resolved");
1509 else if (sym->sy_resolving)
1510 fprintf (file, " resolving");
1511 if (sym->sy_used_in_reloc)
1512 fprintf (file, " used-in-reloc");
1513 if (sym->sy_used)
1514 fprintf (file, " used");
1515 if (S_IS_LOCAL (sym))
1516 fprintf (file, " local");
1517 if (S_IS_EXTERN (sym))
1518 fprintf (file, " extern");
1519 if (S_IS_DEBUG (sym))
1520 fprintf (file, " debug");
1521 if (S_IS_DEFINED (sym))
1522 fprintf (file, " defined");
1523 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
1524 if (sym->sy_resolved)
1525 {
1526 segT s = S_GET_SEGMENT (sym);
1527
1528 if (s != undefined_section
1529 && s != expr_section)
1530 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
1531 }
1532 else if (indent_level < 8 && S_GET_SEGMENT (sym) != undefined_section)
1533 {
1534 indent_level++;
1535 fprintf (file, "\n%*s<", indent_level * 4, "");
1536 print_expr_1 (file, &sym->sy_value);
1537 fprintf (file, ">");
1538 indent_level--;
1539 }
1540 fflush (file);
1541 }
1542
1543 void
1544 print_symbol_value (sym)
1545 symbolS *sym;
1546 {
1547 indent_level = 0;
1548 print_symbol_value_1 (stderr, sym);
1549 fprintf (stderr, "\n");
1550 }
1551
1552 void
1553 print_expr_1 (file, exp)
1554 FILE *file;
1555 expressionS *exp;
1556 {
1557 fprintf (file, "expr %lx ", (long) exp);
1558 switch (exp->X_op)
1559 {
1560 case O_illegal:
1561 fprintf (file, "illegal");
1562 break;
1563 case O_absent:
1564 fprintf (file, "absent");
1565 break;
1566 case O_constant:
1567 fprintf (file, "constant %lx", (long) exp->X_add_number);
1568 break;
1569 case O_symbol:
1570 indent_level++;
1571 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
1572 print_symbol_value_1 (file, exp->X_add_symbol);
1573 fprintf (file, ">");
1574 maybe_print_addnum:
1575 if (exp->X_add_number)
1576 fprintf (file, "\n%*s%lx", indent_level * 4, "",
1577 (long) exp->X_add_number);
1578 indent_level--;
1579 break;
1580 case O_register:
1581 fprintf (file, "register #%d", (int) exp->X_add_number);
1582 break;
1583 case O_big:
1584 fprintf (file, "big");
1585 break;
1586 case O_uminus:
1587 fprintf (file, "uminus -<");
1588 indent_level++;
1589 print_symbol_value_1 (file, exp->X_add_symbol);
1590 fprintf (file, ">");
1591 goto maybe_print_addnum;
1592 case O_bit_not:
1593 fprintf (file, "bit_not");
1594 break;
1595 case O_multiply:
1596 fprintf (file, "multiply");
1597 break;
1598 case O_divide:
1599 fprintf (file, "divide");
1600 break;
1601 case O_modulus:
1602 fprintf (file, "modulus");
1603 break;
1604 case O_left_shift:
1605 fprintf (file, "lshift");
1606 break;
1607 case O_right_shift:
1608 fprintf (file, "rshift");
1609 break;
1610 case O_bit_inclusive_or:
1611 fprintf (file, "bit_ior");
1612 break;
1613 case O_bit_exclusive_or:
1614 fprintf (file, "bit_xor");
1615 break;
1616 case O_bit_and:
1617 fprintf (file, "bit_and");
1618 break;
1619 case O_eq:
1620 fprintf (file, "eq");
1621 break;
1622 case O_ne:
1623 fprintf (file, "ne");
1624 break;
1625 case O_lt:
1626 fprintf (file, "lt");
1627 break;
1628 case O_le:
1629 fprintf (file, "le");
1630 break;
1631 case O_ge:
1632 fprintf (file, "ge");
1633 break;
1634 case O_gt:
1635 fprintf (file, "gt");
1636 break;
1637 case O_logical_and:
1638 fprintf (file, "logical_and");
1639 break;
1640 case O_logical_or:
1641 fprintf (file, "logical_or");
1642 break;
1643 case O_add:
1644 indent_level++;
1645 fprintf (file, "add\n%*s<", indent_level * 4, "");
1646 print_symbol_value_1 (file, exp->X_add_symbol);
1647 fprintf (file, ">\n%*s<", indent_level * 4, "");
1648 print_symbol_value_1 (file, exp->X_op_symbol);
1649 fprintf (file, ">");
1650 goto maybe_print_addnum;
1651 case O_subtract:
1652 indent_level++;
1653 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
1654 print_symbol_value_1 (file, exp->X_add_symbol);
1655 fprintf (file, ">\n%*s<", indent_level * 4, "");
1656 print_symbol_value_1 (file, exp->X_op_symbol);
1657 fprintf (file, ">");
1658 goto maybe_print_addnum;
1659 default:
1660 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
1661 break;
1662 }
1663 fflush (stdout);
1664 }
1665
1666 void
1667 print_expr (exp)
1668 expressionS *exp;
1669 {
1670 print_expr_1 (stderr, exp);
1671 fprintf (stderr, "\n");
1672 }
1673
1674 void
1675 symbol_print_statistics (file)
1676 FILE *file;
1677 {
1678 hash_print_statistics (file, "symbol table", sy_hash);
1679 }
1680
1681 /* end of symbols.c */
This page took 0.088585 seconds and 4 git commands to generate.