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