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