1 /* This is the machine dependent code of the Visium Assembler.
3 Copyright (C) 2005-2024 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 #include "safe-ctype.h"
27 #include "opcode/visium.h"
28 #include "elf/visium.h"
29 #include "dwarf2dbg.h"
30 #include "dw2gencfi.h"
32 /* Relocations and fixups:
34 There are two different cases where an instruction or data
35 directive operand requires relocation, or fixup.
37 1. Relative branch instructions, take an 16-bit signed word
38 offset. The formula for computing the offset is this:
40 offset = (destination - pc) / 4
42 Branch instructions never branch to a label not declared
43 locally, so the actual offset can always be computed by the assembler.
44 However, we provide a relocation type to support this.
46 2. Load literal instructions, such as MOVIU, which take a 16-bit
47 literal operand. The literal may be the top or bottom half of
48 a 32-bit value computed by the assembler, or by the linker. We provide
49 two relocation types here.
51 3. Data items (long, word and byte) preset with a value computed by
55 /* This string holds the chars that always start a comment. If the
56 pre-processor is disabled, these aren't very useful. */
57 const char comment_chars
[] = "!;";
59 /* This array holds the chars that only start a comment at the beginning
60 of a line. If the line seems to have the form '# 123 filename' .line
61 and .file directives will appear in the pre-processed output. Note that
62 input_file.c hand checks for '#' at the beginning of the first line of
63 the input file. This is because the compiler outputs #NO_APP at the
64 beginning of its output. Also note that comments like this one will
66 const char line_comment_chars
[] = "#!;";
67 const char line_separator_chars
[] = "";
69 /* Chars that can be used to separate mantissa from exponent in floating point
71 const char EXP_CHARS
[] = "eE";
73 /* Chars that mean this number is a floating point constant, as in
74 "0f12.456" or "0d1.2345e12".
76 ...Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
77 changed in read.c. Ideally it shouldn't have to know about it at all,
78 but nothing is ideal around here. */
79 const char FLT_CHARS
[] = "rRsSfFdDxXeE";
81 /* The size of a relocation record. */
82 const int md_reloc_size
= 8;
84 /* The architecture for which we are assembling. */
93 static enum visium_arch_val visium_arch
= VISIUM_ARCH_DEF
;
95 /* The opcode architecture for which we are assembling. In contrast to the
96 previous one, this only determines which instructions are supported. */
97 static enum visium_opcode_arch_val visium_opcode_arch
= VISIUM_OPCODE_ARCH_DEF
;
99 /* Flags to set in the ELF header e_flags field. */
100 static flagword visium_flags
= 0;
102 /* More than this number of nops in an alignment op gets a branch instead. */
103 static unsigned int nop_limit
= 5;
106 /* Translate internal representation of relocation info to BFD target
109 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
112 bfd_reloc_code_real_type code
;
114 reloc
= XNEW (arelent
);
116 reloc
->sym_ptr_ptr
= XNEW (asymbol
*);
117 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
118 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
120 switch (fixp
->fx_r_type
)
125 case BFD_RELOC_8_PCREL
:
126 case BFD_RELOC_16_PCREL
:
127 case BFD_RELOC_32_PCREL
:
128 case BFD_RELOC_VISIUM_HI16
:
129 case BFD_RELOC_VISIUM_LO16
:
130 case BFD_RELOC_VISIUM_IM16
:
131 case BFD_RELOC_VISIUM_REL16
:
132 case BFD_RELOC_VISIUM_HI16_PCREL
:
133 case BFD_RELOC_VISIUM_LO16_PCREL
:
134 case BFD_RELOC_VISIUM_IM16_PCREL
:
135 case BFD_RELOC_VTABLE_INHERIT
:
136 case BFD_RELOC_VTABLE_ENTRY
:
137 code
= fixp
->fx_r_type
;
140 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
141 "internal error: unknown relocation type %d (`%s')",
143 bfd_get_reloc_code_name (fixp
->fx_r_type
));
147 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, code
);
148 if (reloc
->howto
== 0)
150 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
151 "internal error: can't export reloc type %d (`%s')",
152 fixp
->fx_r_type
, bfd_get_reloc_code_name (code
));
156 /* Write the addend. */
157 if (reloc
->howto
->pc_relative
== 0)
158 reloc
->addend
= fixp
->fx_addnumber
;
160 reloc
->addend
= fixp
->fx_offset
;
165 static void visium_rdata (int);
167 static void visium_update_parity_bit (char *);
168 static char *parse_exp (char *, expressionS
*);
170 /* This table describes all the machine specific pseudo-ops the assembler
171 has to support, and that aren't handled elsewhere. The fields are:
173 1: Pseudo-op name without dot.
174 2: Function to call to execute this pseudo-op.
175 3: Integer arg to pass to the function. */
176 const pseudo_typeS md_pseudo_table
[] =
178 {"align", s_align_bytes
, 0},
179 {"noopt", s_ignore
, 0},
180 {"optim", s_ignore
, 0},
181 {"rdata", visium_rdata
, 0},
182 {"rodata", visium_rdata
, 0},
188 visium_rdata (int xxx
)
190 char *save_line
= input_line_pointer
;
191 static char section
[] = ".rodata\n";
193 /* Just pretend this is .section .rodata */
194 input_line_pointer
= section
;
195 obj_elf_section (xxx
);
196 input_line_pointer
= save_line
;
199 /* Align a section. */
201 md_section_align (asection
*seg
, valueT addr
)
203 int align
= bfd_section_alignment (seg
);
205 return ((addr
+ (1 << align
) - 1) & -(1 << align
));
209 md_number_to_chars (char *buf
, valueT val
, int n
)
211 number_to_chars_bigendian (buf
, val
, n
);
215 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
220 /* The parse options. */
221 const char *md_shortopts
= "m:";
223 struct option md_longopts
[] =
225 {NULL
, no_argument
, NULL
, 0}
228 size_t md_longopts_size
= sizeof (md_longopts
);
230 struct visium_option_table
232 char *option
; /* Option name to match. */
233 char *help
; /* Help information. */
234 int *var
; /* Variable to change. */
235 int value
; /* To what to change it. */
236 char *deprecated
; /* If non-null, print this message. */
239 static struct visium_option_table visium_opts
[] =
241 {NULL
, NULL
, NULL
, 0, NULL
}
244 struct visium_arch_option_table
247 enum visium_arch_val value
;
250 static struct visium_arch_option_table visium_archs
[] =
252 {"mcm24", VISIUM_ARCH_MCM24
},
253 {"mcm", VISIUM_ARCH_MCM
},
254 {"gr5", VISIUM_ARCH_MCM
},
255 {"gr6", VISIUM_ARCH_GR6
},
258 struct visium_long_option_table
260 const char *option
; /* Substring to match. */
261 const char *help
; /* Help information. */
262 int (*func
) (const char *subopt
); /* Function to decode sub-option. */
263 const char *deprecated
; /* If non-null, print this message. */
267 visium_parse_arch (const char *str
)
271 if (strlen (str
) == 0)
273 as_bad ("missing architecture name `%s'", str
);
277 for (i
= 0; i
< ARRAY_SIZE (visium_archs
); i
++)
278 if (strcmp (visium_archs
[i
].name
, str
) == 0)
280 visium_arch
= visium_archs
[i
].value
;
284 as_bad ("unknown architecture `%s'\n", str
);
288 static struct visium_long_option_table visium_long_opts
[] =
290 {"mtune=", "<arch_name>\t assemble for architecture <arch name>",
291 visium_parse_arch
, NULL
},
292 {NULL
, NULL
, NULL
, NULL
}
296 md_parse_option (int c
, const char *arg
)
298 struct visium_option_table
*opt
;
299 struct visium_long_option_table
*lopt
;
304 /* Listing option. Just ignore these, we don't support additional
309 for (opt
= visium_opts
; opt
->option
!= NULL
; opt
++)
311 if (c
== opt
->option
[0]
312 && ((arg
== NULL
&& opt
->option
[1] == 0)
313 || strcmp (arg
, opt
->option
+ 1) == 0))
315 /* If the option is deprecated, tell the user. */
316 if (opt
->deprecated
!= NULL
)
317 as_tsktsk ("option `-%c%s' is deprecated: %s", c
,
318 arg
? arg
: "", opt
->deprecated
);
320 if (opt
->var
!= NULL
)
321 *opt
->var
= opt
->value
;
327 for (lopt
= visium_long_opts
; lopt
->option
!= NULL
; lopt
++)
329 /* These options are expected to have an argument. */
330 if (c
== lopt
->option
[0]
332 && strncmp (arg
, lopt
->option
+ 1,
333 strlen (lopt
->option
+ 1)) == 0)
335 /* If the option is deprecated, tell the user. */
336 if (lopt
->deprecated
!= NULL
)
337 as_tsktsk ("option `-%c%s' is deprecated: %s", c
, arg
,
340 /* Call the sup-option parser. */
341 return lopt
->func (arg
+ strlen (lopt
->option
) - 1);
352 md_show_usage (FILE * fp
)
354 struct visium_option_table
*opt
;
355 struct visium_long_option_table
*lopt
;
357 fprintf (fp
, " Visium-specific assembler options:\n");
359 for (opt
= visium_opts
; opt
->option
!= NULL
; opt
++)
360 if (opt
->help
!= NULL
)
361 fprintf (fp
, " -%-23s%s\n", opt
->option
, opt
->help
);
363 for (lopt
= visium_long_opts
; lopt
->option
!= NULL
; lopt
++)
364 if (lopt
->help
!= NULL
)
365 fprintf (fp
, " -%s%s\n", lopt
->option
, lopt
->help
);
369 /* Interface to relax_segment. */
371 /* Return the estimate of the size of a machine dependent frag
372 before any relaxing is done. It may also create any necessary
375 md_estimate_size_before_relax (fragS
* fragP
,
376 segT segment ATTRIBUTE_UNUSED
)
382 /* Get the address of a symbol during relaxation. From tc-arm.c. */
384 relaxed_symbol_addr (fragS
*fragp
, long stretch
)
390 sym
= fragp
->fr_symbol
;
391 sym_frag
= symbol_get_frag (sym
);
392 know (S_GET_SEGMENT (sym
) != absolute_section
393 || sym_frag
== &zero_address_frag
);
394 addr
= S_GET_VALUE (sym
) + fragp
->fr_offset
;
396 /* If frag has yet to be reached on this pass, assume it will
397 move by STRETCH just as we did. If this is not so, it will
398 be because some frag between grows, and that will force
401 && sym_frag
->relax_marker
!= fragp
->relax_marker
)
405 /* Adjust stretch for any alignment frag. Note that if have
406 been expanding the earlier code, the symbol may be
407 defined in what appears to be an earlier frag. FIXME:
408 This doesn't handle the fr_subtype field, which specifies
409 a maximum number of bytes to skip when doing an
411 for (f
= fragp
; f
!= NULL
&& f
!= sym_frag
; f
= f
->fr_next
)
413 if (f
->fr_type
== rs_align
|| f
->fr_type
== rs_align_code
)
416 stretch
= - ((- stretch
)
417 & ~ ((1 << (int) f
->fr_offset
) - 1));
419 stretch
&= ~ ((1 << (int) f
->fr_offset
) - 1);
431 /* Relax a machine dependent frag. This returns the amount by which
432 the current size of the frag should change. */
434 visium_relax_frag (asection
*sec
, fragS
*fragP
, long stretch
)
436 int old_size
, new_size
;
439 /* We only handle relaxation for the BRR instruction. */
440 gas_assert (fragP
->fr_subtype
== mode_ci
);
442 if (!S_IS_DEFINED (fragP
->fr_symbol
)
443 || sec
!= S_GET_SEGMENT (fragP
->fr_symbol
)
444 || S_IS_WEAK (fragP
->fr_symbol
))
447 old_size
= fragP
->fr_var
;
448 addr
= relaxed_symbol_addr (fragP
, stretch
);
450 /* If the target is the address of the instruction, we'll insert a NOP. */
451 if (addr
== fragP
->fr_address
+ fragP
->fr_fix
)
456 fragP
->fr_var
= new_size
;
457 return new_size
- old_size
;
460 /* Convert a machine dependent frag. */
462 md_convert_frag (bfd
* abfd ATTRIBUTE_UNUSED
, segT sec ATTRIBUTE_UNUSED
,
465 char *buf
= &fragP
->fr_literal
[0] + fragP
->fr_fix
;
469 /* We only handle relaxation for the BRR instruction. */
470 gas_assert (fragP
->fr_subtype
== mode_ci
);
472 /* Insert the NOP if requested. */
473 if (fragP
->fr_var
== 8)
475 memcpy (buf
+ 4, buf
, 4);
481 exp
.X_add_symbol
= fragP
->fr_symbol
;
482 exp
.X_add_number
= fragP
->fr_offset
;
484 /* Now we can create the relocation at the correct offset. */
485 fixP
= fix_new_exp (fragP
, fragP
->fr_fix
, 4, &exp
, 1, BFD_RELOC_VISIUM_REL16
);
486 fixP
->fx_file
= fragP
->fr_file
;
487 fixP
->fx_line
= fragP
->fr_line
;
492 /* The location from which a PC relative jump should be calculated,
493 given a PC relative jump reloc. */
495 visium_pcrel_from_section (fixS
*fixP
, segT sec
)
497 if (fixP
->fx_addsy
!= (symbolS
*) NULL
498 && (!S_IS_DEFINED (fixP
->fx_addsy
)
499 || S_GET_SEGMENT (fixP
->fx_addsy
) != sec
))
501 /* The symbol is undefined (or is defined but not in this section).
502 Let the linker figure it out. */
506 /* Return the address of the instruction. */
507 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
510 /* Indicate whether a fixup against a locally defined
511 symbol should be adjusted to be against the section
514 visium_fix_adjustable (fixS
*fix
)
516 /* We need the symbol name for the VTABLE entries. */
517 return (fix
->fx_r_type
!= BFD_RELOC_VTABLE_INHERIT
518 && fix
->fx_r_type
!= BFD_RELOC_VTABLE_ENTRY
);
521 /* Update the parity bit of the 4-byte instruction in BUF. */
523 visium_update_parity_bit (char *buf
)
525 int p1
= (buf
[0] & 0x7f) ^ buf
[1] ^ buf
[2] ^ buf
[3];
529 for (i
= 1; i
<= 8; i
++)
535 buf
[0] = (buf
[0] & 0x7f) | ((p2
<< 7) & 0x80);
538 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
539 of an rs_align_code fragment. */
541 visium_handle_align (fragS
*fragP
)
544 = fragP
->fr_next
->fr_address
- (fragP
->fr_address
+ fragP
->fr_fix
);
545 valueT fix
= count
& 3;
546 char *p
= fragP
->fr_literal
+ fragP
->fr_fix
;
553 fragP
->fr_fix
+= fix
;
561 if (count
> 4 * nop_limit
&& count
<= 131068)
565 /* Make a branch, then follow with nops. Insert another
566 frag to handle the nops. */
567 md_number_to_chars (p
, 0x78000000 + (count
>> 2), 4);
568 visium_update_parity_bit (p
);
570 rest
= xmalloc (SIZEOF_STRUCT_FRAG
+ 4);
571 memcpy (rest
, fragP
, SIZEOF_STRUCT_FRAG
);
572 fragP
->fr_next
= rest
;
573 rest
->fr_address
+= rest
->fr_fix
+ 4;
575 /* If we leave the next frag as rs_align_code we'll come here
576 again, resulting in a bunch of branches rather than a
577 branch followed by nops. */
578 rest
->fr_type
= rs_align
;
579 p
= rest
->fr_literal
;
585 /* Apply a fixS to the frags, now that we know the value it ought to
588 md_apply_fix (fixS
* fixP
, valueT
* value
, segT segment
)
590 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
596 gas_assert (fixP
->fx_r_type
< BFD_RELOC_UNUSED
);
598 /* Remember value for tc_gen_reloc. */
599 fixP
->fx_addnumber
= val
;
601 /* Since DIFF_EXPR_OK is defined, .-foo gets turned into PC
602 relative relocs. If this has happened, a non-PC relative
603 reloc must be reinstalled with its PC relative version here. */
606 switch (fixP
->fx_r_type
)
609 fixP
->fx_r_type
= BFD_RELOC_8_PCREL
;
612 fixP
->fx_r_type
= BFD_RELOC_16_PCREL
;
615 fixP
->fx_r_type
= BFD_RELOC_32_PCREL
;
617 case BFD_RELOC_VISIUM_HI16
:
618 fixP
->fx_r_type
= BFD_RELOC_VISIUM_HI16_PCREL
;
620 case BFD_RELOC_VISIUM_LO16
:
621 fixP
->fx_r_type
= BFD_RELOC_VISIUM_LO16_PCREL
;
623 case BFD_RELOC_VISIUM_IM16
:
624 fixP
->fx_r_type
= BFD_RELOC_VISIUM_IM16_PCREL
;
631 /* If this is a data relocation, just output VAL. */
632 switch (fixP
->fx_r_type
)
635 case BFD_RELOC_8_PCREL
:
636 md_number_to_chars (buf
, val
, 1);
639 case BFD_RELOC_16_PCREL
:
640 md_number_to_chars (buf
, val
, 2);
643 case BFD_RELOC_32_PCREL
:
644 md_number_to_chars (buf
, val
, 4);
646 case BFD_RELOC_VTABLE_INHERIT
:
647 case BFD_RELOC_VTABLE_ENTRY
:
651 /* It's a relocation against an instruction. */
652 insn
= bfd_getb32 ((unsigned char *) buf
);
654 switch (fixP
->fx_r_type
)
656 case BFD_RELOC_VISIUM_REL16
:
657 if (fixP
->fx_addsy
== NULL
658 || (S_IS_DEFINED (fixP
->fx_addsy
)
659 && S_GET_SEGMENT (fixP
->fx_addsy
) == segment
))
661 if (val
> 0x1fffc || val
< -0x20000)
663 (fixP
->fx_file
, fixP
->fx_line
,
664 "16-bit word displacement out of range: value = %d",
668 insn
= (insn
& 0xffff0000) | (val
& 0x0000ffff);
672 case BFD_RELOC_VISIUM_HI16
:
673 case BFD_RELOC_VISIUM_HI16_PCREL
:
674 if (fixP
->fx_addsy
== NULL
)
675 insn
= (insn
& 0xffff0000) | ((val
>> 16) & 0x0000ffff);
678 case BFD_RELOC_VISIUM_LO16
:
679 case BFD_RELOC_VISIUM_LO16_PCREL
:
680 if (fixP
->fx_addsy
== NULL
)
681 insn
= (insn
& 0xffff0000) | (val
& 0x0000ffff);
684 case BFD_RELOC_VISIUM_IM16
:
685 case BFD_RELOC_VISIUM_IM16_PCREL
:
686 if (fixP
->fx_addsy
== NULL
)
688 if ((val
& 0xffff0000) != 0)
689 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
690 "16-bit immediate out of range: value = %d",
693 insn
= (insn
& 0xffff0000) | val
;
699 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
700 "bad or unhandled relocation type: 0x%02x",
705 bfd_putb32 (insn
, (unsigned char *) buf
);
706 visium_update_parity_bit (buf
);
710 /* Are we finished with this relocation now? */
711 if (fixP
->fx_addsy
== NULL
)
716 parse_exp (char *s
, expressionS
* op
)
718 char *save
= input_line_pointer
;
726 input_line_pointer
= s
;
728 new = input_line_pointer
;
729 input_line_pointer
= save
;
733 /* If the given string is a Visium opcode mnemonic return the code
734 otherwise return -1. Use binary chop to find matching entry. */
736 get_opcode (int *code
, enum addressing_mode
*mode
, char *flags
, char *mnem
)
739 int r
= sizeof (opcode_table
) / sizeof (struct opcode_entry
) - 1;
743 int mid
= (l
+ r
) / 2;
744 int ans
= strcmp (mnem
, opcode_table
[mid
].mnem
);
752 *code
= opcode_table
[mid
].code
;
753 *mode
= opcode_table
[mid
].mode
;
754 *flags
= opcode_table
[mid
].flags
;
764 /* This function is called when the assembler starts up. It is called
765 after the options have been parsed and the output file has been
772 case VISIUM_ARCH_DEF
:
774 case VISIUM_ARCH_MCM24
:
775 visium_opcode_arch
= VISIUM_OPCODE_ARCH_GR5
;
776 visium_flags
|= EF_VISIUM_ARCH_MCM24
;
778 case VISIUM_ARCH_MCM
:
779 visium_opcode_arch
= VISIUM_OPCODE_ARCH_GR5
;
780 visium_flags
|= EF_VISIUM_ARCH_MCM
;
782 case VISIUM_ARCH_GR6
:
783 visium_opcode_arch
= VISIUM_OPCODE_ARCH_GR6
;
784 visium_flags
|= EF_VISIUM_ARCH_MCM
| EF_VISIUM_ARCH_GR6
;
791 bfd_set_private_flags (stdoutput
, visium_flags
);
794 /* This is identical to the md_atof in m68k.c. I think this is right,
797 Turn a string in input_line_pointer into a floating point constant of type
798 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
799 emitted is stored in *sizeP . An error message is returned,
803 md_atof (int type
, char *litP
, int *sizeP
)
806 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
837 return _("Bad call to MD_ATOF()");
840 t
= atof_ieee (input_line_pointer
, type
, words
);
842 input_line_pointer
= t
;
843 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
845 if (target_big_endian
)
847 for (i
= 0; i
< prec
; i
++)
849 md_number_to_chars (litP
, (valueT
) words
[i
],
850 sizeof (LITTLENUM_TYPE
));
851 litP
+= sizeof (LITTLENUM_TYPE
);
856 for (i
= prec
- 1; i
>= 0; i
--)
858 md_number_to_chars (litP
, (valueT
) words
[i
],
859 sizeof (LITTLENUM_TYPE
));
860 litP
+= sizeof (LITTLENUM_TYPE
);
870 while (*s
== ' ' || *s
== '\t')
877 parse_gen_reg (char **sptr
, int *rptr
)
879 char *s
= skip_space (*sptr
);
885 memset (buf
, '\0', 10);
886 while ((ISALNUM (*s
)) && cnt
< 10)
887 buf
[cnt
++] = TOLOWER (*s
++);
890 r
= sizeof (gen_reg_table
) / sizeof (struct reg_entry
) - 1;
894 int mid
= (l
+ r
) / 2;
895 int ans
= strcmp (buf
, gen_reg_table
[mid
].name
);
903 *rptr
= gen_reg_table
[mid
].code
;
914 parse_fp_reg (char **sptr
, int *rptr
)
916 char *s
= skip_space (*sptr
);
922 memset (buf
, '\0', 10);
923 while ((ISALNUM (*s
)) && cnt
< 10)
924 buf
[cnt
++] = TOLOWER (*s
++);
927 r
= sizeof (fp_reg_table
) / sizeof (struct reg_entry
) - 1;
931 int mid
= (l
+ r
) / 2;
932 int ans
= strcmp (buf
, fp_reg_table
[mid
].name
);
940 *rptr
= fp_reg_table
[mid
].code
;
951 parse_cc (char **sptr
, int *rptr
)
953 char *s
= skip_space (*sptr
);
959 memset (buf
, '\0', 10);
960 while ((ISALNUM (*s
)) && cnt
< 10)
961 buf
[cnt
++] = TOLOWER (*s
++);
964 r
= sizeof (cc_table
) / sizeof (struct cc_entry
) - 1;
968 int mid
= (l
+ r
) / 2;
969 int ans
= strcmp (buf
, cc_table
[mid
].name
);
977 *rptr
= cc_table
[mid
].code
;
987 /* Previous dest is the destination register number of the instruction
988 before the current one. */
989 static int previous_dest
= 0;
990 static int previous_mode
= 0;
991 static int condition_code
= 0;
992 static int this_dest
= 0;
993 static int this_mode
= 0;
996 /* This is the main function in this file. It takes a line of assembly language
997 source code and assembles it. Note, labels and pseudo ops have already
998 been removed, so too has leading white space. */
1000 md_assemble (char *str0
)
1006 enum addressing_mode amode
;
1012 relax_substateT relax
= 0;
1018 /* Initialize the expression. */
1021 /* Initialize destination register.
1022 If the instruction we just looked at is in the delay slot of an
1023 unconditional branch, then there is no index hazard. */
1024 if ((previous_mode
== mode_cad
|| previous_mode
== mode_ci
)
1025 && condition_code
== 15)
1028 previous_dest
= this_dest
;
1029 previous_mode
= this_mode
;
1032 /* Drop leading whitespace (probably not required). */
1036 /* Get opcode mnemonic and make sure it's in lower case. */
1038 memset (mnem
, '\0', 10);
1039 while ((ISALNUM (*str
) || *str
== '.' || *str
== '_') && cnt
< 10)
1040 mnem
[cnt
++] = TOLOWER (*str
++);
1042 /* Look up mnemonic in opcode table, and get the code,
1043 the instruction format, and the flags that indicate
1044 which family members support this mnemonic. */
1045 if (get_opcode (&opcode
, &amode
, &arch_flags
, mnem
) < 0)
1047 as_bad ("Unknown instruction mnemonic `%s'", mnem
);
1051 if ((VISIUM_OPCODE_ARCH_MASK (visium_opcode_arch
) & arch_flags
) == 0)
1053 as_bad ("Architecture mismatch on `%s'", mnem
);
1065 ans
= parse_gen_reg (&str
, &r1
);
1068 as_bad ("Dest register required");
1071 opcode
|= (r1
<< 10);
1078 ans
= parse_gen_reg (&str
, &r1
);
1081 as_bad ("SourceA register required");
1084 opcode
|= (r1
<< 16);
1088 /* register * register
1091 ans
= parse_gen_reg (&str
, &r1
);
1094 as_bad ("SourceA register required");
1097 str
= skip_space (str
);
1101 ans
= parse_gen_reg (&str
, &r2
);
1104 as_bad ("SourceB register required");
1107 opcode
|= (r1
<< 16) | (r2
<< 4);
1111 as_bad ("SourceB register required");
1117 /* register := register
1120 ans
= parse_gen_reg (&str
, &r1
);
1123 as_bad ("Dest register required");
1126 str
= skip_space (str
);
1130 ans
= parse_gen_reg (&str
, &r2
);
1133 as_bad ("SourceA register required");
1136 opcode
|= (r1
<< 10) | (r2
<< 16);
1140 as_bad ("SourceB register required");
1147 /* register := register * register
1150 ans
= parse_gen_reg (&str
, &r1
);
1153 as_bad ("Dest register required");
1156 str
= skip_space (str
);
1160 ans
= parse_gen_reg (&str
, &r2
);
1163 as_bad ("SourceA register required");
1166 str
= skip_space (str
);
1170 ans
= parse_gen_reg (&str
, &r3
);
1173 as_bad ("SourceB register required");
1177 /* Got three regs, assemble instruction. */
1178 opcode
|= (r1
<< 10) | (r2
<< 16) | (r3
<< 4);
1182 as_bad ("SourceA register required");
1188 as_bad ("Dest register required");
1195 /* 5-bit immediate * register * register
1198 str
= parse_exp (str
, &e1
);
1199 str
= skip_space (str
);
1200 if (e1
.X_op
!= O_absent
&& *str
== ',')
1202 int eam_op
= e1
.X_add_number
;
1204 str
= skip_space (str
+ 1);
1205 ans
= parse_gen_reg (&str
, &r2
);
1208 as_bad ("SourceA register required");
1211 str
= skip_space (str
);
1215 ans
= parse_gen_reg (&str
, &r3
);
1218 as_bad ("SourceB register required");
1222 /* Got three operands, assemble instruction. */
1223 if (eam_op
< 0 || eam_op
> 31)
1225 as_bad ("eam_op out of range");
1227 opcode
|= ((eam_op
& 0x1f) << 10) | (r2
<< 16) | (r3
<< 4);
1232 as_bad ("EAM_OP required");
1238 /* zero * register * register
1241 ans
= parse_gen_reg (&str
, &r1
);
1244 as_bad ("SourceA register required");
1247 str
= skip_space (str
);
1251 ans
= parse_gen_reg (&str
, &r2
);
1254 as_bad ("SourceB register required");
1257 opcode
|= (r1
<< 16) | (r2
<< 4);
1261 as_bad ("SourceB register required");
1267 /* register * register * zero
1270 ans
= parse_gen_reg (&str
, &r1
);
1273 as_bad ("Dest register required");
1276 str
= skip_space (str
);
1280 ans
= parse_gen_reg (&str
, &r2
);
1283 as_bad ("SourceA register required");
1286 opcode
|= (r1
<< 10) | (r2
<< 16);
1290 as_bad ("SourceA register required");
1297 /* condition * register * register
1300 ans
= parse_cc (&str
, &cc
);
1303 as_bad ("condition code required");
1307 str
= skip_space (str
);
1310 str
= skip_space (str
+ 1);
1311 ans
= parse_gen_reg (&str
, &r2
);
1314 as_bad ("SourceA register required");
1317 str
= skip_space (str
);
1321 ans
= parse_gen_reg (&str
, &r3
);
1324 as_bad ("Dest register required");
1328 /* Got three operands, assemble instruction. */
1329 opcode
|= (cc
<< 27) | (r2
<< 16) | (r3
<< 10);
1333 as_bad ("Dest register required");
1339 as_bad ("SourceA register required");
1343 if (previous_mode
== mode_cad
|| previous_mode
== mode_ci
)
1344 as_bad ("branch instruction in delay slot");
1346 /* For the GR6, BRA insns must be aligned on 64-bit boundaries. */
1347 if (visium_arch
== VISIUM_ARCH_GR6
)
1348 do_align (3, NULL
, 0, 0);
1351 condition_code
= cc
;
1355 /* register := register * 5-bit immediate/register shift count
1358 ans
= parse_gen_reg (&str
, &r1
);
1361 as_bad ("Dest register required");
1364 str
= skip_space (str
);
1368 ans
= parse_gen_reg (&str
, &r2
);
1371 as_bad ("SourceA register required");
1374 str
= skip_space (str
);
1378 ans
= parse_gen_reg (&str
, &r3
);
1381 opcode
|= (r1
<< 10) | (r2
<< 16) | (r3
<< 4);
1385 str
= parse_exp (str
, &e1
);
1386 if (e1
.X_op
== O_constant
)
1388 int imm
= e1
.X_add_number
;
1390 if (imm
< 0 || imm
> 31)
1391 as_bad ("immediate value out of range");
1394 (r1
<< 10) | (r2
<< 16) | (1 << 9) | ((imm
& 0x1f) <<
1399 as_bad ("immediate operand required");
1407 as_bad ("SourceA register required");
1414 /* register := 5-bit immediate
1417 ans
= parse_gen_reg (&str
, &r1
);
1420 as_bad ("Dest register required");
1423 str
= skip_space (str
);
1427 str
= parse_exp (str
, &e1
);
1428 if (e1
.X_op
== O_constant
)
1430 int opnd2
= e1
.X_add_number
;
1432 if (opnd2
< 0 || opnd2
> 31)
1434 as_bad ("immediate operand out of range");
1437 opcode
|= (r1
<< 10) | ((opnd2
& 0x1f) << 4);
1441 as_bad ("immediate operand required");
1447 as_bad ("immediate operand required");
1454 /* 5-bit immediate * register, e.g. trace 1,r1 */
1455 str
= parse_exp (str
, &e1
);
1456 str
= skip_space (str
);
1457 if (e1
.X_op
== O_constant
&& *str
== ',')
1459 int opnd1
= e1
.X_add_number
;
1461 str
= skip_space (str
+ 1);
1462 ans
= parse_gen_reg (&str
, &r2
);
1465 as_bad ("SourceA register required");
1469 /* Got two operands, assemble instruction. */
1470 if (opnd1
< 0 || opnd1
> 31)
1472 as_bad ("1st operand out of range");
1474 opcode
|= ((opnd1
& 0x1f) << 10) | (r2
<< 16);
1478 as_bad ("Immediate operand required");
1484 /* register *= 16-bit unsigned immediate
1487 ans
= parse_gen_reg (&str
, &r1
);
1490 as_bad ("Dest register required");
1493 opcode
|= (r1
<< 16);
1495 str
= skip_space (str
);
1498 as_bad ("immediate value missing");
1505 /* MOVIL/WRTL traditionally get an implicit "%l" applied
1506 to their immediate value. For other opcodes, unless
1507 the immediate value is decorated with "%u" or "%l"
1508 it must be in the range 0 .. 65535. */
1509 if ((opcode
& 0x7fe00000) == 0x04800000
1510 || (opcode
& 0x7fe00000) == 0x05000000)
1511 reloc
= BFD_RELOC_VISIUM_LO16
;
1513 reloc
= BFD_RELOC_VISIUM_IM16
;
1515 str
= skip_space (str
+ 1);
1520 reloc
= BFD_RELOC_VISIUM_HI16
;
1521 else if (str
[1] == 'l')
1522 reloc
= BFD_RELOC_VISIUM_LO16
;
1525 as_bad ("bad char after %%");
1531 str
= parse_exp (str
, &e1
);
1532 if (e1
.X_op
!= O_absent
)
1534 if (e1
.X_op
== O_constant
)
1536 int imm
= e1
.X_add_number
;
1538 if (reloc
== BFD_RELOC_VISIUM_HI16
)
1539 opcode
|= ((imm
>> 16) & 0xffff);
1540 else if (reloc
== BFD_RELOC_VISIUM_LO16
)
1541 opcode
|= (imm
& 0xffff);
1544 if (imm
< 0 || imm
> 0xffff)
1545 as_bad ("immediate value out of range");
1547 opcode
|= (imm
& 0xffff);
1549 /* No relocation is needed. */
1555 as_bad ("immediate value missing");
1561 /* register * register * 5-bit immediate,
1562 SourceB * SourceA * Index
1566 str
= skip_space (str
);
1571 str
= parse_exp (str
, &e1
);
1572 if (e1
.X_op
== O_constant
)
1574 indx
= e1
.X_add_number
;
1576 if (indx
< 0 || indx
> 31)
1578 as_bad ("Index out of range");
1584 as_bad ("Index(SourceA) required");
1589 str
= skip_space (str
);
1593 as_bad ("Index(SourceA) required");
1597 str
= skip_space (str
+ 1);
1599 ans
= parse_gen_reg (&str
, &r1
);
1602 as_bad ("SourceA register required");
1605 str
= skip_space (str
);
1608 as_bad ("(SourceA) required");
1611 str
= skip_space (str
+ 1);
1615 str
= skip_space (str
+ 1);
1616 ans
= parse_gen_reg (&str
, &r2
);
1619 as_bad ("SourceB register required");
1625 as_bad ("SourceB register required");
1629 opcode
|= (r1
<< 16) | (r2
<< 4) | ((indx
& 0x1f) << 10);
1631 if (indx
!= 0 && previous_mode
== mode_cad
)
1633 /* We're in a delay slot.
1634 If the base reg is the destination of the branch, then issue
1636 Otherwise it is safe to use the base and index. */
1637 if (previous_dest
!= 0 && r1
== previous_dest
)
1639 as_bad ("base register not ready");
1643 else if (previous_dest
!= 0
1644 && r1
== previous_dest
1645 && (visium_arch
== VISIUM_ARCH_MCM
1646 || visium_arch
== VISIUM_ARCH_MCM24
1647 || (visium_arch
== VISIUM_ARCH_DEF
&& indx
!= 0)))
1649 as_warn ("base register not ready, NOP inserted.");
1650 /* Insert a NOP before the write instruction. */
1651 output
= frag_more (4);
1652 memset (output
, 0, 4);
1657 /* register := register * 5-bit immediate
1661 ans
= parse_gen_reg (&str
, &r1
);
1664 as_bad ("Dest register required");
1667 str
= skip_space (str
);
1670 as_bad ("SourceA required");
1673 str
= skip_space (str
+ 1);
1678 str
= parse_exp (str
, &e1
);
1679 if (e1
.X_op
== O_constant
)
1681 indx
= e1
.X_add_number
;
1683 if (indx
< 0 || indx
> 31)
1685 as_bad ("Index out of range");
1691 as_bad ("Immediate 0 to 31 required");
1697 as_bad ("(SourceA) required");
1701 ans
= parse_gen_reg (&str
, &r2
);
1704 as_bad ("SourceA register required");
1707 str
= skip_space (str
);
1710 as_bad ("(SourceA) required");
1714 opcode
|= (r1
<< 10) | (r2
<< 16) | ((indx
& 0x1f) << 4);
1717 if (indx
!= 0 && previous_mode
== mode_cad
)
1719 /* We're in a delay slot.
1720 If the base reg is the destination of the branch, then issue
1722 Otherwise it is safe to use the base and index. */
1723 if (previous_dest
!= 0 && r2
== previous_dest
)
1725 as_bad ("base register not ready");
1729 else if (previous_dest
!= 0
1730 && r2
== previous_dest
1731 && (visium_arch
== VISIUM_ARCH_MCM
1732 || visium_arch
== VISIUM_ARCH_MCM24
1733 || (visium_arch
== VISIUM_ARCH_DEF
&& indx
!= 0)))
1735 as_warn ("base register not ready, NOP inserted.");
1736 /* Insert a NOP before the read instruction. */
1737 output
= frag_more (4);
1738 memset (output
, 0, 4);
1746 str
= skip_space (str
);
1750 /* condition * 16-bit signed word displacement
1753 ans
= parse_cc (&str
, &cc
);
1756 as_bad ("condition code required");
1759 opcode
|= (cc
<< 27);
1761 str
= skip_space (str
);
1764 str
= skip_space (str
+ 1);
1765 str
= parse_exp (str
, &e1
);
1766 if (e1
.X_op
!= O_absent
)
1768 if (e1
.X_op
== O_constant
)
1770 int imm
= e1
.X_add_number
;
1772 if (imm
< -32768 || imm
> 32767)
1773 as_bad ("immediate value out of range");
1775 /* The GR6 doesn't correctly handle a 0 displacement
1776 so we insert a NOP and change it to -1. */
1777 if (imm
== 0 && cc
!= 0 && visium_arch
== VISIUM_ARCH_GR6
)
1779 output
= frag_more (4);
1780 memset (output
, 0, 4);
1784 opcode
|= (imm
& 0xffff);
1786 else if (e1
.X_op
== O_symbol
)
1788 /* The GR6 doesn't correctly handle a 0 displacement
1789 so the instruction requires relaxation. */
1790 if (cc
!= 0 && visium_arch
== VISIUM_ARCH_GR6
)
1793 reloc
= BFD_RELOC_VISIUM_REL16
;
1797 as_bad ("immediate value missing");
1803 as_bad ("immediate value missing");
1809 as_bad ("immediate value missing");
1813 if (previous_mode
== mode_cad
|| previous_mode
== mode_ci
)
1814 as_bad ("branch instruction in delay slot");
1816 condition_code
= cc
;
1820 /* float := float * float
1823 ans
= parse_fp_reg (&str
, &r1
);
1826 as_bad ("floating point destination register required");
1829 str
= skip_space (str
);
1833 ans
= parse_fp_reg (&str
, &r2
);
1836 as_bad ("floating point SourceA register required");
1839 str
= skip_space (str
);
1843 ans
= parse_fp_reg (&str
, &r3
);
1846 as_bad ("floating point SourceB register required");
1850 /* Got 3 floating regs, assemble instruction. */
1851 opcode
|= (r1
<< 10) | (r2
<< 16) | (r3
<< 4);
1855 as_bad ("floating point SourceB register required");
1861 as_bad ("floating point SourceA register required");
1867 /* 4-bit immediate * float * float * float
1869 fpinst 10,f1,f2,f3 */
1870 str
= parse_exp (str
, &e1
);
1871 str
= skip_space (str
);
1872 if (e1
.X_op
!= O_absent
&& *str
== ',')
1874 int finst
= e1
.X_add_number
;
1876 str
= skip_space (str
+ 1);
1877 ans
= parse_fp_reg (&str
, &r1
);
1880 as_bad ("floating point destination register required");
1883 str
= skip_space (str
);
1887 ans
= parse_fp_reg (&str
, &r2
);
1890 as_bad ("floating point SourceA register required");
1893 str
= skip_space (str
);
1897 ans
= parse_fp_reg (&str
, &r3
);
1900 as_bad ("floating point SourceB register required");
1904 /* Got immediate and 3 floating regs,
1905 assemble instruction. */
1906 if (finst
< 0 || finst
> 15)
1907 as_bad ("finst out of range");
1910 ((finst
& 0xf) << 27) | (r1
<< 10) | (r2
<< 16) | (r3
<<
1915 as_bad ("floating point SourceB register required");
1921 as_bad ("floating point SourceA register required");
1927 as_bad ("finst missing");
1933 /* 4-bit immediate * register * float * float
1935 fpuread 4,r25,f2,f3 */
1936 str
= parse_exp (str
, &e1
);
1937 str
= skip_space (str
);
1938 if (e1
.X_op
!= O_absent
&& *str
== ',')
1940 int finst
= e1
.X_add_number
;
1942 str
= skip_space (str
+ 1);
1943 ans
= parse_gen_reg (&str
, &r1
);
1946 as_bad ("destination general register required");
1949 str
= skip_space (str
);
1953 ans
= parse_fp_reg (&str
, &r2
);
1956 as_bad ("floating point SourceA register required");
1959 str
= skip_space (str
);
1963 ans
= parse_fp_reg (&str
, &r3
);
1966 as_bad ("floating point SourceB register required");
1970 /* Got immediate and 3 floating regs,
1971 assemble instruction. */
1972 if (finst
< 0 || finst
> 15)
1973 as_bad ("finst out of range");
1976 ((finst
& 0xf) << 27) | (r1
<< 10) | (r2
<< 16) | (r3
<<
1981 as_bad ("floating point SourceB register required");
1987 as_bad ("floating point SourceA register required");
1993 as_bad ("finst missing");
2002 ans
= parse_fp_reg (&str
, &r1
);
2005 as_bad ("floating point destination register required");
2008 str
= skip_space (str
);
2012 ans
= parse_fp_reg (&str
, &r2
);
2015 as_bad ("floating point source register required");
2019 /* Got 2 floating regs, assemble instruction. */
2020 opcode
|= (r1
<< 10) | (r2
<< 16);
2024 as_bad ("floating point source register required");
2030 /* float := register
2033 ans
= parse_fp_reg (&str
, &r1
);
2036 as_bad ("floating point destination register required");
2039 str
= skip_space (str
);
2043 ans
= parse_gen_reg (&str
, &r2
);
2046 as_bad ("SourceA general register required");
2050 /* Got 2 regs, assemble instruction. */
2051 opcode
|= (r1
<< 10) | (r2
<< 16);
2055 as_bad ("SourceA general register required");
2061 /* register := float * float
2064 For the GR6, register must be r0 and can be omitted. */
2065 ans
= parse_gen_reg (&str
, &r1
);
2068 if (visium_opcode_arch
== VISIUM_OPCODE_ARCH_GR5
)
2070 as_bad ("Dest general register required");
2077 if (r1
!= 0 && visium_opcode_arch
!= VISIUM_OPCODE_ARCH_GR5
)
2079 as_bad ("FCMP/FCMPE can only use r0 as Dest register");
2083 str
= skip_space (str
);
2088 as_bad ("floating point SourceA register required");
2093 ans
= parse_fp_reg (&str
, &r2
);
2096 as_bad ("floating point SourceA register required");
2099 str
= skip_space (str
);
2103 ans
= parse_fp_reg (&str
, &r3
);
2106 as_bad ("floating point SourceB register required");
2110 /* Got 3 regs, assemble instruction. */
2111 opcode
|= (r1
<< 10) | (r2
<< 16) | (r3
<< 4);
2118 /* register := float
2121 ans
= parse_gen_reg (&str
, &r1
);
2124 as_bad ("Dest general register required");
2127 str
= skip_space (str
);
2131 ans
= parse_fp_reg (&str
, &r2
);
2134 as_bad ("floating point source register required");
2138 /* Got 2 regs, assemble instruction. */
2139 opcode
|= (r1
<< 10) | (r2
<< 16);
2143 as_bad ("floating point source register required");
2151 /* register register register, all sources and destinations
2155 ans
= parse_gen_reg (&str
, &r1
);
2158 as_bad ("destination address register required");
2161 str
= skip_space (str
);
2165 ans
= parse_gen_reg (&str
, &r2
);
2168 as_bad ("source address register required");
2171 str
= skip_space (str
);
2175 ans
= parse_gen_reg (&str
, &r3
);
2178 as_bad ("count register required");
2182 /* We insist on three registers but the opcode can only use
2184 if (r1
!= 1 || r2
!= 2 || r3
!= 3)
2186 as_bad ("BMI/BMD can only use format op r1,r2,r3");
2190 /* Opcode is unmodified by what comes out of the table. */
2194 as_bad ("register required");
2200 as_bad ("register required");
2212 output
= frag_var (rs_machine_dependent
, 8, 4, relax
, e1
.X_add_symbol
,
2213 e1
.X_add_number
, NULL
);
2215 output
= frag_more (4);
2217 /* Build the 32-bit instruction in a host-endian-neutral fashion. */
2218 output
[0] = (opcode
>> 24) & 0xff;
2219 output
[1] = (opcode
>> 16) & 0xff;
2220 output
[2] = (opcode
>> 8) & 0xff;
2221 output
[3] = (opcode
>> 0) & 0xff;
2224 /* The size of the instruction is unknown, so tie the debug info to the
2225 start of the instruction. */
2226 dwarf2_emit_insn (0);
2230 fix_new_exp (frag_now
, output
- frag_now
->fr_literal
, 4, &e1
,
2231 reloc
== BFD_RELOC_VISIUM_REL16
, reloc
);
2233 visium_update_parity_bit (output
);
2235 dwarf2_emit_insn (4);
2239 as_bad ("junk after instruction");
2243 visium_cfi_frame_initial_instructions (void)
2245 /* The CFA is in SP on function entry. */
2246 cfi_add_CFA_def_cfa (23, 0);
2250 visium_regname_to_dw2regnum (char *regname
)
2255 if (regname
[0] == 'f' && regname
[1] == 'p' && !regname
[2])
2258 if (regname
[0] == 's' && regname
[1] == 'p' && !regname
[2])
2261 if (regname
[0] == 'm' && regname
[1] == 'd' && !regname
[3])
2264 case 'b': return 32;
2265 case 'a': return 33;
2266 case 'c': return 34;
2267 default : return -1;
2270 if (regname
[0] == 'f' || regname
[0] == 'r')
2273 unsigned int regnum
= strtoul (regname
+ 1, &p
, 10);
2276 if (regnum
>= (regname
[0] == 'f' ? 16 : 32))
2278 if (regname
[0] == 'f')