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