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