source: trunk/binutils/gas/config/tc-sparc.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 109.3 KB
Line 
1/* tc-sparc.c -- Assemble for the SPARC
2 Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public
18 License along with GAS; see the file COPYING. If not, write
19 to the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include <stdio.h>
23
24#include "as.h"
25#include "safe-ctype.h"
26#include "subsegs.h"
27
28#include "opcode/sparc.h"
29
30#ifdef OBJ_ELF
31#include "elf/sparc.h"
32#include "dwarf2dbg.h"
33#endif
34
35/* Some ancient Sun C compilers would not take such hex constants as
36 unsigned, and would end up sign-extending them to form an offsetT,
37 so use these constants instead. */
38#define U0xffffffff ((((unsigned long) 1 << 16) << 16) - 1)
39#define U0x80000000 ((((unsigned long) 1 << 16) << 15))
40
41static struct sparc_arch *lookup_arch PARAMS ((char *));
42static void init_default_arch PARAMS ((void));
43static int sparc_ip PARAMS ((char *, const struct sparc_opcode **));
44static int in_signed_range PARAMS ((bfd_signed_vma, bfd_signed_vma));
45static int in_unsigned_range PARAMS ((bfd_vma, bfd_vma));
46static int in_bitfield_range PARAMS ((bfd_signed_vma, bfd_signed_vma));
47static int sparc_ffs PARAMS ((unsigned int));
48static void synthetize_setuw PARAMS ((const struct sparc_opcode *));
49static void synthetize_setsw PARAMS ((const struct sparc_opcode *));
50static void synthetize_setx PARAMS ((const struct sparc_opcode *));
51static bfd_vma BSR PARAMS ((bfd_vma, int));
52static int cmp_reg_entry PARAMS ((const PTR, const PTR));
53static int parse_keyword_arg PARAMS ((int (*) (const char *), char **, int *));
54static int parse_const_expr_arg PARAMS ((char **, int *));
55static int get_expression PARAMS ((char *str));
56
57/* Default architecture. */
58/* ??? The default value should be V8, but sparclite support was added
59 by making it the default. GCC now passes -Asparclite, so maybe sometime in
60 the future we can set this to V8. */
61#ifndef DEFAULT_ARCH
62#define DEFAULT_ARCH "sparclite"
63#endif
64static char *default_arch = DEFAULT_ARCH;
65
66/* Non-zero if the initial values of `max_architecture' and `sparc_arch_size'
67 have been set. */
68static int default_init_p;
69
70/* Current architecture. We don't bump up unless necessary. */
71static enum sparc_opcode_arch_val current_architecture = SPARC_OPCODE_ARCH_V6;
72
73/* The maximum architecture level we can bump up to.
74 In a 32 bit environment, don't allow bumping up to v9 by default.
75 The native assembler works this way. The user is required to pass
76 an explicit argument before we'll create v9 object files. However, if
77 we don't see any v9 insns, a v8plus object file is not created. */
78static enum sparc_opcode_arch_val max_architecture;
79
80/* Either 32 or 64, selects file format. */
81static int sparc_arch_size;
82/* Initial (default) value, recorded separately in case a user option
83 changes the value before md_show_usage is called. */
84static int default_arch_size;
85
86#ifdef OBJ_ELF
87/* The currently selected v9 memory model. Currently only used for
88 ELF. */
89static enum { MM_TSO, MM_PSO, MM_RMO } sparc_memory_model = MM_RMO;
90#endif
91
92static int architecture_requested;
93static int warn_on_bump;
94
95/* If warn_on_bump and the needed architecture is higher than this
96 architecture, issue a warning. */
97static enum sparc_opcode_arch_val warn_after_architecture;
98
99/* Non-zero if as should generate error if an undeclared g[23] register
100 has been used in -64. */
101static int no_undeclared_regs;
102
103/* Non-zero if we should try to relax jumps and calls. */
104static int sparc_relax;
105
106/* Non-zero if we are generating PIC code. */
107int sparc_pic_code;
108
109/* Non-zero if we should give an error when misaligned data is seen. */
110static int enforce_aligned_data;
111
112extern int target_big_endian;
113
114static int target_little_endian_data;
115
116/* Symbols for global registers on v9. */
117static symbolS *globals[8];
118
119/* V9 and 86x have big and little endian data, but instructions are always big
120 endian. The sparclet has bi-endian support but both data and insns have
121 the same endianness. Global `target_big_endian' is used for data.
122 The following macro is used for instructions. */
123#ifndef INSN_BIG_ENDIAN
124#define INSN_BIG_ENDIAN (target_big_endian \
125 || default_arch_type == sparc86x \
126 || SPARC_OPCODE_ARCH_V9_P (max_architecture))
127#endif
128
129/* Handle of the OPCODE hash table. */
130static struct hash_control *op_hash;
131
132static int log2 PARAMS ((int));
133static void s_data1 PARAMS ((void));
134static void s_seg PARAMS ((int));
135static void s_proc PARAMS ((int));
136static void s_reserve PARAMS ((int));
137static void s_common PARAMS ((int));
138static void s_empty PARAMS ((int));
139static void s_uacons PARAMS ((int));
140static void s_ncons PARAMS ((int));
141#ifdef OBJ_ELF
142static void s_register PARAMS ((int));
143#endif
144
145const pseudo_typeS md_pseudo_table[] =
146{
147 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0). */
148 {"common", s_common, 0},
149 {"empty", s_empty, 0},
150 {"global", s_globl, 0},
151 {"half", cons, 2},
152 {"nword", s_ncons, 0},
153 {"optim", s_ignore, 0},
154 {"proc", s_proc, 0},
155 {"reserve", s_reserve, 0},
156 {"seg", s_seg, 0},
157 {"skip", s_space, 0},
158 {"word", cons, 4},
159 {"xword", cons, 8},
160 {"uahalf", s_uacons, 2},
161 {"uaword", s_uacons, 4},
162 {"uaxword", s_uacons, 8},
163#ifdef OBJ_ELF
164 {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
165 {"loc", dwarf2_directive_loc, 0},
166 /* These are specific to sparc/svr4. */
167 {"2byte", s_uacons, 2},
168 {"4byte", s_uacons, 4},
169 {"8byte", s_uacons, 8},
170 {"register", s_register, 0},
171#endif
172 {NULL, 0, 0},
173};
174
175/* Size of relocation record. */
176const int md_reloc_size = 12;
177
178/* This array holds the chars that always start a comment. If the
179 pre-processor is disabled, these aren't very useful. */
180const char comment_chars[] = "!"; /* JF removed '|' from
181 comment_chars. */
182
183/* This array holds the chars that only start a comment at the beginning of
184 a line. If the line seems to have the form '# 123 filename'
185 .line and .file directives will appear in the pre-processed output. */
186/* Note that input_file.c hand checks for '#' at the beginning of the
187 first line of the input file. This is because the compiler outputs
188 #NO_APP at the beginning of its output. */
189/* Also note that comments started like this one will always
190 work if '/' isn't otherwise defined. */
191const char line_comment_chars[] = "#";
192
193const char line_separator_chars[] = ";";
194
195/* Chars that can be used to separate mant from exp in floating point
196 nums. */
197const char EXP_CHARS[] = "eE";
198
199/* Chars that mean this number is a floating point constant.
200 As in 0f12.456
201 or 0d1.2345e12 */
202const char FLT_CHARS[] = "rRsSfFdDxXpP";
203
204/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
205 changed in read.c. Ideally it shouldn't have to know about it at all,
206 but nothing is ideal around here. */
207
208#define isoctal(c) ((unsigned) ((c) - '0') < '8')
209
210struct sparc_it
211 {
212 char *error;
213 unsigned long opcode;
214 struct nlist *nlistp;
215 expressionS exp;
216 expressionS exp2;
217 int pcrel;
218 bfd_reloc_code_real_type reloc;
219 };
220
221struct sparc_it the_insn, set_insn;
222
223static void output_insn
224 PARAMS ((const struct sparc_opcode *, struct sparc_it *));
225
226
227/* Table of arguments to -A.
228 The sparc_opcode_arch table in sparc-opc.c is insufficient and incorrect
229 for this use. That table is for opcodes only. This table is for opcodes
230 and file formats. */
231
232enum sparc_arch_types {v6, v7, v8, sparclet, sparclite, sparc86x, v8plus,
233 v8plusa, v9, v9a, v9b, v9_64};
234
235static struct sparc_arch {
236 char *name;
237 char *opcode_arch;
238 enum sparc_arch_types arch_type;
239 /* Default word size, as specified during configuration.
240 A value of zero means can't be used to specify default architecture. */
241 int default_arch_size;
242 /* Allowable arg to -A? */
243 int user_option_p;
244} sparc_arch_table[] = {
245 { "v6", "v6", v6, 0, 1 },
246 { "v7", "v7", v7, 0, 1 },
247 { "v8", "v8", v8, 32, 1 },
248 { "sparclet", "sparclet", sparclet, 32, 1 },
249 { "sparclite", "sparclite", sparclite, 32, 1 },
250 { "sparc86x", "sparclite", sparc86x, 32, 1 },
251 { "v8plus", "v9", v9, 0, 1 },
252 { "v8plusa", "v9a", v9, 0, 1 },
253 { "v8plusb", "v9b", v9, 0, 1 },
254 { "v9", "v9", v9, 0, 1 },
255 { "v9a", "v9a", v9, 0, 1 },
256 { "v9b", "v9b", v9, 0, 1 },
257 /* This exists to allow configure.in/Makefile.in to pass one
258 value to specify both the default machine and default word size. */
259 { "v9-64", "v9", v9, 64, 0 },
260 { NULL, NULL, v8, 0, 0 }
261};
262
263/* Variant of default_arch */
264static enum sparc_arch_types default_arch_type;
265
266static struct sparc_arch *
267lookup_arch (name)
268 char *name;
269{
270 struct sparc_arch *sa;
271
272 for (sa = &sparc_arch_table[0]; sa->name != NULL; sa++)
273 if (strcmp (sa->name, name) == 0)
274 break;
275 if (sa->name == NULL)
276 return NULL;
277 return sa;
278}
279
280/* Initialize the default opcode arch and word size from the default
281 architecture name. */
282
283static void
284init_default_arch ()
285{
286 struct sparc_arch *sa = lookup_arch (default_arch);
287
288 if (sa == NULL
289 || sa->default_arch_size == 0)
290 as_fatal (_("Invalid default architecture, broken assembler."));
291
292 max_architecture = sparc_opcode_lookup_arch (sa->opcode_arch);
293 if (max_architecture == SPARC_OPCODE_ARCH_BAD)
294 as_fatal (_("Bad opcode table, broken assembler."));
295 default_arch_size = sparc_arch_size = sa->default_arch_size;
296 default_init_p = 1;
297 default_arch_type = sa->arch_type;
298}
299
300/* Called by TARGET_FORMAT. */
301
302const char *
303sparc_target_format ()
304{
305 /* We don't get a chance to initialize anything before we're called,
306 so handle that now. */
307 if (! default_init_p)
308 init_default_arch ();
309
310#ifdef OBJ_AOUT
311#ifdef TE_NetBSD
312 return "a.out-sparc-netbsd";
313#else
314#ifdef TE_SPARCAOUT
315 if (target_big_endian)
316 return "a.out-sunos-big";
317 else if (default_arch_type == sparc86x && target_little_endian_data)
318 return "a.out-sunos-big";
319 else
320 return "a.out-sparc-little";
321#else
322 return "a.out-sunos-big";
323#endif
324#endif
325#endif
326
327#ifdef OBJ_BOUT
328 return "b.out.big";
329#endif
330
331#ifdef OBJ_COFF
332#ifdef TE_LYNX
333 return "coff-sparc-lynx";
334#else
335 return "coff-sparc";
336#endif
337#endif
338
339#ifdef OBJ_ELF
340 return sparc_arch_size == 64 ? "elf64-sparc" : "elf32-sparc";
341#endif
342
343 abort ();
344}
345
346
347/* md_parse_option
348 * Invocation line includes a switch not recognized by the base assembler.
349 * See if it's a processor-specific option. These are:
350 *
351 * -bump
352 * Warn on architecture bumps. See also -A.
353 *
354 * -Av6, -Av7, -Av8, -Asparclite, -Asparclet
355 * Standard 32 bit architectures.
356 * -Av9, -Av9a, -Av9b
357 * Sparc64 in either a 32 or 64 bit world (-32/-64 says which).
358 * This used to only mean 64 bits, but properly specifying it
359 * complicated gcc's ASM_SPECs, so now opcode selection is
360 * specified orthogonally to word size (except when specifying
361 * the default, but that is an internal implementation detail).
362 * -Av8plus, -Av8plusa, -Av8plusb
363 * Same as -Av9{,a,b}.
364 * -xarch=v8plus, -xarch=v8plusa, -xarch=v8plusb
365 * Same as -Av8plus{,a,b} -32, for compatibility with Sun's
366 * assembler.
367 * -xarch=v9, -xarch=v9a, -xarch=v9b
368 * Same as -Av9{,a,b} -64, for compatibility with Sun's
369 * assembler.
370 *
371 * Select the architecture and possibly the file format.
372 * Instructions or features not supported by the selected
373 * architecture cause fatal errors.
374 *
375 * The default is to start at v6, and bump the architecture up
376 * whenever an instruction is seen at a higher level. In 32 bit
377 * environments, v9 is not bumped up to, the user must pass
378 * -Av8plus{,a,b}.
379 *
380 * If -bump is specified, a warning is printing when bumping to
381 * higher levels.
382 *
383 * If an architecture is specified, all instructions must match
384 * that architecture. Any higher level instructions are flagged
385 * as errors. Note that in the 32 bit environment specifying
386 * -Av8plus does not automatically create a v8plus object file, a
387 * v9 insn must be seen.
388 *
389 * If both an architecture and -bump are specified, the
390 * architecture starts at the specified level, but bumps are
391 * warnings. Note that we can't set `current_architecture' to
392 * the requested level in this case: in the 32 bit environment,
393 * we still must avoid creating v8plus object files unless v9
394 * insns are seen.
395 *
396 * Note:
397 * Bumping between incompatible architectures is always an
398 * error. For example, from sparclite to v9.
399 */
400
401#ifdef OBJ_ELF
402const char *md_shortopts = "A:K:VQ:sq";
403#else
404#ifdef OBJ_AOUT
405const char *md_shortopts = "A:k";
406#else
407const char *md_shortopts = "A:";
408#endif
409#endif
410struct option md_longopts[] = {
411#define OPTION_BUMP (OPTION_MD_BASE)
412 {"bump", no_argument, NULL, OPTION_BUMP},
413#define OPTION_SPARC (OPTION_MD_BASE + 1)
414 {"sparc", no_argument, NULL, OPTION_SPARC},
415#define OPTION_XARCH (OPTION_MD_BASE + 2)
416 {"xarch", required_argument, NULL, OPTION_XARCH},
417#ifdef OBJ_ELF
418#define OPTION_32 (OPTION_MD_BASE + 3)
419 {"32", no_argument, NULL, OPTION_32},
420#define OPTION_64 (OPTION_MD_BASE + 4)
421 {"64", no_argument, NULL, OPTION_64},
422#define OPTION_TSO (OPTION_MD_BASE + 5)
423 {"TSO", no_argument, NULL, OPTION_TSO},
424#define OPTION_PSO (OPTION_MD_BASE + 6)
425 {"PSO", no_argument, NULL, OPTION_PSO},
426#define OPTION_RMO (OPTION_MD_BASE + 7)
427 {"RMO", no_argument, NULL, OPTION_RMO},
428#endif
429#ifdef SPARC_BIENDIAN
430#define OPTION_LITTLE_ENDIAN (OPTION_MD_BASE + 8)
431 {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
432#define OPTION_BIG_ENDIAN (OPTION_MD_BASE + 9)
433 {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
434#endif
435#define OPTION_ENFORCE_ALIGNED_DATA (OPTION_MD_BASE + 10)
436 {"enforce-aligned-data", no_argument, NULL, OPTION_ENFORCE_ALIGNED_DATA},
437#define OPTION_LITTLE_ENDIAN_DATA (OPTION_MD_BASE + 11)
438 {"little-endian-data", no_argument, NULL, OPTION_LITTLE_ENDIAN_DATA},
439#ifdef OBJ_ELF
440#define OPTION_NO_UNDECLARED_REGS (OPTION_MD_BASE + 12)
441 {"no-undeclared-regs", no_argument, NULL, OPTION_NO_UNDECLARED_REGS},
442#define OPTION_UNDECLARED_REGS (OPTION_MD_BASE + 13)
443 {"undeclared-regs", no_argument, NULL, OPTION_UNDECLARED_REGS},
444#endif
445#define OPTION_RELAX (OPTION_MD_BASE + 14)
446 {"relax", no_argument, NULL, OPTION_RELAX},
447#define OPTION_NO_RELAX (OPTION_MD_BASE + 15)
448 {"no-relax", no_argument, NULL, OPTION_NO_RELAX},
449 {NULL, no_argument, NULL, 0}
450};
451
452size_t md_longopts_size = sizeof (md_longopts);
453
454int
455md_parse_option (c, arg)
456 int c;
457 char *arg;
458{
459 /* We don't get a chance to initialize anything before we're called,
460 so handle that now. */
461 if (! default_init_p)
462 init_default_arch ();
463
464 switch (c)
465 {
466 case OPTION_BUMP:
467 warn_on_bump = 1;
468 warn_after_architecture = SPARC_OPCODE_ARCH_V6;
469 break;
470
471 case OPTION_XARCH:
472#ifdef OBJ_ELF
473 if (strncmp (arg, "v9", 2) != 0)
474 md_parse_option (OPTION_32, NULL);
475 else
476 md_parse_option (OPTION_64, NULL);
477#endif
478 /* Fall through. */
479
480 case 'A':
481 {
482 struct sparc_arch *sa;
483 enum sparc_opcode_arch_val opcode_arch;
484
485 sa = lookup_arch (arg);
486 if (sa == NULL
487 || ! sa->user_option_p)
488 {
489 if (c == OPTION_XARCH)
490 as_bad (_("invalid architecture -xarch=%s"), arg);
491 else
492 as_bad (_("invalid architecture -A%s"), arg);
493 return 0;
494 }
495
496 opcode_arch = sparc_opcode_lookup_arch (sa->opcode_arch);
497 if (opcode_arch == SPARC_OPCODE_ARCH_BAD)
498 as_fatal (_("Bad opcode table, broken assembler."));
499
500 max_architecture = opcode_arch;
501 architecture_requested = 1;
502 }
503 break;
504
505 case OPTION_SPARC:
506 /* Ignore -sparc, used by SunOS make default .s.o rule. */
507 break;
508
509 case OPTION_ENFORCE_ALIGNED_DATA:
510 enforce_aligned_data = 1;
511 break;
512
513#ifdef SPARC_BIENDIAN
514 case OPTION_LITTLE_ENDIAN:
515 target_big_endian = 0;
516 if (default_arch_type != sparclet)
517 as_fatal ("This target does not support -EL");
518 break;
519 case OPTION_LITTLE_ENDIAN_DATA:
520 target_little_endian_data = 1;
521 target_big_endian = 0;
522 if (default_arch_type != sparc86x
523 && default_arch_type != v9)
524 as_fatal ("This target does not support --little-endian-data");
525 break;
526 case OPTION_BIG_ENDIAN:
527 target_big_endian = 1;
528 break;
529#endif
530
531#ifdef OBJ_AOUT
532 case 'k':
533 sparc_pic_code = 1;
534 break;
535#endif
536
537#ifdef OBJ_ELF
538 case OPTION_32:
539 case OPTION_64:
540 {
541 const char **list, **l;
542
543 sparc_arch_size = c == OPTION_32 ? 32 : 64;
544 list = bfd_target_list ();
545 for (l = list; *l != NULL; l++)
546 {
547 if (sparc_arch_size == 32)
548 {
549 if (strcmp (*l, "elf32-sparc") == 0)
550 break;
551 }
552 else
553 {
554 if (strcmp (*l, "elf64-sparc") == 0)
555 break;
556 }
557 }
558 if (*l == NULL)
559 as_fatal (_("No compiled in support for %d bit object file format"),
560 sparc_arch_size);
561 free (list);
562 }
563 break;
564
565 case OPTION_TSO:
566 sparc_memory_model = MM_TSO;
567 break;
568
569 case OPTION_PSO:
570 sparc_memory_model = MM_PSO;
571 break;
572
573 case OPTION_RMO:
574 sparc_memory_model = MM_RMO;
575 break;
576
577 case 'V':
578 print_version_id ();
579 break;
580
581 case 'Q':
582 /* Qy - do emit .comment
583 Qn - do not emit .comment. */
584 break;
585
586 case 's':
587 /* Use .stab instead of .stab.excl. */
588 break;
589
590 case 'q':
591 /* quick -- Native assembler does fewer checks. */
592 break;
593
594 case 'K':
595 if (strcmp (arg, "PIC") != 0)
596 as_warn (_("Unrecognized option following -K"));
597 else
598 sparc_pic_code = 1;
599 break;
600
601 case OPTION_NO_UNDECLARED_REGS:
602 no_undeclared_regs = 1;
603 break;
604
605 case OPTION_UNDECLARED_REGS:
606 no_undeclared_regs = 0;
607 break;
608#endif
609
610 case OPTION_RELAX:
611 sparc_relax = 1;
612 break;
613
614 case OPTION_NO_RELAX:
615 sparc_relax = 0;
616 break;
617
618 default:
619 return 0;
620 }
621
622 return 1;
623}
624
625void
626md_show_usage (stream)
627 FILE *stream;
628{
629 const struct sparc_arch *arch;
630 int column;
631
632 /* We don't get a chance to initialize anything before we're called,
633 so handle that now. */
634 if (! default_init_p)
635 init_default_arch ();
636
637 fprintf (stream, _("SPARC options:\n"));
638 column = 0;
639 for (arch = &sparc_arch_table[0]; arch->name; arch++)
640 {
641 if (!arch->user_option_p)
642 continue;
643 if (arch != &sparc_arch_table[0])
644 fprintf (stream, " | ");
645 if (column + strlen (arch->name) > 70)
646 {
647 column = 0;
648 fputc ('\n', stream);
649 }
650 column += 5 + 2 + strlen (arch->name);
651 fprintf (stream, "-A%s", arch->name);
652 }
653 for (arch = &sparc_arch_table[0]; arch->name; arch++)
654 {
655 if (!arch->user_option_p)
656 continue;
657 fprintf (stream, " | ");
658 if (column + strlen (arch->name) > 65)
659 {
660 column = 0;
661 fputc ('\n', stream);
662 }
663 column += 5 + 7 + strlen (arch->name);
664 fprintf (stream, "-xarch=%s", arch->name);
665 }
666 fprintf (stream, _("\n\
667 specify variant of SPARC architecture\n\
668-bump warn when assembler switches architectures\n\
669-sparc ignored\n\
670--enforce-aligned-data force .long, etc., to be aligned correctly\n\
671-relax relax jumps and branches (default)\n\
672-no-relax avoid changing any jumps and branches\n"));
673#ifdef OBJ_AOUT
674 fprintf (stream, _("\
675-k generate PIC\n"));
676#endif
677#ifdef OBJ_ELF
678 fprintf (stream, _("\
679-32 create 32 bit object file\n\
680-64 create 64 bit object file\n"));
681 fprintf (stream, _("\
682 [default is %d]\n"), default_arch_size);
683 fprintf (stream, _("\
684-TSO use Total Store Ordering\n\
685-PSO use Partial Store Ordering\n\
686-RMO use Relaxed Memory Ordering\n"));
687 fprintf (stream, _("\
688 [default is %s]\n"), (default_arch_size == 64) ? "RMO" : "TSO");
689 fprintf (stream, _("\
690-KPIC generate PIC\n\
691-V print assembler version number\n\
692-undeclared-regs ignore application global register usage without\n\
693 appropriate .register directive (default)\n\
694-no-undeclared-regs force error on application global register usage\n\
695 without appropriate .register directive\n\
696-q ignored\n\
697-Qy, -Qn ignored\n\
698-s ignored\n"));
699#endif
700#ifdef SPARC_BIENDIAN
701 fprintf (stream, _("\
702-EL generate code for a little endian machine\n\
703-EB generate code for a big endian machine\n\
704--little-endian-data generate code for a machine having big endian\n\
705 instructions and little endian data.\n"));
706#endif
707}
708
709
710/* Native operand size opcode translation. */
711struct
712 {
713 char *name;
714 char *name32;
715 char *name64;
716 } native_op_table[] =
717{
718 {"ldn", "ld", "ldx"},
719 {"ldna", "lda", "ldxa"},
720 {"stn", "st", "stx"},
721 {"stna", "sta", "stxa"},
722 {"slln", "sll", "sllx"},
723 {"srln", "srl", "srlx"},
724 {"sran", "sra", "srax"},
725 {"casn", "cas", "casx"},
726 {"casna", "casa", "casxa"},
727 {"clrn", "clr", "clrx"},
728 {NULL, NULL, NULL},
729};
730
731
732/* sparc64 priviledged registers. */
733
734struct priv_reg_entry
735{
736 char *name;
737 int regnum;
738};
739
740struct priv_reg_entry priv_reg_table[] =
741{
742 {"tpc", 0},
743 {"tnpc", 1},
744 {"tstate", 2},
745 {"tt", 3},
746 {"tick", 4},
747 {"tba", 5},
748 {"pstate", 6},
749 {"tl", 7},
750 {"pil", 8},
751 {"cwp", 9},
752 {"cansave", 10},
753 {"canrestore", 11},
754 {"cleanwin", 12},
755 {"otherwin", 13},
756 {"wstate", 14},
757 {"fq", 15},
758 {"ver", 31},
759 {"", -1}, /* End marker. */
760};
761
762/* v9a specific asrs. */
763
764struct priv_reg_entry v9a_asr_table[] =
765{
766 {"tick_cmpr", 23},
767 {"sys_tick_cmpr", 25},
768 {"sys_tick", 24},
769 {"softint", 22},
770 {"set_softint", 20},
771 {"pic", 17},
772 {"pcr", 16},
773 {"gsr", 19},
774 {"dcr", 18},
775 {"clear_softint", 21},
776 {"", -1}, /* End marker. */
777};
778
779static int
780cmp_reg_entry (parg, qarg)
781 const PTR parg;
782 const PTR qarg;
783{
784 const struct priv_reg_entry *p = (const struct priv_reg_entry *) parg;
785 const struct priv_reg_entry *q = (const struct priv_reg_entry *) qarg;
786
787 return strcmp (q->name, p->name);
788}
789
790
791/* This function is called once, at assembler startup time. It should
792 set up all the tables, etc. that the MD part of the assembler will
793 need. */
794
795void
796md_begin ()
797{
798 register const char *retval = NULL;
799 int lose = 0;
800 register unsigned int i = 0;
801
802 /* We don't get a chance to initialize anything before md_parse_option
803 is called, and it may not be called, so handle default initialization
804 now if not already done. */
805 if (! default_init_p)
806 init_default_arch ();
807
808 op_hash = hash_new ();
809
810 while (i < (unsigned int) sparc_num_opcodes)
811 {
812 const char *name = sparc_opcodes[i].name;
813 retval = hash_insert (op_hash, name, (PTR) &sparc_opcodes[i]);
814 if (retval != NULL)
815 {
816 as_bad (_("Internal error: can't hash `%s': %s\n"),
817 sparc_opcodes[i].name, retval);
818 lose = 1;
819 }
820 do
821 {
822 if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
823 {
824 as_bad (_("Internal error: losing opcode: `%s' \"%s\"\n"),
825 sparc_opcodes[i].name, sparc_opcodes[i].args);
826 lose = 1;
827 }
828 ++i;
829 }
830 while (i < (unsigned int) sparc_num_opcodes
831 && !strcmp (sparc_opcodes[i].name, name));
832 }
833
834 for (i = 0; native_op_table[i].name; i++)
835 {
836 const struct sparc_opcode *insn;
837 char *name = ((sparc_arch_size == 32)
838 ? native_op_table[i].name32
839 : native_op_table[i].name64);
840 insn = (struct sparc_opcode *) hash_find (op_hash, name);
841 if (insn == NULL)
842 {
843 as_bad (_("Internal error: can't find opcode `%s' for `%s'\n"),
844 name, native_op_table[i].name);
845 lose = 1;
846 }
847 else
848 {
849 retval = hash_insert (op_hash, native_op_table[i].name, (PTR) insn);
850 if (retval != NULL)
851 {
852 as_bad (_("Internal error: can't hash `%s': %s\n"),
853 sparc_opcodes[i].name, retval);
854 lose = 1;
855 }
856 }
857 }
858
859 if (lose)
860 as_fatal (_("Broken assembler. No assembly attempted."));
861
862 qsort (priv_reg_table, sizeof (priv_reg_table) / sizeof (priv_reg_table[0]),
863 sizeof (priv_reg_table[0]), cmp_reg_entry);
864
865 /* If -bump, record the architecture level at which we start issuing
866 warnings. The behaviour is different depending upon whether an
867 architecture was explicitly specified. If it wasn't, we issue warnings
868 for all upwards bumps. If it was, we don't start issuing warnings until
869 we need to bump beyond the requested architecture or when we bump between
870 conflicting architectures. */
871
872 if (warn_on_bump
873 && architecture_requested)
874 {
875 /* `max_architecture' records the requested architecture.
876 Issue warnings if we go above it. */
877 warn_after_architecture = max_architecture;
878
879 /* Find the highest architecture level that doesn't conflict with
880 the requested one. */
881 for (max_architecture = SPARC_OPCODE_ARCH_MAX;
882 max_architecture > warn_after_architecture;
883 --max_architecture)
884 if (! SPARC_OPCODE_CONFLICT_P (max_architecture,
885 warn_after_architecture))
886 break;
887 }
888}
889
890/* Called after all assembly has been done. */
891
892void
893sparc_md_end ()
894{
895 unsigned long mach = bfd_mach_sparc;
896
897 if (sparc_arch_size == 64)
898 switch (current_architecture)
899 {
900 case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v9a; break;
901 case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v9b; break;
902 default: mach = bfd_mach_sparc_v9; break;
903 }
904 else
905 switch (current_architecture)
906 {
907 case SPARC_OPCODE_ARCH_SPARCLET: mach = bfd_mach_sparc_sparclet; break;
908 case SPARC_OPCODE_ARCH_V9: mach = bfd_mach_sparc_v8plus; break;
909 case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v8plusa; break;
910 case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v8plusb; break;
911 /* The sparclite is treated like a normal sparc. Perhaps it shouldn't
912 be but for now it is (since that's the way it's always been
913 treated). */
914 default: break;
915 }
916 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, mach);
917}
918
919
920/* Return non-zero if VAL is in the range -(MAX+1) to MAX. */
921
922static INLINE int
923in_signed_range (val, max)
924 bfd_signed_vma val, max;
925{
926 if (max <= 0)
927 abort ();
928 /* Sign-extend the value from the architecture word size, so that
929 0xffffffff is always considered -1 on sparc32. */
930 if (sparc_arch_size == 32)
931 {
932 bfd_signed_vma sign = (bfd_signed_vma) 1 << 31;
933 val = ((val & U0xffffffff) ^ sign) - sign;
934 }
935 if (val > max)
936 return 0;
937 if (val < ~max)
938 return 0;
939 return 1;
940}
941
942/* Return non-zero if VAL is in the range 0 to MAX. */
943
944static INLINE int
945in_unsigned_range (val, max)
946 bfd_vma val, max;
947{
948 if (val > max)
949 return 0;
950 return 1;
951}
952
953/* Return non-zero if VAL is in the range -(MAX/2+1) to MAX.
954 (e.g. -15 to +31). */
955
956static INLINE int
957in_bitfield_range (val, max)
958 bfd_signed_vma val, max;
959{
960 if (max <= 0)
961 abort ();
962 if (val > max)
963 return 0;
964 if (val < ~(max >> 1))
965 return 0;
966 return 1;
967}
968
969static int
970sparc_ffs (mask)
971 unsigned int mask;
972{
973 int i;
974
975 if (mask == 0)
976 return -1;
977
978 for (i = 0; (mask & 1) == 0; ++i)
979 mask >>= 1;
980 return i;
981}
982
983/* Implement big shift right. */
984static bfd_vma
985BSR (val, amount)
986 bfd_vma val;
987 int amount;
988{
989 if (sizeof (bfd_vma) <= 4 && amount >= 32)
990 as_fatal (_("Support for 64-bit arithmetic not compiled in."));
991 return val >> amount;
992}
993
994
995/* For communication between sparc_ip and get_expression. */
996static char *expr_end;
997
998/* Values for `special_case'.
999 Instructions that require wierd handling because they're longer than
1000 4 bytes. */
1001#define SPECIAL_CASE_NONE 0
1002#define SPECIAL_CASE_SET 1
1003#define SPECIAL_CASE_SETSW 2
1004#define SPECIAL_CASE_SETX 3
1005/* FIXME: sparc-opc.c doesn't have necessary "S" trigger to enable this. */
1006#define SPECIAL_CASE_FDIV 4
1007
1008/* Bit masks of various insns. */
1009#define NOP_INSN 0x01000000
1010#define OR_INSN 0x80100000
1011#define XOR_INSN 0x80180000
1012#define FMOVS_INSN 0x81A00020
1013#define SETHI_INSN 0x01000000
1014#define SLLX_INSN 0x81281000
1015#define SRA_INSN 0x81380000
1016
1017/* The last instruction to be assembled. */
1018static const struct sparc_opcode *last_insn;
1019/* The assembled opcode of `last_insn'. */
1020static unsigned long last_opcode;
1021
1022
1023/* Handle the set and setuw synthetic instructions. */
1024
1025static void
1026synthetize_setuw (insn)
1027 const struct sparc_opcode *insn;
1028{
1029 int need_hi22_p = 0;
1030 int rd = (the_insn.opcode & RD (~0)) >> 25;
1031
1032 if (the_insn.exp.X_op == O_constant)
1033 {
1034 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1035 {
1036 if (sizeof (offsetT) > 4
1037 && (the_insn.exp.X_add_number < 0
1038 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1039 as_warn (_("set: number not in 0..4294967295 range"));
1040 }
1041 else
1042 {
1043 if (sizeof (offsetT) > 4
1044 && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1045 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1046 as_warn (_("set: number not in -2147483648..4294967295 range"));
1047 the_insn.exp.X_add_number = (int) the_insn.exp.X_add_number;
1048 }
1049 }
1050
1051 /* See if operand is absolute and small; skip sethi if so. */
1052 if (the_insn.exp.X_op != O_constant
1053 || the_insn.exp.X_add_number >= (1 << 12)
1054 || the_insn.exp.X_add_number < -(1 << 12))
1055 {
1056 the_insn.opcode = (SETHI_INSN | RD (rd)
1057 | ((the_insn.exp.X_add_number >> 10)
1058 & (the_insn.exp.X_op == O_constant
1059 ? 0x3fffff : 0)));
1060 the_insn.reloc = (the_insn.exp.X_op != O_constant
1061 ? BFD_RELOC_HI22 : BFD_RELOC_NONE);
1062 output_insn (insn, &the_insn);
1063 need_hi22_p = 1;
1064 }
1065
1066 /* See if operand has no low-order bits; skip OR if so. */
1067 if (the_insn.exp.X_op != O_constant
1068 || (need_hi22_p && (the_insn.exp.X_add_number & 0x3FF) != 0)
1069 || ! need_hi22_p)
1070 {
1071 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (rd) : 0)
1072 | RD (rd) | IMMED
1073 | (the_insn.exp.X_add_number
1074 & (the_insn.exp.X_op != O_constant
1075 ? 0 : need_hi22_p ? 0x3ff : 0x1fff)));
1076 the_insn.reloc = (the_insn.exp.X_op != O_constant
1077 ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
1078 output_insn (insn, &the_insn);
1079 }
1080}
1081
1082/* Handle the setsw synthetic instruction. */
1083
1084static void
1085synthetize_setsw (insn)
1086 const struct sparc_opcode *insn;
1087{
1088 int low32, rd, opc;
1089
1090 rd = (the_insn.opcode & RD (~0)) >> 25;
1091
1092 if (the_insn.exp.X_op != O_constant)
1093 {
1094 synthetize_setuw (insn);
1095
1096 /* Need to sign extend it. */
1097 the_insn.opcode = (SRA_INSN | RS1 (rd) | RD (rd));
1098 the_insn.reloc = BFD_RELOC_NONE;
1099 output_insn (insn, &the_insn);
1100 return;
1101 }
1102
1103 if (sizeof (offsetT) > 4
1104 && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1105 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1106 as_warn (_("setsw: number not in -2147483648..4294967295 range"));
1107
1108 low32 = the_insn.exp.X_add_number;
1109
1110 if (low32 >= 0)
1111 {
1112 synthetize_setuw (insn);
1113 return;
1114 }
1115
1116 opc = OR_INSN;
1117
1118 the_insn.reloc = BFD_RELOC_NONE;
1119 /* See if operand is absolute and small; skip sethi if so. */
1120 if (low32 < -(1 << 12))
1121 {
1122 the_insn.opcode = (SETHI_INSN | RD (rd)
1123 | (((~the_insn.exp.X_add_number) >> 10) & 0x3fffff));
1124 output_insn (insn, &the_insn);
1125 low32 = 0x1c00 | (low32 & 0x3ff);
1126 opc = RS1 (rd) | XOR_INSN;
1127 }
1128
1129 the_insn.opcode = (opc | RD (rd) | IMMED
1130 | (low32 & 0x1fff));
1131 output_insn (insn, &the_insn);
1132}
1133
1134/* Handle the setsw synthetic instruction. */
1135
1136static void
1137synthetize_setx (insn)
1138 const struct sparc_opcode *insn;
1139{
1140 int upper32, lower32;
1141 int tmpreg = (the_insn.opcode & RS1 (~0)) >> 14;
1142 int dstreg = (the_insn.opcode & RD (~0)) >> 25;
1143 int upper_dstreg;
1144 int need_hh22_p = 0, need_hm10_p = 0, need_hi22_p = 0, need_lo10_p = 0;
1145 int need_xor10_p = 0;
1146
1147#define SIGNEXT32(x) ((((x) & U0xffffffff) ^ U0x80000000) - U0x80000000)
1148 lower32 = SIGNEXT32 (the_insn.exp.X_add_number);
1149 upper32 = SIGNEXT32 (BSR (the_insn.exp.X_add_number, 32));
1150#undef SIGNEXT32
1151
1152 upper_dstreg = tmpreg;
1153 /* The tmp reg should not be the dst reg. */
1154 if (tmpreg == dstreg)
1155 as_warn (_("setx: temporary register same as destination register"));
1156
1157 /* ??? Obviously there are other optimizations we can do
1158 (e.g. sethi+shift for 0x1f0000000) and perhaps we shouldn't be
1159 doing some of these. Later. If you do change things, try to
1160 change all of this to be table driven as well. */
1161 /* What to output depends on the number if it's constant.
1162 Compute that first, then output what we've decided upon. */
1163 if (the_insn.exp.X_op != O_constant)
1164 {
1165 if (sparc_arch_size == 32)
1166 {
1167 /* When arch size is 32, we want setx to be equivalent
1168 to setuw for anything but constants. */
1169 the_insn.exp.X_add_number &= 0xffffffff;
1170 synthetize_setuw (insn);
1171 return;
1172 }
1173 need_hh22_p = need_hm10_p = need_hi22_p = need_lo10_p = 1;
1174 lower32 = 0;
1175 upper32 = 0;
1176 }
1177 else
1178 {
1179 /* Reset X_add_number, we've extracted it as upper32/lower32.
1180 Otherwise fixup_segment will complain about not being able to
1181 write an 8 byte number in a 4 byte field. */
1182 the_insn.exp.X_add_number = 0;
1183
1184 /* Only need hh22 if `or' insn can't handle constant. */
1185 if (upper32 < -(1 << 12) || upper32 >= (1 << 12))
1186 need_hh22_p = 1;
1187
1188 /* Does bottom part (after sethi) have bits? */
1189 if ((need_hh22_p && (upper32 & 0x3ff) != 0)
1190 /* No hh22, but does upper32 still have bits we can't set
1191 from lower32? */
1192 || (! need_hh22_p && upper32 != 0 && upper32 != -1))
1193 need_hm10_p = 1;
1194
1195 /* If the lower half is all zero, we build the upper half directly
1196 into the dst reg. */
1197 if (lower32 != 0
1198 /* Need lower half if number is zero or 0xffffffff00000000. */
1199 || (! need_hh22_p && ! need_hm10_p))
1200 {
1201 /* No need for sethi if `or' insn can handle constant. */
1202 if (lower32 < -(1 << 12) || lower32 >= (1 << 12)
1203 /* Note that we can't use a negative constant in the `or'
1204 insn unless the upper 32 bits are all ones. */
1205 || (lower32 < 0 && upper32 != -1)
1206 || (lower32 >= 0 && upper32 == -1))
1207 need_hi22_p = 1;
1208
1209 if (need_hi22_p && upper32 == -1)
1210 need_xor10_p = 1;
1211
1212 /* Does bottom part (after sethi) have bits? */
1213 else if ((need_hi22_p && (lower32 & 0x3ff) != 0)
1214 /* No sethi. */
1215 || (! need_hi22_p && (lower32 & 0x1fff) != 0)
1216 /* Need `or' if we didn't set anything else. */
1217 || (! need_hi22_p && ! need_hh22_p && ! need_hm10_p))
1218 need_lo10_p = 1;
1219 }
1220 else
1221 /* Output directly to dst reg if lower 32 bits are all zero. */
1222 upper_dstreg = dstreg;
1223 }
1224
1225 if (!upper_dstreg && dstreg)
1226 as_warn (_("setx: illegal temporary register g0"));
1227
1228 if (need_hh22_p)
1229 {
1230 the_insn.opcode = (SETHI_INSN | RD (upper_dstreg)
1231 | ((upper32 >> 10) & 0x3fffff));
1232 the_insn.reloc = (the_insn.exp.X_op != O_constant
1233 ? BFD_RELOC_SPARC_HH22 : BFD_RELOC_NONE);
1234 output_insn (insn, &the_insn);
1235 }
1236
1237 if (need_hi22_p)
1238 {
1239 the_insn.opcode = (SETHI_INSN | RD (dstreg)
1240 | (((need_xor10_p ? ~lower32 : lower32)
1241 >> 10) & 0x3fffff));
1242 the_insn.reloc = (the_insn.exp.X_op != O_constant
1243 ? BFD_RELOC_SPARC_LM22 : BFD_RELOC_NONE);
1244 output_insn (insn, &the_insn);
1245 }
1246
1247 if (need_hm10_p)
1248 {
1249 the_insn.opcode = (OR_INSN
1250 | (need_hh22_p ? RS1 (upper_dstreg) : 0)
1251 | RD (upper_dstreg)
1252 | IMMED
1253 | (upper32 & (need_hh22_p ? 0x3ff : 0x1fff)));
1254 the_insn.reloc = (the_insn.exp.X_op != O_constant
1255 ? BFD_RELOC_SPARC_HM10 : BFD_RELOC_NONE);
1256 output_insn (insn, &the_insn);
1257 }
1258
1259 if (need_lo10_p)
1260 {
1261 /* FIXME: One nice optimization to do here is to OR the low part
1262 with the highpart if hi22 isn't needed and the low part is
1263 positive. */
1264 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (dstreg) : 0)
1265 | RD (dstreg)
1266 | IMMED
1267 | (lower32 & (need_hi22_p ? 0x3ff : 0x1fff)));
1268 the_insn.reloc = (the_insn.exp.X_op != O_constant
1269 ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
1270 output_insn (insn, &the_insn);
1271 }
1272
1273 /* If we needed to build the upper part, shift it into place. */
1274 if (need_hh22_p || need_hm10_p)
1275 {
1276 the_insn.opcode = (SLLX_INSN | RS1 (upper_dstreg) | RD (upper_dstreg)
1277 | IMMED | 32);
1278 the_insn.reloc = BFD_RELOC_NONE;
1279 output_insn (insn, &the_insn);
1280 }
1281
1282 /* To get -1 in upper32, we do sethi %hi(~x), r; xor r, -0x400 | x, r. */
1283 if (need_xor10_p)
1284 {
1285 the_insn.opcode = (XOR_INSN | RS1 (dstreg) | RD (dstreg) | IMMED
1286 | 0x1c00 | (lower32 & 0x3ff));
1287 the_insn.reloc = BFD_RELOC_NONE;
1288 output_insn (insn, &the_insn);
1289 }
1290
1291 /* If we needed to build both upper and lower parts, OR them together. */
1292 else if ((need_hh22_p || need_hm10_p) && (need_hi22_p || need_lo10_p))
1293 {
1294 the_insn.opcode = (OR_INSN | RS1 (dstreg) | RS2 (upper_dstreg)
1295 | RD (dstreg));
1296 the_insn.reloc = BFD_RELOC_NONE;
1297 output_insn (insn, &the_insn);
1298 }
1299}
1300
1301
1302/* Main entry point to assemble one instruction. */
1303
1304void
1305md_assemble (str)
1306 char *str;
1307{
1308 const struct sparc_opcode *insn;
1309 int special_case;
1310
1311 know (str);
1312 special_case = sparc_ip (str, &insn);
1313
1314 /* We warn about attempts to put a floating point branch in a delay slot,
1315 unless the delay slot has been annulled. */
1316 if (insn != NULL
1317 && last_insn != NULL
1318 && (insn->flags & F_FBR) != 0
1319 && (last_insn->flags & F_DELAYED) != 0
1320 /* ??? This test isn't completely accurate. We assume anything with
1321 F_{UNBR,CONDBR,FBR} set is annullable. */
1322 && ((last_insn->flags & (F_UNBR | F_CONDBR | F_FBR)) == 0
1323 || (last_opcode & ANNUL) == 0))
1324 as_warn (_("FP branch in delay slot"));
1325
1326 /* SPARC before v9 requires a nop instruction between a floating
1327 point instruction and a floating point branch. We insert one
1328 automatically, with a warning. */
1329 if (max_architecture < SPARC_OPCODE_ARCH_V9
1330 && insn != NULL
1331 && last_insn != NULL
1332 && (insn->flags & F_FBR) != 0
1333 && (last_insn->flags & F_FLOAT) != 0)
1334 {
1335 struct sparc_it nop_insn;
1336
1337 nop_insn.opcode = NOP_INSN;
1338 nop_insn.reloc = BFD_RELOC_NONE;
1339 output_insn (insn, &nop_insn);
1340 as_warn (_("FP branch preceded by FP instruction; NOP inserted"));
1341 }
1342
1343 switch (special_case)
1344 {
1345 case SPECIAL_CASE_NONE:
1346 /* Normal insn. */
1347 output_insn (insn, &the_insn);
1348 break;
1349
1350 case SPECIAL_CASE_SETSW:
1351 synthetize_setsw (insn);
1352 break;
1353
1354 case SPECIAL_CASE_SET:
1355 synthetize_setuw (insn);
1356 break;
1357
1358 case SPECIAL_CASE_SETX:
1359 synthetize_setx (insn);
1360 break;
1361
1362 case SPECIAL_CASE_FDIV:
1363 {
1364 int rd = (the_insn.opcode >> 25) & 0x1f;
1365
1366 output_insn (insn, &the_insn);
1367
1368 /* According to information leaked from Sun, the "fdiv" instructions
1369 on early SPARC machines would produce incorrect results sometimes.
1370 The workaround is to add an fmovs of the destination register to
1371 itself just after the instruction. This was true on machines
1372 with Weitek 1165 float chips, such as the Sun-4/260 and /280. */
1373 assert (the_insn.reloc == BFD_RELOC_NONE);
1374 the_insn.opcode = FMOVS_INSN | rd | RD (rd);
1375 output_insn (insn, &the_insn);
1376 return;
1377 }
1378
1379 default:
1380 as_fatal (_("failed special case insn sanity check"));
1381 }
1382}
1383
1384/* Subroutine of md_assemble to do the actual parsing. */
1385
1386static int
1387sparc_ip (str, pinsn)
1388 char *str;
1389 const struct sparc_opcode **pinsn;
1390{
1391 char *error_message = "";
1392 char *s;
1393 const char *args;
1394 char c;
1395 const struct sparc_opcode *insn;
1396 char *argsStart;
1397 unsigned long opcode;
1398 unsigned int mask = 0;
1399 int match = 0;
1400 int comma = 0;
1401 int v9_arg_p;
1402 int special_case = SPECIAL_CASE_NONE;
1403
1404 s = str;
1405 if (ISLOWER (*s))
1406 {
1407 do
1408 ++s;
1409 while (ISLOWER (*s) || ISDIGIT (*s));
1410 }
1411
1412 switch (*s)
1413 {
1414 case '\0':
1415 break;
1416
1417 case ',':
1418 comma = 1;
1419 /* Fall through. */
1420
1421 case ' ':
1422 *s++ = '\0';
1423 break;
1424
1425 default:
1426 as_fatal (_("Unknown opcode: `%s'"), str);
1427 }
1428 insn = (struct sparc_opcode *) hash_find (op_hash, str);
1429 *pinsn = insn;
1430 if (insn == NULL)
1431 {
1432 as_bad (_("Unknown opcode: `%s'"), str);
1433 return special_case;
1434 }
1435 if (comma)
1436 {
1437 *--s = ',';
1438 }
1439
1440 argsStart = s;
1441 for (;;)
1442 {
1443 opcode = insn->match;
1444 memset (&the_insn, '\0', sizeof (the_insn));
1445 the_insn.reloc = BFD_RELOC_NONE;
1446 v9_arg_p = 0;
1447
1448 /* Build the opcode, checking as we go to make sure that the
1449 operands match. */
1450 for (args = insn->args;; ++args)
1451 {
1452 switch (*args)
1453 {
1454 case 'K':
1455 {
1456 int kmask = 0;
1457
1458 /* Parse a series of masks. */
1459 if (*s == '#')
1460 {
1461 while (*s == '#')
1462 {
1463 int mask;
1464
1465 if (! parse_keyword_arg (sparc_encode_membar, &s,
1466 &mask))
1467 {
1468 error_message = _(": invalid membar mask name");
1469 goto error;
1470 }
1471 kmask |= mask;
1472 while (*s == ' ')
1473 ++s;
1474 if (*s == '|' || *s == '+')
1475 ++s;
1476 while (*s == ' ')
1477 ++s;
1478 }
1479 }
1480 else
1481 {
1482 if (! parse_const_expr_arg (&s, &kmask))
1483 {
1484 error_message = _(": invalid membar mask expression");
1485 goto error;
1486 }
1487 if (kmask < 0 || kmask > 127)
1488 {
1489 error_message = _(": invalid membar mask number");
1490 goto error;
1491 }
1492 }
1493
1494 opcode |= MEMBAR (kmask);
1495 continue;
1496 }
1497
1498 case '3':
1499 {
1500 int smask = 0;
1501
1502 if (! parse_const_expr_arg (&s, &smask))
1503 {
1504 error_message = _(": invalid siam mode expression");
1505 goto error;
1506 }
1507 if (smask < 0 || smask > 7)
1508 {
1509 error_message = _(": invalid siam mode number");
1510 goto error;
1511 }
1512 opcode |= smask;
1513 continue;
1514 }
1515
1516 case '*':
1517 {
1518 int fcn = 0;
1519
1520 /* Parse a prefetch function. */
1521 if (*s == '#')
1522 {
1523 if (! parse_keyword_arg (sparc_encode_prefetch, &s, &fcn))
1524 {
1525 error_message = _(": invalid prefetch function name");
1526 goto error;
1527 }
1528 }
1529 else
1530 {
1531 if (! parse_const_expr_arg (&s, &fcn))
1532 {
1533 error_message = _(": invalid prefetch function expression");
1534 goto error;
1535 }
1536 if (fcn < 0 || fcn > 31)
1537 {
1538 error_message = _(": invalid prefetch function number");
1539 goto error;
1540 }
1541 }
1542 opcode |= RD (fcn);
1543 continue;
1544 }
1545
1546 case '!':
1547 case '?':
1548 /* Parse a sparc64 privileged register. */
1549 if (*s == '%')
1550 {
1551 struct priv_reg_entry *p = priv_reg_table;
1552 unsigned int len = 9999999; /* Init to make gcc happy. */
1553
1554 s += 1;
1555 while (p->name[0] > s[0])
1556 p++;
1557 while (p->name[0] == s[0])
1558 {
1559 len = strlen (p->name);
1560 if (strncmp (p->name, s, len) == 0)
1561 break;
1562 p++;
1563 }
1564 if (p->name[0] != s[0])
1565 {
1566 error_message = _(": unrecognizable privileged register");
1567 goto error;
1568 }
1569 if (*args == '?')
1570 opcode |= (p->regnum << 14);
1571 else
1572 opcode |= (p->regnum << 25);
1573 s += len;
1574 continue;
1575 }
1576 else
1577 {
1578 error_message = _(": unrecognizable privileged register");
1579 goto error;
1580 }
1581
1582 case '_':
1583 case '/':
1584 /* Parse a v9a/v9b ancillary state register. */
1585 if (*s == '%')
1586 {
1587 struct priv_reg_entry *p = v9a_asr_table;
1588 unsigned int len = 9999999; /* Init to make gcc happy. */
1589
1590 s += 1;
1591 while (p->name[0] > s[0])
1592 p++;
1593 while (p->name[0] == s[0])
1594 {
1595 len = strlen (p->name);
1596 if (strncmp (p->name, s, len) == 0)
1597 break;
1598 p++;
1599 }
1600 if (p->name[0] != s[0])
1601 {
1602 error_message = _(": unrecognizable v9a or v9b ancillary state register");
1603 goto error;
1604 }
1605 if (*args == '/' && (p->regnum == 20 || p->regnum == 21))
1606 {
1607 error_message = _(": rd on write only ancillary state register");
1608 goto error;
1609 }
1610 if (p->regnum >= 24
1611 && (insn->architecture
1612 & SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9A)))
1613 {
1614 /* %sys_tick and %sys_tick_cmpr are v9bnotv9a */
1615 error_message = _(": unrecognizable v9a ancillary state register");
1616 goto error;
1617 }
1618 if (*args == '/')
1619 opcode |= (p->regnum << 14);
1620 else
1621 opcode |= (p->regnum << 25);
1622 s += len;
1623 continue;
1624 }
1625 else
1626 {
1627 error_message = _(": unrecognizable v9a or v9b ancillary state register");
1628 goto error;
1629 }
1630
1631 case 'M':
1632 case 'm':
1633 if (strncmp (s, "%asr", 4) == 0)
1634 {
1635 s += 4;
1636
1637 if (ISDIGIT (*s))
1638 {
1639 long num = 0;
1640
1641 while (ISDIGIT (*s))
1642 {
1643 num = num * 10 + *s - '0';
1644 ++s;
1645 }
1646
1647 if (current_architecture >= SPARC_OPCODE_ARCH_V9)
1648 {
1649 if (num < 16 || 31 < num)
1650 {
1651 error_message = _(": asr number must be between 16 and 31");
1652 goto error;
1653 }
1654 }
1655 else
1656 {
1657 if (num < 0 || 31 < num)
1658 {
1659 error_message = _(": asr number must be between 0 and 31");
1660 goto error;
1661 }
1662 }
1663
1664 opcode |= (*args == 'M' ? RS1 (num) : RD (num));
1665 continue;
1666 }
1667 else
1668 {
1669 error_message = _(": expecting %asrN");
1670 goto error;
1671 }
1672 } /* if %asr */
1673 break;
1674
1675 case 'I':
1676 the_insn.reloc = BFD_RELOC_SPARC_11;
1677 goto immediate;
1678
1679 case 'j':
1680 the_insn.reloc = BFD_RELOC_SPARC_10;
1681 goto immediate;
1682
1683 case 'X':
1684 /* V8 systems don't understand BFD_RELOC_SPARC_5. */
1685 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1686 the_insn.reloc = BFD_RELOC_SPARC_5;
1687 else
1688 the_insn.reloc = BFD_RELOC_SPARC13;
1689 /* These fields are unsigned, but for upward compatibility,
1690 allow negative values as well. */
1691 goto immediate;
1692
1693 case 'Y':
1694 /* V8 systems don't understand BFD_RELOC_SPARC_6. */
1695 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1696 the_insn.reloc = BFD_RELOC_SPARC_6;
1697 else
1698 the_insn.reloc = BFD_RELOC_SPARC13;
1699 /* These fields are unsigned, but for upward compatibility,
1700 allow negative values as well. */
1701 goto immediate;
1702
1703 case 'k':
1704 the_insn.reloc = /* RELOC_WDISP2_14 */ BFD_RELOC_SPARC_WDISP16;
1705 the_insn.pcrel = 1;
1706 goto immediate;
1707
1708 case 'G':
1709 the_insn.reloc = BFD_RELOC_SPARC_WDISP19;
1710 the_insn.pcrel = 1;
1711 goto immediate;
1712
1713 case 'N':
1714 if (*s == 'p' && s[1] == 'n')
1715 {
1716 s += 2;
1717 continue;
1718 }
1719 break;
1720
1721 case 'T':
1722 if (*s == 'p' && s[1] == 't')
1723 {
1724 s += 2;
1725 continue;
1726 }
1727 break;
1728
1729 case 'z':
1730 if (*s == ' ')
1731 {
1732 ++s;
1733 }
1734 if (strncmp (s, "%icc", 4) == 0)
1735 {
1736 s += 4;
1737 continue;
1738 }
1739 break;
1740
1741 case 'Z':
1742 if (*s == ' ')
1743 {
1744 ++s;
1745 }
1746 if (strncmp (s, "%xcc", 4) == 0)
1747 {
1748 s += 4;
1749 continue;
1750 }
1751 break;
1752
1753 case '6':
1754 if (*s == ' ')
1755 {
1756 ++s;
1757 }
1758 if (strncmp (s, "%fcc0", 5) == 0)
1759 {
1760 s += 5;
1761 continue;
1762 }
1763 break;
1764
1765 case '7':
1766 if (*s == ' ')
1767 {
1768 ++s;
1769 }
1770 if (strncmp (s, "%fcc1", 5) == 0)
1771 {
1772 s += 5;
1773 continue;
1774 }
1775 break;
1776
1777 case '8':
1778 if (*s == ' ')
1779 {
1780 ++s;
1781 }
1782 if (strncmp (s, "%fcc2", 5) == 0)
1783 {
1784 s += 5;
1785 continue;
1786 }
1787 break;
1788
1789 case '9':
1790 if (*s == ' ')
1791 {
1792 ++s;
1793 }
1794 if (strncmp (s, "%fcc3", 5) == 0)
1795 {
1796 s += 5;
1797 continue;
1798 }
1799 break;
1800
1801 case 'P':
1802 if (strncmp (s, "%pc", 3) == 0)
1803 {
1804 s += 3;
1805 continue;
1806 }
1807 break;
1808
1809 case 'W':
1810 if (strncmp (s, "%tick", 5) == 0)
1811 {
1812 s += 5;
1813 continue;
1814 }
1815 break;
1816
1817 case '\0': /* End of args. */
1818 if (s[0] == ',' && s[1] == '%')
1819 {
1820 static const struct tls_ops {
1821 /* The name as it appears in assembler. */
1822 char *name;
1823 /* strlen (name), precomputed for speed */
1824 int len;
1825 /* The reloc this pseudo-op translates to. */
1826 int reloc;
1827 /* 1 if call. */
1828 int call;
1829 } tls_ops[] = {
1830 { "tgd_add", 7, BFD_RELOC_SPARC_TLS_GD_ADD, 0 },
1831 { "tgd_call", 8, BFD_RELOC_SPARC_TLS_GD_CALL, 1 },
1832 { "tldm_add", 8, BFD_RELOC_SPARC_TLS_LDM_ADD, 0 },
1833 { "tldm_call", 9, BFD_RELOC_SPARC_TLS_LDM_CALL, 1 },
1834 { "tldo_add", 8, BFD_RELOC_SPARC_TLS_LDO_ADD, 0 },
1835 { "tie_ldx", 7, BFD_RELOC_SPARC_TLS_IE_LDX, 0 },
1836 { "tie_ld", 6, BFD_RELOC_SPARC_TLS_IE_LD, 0 },
1837 { "tie_add", 7, BFD_RELOC_SPARC_TLS_IE_ADD, 0 }
1838 };
1839 const struct tls_ops *o;
1840 char *s1;
1841 int npar = 0;
1842
1843 for (o = tls_ops; o->name; o++)
1844 if (strncmp (s + 2, o->name, o->len) == 0)
1845 break;
1846 if (o->name == NULL)
1847 break;
1848
1849 if (s[o->len + 2] != '(')
1850 {
1851 as_bad (_("Illegal operands: %%%s requires arguments in ()"), o->name);
1852 return special_case;
1853 }
1854
1855 if (! o->call && the_insn.reloc != BFD_RELOC_NONE)
1856 {
1857 as_bad (_("Illegal operands: %%%s cannot be used together with other relocs in the insn ()"),
1858 o->name);
1859 return special_case;
1860 }
1861
1862 if (o->call
1863 && (the_insn.reloc != BFD_RELOC_32_PCREL_S2
1864 || the_insn.exp.X_add_number != 0
1865 || the_insn.exp.X_add_symbol
1866 != symbol_find_or_make ("__tls_get_addr")))
1867 {
1868 as_bad (_("Illegal operands: %%%s can be only used with call __tls_get_addr"),
1869 o->name);
1870 return special_case;
1871 }
1872
1873 the_insn.reloc = o->reloc;
1874 memset (&the_insn.exp, 0, sizeof (the_insn.exp));
1875 s += o->len + 3;
1876
1877 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
1878 if (*s1 == '(')
1879 npar++;
1880 else if (*s1 == ')')
1881 {
1882 if (!npar)
1883 break;
1884 npar--;
1885 }
1886
1887 if (*s1 != ')')
1888 {
1889 as_bad (_("Illegal operands: %%%s requires arguments in ()"), o->name);
1890 return special_case;
1891 }
1892
1893 *s1 = '\0';
1894 (void) get_expression (s);
1895 *s1 = ')';
1896 s = s1 + 1;
1897 }
1898 if (*s == '\0')
1899 match = 1;
1900 break;
1901
1902 case '+':
1903 if (*s == '+')
1904 {
1905 ++s;
1906 continue;
1907 }
1908 if (*s == '-')
1909 {
1910 continue;
1911 }
1912 break;
1913
1914 case '[': /* These must match exactly. */
1915 case ']':
1916 case ',':
1917 case ' ':
1918 if (*s++ == *args)
1919 continue;
1920 break;
1921
1922 case '#': /* Must be at least one digit. */
1923 if (ISDIGIT (*s++))
1924 {
1925 while (ISDIGIT (*s))
1926 {
1927 ++s;
1928 }
1929 continue;
1930 }
1931 break;
1932
1933 case 'C': /* Coprocessor state register. */
1934 if (strncmp (s, "%csr", 4) == 0)
1935 {
1936 s += 4;
1937 continue;
1938 }
1939 break;
1940
1941 case 'b': /* Next operand is a coprocessor register. */
1942 case 'c':
1943 case 'D':
1944 if (*s++ == '%' && *s++ == 'c' && ISDIGIT (*s))
1945 {
1946 mask = *s++;
1947 if (ISDIGIT (*s))
1948 {
1949 mask = 10 * (mask - '0') + (*s++ - '0');
1950 if (mask >= 32)
1951 {
1952 break;
1953 }
1954 }
1955 else
1956 {
1957 mask -= '0';
1958 }
1959 switch (*args)
1960 {
1961
1962 case 'b':
1963 opcode |= mask << 14;
1964 continue;
1965
1966 case 'c':
1967 opcode |= mask;
1968 continue;
1969
1970 case 'D':
1971 opcode |= mask << 25;
1972 continue;
1973 }
1974 }
1975 break;
1976
1977 case 'r': /* next operand must be a register */
1978 case 'O':
1979 case '1':
1980 case '2':
1981 case 'd':
1982 if (*s++ == '%')
1983 {
1984 switch (c = *s++)
1985 {
1986
1987 case 'f': /* frame pointer */
1988 if (*s++ == 'p')
1989 {
1990 mask = 0x1e;
1991 break;
1992 }
1993 goto error;
1994
1995 case 'g': /* global register */
1996 c = *s++;
1997 if (isoctal (c))
1998 {
1999 mask = c - '0';
2000 break;
2001 }
2002 goto error;
2003
2004 case 'i': /* in register */
2005 c = *s++;
2006 if (isoctal (c))
2007 {
2008 mask = c - '0' + 24;
2009 break;
2010 }
2011 goto error;
2012
2013 case 'l': /* local register */
2014 c = *s++;
2015 if (isoctal (c))
2016 {
2017 mask = (c - '0' + 16);
2018 break;
2019 }
2020 goto error;
2021
2022 case 'o': /* out register */
2023 c = *s++;
2024 if (isoctal (c))
2025 {
2026 mask = (c - '0' + 8);
2027 break;
2028 }
2029 goto error;
2030
2031 case 's': /* stack pointer */
2032 if (*s++ == 'p')
2033 {
2034 mask = 0xe;
2035 break;
2036 }
2037 goto error;
2038
2039 case 'r': /* any register */
2040 if (!ISDIGIT ((c = *s++)))
2041 {
2042 goto error;
2043 }
2044 /* FALLTHROUGH */
2045 case '0':
2046 case '1':
2047 case '2':
2048 case '3':
2049 case '4':
2050 case '5':
2051 case '6':
2052 case '7':
2053 case '8':
2054 case '9':
2055 if (ISDIGIT (*s))
2056 {
2057 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32)
2058 {
2059 goto error;
2060 }
2061 }
2062 else
2063 {
2064 c -= '0';
2065 }
2066 mask = c;
2067 break;
2068
2069 default:
2070 goto error;
2071 }
2072
2073 if ((mask & ~1) == 2 && sparc_arch_size == 64
2074 && no_undeclared_regs && ! globals[mask])
2075 as_bad (_("detected global register use not covered by .register pseudo-op"));
2076
2077 /* Got the register, now figure out where
2078 it goes in the opcode. */
2079 switch (*args)
2080 {
2081 case '1':
2082 opcode |= mask << 14;
2083 continue;
2084
2085 case '2':
2086 opcode |= mask;
2087 continue;
2088
2089 case 'd':
2090 opcode |= mask << 25;
2091 continue;
2092
2093 case 'r':
2094 opcode |= (mask << 25) | (mask << 14);
2095 continue;
2096
2097 case 'O':
2098 opcode |= (mask << 25) | (mask << 0);
2099 continue;
2100 }
2101 }
2102 break;
2103
2104 case 'e': /* next operand is a floating point register */
2105 case 'v':
2106 case 'V':
2107
2108 case 'f':
2109 case 'B':
2110 case 'R':
2111
2112 case 'g':
2113 case 'H':
2114 case 'J':
2115 {
2116 char format;
2117
2118 if (*s++ == '%'
2119 && ((format = *s) == 'f')
2120 && ISDIGIT (*++s))
2121 {
2122 for (mask = 0; ISDIGIT (*s); ++s)
2123 {
2124 mask = 10 * mask + (*s - '0');
2125 } /* read the number */
2126
2127 if ((*args == 'v'
2128 || *args == 'B'
2129 || *args == 'H')
2130 && (mask & 1))
2131 {
2132 break;
2133 } /* register must be even numbered */
2134
2135 if ((*args == 'V'
2136 || *args == 'R'
2137 || *args == 'J')
2138 && (mask & 3))
2139 {
2140 break;
2141 } /* register must be multiple of 4 */
2142
2143 if (mask >= 64)
2144 {
2145 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2146 error_message = _(": There are only 64 f registers; [0-63]");
2147 else
2148 error_message = _(": There are only 32 f registers; [0-31]");
2149 goto error;
2150 } /* on error */
2151 else if (mask >= 32)
2152 {
2153 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2154 {
2155 v9_arg_p = 1;
2156 mask -= 31; /* wrap high bit */
2157 }
2158 else
2159 {
2160 error_message = _(": There are only 32 f registers; [0-31]");
2161 goto error;
2162 }
2163 }
2164 }
2165 else
2166 {
2167 break;
2168 } /* if not an 'f' register. */
2169
2170 switch (*args)
2171 {
2172 case 'v':
2173 case 'V':
2174 case 'e':
2175 opcode |= RS1 (mask);
2176 continue;
2177
2178 case 'f':
2179 case 'B':
2180 case 'R':
2181 opcode |= RS2 (mask);
2182 continue;
2183
2184 case 'g':
2185 case 'H':
2186 case 'J':
2187 opcode |= RD (mask);
2188 continue;
2189 } /* Pack it in. */
2190
2191 know (0);
2192 break;
2193 } /* float arg */
2194
2195 case 'F':
2196 if (strncmp (s, "%fsr", 4) == 0)
2197 {
2198 s += 4;
2199 continue;
2200 }
2201 break;
2202
2203 case '0': /* 64 bit immediate (set, setsw, setx insn) */
2204 the_insn.reloc = BFD_RELOC_NONE; /* reloc handled elsewhere */
2205 goto immediate;
2206
2207 case 'l': /* 22 bit PC relative immediate */
2208 the_insn.reloc = BFD_RELOC_SPARC_WDISP22;
2209 the_insn.pcrel = 1;
2210 goto immediate;
2211
2212 case 'L': /* 30 bit immediate */
2213 the_insn.reloc = BFD_RELOC_32_PCREL_S2;
2214 the_insn.pcrel = 1;
2215 goto immediate;
2216
2217 case 'h':
2218 case 'n': /* 22 bit immediate */
2219 the_insn.reloc = BFD_RELOC_SPARC22;
2220 goto immediate;
2221
2222 case 'i': /* 13 bit immediate */
2223 the_insn.reloc = BFD_RELOC_SPARC13;
2224
2225 /* fallthrough */
2226
2227 immediate:
2228 if (*s == ' ')
2229 s++;
2230
2231 {
2232 char *s1;
2233 char *op_arg = NULL;
2234 expressionS op_exp;
2235 bfd_reloc_code_real_type old_reloc = the_insn.reloc;
2236
2237 /* Check for %hi, etc. */
2238 if (*s == '%')
2239 {
2240 static const struct ops {
2241 /* The name as it appears in assembler. */
2242 char *name;
2243 /* strlen (name), precomputed for speed */
2244 int len;
2245 /* The reloc this pseudo-op translates to. */
2246 int reloc;
2247 /* Non-zero if for v9 only. */
2248 int v9_p;
2249 /* Non-zero if can be used in pc-relative contexts. */
2250 int pcrel_p;/*FIXME:wip*/
2251 } ops[] = {
2252 /* hix/lox must appear before hi/lo so %hix won't be
2253 mistaken for %hi. */
2254 { "hix", 3, BFD_RELOC_SPARC_HIX22, 1, 0 },
2255 { "lox", 3, BFD_RELOC_SPARC_LOX10, 1, 0 },
2256 { "hi", 2, BFD_RELOC_HI22, 0, 1 },
2257 { "lo", 2, BFD_RELOC_LO10, 0, 1 },
2258 { "hh", 2, BFD_RELOC_SPARC_HH22, 1, 1 },
2259 { "hm", 2, BFD_RELOC_SPARC_HM10, 1, 1 },
2260 { "lm", 2, BFD_RELOC_SPARC_LM22, 1, 1 },
2261 { "h44", 3, BFD_RELOC_SPARC_H44, 1, 0 },
2262 { "m44", 3, BFD_RELOC_SPARC_M44, 1, 0 },
2263 { "l44", 3, BFD_RELOC_SPARC_L44, 1, 0 },
2264 { "uhi", 3, BFD_RELOC_SPARC_HH22, 1, 0 },
2265 { "ulo", 3, BFD_RELOC_SPARC_HM10, 1, 0 },
2266 { "tgd_hi22", 8, BFD_RELOC_SPARC_TLS_GD_HI22, 0, 0 },
2267 { "tgd_lo10", 8, BFD_RELOC_SPARC_TLS_GD_LO10, 0, 0 },
2268 { "tldm_hi22", 9, BFD_RELOC_SPARC_TLS_LDM_HI22, 0, 0 },
2269 { "tldm_lo10", 9, BFD_RELOC_SPARC_TLS_LDM_LO10, 0, 0 },
2270 { "tldo_hix22", 10, BFD_RELOC_SPARC_TLS_LDO_HIX22, 0,
2271 0 },
2272 { "tldo_lox10", 10, BFD_RELOC_SPARC_TLS_LDO_LOX10, 0,
2273 0 },
2274 { "tie_hi22", 8, BFD_RELOC_SPARC_TLS_IE_HI22, 0, 0 },
2275 { "tie_lo10", 8, BFD_RELOC_SPARC_TLS_IE_LO10, 0, 0 },
2276 { "tle_hix22", 9, BFD_RELOC_SPARC_TLS_LE_HIX22, 0, 0 },
2277 { "tle_lox10", 9, BFD_RELOC_SPARC_TLS_LE_LOX10, 0, 0 },
2278 { NULL, 0, 0, 0, 0 }
2279 };
2280 const struct ops *o;
2281
2282 for (o = ops; o->name; o++)
2283 if (strncmp (s + 1, o->name, o->len) == 0)
2284 break;
2285 if (o->name == NULL)
2286 break;
2287
2288 if (s[o->len + 1] != '(')
2289 {
2290 as_bad (_("Illegal operands: %%%s requires arguments in ()"), o->name);
2291 return special_case;
2292 }
2293
2294 op_arg = o->name;
2295 the_insn.reloc = o->reloc;
2296 s += o->len + 2;
2297 v9_arg_p = o->v9_p;
2298 }
2299
2300 /* Note that if the get_expression() fails, we will still
2301 have created U entries in the symbol table for the
2302 'symbols' in the input string. Try not to create U
2303 symbols for registers, etc. */
2304
2305 /* This stuff checks to see if the expression ends in
2306 +%reg. If it does, it removes the register from
2307 the expression, and re-sets 's' to point to the
2308 right place. */
2309
2310 if (op_arg)
2311 {
2312 int npar = 0;
2313
2314 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2315 if (*s1 == '(')
2316 npar++;
2317 else if (*s1 == ')')
2318 {
2319 if (!npar)
2320 break;
2321 npar--;
2322 }
2323
2324 if (*s1 != ')')
2325 {
2326 as_bad (_("Illegal operands: %%%s requires arguments in ()"), op_arg);
2327 return special_case;
2328 }
2329
2330 *s1 = '\0';
2331 (void) get_expression (s);
2332 *s1 = ')';
2333 s = s1 + 1;
2334 if (*s == ',' || *s == ']' || !*s)
2335 continue;
2336 if (*s != '+' && *s != '-')
2337 {
2338 as_bad (_("Illegal operands: Can't do arithmetics other than + and - involving %%%s()"), op_arg);
2339 return special_case;
2340 }
2341 *s1 = '0';
2342 s = s1;
2343 op_exp = the_insn.exp;
2344 memset (&the_insn.exp, 0, sizeof (the_insn.exp));
2345 }
2346
2347 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2348 ;
2349
2350 if (s1 != s && ISDIGIT (s1[-1]))
2351 {
2352 if (s1[-2] == '%' && s1[-3] == '+')
2353 s1 -= 3;
2354 else if (strchr ("goli0123456789", s1[-2]) && s1[-3] == '%' && s1[-4] == '+')
2355 s1 -= 4;
2356 else
2357 s1 = NULL;
2358 if (s1)
2359 {
2360 *s1 = '\0';
2361 if (op_arg && s1 == s + 1)
2362 the_insn.exp.X_op = O_absent;
2363 else
2364 (void) get_expression (s);
2365 *s1 = '+';
2366 if (op_arg)
2367 *s = ')';
2368 s = s1;
2369 }
2370 }
2371 else
2372 s1 = NULL;
2373
2374 if (!s1)
2375 {
2376 (void) get_expression (s);
2377 if (op_arg)
2378 *s = ')';
2379 s = expr_end;
2380 }
2381
2382 if (op_arg)
2383 {
2384 the_insn.exp2 = the_insn.exp;
2385 the_insn.exp = op_exp;
2386 if (the_insn.exp2.X_op == O_absent)
2387 the_insn.exp2.X_op = O_illegal;
2388 else if (the_insn.exp.X_op == O_absent)
2389 {
2390 the_insn.exp = the_insn.exp2;
2391 the_insn.exp2.X_op = O_illegal;
2392 }
2393 else if (the_insn.exp.X_op == O_constant)
2394 {
2395 valueT val = the_insn.exp.X_add_number;
2396 switch (the_insn.reloc)
2397 {
2398 default:
2399 break;
2400
2401 case BFD_RELOC_SPARC_HH22:
2402 val = BSR (val, 32);
2403 /* Fall through. */
2404
2405 case BFD_RELOC_SPARC_LM22:
2406 case BFD_RELOC_HI22:
2407 val = (val >> 10) & 0x3fffff;
2408 break;
2409
2410 case BFD_RELOC_SPARC_HM10:
2411 val = BSR (val, 32);
2412 /* Fall through. */
2413
2414 case BFD_RELOC_LO10:
2415 val &= 0x3ff;
2416 break;
2417
2418 case BFD_RELOC_SPARC_H44:
2419 val >>= 22;
2420 val &= 0x3fffff;
2421 break;
2422
2423 case BFD_RELOC_SPARC_M44:
2424 val >>= 12;
2425 val &= 0x3ff;
2426 break;
2427
2428 case BFD_RELOC_SPARC_L44:
2429 val &= 0xfff;
2430 break;
2431
2432 case BFD_RELOC_SPARC_HIX22:
2433 val = ~val;
2434 val = (val >> 10) & 0x3fffff;
2435 break;
2436
2437 case BFD_RELOC_SPARC_LOX10:
2438 val = (val & 0x3ff) | 0x1c00;
2439 break;
2440 }
2441 the_insn.exp = the_insn.exp2;
2442 the_insn.exp.X_add_number += val;
2443 the_insn.exp2.X_op = O_illegal;
2444 the_insn.reloc = old_reloc;
2445 }
2446 else if (the_insn.exp2.X_op != O_constant)
2447 {
2448 as_bad (_("Illegal operands: Can't add non-constant expression to %%%s()"), op_arg);
2449 return special_case;
2450 }
2451 else
2452 {
2453 if (old_reloc != BFD_RELOC_SPARC13
2454 || the_insn.reloc != BFD_RELOC_LO10
2455 || sparc_arch_size != 64
2456 || sparc_pic_code)
2457 {
2458 as_bad (_("Illegal operands: Can't do arithmetics involving %%%s() of a relocatable symbol"), op_arg);
2459 return special_case;
2460 }
2461 the_insn.reloc = BFD_RELOC_SPARC_OLO10;
2462 }
2463 }
2464 }
2465 /* Check for constants that don't require emitting a reloc. */
2466 if (the_insn.exp.X_op == O_constant
2467 && the_insn.exp.X_add_symbol == 0
2468 && the_insn.exp.X_op_symbol == 0)
2469 {
2470 /* For pc-relative call instructions, we reject
2471 constants to get better code. */
2472 if (the_insn.pcrel
2473 && the_insn.reloc == BFD_RELOC_32_PCREL_S2
2474 && in_signed_range (the_insn.exp.X_add_number, 0x3fff))
2475 {
2476 error_message = _(": PC-relative operand can't be a constant");
2477 goto error;
2478 }
2479
2480 if (the_insn.reloc >= BFD_RELOC_SPARC_TLS_GD_HI22
2481 && the_insn.reloc <= BFD_RELOC_SPARC_TLS_TPOFF64)
2482 {
2483 error_message = _(": TLS operand can't be a constant");
2484 goto error;
2485 }
2486
2487 /* Constants that won't fit are checked in md_apply_fix3
2488 and bfd_install_relocation.
2489 ??? It would be preferable to install the constants
2490 into the insn here and save having to create a fixS
2491 for each one. There already exists code to handle
2492 all the various cases (e.g. in md_apply_fix3 and
2493 bfd_install_relocation) so duplicating all that code
2494 here isn't right. */
2495 }
2496
2497 continue;
2498
2499 case 'a':
2500 if (*s++ == 'a')
2501 {
2502 opcode |= ANNUL;
2503 continue;
2504 }
2505 break;
2506
2507 case 'A':
2508 {
2509 int asi = 0;
2510
2511 /* Parse an asi. */
2512 if (*s == '#')
2513 {
2514 if (! parse_keyword_arg (sparc_encode_asi, &s, &asi))
2515 {
2516 error_message = _(": invalid ASI name");
2517 goto error;
2518 }
2519 }
2520 else
2521 {
2522 if (! parse_const_expr_arg (&s, &asi))
2523 {
2524 error_message = _(": invalid ASI expression");
2525 goto error;
2526 }
2527 if (asi < 0 || asi > 255)
2528 {
2529 error_message = _(": invalid ASI number");
2530 goto error;
2531 }
2532 }
2533 opcode |= ASI (asi);
2534 continue;
2535 } /* Alternate space. */
2536
2537 case 'p':
2538 if (strncmp (s, "%psr", 4) == 0)
2539 {
2540 s += 4;
2541 continue;
2542 }
2543 break;
2544
2545 case 'q': /* Floating point queue. */
2546 if (strncmp (s, "%fq", 3) == 0)
2547 {
2548 s += 3;
2549 continue;
2550 }
2551 break;
2552
2553 case 'Q': /* Coprocessor queue. */
2554 if (strncmp (s, "%cq", 3) == 0)
2555 {
2556 s += 3;
2557 continue;
2558 }
2559 break;
2560
2561 case 'S':
2562 if (strcmp (str, "set") == 0
2563 || strcmp (str, "setuw") == 0)
2564 {
2565 special_case = SPECIAL_CASE_SET;
2566 continue;
2567 }
2568 else if (strcmp (str, "setsw") == 0)
2569 {
2570 special_case = SPECIAL_CASE_SETSW;
2571 continue;
2572 }
2573 else if (strcmp (str, "setx") == 0)
2574 {
2575 special_case = SPECIAL_CASE_SETX;
2576 continue;
2577 }
2578 else if (strncmp (str, "fdiv", 4) == 0)
2579 {
2580 special_case = SPECIAL_CASE_FDIV;
2581 continue;
2582 }
2583 break;
2584
2585 case 'o':
2586 if (strncmp (s, "%asi", 4) != 0)
2587 break;
2588 s += 4;
2589 continue;
2590
2591 case 's':
2592 if (strncmp (s, "%fprs", 5) != 0)
2593 break;
2594 s += 5;
2595 continue;
2596
2597 case 'E':
2598 if (strncmp (s, "%ccr", 4) != 0)
2599 break;
2600 s += 4;
2601 continue;
2602
2603 case 't':
2604 if (strncmp (s, "%tbr", 4) != 0)
2605 break;
2606 s += 4;
2607 continue;
2608
2609 case 'w':
2610 if (strncmp (s, "%wim", 4) != 0)
2611 break;
2612 s += 4;
2613 continue;
2614
2615 case 'x':
2616 {
2617 char *push = input_line_pointer;
2618 expressionS e;
2619
2620 input_line_pointer = s;
2621 expression (&e);
2622 if (e.X_op == O_constant)
2623 {
2624 int n = e.X_add_number;
2625 if (n != e.X_add_number || (n & ~0x1ff) != 0)
2626 as_bad (_("OPF immediate operand out of range (0-0x1ff)"));
2627 else
2628 opcode |= e.X_add_number << 5;
2629 }
2630 else
2631 as_bad (_("non-immediate OPF operand, ignored"));
2632 s = input_line_pointer;
2633 input_line_pointer = push;
2634 continue;
2635 }
2636
2637 case 'y':
2638 if (strncmp (s, "%y", 2) != 0)
2639 break;
2640 s += 2;
2641 continue;
2642
2643 case 'u':
2644 case 'U':
2645 {
2646 /* Parse a sparclet cpreg. */
2647 int cpreg;
2648 if (! parse_keyword_arg (sparc_encode_sparclet_cpreg, &s, &cpreg))
2649 {
2650 error_message = _(": invalid cpreg name");
2651 goto error;
2652 }
2653 opcode |= (*args == 'U' ? RS1 (cpreg) : RD (cpreg));
2654 continue;
2655 }
2656
2657 default:
2658 as_fatal (_("failed sanity check."));
2659 } /* switch on arg code. */
2660
2661 /* Break out of for() loop. */
2662 break;
2663 } /* For each arg that we expect. */
2664
2665 error:
2666 if (match == 0)
2667 {
2668 /* Args don't match. */
2669 if (&insn[1] - sparc_opcodes < sparc_num_opcodes
2670 && (insn->name == insn[1].name
2671 || !strcmp (insn->name, insn[1].name)))
2672 {
2673 ++insn;
2674 s = argsStart;
2675 continue;
2676 }
2677 else
2678 {
2679 as_bad (_("Illegal operands%s"), error_message);
2680 return special_case;
2681 }
2682 }
2683 else
2684 {
2685 /* We have a match. Now see if the architecture is OK. */
2686 int needed_arch_mask = insn->architecture;
2687
2688 if (v9_arg_p)
2689 {
2690 needed_arch_mask &=
2691 ~(SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9) - 1);
2692 if (! needed_arch_mask)
2693 needed_arch_mask =
2694 SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9);
2695 }
2696
2697 if (needed_arch_mask
2698 & SPARC_OPCODE_SUPPORTED (current_architecture))
2699 /* OK. */
2700 ;
2701 /* Can we bump up the architecture? */
2702 else if (needed_arch_mask
2703 & SPARC_OPCODE_SUPPORTED (max_architecture))
2704 {
2705 enum sparc_opcode_arch_val needed_architecture =
2706 sparc_ffs (SPARC_OPCODE_SUPPORTED (max_architecture)
2707 & needed_arch_mask);
2708
2709 assert (needed_architecture <= SPARC_OPCODE_ARCH_MAX);
2710 if (warn_on_bump
2711 && needed_architecture > warn_after_architecture)
2712 {
2713 as_warn (_("architecture bumped from \"%s\" to \"%s\" on \"%s\""),
2714 sparc_opcode_archs[current_architecture].name,
2715 sparc_opcode_archs[needed_architecture].name,
2716 str);
2717 warn_after_architecture = needed_architecture;
2718 }
2719 current_architecture = needed_architecture;
2720 }
2721 /* Conflict. */
2722 /* ??? This seems to be a bit fragile. What if the next entry in
2723 the opcode table is the one we want and it is supported?
2724 It is possible to arrange the table today so that this can't
2725 happen but what about tomorrow? */
2726 else
2727 {
2728 int arch, printed_one_p = 0;
2729 char *p;
2730 char required_archs[SPARC_OPCODE_ARCH_MAX * 16];
2731
2732 /* Create a list of the architectures that support the insn. */
2733 needed_arch_mask &= ~SPARC_OPCODE_SUPPORTED (max_architecture);
2734 p = required_archs;
2735 arch = sparc_ffs (needed_arch_mask);
2736 while ((1 << arch) <= needed_arch_mask)
2737 {
2738 if ((1 << arch) & needed_arch_mask)
2739 {
2740 if (printed_one_p)
2741 *p++ = '|';
2742 strcpy (p, sparc_opcode_archs[arch].name);
2743 p += strlen (p);
2744 printed_one_p = 1;
2745 }
2746 ++arch;
2747 }
2748
2749 as_bad (_("Architecture mismatch on \"%s\"."), str);
2750 as_tsktsk (_(" (Requires %s; requested architecture is %s.)"),
2751 required_archs,
2752 sparc_opcode_archs[max_architecture].name);
2753 return special_case;
2754 }
2755 } /* If no match. */
2756
2757 break;
2758 } /* Forever looking for a match. */
2759
2760 the_insn.opcode = opcode;
2761 return special_case;
2762}
2763
2764/* Parse an argument that can be expressed as a keyword.
2765 (eg: #StoreStore or %ccfr).
2766 The result is a boolean indicating success.
2767 If successful, INPUT_POINTER is updated. */
2768
2769static int
2770parse_keyword_arg (lookup_fn, input_pointerP, valueP)
2771 int (*lookup_fn) PARAMS ((const char *));
2772 char **input_pointerP;
2773 int *valueP;
2774{
2775 int value;
2776 char c, *p, *q;
2777
2778 p = *input_pointerP;
2779 for (q = p + (*p == '#' || *p == '%');
2780 ISALNUM (*q) || *q == '_';
2781 ++q)
2782 continue;
2783 c = *q;
2784 *q = 0;
2785 value = (*lookup_fn) (p);
2786 *q = c;
2787 if (value == -1)
2788 return 0;
2789 *valueP = value;
2790 *input_pointerP = q;
2791 return 1;
2792}
2793
2794/* Parse an argument that is a constant expression.
2795 The result is a boolean indicating success. */
2796
2797static int
2798parse_const_expr_arg (input_pointerP, valueP)
2799 char **input_pointerP;
2800 int *valueP;
2801{
2802 char *save = input_line_pointer;
2803 expressionS exp;
2804
2805 input_line_pointer = *input_pointerP;
2806 /* The next expression may be something other than a constant
2807 (say if we're not processing the right variant of the insn).
2808 Don't call expression unless we're sure it will succeed as it will
2809 signal an error (which we want to defer until later). */
2810 /* FIXME: It might be better to define md_operand and have it recognize
2811 things like %asi, etc. but continuing that route through to the end
2812 is a lot of work. */
2813 if (*input_line_pointer == '%')
2814 {
2815 input_line_pointer = save;
2816 return 0;
2817 }
2818 expression (&exp);
2819 *input_pointerP = input_line_pointer;
2820 input_line_pointer = save;
2821 if (exp.X_op != O_constant)
2822 return 0;
2823 *valueP = exp.X_add_number;
2824 return 1;
2825}
2826
2827/* Subroutine of sparc_ip to parse an expression. */
2828
2829static int
2830get_expression (str)
2831 char *str;
2832{
2833 char *save_in;
2834 segT seg;
2835
2836 save_in = input_line_pointer;
2837 input_line_pointer = str;
2838 seg = expression (&the_insn.exp);
2839 if (seg != absolute_section
2840 && seg != text_section
2841 && seg != data_section
2842 && seg != bss_section
2843 && seg != undefined_section)
2844 {
2845 the_insn.error = _("bad segment");
2846 expr_end = input_line_pointer;
2847 input_line_pointer = save_in;
2848 return 1;
2849 }
2850 expr_end = input_line_pointer;
2851 input_line_pointer = save_in;
2852 return 0;
2853}
2854
2855/* Subroutine of md_assemble to output one insn. */
2856
2857static void
2858output_insn (insn, the_insn)
2859 const struct sparc_opcode *insn;
2860 struct sparc_it *the_insn;
2861{
2862 char *toP = frag_more (4);
2863
2864 /* Put out the opcode. */
2865 if (INSN_BIG_ENDIAN)
2866 number_to_chars_bigendian (toP, (valueT) the_insn->opcode, 4);
2867 else
2868 number_to_chars_littleendian (toP, (valueT) the_insn->opcode, 4);
2869
2870 /* Put out the symbol-dependent stuff. */
2871 if (the_insn->reloc != BFD_RELOC_NONE)
2872 {
2873 fixS *fixP = fix_new_exp (frag_now, /* Which frag. */
2874 (toP - frag_now->fr_literal), /* Where. */
2875 4, /* Size. */
2876 &the_insn->exp,
2877 the_insn->pcrel,
2878 the_insn->reloc);
2879 /* Turn off overflow checking in fixup_segment. We'll do our
2880 own overflow checking in md_apply_fix3. This is necessary because
2881 the insn size is 4 and fixup_segment will signal an overflow for
2882 large 8 byte quantities. */
2883 fixP->fx_no_overflow = 1;
2884 if (the_insn->reloc == BFD_RELOC_SPARC_OLO10)
2885 fixP->tc_fix_data = the_insn->exp2.X_add_number;
2886 }
2887
2888 last_insn = insn;
2889 last_opcode = the_insn->opcode;
2890
2891#ifdef OBJ_ELF
2892 dwarf2_emit_insn (4);
2893#endif
2894}
2895
2896
2897/* This is identical to the md_atof in m68k.c. I think this is right,
2898 but I'm not sure.
2899
2900 Turn a string in input_line_pointer into a floating point constant
2901 of type TYPE, and store the appropriate bytes in *LITP. The number
2902 of LITTLENUMS emitted is stored in *SIZEP. An error message is
2903 returned, or NULL on OK. */
2904
2905/* Equal to MAX_PRECISION in atof-ieee.c. */
2906#define MAX_LITTLENUMS 6
2907
2908char *
2909md_atof (type, litP, sizeP)
2910 char type;
2911 char *litP;
2912 int *sizeP;
2913{
2914 int i, prec;
2915 LITTLENUM_TYPE words[MAX_LITTLENUMS];
2916 char *t;
2917
2918 switch (type)
2919 {
2920 case 'f':
2921 case 'F':
2922 case 's':
2923 case 'S':
2924 prec = 2;
2925 break;
2926
2927 case 'd':
2928 case 'D':
2929 case 'r':
2930 case 'R':
2931 prec = 4;
2932 break;
2933
2934 case 'x':
2935 case 'X':
2936 prec = 6;
2937 break;
2938
2939 case 'p':
2940 case 'P':
2941 prec = 6;
2942 break;
2943
2944 default:
2945 *sizeP = 0;
2946 return _("Bad call to MD_ATOF()");
2947 }
2948
2949 t = atof_ieee (input_line_pointer, type, words);
2950 if (t)
2951 input_line_pointer = t;
2952 *sizeP = prec * sizeof (LITTLENUM_TYPE);
2953
2954 if (target_big_endian)
2955 {
2956 for (i = 0; i < prec; i++)
2957 {
2958 md_number_to_chars (litP, (valueT) words[i],
2959 sizeof (LITTLENUM_TYPE));
2960 litP += sizeof (LITTLENUM_TYPE);
2961 }
2962 }
2963 else
2964 {
2965 for (i = prec - 1; i >= 0; i--)
2966 {
2967 md_number_to_chars (litP, (valueT) words[i],
2968 sizeof (LITTLENUM_TYPE));
2969 litP += sizeof (LITTLENUM_TYPE);
2970 }
2971 }
2972
2973 return 0;
2974}
2975
2976/* Write a value out to the object file, using the appropriate
2977 endianness. */
2978
2979void
2980md_number_to_chars (buf, val, n)
2981 char *buf;
2982 valueT val;
2983 int n;
2984{
2985 if (target_big_endian)
2986 number_to_chars_bigendian (buf, val, n);
2987 else if (target_little_endian_data
2988 && ((n == 4 || n == 2) && ~now_seg->flags & SEC_ALLOC))
2989 /* Output debug words, which are not in allocated sections, as big
2990 endian. */
2991 number_to_chars_bigendian (buf, val, n);
2992 else if (target_little_endian_data || ! target_big_endian)
2993 number_to_chars_littleendian (buf, val, n);
2994}
2995
2996
2997/* Apply a fixS to the frags, now that we know the value it ought to
2998 hold. */
2999
3000void
3001md_apply_fix3 (fixP, valP, segment)
3002 fixS *fixP;
3003 valueT *valP;
3004 segT segment ATTRIBUTE_UNUSED;
3005{
3006 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3007 offsetT val = * (offsetT *) valP;
3008 long insn;
3009
3010 assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
3011
3012 fixP->fx_addnumber = val; /* Remember value for emit_reloc. */
3013
3014#ifdef OBJ_ELF
3015 /* SPARC ELF relocations don't use an addend in the data field. */
3016 if (fixP->fx_addsy != NULL)
3017 return;
3018#endif
3019
3020 /* This is a hack. There should be a better way to
3021 handle this. Probably in terms of howto fields, once
3022 we can look at these fixups in terms of howtos. */
3023 if (fixP->fx_r_type == BFD_RELOC_32_PCREL_S2 && fixP->fx_addsy)
3024 val += fixP->fx_where + fixP->fx_frag->fr_address;
3025
3026#ifdef OBJ_AOUT
3027 /* FIXME: More ridiculous gas reloc hacking. If we are going to
3028 generate a reloc, then we just want to let the reloc addend set
3029 the value. We do not want to also stuff the addend into the
3030 object file. Including the addend in the object file works when
3031 doing a static link, because the linker will ignore the object
3032 file contents. However, the dynamic linker does not ignore the
3033 object file contents. */
3034 if (fixP->fx_addsy != NULL
3035 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2)
3036 val = 0;
3037
3038 /* When generating PIC code, we do not want an addend for a reloc
3039 against a local symbol. We adjust fx_addnumber to cancel out the
3040 value already included in val, and to also cancel out the
3041 adjustment which bfd_install_relocation will create. */
3042 if (sparc_pic_code
3043 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2
3044 && fixP->fx_addsy != NULL
3045 && ! S_IS_COMMON (fixP->fx_addsy)
3046 && symbol_section_p (fixP->fx_addsy))
3047 fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3048
3049 /* When generating PIC code, we need to fiddle to get
3050 bfd_install_relocation to do the right thing for a PC relative
3051 reloc against a local symbol which we are going to keep. */
3052 if (sparc_pic_code
3053 && fixP->fx_r_type == BFD_RELOC_32_PCREL_S2
3054 && fixP->fx_addsy != NULL
3055 && (S_IS_EXTERNAL (fixP->fx_addsy)
3056 || S_IS_WEAK (fixP->fx_addsy))
3057 && S_IS_DEFINED (fixP->fx_addsy)
3058 && ! S_IS_COMMON (fixP->fx_addsy))
3059 {
3060 val = 0;
3061 fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3062 }
3063#endif
3064
3065 /* If this is a data relocation, just output VAL. */
3066
3067 if (fixP->fx_r_type == BFD_RELOC_16
3068 || fixP->fx_r_type == BFD_RELOC_SPARC_UA16)
3069 {
3070 md_number_to_chars (buf, val, 2);
3071 }
3072 else if (fixP->fx_r_type == BFD_RELOC_32
3073 || fixP->fx_r_type == BFD_RELOC_SPARC_UA32
3074 || fixP->fx_r_type == BFD_RELOC_SPARC_REV32)
3075 {
3076 md_number_to_chars (buf, val, 4);
3077 }
3078 else if (fixP->fx_r_type == BFD_RELOC_64
3079 || fixP->fx_r_type == BFD_RELOC_SPARC_UA64)
3080 {
3081 md_number_to_chars (buf, val, 8);
3082 }
3083 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3084 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3085 {
3086 fixP->fx_done = 0;
3087 return;
3088 }
3089 else
3090 {
3091 /* It's a relocation against an instruction. */
3092
3093 if (INSN_BIG_ENDIAN)
3094 insn = bfd_getb32 ((unsigned char *) buf);
3095 else
3096 insn = bfd_getl32 ((unsigned char *) buf);
3097
3098 switch (fixP->fx_r_type)
3099 {
3100 case BFD_RELOC_32_PCREL_S2:
3101 val = val >> 2;
3102 /* FIXME: This increment-by-one deserves a comment of why it's
3103 being done! */
3104 if (! sparc_pic_code
3105 || fixP->fx_addsy == NULL
3106 || symbol_section_p (fixP->fx_addsy))
3107 ++val;
3108
3109 insn |= val & 0x3fffffff;
3110
3111 /* See if we have a delay slot. */
3112 if (sparc_relax && fixP->fx_where + 8 <= fixP->fx_frag->fr_fix)
3113 {
3114#define G0 0
3115#define O7 15
3116#define XCC (2 << 20)
3117#define COND(x) (((x)&0xf)<<25)
3118#define CONDA COND(0x8)
3119#define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
3120#define INSN_BA (F2(0,2) | CONDA)
3121#define INSN_OR F3(2, 0x2, 0)
3122#define INSN_NOP F2(0,4)
3123
3124 long delay;
3125
3126 /* If the instruction is a call with either:
3127 restore
3128 arithmetic instruction with rd == %o7
3129 where rs1 != %o7 and rs2 if it is register != %o7
3130 then we can optimize if the call destination is near
3131 by changing the call into a branch always. */
3132 if (INSN_BIG_ENDIAN)
3133 delay = bfd_getb32 ((unsigned char *) buf + 4);
3134 else
3135 delay = bfd_getl32 ((unsigned char *) buf + 4);
3136 if ((insn & OP (~0)) != OP (1) || (delay & OP (~0)) != OP (2))
3137 break;
3138 if ((delay & OP3 (~0)) != OP3 (0x3d) /* Restore. */
3139 && ((delay & OP3 (0x28)) != 0 /* Arithmetic. */
3140 || ((delay & RD (~0)) != RD (O7))))
3141 break;
3142 if ((delay & RS1 (~0)) == RS1 (O7)
3143 || ((delay & F3I (~0)) == 0
3144 && (delay & RS2 (~0)) == RS2 (O7)))
3145 break;
3146 /* Ensure the branch will fit into simm22. */
3147 if ((val & 0x3fe00000)
3148 && (val & 0x3fe00000) != 0x3fe00000)
3149 break;
3150 /* Check if the arch is v9 and branch will fit
3151 into simm19. */
3152 if (((val & 0x3c0000) == 0
3153 || (val & 0x3c0000) == 0x3c0000)
3154 && (sparc_arch_size == 64
3155 || current_architecture >= SPARC_OPCODE_ARCH_V9))
3156 /* ba,pt %xcc */
3157 insn = INSN_BPA | (val & 0x7ffff);
3158 else
3159 /* ba */
3160 insn = INSN_BA | (val & 0x3fffff);
3161 if (fixP->fx_where >= 4
3162 && ((delay & (0xffffffff ^ RS1 (~0)))
3163 == (INSN_OR | RD (O7) | RS2 (G0))))
3164 {
3165 long setter;
3166 int reg;
3167
3168 if (INSN_BIG_ENDIAN)
3169 setter = bfd_getb32 ((unsigned char *) buf - 4);
3170 else
3171 setter = bfd_getl32 ((unsigned char *) buf - 4);
3172 if ((setter & (0xffffffff ^ RD (~0)))
3173 != (INSN_OR | RS1 (O7) | RS2 (G0)))
3174 break;
3175 /* The sequence was
3176 or %o7, %g0, %rN
3177 call foo
3178 or %rN, %g0, %o7
3179
3180 If call foo was replaced with ba, replace
3181 or %rN, %g0, %o7 with nop. */
3182 reg = (delay & RS1 (~0)) >> 14;
3183 if (reg != ((setter & RD (~0)) >> 25)
3184 || reg == G0 || reg == O7)
3185 break;
3186
3187 if (INSN_BIG_ENDIAN)
3188 bfd_putb32 (INSN_NOP, (unsigned char *) buf + 4);
3189 else
3190 bfd_putl32 (INSN_NOP, (unsigned char *) buf + 4);
3191 }
3192 }
3193 break;
3194
3195 case BFD_RELOC_SPARC_11:
3196 if (! in_signed_range (val, 0x7ff))
3197 as_bad_where (fixP->fx_file, fixP->fx_line,
3198 _("relocation overflow"));
3199 insn |= val & 0x7ff;
3200 break;
3201
3202 case BFD_RELOC_SPARC_10:
3203 if (! in_signed_range (val, 0x3ff))
3204 as_bad_where (fixP->fx_file, fixP->fx_line,
3205 _("relocation overflow"));
3206 insn |= val & 0x3ff;
3207 break;
3208
3209 case BFD_RELOC_SPARC_7:
3210 if (! in_bitfield_range (val, 0x7f))
3211 as_bad_where (fixP->fx_file, fixP->fx_line,
3212 _("relocation overflow"));
3213 insn |= val & 0x7f;
3214 break;
3215
3216 case BFD_RELOC_SPARC_6:
3217 if (! in_bitfield_range (val, 0x3f))
3218 as_bad_where (fixP->fx_file, fixP->fx_line,
3219 _("relocation overflow"));
3220 insn |= val & 0x3f;
3221 break;
3222
3223 case BFD_RELOC_SPARC_5:
3224 if (! in_bitfield_range (val, 0x1f))
3225 as_bad_where (fixP->fx_file, fixP->fx_line,
3226 _("relocation overflow"));
3227 insn |= val & 0x1f;
3228 break;
3229
3230 case BFD_RELOC_SPARC_WDISP16:
3231 /* FIXME: simplify. */
3232 if (((val > 0) && (val & ~0x3fffc))
3233 || ((val < 0) && (~(val - 1) & ~0x3fffc)))
3234 as_bad_where (fixP->fx_file, fixP->fx_line,
3235 _("relocation overflow"));
3236 /* FIXME: The +1 deserves a comment. */
3237 val = (val >> 2) + 1;
3238 insn |= ((val & 0xc000) << 6) | (val & 0x3fff);
3239 break;
3240
3241 case BFD_RELOC_SPARC_WDISP19:
3242 /* FIXME: simplify. */
3243 if (((val > 0) && (val & ~0x1ffffc))
3244 || ((val < 0) && (~(val - 1) & ~0x1ffffc)))
3245 as_bad_where (fixP->fx_file, fixP->fx_line,
3246 _("relocation overflow"));
3247 /* FIXME: The +1 deserves a comment. */
3248 val = (val >> 2) + 1;
3249 insn |= val & 0x7ffff;
3250 break;
3251
3252 case BFD_RELOC_SPARC_HH22:
3253 val = BSR (val, 32);
3254 /* Fall through. */
3255
3256 case BFD_RELOC_SPARC_LM22:
3257 case BFD_RELOC_HI22:
3258 if (!fixP->fx_addsy)
3259 insn |= (val >> 10) & 0x3fffff;
3260 else
3261 /* FIXME: Need comment explaining why we do this. */
3262 insn &= ~0xffff;
3263 break;
3264
3265 case BFD_RELOC_SPARC22:
3266 if (val & ~0x003fffff)
3267 as_bad_where (fixP->fx_file, fixP->fx_line,
3268 _("relocation overflow"));
3269 insn |= (val & 0x3fffff);
3270 break;
3271
3272 case BFD_RELOC_SPARC_HM10:
3273 val = BSR (val, 32);
3274 /* Fall through. */
3275
3276 case BFD_RELOC_LO10:
3277 if (!fixP->fx_addsy)
3278 insn |= val & 0x3ff;
3279 else
3280 /* FIXME: Need comment explaining why we do this. */
3281 insn &= ~0xff;
3282 break;
3283
3284 case BFD_RELOC_SPARC_OLO10:
3285 val &= 0x3ff;
3286 val += fixP->tc_fix_data;
3287 /* Fall through. */
3288
3289 case BFD_RELOC_SPARC13:
3290 if (! in_signed_range (val, 0x1fff))
3291 as_bad_where (fixP->fx_file, fixP->fx_line,
3292 _("relocation overflow"));
3293 insn |= val & 0x1fff;
3294 break;
3295
3296 case BFD_RELOC_SPARC_WDISP22:
3297 val = (val >> 2) + 1;
3298 /* Fall through. */
3299 case BFD_RELOC_SPARC_BASE22:
3300 insn |= val & 0x3fffff;
3301 break;
3302
3303 case BFD_RELOC_SPARC_H44:
3304 if (!fixP->fx_addsy)
3305 {
3306 bfd_vma tval = val;
3307 tval >>= 22;
3308 insn |= tval & 0x3fffff;
3309 }
3310 break;
3311
3312 case BFD_RELOC_SPARC_M44:
3313 if (!fixP->fx_addsy)
3314 insn |= (val >> 12) & 0x3ff;
3315 break;
3316
3317 case BFD_RELOC_SPARC_L44:
3318 if (!fixP->fx_addsy)
3319 insn |= val & 0xfff;
3320 break;
3321
3322 case BFD_RELOC_SPARC_HIX22:
3323 if (!fixP->fx_addsy)
3324 {
3325 val ^= ~(offsetT) 0;
3326 insn |= (val >> 10) & 0x3fffff;
3327 }
3328 break;
3329
3330 case BFD_RELOC_SPARC_LOX10:
3331 if (!fixP->fx_addsy)
3332 insn |= 0x1c00 | (val & 0x3ff);
3333 break;
3334
3335 case BFD_RELOC_NONE:
3336 default:
3337 as_bad_where (fixP->fx_file, fixP->fx_line,
3338 _("bad or unhandled relocation type: 0x%02x"),
3339 fixP->fx_r_type);
3340 break;
3341 }
3342
3343 if (INSN_BIG_ENDIAN)
3344 bfd_putb32 (insn, (unsigned char *) buf);
3345 else
3346 bfd_putl32 (insn, (unsigned char *) buf);
3347 }
3348
3349 /* Are we finished with this relocation now? */
3350 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
3351 fixP->fx_done = 1;
3352}
3353
3354/* Translate internal representation of relocation info to BFD target
3355 format. */
3356
3357arelent **
3358tc_gen_reloc (section, fixp)
3359 asection *section ATTRIBUTE_UNUSED;
3360 fixS *fixp;
3361{
3362 static arelent *relocs[3];
3363 arelent *reloc;
3364 bfd_reloc_code_real_type code;
3365
3366 relocs[0] = reloc = (arelent *) xmalloc (sizeof (arelent));
3367 relocs[1] = NULL;
3368
3369 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3370 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3371 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3372
3373 switch (fixp->fx_r_type)
3374 {
3375 case BFD_RELOC_16:
3376 case BFD_RELOC_32:
3377 case BFD_RELOC_HI22:
3378 case BFD_RELOC_LO10:
3379 case BFD_RELOC_32_PCREL_S2:
3380 case BFD_RELOC_SPARC13:
3381 case BFD_RELOC_SPARC22:
3382 case BFD_RELOC_SPARC_BASE13:
3383 case BFD_RELOC_SPARC_WDISP16:
3384 case BFD_RELOC_SPARC_WDISP19:
3385 case BFD_RELOC_SPARC_WDISP22:
3386 case BFD_RELOC_64:
3387 case BFD_RELOC_SPARC_5:
3388 case BFD_RELOC_SPARC_6:
3389 case BFD_RELOC_SPARC_7:
3390 case BFD_RELOC_SPARC_10:
3391 case BFD_RELOC_SPARC_11:
3392 case BFD_RELOC_SPARC_HH22:
3393 case BFD_RELOC_SPARC_HM10:
3394 case BFD_RELOC_SPARC_LM22:
3395 case BFD_RELOC_SPARC_PC_HH22:
3396 case BFD_RELOC_SPARC_PC_HM10:
3397 case BFD_RELOC_SPARC_PC_LM22:
3398 case BFD_RELOC_SPARC_H44:
3399 case BFD_RELOC_SPARC_M44:
3400 case BFD_RELOC_SPARC_L44:
3401 case BFD_RELOC_SPARC_HIX22:
3402 case BFD_RELOC_SPARC_LOX10:
3403 case BFD_RELOC_SPARC_REV32:
3404 case BFD_RELOC_SPARC_OLO10:
3405 case BFD_RELOC_SPARC_UA16:
3406 case BFD_RELOC_SPARC_UA32:
3407 case BFD_RELOC_SPARC_UA64:
3408 case BFD_RELOC_8_PCREL:
3409 case BFD_RELOC_16_PCREL:
3410 case BFD_RELOC_32_PCREL:
3411 case BFD_RELOC_64_PCREL:
3412 case BFD_RELOC_SPARC_PLT32:
3413 case BFD_RELOC_SPARC_PLT64:
3414 case BFD_RELOC_VTABLE_ENTRY:
3415 case BFD_RELOC_VTABLE_INHERIT:
3416 case BFD_RELOC_SPARC_TLS_GD_HI22:
3417 case BFD_RELOC_SPARC_TLS_GD_LO10:
3418 case BFD_RELOC_SPARC_TLS_GD_ADD:
3419 case BFD_RELOC_SPARC_TLS_GD_CALL:
3420 case BFD_RELOC_SPARC_TLS_LDM_HI22:
3421 case BFD_RELOC_SPARC_TLS_LDM_LO10:
3422 case BFD_RELOC_SPARC_TLS_LDM_ADD:
3423 case BFD_RELOC_SPARC_TLS_LDM_CALL:
3424 case BFD_RELOC_SPARC_TLS_LDO_HIX22:
3425 case BFD_RELOC_SPARC_TLS_LDO_LOX10:
3426 case BFD_RELOC_SPARC_TLS_LDO_ADD:
3427 case BFD_RELOC_SPARC_TLS_IE_HI22:
3428 case BFD_RELOC_SPARC_TLS_IE_LO10:
3429 case BFD_RELOC_SPARC_TLS_IE_LD:
3430 case BFD_RELOC_SPARC_TLS_IE_LDX:
3431 case BFD_RELOC_SPARC_TLS_IE_ADD:
3432 case BFD_RELOC_SPARC_TLS_LE_HIX22:
3433 case BFD_RELOC_SPARC_TLS_LE_LOX10:
3434 case BFD_RELOC_SPARC_TLS_DTPOFF32:
3435 case BFD_RELOC_SPARC_TLS_DTPOFF64:
3436 code = fixp->fx_r_type;
3437 break;
3438 default:
3439 abort ();
3440 return NULL;
3441 }
3442
3443#if defined (OBJ_ELF) || defined (OBJ_AOUT)
3444 /* If we are generating PIC code, we need to generate a different
3445 set of relocs. */
3446
3447#ifdef OBJ_ELF
3448#define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
3449#else
3450#define GOT_NAME "__GLOBAL_OFFSET_TABLE_"
3451#endif
3452
3453 /* This code must be parallel to the OBJ_ELF tc_fix_adjustable. */
3454
3455 if (sparc_pic_code)
3456 {
3457 switch (code)
3458 {
3459 case BFD_RELOC_32_PCREL_S2:
3460 if (generic_force_reloc (fixp))
3461 code = BFD_RELOC_SPARC_WPLT30;
3462 break;
3463 case BFD_RELOC_HI22:
3464 if (fixp->fx_addsy != NULL
3465 && strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
3466 code = BFD_RELOC_SPARC_PC22;
3467 else
3468 code = BFD_RELOC_SPARC_GOT22;
3469 break;
3470 case BFD_RELOC_LO10:
3471 if (fixp->fx_addsy != NULL
3472 && strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
3473 code = BFD_RELOC_SPARC_PC10;
3474 else
3475 code = BFD_RELOC_SPARC_GOT10;
3476 break;
3477 case BFD_RELOC_SPARC13:
3478 code = BFD_RELOC_SPARC_GOT13;
3479 break;
3480 default:
3481 break;
3482 }
3483 }
3484#endif /* defined (OBJ_ELF) || defined (OBJ_AOUT) */
3485
3486 if (code == BFD_RELOC_SPARC_OLO10)
3487 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO10);
3488 else
3489 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
3490 if (reloc->howto == 0)
3491 {
3492 as_bad_where (fixp->fx_file, fixp->fx_line,
3493 _("internal error: can't export reloc type %d (`%s')"),
3494 fixp->fx_r_type, bfd_get_reloc_code_name (code));
3495 xfree (reloc);
3496 relocs[0] = NULL;
3497 return relocs;
3498 }
3499
3500 /* @@ Why fx_addnumber sometimes and fx_offset other times? */
3501#ifdef OBJ_AOUT
3502
3503 if (reloc->howto->pc_relative == 0
3504 || code == BFD_RELOC_SPARC_PC10
3505 || code == BFD_RELOC_SPARC_PC22)
3506 reloc->addend = fixp->fx_addnumber;
3507 else if (sparc_pic_code
3508 && fixp->fx_r_type == BFD_RELOC_32_PCREL_S2
3509 && fixp->fx_addsy != NULL
3510 && (S_IS_EXTERNAL (fixp->fx_addsy)
3511 || S_IS_WEAK (fixp->fx_addsy))
3512 && S_IS_DEFINED (fixp->fx_addsy)
3513 && ! S_IS_COMMON (fixp->fx_addsy))
3514 reloc->addend = fixp->fx_addnumber;
3515 else
3516 reloc->addend = fixp->fx_offset - reloc->address;
3517
3518#else /* elf or coff */
3519
3520 if (code != BFD_RELOC_32_PCREL_S2
3521 && code != BFD_RELOC_SPARC_WDISP22
3522 && code != BFD_RELOC_SPARC_WDISP16
3523 && code != BFD_RELOC_SPARC_WDISP19
3524 && code != BFD_RELOC_SPARC_WPLT30
3525 && code != BFD_RELOC_SPARC_TLS_GD_CALL
3526 && code != BFD_RELOC_SPARC_TLS_LDM_CALL)
3527 reloc->addend = fixp->fx_addnumber;
3528 else if (symbol_section_p (fixp->fx_addsy))
3529 reloc->addend = (section->vma
3530 + fixp->fx_addnumber
3531 + md_pcrel_from (fixp));
3532 else
3533 reloc->addend = fixp->fx_offset;
3534#endif
3535
3536 /* We expand R_SPARC_OLO10 to R_SPARC_LO10 and R_SPARC_13
3537 on the same location. */
3538 if (code == BFD_RELOC_SPARC_OLO10)
3539 {
3540 relocs[1] = reloc = (arelent *) xmalloc (sizeof (arelent));
3541 relocs[2] = NULL;
3542
3543 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3544 *reloc->sym_ptr_ptr
3545 = symbol_get_bfdsym (section_symbol (absolute_section));
3546 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3547 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_SPARC13);
3548 reloc->addend = fixp->tc_fix_data;
3549 }
3550
3551 return relocs;
3552}
3553
3554
3555/* We have no need to default values of symbols. */
3556
3557symbolS *
3558md_undefined_symbol (name)
3559 char *name ATTRIBUTE_UNUSED;
3560{
3561 return 0;
3562}
3563
3564/* Round up a section size to the appropriate boundary. */
3565
3566valueT
3567md_section_align (segment, size)
3568 segT segment ATTRIBUTE_UNUSED;
3569 valueT size;
3570{
3571#ifndef OBJ_ELF
3572 /* This is not right for ELF; a.out wants it, and COFF will force
3573 the alignment anyways. */
3574 valueT align = ((valueT) 1
3575 << (valueT) bfd_get_section_alignment (stdoutput, segment));
3576 valueT newsize;
3577
3578 /* Turn alignment value into a mask. */
3579 align--;
3580 newsize = (size + align) & ~align;
3581 return newsize;
3582#else
3583 return size;
3584#endif
3585}
3586
3587/* Exactly what point is a PC-relative offset relative TO?
3588 On the sparc, they're relative to the address of the offset, plus
3589 its size. This gets us to the following instruction.
3590 (??? Is this right? FIXME-SOON) */
3591long
3592md_pcrel_from (fixP)
3593 fixS *fixP;
3594{
3595 long ret;
3596
3597 ret = fixP->fx_where + fixP->fx_frag->fr_address;
3598 if (! sparc_pic_code
3599 || fixP->fx_addsy == NULL
3600 || symbol_section_p (fixP->fx_addsy))
3601 ret += fixP->fx_size;
3602 return ret;
3603}
3604
3605
3606/* Return log2 (VALUE), or -1 if VALUE is not an exact positive power
3607 of two. */
3608
3609static int
3610log2 (value)
3611 int value;
3612{
3613 int shift;
3614
3615 if (value <= 0)
3616 return -1;
3617
3618 for (shift = 0; (value & 1) == 0; value >>= 1)
3619 ++shift;
3620
3621 return (value == 1) ? shift : -1;
3622}
3623
3624/* Sort of like s_lcomm. */
3625
3626#ifndef OBJ_ELF
3627static int max_alignment = 15;
3628#endif
3629
3630static void
3631s_reserve (ignore)
3632 int ignore ATTRIBUTE_UNUSED;
3633{
3634 char *name;
3635 char *p;
3636 char c;
3637 int align;
3638 int size;
3639 int temp;
3640 symbolS *symbolP;
3641
3642 name = input_line_pointer;
3643 c = get_symbol_end ();
3644 p = input_line_pointer;
3645 *p = c;
3646 SKIP_WHITESPACE ();
3647
3648 if (*input_line_pointer != ',')
3649 {
3650 as_bad (_("Expected comma after name"));
3651 ignore_rest_of_line ();
3652 return;
3653 }
3654
3655 ++input_line_pointer;
3656
3657 if ((size = get_absolute_expression ()) < 0)
3658 {
3659 as_bad (_("BSS length (%d.) <0! Ignored."), size);
3660 ignore_rest_of_line ();
3661 return;
3662 } /* Bad length. */
3663
3664 *p = 0;
3665 symbolP = symbol_find_or_make (name);
3666 *p = c;
3667
3668 if (strncmp (input_line_pointer, ",\"bss\"", 6) != 0
3669 && strncmp (input_line_pointer, ",\".bss\"", 7) != 0)
3670 {
3671 as_bad (_("bad .reserve segment -- expected BSS segment"));
3672 return;
3673 }
3674
3675 if (input_line_pointer[2] == '.')
3676 input_line_pointer += 7;
3677 else
3678 input_line_pointer += 6;
3679 SKIP_WHITESPACE ();
3680
3681 if (*input_line_pointer == ',')
3682 {
3683 ++input_line_pointer;
3684
3685 SKIP_WHITESPACE ();
3686 if (*input_line_pointer == '\n')
3687 {
3688 as_bad (_("missing alignment"));
3689 ignore_rest_of_line ();
3690 return;
3691 }
3692
3693 align = (int) get_absolute_expression ();
3694
3695#ifndef OBJ_ELF
3696 if (align > max_alignment)
3697 {
3698 align = max_alignment;
3699 as_warn (_("alignment too large; assuming %d"), align);
3700 }
3701#endif
3702
3703 if (align < 0)
3704 {
3705 as_bad (_("negative alignment"));
3706 ignore_rest_of_line ();
3707 return;
3708 }
3709
3710 if (align != 0)
3711 {
3712 temp = log2 (align);
3713 if (temp < 0)
3714 {
3715 as_bad (_("alignment not a power of 2"));
3716 ignore_rest_of_line ();
3717 return;
3718 }
3719
3720 align = temp;
3721 }
3722
3723 record_alignment (bss_section, align);
3724 }
3725 else
3726 align = 0;
3727
3728 if (!S_IS_DEFINED (symbolP)
3729#ifdef OBJ_AOUT
3730 && S_GET_OTHER (symbolP) == 0
3731 && S_GET_DESC (symbolP) == 0
3732#endif
3733 )
3734 {
3735 if (! need_pass_2)
3736 {
3737 char *pfrag;
3738 segT current_seg = now_seg;
3739 subsegT current_subseg = now_subseg;
3740
3741 /* Switch to bss. */
3742 subseg_set (bss_section, 1);
3743
3744 if (align)
3745 /* Do alignment. */
3746 frag_align (align, 0, 0);
3747
3748 /* Detach from old frag. */
3749 if (S_GET_SEGMENT (symbolP) == bss_section)
3750 symbol_get_frag (symbolP)->fr_symbol = NULL;
3751
3752 symbol_set_frag (symbolP, frag_now);
3753 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
3754 (offsetT) size, (char *) 0);
3755 *pfrag = 0;
3756
3757 S_SET_SEGMENT (symbolP, bss_section);
3758
3759 subseg_set (current_seg, current_subseg);
3760
3761#ifdef OBJ_ELF
3762 S_SET_SIZE (symbolP, size);
3763#endif
3764 }
3765 }
3766 else
3767 {
3768 as_warn ("Ignoring attempt to re-define symbol %s",
3769 S_GET_NAME (symbolP));
3770 } /* if not redefining. */
3771
3772 demand_empty_rest_of_line ();
3773}
3774
3775static void
3776s_common (ignore)
3777 int ignore ATTRIBUTE_UNUSED;
3778{
3779 char *name;
3780 char c;
3781 char *p;
3782 int temp, size;
3783 symbolS *symbolP;
3784
3785 name = input_line_pointer;
3786 c = get_symbol_end ();
3787 /* Just after name is now '\0'. */
3788 p = input_line_pointer;
3789 *p = c;
3790 SKIP_WHITESPACE ();
3791 if (*input_line_pointer != ',')
3792 {
3793 as_bad (_("Expected comma after symbol-name"));
3794 ignore_rest_of_line ();
3795 return;
3796 }
3797
3798 /* Skip ','. */
3799 input_line_pointer++;
3800
3801 if ((temp = get_absolute_expression ()) < 0)
3802 {
3803 as_bad (_(".COMMon length (%d.) <0! Ignored."), temp);
3804 ignore_rest_of_line ();
3805 return;
3806 }
3807 size = temp;
3808 *p = 0;
3809 symbolP = symbol_find_or_make (name);
3810 *p = c;
3811 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
3812 {
3813 as_bad (_("Ignoring attempt to re-define symbol"));
3814 ignore_rest_of_line ();
3815 return;
3816 }
3817 if (S_GET_VALUE (symbolP) != 0)
3818 {
3819 if (S_GET_VALUE (symbolP) != (valueT) size)
3820 {
3821 as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %d."),
3822 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
3823 }
3824 }
3825 else
3826 {
3827#ifndef OBJ_ELF
3828 S_SET_VALUE (symbolP, (valueT) size);
3829 S_SET_EXTERNAL (symbolP);
3830#endif
3831 }
3832 know (symbol_get_frag (symbolP) == &zero_address_frag);
3833 if (*input_line_pointer != ',')
3834 {
3835 as_bad (_("Expected comma after common length"));
3836 ignore_rest_of_line ();
3837 return;
3838 }
3839 input_line_pointer++;
3840 SKIP_WHITESPACE ();
3841 if (*input_line_pointer != '"')
3842 {
3843 temp = get_absolute_expression ();
3844
3845#ifndef OBJ_ELF
3846 if (temp > max_alignment)
3847 {
3848 temp = max_alignment;
3849 as_warn (_("alignment too large; assuming %d"), temp);
3850 }
3851#endif
3852
3853 if (temp < 0)
3854 {
3855 as_bad (_("negative alignment"));
3856 ignore_rest_of_line ();
3857 return;
3858 }
3859
3860#ifdef OBJ_ELF
3861 if (symbol_get_obj (symbolP)->local)
3862 {
3863 segT old_sec;
3864 int old_subsec;
3865 char *p;
3866 int align;
3867
3868 old_sec = now_seg;
3869 old_subsec = now_subseg;
3870
3871 if (temp == 0)
3872 align = 0;
3873 else
3874 align = log2 (temp);
3875
3876 if (align < 0)
3877 {
3878 as_bad (_("alignment not a power of 2"));
3879 ignore_rest_of_line ();
3880 return;
3881 }
3882
3883 record_alignment (bss_section, align);
3884 subseg_set (bss_section, 0);
3885 if (align)
3886 frag_align (align, 0, 0);
3887 if (S_GET_SEGMENT (symbolP) == bss_section)
3888 symbol_get_frag (symbolP)->fr_symbol = 0;
3889 symbol_set_frag (symbolP, frag_now);
3890 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
3891 (offsetT) size, (char *) 0);
3892 *p = 0;
3893 S_SET_SEGMENT (symbolP, bss_section);
3894 S_CLEAR_EXTERNAL (symbolP);
3895 S_SET_SIZE (symbolP, size);
3896 subseg_set (old_sec, old_subsec);
3897 }
3898 else
3899#endif /* OBJ_ELF */
3900 {
3901 allocate_common:
3902 S_SET_VALUE (symbolP, (valueT) size);
3903#ifdef OBJ_ELF
3904 S_SET_ALIGN (symbolP, temp);
3905 S_SET_SIZE (symbolP, size);
3906#endif
3907 S_SET_EXTERNAL (symbolP);
3908 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
3909 }
3910 }
3911 else
3912 {
3913 input_line_pointer++;
3914 /* @@ Some use the dot, some don't. Can we get some consistency?? */
3915 if (*input_line_pointer == '.')
3916 input_line_pointer++;
3917 /* @@ Some say data, some say bss. */
3918 if (strncmp (input_line_pointer, "bss\"", 4)
3919 && strncmp (input_line_pointer, "data\"", 5))
3920 {
3921 while (*--input_line_pointer != '"')
3922 ;
3923 input_line_pointer--;
3924 goto bad_common_segment;
3925 }
3926 while (*input_line_pointer++ != '"')
3927 ;
3928 goto allocate_common;
3929 }
3930
3931#ifdef BFD_ASSEMBLER
3932 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
3933#endif
3934
3935 demand_empty_rest_of_line ();
3936 return;
3937
3938 {
3939 bad_common_segment:
3940 p = input_line_pointer;
3941 while (*p && *p != '\n')
3942 p++;
3943 c = *p;
3944 *p = '\0';
3945 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
3946 *p = c;
3947 input_line_pointer = p;
3948 ignore_rest_of_line ();
3949 return;
3950 }
3951}
3952
3953/* Handle the .empty pseudo-op. This supresses the warnings about
3954 invalid delay slot usage. */
3955
3956static void
3957s_empty (ignore)
3958 int ignore ATTRIBUTE_UNUSED;
3959{
3960 /* The easy way to implement is to just forget about the last
3961 instruction. */
3962 last_insn = NULL;
3963}
3964
3965static void
3966s_seg (ignore)
3967 int ignore ATTRIBUTE_UNUSED;
3968{
3969
3970 if (strncmp (input_line_pointer, "\"text\"", 6) == 0)
3971 {
3972 input_line_pointer += 6;
3973 s_text (0);
3974 return;
3975 }
3976 if (strncmp (input_line_pointer, "\"data\"", 6) == 0)
3977 {
3978 input_line_pointer += 6;
3979 s_data (0);
3980 return;
3981 }
3982 if (strncmp (input_line_pointer, "\"data1\"", 7) == 0)
3983 {
3984 input_line_pointer += 7;
3985 s_data1 ();
3986 return;
3987 }
3988 if (strncmp (input_line_pointer, "\"bss\"", 5) == 0)
3989 {
3990 input_line_pointer += 5;
3991 /* We only support 2 segments -- text and data -- for now, so
3992 things in the "bss segment" will have to go into data for now.
3993 You can still allocate SEG_BSS stuff with .lcomm or .reserve. */
3994 subseg_set (data_section, 255); /* FIXME-SOMEDAY. */
3995 return;
3996 }
3997 as_bad (_("Unknown segment type"));
3998 demand_empty_rest_of_line ();
3999}
4000
4001static void
4002s_data1 ()
4003{
4004 subseg_set (data_section, 1);
4005 demand_empty_rest_of_line ();
4006}
4007
4008static void
4009s_proc (ignore)
4010 int ignore ATTRIBUTE_UNUSED;
4011{
4012 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4013 {
4014 ++input_line_pointer;
4015 }
4016 ++input_line_pointer;
4017}
4018
4019/* This static variable is set by s_uacons to tell sparc_cons_align
4020 that the expession does not need to be aligned. */
4021
4022static int sparc_no_align_cons = 0;
4023
4024/* This static variable is set by sparc_cons to emit requested types
4025 of relocations in cons_fix_new_sparc. */
4026
4027static const char *sparc_cons_special_reloc;
4028
4029/* This handles the unaligned space allocation pseudo-ops, such as
4030 .uaword. .uaword is just like .word, but the value does not need
4031 to be aligned. */
4032
4033static void
4034s_uacons (bytes)
4035 int bytes;
4036{
4037 /* Tell sparc_cons_align not to align this value. */
4038 sparc_no_align_cons = 1;
4039 cons (bytes);
4040 sparc_no_align_cons = 0;
4041}
4042
4043/* This handles the native word allocation pseudo-op .nword.
4044 For sparc_arch_size 32 it is equivalent to .word, for
4045 sparc_arch_size 64 it is equivalent to .xword. */
4046
4047static void
4048s_ncons (bytes)
4049 int bytes ATTRIBUTE_UNUSED;
4050{
4051 cons (sparc_arch_size == 32 ? 4 : 8);
4052}
4053
4054#ifdef OBJ_ELF
4055/* Handle the SPARC ELF .register pseudo-op. This sets the binding of a
4056 global register.
4057 The syntax is:
4058
4059 .register %g[2367],{#scratch|symbolname|#ignore}
4060*/
4061
4062static void
4063s_register (ignore)
4064 int ignore ATTRIBUTE_UNUSED;
4065{
4066 char c;
4067 int reg;
4068 int flags;
4069 const char *regname;
4070
4071 if (input_line_pointer[0] != '%'
4072 || input_line_pointer[1] != 'g'
4073 || ((input_line_pointer[2] & ~1) != '2'
4074 && (input_line_pointer[2] & ~1) != '6')
4075 || input_line_pointer[3] != ',')
4076 as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
4077 reg = input_line_pointer[2] - '0';
4078 input_line_pointer += 4;
4079
4080 if (*input_line_pointer == '#')
4081 {
4082 ++input_line_pointer;
4083 regname = input_line_pointer;
4084 c = get_symbol_end ();
4085 if (strcmp (regname, "scratch") && strcmp (regname, "ignore"))
4086 as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
4087 if (regname[0] == 'i')
4088 regname = NULL;
4089 else
4090 regname = "";
4091 }
4092 else
4093 {
4094 regname = input_line_pointer;
4095 c = get_symbol_end ();
4096 }
4097 if (sparc_arch_size == 64)
4098 {
4099 if (globals[reg])
4100 {
4101 if ((regname && globals[reg] != (symbolS *) 1
4102 && strcmp (S_GET_NAME (globals[reg]), regname))
4103 || ((regname != NULL) ^ (globals[reg] != (symbolS *) 1)))
4104 as_bad (_("redefinition of global register"));
4105 }
4106 else
4107 {
4108 if (regname == NULL)
4109 globals[reg] = (symbolS *) 1;
4110 else
4111 {
4112 if (*regname)
4113 {
4114 if (symbol_find (regname))
4115 as_bad (_("Register symbol %s already defined."),
4116 regname);
4117 }
4118 globals[reg] = symbol_make (regname);
4119 flags = symbol_get_bfdsym (globals[reg])->flags;
4120 if (! *regname)
4121 flags = flags & ~(BSF_GLOBAL|BSF_LOCAL|BSF_WEAK);
4122 if (! (flags & (BSF_GLOBAL|BSF_LOCAL|BSF_WEAK)))
4123 flags |= BSF_GLOBAL;
4124 symbol_get_bfdsym (globals[reg])->flags = flags;
4125 S_SET_VALUE (globals[reg], (valueT) reg);
4126 S_SET_ALIGN (globals[reg], reg);
4127 S_SET_SIZE (globals[reg], 0);
4128 /* Although we actually want undefined_section here,
4129 we have to use absolute_section, because otherwise
4130 generic as code will make it a COM section.
4131 We fix this up in sparc_adjust_symtab. */
4132 S_SET_SEGMENT (globals[reg], absolute_section);
4133 S_SET_OTHER (globals[reg], 0);
4134 elf_symbol (symbol_get_bfdsym (globals[reg]))
4135 ->internal_elf_sym.st_info =
4136 ELF_ST_INFO(STB_GLOBAL, STT_REGISTER);
4137 elf_symbol (symbol_get_bfdsym (globals[reg]))
4138 ->internal_elf_sym.st_shndx = SHN_UNDEF;
4139 }
4140 }
4141 }
4142
4143 *input_line_pointer = c;
4144
4145 demand_empty_rest_of_line ();
4146}
4147
4148/* Adjust the symbol table. We set undefined sections for STT_REGISTER
4149 symbols which need it. */
4150
4151void
4152sparc_adjust_symtab ()
4153{
4154 symbolS *sym;
4155
4156 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
4157 {
4158 if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4159 ->internal_elf_sym.st_info) != STT_REGISTER)
4160 continue;
4161
4162 if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4163 ->internal_elf_sym.st_shndx != SHN_UNDEF))
4164 continue;
4165
4166 S_SET_SEGMENT (sym, undefined_section);
4167 }
4168}
4169#endif
4170
4171/* If the --enforce-aligned-data option is used, we require .word,
4172 et. al., to be aligned correctly. We do it by setting up an
4173 rs_align_code frag, and checking in HANDLE_ALIGN to make sure that
4174 no unexpected alignment was introduced.
4175
4176 The SunOS and Solaris native assemblers enforce aligned data by
4177 default. We don't want to do that, because gcc can deliberately
4178 generate misaligned data if the packed attribute is used. Instead,
4179 we permit misaligned data by default, and permit the user to set an
4180 option to check for it. */
4181
4182void
4183sparc_cons_align (nbytes)
4184 int nbytes;
4185{
4186 int nalign;
4187 char *p;
4188
4189 /* Only do this if we are enforcing aligned data. */
4190 if (! enforce_aligned_data)
4191 return;
4192
4193 /* Don't align if this is an unaligned pseudo-op. */
4194 if (sparc_no_align_cons)
4195 return;
4196
4197 nalign = log2 (nbytes);
4198 if (nalign == 0)
4199 return;
4200
4201 assert (nalign > 0);
4202
4203 if (now_seg == absolute_section)
4204 {
4205 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
4206 as_bad (_("misaligned data"));
4207 return;
4208 }
4209
4210 p = frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
4211 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
4212
4213 record_alignment (now_seg, nalign);
4214}
4215
4216/* This is called from HANDLE_ALIGN in tc-sparc.h. */
4217
4218void
4219sparc_handle_align (fragp)
4220 fragS *fragp;
4221{
4222 int count, fix;
4223 char *p;
4224
4225 count = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
4226
4227 switch (fragp->fr_type)
4228 {
4229 case rs_align_test:
4230 if (count != 0)
4231 as_bad_where (fragp->fr_file, fragp->fr_line, _("misaligned data"));
4232 break;
4233
4234 case rs_align_code:
4235 p = fragp->fr_literal + fragp->fr_fix;
4236 fix = 0;
4237
4238 if (count & 3)
4239 {
4240 fix = count & 3;
4241 memset (p, 0, fix);
4242 p += fix;
4243 count -= fix;
4244 }
4245
4246 if (SPARC_OPCODE_ARCH_V9_P (max_architecture) && count > 8)
4247 {
4248 unsigned wval = (0x30680000 | count >> 2); /* ba,a,pt %xcc, 1f */
4249 if (INSN_BIG_ENDIAN)
4250 number_to_chars_bigendian (p, wval, 4);
4251 else
4252 number_to_chars_littleendian (p, wval, 4);
4253 p += 4;
4254 count -= 4;
4255 fix += 4;
4256 }
4257
4258 if (INSN_BIG_ENDIAN)
4259 number_to_chars_bigendian (p, 0x01000000, 4);
4260 else
4261 number_to_chars_littleendian (p, 0x01000000, 4);
4262
4263 fragp->fr_fix += fix;
4264 fragp->fr_var = 4;
4265 break;
4266
4267 default:
4268 break;
4269 }
4270}
4271
4272#ifdef OBJ_ELF
4273/* Some special processing for a Sparc ELF file. */
4274
4275void
4276sparc_elf_final_processing ()
4277{
4278 /* Set the Sparc ELF flag bits. FIXME: There should probably be some
4279 sort of BFD interface for this. */
4280 if (sparc_arch_size == 64)
4281 {
4282 switch (sparc_memory_model)
4283 {
4284 case MM_RMO:
4285 elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_RMO;
4286 break;
4287 case MM_PSO:
4288 elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_PSO;
4289 break;
4290 default:
4291 break;
4292 }
4293 }
4294 else if (current_architecture >= SPARC_OPCODE_ARCH_V9)
4295 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_32PLUS;
4296 if (current_architecture == SPARC_OPCODE_ARCH_V9A)
4297 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1;
4298 else if (current_architecture == SPARC_OPCODE_ARCH_V9B)
4299 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1|EF_SPARC_SUN_US3;
4300}
4301
4302void
4303sparc_cons (exp, size)
4304 expressionS *exp;
4305 int size;
4306{
4307 char *save;
4308
4309 SKIP_WHITESPACE ();
4310 sparc_cons_special_reloc = NULL;
4311 save = input_line_pointer;
4312 if (input_line_pointer[0] == '%'
4313 && input_line_pointer[1] == 'r'
4314 && input_line_pointer[2] == '_')
4315 {
4316 if (strncmp (input_line_pointer + 3, "disp", 4) == 0)
4317 {
4318 input_line_pointer += 7;
4319 sparc_cons_special_reloc = "disp";
4320 }
4321 else if (strncmp (input_line_pointer + 3, "plt", 3) == 0)
4322 {
4323 if (size != 4 && size != 8)
4324 as_bad (_("Illegal operands: %%r_plt in %d-byte data field"), size);
4325 else
4326 {
4327 input_line_pointer += 6;
4328 sparc_cons_special_reloc = "plt";
4329 }
4330 }
4331 else if (strncmp (input_line_pointer + 3, "tls_dtpoff", 10) == 0)
4332 {
4333 if (size != 4 && size != 8)
4334 as_bad (_("Illegal operands: %%r_tls_dtpoff in %d-byte data field"), size);
4335 else
4336 {
4337 input_line_pointer += 13;
4338 sparc_cons_special_reloc = "tls_dtpoff";
4339 }
4340 }
4341 if (sparc_cons_special_reloc)
4342 {
4343 int bad = 0;
4344
4345 switch (size)
4346 {
4347 case 1:
4348 if (*input_line_pointer != '8')
4349 bad = 1;
4350 input_line_pointer--;
4351 break;
4352 case 2:
4353 if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
4354 bad = 1;
4355 break;
4356 case 4:
4357 if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
4358 bad = 1;
4359 break;
4360 case 8:
4361 if (input_line_pointer[0] != '6' || input_line_pointer[1] != '4')
4362 bad = 1;
4363 break;
4364 default:
4365 bad = 1;
4366 break;
4367 }
4368
4369 if (bad)
4370 {
4371 as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
4372 sparc_cons_special_reloc, size * 8, size);
4373 }
4374 else
4375 {
4376 input_line_pointer += 2;
4377 if (*input_line_pointer != '(')
4378 {
4379 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4380 sparc_cons_special_reloc, size * 8);
4381 bad = 1;
4382 }
4383 }
4384
4385 if (bad)
4386 {
4387 input_line_pointer = save;
4388 sparc_cons_special_reloc = NULL;
4389 }
4390 else
4391 {
4392 int c;
4393 char *end = ++input_line_pointer;
4394 int npar = 0;
4395
4396 while (! is_end_of_line[(c = *end)])
4397 {
4398 if (c == '(')
4399 npar++;
4400 else if (c == ')')
4401 {
4402 if (!npar)
4403 break;
4404 npar--;
4405 }
4406 end++;
4407 }
4408
4409 if (c != ')')
4410 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4411 sparc_cons_special_reloc, size * 8);
4412 else
4413 {
4414 *end = '\0';
4415 expression (exp);
4416 *end = c;
4417 if (input_line_pointer != end)
4418 {
4419 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
4420 sparc_cons_special_reloc, size * 8);
4421 }
4422 else
4423 {
4424 input_line_pointer++;
4425 SKIP_WHITESPACE ();
4426 c = *input_line_pointer;
4427 if (! is_end_of_line[c] && c != ',')
4428 as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
4429 sparc_cons_special_reloc, size * 8);
4430 }
4431 }
4432 }
4433 }
4434 }
4435 if (sparc_cons_special_reloc == NULL)
4436 expression (exp);
4437}
4438
4439#endif
4440
4441/* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
4442 reloc for a cons. We could use the definition there, except that
4443 we want to handle little endian relocs specially. */
4444
4445void
4446cons_fix_new_sparc (frag, where, nbytes, exp)
4447 fragS *frag;
4448 int where;
4449 unsigned int nbytes;
4450 expressionS *exp;
4451{
4452 bfd_reloc_code_real_type r;
4453
4454 r = (nbytes == 1 ? BFD_RELOC_8 :
4455 (nbytes == 2 ? BFD_RELOC_16 :
4456 (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
4457
4458 if (target_little_endian_data
4459 && nbytes == 4
4460 && now_seg->flags & SEC_ALLOC)
4461 r = BFD_RELOC_SPARC_REV32;
4462
4463 if (sparc_cons_special_reloc)
4464 {
4465 if (*sparc_cons_special_reloc == 'd')
4466 switch (nbytes)
4467 {
4468 case 1: r = BFD_RELOC_8_PCREL; break;
4469 case 2: r = BFD_RELOC_16_PCREL; break;
4470 case 4: r = BFD_RELOC_32_PCREL; break;
4471 case 8: r = BFD_RELOC_64_PCREL; break;
4472 default: abort ();
4473 }
4474 else if (*sparc_cons_special_reloc == 'p')
4475 switch (nbytes)
4476 {
4477 case 4: r = BFD_RELOC_SPARC_PLT32; break;
4478 case 8: r = BFD_RELOC_SPARC_PLT64; break;
4479 }
4480 else
4481 switch (nbytes)
4482 {
4483 case 4: r = BFD_RELOC_SPARC_TLS_DTPOFF32; break;
4484 case 8: r = BFD_RELOC_SPARC_TLS_DTPOFF64; break;
4485 }
4486 }
4487 else if (sparc_no_align_cons)
4488 {
4489 switch (nbytes)
4490 {
4491 case 2: r = BFD_RELOC_SPARC_UA16; break;
4492 case 4: r = BFD_RELOC_SPARC_UA32; break;
4493 case 8: r = BFD_RELOC_SPARC_UA64; break;
4494 default: abort ();
4495 }
4496 }
4497
4498 fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
4499}
Note: See TracBrowser for help on using the repository browser.