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