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