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