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