Commit | Line | Data |
---|---|---|
e23eba97 | 1 | /* tc-riscv.c -- RISC-V assembler |
2571583a | 2 | Copyright (C) 2011-2017 Free Software Foundation, Inc. |
e23eba97 NC |
3 | |
4 | Contributed by Andrew Waterman (andrew@sifive.com). | |
5 | Based on MIPS target. | |
6 | ||
7 | This file is part of GAS. | |
8 | ||
9 | GAS is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 3, or (at your option) | |
12 | any later version. | |
13 | ||
14 | GAS is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; see the file COPYING3. If not, | |
21 | see <http://www.gnu.org/licenses/>. */ | |
22 | ||
23 | #include "as.h" | |
24 | #include "config.h" | |
25 | #include "subsegs.h" | |
26 | #include "safe-ctype.h" | |
27 | ||
28 | #include "itbl-ops.h" | |
29 | #include "dwarf2dbg.h" | |
30 | #include "dw2gencfi.h" | |
1d65abb5 | 31 | #include "struc-symbol.h" |
e23eba97 NC |
32 | |
33 | #include "elf/riscv.h" | |
34 | #include "opcode/riscv.h" | |
35 | ||
36 | #include <stdint.h> | |
37 | ||
38 | /* Information about an instruction, including its format, operands | |
39 | and fixups. */ | |
40 | struct riscv_cl_insn | |
41 | { | |
42 | /* The opcode's entry in riscv_opcodes. */ | |
43 | const struct riscv_opcode *insn_mo; | |
44 | ||
45 | /* The encoded instruction bits. */ | |
46 | insn_t insn_opcode; | |
47 | ||
48 | /* The frag that contains the instruction. */ | |
49 | struct frag *frag; | |
50 | ||
51 | /* The offset into FRAG of the first instruction byte. */ | |
52 | long where; | |
53 | ||
54 | /* The relocs associated with the instruction, if any. */ | |
55 | fixS *fixp; | |
56 | }; | |
57 | ||
58 | #ifndef DEFAULT_ARCH | |
59 | #define DEFAULT_ARCH "riscv64" | |
60 | #endif | |
61 | ||
62 | static const char default_arch[] = DEFAULT_ARCH; | |
63 | ||
2922d21d AW |
64 | static unsigned xlen = 0; /* width of an x-register */ |
65 | static unsigned abi_xlen = 0; /* width of a pointer in the ABI */ | |
e23eba97 | 66 | |
2922d21d | 67 | #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw") |
e23eba97 NC |
68 | #define ADD32_INSN (xlen == 64 ? "addiw" : "addi") |
69 | ||
70 | static unsigned elf_flags = 0; | |
71 | ||
72 | /* This is the set of options which the .option pseudo-op may modify. */ | |
73 | ||
74 | struct riscv_set_options | |
75 | { | |
76 | int pic; /* Generate position-independent code. */ | |
77 | int rvc; /* Generate RVC code. */ | |
45f76423 | 78 | int relax; /* Emit relocs the linker is allowed to relax. */ |
e23eba97 NC |
79 | }; |
80 | ||
81 | static struct riscv_set_options riscv_opts = | |
82 | { | |
83 | 0, /* pic */ | |
84 | 0, /* rvc */ | |
45f76423 | 85 | 1, /* relax */ |
e23eba97 NC |
86 | }; |
87 | ||
88 | static void | |
89 | riscv_set_rvc (bfd_boolean rvc_value) | |
90 | { | |
91 | if (rvc_value) | |
92 | elf_flags |= EF_RISCV_RVC; | |
93 | ||
94 | riscv_opts.rvc = rvc_value; | |
95 | } | |
96 | ||
97 | struct riscv_subset | |
98 | { | |
99 | const char *name; | |
100 | ||
101 | struct riscv_subset *next; | |
102 | }; | |
103 | ||
104 | static struct riscv_subset *riscv_subsets; | |
105 | ||
106 | static bfd_boolean | |
107 | riscv_subset_supports (const char *feature) | |
108 | { | |
109 | struct riscv_subset *s; | |
110 | char *p; | |
111 | unsigned xlen_required = strtoul (feature, &p, 10); | |
112 | ||
113 | if (xlen_required && xlen != xlen_required) | |
114 | return FALSE; | |
115 | ||
116 | for (s = riscv_subsets; s != NULL; s = s->next) | |
117 | if (strcasecmp (s->name, p) == 0) | |
118 | return TRUE; | |
119 | ||
120 | return FALSE; | |
121 | } | |
122 | ||
123 | static void | |
124 | riscv_add_subset (const char *subset) | |
125 | { | |
126 | struct riscv_subset *s = xmalloc (sizeof *s); | |
127 | ||
128 | s->name = xstrdup (subset); | |
129 | s->next = riscv_subsets; | |
130 | riscv_subsets = s; | |
131 | } | |
132 | ||
2922d21d | 133 | /* Set which ISA and extensions are available. */ |
e23eba97 | 134 | |
e23eba97 | 135 | static void |
2922d21d | 136 | riscv_set_arch (const char *s) |
e23eba97 | 137 | { |
2922d21d | 138 | const char *all_subsets = "imafdc"; |
e23eba97 | 139 | const char *extension = NULL; |
2922d21d | 140 | const char *p = s; |
e23eba97 | 141 | |
2922d21d | 142 | if (strncmp (p, "rv32", 4) == 0) |
e23eba97 NC |
143 | { |
144 | xlen = 32; | |
145 | p += 4; | |
146 | } | |
2922d21d | 147 | else if (strncmp (p, "rv64", 4) == 0) |
e23eba97 NC |
148 | { |
149 | xlen = 64; | |
150 | p += 4; | |
151 | } | |
2922d21d AW |
152 | else |
153 | as_fatal ("-march=%s: ISA string must begin with rv32 or rv64", s); | |
e23eba97 | 154 | |
2922d21d | 155 | switch (*p) |
e23eba97 | 156 | { |
2922d21d | 157 | case 'i': |
e23eba97 NC |
158 | break; |
159 | ||
2922d21d | 160 | case 'g': |
e23eba97 | 161 | p++; |
2922d21d | 162 | for ( ; *all_subsets != 'c'; all_subsets++) |
e23eba97 | 163 | { |
2922d21d | 164 | const char subset[] = {*all_subsets, '\0'}; |
e23eba97 NC |
165 | riscv_add_subset (subset); |
166 | } | |
167 | break; | |
168 | ||
169 | default: | |
2922d21d | 170 | as_fatal ("-march=%s: first ISA subset must be `i' or `g'", s); |
e23eba97 NC |
171 | } |
172 | ||
173 | while (*p) | |
174 | { | |
2922d21d | 175 | if (*p == 'x') |
e23eba97 NC |
176 | { |
177 | char *subset = xstrdup (p), *q = subset; | |
178 | ||
179 | while (*++q != '\0' && *q != '_') | |
180 | ; | |
181 | *q = '\0'; | |
182 | ||
183 | if (extension) | |
2922d21d AW |
184 | as_fatal ("-march=%s: only one non-standard extension is supported" |
185 | " (found `%s' and `%s')", s, extension, subset); | |
e23eba97 NC |
186 | extension = subset; |
187 | riscv_add_subset (subset); | |
188 | p += strlen (subset); | |
189 | free (subset); | |
190 | } | |
191 | else if (*p == '_') | |
192 | p++; | |
193 | else if ((all_subsets = strchr (all_subsets, *p)) != NULL) | |
194 | { | |
195 | const char subset[] = {*p, 0}; | |
196 | riscv_add_subset (subset); | |
e23eba97 NC |
197 | all_subsets++; |
198 | p++; | |
199 | } | |
cc917fd9 KC |
200 | else if (*p == 'q') |
201 | { | |
202 | const char subset[] = {*p, 0}; | |
203 | riscv_add_subset (subset); | |
204 | p++; | |
205 | } | |
e23eba97 | 206 | else |
2922d21d | 207 | as_fatal ("-march=%s: unsupported ISA subset `%c'", s, *p); |
e23eba97 NC |
208 | } |
209 | } | |
210 | ||
211 | /* Handle of the OPCODE hash table. */ | |
212 | static struct hash_control *op_hash = NULL; | |
213 | ||
214 | /* This array holds the chars that always start a comment. If the | |
215 | pre-processor is disabled, these aren't very useful */ | |
216 | const char comment_chars[] = "#"; | |
217 | ||
218 | /* This array holds the chars that only start a comment at the beginning of | |
219 | a line. If the line seems to have the form '# 123 filename' | |
220 | .line and .file directives will appear in the pre-processed output */ | |
221 | /* Note that input_file.c hand checks for '#' at the beginning of the | |
222 | first line of the input file. This is because the compiler outputs | |
223 | #NO_APP at the beginning of its output. */ | |
224 | /* Also note that C style comments are always supported. */ | |
225 | const char line_comment_chars[] = "#"; | |
226 | ||
227 | /* This array holds machine specific line separator characters. */ | |
228 | const char line_separator_chars[] = ";"; | |
229 | ||
230 | /* Chars that can be used to separate mant from exp in floating point nums */ | |
231 | const char EXP_CHARS[] = "eE"; | |
232 | ||
233 | /* Chars that mean this number is a floating point constant */ | |
234 | /* As in 0f12.456 */ | |
235 | /* or 0d1.2345e12 */ | |
236 | const char FLT_CHARS[] = "rRsSfFdDxXpP"; | |
237 | ||
238 | /* Macros for encoding relaxation state for RVC branches and far jumps. */ | |
239 | #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \ | |
240 | ((relax_substateT) \ | |
241 | (0xc0000000 \ | |
242 | | ((uncond) ? 1 : 0) \ | |
243 | | ((rvc) ? 2 : 0) \ | |
244 | | ((length) << 2))) | |
245 | #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000) | |
246 | #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF) | |
247 | #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0) | |
248 | #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0) | |
249 | ||
250 | /* Is the given value a sign-extended 32-bit value? */ | |
251 | #define IS_SEXT_32BIT_NUM(x) \ | |
252 | (((x) &~ (offsetT) 0x7fffffff) == 0 \ | |
253 | || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff)) | |
254 | ||
255 | /* Is the given value a zero-extended 32-bit value? Or a negated one? */ | |
256 | #define IS_ZEXT_32BIT_NUM(x) \ | |
257 | (((x) &~ (offsetT) 0xffffffff) == 0 \ | |
258 | || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff)) | |
259 | ||
260 | /* Change INSN's opcode so that the operand given by FIELD has value VALUE. | |
261 | INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */ | |
262 | #define INSERT_OPERAND(FIELD, INSN, VALUE) \ | |
263 | INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD) | |
264 | ||
265 | /* Determine if an instruction matches an opcode. */ | |
266 | #define OPCODE_MATCHES(OPCODE, OP) \ | |
267 | (((OPCODE) & MASK_##OP) == MATCH_##OP) | |
268 | ||
269 | static char *expr_end; | |
270 | ||
271 | /* The default target format to use. */ | |
272 | ||
273 | const char * | |
274 | riscv_target_format (void) | |
275 | { | |
276 | return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv"; | |
277 | } | |
278 | ||
279 | /* Return the length of instruction INSN. */ | |
280 | ||
281 | static inline unsigned int | |
282 | insn_length (const struct riscv_cl_insn *insn) | |
283 | { | |
284 | return riscv_insn_length (insn->insn_opcode); | |
285 | } | |
286 | ||
287 | /* Initialise INSN from opcode entry MO. Leave its position unspecified. */ | |
288 | ||
289 | static void | |
290 | create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo) | |
291 | { | |
292 | insn->insn_mo = mo; | |
293 | insn->insn_opcode = mo->match; | |
294 | insn->frag = NULL; | |
295 | insn->where = 0; | |
296 | insn->fixp = NULL; | |
297 | } | |
298 | ||
299 | /* Install INSN at the location specified by its "frag" and "where" fields. */ | |
300 | ||
301 | static void | |
302 | install_insn (const struct riscv_cl_insn *insn) | |
303 | { | |
304 | char *f = insn->frag->fr_literal + insn->where; | |
305 | md_number_to_chars (f, insn->insn_opcode, insn_length (insn)); | |
306 | } | |
307 | ||
308 | /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly | |
309 | and install the opcode in the new location. */ | |
310 | ||
311 | static void | |
312 | move_insn (struct riscv_cl_insn *insn, fragS *frag, long where) | |
313 | { | |
314 | insn->frag = frag; | |
315 | insn->where = where; | |
316 | if (insn->fixp != NULL) | |
317 | { | |
318 | insn->fixp->fx_frag = frag; | |
319 | insn->fixp->fx_where = where; | |
320 | } | |
321 | install_insn (insn); | |
322 | } | |
323 | ||
324 | /* Add INSN to the end of the output. */ | |
325 | ||
326 | static void | |
327 | add_fixed_insn (struct riscv_cl_insn *insn) | |
328 | { | |
329 | char *f = frag_more (insn_length (insn)); | |
330 | move_insn (insn, frag_now, f - frag_now->fr_literal); | |
331 | } | |
332 | ||
333 | static void | |
334 | add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var, | |
335 | relax_substateT subtype, symbolS *symbol, offsetT offset) | |
336 | { | |
337 | frag_grow (max_chars); | |
338 | move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal); | |
339 | frag_var (rs_machine_dependent, max_chars, var, | |
340 | subtype, symbol, offset, NULL); | |
341 | } | |
342 | ||
343 | /* Compute the length of a branch sequence, and adjust the stored length | |
344 | accordingly. If FRAGP is NULL, the worst-case length is returned. */ | |
345 | ||
346 | static unsigned | |
347 | relaxed_branch_length (fragS *fragp, asection *sec, int update) | |
348 | { | |
349 | int jump, rvc, length = 8; | |
350 | ||
351 | if (!fragp) | |
352 | return length; | |
353 | ||
354 | jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype); | |
355 | rvc = RELAX_BRANCH_RVC (fragp->fr_subtype); | |
356 | length = RELAX_BRANCH_LENGTH (fragp->fr_subtype); | |
357 | ||
358 | /* Assume jumps are in range; the linker will catch any that aren't. */ | |
359 | length = jump ? 4 : 8; | |
360 | ||
361 | if (fragp->fr_symbol != NULL | |
362 | && S_IS_DEFINED (fragp->fr_symbol) | |
01156111 | 363 | && !S_IS_WEAK (fragp->fr_symbol) |
e23eba97 NC |
364 | && sec == S_GET_SEGMENT (fragp->fr_symbol)) |
365 | { | |
366 | offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset; | |
367 | bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH; | |
368 | val -= fragp->fr_address + fragp->fr_fix; | |
369 | ||
370 | if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range) | |
371 | length = 2; | |
372 | else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH) | |
373 | length = 4; | |
374 | else if (!jump && rvc) | |
375 | length = 6; | |
376 | } | |
377 | ||
378 | if (update) | |
379 | fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length); | |
380 | ||
381 | return length; | |
382 | } | |
383 | ||
384 | struct regname | |
385 | { | |
386 | const char *name; | |
387 | unsigned int num; | |
388 | }; | |
389 | ||
390 | enum reg_class | |
391 | { | |
392 | RCLASS_GPR, | |
393 | RCLASS_FPR, | |
394 | RCLASS_CSR, | |
395 | RCLASS_MAX | |
396 | }; | |
397 | ||
398 | static struct hash_control *reg_names_hash = NULL; | |
399 | ||
400 | #define ENCODE_REG_HASH(cls, n) \ | |
401 | ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1)) | |
402 | #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX) | |
403 | #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX) | |
404 | ||
405 | static void | |
406 | hash_reg_name (enum reg_class class, const char *name, unsigned n) | |
407 | { | |
408 | void *hash = ENCODE_REG_HASH (class, n); | |
409 | const char *retval = hash_insert (reg_names_hash, name, hash); | |
410 | ||
411 | if (retval != NULL) | |
412 | as_fatal (_("internal error: can't hash `%s': %s"), name, retval); | |
413 | } | |
414 | ||
415 | static void | |
416 | hash_reg_names (enum reg_class class, const char * const names[], unsigned n) | |
417 | { | |
418 | unsigned i; | |
419 | ||
420 | for (i = 0; i < n; i++) | |
421 | hash_reg_name (class, names[i], i); | |
422 | } | |
423 | ||
424 | static unsigned int | |
425 | reg_lookup_internal (const char *s, enum reg_class class) | |
426 | { | |
427 | struct regname *r = (struct regname *) hash_find (reg_names_hash, s); | |
428 | ||
429 | if (r == NULL || DECODE_REG_CLASS (r) != class) | |
430 | return -1; | |
431 | return DECODE_REG_NUM (r); | |
432 | } | |
433 | ||
434 | static bfd_boolean | |
435 | reg_lookup (char **s, enum reg_class class, unsigned int *regnop) | |
436 | { | |
437 | char *e; | |
438 | char save_c; | |
439 | int reg = -1; | |
440 | ||
441 | /* Find end of name. */ | |
442 | e = *s; | |
443 | if (is_name_beginner (*e)) | |
444 | ++e; | |
445 | while (is_part_of_name (*e)) | |
446 | ++e; | |
447 | ||
448 | /* Terminate name. */ | |
449 | save_c = *e; | |
450 | *e = '\0'; | |
451 | ||
452 | /* Look for the register. Advance to next token if one was recognized. */ | |
453 | if ((reg = reg_lookup_internal (*s, class)) >= 0) | |
454 | *s = e; | |
455 | ||
456 | *e = save_c; | |
457 | if (regnop) | |
458 | *regnop = reg; | |
459 | return reg >= 0; | |
460 | } | |
461 | ||
462 | static bfd_boolean | |
463 | arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop) | |
464 | { | |
465 | const char *p = strchr (*s, ','); | |
466 | size_t i, len = p ? (size_t)(p - *s) : strlen (*s); | |
467 | ||
468 | for (i = 0; i < size; i++) | |
469 | if (array[i] != NULL && strncmp (array[i], *s, len) == 0) | |
470 | { | |
471 | *regnop = i; | |
472 | *s += len; | |
473 | return TRUE; | |
474 | } | |
475 | ||
476 | return FALSE; | |
477 | } | |
478 | ||
479 | /* For consistency checking, verify that all bits are specified either | |
480 | by the match/mask part of the instruction definition, or by the | |
481 | operand list. */ | |
482 | static bfd_boolean | |
483 | validate_riscv_insn (const struct riscv_opcode *opc) | |
484 | { | |
485 | const char *p = opc->args; | |
486 | char c; | |
487 | insn_t used_bits = opc->mask; | |
488 | int insn_width = 8 * riscv_insn_length (opc->match); | |
489 | insn_t required_bits = ~0ULL >> (64 - insn_width); | |
490 | ||
491 | if ((used_bits & opc->match) != (opc->match & required_bits)) | |
492 | { | |
493 | as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"), | |
494 | opc->name, opc->args); | |
495 | return FALSE; | |
496 | } | |
497 | ||
498 | #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift))) | |
499 | while (*p) | |
500 | switch (c = *p++) | |
501 | { | |
502 | case 'C': /* RVC */ | |
503 | switch (c = *p++) | |
504 | { | |
1d65abb5 | 505 | case 'a': used_bits |= ENCODE_RVC_J_IMM (-1U); break; |
e23eba97 NC |
506 | case 'c': break; /* RS1, constrained to equal sp */ |
507 | case 'i': used_bits |= ENCODE_RVC_SIMM3(-1U); break; | |
1d65abb5 | 508 | case 'j': used_bits |= ENCODE_RVC_IMM (-1U); break; |
b416fe87 | 509 | case 'o': used_bits |= ENCODE_RVC_IMM (-1U); break; |
1d65abb5 AW |
510 | case 'k': used_bits |= ENCODE_RVC_LW_IMM (-1U); break; |
511 | case 'l': used_bits |= ENCODE_RVC_LD_IMM (-1U); break; | |
512 | case 'm': used_bits |= ENCODE_RVC_LWSP_IMM (-1U); break; | |
513 | case 'n': used_bits |= ENCODE_RVC_LDSP_IMM (-1U); break; | |
514 | case 'p': used_bits |= ENCODE_RVC_B_IMM (-1U); break; | |
e23eba97 NC |
515 | case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break; |
516 | case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break; | |
1d65abb5 AW |
517 | case 'u': used_bits |= ENCODE_RVC_IMM (-1U); break; |
518 | case 'v': used_bits |= ENCODE_RVC_IMM (-1U); break; | |
e23eba97 NC |
519 | case 'w': break; /* RS1S, constrained to equal RD */ |
520 | case 'x': break; /* RS2S, constrained to equal RD */ | |
1d65abb5 AW |
521 | case 'K': used_bits |= ENCODE_RVC_ADDI4SPN_IMM (-1U); break; |
522 | case 'L': used_bits |= ENCODE_RVC_ADDI16SP_IMM (-1U); break; | |
523 | case 'M': used_bits |= ENCODE_RVC_SWSP_IMM (-1U); break; | |
524 | case 'N': used_bits |= ENCODE_RVC_SDSP_IMM (-1U); break; | |
e23eba97 NC |
525 | case 'U': break; /* RS1, constrained to equal RD */ |
526 | case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break; | |
1d65abb5 AW |
527 | case '<': used_bits |= ENCODE_RVC_IMM (-1U); break; |
528 | case '>': used_bits |= ENCODE_RVC_IMM (-1U); break; | |
e23eba97 NC |
529 | case 'T': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break; |
530 | case 'D': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break; | |
531 | default: | |
532 | as_bad (_("internal: bad RISC-V opcode (unknown operand type `C%c'): %s %s"), | |
533 | c, opc->name, opc->args); | |
534 | return FALSE; | |
535 | } | |
536 | break; | |
537 | case ',': break; | |
538 | case '(': break; | |
539 | case ')': break; | |
540 | case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break; | |
541 | case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break; | |
542 | case 'A': break; | |
543 | case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD); break; | |
544 | case 'Z': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break; | |
545 | case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break; | |
546 | case 'I': break; | |
547 | case 'R': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break; | |
548 | case 'S': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break; | |
549 | case 'U': USE_BITS (OP_MASK_RS1, OP_SH_RS1); /* fallthru */ | |
550 | case 'T': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break; | |
551 | case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break; | |
552 | case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break; | |
553 | case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break; | |
554 | case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break; | |
555 | case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break; | |
556 | case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break; | |
557 | case 'o': | |
1d65abb5 AW |
558 | case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break; |
559 | case 'a': used_bits |= ENCODE_UJTYPE_IMM (-1U); break; | |
560 | case 'p': used_bits |= ENCODE_SBTYPE_IMM (-1U); break; | |
561 | case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break; | |
562 | case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break; | |
e23eba97 NC |
563 | case '[': break; |
564 | case ']': break; | |
565 | case '0': break; | |
566 | default: | |
567 | as_bad (_("internal: bad RISC-V opcode " | |
568 | "(unknown operand type `%c'): %s %s"), | |
569 | c, opc->name, opc->args); | |
570 | return FALSE; | |
571 | } | |
572 | #undef USE_BITS | |
573 | if (used_bits != required_bits) | |
574 | { | |
575 | as_bad (_("internal: bad RISC-V opcode (bits 0x%lx undefined): %s %s"), | |
576 | ~(unsigned long)(used_bits & required_bits), | |
577 | opc->name, opc->args); | |
578 | return FALSE; | |
579 | } | |
580 | return TRUE; | |
581 | } | |
582 | ||
583 | struct percent_op_match | |
584 | { | |
585 | const char *str; | |
586 | bfd_reloc_code_real_type reloc; | |
587 | }; | |
588 | ||
589 | /* This function is called once, at assembler startup time. It should set up | |
590 | all the tables, etc. that the MD part of the assembler will need. */ | |
591 | ||
592 | void | |
593 | md_begin (void) | |
594 | { | |
595 | int i = 0; | |
2922d21d | 596 | unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32; |
e23eba97 | 597 | |
2922d21d | 598 | if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach)) |
e23eba97 NC |
599 | as_warn (_("Could not set architecture and machine")); |
600 | ||
601 | op_hash = hash_new (); | |
602 | ||
603 | while (riscv_opcodes[i].name) | |
604 | { | |
605 | const char *name = riscv_opcodes[i].name; | |
606 | const char *hash_error = | |
607 | hash_insert (op_hash, name, (void *) &riscv_opcodes[i]); | |
608 | ||
609 | if (hash_error) | |
610 | { | |
611 | fprintf (stderr, _("internal error: can't hash `%s': %s\n"), | |
612 | riscv_opcodes[i].name, hash_error); | |
613 | /* Probably a memory allocation problem? Give up now. */ | |
614 | as_fatal (_("Broken assembler. No assembly attempted.")); | |
615 | } | |
616 | ||
617 | do | |
618 | { | |
619 | if (riscv_opcodes[i].pinfo != INSN_MACRO) | |
620 | { | |
621 | if (!validate_riscv_insn (&riscv_opcodes[i])) | |
622 | as_fatal (_("Broken assembler. No assembly attempted.")); | |
623 | } | |
624 | ++i; | |
625 | } | |
626 | while (riscv_opcodes[i].name && !strcmp (riscv_opcodes[i].name, name)); | |
627 | } | |
628 | ||
629 | reg_names_hash = hash_new (); | |
630 | hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR); | |
631 | hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR); | |
632 | hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR); | |
633 | hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR); | |
634 | ||
635 | #define DECLARE_CSR(name, num) hash_reg_name (RCLASS_CSR, #name, num); | |
636 | #include "opcode/riscv-opc.h" | |
637 | #undef DECLARE_CSR | |
638 | ||
639 | /* Set the default alignment for the text section. */ | |
640 | record_alignment (text_section, riscv_opts.rvc ? 1 : 2); | |
641 | } | |
642 | ||
45f76423 AW |
643 | static insn_t |
644 | riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value) | |
645 | { | |
646 | switch (reloc_type) | |
647 | { | |
648 | case BFD_RELOC_32: | |
649 | return value; | |
650 | ||
651 | case BFD_RELOC_RISCV_HI20: | |
652 | return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)); | |
653 | ||
654 | case BFD_RELOC_RISCV_LO12_S: | |
655 | return ENCODE_STYPE_IMM (value); | |
656 | ||
657 | case BFD_RELOC_RISCV_LO12_I: | |
658 | return ENCODE_ITYPE_IMM (value); | |
659 | ||
660 | default: | |
661 | abort (); | |
662 | } | |
663 | } | |
664 | ||
e23eba97 NC |
665 | /* Output an instruction. IP is the instruction information. |
666 | ADDRESS_EXPR is an operand of the instruction to be used with | |
667 | RELOC_TYPE. */ | |
668 | ||
669 | static void | |
670 | append_insn (struct riscv_cl_insn *ip, expressionS *address_expr, | |
671 | bfd_reloc_code_real_type reloc_type) | |
672 | { | |
673 | dwarf2_emit_insn (0); | |
674 | ||
675 | if (reloc_type != BFD_RELOC_UNUSED) | |
676 | { | |
677 | reloc_howto_type *howto; | |
678 | ||
1d65abb5 | 679 | gas_assert (address_expr); |
e23eba97 NC |
680 | if (reloc_type == BFD_RELOC_12_PCREL |
681 | || reloc_type == BFD_RELOC_RISCV_JMP) | |
682 | { | |
683 | int j = reloc_type == BFD_RELOC_RISCV_JMP; | |
684 | int best_case = riscv_insn_length (ip->insn_opcode); | |
685 | unsigned worst_case = relaxed_branch_length (NULL, NULL, 0); | |
686 | add_relaxed_insn (ip, worst_case, best_case, | |
687 | RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case), | |
688 | address_expr->X_add_symbol, | |
689 | address_expr->X_add_number); | |
690 | return; | |
691 | } | |
45f76423 | 692 | else |
e23eba97 | 693 | { |
45f76423 AW |
694 | howto = bfd_reloc_type_lookup (stdoutput, reloc_type); |
695 | if (howto == NULL) | |
696 | as_bad (_("Unsupported RISC-V relocation number %d"), reloc_type); | |
e23eba97 | 697 | |
45f76423 AW |
698 | ip->fixp = fix_new_exp (ip->frag, ip->where, |
699 | bfd_get_reloc_size (howto), | |
700 | address_expr, FALSE, reloc_type); | |
e23eba97 | 701 | |
45f76423 | 702 | ip->fixp->fx_tcbit = riscv_opts.relax; |
e23eba97 | 703 | } |
e23eba97 NC |
704 | } |
705 | ||
e23eba97 NC |
706 | add_fixed_insn (ip); |
707 | install_insn (ip); | |
708 | } | |
709 | ||
710 | /* Build an instruction created by a macro expansion. This is passed | |
711 | a pointer to the count of instructions created so far, an | |
712 | expression, the name of the instruction to build, an operand format | |
713 | string, and corresponding arguments. */ | |
714 | ||
715 | static void | |
716 | macro_build (expressionS *ep, const char *name, const char *fmt, ...) | |
717 | { | |
718 | const struct riscv_opcode *mo; | |
719 | struct riscv_cl_insn insn; | |
720 | bfd_reloc_code_real_type r; | |
721 | va_list args; | |
722 | ||
723 | va_start (args, fmt); | |
724 | ||
725 | r = BFD_RELOC_UNUSED; | |
726 | mo = (struct riscv_opcode *) hash_find (op_hash, name); | |
727 | gas_assert (mo); | |
728 | ||
729 | /* Find a non-RVC variant of the instruction. append_insn will compress | |
730 | it if possible. */ | |
731 | while (riscv_insn_length (mo->match) < 4) | |
732 | mo++; | |
733 | gas_assert (strcmp (name, mo->name) == 0); | |
734 | ||
735 | create_insn (&insn, mo); | |
736 | for (;;) | |
737 | { | |
738 | switch (*fmt++) | |
739 | { | |
740 | case 'd': | |
741 | INSERT_OPERAND (RD, insn, va_arg (args, int)); | |
742 | continue; | |
743 | ||
744 | case 's': | |
745 | INSERT_OPERAND (RS1, insn, va_arg (args, int)); | |
746 | continue; | |
747 | ||
748 | case 't': | |
749 | INSERT_OPERAND (RS2, insn, va_arg (args, int)); | |
750 | continue; | |
751 | ||
752 | case '>': | |
753 | INSERT_OPERAND (SHAMT, insn, va_arg (args, int)); | |
754 | continue; | |
755 | ||
756 | case 'j': | |
757 | case 'u': | |
758 | case 'q': | |
759 | gas_assert (ep != NULL); | |
760 | r = va_arg (args, int); | |
761 | continue; | |
762 | ||
763 | case '\0': | |
764 | break; | |
765 | case ',': | |
766 | continue; | |
767 | default: | |
768 | as_fatal (_("internal error: invalid macro")); | |
769 | } | |
770 | break; | |
771 | } | |
772 | va_end (args); | |
773 | gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL); | |
774 | ||
775 | append_insn (&insn, ep, r); | |
776 | } | |
777 | ||
778 | /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits | |
779 | unset. */ | |
780 | static void | |
781 | normalize_constant_expr (expressionS *ex) | |
782 | { | |
783 | if (xlen > 32) | |
784 | return; | |
785 | if ((ex->X_op == O_constant || ex->X_op == O_symbol) | |
786 | && IS_ZEXT_32BIT_NUM (ex->X_add_number)) | |
787 | ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000) | |
788 | - 0x80000000); | |
789 | } | |
790 | ||
791 | /* Fail if an expression is not a constant. */ | |
792 | ||
793 | static void | |
794 | check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex) | |
795 | { | |
796 | if (ex->X_op == O_big) | |
797 | as_bad (_("unsupported large constant")); | |
798 | else if (ex->X_op != O_constant) | |
799 | as_bad (_("Instruction %s requires absolute expression"), | |
800 | ip->insn_mo->name); | |
801 | normalize_constant_expr (ex); | |
802 | } | |
803 | ||
804 | static symbolS * | |
805 | make_internal_label (void) | |
806 | { | |
807 | return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, | |
1d65abb5 | 808 | (valueT) frag_now_fix (), frag_now); |
e23eba97 NC |
809 | } |
810 | ||
811 | /* Load an entry from the GOT. */ | |
812 | static void | |
813 | pcrel_access (int destreg, int tempreg, expressionS *ep, | |
814 | const char *lo_insn, const char *lo_pattern, | |
815 | bfd_reloc_code_real_type hi_reloc, | |
816 | bfd_reloc_code_real_type lo_reloc) | |
817 | { | |
818 | expressionS ep2; | |
819 | ep2.X_op = O_symbol; | |
820 | ep2.X_add_symbol = make_internal_label (); | |
821 | ep2.X_add_number = 0; | |
822 | ||
823 | macro_build (ep, "auipc", "d,u", tempreg, hi_reloc); | |
824 | macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc); | |
825 | } | |
826 | ||
827 | static void | |
828 | pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn, | |
829 | bfd_reloc_code_real_type hi_reloc, | |
830 | bfd_reloc_code_real_type lo_reloc) | |
831 | { | |
832 | pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc); | |
833 | } | |
834 | ||
835 | static void | |
836 | pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn, | |
837 | bfd_reloc_code_real_type hi_reloc, | |
838 | bfd_reloc_code_real_type lo_reloc) | |
839 | { | |
840 | pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc); | |
841 | } | |
842 | ||
843 | /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */ | |
844 | static void | |
845 | riscv_call (int destreg, int tempreg, expressionS *ep, | |
846 | bfd_reloc_code_real_type reloc) | |
847 | { | |
848 | macro_build (ep, "auipc", "d,u", tempreg, reloc); | |
849 | macro_build (NULL, "jalr", "d,s", destreg, tempreg); | |
850 | } | |
851 | ||
852 | /* Load an integer constant into a register. */ | |
853 | ||
854 | static void | |
855 | load_const (int reg, expressionS *ep) | |
856 | { | |
857 | int shift = RISCV_IMM_BITS; | |
858 | expressionS upper = *ep, lower = *ep; | |
859 | lower.X_add_number = (int32_t) ep->X_add_number << (32-shift) >> (32-shift); | |
860 | upper.X_add_number -= lower.X_add_number; | |
861 | ||
862 | if (ep->X_op != O_constant) | |
863 | { | |
864 | as_bad (_("unsupported large constant")); | |
865 | return; | |
866 | } | |
867 | ||
1d65abb5 | 868 | if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number)) |
e23eba97 NC |
869 | { |
870 | /* Reduce to a signed 32-bit constant using SLLI and ADDI. */ | |
871 | while (((upper.X_add_number >> shift) & 1) == 0) | |
872 | shift++; | |
873 | ||
874 | upper.X_add_number = (int64_t) upper.X_add_number >> shift; | |
1d65abb5 | 875 | load_const (reg, &upper); |
e23eba97 NC |
876 | |
877 | macro_build (NULL, "slli", "d,s,>", reg, reg, shift); | |
878 | if (lower.X_add_number != 0) | |
879 | macro_build (&lower, "addi", "d,s,j", reg, reg, BFD_RELOC_RISCV_LO12_I); | |
880 | } | |
881 | else | |
882 | { | |
883 | /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */ | |
884 | int hi_reg = 0; | |
885 | ||
886 | if (upper.X_add_number != 0) | |
887 | { | |
888 | macro_build (ep, "lui", "d,u", reg, BFD_RELOC_RISCV_HI20); | |
889 | hi_reg = reg; | |
890 | } | |
891 | ||
892 | if (lower.X_add_number != 0 || hi_reg == 0) | |
893 | macro_build (ep, ADD32_INSN, "d,s,j", reg, hi_reg, | |
894 | BFD_RELOC_RISCV_LO12_I); | |
895 | } | |
896 | } | |
897 | ||
898 | /* Expand RISC-V assembly macros into one or more instructions. */ | |
899 | static void | |
900 | macro (struct riscv_cl_insn *ip, expressionS *imm_expr, | |
901 | bfd_reloc_code_real_type *imm_reloc) | |
902 | { | |
903 | int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD; | |
904 | int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1; | |
905 | int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2; | |
906 | int mask = ip->insn_mo->mask; | |
907 | ||
908 | switch (mask) | |
909 | { | |
910 | case M_LI: | |
911 | load_const (rd, imm_expr); | |
912 | break; | |
913 | ||
914 | case M_LA: | |
915 | case M_LLA: | |
916 | /* Load the address of a symbol into a register. */ | |
917 | if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number)) | |
918 | as_bad (_("offset too large")); | |
919 | ||
920 | if (imm_expr->X_op == O_constant) | |
921 | load_const (rd, imm_expr); | |
922 | else if (riscv_opts.pic && mask == M_LA) /* Global PIC symbol */ | |
923 | pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN, | |
924 | BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
925 | else /* Local PIC symbol, or any non-PIC symbol */ | |
926 | pcrel_load (rd, rd, imm_expr, "addi", | |
927 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
928 | break; | |
929 | ||
930 | case M_LA_TLS_GD: | |
931 | pcrel_load (rd, rd, imm_expr, "addi", | |
932 | BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
933 | break; | |
934 | ||
935 | case M_LA_TLS_IE: | |
936 | pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN, | |
937 | BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
938 | break; | |
939 | ||
940 | case M_LB: | |
941 | pcrel_load (rd, rd, imm_expr, "lb", | |
942 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
943 | break; | |
944 | ||
945 | case M_LBU: | |
946 | pcrel_load (rd, rd, imm_expr, "lbu", | |
947 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
948 | break; | |
949 | ||
950 | case M_LH: | |
951 | pcrel_load (rd, rd, imm_expr, "lh", | |
952 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
953 | break; | |
954 | ||
955 | case M_LHU: | |
956 | pcrel_load (rd, rd, imm_expr, "lhu", | |
957 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
958 | break; | |
959 | ||
960 | case M_LW: | |
961 | pcrel_load (rd, rd, imm_expr, "lw", | |
962 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
963 | break; | |
964 | ||
965 | case M_LWU: | |
966 | pcrel_load (rd, rd, imm_expr, "lwu", | |
967 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
968 | break; | |
969 | ||
970 | case M_LD: | |
971 | pcrel_load (rd, rd, imm_expr, "ld", | |
972 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
973 | break; | |
974 | ||
975 | case M_FLW: | |
976 | pcrel_load (rd, rs1, imm_expr, "flw", | |
977 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
978 | break; | |
979 | ||
980 | case M_FLD: | |
981 | pcrel_load (rd, rs1, imm_expr, "fld", | |
982 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I); | |
983 | break; | |
984 | ||
985 | case M_SB: | |
986 | pcrel_store (rs2, rs1, imm_expr, "sb", | |
987 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S); | |
988 | break; | |
989 | ||
990 | case M_SH: | |
991 | pcrel_store (rs2, rs1, imm_expr, "sh", | |
992 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S); | |
993 | break; | |
994 | ||
995 | case M_SW: | |
996 | pcrel_store (rs2, rs1, imm_expr, "sw", | |
997 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S); | |
998 | break; | |
999 | ||
1000 | case M_SD: | |
1001 | pcrel_store (rs2, rs1, imm_expr, "sd", | |
1002 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S); | |
1003 | break; | |
1004 | ||
1005 | case M_FSW: | |
1006 | pcrel_store (rs2, rs1, imm_expr, "fsw", | |
1007 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S); | |
1008 | break; | |
1009 | ||
1010 | case M_FSD: | |
1011 | pcrel_store (rs2, rs1, imm_expr, "fsd", | |
1012 | BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S); | |
1013 | break; | |
1014 | ||
1015 | case M_CALL: | |
1016 | riscv_call (rd, rs1, imm_expr, *imm_reloc); | |
1017 | break; | |
1018 | ||
1019 | default: | |
1020 | as_bad (_("Macro %s not implemented"), ip->insn_mo->name); | |
1021 | break; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | static const struct percent_op_match percent_op_utype[] = | |
1026 | { | |
1027 | {"%tprel_hi", BFD_RELOC_RISCV_TPREL_HI20}, | |
1028 | {"%pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20}, | |
1029 | {"%tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20}, | |
1030 | {"%tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20}, | |
1031 | {"%hi", BFD_RELOC_RISCV_HI20}, | |
1032 | {0, 0} | |
1033 | }; | |
1034 | ||
1035 | static const struct percent_op_match percent_op_itype[] = | |
1036 | { | |
1037 | {"%lo", BFD_RELOC_RISCV_LO12_I}, | |
1038 | {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I}, | |
1039 | {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I}, | |
1040 | {0, 0} | |
1041 | }; | |
1042 | ||
1043 | static const struct percent_op_match percent_op_stype[] = | |
1044 | { | |
1045 | {"%lo", BFD_RELOC_RISCV_LO12_S}, | |
1046 | {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S}, | |
1047 | {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S}, | |
1048 | {0, 0} | |
1049 | }; | |
1050 | ||
1051 | static const struct percent_op_match percent_op_rtype[] = | |
1052 | { | |
1053 | {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD}, | |
1054 | {0, 0} | |
1055 | }; | |
1056 | ||
1057 | /* Return true if *STR points to a relocation operator. When returning true, | |
1058 | move *STR over the operator and store its relocation code in *RELOC. | |
1059 | Leave both *STR and *RELOC alone when returning false. */ | |
1060 | ||
1061 | static bfd_boolean | |
1062 | parse_relocation (char **str, bfd_reloc_code_real_type *reloc, | |
1063 | const struct percent_op_match *percent_op) | |
1064 | { | |
1065 | for ( ; percent_op->str; percent_op++) | |
1066 | if (strncasecmp (*str, percent_op->str, strlen (percent_op->str)) == 0) | |
1067 | { | |
1068 | int len = strlen (percent_op->str); | |
1069 | ||
1070 | if (!ISSPACE ((*str)[len]) && (*str)[len] != '(') | |
1071 | continue; | |
1072 | ||
1073 | *str += strlen (percent_op->str); | |
1074 | *reloc = percent_op->reloc; | |
1075 | ||
1076 | /* Check whether the output BFD supports this relocation. | |
1077 | If not, issue an error and fall back on something safe. */ | |
45f76423 AW |
1078 | if (*reloc != BFD_RELOC_UNUSED |
1079 | && !bfd_reloc_type_lookup (stdoutput, *reloc)) | |
e23eba97 NC |
1080 | { |
1081 | as_bad ("relocation %s isn't supported by the current ABI", | |
1082 | percent_op->str); | |
1083 | *reloc = BFD_RELOC_UNUSED; | |
1084 | } | |
1085 | return TRUE; | |
1086 | } | |
1087 | return FALSE; | |
1088 | } | |
1089 | ||
1090 | static void | |
1091 | my_getExpression (expressionS *ep, char *str) | |
1092 | { | |
1093 | char *save_in; | |
1094 | ||
1095 | save_in = input_line_pointer; | |
1096 | input_line_pointer = str; | |
1097 | expression (ep); | |
1098 | expr_end = input_line_pointer; | |
1099 | input_line_pointer = save_in; | |
1100 | } | |
1101 | ||
1102 | /* Parse string STR as a 16-bit relocatable operand. Store the | |
1103 | expression in *EP and the relocation, if any, in RELOC. | |
1104 | Return the number of relocation operators used (0 or 1). | |
1105 | ||
1106 | On exit, EXPR_END points to the first character after the expression. */ | |
1107 | ||
1108 | static size_t | |
1109 | my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc, | |
1110 | char *str, const struct percent_op_match *percent_op) | |
1111 | { | |
1112 | size_t reloc_index; | |
1113 | unsigned crux_depth, str_depth, regno; | |
1114 | char *crux; | |
1115 | ||
1116 | /* First, check for integer registers. */ | |
1117 | if (reg_lookup (&str, RCLASS_GPR, ®no)) | |
1118 | { | |
1119 | ep->X_op = O_register; | |
1120 | ep->X_add_number = regno; | |
1121 | return 0; | |
1122 | } | |
1123 | ||
1124 | /* Search for the start of the main expression. | |
1125 | End the loop with CRUX pointing to the start | |
1126 | of the main expression and with CRUX_DEPTH containing the number | |
1127 | of open brackets at that point. */ | |
1128 | reloc_index = -1; | |
1129 | str_depth = 0; | |
1130 | do | |
1131 | { | |
1132 | reloc_index++; | |
1133 | crux = str; | |
1134 | crux_depth = str_depth; | |
1135 | ||
1136 | /* Skip over whitespace and brackets, keeping count of the number | |
1137 | of brackets. */ | |
1138 | while (*str == ' ' || *str == '\t' || *str == '(') | |
1139 | if (*str++ == '(') | |
1140 | str_depth++; | |
1141 | } | |
1142 | while (*str == '%' | |
1143 | && reloc_index < 1 | |
1144 | && parse_relocation (&str, reloc, percent_op)); | |
1145 | ||
1146 | my_getExpression (ep, crux); | |
1147 | str = expr_end; | |
1148 | ||
1149 | /* Match every open bracket. */ | |
1150 | while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t')) | |
1151 | if (*str++ == ')') | |
1152 | crux_depth--; | |
1153 | ||
1154 | if (crux_depth > 0) | |
1155 | as_bad ("unclosed '('"); | |
1156 | ||
1157 | expr_end = str; | |
1158 | ||
1159 | return reloc_index; | |
1160 | } | |
1161 | ||
1162 | /* This routine assembles an instruction into its binary format. As a | |
1163 | side effect, it sets the global variable imm_reloc to the type of | |
1164 | relocation to do if one of the operands is an address expression. */ | |
1165 | ||
1166 | static const char * | |
1167 | riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr, | |
1168 | bfd_reloc_code_real_type *imm_reloc) | |
1169 | { | |
1170 | char *s; | |
1171 | const char *args; | |
1172 | char c = 0; | |
1173 | struct riscv_opcode *insn; | |
1174 | char *argsStart; | |
1175 | unsigned int regno; | |
1176 | char save_c = 0; | |
1177 | int argnum; | |
1178 | const struct percent_op_match *p; | |
1179 | const char *error = "unrecognized opcode"; | |
1180 | ||
1181 | /* Parse the name of the instruction. Terminate the string if whitespace | |
1182 | is found so that hash_find only sees the name part of the string. */ | |
1183 | for (s = str; *s != '\0'; ++s) | |
1184 | if (ISSPACE (*s)) | |
1185 | { | |
1186 | save_c = *s; | |
1187 | *s++ = '\0'; | |
1188 | break; | |
1189 | } | |
1190 | ||
1191 | insn = (struct riscv_opcode *) hash_find (op_hash, str); | |
1192 | ||
1193 | argsStart = s; | |
1194 | for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++) | |
1195 | { | |
1196 | if (!riscv_subset_supports (insn->subset)) | |
1197 | continue; | |
1198 | ||
1199 | create_insn (ip, insn); | |
1200 | argnum = 1; | |
1201 | ||
1202 | imm_expr->X_op = O_absent; | |
1203 | *imm_reloc = BFD_RELOC_UNUSED; | |
1204 | p = percent_op_itype; | |
1205 | ||
1206 | for (args = insn->args;; ++args) | |
1207 | { | |
1208 | s += strspn (s, " \t"); | |
1209 | switch (*args) | |
1210 | { | |
1211 | case '\0': /* End of args. */ | |
1212 | if (insn->pinfo != INSN_MACRO) | |
1213 | { | |
1214 | if (!insn->match_func (insn, ip->insn_opcode)) | |
1215 | break; | |
1216 | if (riscv_insn_length (insn->match) == 2 && !riscv_opts.rvc) | |
1217 | break; | |
1218 | } | |
1219 | if (*s != '\0') | |
1220 | break; | |
1221 | /* Successful assembly. */ | |
1222 | error = NULL; | |
1223 | goto out; | |
1224 | ||
1225 | case 'C': /* RVC */ | |
1226 | switch (*++args) | |
1227 | { | |
1228 | case 's': /* RS1 x8-x15 */ | |
1229 | if (!reg_lookup (&s, RCLASS_GPR, ®no) | |
1230 | || !(regno >= 8 && regno <= 15)) | |
1231 | break; | |
1232 | INSERT_OPERAND (CRS1S, *ip, regno % 8); | |
1233 | continue; | |
1234 | case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */ | |
1235 | if (!reg_lookup (&s, RCLASS_GPR, ®no) | |
1236 | || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno) | |
1237 | break; | |
1238 | continue; | |
1239 | case 't': /* RS2 x8-x15 */ | |
1240 | if (!reg_lookup (&s, RCLASS_GPR, ®no) | |
1241 | || !(regno >= 8 && regno <= 15)) | |
1242 | break; | |
1243 | INSERT_OPERAND (CRS2S, *ip, regno % 8); | |
1244 | continue; | |
1245 | case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */ | |
1246 | if (!reg_lookup (&s, RCLASS_GPR, ®no) | |
1247 | || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno) | |
1248 | break; | |
1249 | continue; | |
1250 | case 'U': /* RS1, constrained to equal RD. */ | |
1251 | if (!reg_lookup (&s, RCLASS_GPR, ®no) | |
1252 | || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno) | |
1253 | break; | |
1254 | continue; | |
1255 | case 'V': /* RS2 */ | |
1256 | if (!reg_lookup (&s, RCLASS_GPR, ®no)) | |
1257 | break; | |
1258 | INSERT_OPERAND (CRS2, *ip, regno); | |
1259 | continue; | |
1260 | case 'c': /* RS1, constrained to equal sp. */ | |
1261 | if (!reg_lookup (&s, RCLASS_GPR, ®no) | |
1262 | || regno != X_SP) | |
1263 | break; | |
1264 | continue; | |
1265 | case '>': | |
1266 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1267 | || imm_expr->X_op != O_constant | |
1268 | || imm_expr->X_add_number <= 0 | |
1269 | || imm_expr->X_add_number >= 64) | |
1270 | break; | |
1271 | ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number); | |
1272 | rvc_imm_done: | |
1273 | s = expr_end; | |
1274 | imm_expr->X_op = O_absent; | |
1275 | continue; | |
1276 | case '<': | |
1277 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1278 | || imm_expr->X_op != O_constant | |
1279 | || !VALID_RVC_IMM (imm_expr->X_add_number) | |
1280 | || imm_expr->X_add_number <= 0 | |
1281 | || imm_expr->X_add_number >= 32) | |
1282 | break; | |
1283 | ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number); | |
1284 | goto rvc_imm_done; | |
1285 | case 'i': | |
1286 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1287 | || imm_expr->X_op != O_constant | |
1288 | || imm_expr->X_add_number == 0 | |
1289 | || !VALID_RVC_SIMM3 (imm_expr->X_add_number)) | |
1290 | break; | |
1291 | ip->insn_opcode |= ENCODE_RVC_SIMM3 (imm_expr->X_add_number); | |
1292 | goto rvc_imm_done; | |
1293 | case 'j': | |
1294 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1295 | || imm_expr->X_op != O_constant | |
1296 | || imm_expr->X_add_number == 0 | |
1297 | || !VALID_RVC_IMM (imm_expr->X_add_number)) | |
1298 | break; | |
1299 | ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number); | |
1300 | goto rvc_imm_done; | |
1301 | case 'k': | |
1302 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1303 | || imm_expr->X_op != O_constant | |
1304 | || !VALID_RVC_LW_IMM (imm_expr->X_add_number)) | |
1305 | break; | |
1306 | ip->insn_opcode |= ENCODE_RVC_LW_IMM (imm_expr->X_add_number); | |
1307 | goto rvc_imm_done; | |
1308 | case 'l': | |
1309 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1310 | || imm_expr->X_op != O_constant | |
1311 | || !VALID_RVC_LD_IMM (imm_expr->X_add_number)) | |
1312 | break; | |
1313 | ip->insn_opcode |= ENCODE_RVC_LD_IMM (imm_expr->X_add_number); | |
1314 | goto rvc_imm_done; | |
1315 | case 'm': | |
1316 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1317 | || imm_expr->X_op != O_constant | |
1318 | || !VALID_RVC_LWSP_IMM (imm_expr->X_add_number)) | |
1319 | break; | |
1320 | ip->insn_opcode |= | |
1321 | ENCODE_RVC_LWSP_IMM (imm_expr->X_add_number); | |
1322 | goto rvc_imm_done; | |
1323 | case 'n': | |
1324 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1325 | || imm_expr->X_op != O_constant | |
1326 | || !VALID_RVC_LDSP_IMM (imm_expr->X_add_number)) | |
1327 | break; | |
1328 | ip->insn_opcode |= | |
1329 | ENCODE_RVC_LDSP_IMM (imm_expr->X_add_number); | |
1330 | goto rvc_imm_done; | |
b416fe87 KC |
1331 | case 'o': |
1332 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1333 | || imm_expr->X_op != O_constant | |
1334 | || !VALID_RVC_IMM (imm_expr->X_add_number)) | |
1335 | break; | |
1336 | ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number); | |
1337 | goto rvc_imm_done; | |
e23eba97 NC |
1338 | case 'K': |
1339 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1340 | || imm_expr->X_op != O_constant | |
1341 | || !VALID_RVC_ADDI4SPN_IMM (imm_expr->X_add_number) | |
1342 | || imm_expr->X_add_number == 0) | |
1343 | break; | |
1344 | ip->insn_opcode |= | |
1345 | ENCODE_RVC_ADDI4SPN_IMM (imm_expr->X_add_number); | |
1346 | goto rvc_imm_done; | |
1347 | case 'L': | |
1348 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1349 | || imm_expr->X_op != O_constant | |
1350 | || !VALID_RVC_ADDI16SP_IMM (imm_expr->X_add_number) | |
1351 | || imm_expr->X_add_number == 0) | |
1352 | break; | |
1353 | ip->insn_opcode |= | |
1354 | ENCODE_RVC_ADDI16SP_IMM (imm_expr->X_add_number); | |
1355 | goto rvc_imm_done; | |
1356 | case 'M': | |
1357 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1358 | || imm_expr->X_op != O_constant | |
1359 | || !VALID_RVC_SWSP_IMM (imm_expr->X_add_number)) | |
1360 | break; | |
1361 | ip->insn_opcode |= | |
1362 | ENCODE_RVC_SWSP_IMM (imm_expr->X_add_number); | |
1363 | goto rvc_imm_done; | |
1364 | case 'N': | |
1365 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1366 | || imm_expr->X_op != O_constant | |
1367 | || !VALID_RVC_SDSP_IMM (imm_expr->X_add_number)) | |
1368 | break; | |
1369 | ip->insn_opcode |= | |
1370 | ENCODE_RVC_SDSP_IMM (imm_expr->X_add_number); | |
1371 | goto rvc_imm_done; | |
1372 | case 'u': | |
1373 | p = percent_op_utype; | |
1374 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p)) | |
1375 | break; | |
1376 | rvc_lui: | |
1377 | if (imm_expr->X_op != O_constant | |
1378 | || imm_expr->X_add_number <= 0 | |
1379 | || imm_expr->X_add_number >= RISCV_BIGIMM_REACH | |
1380 | || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2 | |
1381 | && (imm_expr->X_add_number < | |
1382 | RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2))) | |
1383 | break; | |
1384 | ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number); | |
1385 | goto rvc_imm_done; | |
1386 | case 'v': | |
1387 | if (my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1388 | || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1)) | |
1389 | || ((int32_t)imm_expr->X_add_number | |
1390 | != imm_expr->X_add_number)) | |
1391 | break; | |
1392 | imm_expr->X_add_number = | |
1393 | ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS; | |
1394 | goto rvc_lui; | |
1395 | case 'p': | |
1396 | goto branch; | |
1397 | case 'a': | |
1398 | goto jump; | |
1399 | case 'D': /* Floating-point RS2 x8-x15. */ | |
1400 | if (!reg_lookup (&s, RCLASS_FPR, ®no) | |
1401 | || !(regno >= 8 && regno <= 15)) | |
1402 | break; | |
1403 | INSERT_OPERAND (CRS2S, *ip, regno % 8); | |
1404 | continue; | |
1405 | case 'T': /* Floating-point RS2. */ | |
1406 | if (!reg_lookup (&s, RCLASS_FPR, ®no)) | |
1407 | break; | |
1408 | INSERT_OPERAND (CRS2, *ip, regno); | |
1409 | continue; | |
1410 | default: | |
1411 | as_bad (_("bad RVC field specifier 'C%c'\n"), *args); | |
1412 | } | |
1413 | break; | |
1414 | ||
1415 | case ',': | |
1416 | ++argnum; | |
1417 | if (*s++ == *args) | |
1418 | continue; | |
1419 | s--; | |
1420 | break; | |
1421 | ||
1422 | case '(': | |
1423 | case ')': | |
1424 | case '[': | |
1425 | case ']': | |
1426 | if (*s++ == *args) | |
1427 | continue; | |
1428 | break; | |
1429 | ||
1430 | case '<': /* Shift amount, 0 - 31. */ | |
1431 | my_getExpression (imm_expr, s); | |
1432 | check_absolute_expr (ip, imm_expr); | |
1433 | if ((unsigned long) imm_expr->X_add_number > 31) | |
1434 | as_warn (_("Improper shift amount (%lu)"), | |
1435 | (unsigned long) imm_expr->X_add_number); | |
1436 | INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number); | |
1437 | imm_expr->X_op = O_absent; | |
1438 | s = expr_end; | |
1439 | continue; | |
1440 | ||
1441 | case '>': /* Shift amount, 0 - (XLEN-1). */ | |
1442 | my_getExpression (imm_expr, s); | |
1443 | check_absolute_expr (ip, imm_expr); | |
1444 | if ((unsigned long) imm_expr->X_add_number >= xlen) | |
1445 | as_warn (_("Improper shift amount (%lu)"), | |
1446 | (unsigned long) imm_expr->X_add_number); | |
1447 | INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number); | |
1448 | imm_expr->X_op = O_absent; | |
1449 | s = expr_end; | |
1450 | continue; | |
1451 | ||
1452 | case 'Z': /* CSRRxI immediate. */ | |
1453 | my_getExpression (imm_expr, s); | |
1454 | check_absolute_expr (ip, imm_expr); | |
1455 | if ((unsigned long) imm_expr->X_add_number > 31) | |
1456 | as_warn (_("Improper CSRxI immediate (%lu)"), | |
1457 | (unsigned long) imm_expr->X_add_number); | |
1458 | INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number); | |
1459 | imm_expr->X_op = O_absent; | |
1460 | s = expr_end; | |
1461 | continue; | |
1462 | ||
1463 | case 'E': /* Control register. */ | |
1464 | if (reg_lookup (&s, RCLASS_CSR, ®no)) | |
1465 | INSERT_OPERAND (CSR, *ip, regno); | |
1466 | else | |
1467 | { | |
1468 | my_getExpression (imm_expr, s); | |
1469 | check_absolute_expr (ip, imm_expr); | |
1470 | if ((unsigned long) imm_expr->X_add_number > 0xfff) | |
1d65abb5 AW |
1471 | as_warn (_("Improper CSR address (%lu)"), |
1472 | (unsigned long) imm_expr->X_add_number); | |
e23eba97 NC |
1473 | INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number); |
1474 | imm_expr->X_op = O_absent; | |
1475 | s = expr_end; | |
1476 | } | |
1477 | continue; | |
1478 | ||
1479 | case 'm': /* Rounding mode. */ | |
1480 | if (arg_lookup (&s, riscv_rm, ARRAY_SIZE (riscv_rm), ®no)) | |
1481 | { | |
1482 | INSERT_OPERAND (RM, *ip, regno); | |
1483 | continue; | |
1484 | } | |
1485 | break; | |
1486 | ||
1487 | case 'P': | |
1488 | case 'Q': /* Fence predecessor/successor. */ | |
1489 | if (arg_lookup (&s, riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ), | |
1490 | ®no)) | |
1491 | { | |
1492 | if (*args == 'P') | |
1493 | INSERT_OPERAND (PRED, *ip, regno); | |
1494 | else | |
1495 | INSERT_OPERAND (SUCC, *ip, regno); | |
1496 | continue; | |
1497 | } | |
1498 | break; | |
1499 | ||
1500 | case 'd': /* Destination register. */ | |
1501 | case 's': /* Source register. */ | |
1502 | case 't': /* Target register. */ | |
1503 | if (reg_lookup (&s, RCLASS_GPR, ®no)) | |
1504 | { | |
1505 | c = *args; | |
1506 | if (*s == ' ') | |
1507 | ++s; | |
1508 | ||
1509 | /* Now that we have assembled one operand, we use the args | |
1510 | string to figure out where it goes in the instruction. */ | |
1511 | switch (c) | |
1512 | { | |
1513 | case 's': | |
1514 | INSERT_OPERAND (RS1, *ip, regno); | |
1515 | break; | |
1516 | case 'd': | |
1517 | INSERT_OPERAND (RD, *ip, regno); | |
1518 | break; | |
1519 | case 't': | |
1520 | INSERT_OPERAND (RS2, *ip, regno); | |
1521 | break; | |
1522 | } | |
1523 | continue; | |
1524 | } | |
1525 | break; | |
1526 | ||
1527 | case 'D': /* Floating point rd. */ | |
1528 | case 'S': /* Floating point rs1. */ | |
1529 | case 'T': /* Floating point rs2. */ | |
1530 | case 'U': /* Floating point rs1 and rs2. */ | |
1531 | case 'R': /* Floating point rs3. */ | |
1532 | if (reg_lookup (&s, RCLASS_FPR, ®no)) | |
1533 | { | |
1534 | c = *args; | |
1535 | if (*s == ' ') | |
1536 | ++s; | |
1537 | switch (c) | |
1538 | { | |
1539 | case 'D': | |
1540 | INSERT_OPERAND (RD, *ip, regno); | |
1541 | break; | |
1542 | case 'S': | |
1543 | INSERT_OPERAND (RS1, *ip, regno); | |
1544 | break; | |
1545 | case 'U': | |
1546 | INSERT_OPERAND (RS1, *ip, regno); | |
1547 | /* fallthru */ | |
1548 | case 'T': | |
1549 | INSERT_OPERAND (RS2, *ip, regno); | |
1550 | break; | |
1551 | case 'R': | |
1552 | INSERT_OPERAND (RS3, *ip, regno); | |
1553 | break; | |
1554 | } | |
1555 | continue; | |
1556 | } | |
1557 | ||
1558 | break; | |
1559 | ||
1560 | case 'I': | |
1561 | my_getExpression (imm_expr, s); | |
1562 | if (imm_expr->X_op != O_big | |
1563 | && imm_expr->X_op != O_constant) | |
1564 | break; | |
1565 | normalize_constant_expr (imm_expr); | |
1566 | s = expr_end; | |
1567 | continue; | |
1568 | ||
1569 | case 'A': | |
1570 | my_getExpression (imm_expr, s); | |
1571 | normalize_constant_expr (imm_expr); | |
1572 | /* The 'A' format specifier must be a symbol. */ | |
1573 | if (imm_expr->X_op != O_symbol) | |
1574 | break; | |
1575 | *imm_reloc = BFD_RELOC_32; | |
1576 | s = expr_end; | |
1577 | continue; | |
1578 | ||
1579 | case 'j': /* Sign-extended immediate. */ | |
1580 | *imm_reloc = BFD_RELOC_RISCV_LO12_I; | |
1581 | p = percent_op_itype; | |
1582 | goto alu_op; | |
1583 | case 'q': /* Store displacement. */ | |
1584 | p = percent_op_stype; | |
1585 | *imm_reloc = BFD_RELOC_RISCV_LO12_S; | |
1586 | goto load_store; | |
1587 | case 'o': /* Load displacement. */ | |
1588 | p = percent_op_itype; | |
1589 | *imm_reloc = BFD_RELOC_RISCV_LO12_I; | |
1590 | goto load_store; | |
1591 | case '0': /* AMO "displacement," which must be zero. */ | |
1592 | p = percent_op_rtype; | |
1593 | *imm_reloc = BFD_RELOC_UNUSED; | |
1594 | load_store: | |
1595 | /* Check whether there is only a single bracketed expression | |
1596 | left. If so, it must be the base register and the | |
1597 | constant must be zero. */ | |
1598 | imm_expr->X_op = O_constant; | |
1599 | imm_expr->X_add_number = 0; | |
1600 | if (*s == '(' && strchr (s + 1, '(') == 0) | |
1601 | continue; | |
1602 | alu_op: | |
1603 | /* If this value won't fit into a 16 bit offset, then go | |
1604 | find a macro that will generate the 32 bit offset | |
1605 | code pattern. */ | |
1606 | if (!my_getSmallExpression (imm_expr, imm_reloc, s, p)) | |
1607 | { | |
1608 | normalize_constant_expr (imm_expr); | |
1609 | if (imm_expr->X_op != O_constant | |
1610 | || (*args == '0' && imm_expr->X_add_number != 0) | |
1611 | || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2 | |
1612 | || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2) | |
1613 | break; | |
1614 | } | |
1615 | ||
1616 | s = expr_end; | |
1617 | continue; | |
1618 | ||
1619 | case 'p': /* PC-relative offset. */ | |
1620 | branch: | |
1621 | *imm_reloc = BFD_RELOC_12_PCREL; | |
1622 | my_getExpression (imm_expr, s); | |
1623 | s = expr_end; | |
1624 | continue; | |
1625 | ||
1626 | case 'u': /* Upper 20 bits. */ | |
1627 | p = percent_op_utype; | |
1628 | if (!my_getSmallExpression (imm_expr, imm_reloc, s, p) | |
1629 | && imm_expr->X_op == O_constant) | |
1630 | { | |
1631 | if (imm_expr->X_add_number < 0 | |
1632 | || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH) | |
1633 | as_bad (_("lui expression not in range 0..1048575")); | |
1634 | ||
1635 | *imm_reloc = BFD_RELOC_RISCV_HI20; | |
1636 | imm_expr->X_add_number <<= RISCV_IMM_BITS; | |
1637 | } | |
1638 | s = expr_end; | |
1639 | continue; | |
1640 | ||
1641 | case 'a': /* 20-bit PC-relative offset. */ | |
1642 | jump: | |
1643 | my_getExpression (imm_expr, s); | |
1644 | s = expr_end; | |
1645 | *imm_reloc = BFD_RELOC_RISCV_JMP; | |
1646 | continue; | |
1647 | ||
1648 | case 'c': | |
1649 | my_getExpression (imm_expr, s); | |
1650 | s = expr_end; | |
1651 | if (strcmp (s, "@plt") == 0) | |
1652 | { | |
1653 | *imm_reloc = BFD_RELOC_RISCV_CALL_PLT; | |
1654 | s += 4; | |
1655 | } | |
1656 | else | |
1657 | *imm_reloc = BFD_RELOC_RISCV_CALL; | |
1658 | continue; | |
1659 | ||
1660 | default: | |
1661 | as_fatal (_("internal error: bad argument type %c"), *args); | |
1662 | } | |
1663 | break; | |
1664 | } | |
1665 | s = argsStart; | |
1666 | error = _("illegal operands"); | |
1667 | } | |
1668 | ||
1669 | out: | |
1670 | /* Restore the character we might have clobbered above. */ | |
1671 | if (save_c) | |
1672 | *(argsStart - 1) = save_c; | |
1673 | ||
1674 | return error; | |
1675 | } | |
1676 | ||
1677 | void | |
1678 | md_assemble (char *str) | |
1679 | { | |
1680 | struct riscv_cl_insn insn; | |
1681 | expressionS imm_expr; | |
1682 | bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED; | |
1683 | ||
1684 | const char *error = riscv_ip (str, &insn, &imm_expr, &imm_reloc); | |
1685 | ||
1686 | if (error) | |
1687 | { | |
1688 | as_bad ("%s `%s'", error, str); | |
1689 | return; | |
1690 | } | |
1691 | ||
1692 | if (insn.insn_mo->pinfo == INSN_MACRO) | |
1693 | macro (&insn, &imm_expr, &imm_reloc); | |
1694 | else | |
1695 | append_insn (&insn, &imm_expr, imm_reloc); | |
1696 | } | |
1697 | ||
1698 | const char * | |
1699 | md_atof (int type, char *litP, int *sizeP) | |
1700 | { | |
1701 | return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN); | |
1702 | } | |
1703 | ||
1704 | void | |
1705 | md_number_to_chars (char *buf, valueT val, int n) | |
1706 | { | |
1707 | number_to_chars_littleendian (buf, val, n); | |
1708 | } | |
1709 | ||
1710 | const char *md_shortopts = "O::g::G:"; | |
1711 | ||
1712 | enum options | |
1713 | { | |
2922d21d | 1714 | OPTION_MARCH = OPTION_MD_BASE, |
e23eba97 NC |
1715 | OPTION_PIC, |
1716 | OPTION_NO_PIC, | |
2922d21d | 1717 | OPTION_MABI, |
e23eba97 NC |
1718 | OPTION_END_OF_ENUM |
1719 | }; | |
1720 | ||
1721 | struct option md_longopts[] = | |
1722 | { | |
e23eba97 NC |
1723 | {"march", required_argument, NULL, OPTION_MARCH}, |
1724 | {"fPIC", no_argument, NULL, OPTION_PIC}, | |
1725 | {"fpic", no_argument, NULL, OPTION_PIC}, | |
1726 | {"fno-pic", no_argument, NULL, OPTION_NO_PIC}, | |
2922d21d | 1727 | {"mabi", required_argument, NULL, OPTION_MABI}, |
e23eba97 NC |
1728 | |
1729 | {NULL, no_argument, NULL, 0} | |
1730 | }; | |
1731 | size_t md_longopts_size = sizeof (md_longopts); | |
1732 | ||
2922d21d AW |
1733 | enum float_abi { |
1734 | FLOAT_ABI_DEFAULT = -1, | |
1735 | FLOAT_ABI_SOFT, | |
1736 | FLOAT_ABI_SINGLE, | |
1737 | FLOAT_ABI_DOUBLE, | |
1738 | FLOAT_ABI_QUAD | |
e23eba97 | 1739 | }; |
2922d21d AW |
1740 | static enum float_abi float_abi = FLOAT_ABI_DEFAULT; |
1741 | ||
1742 | static void | |
1743 | riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi) | |
1744 | { | |
1745 | abi_xlen = new_xlen; | |
1746 | float_abi = new_float_abi; | |
1747 | } | |
e23eba97 NC |
1748 | |
1749 | int | |
1750 | md_parse_option (int c, const char *arg) | |
1751 | { | |
1752 | switch (c) | |
1753 | { | |
e23eba97 NC |
1754 | case OPTION_MARCH: |
1755 | riscv_set_arch (arg); | |
1756 | break; | |
1757 | ||
1758 | case OPTION_NO_PIC: | |
1759 | riscv_opts.pic = FALSE; | |
1760 | break; | |
1761 | ||
1762 | case OPTION_PIC: | |
1763 | riscv_opts.pic = TRUE; | |
1764 | break; | |
1765 | ||
2922d21d AW |
1766 | case OPTION_MABI: |
1767 | if (strcmp (arg, "ilp32") == 0) | |
1768 | riscv_set_abi (32, FLOAT_ABI_SOFT); | |
1769 | else if (strcmp (arg, "ilp32f") == 0) | |
1770 | riscv_set_abi (32, FLOAT_ABI_SINGLE); | |
1771 | else if (strcmp (arg, "ilp32d") == 0) | |
1772 | riscv_set_abi (32, FLOAT_ABI_DOUBLE); | |
1773 | else if (strcmp (arg, "ilp32q") == 0) | |
1774 | riscv_set_abi (32, FLOAT_ABI_QUAD); | |
1775 | else if (strcmp (arg, "lp64") == 0) | |
1776 | riscv_set_abi (64, FLOAT_ABI_SOFT); | |
1777 | else if (strcmp (arg, "lp64f") == 0) | |
1778 | riscv_set_abi (64, FLOAT_ABI_SINGLE); | |
1779 | else if (strcmp (arg, "lp64d") == 0) | |
1780 | riscv_set_abi (64, FLOAT_ABI_DOUBLE); | |
1781 | else if (strcmp (arg, "lp64q") == 0) | |
1782 | riscv_set_abi (64, FLOAT_ABI_QUAD); | |
1783 | else | |
1784 | return 0; | |
1785 | break; | |
1786 | ||
e23eba97 NC |
1787 | default: |
1788 | return 0; | |
1789 | } | |
1790 | ||
1791 | return 1; | |
1792 | } | |
1793 | ||
1794 | void | |
1795 | riscv_after_parse_args (void) | |
1796 | { | |
e23eba97 NC |
1797 | if (xlen == 0) |
1798 | { | |
1799 | if (strcmp (default_arch, "riscv32") == 0) | |
1800 | xlen = 32; | |
1801 | else if (strcmp (default_arch, "riscv64") == 0) | |
1802 | xlen = 64; | |
1803 | else | |
1804 | as_bad ("unknown default architecture `%s'", default_arch); | |
1805 | } | |
2922d21d AW |
1806 | |
1807 | if (riscv_subsets == NULL) | |
1808 | riscv_set_arch (xlen == 64 ? "rv64g" : "rv32g"); | |
1809 | ||
1810 | /* Add the RVC extension, regardless of -march, to support .option rvc. */ | |
1811 | if (riscv_subset_supports ("c")) | |
1812 | riscv_set_rvc (TRUE); | |
1813 | else | |
1814 | riscv_add_subset ("c"); | |
1815 | ||
1816 | /* Infer ABI from ISA if not specified on command line. */ | |
1817 | if (abi_xlen == 0) | |
1818 | abi_xlen = xlen; | |
1819 | else if (abi_xlen > xlen) | |
1820 | as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen); | |
1821 | else if (abi_xlen < xlen) | |
1822 | as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen); | |
1823 | ||
1824 | if (float_abi == FLOAT_ABI_DEFAULT) | |
1825 | { | |
1826 | struct riscv_subset *subset; | |
1827 | ||
1828 | /* Assume soft-float unless D extension is present. */ | |
1829 | float_abi = FLOAT_ABI_SOFT; | |
1830 | ||
1831 | for (subset = riscv_subsets; subset != NULL; subset = subset->next) | |
cc917fd9 KC |
1832 | { |
1833 | if (strcasecmp (subset->name, "D") == 0) | |
1834 | float_abi = FLOAT_ABI_DOUBLE; | |
1835 | if (strcasecmp (subset->name, "Q") == 0) | |
1836 | float_abi = FLOAT_ABI_QUAD; | |
1837 | } | |
2922d21d AW |
1838 | } |
1839 | ||
1840 | /* Insert float_abi into the EF_RISCV_FLOAT_ABI field of elf_flags. */ | |
1841 | elf_flags |= float_abi * (EF_RISCV_FLOAT_ABI & ~(EF_RISCV_FLOAT_ABI << 1)); | |
e23eba97 NC |
1842 | } |
1843 | ||
1844 | long | |
1845 | md_pcrel_from (fixS *fixP) | |
1846 | { | |
1847 | return fixP->fx_where + fixP->fx_frag->fr_address; | |
1848 | } | |
1849 | ||
1850 | /* Apply a fixup to the object file. */ | |
1851 | ||
1852 | void | |
1853 | md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) | |
1854 | { | |
45f76423 | 1855 | unsigned int subtype; |
e23eba97 | 1856 | bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where); |
45f76423 | 1857 | bfd_boolean relaxable = FALSE; |
c1b465c9 | 1858 | offsetT loc; |
e23eba97 NC |
1859 | |
1860 | /* Remember value for tc_gen_reloc. */ | |
1861 | fixP->fx_addnumber = *valP; | |
1862 | ||
1863 | switch (fixP->fx_r_type) | |
1864 | { | |
e23eba97 NC |
1865 | case BFD_RELOC_RISCV_HI20: |
1866 | case BFD_RELOC_RISCV_LO12_I: | |
1867 | case BFD_RELOC_RISCV_LO12_S: | |
45f76423 AW |
1868 | bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP) |
1869 | | bfd_getl32 (buf), buf); | |
a5ec5e3f AW |
1870 | if (fixP->fx_addsy == NULL) |
1871 | fixP->fx_done = TRUE; | |
45f76423 AW |
1872 | relaxable = TRUE; |
1873 | break; | |
1874 | ||
1875 | case BFD_RELOC_RISCV_GOT_HI20: | |
1876 | case BFD_RELOC_RISCV_PCREL_HI20: | |
e23eba97 NC |
1877 | case BFD_RELOC_RISCV_ADD8: |
1878 | case BFD_RELOC_RISCV_ADD16: | |
1879 | case BFD_RELOC_RISCV_ADD32: | |
1880 | case BFD_RELOC_RISCV_ADD64: | |
45f76423 | 1881 | case BFD_RELOC_RISCV_SUB6: |
e23eba97 NC |
1882 | case BFD_RELOC_RISCV_SUB8: |
1883 | case BFD_RELOC_RISCV_SUB16: | |
1884 | case BFD_RELOC_RISCV_SUB32: | |
1885 | case BFD_RELOC_RISCV_SUB64: | |
45f76423 AW |
1886 | case BFD_RELOC_RISCV_RELAX: |
1887 | break; | |
1888 | ||
1889 | case BFD_RELOC_RISCV_TPREL_HI20: | |
1890 | case BFD_RELOC_RISCV_TPREL_LO12_I: | |
1891 | case BFD_RELOC_RISCV_TPREL_LO12_S: | |
1892 | case BFD_RELOC_RISCV_TPREL_ADD: | |
1893 | relaxable = TRUE; | |
1894 | /* Fall through. */ | |
1895 | ||
1896 | case BFD_RELOC_RISCV_TLS_GOT_HI20: | |
1897 | case BFD_RELOC_RISCV_TLS_GD_HI20: | |
1898 | case BFD_RELOC_RISCV_TLS_DTPREL32: | |
1899 | case BFD_RELOC_RISCV_TLS_DTPREL64: | |
e294484e AW |
1900 | if (fixP->fx_addsy != NULL) |
1901 | S_SET_THREAD_LOCAL (fixP->fx_addsy); | |
1902 | else | |
1903 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
1904 | _("TLS relocation against a constant")); | |
e23eba97 NC |
1905 | break; |
1906 | ||
1907 | case BFD_RELOC_64: | |
1908 | case BFD_RELOC_32: | |
1909 | case BFD_RELOC_16: | |
1910 | case BFD_RELOC_8: | |
45f76423 | 1911 | case BFD_RELOC_RISCV_CFA: |
e23eba97 NC |
1912 | if (fixP->fx_addsy && fixP->fx_subsy) |
1913 | { | |
1914 | fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP)); | |
1915 | fixP->fx_next->fx_addsy = fixP->fx_subsy; | |
1916 | fixP->fx_next->fx_subsy = NULL; | |
1917 | fixP->fx_next->fx_offset = 0; | |
1918 | fixP->fx_subsy = NULL; | |
1919 | ||
1920 | switch (fixP->fx_r_type) | |
1921 | { | |
1922 | case BFD_RELOC_64: | |
1923 | fixP->fx_r_type = BFD_RELOC_RISCV_ADD64; | |
1924 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64; | |
1925 | break; | |
1926 | ||
1927 | case BFD_RELOC_32: | |
1928 | fixP->fx_r_type = BFD_RELOC_RISCV_ADD32; | |
1929 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32; | |
1930 | break; | |
1931 | ||
1932 | case BFD_RELOC_16: | |
1933 | fixP->fx_r_type = BFD_RELOC_RISCV_ADD16; | |
1934 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16; | |
1935 | break; | |
1936 | ||
1937 | case BFD_RELOC_8: | |
1938 | fixP->fx_r_type = BFD_RELOC_RISCV_ADD8; | |
1939 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8; | |
073808ed | 1940 | break; |
e23eba97 | 1941 | |
45f76423 AW |
1942 | case BFD_RELOC_RISCV_CFA: |
1943 | /* Load the byte to get the subtype. */ | |
c1b465c9 KLC |
1944 | subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]); |
1945 | loc = fixP->fx_frag->fr_fix - (subtype & 7); | |
45f76423 AW |
1946 | switch (subtype) |
1947 | { | |
1948 | case DW_CFA_advance_loc1: | |
c1b465c9 KLC |
1949 | fixP->fx_where = loc + 1; |
1950 | fixP->fx_next->fx_where = loc + 1; | |
45f76423 AW |
1951 | fixP->fx_r_type = BFD_RELOC_RISCV_SET8; |
1952 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8; | |
1953 | break; | |
1954 | ||
1955 | case DW_CFA_advance_loc2: | |
1956 | fixP->fx_size = 2; | |
45f76423 | 1957 | fixP->fx_next->fx_size = 2; |
c1b465c9 KLC |
1958 | fixP->fx_where = loc + 1; |
1959 | fixP->fx_next->fx_where = loc + 1; | |
45f76423 AW |
1960 | fixP->fx_r_type = BFD_RELOC_RISCV_SET16; |
1961 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16; | |
1962 | break; | |
1963 | ||
1964 | case DW_CFA_advance_loc4: | |
1965 | fixP->fx_size = 4; | |
45f76423 | 1966 | fixP->fx_next->fx_size = 4; |
c1b465c9 KLC |
1967 | fixP->fx_where = loc; |
1968 | fixP->fx_next->fx_where = loc; | |
45f76423 AW |
1969 | fixP->fx_r_type = BFD_RELOC_RISCV_SET32; |
1970 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32; | |
1971 | break; | |
1972 | ||
1973 | default: | |
1974 | if (subtype < 0x80 && (subtype & 0x40)) | |
1975 | { | |
1976 | /* DW_CFA_advance_loc */ | |
2aece2ba KLC |
1977 | fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode; |
1978 | fixP->fx_next->fx_frag = fixP->fx_frag; | |
45f76423 AW |
1979 | fixP->fx_r_type = BFD_RELOC_RISCV_SET6; |
1980 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6; | |
1981 | } | |
1982 | else | |
1983 | as_fatal (_("internal error: bad CFA value #%d"), subtype); | |
1984 | break; | |
1985 | } | |
1986 | break; | |
1987 | ||
e23eba97 NC |
1988 | default: |
1989 | /* This case is unreachable. */ | |
1990 | abort (); | |
1991 | } | |
1992 | } | |
1993 | /* Fall through. */ | |
1994 | ||
1995 | case BFD_RELOC_RVA: | |
1996 | /* If we are deleting this reloc entry, we must fill in the | |
1997 | value now. This can happen if we have a .word which is not | |
1998 | resolved when it appears but is later defined. */ | |
1999 | if (fixP->fx_addsy == NULL) | |
2000 | { | |
2001 | gas_assert (fixP->fx_size <= sizeof (valueT)); | |
2002 | md_number_to_chars ((char *) buf, *valP, fixP->fx_size); | |
2003 | fixP->fx_done = 1; | |
2004 | } | |
2005 | break; | |
2006 | ||
2007 | case BFD_RELOC_RISCV_JMP: | |
2008 | if (fixP->fx_addsy) | |
2009 | { | |
2010 | /* Fill in a tentative value to improve objdump readability. */ | |
2011 | bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP; | |
2012 | bfd_vma delta = target - md_pcrel_from (fixP); | |
2013 | bfd_putl32 (bfd_getl32 (buf) | ENCODE_UJTYPE_IMM (delta), buf); | |
2014 | } | |
2015 | break; | |
2016 | ||
2017 | case BFD_RELOC_12_PCREL: | |
2018 | if (fixP->fx_addsy) | |
2019 | { | |
2020 | /* Fill in a tentative value to improve objdump readability. */ | |
2021 | bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP; | |
2022 | bfd_vma delta = target - md_pcrel_from (fixP); | |
2023 | bfd_putl32 (bfd_getl32 (buf) | ENCODE_SBTYPE_IMM (delta), buf); | |
2024 | } | |
2025 | break; | |
2026 | ||
2027 | case BFD_RELOC_RISCV_RVC_BRANCH: | |
2028 | if (fixP->fx_addsy) | |
2029 | { | |
2030 | /* Fill in a tentative value to improve objdump readability. */ | |
2031 | bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP; | |
2032 | bfd_vma delta = target - md_pcrel_from (fixP); | |
2033 | bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_B_IMM (delta), buf); | |
2034 | } | |
2035 | break; | |
2036 | ||
2037 | case BFD_RELOC_RISCV_RVC_JUMP: | |
2038 | if (fixP->fx_addsy) | |
2039 | { | |
2040 | /* Fill in a tentative value to improve objdump readability. */ | |
2041 | bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP; | |
2042 | bfd_vma delta = target - md_pcrel_from (fixP); | |
2043 | bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_J_IMM (delta), buf); | |
2044 | } | |
2045 | break; | |
2046 | ||
e23eba97 NC |
2047 | case BFD_RELOC_RISCV_CALL: |
2048 | case BFD_RELOC_RISCV_CALL_PLT: | |
45f76423 AW |
2049 | relaxable = TRUE; |
2050 | break; | |
2051 | ||
2052 | case BFD_RELOC_RISCV_PCREL_LO12_S: | |
2053 | case BFD_RELOC_RISCV_PCREL_LO12_I: | |
e23eba97 NC |
2054 | case BFD_RELOC_RISCV_ALIGN: |
2055 | break; | |
2056 | ||
2057 | default: | |
2058 | /* We ignore generic BFD relocations we don't know about. */ | |
2059 | if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL) | |
2060 | as_fatal (_("internal error: bad relocation #%d"), fixP->fx_r_type); | |
2061 | } | |
45f76423 | 2062 | |
e294484e AW |
2063 | if (fixP->fx_subsy != NULL) |
2064 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
2065 | _("unsupported symbol subtraction")); | |
2066 | ||
45f76423 AW |
2067 | /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */ |
2068 | if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL) | |
2069 | { | |
2070 | fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP)); | |
2071 | fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL; | |
2072 | fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX; | |
2073 | } | |
2074 | } | |
2075 | ||
2076 | /* Because the value of .cfi_remember_state may changed after relaxation, | |
2077 | we insert a fix to relocate it again in link-time. */ | |
2078 | ||
2079 | void | |
2080 | riscv_pre_output_hook (void) | |
2081 | { | |
2082 | const frchainS *frch; | |
2083 | const asection *s; | |
2084 | ||
2085 | for (s = stdoutput->sections; s; s = s->next) | |
2086 | for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next) | |
2087 | { | |
7cb7b948 | 2088 | fragS *frag; |
45f76423 AW |
2089 | |
2090 | for (frag = frch->frch_root; frag; frag = frag->fr_next) | |
2091 | { | |
2092 | if (frag->fr_type == rs_cfa) | |
2093 | { | |
45f76423 AW |
2094 | expressionS exp; |
2095 | ||
2096 | symbolS *add_symbol = frag->fr_symbol->sy_value.X_add_symbol; | |
2097 | symbolS *op_symbol = frag->fr_symbol->sy_value.X_op_symbol; | |
2098 | ||
2099 | exp.X_op = O_subtract; | |
2100 | exp.X_add_symbol = add_symbol; | |
2101 | exp.X_add_number = 0; | |
2102 | exp.X_op_symbol = op_symbol; | |
2103 | ||
c1b465c9 | 2104 | fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0, |
45f76423 AW |
2105 | BFD_RELOC_RISCV_CFA); |
2106 | } | |
2107 | } | |
2108 | } | |
e23eba97 NC |
2109 | } |
2110 | ||
45f76423 | 2111 | |
e23eba97 NC |
2112 | /* This structure is used to hold a stack of .option values. */ |
2113 | ||
2114 | struct riscv_option_stack | |
2115 | { | |
2116 | struct riscv_option_stack *next; | |
2117 | struct riscv_set_options options; | |
2118 | }; | |
2119 | ||
2120 | static struct riscv_option_stack *riscv_opts_stack; | |
2121 | ||
2122 | /* Handle the .option pseudo-op. */ | |
2123 | ||
2124 | static void | |
2125 | s_riscv_option (int x ATTRIBUTE_UNUSED) | |
2126 | { | |
2127 | char *name = input_line_pointer, ch; | |
2128 | ||
2129 | while (!is_end_of_line[(unsigned char) *input_line_pointer]) | |
2130 | ++input_line_pointer; | |
2131 | ch = *input_line_pointer; | |
2132 | *input_line_pointer = '\0'; | |
2133 | ||
2134 | if (strcmp (name, "rvc") == 0) | |
2135 | riscv_set_rvc (TRUE); | |
2136 | else if (strcmp (name, "norvc") == 0) | |
2137 | riscv_set_rvc (FALSE); | |
2138 | else if (strcmp (name, "pic") == 0) | |
2139 | riscv_opts.pic = TRUE; | |
2140 | else if (strcmp (name, "nopic") == 0) | |
2141 | riscv_opts.pic = FALSE; | |
45f76423 AW |
2142 | else if (strcmp (name, "relax") == 0) |
2143 | riscv_opts.relax = TRUE; | |
2144 | else if (strcmp (name, "norelax") == 0) | |
2145 | riscv_opts.relax = FALSE; | |
e23eba97 NC |
2146 | else if (strcmp (name, "push") == 0) |
2147 | { | |
2148 | struct riscv_option_stack *s; | |
2149 | ||
2150 | s = (struct riscv_option_stack *) xmalloc (sizeof *s); | |
2151 | s->next = riscv_opts_stack; | |
2152 | s->options = riscv_opts; | |
2153 | riscv_opts_stack = s; | |
2154 | } | |
2155 | else if (strcmp (name, "pop") == 0) | |
2156 | { | |
2157 | struct riscv_option_stack *s; | |
2158 | ||
2159 | s = riscv_opts_stack; | |
2160 | if (s == NULL) | |
2161 | as_bad (_(".option pop with no .option push")); | |
2162 | else | |
2163 | { | |
2164 | riscv_opts = s->options; | |
2165 | riscv_opts_stack = s->next; | |
2166 | free (s); | |
2167 | } | |
2168 | } | |
2169 | else | |
2170 | { | |
2171 | as_warn (_("Unrecognized .option directive: %s\n"), name); | |
2172 | } | |
2173 | *input_line_pointer = ch; | |
2174 | demand_empty_rest_of_line (); | |
2175 | } | |
2176 | ||
2177 | /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate | |
2178 | a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for | |
2179 | use in DWARF debug information. */ | |
2180 | ||
2181 | static void | |
2182 | s_dtprel (int bytes) | |
2183 | { | |
2184 | expressionS ex; | |
2185 | char *p; | |
2186 | ||
2187 | expression (&ex); | |
2188 | ||
2189 | if (ex.X_op != O_symbol) | |
2190 | { | |
2191 | as_bad (_("Unsupported use of %s"), (bytes == 8 | |
2192 | ? ".dtpreldword" | |
2193 | : ".dtprelword")); | |
2194 | ignore_rest_of_line (); | |
2195 | } | |
2196 | ||
2197 | p = frag_more (bytes); | |
2198 | md_number_to_chars (p, 0, bytes); | |
2199 | fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, | |
2200 | (bytes == 8 | |
2201 | ? BFD_RELOC_RISCV_TLS_DTPREL64 | |
2202 | : BFD_RELOC_RISCV_TLS_DTPREL32)); | |
2203 | ||
2204 | demand_empty_rest_of_line (); | |
2205 | } | |
2206 | ||
2207 | /* Handle the .bss pseudo-op. */ | |
2208 | ||
2209 | static void | |
2210 | s_bss (int ignore ATTRIBUTE_UNUSED) | |
2211 | { | |
2212 | subseg_set (bss_section, 0); | |
2213 | demand_empty_rest_of_line (); | |
2214 | } | |
2215 | ||
e23eba97 | 2216 | static void |
d115ab8e | 2217 | riscv_make_nops (char *buf, bfd_vma bytes) |
e23eba97 | 2218 | { |
d115ab8e | 2219 | bfd_vma i = 0; |
e23eba97 | 2220 | |
e5b737de AW |
2221 | /* RISC-V instructions cannot begin or end on odd addresses, so this case |
2222 | means we are not within a valid instruction sequence. It is thus safe | |
2223 | to use a zero byte, even though that is not a valid instruction. */ | |
2224 | if (bytes % 2 == 1) | |
2225 | buf[i++] = 0; | |
2226 | ||
2227 | /* Use at most one 2-byte NOP. */ | |
2228 | if ((bytes - i) % 4 == 2) | |
e23eba97 | 2229 | { |
e5b737de | 2230 | md_number_to_chars (buf + i, RVC_NOP, 2); |
d115ab8e | 2231 | i += 2; |
e23eba97 NC |
2232 | } |
2233 | ||
e5b737de | 2234 | /* Fill the remainder with 4-byte NOPs. */ |
d115ab8e AW |
2235 | for ( ; i < bytes; i += 4) |
2236 | md_number_to_chars (buf + i, RISCV_NOP, 4); | |
2237 | } | |
e23eba97 | 2238 | |
d115ab8e AW |
2239 | /* Called from md_do_align. Used to create an alignment frag in a |
2240 | code section by emitting a worst-case NOP sequence that the linker | |
2241 | will later relax to the correct number of NOPs. We can't compute | |
2242 | the correct alignment now because of other linker relaxations. */ | |
2243 | ||
2244 | bfd_boolean | |
2245 | riscv_frag_align_code (int n) | |
2246 | { | |
e5b737de AW |
2247 | bfd_vma bytes = (bfd_vma) 1 << n; |
2248 | bfd_vma min_text_alignment_order = riscv_opts.rvc ? 1 : 2; | |
2249 | bfd_vma min_text_alignment = (bfd_vma) 1 << min_text_alignment_order; | |
2250 | ||
2251 | /* First, get back to minimal alignment. */ | |
2252 | frag_align_code (min_text_alignment_order, 0); | |
d115ab8e AW |
2253 | |
2254 | /* When not relaxing, riscv_handle_align handles code alignment. */ | |
2255 | if (!riscv_opts.relax) | |
2256 | return FALSE; | |
e23eba97 | 2257 | |
d115ab8e | 2258 | if (bytes > min_text_alignment) |
e23eba97 | 2259 | { |
d115ab8e | 2260 | bfd_vma worst_case_bytes = bytes - min_text_alignment; |
e23eba97 | 2261 | char *nops = frag_more (worst_case_bytes); |
e23eba97 | 2262 | expressionS ex; |
d115ab8e | 2263 | |
e23eba97 NC |
2264 | ex.X_op = O_constant; |
2265 | ex.X_add_number = worst_case_bytes; | |
2266 | ||
d115ab8e AW |
2267 | riscv_make_nops (nops, worst_case_bytes); |
2268 | ||
e23eba97 NC |
2269 | fix_new_exp (frag_now, nops - frag_now->fr_literal, 0, |
2270 | &ex, FALSE, BFD_RELOC_RISCV_ALIGN); | |
2271 | } | |
e23eba97 | 2272 | |
d115ab8e AW |
2273 | return TRUE; |
2274 | } | |
e23eba97 | 2275 | |
d115ab8e AW |
2276 | /* Implement HANDLE_ALIGN. */ |
2277 | ||
2278 | void | |
2279 | riscv_handle_align (fragS *fragP) | |
2280 | { | |
2281 | switch (fragP->fr_type) | |
2282 | { | |
2283 | case rs_align_code: | |
2284 | /* When relaxing, riscv_frag_align_code handles code alignment. */ | |
2285 | if (!riscv_opts.relax) | |
2286 | { | |
2287 | bfd_signed_vma count = fragP->fr_next->fr_address | |
2288 | - fragP->fr_address - fragP->fr_fix; | |
2289 | ||
2290 | if (count <= 0) | |
2291 | break; | |
2292 | ||
2293 | count &= MAX_MEM_FOR_RS_ALIGN_CODE; | |
2294 | riscv_make_nops (fragP->fr_literal + fragP->fr_fix, count); | |
2295 | fragP->fr_var = count; | |
2296 | } | |
2297 | break; | |
2298 | ||
2299 | default: | |
2300 | break; | |
2301 | } | |
e23eba97 NC |
2302 | } |
2303 | ||
2304 | int | |
2305 | md_estimate_size_before_relax (fragS *fragp, asection *segtype) | |
2306 | { | |
2307 | return (fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE)); | |
2308 | } | |
2309 | ||
2310 | /* Translate internal representation of relocation info to BFD target | |
2311 | format. */ | |
2312 | ||
2313 | arelent * | |
2314 | tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp) | |
2315 | { | |
2316 | arelent *reloc = (arelent *) xmalloc (sizeof (arelent)); | |
2317 | ||
2318 | reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); | |
2319 | *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); | |
2320 | reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
2321 | reloc->addend = fixp->fx_addnumber; | |
2322 | ||
2323 | reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
2324 | if (reloc->howto == NULL) | |
2325 | { | |
2326 | if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8) | |
2327 | && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL) | |
2328 | { | |
2329 | /* We don't have R_RISCV_8/16, but for this special case, | |
2330 | we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */ | |
2331 | return reloc; | |
2332 | } | |
2333 | ||
2334 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
2335 | _("cannot represent %s relocation in object file"), | |
2336 | bfd_get_reloc_code_name (fixp->fx_r_type)); | |
2337 | return NULL; | |
2338 | } | |
2339 | ||
2340 | return reloc; | |
2341 | } | |
2342 | ||
2343 | int | |
2344 | riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED) | |
2345 | { | |
2346 | if (RELAX_BRANCH_P (fragp->fr_subtype)) | |
2347 | { | |
2348 | offsetT old_var = fragp->fr_var; | |
2349 | fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE); | |
2350 | return fragp->fr_var - old_var; | |
2351 | } | |
2352 | ||
2353 | return 0; | |
2354 | } | |
2355 | ||
2356 | /* Expand far branches to multi-instruction sequences. */ | |
2357 | ||
2358 | static void | |
2359 | md_convert_frag_branch (fragS *fragp) | |
2360 | { | |
2361 | bfd_byte *buf; | |
2362 | expressionS exp; | |
2363 | fixS *fixp; | |
2364 | insn_t insn; | |
2365 | int rs1, reloc; | |
2366 | ||
2367 | buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix; | |
2368 | ||
2369 | exp.X_op = O_symbol; | |
2370 | exp.X_add_symbol = fragp->fr_symbol; | |
2371 | exp.X_add_number = fragp->fr_offset; | |
2372 | ||
2373 | gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype)); | |
2374 | ||
2375 | if (RELAX_BRANCH_RVC (fragp->fr_subtype)) | |
2376 | { | |
2377 | switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype)) | |
2378 | { | |
2379 | case 8: | |
2380 | case 4: | |
2381 | /* Expand the RVC branch into a RISC-V one. */ | |
2382 | insn = bfd_getl16 (buf); | |
2383 | rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S); | |
2384 | if ((insn & MASK_C_J) == MATCH_C_J) | |
2385 | insn = MATCH_JAL; | |
2386 | else if ((insn & MASK_C_JAL) == MATCH_C_JAL) | |
2387 | insn = MATCH_JAL | (X_RA << OP_SH_RD); | |
2388 | else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ) | |
2389 | insn = MATCH_BEQ | (rs1 << OP_SH_RS1); | |
2390 | else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ) | |
2391 | insn = MATCH_BNE | (rs1 << OP_SH_RS1); | |
2392 | else | |
2393 | abort (); | |
2394 | bfd_putl32 (insn, buf); | |
2395 | break; | |
2396 | ||
2397 | case 6: | |
2398 | /* Invert the branch condition. Branch over the jump. */ | |
2399 | insn = bfd_getl16 (buf); | |
2400 | insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ; | |
2401 | insn |= ENCODE_RVC_B_IMM (6); | |
2402 | bfd_putl16 (insn, buf); | |
2403 | buf += 2; | |
2404 | goto jump; | |
2405 | ||
2406 | case 2: | |
2407 | /* Just keep the RVC branch. */ | |
2408 | reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype) | |
2409 | ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH; | |
2410 | fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal, | |
2411 | 2, &exp, FALSE, reloc); | |
2412 | buf += 2; | |
2413 | goto done; | |
2414 | ||
2415 | default: | |
1d65abb5 | 2416 | abort (); |
e23eba97 NC |
2417 | } |
2418 | } | |
2419 | ||
2420 | switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype)) | |
2421 | { | |
2422 | case 8: | |
2423 | gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype)); | |
2424 | ||
2425 | /* Invert the branch condition. Branch over the jump. */ | |
2426 | insn = bfd_getl32 (buf); | |
2427 | insn ^= MATCH_BEQ ^ MATCH_BNE; | |
2428 | insn |= ENCODE_SBTYPE_IMM (8); | |
2429 | md_number_to_chars ((char *) buf, insn, 4); | |
2430 | buf += 4; | |
2431 | ||
2432 | jump: | |
2433 | /* Jump to the target. */ | |
2434 | fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal, | |
2435 | 4, &exp, FALSE, BFD_RELOC_RISCV_JMP); | |
2436 | md_number_to_chars ((char *) buf, MATCH_JAL, 4); | |
2437 | buf += 4; | |
2438 | break; | |
2439 | ||
2440 | case 4: | |
2441 | reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype) | |
2442 | ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL; | |
2443 | fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal, | |
2444 | 4, &exp, FALSE, reloc); | |
2445 | buf += 4; | |
2446 | break; | |
2447 | ||
2448 | default: | |
2449 | abort (); | |
2450 | } | |
2451 | ||
2452 | done: | |
2453 | fixp->fx_file = fragp->fr_file; | |
2454 | fixp->fx_line = fragp->fr_line; | |
2455 | ||
2456 | gas_assert (buf == (bfd_byte *)fragp->fr_literal | |
2457 | + fragp->fr_fix + fragp->fr_var); | |
2458 | ||
2459 | fragp->fr_fix += fragp->fr_var; | |
2460 | } | |
2461 | ||
2462 | /* Relax a machine dependent frag. This returns the amount by which | |
2463 | the current size of the frag should change. */ | |
2464 | ||
2465 | void | |
2466 | md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED, | |
2467 | fragS *fragp) | |
2468 | { | |
2469 | gas_assert (RELAX_BRANCH_P (fragp->fr_subtype)); | |
2470 | md_convert_frag_branch (fragp); | |
2471 | } | |
2472 | ||
2473 | void | |
2474 | md_show_usage (FILE *stream) | |
2475 | { | |
2476 | fprintf (stream, _("\ | |
2477 | RISC-V options:\n\ | |
e23eba97 NC |
2478 | -fpic generate position-independent code\n\ |
2479 | -fno-pic don't generate position-independent code (default)\n\ | |
19683c04 PD |
2480 | -march=ISA set the RISC-V architecture\n\ |
2481 | -mabi=ABI set the RISC-V ABI\n\ | |
e23eba97 NC |
2482 | ")); |
2483 | } | |
2484 | ||
2485 | /* Standard calling conventions leave the CFA at SP on entry. */ | |
2486 | void | |
2487 | riscv_cfi_frame_initial_instructions (void) | |
2488 | { | |
2489 | cfi_add_CFA_def_cfa_register (X_SP); | |
2490 | } | |
2491 | ||
2492 | int | |
2493 | tc_riscv_regname_to_dw2regnum (char *regname) | |
2494 | { | |
2495 | int reg; | |
2496 | ||
2497 | if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0) | |
2498 | return reg; | |
2499 | ||
2500 | if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0) | |
2501 | return reg + 32; | |
2502 | ||
2503 | as_bad (_("unknown register `%s'"), regname); | |
2504 | return -1; | |
2505 | } | |
2506 | ||
2507 | void | |
2508 | riscv_elf_final_processing (void) | |
2509 | { | |
e23eba97 | 2510 | elf_elfheader (stdoutput)->e_flags |= elf_flags; |
e23eba97 NC |
2511 | } |
2512 | ||
2513 | /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions, | |
2514 | since these directives break relaxation when used with symbol deltas. */ | |
2515 | ||
2516 | static void | |
2517 | s_riscv_leb128 (int sign) | |
2518 | { | |
2519 | expressionS exp; | |
2520 | char *save_in = input_line_pointer; | |
2521 | ||
2522 | expression (&exp); | |
2523 | if (exp.X_op != O_constant) | |
2524 | as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u'); | |
2525 | demand_empty_rest_of_line (); | |
2526 | ||
2527 | input_line_pointer = save_in; | |
2528 | return s_leb128 (sign); | |
2529 | } | |
2530 | ||
2531 | /* Pseudo-op table. */ | |
2532 | ||
2533 | static const pseudo_typeS riscv_pseudo_table[] = | |
2534 | { | |
2535 | /* RISC-V-specific pseudo-ops. */ | |
2536 | {"option", s_riscv_option, 0}, | |
2537 | {"half", cons, 2}, | |
2538 | {"word", cons, 4}, | |
2539 | {"dword", cons, 8}, | |
2540 | {"dtprelword", s_dtprel, 4}, | |
2541 | {"dtpreldword", s_dtprel, 8}, | |
2542 | {"bss", s_bss, 0}, | |
e23eba97 NC |
2543 | {"uleb128", s_riscv_leb128, 0}, |
2544 | {"sleb128", s_riscv_leb128, 1}, | |
2545 | ||
2546 | { NULL, NULL, 0 }, | |
2547 | }; | |
2548 | ||
2549 | void | |
2550 | riscv_pop_insert (void) | |
2551 | { | |
2552 | extern void pop_insert (const pseudo_typeS *); | |
2553 | ||
2554 | pop_insert (riscv_pseudo_table); | |
2555 | } |