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