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