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