00a961daf42fd81208f2bd684d6743946b68accb
[deliverable/binutils-gdb.git] / gas / config / tc-sparc.c
1 /* tc-sparc.c -- Assemble for the SPARC
2 Copyright (C) 1989, 90-95, 1996 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 2, 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
17 License along with GAS; see the file COPYING. If not, write
18 to the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <stdio.h>
22 #include <ctype.h>
23
24 #include "as.h"
25 #include "subsegs.h"
26
27 /* careful, this file includes data *declarations* */
28 #include "opcode/sparc.h"
29
30 static void sparc_ip PARAMS ((char *, const struct sparc_opcode **));
31
32 /* Current architecture. We don't bump up unless necessary. */
33 static enum sparc_opcode_arch_val current_architecture = SPARC_OPCODE_ARCH_V6;
34
35 /* The maximum architecture level we can bump up to.
36 In a 32 bit environment, don't allow bumping up to v9 by default.
37 The native assembler works this way. The user is required to pass
38 an explicit argument before we'll create v9 object files. However, if
39 we don't see any v9 insns, a v9 object file is not created. */
40 #ifdef SPARC_ARCH64
41 static enum sparc_opcode_arch_val max_architecture = SPARC_OPCODE_ARCH_V9;
42 #else
43 /* ??? This should be V8, but sparclite support was added by making it the
44 default. GCC now passes -Asparclite, so maybe sometime in the future
45 we can set this to V8. */
46 static enum sparc_opcode_arch_val max_architecture = SPARC_OPCODE_ARCH_SPARCLITE;
47 #endif
48
49 static int architecture_requested;
50 static int warn_on_bump;
51
52 /* If warn_on_bump and the needed architecture is higher than this
53 architecture, issue a warning. */
54 static enum sparc_opcode_arch_val warn_after_architecture;
55
56 /* Non-zero if we are generating PIC code. */
57 int sparc_pic_code;
58
59 /* Non-zero if we should give an error when misaligned data is seen. */
60 static int enforce_aligned_data;
61
62 extern int target_big_endian;
63
64 /* V9 has big and little endian data, but instructions are always big endian.
65 The sparclet has bi-endian support but both data and insns have the same
66 endianness. Global `target_big_endian' is used for data. The following
67 macro is used for instructions. */
68 #define INSN_BIG_ENDIAN (target_big_endian \
69 || SPARC_OPCODE_ARCH_V9_P (max_architecture))
70
71 /* handle of the OPCODE hash table */
72 static struct hash_control *op_hash;
73
74 static void s_data1 PARAMS ((void));
75 static void s_seg PARAMS ((int));
76 static void s_proc PARAMS ((int));
77 static void s_reserve PARAMS ((int));
78 static void s_common PARAMS ((int));
79 static void s_empty PARAMS ((int));
80 static void s_uacons PARAMS ((int));
81
82 const pseudo_typeS md_pseudo_table[] =
83 {
84 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0) */
85 {"common", s_common, 0},
86 {"empty", s_empty, 0},
87 {"global", s_globl, 0},
88 {"half", cons, 2},
89 {"optim", s_ignore, 0},
90 {"proc", s_proc, 0},
91 {"reserve", s_reserve, 0},
92 {"seg", s_seg, 0},
93 {"skip", s_space, 0},
94 {"word", cons, 4},
95 {"xword", cons, 8},
96 {"uahalf", s_uacons, 2},
97 {"uaword", s_uacons, 4},
98 {"uaxword", s_uacons, 8},
99 #ifdef OBJ_ELF
100 /* these are specific to sparc/svr4 */
101 {"pushsection", obj_elf_section, 0},
102 {"popsection", obj_elf_previous, 0},
103 {"2byte", s_uacons, 2},
104 {"4byte", s_uacons, 4},
105 {"8byte", s_uacons, 8},
106 #endif
107 {NULL, 0, 0},
108 };
109
110 const int md_reloc_size = 12; /* Size of relocation record */
111
112 /* This array holds the chars that always start a comment. If the
113 pre-processor is disabled, these aren't very useful */
114 const char comment_chars[] = "!"; /* JF removed '|' from comment_chars */
115
116 /* This array holds the chars that only start a comment at the beginning of
117 a line. If the line seems to have the form '# 123 filename'
118 .line and .file directives will appear in the pre-processed output */
119 /* Note that input_file.c hand checks for '#' at the beginning of the
120 first line of the input file. This is because the compiler outputs
121 #NO_APP at the beginning of its output. */
122 /* Also note that comments started like this one will always
123 work if '/' isn't otherwise defined. */
124 const char line_comment_chars[] = "#";
125
126 const char line_separator_chars[] = "";
127
128 /* Chars that can be used to separate mant from exp in floating point nums */
129 const char EXP_CHARS[] = "eE";
130
131 /* Chars that mean this number is a floating point constant */
132 /* As in 0f12.456 */
133 /* or 0d1.2345e12 */
134 const char FLT_CHARS[] = "rRsSfFdDxXpP";
135
136 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
137 changed in read.c. Ideally it shouldn't have to know about it at all,
138 but nothing is ideal around here. */
139
140 static unsigned char octal[256];
141 #define isoctal(c) octal[(unsigned char) (c)]
142 static unsigned char toHex[256];
143
144 struct sparc_it
145 {
146 char *error;
147 unsigned long opcode;
148 struct nlist *nlistp;
149 expressionS exp;
150 int pcrel;
151 bfd_reloc_code_real_type reloc;
152 };
153
154 struct sparc_it the_insn, set_insn;
155
156 /* Return non-zero if VAL is in the range -(MAX+1) to MAX. */
157
158 static INLINE int
159 in_signed_range (val, max)
160 bfd_signed_vma val, max;
161 {
162 if (max <= 0)
163 abort ();
164 if (val > max)
165 return 0;
166 if (val < ~max)
167 return 0;
168 return 1;
169 }
170
171 /* Return non-zero if VAL is in the range -(MAX/2+1) to MAX.
172 (e.g. -15 to +31). */
173
174 static INLINE int
175 in_bitfield_range (val, max)
176 bfd_signed_vma val, max;
177 {
178 if (max <= 0)
179 abort ();
180 if (val > max)
181 return 0;
182 if (val < ~(max >> 1))
183 return 0;
184 return 1;
185 }
186
187 static int
188 sparc_ffs (mask)
189 unsigned int mask;
190 {
191 int i;
192
193 if (mask == 0)
194 return -1;
195
196 for (i = 0; (mask & 1) == 0; ++i)
197 mask >>= 1;
198 return i;
199 }
200
201 /* Implement big shift right. */
202 static bfd_vma
203 BSR (val, amount)
204 bfd_vma val;
205 int amount;
206 {
207 if (sizeof (bfd_vma) <= 4 && amount >= 32)
208 as_fatal ("Support for 64-bit arithmetic not compiled in.");
209 return val >> amount;
210 }
211
212 #if 0
213 static void print_insn PARAMS ((struct sparc_it *insn));
214 #endif
215 static int getExpression PARAMS ((char *str));
216
217 static char *expr_end;
218 static int special_case;
219
220 /*
221 * Instructions that require wierd handling because they're longer than
222 * 4 bytes.
223 */
224 #define SPECIAL_CASE_SET 1
225 #define SPECIAL_CASE_SETSW 2
226 #define SPECIAL_CASE_SETX 3
227 /* FIXME: sparc-opc.c doesn't have necessary "S" trigger to enable this. */
228 #define SPECIAL_CASE_FDIV 4
229
230 /* Bit masks of various insns. */
231 #define NOP_INSN 0x01000000
232 #define OR_INSN 0x80100000
233 #define FMOVS_INSN 0x81A00020
234 #define SETHI_INSN 0x01000000
235 #define SLLX_INSN 0x81281000
236 #define SRA_INSN 0x81380000
237
238 /* The last instruction to be assembled. */
239 static const struct sparc_opcode *last_insn;
240
241 /*
242 * sort of like s_lcomm
243 *
244 */
245 #ifndef OBJ_ELF
246 static int max_alignment = 15;
247 #endif
248
249 static void
250 s_reserve (ignore)
251 int ignore;
252 {
253 char *name;
254 char *p;
255 char c;
256 int align;
257 int size;
258 int temp;
259 symbolS *symbolP;
260
261 name = input_line_pointer;
262 c = get_symbol_end ();
263 p = input_line_pointer;
264 *p = c;
265 SKIP_WHITESPACE ();
266
267 if (*input_line_pointer != ',')
268 {
269 as_bad ("Expected comma after name");
270 ignore_rest_of_line ();
271 return;
272 }
273
274 ++input_line_pointer;
275
276 if ((size = get_absolute_expression ()) < 0)
277 {
278 as_bad ("BSS length (%d.) <0! Ignored.", size);
279 ignore_rest_of_line ();
280 return;
281 } /* bad length */
282
283 *p = 0;
284 symbolP = symbol_find_or_make (name);
285 *p = c;
286
287 if (strncmp (input_line_pointer, ",\"bss\"", 6) != 0
288 && strncmp (input_line_pointer, ",\".bss\"", 7) != 0)
289 {
290 as_bad ("bad .reserve segment -- expected BSS segment");
291 return;
292 }
293
294 if (input_line_pointer[2] == '.')
295 input_line_pointer += 7;
296 else
297 input_line_pointer += 6;
298 SKIP_WHITESPACE ();
299
300 if (*input_line_pointer == ',')
301 {
302 ++input_line_pointer;
303
304 SKIP_WHITESPACE ();
305 if (*input_line_pointer == '\n')
306 {
307 as_bad ("Missing alignment");
308 return;
309 }
310
311 align = get_absolute_expression ();
312 #ifndef OBJ_ELF
313 if (align > max_alignment)
314 {
315 align = max_alignment;
316 as_warn ("Alignment too large: %d. assumed.", align);
317 }
318 #endif
319 if (align < 0)
320 {
321 align = 0;
322 as_warn ("Alignment negative. 0 assumed.");
323 }
324
325 record_alignment (bss_section, align);
326
327 /* convert to a power of 2 alignment */
328 for (temp = 0; (align & 1) == 0; align >>= 1, ++temp);;
329
330 if (align != 1)
331 {
332 as_bad ("Alignment not a power of 2");
333 ignore_rest_of_line ();
334 return;
335 } /* not a power of two */
336
337 align = temp;
338 } /* if has optional alignment */
339 else
340 align = 0;
341
342 if (!S_IS_DEFINED (symbolP)
343 #ifdef OBJ_AOUT
344 && S_GET_OTHER (symbolP) == 0
345 && S_GET_DESC (symbolP) == 0
346 #endif
347 )
348 {
349 if (! need_pass_2)
350 {
351 char *pfrag;
352 segT current_seg = now_seg;
353 subsegT current_subseg = now_subseg;
354
355 subseg_set (bss_section, 1); /* switch to bss */
356
357 if (align)
358 frag_align (align, 0); /* do alignment */
359
360 /* detach from old frag */
361 if (S_GET_SEGMENT(symbolP) == bss_section)
362 symbolP->sy_frag->fr_symbol = NULL;
363
364 symbolP->sy_frag = frag_now;
365 pfrag = frag_var (rs_org, 1, 1, (relax_substateT)0, symbolP,
366 size, (char *)0);
367 *pfrag = 0;
368
369 S_SET_SEGMENT (symbolP, bss_section);
370
371 subseg_set (current_seg, current_subseg);
372 }
373 }
374 else
375 {
376 as_warn("Ignoring attempt to re-define symbol %s",
377 S_GET_NAME (symbolP));
378 } /* if not redefining */
379
380 demand_empty_rest_of_line ();
381 }
382
383 static void
384 s_common (ignore)
385 int ignore;
386 {
387 char *name;
388 char c;
389 char *p;
390 int temp, size;
391 symbolS *symbolP;
392
393 name = input_line_pointer;
394 c = get_symbol_end ();
395 /* just after name is now '\0' */
396 p = input_line_pointer;
397 *p = c;
398 SKIP_WHITESPACE ();
399 if (*input_line_pointer != ',')
400 {
401 as_bad ("Expected comma after symbol-name");
402 ignore_rest_of_line ();
403 return;
404 }
405 input_line_pointer++; /* skip ',' */
406 if ((temp = get_absolute_expression ()) < 0)
407 {
408 as_bad (".COMMon length (%d.) <0! Ignored.", temp);
409 ignore_rest_of_line ();
410 return;
411 }
412 size = temp;
413 *p = 0;
414 symbolP = symbol_find_or_make (name);
415 *p = c;
416 if (S_IS_DEFINED (symbolP))
417 {
418 as_bad ("Ignoring attempt to re-define symbol");
419 ignore_rest_of_line ();
420 return;
421 }
422 if (S_GET_VALUE (symbolP) != 0)
423 {
424 if (S_GET_VALUE (symbolP) != size)
425 {
426 as_warn ("Length of .comm \"%s\" is already %ld. Not changed to %d.",
427 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
428 }
429 }
430 else
431 {
432 #ifndef OBJ_ELF
433 S_SET_VALUE (symbolP, (valueT) size);
434 S_SET_EXTERNAL (symbolP);
435 #endif
436 }
437 know (symbolP->sy_frag == &zero_address_frag);
438 if (*input_line_pointer != ',')
439 {
440 as_bad ("Expected comma after common length");
441 ignore_rest_of_line ();
442 return;
443 }
444 input_line_pointer++;
445 SKIP_WHITESPACE ();
446 if (*input_line_pointer != '"')
447 {
448 temp = get_absolute_expression ();
449 #ifndef OBJ_ELF
450 if (temp > max_alignment)
451 {
452 temp = max_alignment;
453 as_warn ("Common alignment too large: %d. assumed", temp);
454 }
455 #endif
456 if (temp < 0)
457 {
458 temp = 0;
459 as_warn ("Common alignment negative; 0 assumed");
460 }
461 #ifdef OBJ_ELF
462 if (symbolP->local)
463 {
464 segT old_sec;
465 int old_subsec;
466 char *p;
467 int align;
468
469 old_sec = now_seg;
470 old_subsec = now_subseg;
471 align = temp;
472 record_alignment (bss_section, align);
473 subseg_set (bss_section, 0);
474 if (align)
475 frag_align (align, 0);
476 if (S_GET_SEGMENT (symbolP) == bss_section)
477 symbolP->sy_frag->fr_symbol = 0;
478 symbolP->sy_frag = frag_now;
479 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
480 (char *) 0);
481 *p = 0;
482 S_SET_SEGMENT (symbolP, bss_section);
483 S_CLEAR_EXTERNAL (symbolP);
484 subseg_set (old_sec, old_subsec);
485 }
486 else
487 #endif
488 {
489 allocate_common:
490 S_SET_VALUE (symbolP, (valueT) size);
491 #ifdef OBJ_ELF
492 S_SET_ALIGN (symbolP, temp);
493 #endif
494 S_SET_EXTERNAL (symbolP);
495 /* should be common, but this is how gas does it for now */
496 S_SET_SEGMENT (symbolP, bfd_und_section_ptr);
497 }
498 }
499 else
500 {
501 input_line_pointer++;
502 /* @@ Some use the dot, some don't. Can we get some consistency?? */
503 if (*input_line_pointer == '.')
504 input_line_pointer++;
505 /* @@ Some say data, some say bss. */
506 if (strncmp (input_line_pointer, "bss\"", 4)
507 && strncmp (input_line_pointer, "data\"", 5))
508 {
509 while (*--input_line_pointer != '"')
510 ;
511 input_line_pointer--;
512 goto bad_common_segment;
513 }
514 while (*input_line_pointer++ != '"')
515 ;
516 goto allocate_common;
517 }
518 demand_empty_rest_of_line ();
519 return;
520
521 {
522 bad_common_segment:
523 p = input_line_pointer;
524 while (*p && *p != '\n')
525 p++;
526 c = *p;
527 *p = '\0';
528 as_bad ("bad .common segment %s", input_line_pointer + 1);
529 *p = c;
530 input_line_pointer = p;
531 ignore_rest_of_line ();
532 return;
533 }
534 }
535
536 /* Handle the .empty pseudo-op. This supresses the warnings about
537 invalid delay slot usage. */
538
539 static void
540 s_empty (ignore)
541 int ignore;
542 {
543 /* The easy way to implement is to just forget about the last
544 instruction. */
545 last_insn = NULL;
546 }
547
548 static void
549 s_seg (ignore)
550 int ignore;
551 {
552
553 if (strncmp (input_line_pointer, "\"text\"", 6) == 0)
554 {
555 input_line_pointer += 6;
556 s_text (0);
557 return;
558 }
559 if (strncmp (input_line_pointer, "\"data\"", 6) == 0)
560 {
561 input_line_pointer += 6;
562 s_data (0);
563 return;
564 }
565 if (strncmp (input_line_pointer, "\"data1\"", 7) == 0)
566 {
567 input_line_pointer += 7;
568 s_data1 ();
569 return;
570 }
571 if (strncmp (input_line_pointer, "\"bss\"", 5) == 0)
572 {
573 input_line_pointer += 5;
574 /* We only support 2 segments -- text and data -- for now, so
575 things in the "bss segment" will have to go into data for now.
576 You can still allocate SEG_BSS stuff with .lcomm or .reserve. */
577 subseg_set (data_section, 255); /* FIXME-SOMEDAY */
578 return;
579 }
580 as_bad ("Unknown segment type");
581 demand_empty_rest_of_line ();
582 }
583
584 static void
585 s_data1 ()
586 {
587 subseg_set (data_section, 1);
588 demand_empty_rest_of_line ();
589 }
590
591 static void
592 s_proc (ignore)
593 int ignore;
594 {
595 while (!is_end_of_line[(unsigned char) *input_line_pointer])
596 {
597 ++input_line_pointer;
598 }
599 ++input_line_pointer;
600 }
601
602 /* This static variable is set by s_uacons to tell sparc_cons_align
603 that the expession does not need to be aligned. */
604
605 static int sparc_no_align_cons = 0;
606
607 /* This handles the unaligned space allocation pseudo-ops, such as
608 .uaword. .uaword is just like .word, but the value does not need
609 to be aligned. */
610
611 static void
612 s_uacons (bytes)
613 int bytes;
614 {
615 /* Tell sparc_cons_align not to align this value. */
616 sparc_no_align_cons = 1;
617 cons (bytes);
618 }
619
620 /* If the --enforce-aligned-data option is used, we require .word,
621 et. al., to be aligned correctly. We do it by setting up an
622 rs_align_code frag, and checking in HANDLE_ALIGN to make sure that
623 no unexpected alignment was introduced.
624
625 The SunOS and Solaris native assemblers enforce aligned data by
626 default. We don't want to do that, because gcc can deliberately
627 generate misaligned data if the packed attribute is used. Instead,
628 we permit misaligned data by default, and permit the user to set an
629 option to check for it. */
630
631 void
632 sparc_cons_align (nbytes)
633 int nbytes;
634 {
635 int nalign;
636 char *p;
637
638 /* Only do this if we are enforcing aligned data. */
639 if (! enforce_aligned_data)
640 return;
641
642 if (sparc_no_align_cons)
643 {
644 /* This is an unaligned pseudo-op. */
645 sparc_no_align_cons = 0;
646 return;
647 }
648
649 nalign = 0;
650 while ((nbytes & 1) == 0)
651 {
652 ++nalign;
653 nbytes >>= 1;
654 }
655
656 if (nalign == 0)
657 return;
658
659 if (now_seg == absolute_section)
660 {
661 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
662 as_bad ("misaligned data");
663 return;
664 }
665
666 p = frag_var (rs_align_code, 1, 1, (relax_substateT) 0,
667 (symbolS *) NULL, (long) nalign, (char *) NULL);
668
669 record_alignment (now_seg, nalign);
670 }
671
672 /* This is where we do the unexpected alignment check. */
673
674 void
675 sparc_handle_align (fragp)
676 fragS *fragp;
677 {
678 if (fragp->fr_type == rs_align_code
679 && fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix != 0)
680 as_bad_where (fragp->fr_file, fragp->fr_line, "misaligned data");
681 }
682
683 /* sparc64 priviledged registers */
684
685 struct priv_reg_entry
686 {
687 char *name;
688 int regnum;
689 };
690
691 struct priv_reg_entry priv_reg_table[] =
692 {
693 {"tpc", 0},
694 {"tnpc", 1},
695 {"tstate", 2},
696 {"tt", 3},
697 {"tick", 4},
698 {"tba", 5},
699 {"pstate", 6},
700 {"tl", 7},
701 {"pil", 8},
702 {"cwp", 9},
703 {"cansave", 10},
704 {"canrestore", 11},
705 {"cleanwin", 12},
706 {"otherwin", 13},
707 {"wstate", 14},
708 {"fq", 15},
709 {"ver", 31},
710 {"", -1}, /* end marker */
711 };
712
713 static int
714 cmp_reg_entry (p, q)
715 struct priv_reg_entry *p, *q;
716 {
717 return strcmp (q->name, p->name);
718 }
719
720 /* This function is called once, at assembler startup time. It should
721 set up all the tables, etc. that the MD part of the assembler will need. */
722
723 void
724 md_begin ()
725 {
726 register const char *retval = NULL;
727 int lose = 0;
728 register unsigned int i = 0;
729
730 op_hash = hash_new ();
731
732 while (i < sparc_num_opcodes)
733 {
734 const char *name = sparc_opcodes[i].name;
735 retval = hash_insert (op_hash, name, &sparc_opcodes[i]);
736 if (retval != NULL)
737 {
738 fprintf (stderr, "internal error: can't hash `%s': %s\n",
739 sparc_opcodes[i].name, retval);
740 lose = 1;
741 }
742 do
743 {
744 if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
745 {
746 fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
747 sparc_opcodes[i].name, sparc_opcodes[i].args);
748 lose = 1;
749 }
750 ++i;
751 }
752 while (i < sparc_num_opcodes
753 && !strcmp (sparc_opcodes[i].name, name));
754 }
755
756 if (lose)
757 as_fatal ("Broken assembler. No assembly attempted.");
758
759 for (i = '0'; i < '8'; ++i)
760 octal[i] = 1;
761 for (i = '0'; i <= '9'; ++i)
762 toHex[i] = i - '0';
763 for (i = 'a'; i <= 'f'; ++i)
764 toHex[i] = i + 10 - 'a';
765 for (i = 'A'; i <= 'F'; ++i)
766 toHex[i] = i + 10 - 'A';
767
768 qsort (priv_reg_table, sizeof (priv_reg_table) / sizeof (priv_reg_table[0]),
769 sizeof (priv_reg_table[0]), cmp_reg_entry);
770
771 /* If -bump, record the architecture level at which we start issuing
772 warnings. The behaviour is different depending upon whether an
773 architecture was explicitly specified. If it wasn't, we issue warnings
774 for all upwards bumps. If it was, we don't start issuing warnings until
775 we need to bump beyond the requested architecture or when we bump between
776 conflicting architectures. */
777
778 if (warn_on_bump
779 && architecture_requested)
780 {
781 /* `max_architecture' records the requested architecture.
782 Issue warnings if we go above it. */
783 warn_after_architecture = max_architecture;
784
785 /* Find the highest architecture level that doesn't conflict with
786 the requested one. */
787 for (max_architecture = SPARC_OPCODE_ARCH_MAX;
788 max_architecture > warn_after_architecture;
789 --max_architecture)
790 if (! SPARC_OPCODE_CONFLICT_P (max_architecture,
791 warn_after_architecture))
792 break;
793 }
794 }
795
796 /* Called after all assembly has been done. */
797
798 void
799 sparc_md_end ()
800 {
801 #ifdef SPARC_ARCH64
802 if (current_architecture == SPARC_OPCODE_ARCH_V9A)
803 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v9a);
804 else
805 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v9);
806 #else
807 if (current_architecture == SPARC_OPCODE_ARCH_V9)
808 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v8plus);
809 else if (current_architecture == SPARC_OPCODE_ARCH_V9A)
810 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_v8plusa);
811 else if (current_architecture == SPARC_OPCODE_ARCH_SPARCLET)
812 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc_sparclet);
813 else
814 {
815 /* The sparclite is treated like a normal sparc. Perhaps it shouldn't
816 be but for now it is (since that's the way it's always been
817 treated). */
818 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, bfd_mach_sparc);
819 }
820 #endif
821 }
822
823 /* Utility to output one insn. */
824
825 static void
826 output_insn (insn, the_insn)
827 const struct sparc_opcode *insn;
828 struct sparc_it *the_insn;
829 {
830 char *toP = frag_more (4);
831
832 /* put out the opcode */
833 if (INSN_BIG_ENDIAN)
834 number_to_chars_bigendian (toP, (valueT) the_insn->opcode, 4);
835 else
836 number_to_chars_littleendian (toP, (valueT) the_insn->opcode, 4);
837
838 /* put out the symbol-dependent stuff */
839 if (the_insn->reloc != BFD_RELOC_NONE)
840 {
841 fix_new_exp (frag_now, /* which frag */
842 (toP - frag_now->fr_literal), /* where */
843 4, /* size */
844 &the_insn->exp,
845 the_insn->pcrel,
846 the_insn->reloc);
847 }
848
849 last_insn = insn;
850 }
851
852 void
853 md_assemble (str)
854 char *str;
855 {
856 const struct sparc_opcode *insn;
857
858 know (str);
859 special_case = 0;
860 sparc_ip (str, &insn);
861
862 /* We warn about attempts to put a floating point branch in a delay slot. */
863 if (insn != NULL
864 && last_insn != NULL
865 && (insn->flags & F_FBR) != 0
866 && (last_insn->flags & F_DELAYED) != 0)
867 as_warn ("FP branch in delay slot");
868
869 /* SPARC before v9 requires a nop instruction between a floating
870 point instruction and a floating point branch. We insert one
871 automatically, with a warning. */
872 if (max_architecture < SPARC_OPCODE_ARCH_V9
873 && insn != NULL
874 && last_insn != NULL
875 && (insn->flags & F_FBR) != 0
876 && (last_insn->flags & F_FLOAT) != 0)
877 {
878 struct sparc_it nop_insn;
879
880 nop_insn.opcode = NOP_INSN;
881 nop_insn.reloc = BFD_RELOC_NONE;
882 output_insn (insn, &nop_insn);
883 as_warn ("FP branch preceded by FP instruction; NOP inserted");
884 }
885
886 switch (special_case)
887 {
888 case 0:
889 /* normal insn */
890 output_insn (insn, &the_insn);
891 break;
892
893 case SPECIAL_CASE_SET:
894 {
895 int need_hi22_p = 0;
896
897 /* "set" is not defined for negative numbers in v9: it doesn't yield
898 what you expect it to. */
899 if (SPARC_OPCODE_ARCH_V9_P (max_architecture)
900 && the_insn.exp.X_op == O_constant)
901 {
902 if (the_insn.exp.X_add_number < 0)
903 as_warn ("set: used with negative number");
904 else if (the_insn.exp.X_add_number > 0xffffffff)
905 as_warn ("set: number larger than 4294967295");
906 }
907
908 /* See if operand is absolute and small; skip sethi if so. */
909 if (the_insn.exp.X_op != O_constant
910 || the_insn.exp.X_add_number >= (1 << 12)
911 || the_insn.exp.X_add_number < -(1 << 12))
912 {
913 output_insn (insn, &the_insn);
914 need_hi22_p = 1;
915 }
916 /* See if operand has no low-order bits; skip OR if so. */
917 if (the_insn.exp.X_op != O_constant
918 || (need_hi22_p && (the_insn.exp.X_add_number & 0x3FF) != 0)
919 || ! need_hi22_p)
920 {
921 int rd = (the_insn.opcode & RD (~0)) >> 25;
922 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (rd) : 0)
923 | RD (rd)
924 | IMMED
925 | (the_insn.exp.X_add_number
926 & (need_hi22_p ? 0x3ff : 0x1fff)));
927 the_insn.reloc = (the_insn.exp.X_op != O_constant
928 ? BFD_RELOC_LO10
929 : BFD_RELOC_NONE);
930 output_insn (insn, &the_insn);
931 }
932 break;
933 }
934
935 case SPECIAL_CASE_SETSW:
936 {
937 /* FIXME: Not finished. */
938 break;
939 }
940
941 case SPECIAL_CASE_SETX:
942 {
943 #define SIGNEXT32(x) ((((x) & 0xffffffff) ^ 0x80000000) - 0x80000000)
944 int upper32 = SIGNEXT32 (BSR (the_insn.exp.X_add_number, 32));
945 int lower32 = SIGNEXT32 (the_insn.exp.X_add_number);
946 #undef SIGNEXT32
947 int tmpreg = (the_insn.opcode & RS1 (~0)) >> 14;
948 int dstreg = (the_insn.opcode & RD (~0)) >> 25;
949 /* Output directly to dst reg if lower 32 bits are all zero. */
950 int upper_dstreg = (the_insn.exp.X_op == O_constant
951 && lower32 == 0) ? dstreg : tmpreg;
952 int need_hh22_p = 0, need_hm10_p = 0, need_hi22_p = 0, need_lo10_p = 0;
953
954 /* The tmp reg should not be the dst reg. */
955 if (tmpreg == dstreg)
956 as_warn ("setx: temporary register same as destination register");
957
958 /* Reset X_add_number, we've extracted it as upper32/lower32.
959 Otherwise fixup_segment will complain about not being able to
960 write an 8 byte number in a 4 byte field. */
961 the_insn.exp.X_add_number = 0;
962
963 /* ??? Obviously there are other optimizations we can do
964 (e.g. sethi+shift for 0x1f0000000) and perhaps we shouldn't be
965 doing some of these. Later. If you do change things, try to
966 change all of this to be table driven as well. */
967
968 /* What to output depends on the number if it's constant.
969 Compute that first, then output what we've decided upon. */
970 if (the_insn.exp.X_op != O_constant)
971 need_hh22_p = need_hm10_p = need_hi22_p = need_lo10_p = 1;
972 else
973 {
974 /* Only need hh22 if `or' insn can't handle constant. */
975 if (upper32 < -(1 << 12) || upper32 >= (1 << 12))
976 need_hh22_p = 1;
977
978 /* Does bottom part (after sethi) have bits? */
979 if ((need_hh22_p && (upper32 & 0x3ff) != 0)
980 /* No hh22, but does upper32 still have bits we can't set
981 from lower32? */
982 || (! need_hh22_p
983 && upper32 != 0
984 && (upper32 != -1 || lower32 >= 0)))
985 need_hm10_p = 1;
986
987 /* If the lower half is all zero, we build the upper half directly
988 into the dst reg. */
989 if (lower32 != 0
990 /* Need lower half if number is zero. */
991 || (! need_hh22_p && ! need_hm10_p))
992 {
993 /* No need for sethi if `or' insn can handle constant. */
994 if (lower32 < -(1 << 12) || lower32 >= (1 << 12)
995 /* Note that we can't use a negative constant in the `or'
996 insn unless the upper 32 bits are all ones. */
997 || (lower32 < 0 && upper32 != -1))
998 need_hi22_p = 1;
999
1000 /* Does bottom part (after sethi) have bits? */
1001 if ((need_hi22_p && (lower32 & 0x3ff) != 0)
1002 /* No sethi. */
1003 || (! need_hi22_p && (lower32 & 0x1fff) != 0)
1004 /* Need `or' if we didn't set anything else. */
1005 || (! need_hi22_p && ! need_hh22_p && ! need_hm10_p))
1006 need_lo10_p = 1;
1007 }
1008 }
1009
1010 if (need_hh22_p)
1011 {
1012 the_insn.opcode = (SETHI_INSN | RD (upper_dstreg)
1013 | ((upper32 >> 10) & 0x3fffff));
1014 the_insn.reloc = (the_insn.exp.X_op != O_constant
1015 ? BFD_RELOC_SPARC_HH22 : BFD_RELOC_NONE);
1016 output_insn (insn, &the_insn);
1017 }
1018
1019 if (need_hm10_p)
1020 {
1021 the_insn.opcode = (OR_INSN
1022 | (need_hh22_p ? RS1 (upper_dstreg) : 0)
1023 | RD (upper_dstreg)
1024 | IMMED
1025 | (upper32
1026 & (need_hh22_p ? 0x3ff : 0x1fff)));
1027 the_insn.reloc = (the_insn.exp.X_op != O_constant
1028 ? BFD_RELOC_SPARC_HM10 : BFD_RELOC_NONE);
1029 output_insn (insn, &the_insn);
1030 }
1031
1032 if (need_hi22_p)
1033 {
1034 the_insn.opcode = (SETHI_INSN | RD (dstreg)
1035 | ((lower32 >> 10) & 0x3fffff));
1036 the_insn.reloc = BFD_RELOC_HI22;
1037 output_insn (insn, &the_insn);
1038 }
1039
1040 if (need_lo10_p)
1041 {
1042 /* FIXME: One nice optimization to do here is to OR the low part
1043 with the highpart if hi22 isn't needed and the low part is
1044 positive. */
1045 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (dstreg) : 0)
1046 | RD (dstreg)
1047 | IMMED
1048 | (lower32
1049 & (need_hi22_p ? 0x3ff : 0x1fff)));
1050 the_insn.reloc = BFD_RELOC_LO10;
1051 output_insn (insn, &the_insn);
1052 }
1053
1054 /* If we needed to build the upper part, shift it into place. */
1055 if (need_hh22_p || need_hm10_p)
1056 {
1057 the_insn.opcode = (SLLX_INSN | RS1 (upper_dstreg) | RD (upper_dstreg)
1058 | IMMED | 32);
1059 the_insn.reloc = BFD_RELOC_NONE;
1060 output_insn (insn, &the_insn);
1061 }
1062
1063 /* If we needed to build both upper and lower parts, OR them together. */
1064 if ((need_hh22_p || need_hm10_p)
1065 && (need_hi22_p || need_lo10_p))
1066 {
1067 the_insn.opcode = (OR_INSN | RS1 (dstreg) | RS2 (upper_dstreg)
1068 | RD (dstreg));
1069 the_insn.reloc = BFD_RELOC_NONE;
1070 output_insn (insn, &the_insn);
1071 }
1072 /* We didn't need both regs, but we may have to sign extend lower32. */
1073 else if (need_hi22_p && upper32 == -1)
1074 {
1075 the_insn.opcode = (SRA_INSN | RS1 (dstreg) | RD (dstreg)
1076 | IMMED | 0);
1077 the_insn.reloc = BFD_RELOC_NONE;
1078 output_insn (insn, &the_insn);
1079 }
1080 break;
1081 }
1082
1083 case SPECIAL_CASE_FDIV:
1084 {
1085 int rd = (the_insn.opcode >> 25) & 0x1f;
1086
1087 output_insn (insn, &the_insn);
1088
1089 /* According to information leaked from Sun, the "fdiv" instructions
1090 on early SPARC machines would produce incorrect results sometimes.
1091 The workaround is to add an fmovs of the destination register to
1092 itself just after the instruction. This was true on machines
1093 with Weitek 1165 float chips, such as the Sun-4/260 and /280. */
1094 assert (the_insn.reloc == BFD_RELOC_NONE);
1095 the_insn.opcode = FMOVS_INSN | rd | RD (rd);
1096 output_insn (insn, &the_insn);
1097 break;
1098 }
1099
1100 default:
1101 as_fatal ("failed special case insn sanity check");
1102 }
1103 }
1104
1105 /* Parse an argument that can be expressed as a keyword.
1106 (eg: #StoreStore or %ccfr).
1107 The result is a boolean indicating success.
1108 If successful, INPUT_POINTER is updated. */
1109
1110 static int
1111 parse_keyword_arg (lookup_fn, input_pointerP, valueP)
1112 int (*lookup_fn) ();
1113 char **input_pointerP;
1114 int *valueP;
1115 {
1116 int value;
1117 char c, *p, *q;
1118
1119 p = *input_pointerP;
1120 for (q = p + (*p == '#' || *p == '%'); isalpha (*q) || *q == '_'; ++q)
1121 continue;
1122 c = *q;
1123 *q = 0;
1124 value = (*lookup_fn) (p);
1125 *q = c;
1126 if (value == -1)
1127 return 0;
1128 *valueP = value;
1129 *input_pointerP = q;
1130 return 1;
1131 }
1132
1133 /* Parse an argument that is a constant expression.
1134 The result is a boolean indicating success. */
1135
1136 static int
1137 parse_const_expr_arg (input_pointerP, valueP)
1138 char **input_pointerP;
1139 int *valueP;
1140 {
1141 char *save = input_line_pointer;
1142 expressionS exp;
1143
1144 input_line_pointer = *input_pointerP;
1145 /* The next expression may be something other than a constant
1146 (say if we're not processing the right variant of the insn).
1147 Don't call expression unless we're sure it will succeed as it will
1148 signal an error (which we want to defer until later). */
1149 /* FIXME: It might be better to define md_operand and have it recognize
1150 things like %asi, etc. but continuing that route through to the end
1151 is a lot of work. */
1152 if (*input_line_pointer == '%')
1153 {
1154 input_line_pointer = save;
1155 return 0;
1156 }
1157 expression (&exp);
1158 *input_pointerP = input_line_pointer;
1159 input_line_pointer = save;
1160 if (exp.X_op != O_constant)
1161 return 0;
1162 *valueP = exp.X_add_number;
1163 return 1;
1164 }
1165
1166 static void
1167 sparc_ip (str, pinsn)
1168 char *str;
1169 const struct sparc_opcode **pinsn;
1170 {
1171 char *error_message = "";
1172 char *s;
1173 const char *args;
1174 char c;
1175 const struct sparc_opcode *insn;
1176 char *argsStart;
1177 unsigned long opcode;
1178 unsigned int mask = 0;
1179 int match = 0;
1180 int comma = 0;
1181 long immediate_max = 0;
1182 int v9_arg_p;
1183
1184 for (s = str; islower (*s) || (*s >= '0' && *s <= '3'); ++s)
1185 ;
1186
1187 switch (*s)
1188 {
1189 case '\0':
1190 break;
1191
1192 case ',':
1193 comma = 1;
1194
1195 /*FALLTHROUGH */
1196
1197 case ' ':
1198 *s++ = '\0';
1199 break;
1200
1201 default:
1202 as_fatal ("Unknown opcode: `%s'", str);
1203 }
1204 insn = (struct sparc_opcode *) hash_find (op_hash, str);
1205 *pinsn = insn;
1206 if (insn == NULL)
1207 {
1208 as_bad ("Unknown opcode: `%s'", str);
1209 return;
1210 }
1211 if (comma)
1212 {
1213 *--s = ',';
1214 }
1215
1216 argsStart = s;
1217 for (;;)
1218 {
1219 opcode = insn->match;
1220 memset (&the_insn, '\0', sizeof (the_insn));
1221 the_insn.reloc = BFD_RELOC_NONE;
1222 v9_arg_p = 0;
1223
1224 /*
1225 * Build the opcode, checking as we go to make
1226 * sure that the operands match
1227 */
1228 for (args = insn->args;; ++args)
1229 {
1230 switch (*args)
1231 {
1232 case 'K':
1233 {
1234 int kmask = 0;
1235
1236 /* Parse a series of masks. */
1237 if (*s == '#')
1238 {
1239 while (*s == '#')
1240 {
1241 int mask;
1242
1243 if (! parse_keyword_arg (sparc_encode_membar, &s,
1244 &mask))
1245 {
1246 error_message = ": invalid membar mask name";
1247 goto error;
1248 }
1249 kmask |= mask;
1250 while (*s == ' ') { ++s; continue; }
1251 if (*s == '|' || *s == '+')
1252 ++s;
1253 while (*s == ' ') { ++s; continue; }
1254 }
1255 }
1256 else
1257 {
1258 if (! parse_const_expr_arg (&s, &kmask))
1259 {
1260 error_message = ": invalid membar mask expression";
1261 goto error;
1262 }
1263 if (kmask < 0 || kmask > 127)
1264 {
1265 error_message = ": invalid membar mask number";
1266 goto error;
1267 }
1268 }
1269
1270 opcode |= MEMBAR (kmask);
1271 continue;
1272 }
1273
1274 case '*':
1275 {
1276 int fcn = 0;
1277
1278 /* Parse a prefetch function. */
1279 if (*s == '#')
1280 {
1281 if (! parse_keyword_arg (sparc_encode_prefetch, &s, &fcn))
1282 {
1283 error_message = ": invalid prefetch function name";
1284 goto error;
1285 }
1286 }
1287 else
1288 {
1289 if (! parse_const_expr_arg (&s, &fcn))
1290 {
1291 error_message = ": invalid prefetch function expression";
1292 goto error;
1293 }
1294 if (fcn < 0 || fcn > 31)
1295 {
1296 error_message = ": invalid prefetch function number";
1297 goto error;
1298 }
1299 }
1300 opcode |= RD (fcn);
1301 continue;
1302 }
1303
1304 case '!':
1305 case '?':
1306 /* Parse a sparc64 privileged register. */
1307 if (*s == '%')
1308 {
1309 struct priv_reg_entry *p = priv_reg_table;
1310 unsigned int len = 9999999; /* init to make gcc happy */
1311
1312 s += 1;
1313 while (p->name[0] > s[0])
1314 p++;
1315 while (p->name[0] == s[0])
1316 {
1317 len = strlen (p->name);
1318 if (strncmp (p->name, s, len) == 0)
1319 break;
1320 p++;
1321 }
1322 if (p->name[0] != s[0])
1323 {
1324 error_message = ": unrecognizable privileged register";
1325 goto error;
1326 }
1327 if (*args == '?')
1328 opcode |= (p->regnum << 14);
1329 else
1330 opcode |= (p->regnum << 25);
1331 s += len;
1332 continue;
1333 }
1334 else
1335 {
1336 error_message = ": unrecognizable privileged register";
1337 goto error;
1338 }
1339
1340 case 'M':
1341 case 'm':
1342 if (strncmp (s, "%asr", 4) == 0)
1343 {
1344 s += 4;
1345
1346 if (isdigit (*s))
1347 {
1348 long num = 0;
1349
1350 while (isdigit (*s))
1351 {
1352 num = num * 10 + *s - '0';
1353 ++s;
1354 }
1355
1356 if (current_architecture >= SPARC_OPCODE_ARCH_V9)
1357 {
1358 if (num < 16 || 31 < num)
1359 {
1360 error_message = ": asr number must be between 16 and 31";
1361 goto error;
1362 }
1363 }
1364 else
1365 {
1366 if (num < 0 || 31 < num)
1367 {
1368 error_message = ": asr number must be between 0 and 31";
1369 goto error;
1370 }
1371 }
1372
1373 opcode |= (*args == 'M' ? RS1 (num) : RD (num));
1374 continue;
1375 }
1376 else
1377 {
1378 error_message = ": expecting %asrN";
1379 goto error;
1380 }
1381 } /* if %asr */
1382 break;
1383
1384 case 'I':
1385 the_insn.reloc = BFD_RELOC_SPARC_11;
1386 immediate_max = 0x03FF;
1387 goto immediate;
1388
1389 case 'j':
1390 the_insn.reloc = BFD_RELOC_SPARC_10;
1391 immediate_max = 0x01FF;
1392 goto immediate;
1393
1394 case 'X':
1395 /* V8 systems don't understand BFD_RELOC_SPARC_5. */
1396 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1397 the_insn.reloc = BFD_RELOC_SPARC_5;
1398 else
1399 the_insn.reloc = BFD_RELOC_SPARC13;
1400 /* These fields are unsigned, but for upward compatibility,
1401 allow negative values as well. */
1402 immediate_max = 0x1f;
1403 goto immediate;
1404
1405 case 'Y':
1406 /* V8 systems don't understand BFD_RELOC_SPARC_6. */
1407 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1408 the_insn.reloc = BFD_RELOC_SPARC_6;
1409 else
1410 the_insn.reloc = BFD_RELOC_SPARC13;
1411 /* These fields are unsigned, but for upward compatibility,
1412 allow negative values as well. */
1413 immediate_max = 0x3f;
1414 goto immediate;
1415
1416 case 'k':
1417 the_insn.reloc = /* RELOC_WDISP2_14 */ BFD_RELOC_SPARC_WDISP16;
1418 the_insn.pcrel = 1;
1419 goto immediate;
1420
1421 case 'G':
1422 the_insn.reloc = BFD_RELOC_SPARC_WDISP19;
1423 the_insn.pcrel = 1;
1424 goto immediate;
1425
1426 case 'N':
1427 if (*s == 'p' && s[1] == 'n')
1428 {
1429 s += 2;
1430 continue;
1431 }
1432 break;
1433
1434 case 'T':
1435 if (*s == 'p' && s[1] == 't')
1436 {
1437 s += 2;
1438 continue;
1439 }
1440 break;
1441
1442 case 'z':
1443 if (*s == ' ')
1444 {
1445 ++s;
1446 }
1447 if (strncmp (s, "%icc", 4) == 0)
1448 {
1449 s += 4;
1450 continue;
1451 }
1452 break;
1453
1454 case 'Z':
1455 if (*s == ' ')
1456 {
1457 ++s;
1458 }
1459 if (strncmp (s, "%xcc", 4) == 0)
1460 {
1461 s += 4;
1462 continue;
1463 }
1464 break;
1465
1466 case '6':
1467 if (*s == ' ')
1468 {
1469 ++s;
1470 }
1471 if (strncmp (s, "%fcc0", 5) == 0)
1472 {
1473 s += 5;
1474 continue;
1475 }
1476 break;
1477
1478 case '7':
1479 if (*s == ' ')
1480 {
1481 ++s;
1482 }
1483 if (strncmp (s, "%fcc1", 5) == 0)
1484 {
1485 s += 5;
1486 continue;
1487 }
1488 break;
1489
1490 case '8':
1491 if (*s == ' ')
1492 {
1493 ++s;
1494 }
1495 if (strncmp (s, "%fcc2", 5) == 0)
1496 {
1497 s += 5;
1498 continue;
1499 }
1500 break;
1501
1502 case '9':
1503 if (*s == ' ')
1504 {
1505 ++s;
1506 }
1507 if (strncmp (s, "%fcc3", 5) == 0)
1508 {
1509 s += 5;
1510 continue;
1511 }
1512 break;
1513
1514 case 'P':
1515 if (strncmp (s, "%pc", 3) == 0)
1516 {
1517 s += 3;
1518 continue;
1519 }
1520 break;
1521
1522 case 'W':
1523 if (strncmp (s, "%tick", 5) == 0)
1524 {
1525 s += 5;
1526 continue;
1527 }
1528 break;
1529
1530 case '\0': /* end of args */
1531 if (*s == '\0')
1532 {
1533 match = 1;
1534 }
1535 break;
1536
1537 case '+':
1538 if (*s == '+')
1539 {
1540 ++s;
1541 continue;
1542 }
1543 if (*s == '-')
1544 {
1545 continue;
1546 }
1547 break;
1548
1549 case '[': /* these must match exactly */
1550 case ']':
1551 case ',':
1552 case ' ':
1553 if (*s++ == *args)
1554 continue;
1555 break;
1556
1557 case '#': /* must be at least one digit */
1558 if (isdigit (*s++))
1559 {
1560 while (isdigit (*s))
1561 {
1562 ++s;
1563 }
1564 continue;
1565 }
1566 break;
1567
1568 case 'C': /* coprocessor state register */
1569 if (strncmp (s, "%csr", 4) == 0)
1570 {
1571 s += 4;
1572 continue;
1573 }
1574 break;
1575
1576 case 'b': /* next operand is a coprocessor register */
1577 case 'c':
1578 case 'D':
1579 if (*s++ == '%' && *s++ == 'c' && isdigit (*s))
1580 {
1581 mask = *s++;
1582 if (isdigit (*s))
1583 {
1584 mask = 10 * (mask - '0') + (*s++ - '0');
1585 if (mask >= 32)
1586 {
1587 break;
1588 }
1589 }
1590 else
1591 {
1592 mask -= '0';
1593 }
1594 switch (*args)
1595 {
1596
1597 case 'b':
1598 opcode |= mask << 14;
1599 continue;
1600
1601 case 'c':
1602 opcode |= mask;
1603 continue;
1604
1605 case 'D':
1606 opcode |= mask << 25;
1607 continue;
1608 }
1609 }
1610 break;
1611
1612 case 'r': /* next operand must be a register */
1613 case 'O':
1614 case '1':
1615 case '2':
1616 case 'd':
1617 if (*s++ == '%')
1618 {
1619 switch (c = *s++)
1620 {
1621
1622 case 'f': /* frame pointer */
1623 if (*s++ == 'p')
1624 {
1625 mask = 0x1e;
1626 break;
1627 }
1628 goto error;
1629
1630 case 'g': /* global register */
1631 if (isoctal (c = *s++))
1632 {
1633 mask = c - '0';
1634 break;
1635 }
1636 goto error;
1637
1638 case 'i': /* in register */
1639 if (isoctal (c = *s++))
1640 {
1641 mask = c - '0' + 24;
1642 break;
1643 }
1644 goto error;
1645
1646 case 'l': /* local register */
1647 if (isoctal (c = *s++))
1648 {
1649 mask = (c - '0' + 16);
1650 break;
1651 }
1652 goto error;
1653
1654 case 'o': /* out register */
1655 if (isoctal (c = *s++))
1656 {
1657 mask = (c - '0' + 8);
1658 break;
1659 }
1660 goto error;
1661
1662 case 's': /* stack pointer */
1663 if (*s++ == 'p')
1664 {
1665 mask = 0xe;
1666 break;
1667 }
1668 goto error;
1669
1670 case 'r': /* any register */
1671 if (!isdigit (c = *s++))
1672 {
1673 goto error;
1674 }
1675 /* FALLTHROUGH */
1676 case '0':
1677 case '1':
1678 case '2':
1679 case '3':
1680 case '4':
1681 case '5':
1682 case '6':
1683 case '7':
1684 case '8':
1685 case '9':
1686 if (isdigit (*s))
1687 {
1688 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32)
1689 {
1690 goto error;
1691 }
1692 }
1693 else
1694 {
1695 c -= '0';
1696 }
1697 mask = c;
1698 break;
1699
1700 default:
1701 goto error;
1702 }
1703
1704 /* Got the register, now figure out where
1705 it goes in the opcode. */
1706 switch (*args)
1707 {
1708 case '1':
1709 opcode |= mask << 14;
1710 continue;
1711
1712 case '2':
1713 opcode |= mask;
1714 continue;
1715
1716 case 'd':
1717 opcode |= mask << 25;
1718 continue;
1719
1720 case 'r':
1721 opcode |= (mask << 25) | (mask << 14);
1722 continue;
1723
1724 case 'O':
1725 opcode |= (mask << 25) | (mask << 0);
1726 continue;
1727 }
1728 }
1729 break;
1730
1731 case 'e': /* next operand is a floating point register */
1732 case 'v':
1733 case 'V':
1734
1735 case 'f':
1736 case 'B':
1737 case 'R':
1738
1739 case 'g':
1740 case 'H':
1741 case 'J':
1742 {
1743 char format;
1744
1745 if (*s++ == '%'
1746 && ((format = *s) == 'f')
1747 && isdigit (*++s))
1748 {
1749 for (mask = 0; isdigit (*s); ++s)
1750 {
1751 mask = 10 * mask + (*s - '0');
1752 } /* read the number */
1753
1754 if ((*args == 'v'
1755 || *args == 'B'
1756 || *args == 'H')
1757 && (mask & 1))
1758 {
1759 break;
1760 } /* register must be even numbered */
1761
1762 if ((*args == 'V'
1763 || *args == 'R'
1764 || *args == 'J')
1765 && (mask & 3))
1766 {
1767 break;
1768 } /* register must be multiple of 4 */
1769
1770 if (mask >= 64)
1771 {
1772 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1773 error_message = ": There are only 64 f registers; [0-63]";
1774 else
1775 error_message = ": There are only 32 f registers; [0-31]";
1776 goto error;
1777 } /* on error */
1778 else if (mask >= 32)
1779 {
1780 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1781 {
1782 v9_arg_p = 1;
1783 mask -= 31; /* wrap high bit */
1784 }
1785 else
1786 {
1787 error_message = ": There are only 32 f registers; [0-31]";
1788 goto error;
1789 }
1790 }
1791 }
1792 else
1793 {
1794 break;
1795 } /* if not an 'f' register. */
1796
1797 switch (*args)
1798 {
1799 case 'v':
1800 case 'V':
1801 case 'e':
1802 opcode |= RS1 (mask);
1803 continue;
1804
1805
1806 case 'f':
1807 case 'B':
1808 case 'R':
1809 opcode |= RS2 (mask);
1810 continue;
1811
1812 case 'g':
1813 case 'H':
1814 case 'J':
1815 opcode |= RD (mask);
1816 continue;
1817 } /* pack it in. */
1818
1819 know (0);
1820 break;
1821 } /* float arg */
1822
1823 case 'F':
1824 if (strncmp (s, "%fsr", 4) == 0)
1825 {
1826 s += 4;
1827 continue;
1828 }
1829 break;
1830
1831 case '0': /* 64 bit immediate (setx insn) */
1832 the_insn.reloc = BFD_RELOC_NONE; /* reloc handled elsewhere */
1833 goto immediate;
1834
1835 case 'h': /* high 22 bits */
1836 the_insn.reloc = BFD_RELOC_HI22;
1837 goto immediate;
1838
1839 case 'l': /* 22 bit PC relative immediate */
1840 the_insn.reloc = BFD_RELOC_SPARC_WDISP22;
1841 the_insn.pcrel = 1;
1842 goto immediate;
1843
1844 case 'L': /* 30 bit immediate */
1845 the_insn.reloc = BFD_RELOC_32_PCREL_S2;
1846 the_insn.pcrel = 1;
1847 goto immediate;
1848
1849 case 'n': /* 22 bit immediate */
1850 the_insn.reloc = BFD_RELOC_SPARC22;
1851 goto immediate;
1852
1853 case 'i': /* 13 bit immediate */
1854 the_insn.reloc = BFD_RELOC_SPARC13;
1855 immediate_max = 0x0FFF;
1856
1857 /*FALLTHROUGH */
1858
1859 immediate:
1860 if (*s == ' ')
1861 s++;
1862 if (*s == '%')
1863 {
1864 if ((c = s[1]) == 'h' && s[2] == 'i')
1865 {
1866 the_insn.reloc = BFD_RELOC_HI22;
1867 s += 3;
1868 }
1869 else if (c == 'l' && s[2] == 'o')
1870 {
1871 the_insn.reloc = BFD_RELOC_LO10;
1872 s += 3;
1873 }
1874 else if (c == 'u'
1875 && s[2] == 'h'
1876 && s[3] == 'i')
1877 {
1878 the_insn.reloc = BFD_RELOC_SPARC_HH22;
1879 s += 4;
1880 v9_arg_p = 1;
1881 }
1882 else if (c == 'u'
1883 && s[2] == 'l'
1884 && s[3] == 'o')
1885 {
1886 the_insn.reloc = BFD_RELOC_SPARC_HM10;
1887 s += 4;
1888 v9_arg_p = 1;
1889 }
1890 else
1891 break;
1892 }
1893 /* Note that if the getExpression() fails, we will still
1894 have created U entries in the symbol table for the
1895 'symbols' in the input string. Try not to create U
1896 symbols for registers, etc. */
1897 {
1898 /* This stuff checks to see if the expression ends in
1899 +%reg. If it does, it removes the register from
1900 the expression, and re-sets 's' to point to the
1901 right place. */
1902
1903 char *s1;
1904
1905 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++) ;
1906
1907 if (s1 != s && isdigit (s1[-1]))
1908 {
1909 if (s1[-2] == '%' && s1[-3] == '+')
1910 {
1911 s1 -= 3;
1912 *s1 = '\0';
1913 (void) getExpression (s);
1914 *s1 = '+';
1915 s = s1;
1916 continue;
1917 }
1918 else if (strchr ("goli0123456789", s1[-2]) && s1[-3] == '%' && s1[-4] == '+')
1919 {
1920 s1 -= 4;
1921 *s1 = '\0';
1922 (void) getExpression (s);
1923 *s1 = '+';
1924 s = s1;
1925 continue;
1926 }
1927 }
1928 }
1929 (void) getExpression (s);
1930 s = expr_end;
1931
1932 if (the_insn.exp.X_op == O_constant
1933 && the_insn.exp.X_add_symbol == 0
1934 && the_insn.exp.X_op_symbol == 0)
1935 {
1936 /* Handle %uhi/%ulo by moving the upper word to the lower
1937 one and pretending it's %hi/%lo. We also need to watch
1938 for %hi/%lo: the top word needs to be zeroed otherwise
1939 fixup_segment will complain the value is too big. */
1940 switch (the_insn.reloc)
1941 {
1942 case BFD_RELOC_SPARC_HH22:
1943 the_insn.reloc = BFD_RELOC_HI22;
1944 the_insn.exp.X_add_number = BSR (the_insn.exp.X_add_number, 32);
1945 break;
1946 case BFD_RELOC_SPARC_HM10:
1947 the_insn.reloc = BFD_RELOC_LO10;
1948 the_insn.exp.X_add_number = BSR (the_insn.exp.X_add_number, 32);
1949 break;
1950 case BFD_RELOC_HI22:
1951 case BFD_RELOC_LO10:
1952 the_insn.exp.X_add_number &= 0xffffffff;
1953 break;
1954 default:
1955 break;
1956 }
1957
1958 /* For pc-relative call instructions, we reject
1959 constants to get better code. */
1960 if (the_insn.pcrel
1961 && the_insn.reloc == BFD_RELOC_32_PCREL_S2
1962 && in_signed_range (the_insn.exp.X_add_number, 0x3fff)
1963 )
1964 {
1965 error_message = ": PC-relative operand can't be a constant";
1966 goto error;
1967 }
1968 /* Check for invalid constant values. Don't warn if
1969 constant was inside %hi or %lo, since these
1970 truncate the constant to fit. */
1971 if (immediate_max != 0
1972 && the_insn.reloc != BFD_RELOC_LO10
1973 && the_insn.reloc != BFD_RELOC_HI22
1974 && !in_signed_range (the_insn.exp.X_add_number,
1975 immediate_max)
1976 )
1977 {
1978 if (the_insn.pcrel)
1979 /* Who knows? After relocation, we may be within
1980 range. Let the linker figure it out. */
1981 {
1982 the_insn.exp.X_op = O_symbol;
1983 the_insn.exp.X_add_symbol = section_symbol (absolute_section);
1984 }
1985 else
1986 /* Immediate value is non-pcrel, and out of
1987 range. */
1988 as_bad ("constant value %ld out of range (%ld .. %ld)",
1989 the_insn.exp.X_add_number,
1990 ~immediate_max, immediate_max);
1991 }
1992 }
1993
1994 /* Reset to prevent extraneous range check. */
1995 immediate_max = 0;
1996
1997 continue;
1998
1999 case 'a':
2000 if (*s++ == 'a')
2001 {
2002 opcode |= ANNUL;
2003 continue;
2004 }
2005 break;
2006
2007 case 'A':
2008 {
2009 int asi = 0;
2010
2011 /* Parse an asi. */
2012 if (*s == '#')
2013 {
2014 if (! parse_keyword_arg (sparc_encode_asi, &s, &asi))
2015 {
2016 error_message = ": invalid ASI name";
2017 goto error;
2018 }
2019 }
2020 else
2021 {
2022 if (! parse_const_expr_arg (&s, &asi))
2023 {
2024 error_message = ": invalid ASI expression";
2025 goto error;
2026 }
2027 if (asi < 0 || asi > 255)
2028 {
2029 error_message = ": invalid ASI number";
2030 goto error;
2031 }
2032 }
2033 opcode |= ASI (asi);
2034 continue;
2035 } /* alternate space */
2036
2037 case 'p':
2038 if (strncmp (s, "%psr", 4) == 0)
2039 {
2040 s += 4;
2041 continue;
2042 }
2043 break;
2044
2045 case 'q': /* floating point queue */
2046 if (strncmp (s, "%fq", 3) == 0)
2047 {
2048 s += 3;
2049 continue;
2050 }
2051 break;
2052
2053 case 'Q': /* coprocessor queue */
2054 if (strncmp (s, "%cq", 3) == 0)
2055 {
2056 s += 3;
2057 continue;
2058 }
2059 break;
2060
2061 case 'S':
2062 if (strcmp (str, "set") == 0
2063 || strcmp (str, "setuw") == 0)
2064 {
2065 special_case = SPECIAL_CASE_SET;
2066 continue;
2067 }
2068 else if (strcmp (str, "setsw") == 0)
2069 {
2070 special_case = SPECIAL_CASE_SETSW;
2071 continue;
2072 }
2073 else if (strcmp (str, "setx") == 0)
2074 {
2075 special_case = SPECIAL_CASE_SETX;
2076 continue;
2077 }
2078 else if (strncmp (str, "fdiv", 4) == 0)
2079 {
2080 special_case = SPECIAL_CASE_FDIV;
2081 continue;
2082 }
2083 break;
2084
2085 case 'o':
2086 if (strncmp (s, "%asi", 4) != 0)
2087 break;
2088 s += 4;
2089 continue;
2090
2091 case 's':
2092 if (strncmp (s, "%fprs", 5) != 0)
2093 break;
2094 s += 5;
2095 continue;
2096
2097 case 'E':
2098 if (strncmp (s, "%ccr", 4) != 0)
2099 break;
2100 s += 4;
2101 continue;
2102
2103 case 't':
2104 if (strncmp (s, "%tbr", 4) != 0)
2105 break;
2106 s += 4;
2107 continue;
2108
2109 case 'w':
2110 if (strncmp (s, "%wim", 4) != 0)
2111 break;
2112 s += 4;
2113 continue;
2114
2115 case 'x':
2116 {
2117 char *push = input_line_pointer;
2118 expressionS e;
2119
2120 input_line_pointer = s;
2121 expression (&e);
2122 if (e.X_op == O_constant)
2123 {
2124 int n = e.X_add_number;
2125 if (n != e.X_add_number || (n & ~0x1ff) != 0)
2126 as_bad ("OPF immediate operand out of range (0-0x1ff)");
2127 else
2128 opcode |= e.X_add_number << 5;
2129 }
2130 else
2131 as_bad ("non-immediate OPF operand, ignored");
2132 s = input_line_pointer;
2133 input_line_pointer = push;
2134 continue;
2135 }
2136
2137 case 'y':
2138 if (strncmp (s, "%y", 2) != 0)
2139 break;
2140 s += 2;
2141 continue;
2142
2143 case 'u':
2144 case 'U':
2145 {
2146 /* Parse a sparclet cpreg. */
2147 int cpreg;
2148 if (! parse_keyword_arg (sparc_encode_sparclet_cpreg, &s, &cpreg))
2149 {
2150 error_message = ": invalid cpreg name";
2151 goto error;
2152 }
2153 opcode |= (*args == 'U' ? RS1 (cpreg) : RD (cpreg));
2154 continue;
2155 }
2156
2157 default:
2158 as_fatal ("failed sanity check.");
2159 } /* switch on arg code */
2160
2161 /* Break out of for() loop. */
2162 break;
2163 } /* for each arg that we expect */
2164
2165 error:
2166 if (match == 0)
2167 {
2168 /* Args don't match. */
2169 if (((unsigned) (&insn[1] - sparc_opcodes)) < sparc_num_opcodes
2170 && (insn->name == insn[1].name
2171 || !strcmp (insn->name, insn[1].name)))
2172 {
2173 ++insn;
2174 s = argsStart;
2175 continue;
2176 }
2177 else
2178 {
2179 as_bad ("Illegal operands%s", error_message);
2180 return;
2181 }
2182 }
2183 else
2184 {
2185 /* We have a match. Now see if the architecture is ok. */
2186 int needed_arch_mask = insn->architecture;
2187
2188 if (v9_arg_p)
2189 {
2190 needed_arch_mask &= ~ ((1 << SPARC_OPCODE_ARCH_V9)
2191 | (1 << SPARC_OPCODE_ARCH_V9A));
2192 needed_arch_mask |= (1 << SPARC_OPCODE_ARCH_V9);
2193 }
2194
2195 if (needed_arch_mask & SPARC_OPCODE_SUPPORTED (current_architecture))
2196 ; /* ok */
2197 /* Can we bump up the architecture? */
2198 else if (needed_arch_mask & SPARC_OPCODE_SUPPORTED (max_architecture))
2199 {
2200 enum sparc_opcode_arch_val needed_architecture =
2201 sparc_ffs (SPARC_OPCODE_SUPPORTED (max_architecture)
2202 & needed_arch_mask);
2203
2204 assert (needed_architecture <= SPARC_OPCODE_ARCH_MAX);
2205 if (warn_on_bump
2206 && needed_architecture > warn_after_architecture)
2207 {
2208 as_warn ("architecture bumped from \"%s\" to \"%s\" on \"%s\"",
2209 sparc_opcode_archs[current_architecture].name,
2210 sparc_opcode_archs[needed_architecture].name,
2211 str);
2212 warn_after_architecture = needed_architecture;
2213 }
2214 current_architecture = needed_architecture;
2215 }
2216 /* Conflict. */
2217 /* ??? This seems to be a bit fragile. What if the next entry in
2218 the opcode table is the one we want and it is supported?
2219 It is possible to arrange the table today so that this can't
2220 happen but what about tomorrow? */
2221 else
2222 {
2223 int arch,printed_one_p = 0;
2224 char *p;
2225 char required_archs[SPARC_OPCODE_ARCH_MAX * 16];
2226
2227 /* Create a list of the architectures that support the insn. */
2228 needed_arch_mask &= ~ SPARC_OPCODE_SUPPORTED (max_architecture);
2229 p = required_archs;
2230 arch = sparc_ffs (needed_arch_mask);
2231 while ((1 << arch) <= needed_arch_mask)
2232 {
2233 if ((1 << arch) & needed_arch_mask)
2234 {
2235 if (printed_one_p)
2236 *p++ = '|';
2237 strcpy (p, sparc_opcode_archs[arch].name);
2238 p += strlen (p);
2239 printed_one_p = 1;
2240 }
2241 ++arch;
2242 }
2243
2244 as_bad ("Architecture mismatch on \"%s\".", str);
2245 as_tsktsk (" (Requires %s; requested architecture is %s.)",
2246 required_archs,
2247 sparc_opcode_archs[max_architecture].name);
2248 return;
2249 }
2250 } /* if no match */
2251
2252 break;
2253 } /* forever looking for a match */
2254
2255 the_insn.opcode = opcode;
2256 }
2257
2258 static int
2259 getExpression (str)
2260 char *str;
2261 {
2262 char *save_in;
2263 segT seg;
2264
2265 save_in = input_line_pointer;
2266 input_line_pointer = str;
2267 seg = expression (&the_insn.exp);
2268 if (seg != absolute_section
2269 && seg != text_section
2270 && seg != data_section
2271 && seg != bss_section
2272 && seg != undefined_section)
2273 {
2274 the_insn.error = "bad segment";
2275 expr_end = input_line_pointer;
2276 input_line_pointer = save_in;
2277 return 1;
2278 }
2279 expr_end = input_line_pointer;
2280 input_line_pointer = save_in;
2281 return 0;
2282 } /* getExpression() */
2283
2284
2285 /*
2286 This is identical to the md_atof in m68k.c. I think this is right,
2287 but I'm not sure.
2288
2289 Turn a string in input_line_pointer into a floating point constant of type
2290 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
2291 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
2292 */
2293
2294 /* Equal to MAX_PRECISION in atof-ieee.c */
2295 #define MAX_LITTLENUMS 6
2296
2297 char *
2298 md_atof (type, litP, sizeP)
2299 char type;
2300 char *litP;
2301 int *sizeP;
2302 {
2303 int i,prec;
2304 LITTLENUM_TYPE words[MAX_LITTLENUMS];
2305 char *t;
2306 char *atof_ieee ();
2307
2308 switch (type)
2309 {
2310 case 'f':
2311 case 'F':
2312 case 's':
2313 case 'S':
2314 prec = 2;
2315 break;
2316
2317 case 'd':
2318 case 'D':
2319 case 'r':
2320 case 'R':
2321 prec = 4;
2322 break;
2323
2324 case 'x':
2325 case 'X':
2326 prec = 6;
2327 break;
2328
2329 case 'p':
2330 case 'P':
2331 prec = 6;
2332 break;
2333
2334 default:
2335 *sizeP = 0;
2336 return "Bad call to MD_ATOF()";
2337 }
2338
2339 t = atof_ieee (input_line_pointer, type, words);
2340 if (t)
2341 input_line_pointer = t;
2342 *sizeP = prec * sizeof (LITTLENUM_TYPE);
2343
2344 if (target_big_endian)
2345 {
2346 for (i = 0; i < prec; i++)
2347 {
2348 md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
2349 litP += sizeof (LITTLENUM_TYPE);
2350 }
2351 }
2352 else
2353 {
2354 for (i = prec - 1; i >= 0; i--)
2355 {
2356 md_number_to_chars (litP, (valueT) words[i], sizeof (LITTLENUM_TYPE));
2357 litP += sizeof (LITTLENUM_TYPE);
2358 }
2359 }
2360
2361 return 0;
2362 }
2363
2364 /* Write a value out to the object file, using the appropriate
2365 endianness. */
2366
2367 void
2368 md_number_to_chars (buf, val, n)
2369 char *buf;
2370 valueT val;
2371 int n;
2372 {
2373 if (target_big_endian)
2374 number_to_chars_bigendian (buf, val, n);
2375 else
2376 number_to_chars_littleendian (buf, val, n);
2377 }
2378
2379 /* Apply a fixS to the frags, now that we know the value it ought to
2380 hold. */
2381
2382 int
2383 md_apply_fix3 (fixP, value, segment)
2384 fixS *fixP;
2385 valueT *value;
2386 segT segment;
2387 {
2388 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2389 offsetT val;
2390 long insn;
2391
2392 val = *value;
2393
2394 assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
2395
2396 fixP->fx_addnumber = val; /* Remember value for emit_reloc */
2397
2398 #ifdef OBJ_ELF
2399 /* FIXME: SPARC ELF relocations don't use an addend in the data
2400 field itself. This whole approach should be somehow combined
2401 with the calls to bfd_install_relocation. Also, the value passed
2402 in by fixup_segment includes the value of a defined symbol. We
2403 don't want to include the value of an externally visible symbol. */
2404 if (fixP->fx_addsy != NULL)
2405 {
2406 if (fixP->fx_addsy->sy_used_in_reloc
2407 && (S_IS_EXTERNAL (fixP->fx_addsy)
2408 || S_IS_WEAK (fixP->fx_addsy)
2409 || (sparc_pic_code && ! fixP->fx_pcrel)
2410 || (S_GET_SEGMENT (fixP->fx_addsy) != segment
2411 && ((bfd_get_section_flags (stdoutput,
2412 S_GET_SEGMENT (fixP->fx_addsy))
2413 & SEC_LINK_ONCE) != 0
2414 || strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
2415 ".gnu.linkonce",
2416 sizeof ".gnu.linkonce" - 1) == 0)))
2417 && S_GET_SEGMENT (fixP->fx_addsy) != absolute_section
2418 && S_GET_SEGMENT (fixP->fx_addsy) != undefined_section
2419 && ! bfd_is_com_section (S_GET_SEGMENT (fixP->fx_addsy)))
2420 fixP->fx_addnumber -= S_GET_VALUE (fixP->fx_addsy);
2421 return 1;
2422 }
2423 #endif
2424
2425 /* This is a hack. There should be a better way to
2426 handle this. Probably in terms of howto fields, once
2427 we can look at these fixups in terms of howtos. */
2428 if (fixP->fx_r_type == BFD_RELOC_32_PCREL_S2 && fixP->fx_addsy)
2429 val += fixP->fx_where + fixP->fx_frag->fr_address;
2430
2431 #ifdef OBJ_AOUT
2432 /* FIXME: More ridiculous gas reloc hacking. If we are going to
2433 generate a reloc, then we just want to let the reloc addend set
2434 the value. We do not want to also stuff the addend into the
2435 object file. Including the addend in the object file works when
2436 doing a static link, because the linker will ignore the object
2437 file contents. However, the dynamic linker does not ignore the
2438 object file contents. */
2439 if (fixP->fx_addsy != NULL
2440 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2)
2441 val = 0;
2442
2443 /* When generating PIC code, we do not want an addend for a reloc
2444 against a local symbol. We adjust fx_addnumber to cancel out the
2445 value already included in val, and to also cancel out the
2446 adjustment which bfd_install_relocation will create. */
2447 if (sparc_pic_code
2448 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2
2449 && fixP->fx_addsy != NULL
2450 && ! S_IS_COMMON (fixP->fx_addsy)
2451 && (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) == 0)
2452 fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
2453 #endif
2454
2455 /* If this is a data relocation, just output VAL. */
2456
2457 if (fixP->fx_r_type == BFD_RELOC_16)
2458 {
2459 md_number_to_chars (buf, val, 2);
2460 }
2461 else if (fixP->fx_r_type == BFD_RELOC_32)
2462 {
2463 md_number_to_chars (buf, val, 4);
2464 }
2465 else if (fixP->fx_r_type == BFD_RELOC_64)
2466 {
2467 md_number_to_chars (buf, val, 8);
2468 }
2469 else
2470 {
2471 /* It's a relocation against an instruction. */
2472
2473 if (INSN_BIG_ENDIAN)
2474 insn = bfd_getb32 ((unsigned char *) buf);
2475 else
2476 insn = bfd_getl32 ((unsigned char *) buf);
2477
2478 switch (fixP->fx_r_type)
2479 {
2480 case BFD_RELOC_32_PCREL_S2:
2481 val = val >> 2;
2482 /* FIXME: This increment-by-one deserves a comment of why it's
2483 being done! */
2484 if (! sparc_pic_code
2485 || fixP->fx_addsy == NULL
2486 || (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
2487 ++val;
2488 insn |= val & 0x3fffffff;
2489 break;
2490
2491 case BFD_RELOC_SPARC_11:
2492 if (! in_signed_range (val, 0x7ff))
2493 as_bad ("relocation overflow.");
2494 insn |= val & 0x7ff;
2495 break;
2496
2497 case BFD_RELOC_SPARC_10:
2498 if (! in_signed_range (val, 0x3ff))
2499 as_bad ("relocation overflow.");
2500 insn |= val & 0x3ff;
2501 break;
2502
2503 case BFD_RELOC_SPARC_6:
2504 if (! in_bitfield_range (val, 0x3f))
2505 as_bad ("relocation overflow.");
2506 insn |= val & 0x3f;
2507 break;
2508
2509 case BFD_RELOC_SPARC_5:
2510 if (! in_bitfield_range (val, 0x1f))
2511 as_bad ("relocation overflow.");
2512 insn |= val & 0x1f;
2513 break;
2514
2515 case BFD_RELOC_SPARC_WDISP16:
2516 /* FIXME: simplify */
2517 if (((val > 0) && (val & ~0x3fffc))
2518 || ((val < 0) && (~(val - 1) & ~0x3fffc)))
2519 as_bad ("relocation overflow.");
2520 /* FIXME: The +1 deserves a comment. */
2521 val = (val >> 2) + 1;
2522 insn |= ((val & 0xc000) << 6) | (val & 0x3fff);
2523 break;
2524
2525 case BFD_RELOC_SPARC_WDISP19:
2526 /* FIXME: simplify */
2527 if (((val > 0) && (val & ~0x1ffffc))
2528 || ((val < 0) && (~(val - 1) & ~0x1ffffc)))
2529 as_bad ("relocation overflow.");
2530 /* FIXME: The +1 deserves a comment. */
2531 val = (val >> 2) + 1;
2532 insn |= val & 0x7ffff;
2533 break;
2534
2535 case BFD_RELOC_SPARC_HH22:
2536 val = BSR (val, 32);
2537 /* intentional fallthrough */
2538
2539 case BFD_RELOC_SPARC_LM22:
2540 case BFD_RELOC_HI22:
2541 if (!fixP->fx_addsy)
2542 {
2543 insn |= (val >> 10) & 0x3fffff;
2544 }
2545 else
2546 {
2547 /* FIXME: Need comment explaining why we do this. */
2548 insn &= ~0xffff;
2549 }
2550 break;
2551
2552 case BFD_RELOC_SPARC22:
2553 if (val & ~0x003fffff)
2554 as_bad ("relocation overflow");
2555 insn |= (val & 0x3fffff);
2556 break;
2557
2558 case BFD_RELOC_SPARC_HM10:
2559 val = BSR (val, 32);
2560 /* intentional fallthrough */
2561
2562 case BFD_RELOC_LO10:
2563 if (!fixP->fx_addsy)
2564 {
2565 insn |= val & 0x3ff;
2566 }
2567 else
2568 {
2569 /* FIXME: Need comment explaining why we do this. */
2570 insn &= ~0xff;
2571 }
2572 break;
2573
2574 case BFD_RELOC_SPARC13:
2575 if (! in_signed_range (val, 0x1fff))
2576 as_bad ("relocation overflow");
2577 insn |= val & 0x1fff;
2578 break;
2579
2580 case BFD_RELOC_SPARC_WDISP22:
2581 val = (val >> 2) + 1;
2582 /* FALLTHROUGH */
2583 case BFD_RELOC_SPARC_BASE22:
2584 insn |= val & 0x3fffff;
2585 break;
2586
2587 case BFD_RELOC_NONE:
2588 default:
2589 as_bad ("bad or unhandled relocation type: 0x%02x", fixP->fx_r_type);
2590 break;
2591 }
2592
2593 if (INSN_BIG_ENDIAN)
2594 bfd_putb32 (insn, (unsigned char *) buf);
2595 else
2596 bfd_putl32 (insn, (unsigned char *) buf);
2597 }
2598
2599 /* Are we finished with this relocation now? */
2600 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
2601 fixP->fx_done = 1;
2602
2603 return 1;
2604 }
2605
2606 /* Translate internal representation of relocation info to BFD target
2607 format. */
2608 arelent *
2609 tc_gen_reloc (section, fixp)
2610 asection *section;
2611 fixS *fixp;
2612 {
2613 arelent *reloc;
2614 bfd_reloc_code_real_type code;
2615
2616 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
2617 assert (reloc != 0);
2618
2619 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
2620 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2621
2622 switch (fixp->fx_r_type)
2623 {
2624 case BFD_RELOC_16:
2625 case BFD_RELOC_32:
2626 case BFD_RELOC_HI22:
2627 case BFD_RELOC_LO10:
2628 case BFD_RELOC_32_PCREL_S2:
2629 case BFD_RELOC_SPARC13:
2630 case BFD_RELOC_SPARC_BASE13:
2631 case BFD_RELOC_SPARC_WDISP16:
2632 case BFD_RELOC_SPARC_WDISP19:
2633 case BFD_RELOC_SPARC_WDISP22:
2634 case BFD_RELOC_64:
2635 case BFD_RELOC_SPARC_5:
2636 case BFD_RELOC_SPARC_6:
2637 case BFD_RELOC_SPARC_10:
2638 case BFD_RELOC_SPARC_11:
2639 case BFD_RELOC_SPARC_HH22:
2640 case BFD_RELOC_SPARC_HM10:
2641 case BFD_RELOC_SPARC_LM22:
2642 case BFD_RELOC_SPARC_PC_HH22:
2643 case BFD_RELOC_SPARC_PC_HM10:
2644 case BFD_RELOC_SPARC_PC_LM22:
2645 code = fixp->fx_r_type;
2646 break;
2647 default:
2648 abort ();
2649 }
2650
2651 #if defined (OBJ_ELF) || defined (OBJ_AOUT)
2652 /* If we are generating PIC code, we need to generate a different
2653 set of relocs. */
2654
2655 #ifdef OBJ_ELF
2656 #define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
2657 #else
2658 #define GOT_NAME "__GLOBAL_OFFSET_TABLE_"
2659 #endif
2660
2661 if (sparc_pic_code)
2662 {
2663 switch (code)
2664 {
2665 case BFD_RELOC_32_PCREL_S2:
2666 if (! S_IS_DEFINED (fixp->fx_addsy)
2667 || S_IS_EXTERNAL (fixp->fx_addsy)
2668 || S_IS_WEAK (fixp->fx_addsy))
2669 code = BFD_RELOC_SPARC_WPLT30;
2670 break;
2671 case BFD_RELOC_HI22:
2672 if (fixp->fx_addsy != NULL
2673 && strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
2674 code = BFD_RELOC_SPARC_PC22;
2675 else
2676 code = BFD_RELOC_SPARC_GOT22;
2677 break;
2678 case BFD_RELOC_LO10:
2679 if (fixp->fx_addsy != NULL
2680 && strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
2681 code = BFD_RELOC_SPARC_PC10;
2682 else
2683 code = BFD_RELOC_SPARC_GOT10;
2684 break;
2685 case BFD_RELOC_SPARC13:
2686 code = BFD_RELOC_SPARC_GOT13;
2687 break;
2688 default:
2689 break;
2690 }
2691 }
2692 #endif /* defined (OBJ_ELF) || defined (OBJ_AOUT) */
2693
2694 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2695 if (reloc->howto == 0)
2696 {
2697 as_bad_where (fixp->fx_file, fixp->fx_line,
2698 "internal error: can't export reloc type %d (`%s')",
2699 fixp->fx_r_type, bfd_get_reloc_code_name (code));
2700 return 0;
2701 }
2702
2703 /* @@ Why fx_addnumber sometimes and fx_offset other times? */
2704 #ifdef OBJ_AOUT
2705
2706 if (reloc->howto->pc_relative == 0
2707 || code == BFD_RELOC_SPARC_PC10
2708 || code == BFD_RELOC_SPARC_PC22)
2709 reloc->addend = fixp->fx_addnumber;
2710 else
2711 reloc->addend = fixp->fx_offset - reloc->address;
2712
2713 #else /* elf or coff */
2714
2715 if (reloc->howto->pc_relative == 0
2716 || code == BFD_RELOC_SPARC_PC10
2717 || code == BFD_RELOC_SPARC_PC22)
2718 reloc->addend = fixp->fx_addnumber;
2719 else if ((fixp->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
2720 reloc->addend = (section->vma
2721 + fixp->fx_addnumber
2722 + md_pcrel_from (fixp));
2723 else
2724 reloc->addend = fixp->fx_offset;
2725 #endif
2726
2727 return reloc;
2728 }
2729
2730
2731 #if 0
2732 /* for debugging only */
2733 static void
2734 print_insn (insn)
2735 struct sparc_it *insn;
2736 {
2737 const char *const Reloc[] = {
2738 "RELOC_8",
2739 "RELOC_16",
2740 "RELOC_32",
2741 "RELOC_DISP8",
2742 "RELOC_DISP16",
2743 "RELOC_DISP32",
2744 "RELOC_WDISP30",
2745 "RELOC_WDISP22",
2746 "RELOC_HI22",
2747 "RELOC_22",
2748 "RELOC_13",
2749 "RELOC_LO10",
2750 "RELOC_SFA_BASE",
2751 "RELOC_SFA_OFF13",
2752 "RELOC_BASE10",
2753 "RELOC_BASE13",
2754 "RELOC_BASE22",
2755 "RELOC_PC10",
2756 "RELOC_PC22",
2757 "RELOC_JMP_TBL",
2758 "RELOC_SEGOFF16",
2759 "RELOC_GLOB_DAT",
2760 "RELOC_JMP_SLOT",
2761 "RELOC_RELATIVE",
2762 "NO_RELOC"
2763 };
2764
2765 if (insn->error)
2766 fprintf (stderr, "ERROR: %s\n");
2767 fprintf (stderr, "opcode=0x%08x\n", insn->opcode);
2768 fprintf (stderr, "reloc = %s\n", Reloc[insn->reloc]);
2769 fprintf (stderr, "exp = {\n");
2770 fprintf (stderr, "\t\tX_add_symbol = %s\n",
2771 ((insn->exp.X_add_symbol != NULL)
2772 ? ((S_GET_NAME (insn->exp.X_add_symbol) != NULL)
2773 ? S_GET_NAME (insn->exp.X_add_symbol)
2774 : "???")
2775 : "0"));
2776 fprintf (stderr, "\t\tX_sub_symbol = %s\n",
2777 ((insn->exp.X_op_symbol != NULL)
2778 ? (S_GET_NAME (insn->exp.X_op_symbol)
2779 ? S_GET_NAME (insn->exp.X_op_symbol)
2780 : "???")
2781 : "0"));
2782 fprintf (stderr, "\t\tX_add_number = %d\n",
2783 insn->exp.X_add_number);
2784 fprintf (stderr, "}\n");
2785 }
2786 #endif
2787 \f
2788 /*
2789 * md_parse_option
2790 * Invocation line includes a switch not recognized by the base assembler.
2791 * See if it's a processor-specific option. These are:
2792 *
2793 * -bump
2794 * Warn on architecture bumps. See also -A.
2795 *
2796 * -Av6, -Av7, -Av8, -Av9, -Av9a, -Asparclite
2797 * -xarch=v8plus, -xarch=v8plusa
2798 * Select the architecture. Instructions or features not
2799 * supported by the selected architecture cause fatal errors.
2800 *
2801 * The default is to start at v6, and bump the architecture up
2802 * whenever an instruction is seen at a higher level. If 32 bit
2803 * environments, v9 is not bumped up to, the user must pass -Av9.
2804 *
2805 * -xarch=v8plus{,a} is for compatibility with the Sun assembler.
2806 *
2807 * If -bump is specified, a warning is printing when bumping to
2808 * higher levels.
2809 *
2810 * If an architecture is specified, all instructions must match
2811 * that architecture. Any higher level instructions are flagged
2812 * as errors. Note that in the 32 bit environment specifying
2813 * -Av9 does not automatically create a v9 object file, a v9
2814 * insn must be seen.
2815 *
2816 * If both an architecture and -bump are specified, the
2817 * architecture starts at the specified level, but bumps are
2818 * warnings. Note that we can't set `current_architecture' to
2819 * the requested level in this case: in the 32 bit environment,
2820 * we still must avoid creating v9 object files unless v9 insns
2821 * are seen.
2822 *
2823 * Note:
2824 * Bumping between incompatible architectures is always an
2825 * error. For example, from sparclite to v9.
2826 */
2827
2828 #ifdef OBJ_ELF
2829 CONST char *md_shortopts = "A:K:VQ:sq";
2830 #else
2831 #ifdef OBJ_AOUT
2832 CONST char *md_shortopts = "A:k";
2833 #else
2834 CONST char *md_shortopts = "A:";
2835 #endif
2836 #endif
2837 struct option md_longopts[] = {
2838 #define OPTION_BUMP (OPTION_MD_BASE)
2839 {"bump", no_argument, NULL, OPTION_BUMP},
2840 #define OPTION_SPARC (OPTION_MD_BASE + 1)
2841 {"sparc", no_argument, NULL, OPTION_SPARC},
2842 #define OPTION_XARCH (OPTION_MD_BASE + 2)
2843 {"xarch", required_argument, NULL, OPTION_XARCH},
2844 #ifdef SPARC_BIENDIAN
2845 #define OPTION_LITTLE_ENDIAN (OPTION_MD_BASE + 3)
2846 {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
2847 #define OPTION_BIG_ENDIAN (OPTION_MD_BASE + 4)
2848 {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
2849 #endif
2850 #define OPTION_ENFORCE_ALIGNED_DATA (OPTION_MD_BASE + 5)
2851 {"enforce-aligned-data", no_argument, NULL, OPTION_ENFORCE_ALIGNED_DATA},
2852 {NULL, no_argument, NULL, 0}
2853 };
2854 size_t md_longopts_size = sizeof(md_longopts);
2855
2856 int
2857 md_parse_option (c, arg)
2858 int c;
2859 char *arg;
2860 {
2861 switch (c)
2862 {
2863 case OPTION_BUMP:
2864 warn_on_bump = 1;
2865 warn_after_architecture = SPARC_OPCODE_ARCH_V6;
2866 break;
2867
2868 case OPTION_XARCH:
2869 /* ??? We could add v8plus and v8plusa to sparc_opcode_archs.
2870 But we might want v8plus to mean something different than v9
2871 someday, and we'd recognize more -xarch options than Sun's
2872 assembler does (which may lead to a conflict someday). */
2873 if (strcmp (arg, "v8plus") == 0)
2874 arg = "v9";
2875 else if (strcmp (arg, "v8plusa") == 0)
2876 arg = "v9a";
2877 else
2878 {
2879 as_bad ("invalid architecture -xarch=%s", arg);
2880 return 0;
2881 }
2882
2883 /* fall through */
2884
2885 case 'A':
2886 {
2887 enum sparc_opcode_arch_val new_arch = sparc_opcode_lookup_arch (arg);
2888
2889 if (new_arch == SPARC_OPCODE_ARCH_BAD)
2890 {
2891 as_bad ("invalid architecture -A%s", arg);
2892 return 0;
2893 }
2894 else
2895 {
2896 max_architecture = new_arch;
2897 architecture_requested = 1;
2898 }
2899 }
2900 break;
2901
2902 case OPTION_SPARC:
2903 /* Ignore -sparc, used by SunOS make default .s.o rule. */
2904 break;
2905
2906 case OPTION_ENFORCE_ALIGNED_DATA:
2907 enforce_aligned_data = 1;
2908 break;
2909
2910 #ifdef SPARC_BIENDIAN
2911 case OPTION_LITTLE_ENDIAN:
2912 target_big_endian = 0;
2913 break;
2914 case OPTION_BIG_ENDIAN:
2915 target_big_endian = 1;
2916 break;
2917 #endif
2918
2919 #ifdef OBJ_AOUT
2920 case 'k':
2921 sparc_pic_code = 1;
2922 break;
2923 #endif
2924
2925 #ifdef OBJ_ELF
2926 case 'V':
2927 print_version_id ();
2928 break;
2929
2930 case 'Q':
2931 /* Qy - do emit .comment
2932 Qn - do not emit .comment */
2933 break;
2934
2935 case 's':
2936 /* use .stab instead of .stab.excl */
2937 break;
2938
2939 case 'q':
2940 /* quick -- native assembler does fewer checks */
2941 break;
2942
2943 case 'K':
2944 if (strcmp (arg, "PIC") != 0)
2945 as_warn ("Unrecognized option following -K");
2946 else
2947 sparc_pic_code = 1;
2948 break;
2949 #endif
2950
2951 default:
2952 return 0;
2953 }
2954
2955 return 1;
2956 }
2957
2958 void
2959 md_show_usage (stream)
2960 FILE *stream;
2961 {
2962 const struct sparc_opcode_arch *arch;
2963
2964 fprintf(stream, "SPARC options:\n");
2965 for (arch = &sparc_opcode_archs[0]; arch->name; arch++)
2966 {
2967 if (arch != &sparc_opcode_archs[0])
2968 fprintf (stream, " | ");
2969 fprintf (stream, "-A%s", arch->name);
2970 }
2971 fprintf (stream, "\n-xarch=v8plus | -xarch=v8plusa\n");
2972 fprintf (stream, "\
2973 specify variant of SPARC architecture\n\
2974 -bump warn when assembler switches architectures\n\
2975 -sparc ignored\n
2976 --enforce-aligned-data force .long, etc., to be aligned correctly\n");
2977 #ifdef OBJ_AOUT
2978 fprintf (stream, "\
2979 -k generate PIC\n");
2980 #endif
2981 #ifdef OBJ_ELF
2982 fprintf (stream, "\
2983 -KPIC generate PIC\n\
2984 -V print assembler version number\n\
2985 -q ignored\n\
2986 -Qy, -Qn ignored\n\
2987 -s ignored\n");
2988 #endif
2989 #ifdef SPARC_BIENDIAN
2990 fprintf (stream, "\
2991 -EL generate code for a little endian machine\n\
2992 -EB generate code for a big endian machine\n");
2993 #endif
2994 }
2995 \f
2996 /* We have no need to default values of symbols. */
2997
2998 /* ARGSUSED */
2999 symbolS *
3000 md_undefined_symbol (name)
3001 char *name;
3002 {
3003 return 0;
3004 } /* md_undefined_symbol() */
3005
3006 /* Round up a section size to the appropriate boundary. */
3007 valueT
3008 md_section_align (segment, size)
3009 segT segment;
3010 valueT size;
3011 {
3012 #ifndef OBJ_ELF
3013 /* This is not right for ELF; a.out wants it, and COFF will force
3014 the alignment anyways. */
3015 valueT align = ((valueT) 1
3016 << (valueT) bfd_get_section_alignment (stdoutput, segment));
3017 valueT newsize;
3018 /* turn alignment value into a mask */
3019 align--;
3020 newsize = (size + align) & ~align;
3021 return newsize;
3022 #else
3023 return size;
3024 #endif
3025 }
3026
3027 /* Exactly what point is a PC-relative offset relative TO?
3028 On the sparc, they're relative to the address of the offset, plus
3029 its size. This gets us to the following instruction.
3030 (??? Is this right? FIXME-SOON) */
3031 long
3032 md_pcrel_from (fixP)
3033 fixS *fixP;
3034 {
3035 long ret;
3036
3037 ret = fixP->fx_where + fixP->fx_frag->fr_address;
3038 if (! sparc_pic_code
3039 || fixP->fx_addsy == NULL
3040 || (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
3041 ret += fixP->fx_size;
3042 return ret;
3043 }
3044
3045 /* end of tc-sparc.c */
This page took 0.108567 seconds and 4 git commands to generate.