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