Update release-README after completing the 2.43 release.
[binutils-gdb.git] / gas / config / tc-or1k.c
blob14b09c9c3e6dff14fbfb61b20e986c4a3c23d76f
1 /* tc-or1k.c -- Assembler for the OpenRISC family.
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
3 Contributed for OR32 by Johan Rydberg, jrydberg@opencores.org
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 3, or (at your option)
10 any later version.
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, see <http://www.gnu.org/licenses/> */
19 #include "as.h"
20 #include "safe-ctype.h"
21 #include "subsegs.h"
22 #include "symcat.h"
23 #include "opcodes/or1k-desc.h"
24 #include "opcodes/or1k-opc.h"
25 #include "cgen.h"
26 #include "elf/or1k.h"
27 #include "dw2gencfi.h"
29 /* Structure to hold all of the different components describing
30 an individual instruction. */
32 typedef struct
34 const CGEN_INSN * insn;
35 const CGEN_INSN * orig_insn;
36 CGEN_FIELDS fields;
37 #if CGEN_INT_INSN_P
38 CGEN_INSN_INT buffer [1];
39 #define INSN_VALUE(buf) (*(buf))
40 #else
41 unsigned char buffer [CGEN_MAX_INSN_SIZE];
42 #define INSN_VALUE(buf) (buf)
43 #endif
44 char * addr;
45 fragS * frag;
46 int num_fixups;
47 fixS * fixups [GAS_CGEN_MAX_FIXUPS];
48 int indices [MAX_OPERAND_INSTANCES];
50 or1k_insn;
52 const char comment_chars[] = ";#";
53 const char line_comment_chars[] = "#";
54 const char line_separator_chars[] = ";";
55 const char EXP_CHARS[] = "eE";
56 const char FLT_CHARS[] = "dD";
58 #define OR1K_SHORTOPTS "m:"
59 const char * md_shortopts = OR1K_SHORTOPTS;
61 struct option md_longopts[] =
63 {NULL, no_argument, NULL, 0}
65 size_t md_longopts_size = sizeof (md_longopts);
67 unsigned long or1k_machine = 0; /* default */
69 int
70 md_parse_option (int c ATTRIBUTE_UNUSED, const char * arg ATTRIBUTE_UNUSED)
72 return 0;
75 void
76 md_show_usage (FILE * stream ATTRIBUTE_UNUSED)
80 static void
81 ignore_pseudo (int val ATTRIBUTE_UNUSED)
83 discard_rest_of_line ();
86 static bool nodelay = false;
87 static void
88 s_nodelay (int val ATTRIBUTE_UNUSED)
90 nodelay = true;
93 /* The target specific pseudo-ops which we support. */
94 const pseudo_typeS md_pseudo_table[] =
96 { "align", s_align_bytes, 0 },
97 { "word", cons, 4 },
98 { "proc", ignore_pseudo, 0 },
99 { "endproc", ignore_pseudo, 0 },
100 { "nodelay", s_nodelay, 0 },
101 { NULL, NULL, 0 }
105 void
106 md_begin (void)
108 /* Initialize the `cgen' interface. */
110 /* Set the machine number and endian. */
111 gas_cgen_cpu_desc = or1k_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
112 CGEN_CPU_OPEN_ENDIAN,
113 CGEN_ENDIAN_BIG,
114 CGEN_CPU_OPEN_END);
115 or1k_cgen_init_asm (gas_cgen_cpu_desc);
117 /* This is a callback from cgen to gas to parse operands. */
118 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
121 void
122 md_assemble (char * str)
124 static int last_insn_had_delay_slot = 0;
125 or1k_insn insn;
126 char * errmsg;
128 /* Initialize GAS's cgen interface for a new instruction. */
129 gas_cgen_init_parse ();
131 insn.insn = or1k_cgen_assemble_insn
132 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
134 if (!insn.insn)
136 as_bad ("%s", errmsg);
137 return;
140 /* Doesn't really matter what we pass for RELAX_P here. */
141 gas_cgen_finish_insn (insn.insn, insn.buffer,
142 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
144 last_insn_had_delay_slot
145 = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
146 (void) last_insn_had_delay_slot;
150 /* The syntax in the manual says constants begin with '#'.
151 We just ignore it. */
153 void
154 md_operand (expressionS * expressionP)
156 if (* input_line_pointer == '#')
158 input_line_pointer ++;
159 expression (expressionP);
163 valueT
164 md_section_align (segT segment, valueT size)
166 int align = bfd_section_alignment (segment);
167 return ((size + (1 << align) - 1) & -(1 << align));
170 symbolS *
171 md_undefined_symbol (char * name ATTRIBUTE_UNUSED)
173 return 0;
177 /* Interface to relax_segment. */
179 const relax_typeS md_relax_table[] =
181 /* The fields are:
182 1) most positive reach of this state,
183 2) most negative reach of this state,
184 3) how many bytes this mode will add to the size of the current frag
185 4) which index into the table to try if we can't fit into this one. */
187 /* The first entry must be unused because an `rlx_more' value of zero ends
188 each list. */
189 {1, 1, 0, 0},
191 /* The displacement used by GAS is from the end of the 4 byte insn,
192 so we subtract 4 from the following. */
193 {(((1 << 25) - 1) << 2) - 4, -(1 << 25) - 4, 0, 0},
197 md_estimate_size_before_relax (fragS * fragP, segT segment ATTRIBUTE_UNUSED)
199 return md_relax_table[fragP->fr_subtype].rlx_length;
202 /* *fragP has been relaxed to its final size, and now needs to have
203 the bytes inside it modified to conform to the new size.
205 Called after relaxation is finished.
206 fragP->fr_type == rs_machine_dependent.
207 fragP->fr_subtype is the subtype of what the address relaxed to. */
209 void
210 md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED,
211 segT sec ATTRIBUTE_UNUSED,
212 fragS * fragP ATTRIBUTE_UNUSED)
214 /* FIXME */
218 /* Functions concerning relocs. */
220 /* The location from which a PC relative jump should be calculated,
221 given a PC relative reloc. */
223 long
224 md_pcrel_from_section (fixS * fixP, segT sec)
226 if (fixP->fx_addsy != (symbolS *) NULL
227 && (! S_IS_DEFINED (fixP->fx_addsy)
228 || (S_GET_SEGMENT (fixP->fx_addsy) != sec)
229 || S_IS_EXTERNAL (fixP->fx_addsy)
230 || S_IS_WEAK (fixP->fx_addsy)))
232 /* The symbol is undefined (or is defined but not in this section).
233 Let the linker figure it out. */
234 return 0;
237 return fixP->fx_frag->fr_address + fixP->fx_where;
241 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
242 Returns BFD_RELOC_NONE if no reloc type can be found.
243 *FIXP may be modified if desired. */
245 bfd_reloc_code_real_type
246 md_cgen_lookup_reloc (const CGEN_INSN * insn ATTRIBUTE_UNUSED,
247 const CGEN_OPERAND * operand,
248 fixS * fixP)
250 if (fixP->fx_cgen.opinfo)
251 return fixP->fx_cgen.opinfo;
253 switch (operand->type)
255 case OR1K_OPERAND_DISP26:
256 fixP->fx_pcrel = 1;
257 return BFD_RELOC_OR1K_REL_26;
259 default: /* avoid -Wall warning */
260 return BFD_RELOC_NONE;
264 /* Write a value out to the object file, using the appropriate endianness. */
266 void
267 md_number_to_chars (char * buf, valueT val, int n)
269 number_to_chars_bigendian (buf, val, n);
272 /* Turn a string in input_line_pointer into a floating point constant of type
273 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
274 emitted is stored in *sizeP . An error message is returned, or NULL on OK. */
276 const char *
277 md_atof (int type, char * litP, int * sizeP)
279 return ieee_md_atof (type, litP, sizeP, true);
282 bool
283 or1k_fix_adjustable (fixS * fixP)
285 /* We need the symbol name for the VTABLE entries. */
286 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
287 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
288 return false;
290 return true;
293 #define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
295 arelent *
296 tc_gen_reloc (asection * section, fixS * fixp)
298 arelent *reloc;
299 bfd_reloc_code_real_type code;
301 reloc = XNEW (arelent);
303 reloc->sym_ptr_ptr = XNEW (asymbol *);
304 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
305 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
307 if (fixp->fx_pcrel)
309 if (section->use_rela_p)
310 fixp->fx_offset -= md_pcrel_from_section (fixp, section);
311 else
312 fixp->fx_offset = reloc->address;
314 reloc->addend = fixp->fx_offset;
316 code = fixp->fx_r_type;
317 switch (code)
319 case BFD_RELOC_16:
320 if (fixp->fx_pcrel)
321 code = BFD_RELOC_16_PCREL;
322 break;
324 case BFD_RELOC_32:
325 if (fixp->fx_pcrel)
326 code = BFD_RELOC_32_PCREL;
327 break;
329 case BFD_RELOC_64:
330 if (fixp->fx_pcrel)
331 code = BFD_RELOC_64_PCREL;
332 break;
334 default:
335 break;
338 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
339 if (reloc->howto == NULL)
341 as_bad_where (fixp->fx_file, fixp->fx_line,
343 ("cannot represent %s relocation in this object file format"),
344 bfd_get_reloc_code_name (code));
345 return NULL;
348 return reloc;
351 void
352 or1k_apply_fix (struct fix *f, valueT *t, segT s)
354 gas_cgen_md_apply_fix (f, t, s);
356 switch (f->fx_r_type)
358 case BFD_RELOC_OR1K_TLS_GD_HI16:
359 case BFD_RELOC_OR1K_TLS_GD_LO16:
360 case BFD_RELOC_OR1K_TLS_GD_PG21:
361 case BFD_RELOC_OR1K_TLS_GD_LO13:
362 case BFD_RELOC_OR1K_TLS_LDM_HI16:
363 case BFD_RELOC_OR1K_TLS_LDM_LO16:
364 case BFD_RELOC_OR1K_TLS_LDM_PG21:
365 case BFD_RELOC_OR1K_TLS_LDM_LO13:
366 case BFD_RELOC_OR1K_TLS_LDO_HI16:
367 case BFD_RELOC_OR1K_TLS_LDO_LO16:
368 case BFD_RELOC_OR1K_TLS_IE_HI16:
369 case BFD_RELOC_OR1K_TLS_IE_LO16:
370 case BFD_RELOC_OR1K_TLS_IE_PG21:
371 case BFD_RELOC_OR1K_TLS_IE_LO13:
372 case BFD_RELOC_OR1K_TLS_LE_HI16:
373 case BFD_RELOC_OR1K_TLS_LE_LO16:
374 S_SET_THREAD_LOCAL (f->fx_addsy);
375 break;
376 default:
377 break;
381 void
382 or1k_elf_final_processing (void)
384 if (nodelay)
385 elf_elfheader (stdoutput)->e_flags |= EF_OR1K_NODELAY;
388 /* Standard calling conventions leave the CFA at SP on entry. */
390 void
391 or1k_cfi_frame_initial_instructions (void)
393 cfi_add_CFA_def_cfa_register (1);