| 1 | /* tc-i386.c -- Assemble Intel syntax code for ix86/x86-64 |
| 2 | Copyright (C) 2009-2020 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GAS, the GNU Assembler. |
| 5 | |
| 6 | GAS is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GAS is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GAS; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
| 19 | 02110-1301, USA. */ |
| 20 | |
| 21 | static struct |
| 22 | { |
| 23 | operatorT op_modifier; /* Operand modifier. */ |
| 24 | int is_mem; /* 1 if operand is memory reference. */ |
| 25 | int is_indirect; /* 1 if operand is indirect reference. */ |
| 26 | int has_offset; /* 1 if operand has offset. */ |
| 27 | unsigned int in_offset; /* >=1 if processing operand of offset. */ |
| 28 | unsigned int in_bracket; /* >=1 if processing operand in brackets. */ |
| 29 | unsigned int in_scale; /* >=1 if processing multiplication operand |
| 30 | * in brackets. */ |
| 31 | i386_operand_type reloc_types; /* Value obtained from lex_got(). */ |
| 32 | const reg_entry *base; /* Base register (if any). */ |
| 33 | const reg_entry *index; /* Index register (if any). */ |
| 34 | offsetT scale_factor; /* Accumulated scale factor. */ |
| 35 | symbolS *seg; |
| 36 | } |
| 37 | intel_state; |
| 38 | |
| 39 | /* offset X_add_symbol */ |
| 40 | #define O_offset O_md32 |
| 41 | /* offset X_add_symbol */ |
| 42 | #define O_short O_md31 |
| 43 | /* near ptr X_add_symbol */ |
| 44 | #define O_near_ptr O_md30 |
| 45 | /* far ptr X_add_symbol */ |
| 46 | #define O_far_ptr O_md29 |
| 47 | /* byte ptr X_add_symbol */ |
| 48 | #define O_byte_ptr O_md28 |
| 49 | /* word ptr X_add_symbol */ |
| 50 | #define O_word_ptr O_md27 |
| 51 | /* dword ptr X_add_symbol */ |
| 52 | #define O_dword_ptr O_md26 |
| 53 | /* qword ptr X_add_symbol */ |
| 54 | #define O_qword_ptr O_md25 |
| 55 | /* mmword ptr X_add_symbol */ |
| 56 | #define O_mmword_ptr O_qword_ptr |
| 57 | /* fword ptr X_add_symbol */ |
| 58 | #define O_fword_ptr O_md24 |
| 59 | /* tbyte ptr X_add_symbol */ |
| 60 | #define O_tbyte_ptr O_md23 |
| 61 | /* oword ptr X_add_symbol */ |
| 62 | #define O_oword_ptr O_md22 |
| 63 | /* xmmword ptr X_add_symbol */ |
| 64 | #define O_xmmword_ptr O_oword_ptr |
| 65 | /* ymmword ptr X_add_symbol */ |
| 66 | #define O_ymmword_ptr O_md21 |
| 67 | /* zmmword ptr X_add_symbol */ |
| 68 | #define O_zmmword_ptr O_md20 |
| 69 | |
| 70 | static struct |
| 71 | { |
| 72 | const char *name; |
| 73 | operatorT op; |
| 74 | unsigned int operands; |
| 75 | } |
| 76 | const i386_operators[] = |
| 77 | { |
| 78 | { "and", O_bit_and, 2 }, |
| 79 | { "eq", O_eq, 2 }, |
| 80 | { "ge", O_ge, 2 }, |
| 81 | { "gt", O_gt, 2 }, |
| 82 | { "le", O_le, 2 }, |
| 83 | { "lt", O_lt, 2 }, |
| 84 | { "mod", O_modulus, 2 }, |
| 85 | { "ne", O_ne, 2 }, |
| 86 | { "not", O_bit_not, 1 }, |
| 87 | { "offset", O_offset, 1 }, |
| 88 | { "or", O_bit_inclusive_or, 2 }, |
| 89 | { "shl", O_left_shift, 2 }, |
| 90 | { "short", O_short, 1 }, |
| 91 | { "shr", O_right_shift, 2 }, |
| 92 | { "xor", O_bit_exclusive_or, 2 }, |
| 93 | { NULL, O_illegal, 0 } |
| 94 | }; |
| 95 | |
| 96 | static struct |
| 97 | { |
| 98 | const char *name; |
| 99 | operatorT op; |
| 100 | unsigned short sz[3]; |
| 101 | } |
| 102 | const i386_types[] = |
| 103 | { |
| 104 | #define I386_TYPE(t, n) { #t, O_##t##_ptr, { n, n, n } } |
| 105 | I386_TYPE(byte, 1), |
| 106 | I386_TYPE(word, 2), |
| 107 | I386_TYPE(dword, 4), |
| 108 | I386_TYPE(fword, 6), |
| 109 | I386_TYPE(qword, 8), |
| 110 | I386_TYPE(mmword, 8), |
| 111 | I386_TYPE(tbyte, 10), |
| 112 | I386_TYPE(oword, 16), |
| 113 | I386_TYPE(xmmword, 16), |
| 114 | I386_TYPE(ymmword, 32), |
| 115 | I386_TYPE(zmmword, 64), |
| 116 | #undef I386_TYPE |
| 117 | { "near", O_near_ptr, { 0xff04, 0xff02, 0xff08 } }, |
| 118 | { "far", O_far_ptr, { 0xff06, 0xff05, 0xff06 } }, |
| 119 | { NULL, O_illegal, { 0, 0, 0 } } |
| 120 | }; |
| 121 | |
| 122 | operatorT i386_operator (const char *name, unsigned int operands, char *pc) |
| 123 | { |
| 124 | unsigned int j; |
| 125 | |
| 126 | if (!intel_syntax) |
| 127 | return O_absent; |
| 128 | |
| 129 | if (!name) |
| 130 | { |
| 131 | if (operands != 2) |
| 132 | return O_illegal; |
| 133 | switch (*input_line_pointer) |
| 134 | { |
| 135 | case ':': |
| 136 | ++input_line_pointer; |
| 137 | return O_full_ptr; |
| 138 | case '[': |
| 139 | ++input_line_pointer; |
| 140 | return O_index; |
| 141 | case '@': |
| 142 | if (this_operand >= 0 && i.reloc[this_operand] == NO_RELOC) |
| 143 | { |
| 144 | int adjust = 0; |
| 145 | char *gotfree_input_line = lex_got (&i.reloc[this_operand], |
| 146 | &adjust, |
| 147 | &intel_state.reloc_types); |
| 148 | |
| 149 | if (!gotfree_input_line) |
| 150 | break; |
| 151 | free (gotfree_input_line); |
| 152 | *input_line_pointer++ = '+'; |
| 153 | memset (input_line_pointer, '0', adjust - 1); |
| 154 | input_line_pointer[adjust - 1] = ' '; |
| 155 | return O_add; |
| 156 | } |
| 157 | break; |
| 158 | } |
| 159 | return O_illegal; |
| 160 | } |
| 161 | |
| 162 | for (j = 0; i386_operators[j].name; ++j) |
| 163 | if (strcasecmp (i386_operators[j].name, name) == 0) |
| 164 | { |
| 165 | if (i386_operators[j].operands |
| 166 | && i386_operators[j].operands != operands) |
| 167 | return O_illegal; |
| 168 | return i386_operators[j].op; |
| 169 | } |
| 170 | |
| 171 | for (j = 0; i386_types[j].name; ++j) |
| 172 | if (strcasecmp (i386_types[j].name, name) == 0) |
| 173 | break; |
| 174 | |
| 175 | if (i386_types[j].name && *pc == ' ') |
| 176 | { |
| 177 | char *pname; |
| 178 | char c; |
| 179 | |
| 180 | ++input_line_pointer; |
| 181 | c = get_symbol_name (&pname); |
| 182 | |
| 183 | if (strcasecmp (pname, "ptr") == 0) |
| 184 | { |
| 185 | /* FIXME: What if c == '"' ? */ |
| 186 | pname[-1] = *pc; |
| 187 | *pc = c; |
| 188 | if (intel_syntax > 0 || operands != 1) |
| 189 | return O_illegal; |
| 190 | return i386_types[j].op; |
| 191 | } |
| 192 | |
| 193 | (void) restore_line_pointer (c); |
| 194 | input_line_pointer = pname - 1; |
| 195 | } |
| 196 | |
| 197 | return O_absent; |
| 198 | } |
| 199 | |
| 200 | static int i386_intel_parse_name (const char *name, expressionS *e) |
| 201 | { |
| 202 | unsigned int j; |
| 203 | |
| 204 | if (! strcmp (name, "$")) |
| 205 | { |
| 206 | current_location (e); |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | for (j = 0; i386_types[j].name; ++j) |
| 211 | if (strcasecmp(i386_types[j].name, name) == 0) |
| 212 | { |
| 213 | e->X_op = O_constant; |
| 214 | e->X_add_number = i386_types[j].sz[flag_code]; |
| 215 | e->X_add_symbol = NULL; |
| 216 | e->X_op_symbol = NULL; |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static INLINE int i386_intel_check (const reg_entry *rreg, |
| 224 | const reg_entry *base, |
| 225 | const reg_entry *iindex) |
| 226 | { |
| 227 | if ((this_operand >= 0 |
| 228 | && rreg != i.op[this_operand].regs) |
| 229 | || base != intel_state.base |
| 230 | || iindex != intel_state.index) |
| 231 | { |
| 232 | as_bad (_("invalid use of register")); |
| 233 | return 0; |
| 234 | } |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | static INLINE void i386_intel_fold (expressionS *e, symbolS *sym) |
| 239 | { |
| 240 | expressionS *exp = symbol_get_value_expression (sym); |
| 241 | if (S_GET_SEGMENT (sym) == absolute_section) |
| 242 | { |
| 243 | offsetT val = e->X_add_number; |
| 244 | |
| 245 | *e = *exp; |
| 246 | e->X_add_number += val; |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | if (exp->X_op == O_symbol |
| 251 | && strcmp (S_GET_NAME (exp->X_add_symbol), |
| 252 | GLOBAL_OFFSET_TABLE_NAME) == 0) |
| 253 | sym = exp->X_add_symbol; |
| 254 | e->X_add_symbol = sym; |
| 255 | e->X_op_symbol = NULL; |
| 256 | e->X_op = O_symbol; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | i386_intel_simplify_register (expressionS *e) |
| 262 | { |
| 263 | int reg_num; |
| 264 | |
| 265 | if (this_operand < 0 || intel_state.in_offset) |
| 266 | { |
| 267 | as_bad (_("invalid use of register")); |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | if (e->X_op == O_register) |
| 272 | reg_num = e->X_add_number; |
| 273 | else |
| 274 | reg_num = e->X_md - 1; |
| 275 | |
| 276 | if (reg_num < 0 || reg_num >= (int) i386_regtab_size) |
| 277 | { |
| 278 | as_bad (_("invalid register number")); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | if (!intel_state.in_bracket) |
| 283 | { |
| 284 | if (i.op[this_operand].regs) |
| 285 | { |
| 286 | as_bad (_("invalid use of register")); |
| 287 | return 0; |
| 288 | } |
| 289 | if (i386_regtab[reg_num].reg_type.bitfield.class == SReg |
| 290 | && i386_regtab[reg_num].reg_num == RegFlat) |
| 291 | { |
| 292 | as_bad (_("invalid use of pseudo-register")); |
| 293 | return 0; |
| 294 | } |
| 295 | i.op[this_operand].regs = i386_regtab + reg_num; |
| 296 | } |
| 297 | else if (!intel_state.index |
| 298 | && (i386_regtab[reg_num].reg_type.bitfield.xmmword |
| 299 | || i386_regtab[reg_num].reg_type.bitfield.ymmword |
| 300 | || i386_regtab[reg_num].reg_type.bitfield.zmmword |
| 301 | || i386_regtab[reg_num].reg_num == RegIZ)) |
| 302 | intel_state.index = i386_regtab + reg_num; |
| 303 | else if (!intel_state.base && !intel_state.in_scale) |
| 304 | intel_state.base = i386_regtab + reg_num; |
| 305 | else if (!intel_state.index) |
| 306 | { |
| 307 | if (intel_state.in_scale |
| 308 | || current_templates->start->base_opcode == 0xf30f1b /* bndmk */ |
| 309 | || (current_templates->start->base_opcode & ~1) == 0x0f1a /* bnd{ld,st}x */ |
| 310 | || i386_regtab[reg_num].reg_type.bitfield.baseindex) |
| 311 | intel_state.index = i386_regtab + reg_num; |
| 312 | else |
| 313 | { |
| 314 | /* Convert base to index and make ESP/RSP the base. */ |
| 315 | intel_state.index = intel_state.base; |
| 316 | intel_state.base = i386_regtab + reg_num; |
| 317 | } |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | /* esp is invalid as index */ |
| 322 | intel_state.index = i386_regtab + REGNAM_EAX + ESP_REG_NUM; |
| 323 | } |
| 324 | return 2; |
| 325 | } |
| 326 | |
| 327 | static int i386_intel_simplify (expressionS *); |
| 328 | |
| 329 | static INLINE int i386_intel_simplify_symbol(symbolS *sym) |
| 330 | { |
| 331 | int ret = i386_intel_simplify (symbol_get_value_expression (sym)); |
| 332 | |
| 333 | if (ret == 2) |
| 334 | { |
| 335 | S_SET_SEGMENT(sym, absolute_section); |
| 336 | ret = 1; |
| 337 | } |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static int i386_intel_simplify (expressionS *e) |
| 342 | { |
| 343 | const reg_entry *the_reg = (this_operand >= 0 |
| 344 | ? i.op[this_operand].regs : NULL); |
| 345 | const reg_entry *base = intel_state.base; |
| 346 | const reg_entry *state_index = intel_state.index; |
| 347 | int ret; |
| 348 | |
| 349 | if (!intel_syntax) |
| 350 | return 1; |
| 351 | |
| 352 | switch (e->X_op) |
| 353 | { |
| 354 | case O_index: |
| 355 | if (e->X_add_symbol) |
| 356 | { |
| 357 | if (!i386_intel_simplify_symbol (e->X_add_symbol) |
| 358 | || !i386_intel_check(the_reg, intel_state.base, |
| 359 | intel_state.index)) |
| 360 | return 0; |
| 361 | } |
| 362 | if (!intel_state.in_offset) |
| 363 | ++intel_state.in_bracket; |
| 364 | ret = i386_intel_simplify_symbol (e->X_op_symbol); |
| 365 | if (!intel_state.in_offset) |
| 366 | --intel_state.in_bracket; |
| 367 | if (!ret) |
| 368 | return 0; |
| 369 | if (e->X_add_symbol) |
| 370 | e->X_op = O_add; |
| 371 | else |
| 372 | i386_intel_fold (e, e->X_op_symbol); |
| 373 | break; |
| 374 | |
| 375 | case O_offset: |
| 376 | intel_state.has_offset = 1; |
| 377 | ++intel_state.in_offset; |
| 378 | ret = i386_intel_simplify_symbol (e->X_add_symbol); |
| 379 | --intel_state.in_offset; |
| 380 | if (!ret || !i386_intel_check(the_reg, base, state_index)) |
| 381 | return 0; |
| 382 | i386_intel_fold (e, e->X_add_symbol); |
| 383 | return ret; |
| 384 | |
| 385 | case O_byte_ptr: |
| 386 | case O_word_ptr: |
| 387 | case O_dword_ptr: |
| 388 | case O_fword_ptr: |
| 389 | case O_qword_ptr: /* O_mmword_ptr */ |
| 390 | case O_tbyte_ptr: |
| 391 | case O_oword_ptr: /* O_xmmword_ptr */ |
| 392 | case O_ymmword_ptr: |
| 393 | case O_zmmword_ptr: |
| 394 | case O_near_ptr: |
| 395 | case O_far_ptr: |
| 396 | if (intel_state.op_modifier == O_absent) |
| 397 | intel_state.op_modifier = e->X_op; |
| 398 | /* FALLTHROUGH */ |
| 399 | case O_short: |
| 400 | if (symbol_get_value_expression (e->X_add_symbol)->X_op |
| 401 | == O_register) |
| 402 | { |
| 403 | as_bad (_("invalid use of register")); |
| 404 | return 0; |
| 405 | } |
| 406 | if (!i386_intel_simplify_symbol (e->X_add_symbol)) |
| 407 | return 0; |
| 408 | i386_intel_fold (e, e->X_add_symbol); |
| 409 | break; |
| 410 | |
| 411 | case O_full_ptr: |
| 412 | if (symbol_get_value_expression (e->X_op_symbol)->X_op |
| 413 | == O_register) |
| 414 | { |
| 415 | as_bad (_("invalid use of register")); |
| 416 | return 0; |
| 417 | } |
| 418 | if (!i386_intel_simplify_symbol (e->X_op_symbol) |
| 419 | || !i386_intel_check(the_reg, intel_state.base, |
| 420 | intel_state.index)) |
| 421 | return 0; |
| 422 | if (!intel_state.in_offset) |
| 423 | { |
| 424 | if (!intel_state.seg) |
| 425 | intel_state.seg = e->X_add_symbol; |
| 426 | else |
| 427 | { |
| 428 | expressionS exp; |
| 429 | |
| 430 | exp.X_op = O_full_ptr; |
| 431 | exp.X_add_symbol = e->X_add_symbol; |
| 432 | exp.X_op_symbol = intel_state.seg; |
| 433 | intel_state.seg = make_expr_symbol (&exp); |
| 434 | } |
| 435 | } |
| 436 | i386_intel_fold (e, e->X_op_symbol); |
| 437 | break; |
| 438 | |
| 439 | case O_multiply: |
| 440 | if (this_operand >= 0 && intel_state.in_bracket) |
| 441 | { |
| 442 | expressionS *scale = NULL; |
| 443 | int has_index = (intel_state.index != NULL); |
| 444 | |
| 445 | if (!intel_state.in_scale++) |
| 446 | intel_state.scale_factor = 1; |
| 447 | |
| 448 | ret = i386_intel_simplify_symbol (e->X_add_symbol); |
| 449 | if (ret && !has_index && intel_state.index) |
| 450 | scale = symbol_get_value_expression (e->X_op_symbol); |
| 451 | |
| 452 | if (ret) |
| 453 | ret = i386_intel_simplify_symbol (e->X_op_symbol); |
| 454 | if (ret && !scale && !has_index && intel_state.index) |
| 455 | scale = symbol_get_value_expression (e->X_add_symbol); |
| 456 | |
| 457 | if (ret && scale) |
| 458 | { |
| 459 | resolve_expression (scale); |
| 460 | if (scale->X_op != O_constant |
| 461 | || intel_state.index->reg_type.bitfield.word) |
| 462 | scale->X_add_number = 0; |
| 463 | intel_state.scale_factor *= scale->X_add_number; |
| 464 | } |
| 465 | |
| 466 | --intel_state.in_scale; |
| 467 | if (!ret) |
| 468 | return 0; |
| 469 | |
| 470 | if (!intel_state.in_scale) |
| 471 | switch (intel_state.scale_factor) |
| 472 | { |
| 473 | case 1: |
| 474 | i.log2_scale_factor = 0; |
| 475 | break; |
| 476 | case 2: |
| 477 | i.log2_scale_factor = 1; |
| 478 | break; |
| 479 | case 4: |
| 480 | i.log2_scale_factor = 2; |
| 481 | break; |
| 482 | case 8: |
| 483 | i.log2_scale_factor = 3; |
| 484 | break; |
| 485 | default: |
| 486 | /* esp is invalid as index */ |
| 487 | intel_state.index = i386_regtab + REGNAM_EAX + ESP_REG_NUM; |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | break; |
| 492 | } |
| 493 | goto fallthrough; |
| 494 | |
| 495 | case O_register: |
| 496 | ret = i386_intel_simplify_register (e); |
| 497 | if (ret == 2) |
| 498 | { |
| 499 | gas_assert (e->X_add_number < (unsigned short) -1); |
| 500 | e->X_md = (unsigned short) e->X_add_number + 1; |
| 501 | e->X_op = O_constant; |
| 502 | e->X_add_number = 0; |
| 503 | } |
| 504 | return ret; |
| 505 | |
| 506 | case O_constant: |
| 507 | if (e->X_md) |
| 508 | return i386_intel_simplify_register (e); |
| 509 | |
| 510 | /* FALLTHROUGH */ |
| 511 | default: |
| 512 | fallthrough: |
| 513 | if (e->X_add_symbol |
| 514 | && !i386_intel_simplify_symbol (e->X_add_symbol)) |
| 515 | return 0; |
| 516 | if (e->X_op == O_add || e->X_op == O_subtract) |
| 517 | { |
| 518 | base = intel_state.base; |
| 519 | state_index = intel_state.index; |
| 520 | } |
| 521 | if (!i386_intel_check (the_reg, base, state_index) |
| 522 | || (e->X_op_symbol |
| 523 | && !i386_intel_simplify_symbol (e->X_op_symbol)) |
| 524 | || !i386_intel_check (the_reg, |
| 525 | (e->X_op != O_add |
| 526 | ? base : intel_state.base), |
| 527 | (e->X_op != O_add |
| 528 | ? state_index : intel_state.index))) |
| 529 | return 0; |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | if (this_operand >= 0 |
| 534 | && e->X_op == O_symbol |
| 535 | && !intel_state.in_offset) |
| 536 | { |
| 537 | segT seg = S_GET_SEGMENT (e->X_add_symbol); |
| 538 | |
| 539 | if (seg != absolute_section |
| 540 | && seg != reg_section |
| 541 | && seg != expr_section) |
| 542 | intel_state.is_mem |= 2 - !intel_state.in_bracket; |
| 543 | } |
| 544 | |
| 545 | return 1; |
| 546 | } |
| 547 | |
| 548 | int i386_need_index_operator (void) |
| 549 | { |
| 550 | return intel_syntax < 0; |
| 551 | } |
| 552 | |
| 553 | static int |
| 554 | i386_intel_operand (char *operand_string, int got_a_float) |
| 555 | { |
| 556 | char *saved_input_line_pointer, *buf; |
| 557 | segT exp_seg; |
| 558 | expressionS exp, *expP; |
| 559 | char suffix = 0; |
| 560 | int ret; |
| 561 | |
| 562 | /* Handle vector immediates. */ |
| 563 | if (RC_SAE_immediate (operand_string)) |
| 564 | return 1; |
| 565 | |
| 566 | /* Initialize state structure. */ |
| 567 | intel_state.op_modifier = O_absent; |
| 568 | intel_state.is_mem = 0; |
| 569 | intel_state.is_indirect = 0; |
| 570 | intel_state.has_offset = 0; |
| 571 | intel_state.base = NULL; |
| 572 | intel_state.index = NULL; |
| 573 | intel_state.seg = NULL; |
| 574 | operand_type_set (&intel_state.reloc_types, ~0); |
| 575 | gas_assert (!intel_state.in_offset); |
| 576 | gas_assert (!intel_state.in_bracket); |
| 577 | gas_assert (!intel_state.in_scale); |
| 578 | |
| 579 | saved_input_line_pointer = input_line_pointer; |
| 580 | input_line_pointer = buf = xstrdup (operand_string); |
| 581 | |
| 582 | intel_syntax = -1; |
| 583 | memset (&exp, 0, sizeof(exp)); |
| 584 | exp_seg = expression (&exp); |
| 585 | ret = i386_intel_simplify (&exp); |
| 586 | intel_syntax = 1; |
| 587 | |
| 588 | SKIP_WHITESPACE (); |
| 589 | |
| 590 | /* Handle vector operations. */ |
| 591 | if (*input_line_pointer == '{') |
| 592 | { |
| 593 | char *end = check_VecOperations (input_line_pointer, NULL); |
| 594 | if (end) |
| 595 | input_line_pointer = end; |
| 596 | else |
| 597 | ret = 0; |
| 598 | } |
| 599 | |
| 600 | if (!is_end_of_line[(unsigned char) *input_line_pointer]) |
| 601 | { |
| 602 | if (ret) |
| 603 | as_bad (_("junk `%s' after expression"), input_line_pointer); |
| 604 | ret = 0; |
| 605 | } |
| 606 | else if (exp.X_op == O_illegal || exp.X_op == O_absent) |
| 607 | { |
| 608 | if (ret) |
| 609 | as_bad (_("invalid expression")); |
| 610 | ret = 0; |
| 611 | } |
| 612 | else if (!intel_state.has_offset |
| 613 | && input_line_pointer > buf |
| 614 | && *(input_line_pointer - 1) == ']') |
| 615 | { |
| 616 | intel_state.is_mem |= 1; |
| 617 | intel_state.is_indirect = 1; |
| 618 | } |
| 619 | |
| 620 | input_line_pointer = saved_input_line_pointer; |
| 621 | free (buf); |
| 622 | |
| 623 | gas_assert (!intel_state.in_offset); |
| 624 | gas_assert (!intel_state.in_bracket); |
| 625 | gas_assert (!intel_state.in_scale); |
| 626 | |
| 627 | if (!ret) |
| 628 | return 0; |
| 629 | |
| 630 | if (intel_state.op_modifier != O_absent |
| 631 | && current_templates->start->base_opcode != 0x8d /* lea */) |
| 632 | { |
| 633 | i.types[this_operand].bitfield.unspecified = 0; |
| 634 | |
| 635 | switch (intel_state.op_modifier) |
| 636 | { |
| 637 | case O_byte_ptr: |
| 638 | i.types[this_operand].bitfield.byte = 1; |
| 639 | suffix = BYTE_MNEM_SUFFIX; |
| 640 | break; |
| 641 | |
| 642 | case O_word_ptr: |
| 643 | i.types[this_operand].bitfield.word = 1; |
| 644 | if (got_a_float == 2) /* "fi..." */ |
| 645 | suffix = SHORT_MNEM_SUFFIX; |
| 646 | else |
| 647 | suffix = WORD_MNEM_SUFFIX; |
| 648 | break; |
| 649 | |
| 650 | case O_dword_ptr: |
| 651 | i.types[this_operand].bitfield.dword = 1; |
| 652 | if ((current_templates->start->name[0] == 'l' |
| 653 | && current_templates->start->name[2] == 's' |
| 654 | && current_templates->start->name[3] == 0) |
| 655 | || current_templates->start->base_opcode == 0x62 /* bound */) |
| 656 | suffix = WORD_MNEM_SUFFIX; |
| 657 | else if (flag_code != CODE_32BIT |
| 658 | && (current_templates->start->opcode_modifier.jump == JUMP |
| 659 | || current_templates->start->opcode_modifier.jump |
| 660 | == JUMP_DWORD)) |
| 661 | suffix = flag_code == CODE_16BIT ? LONG_DOUBLE_MNEM_SUFFIX |
| 662 | : WORD_MNEM_SUFFIX; |
| 663 | else if (got_a_float == 1) /* "f..." */ |
| 664 | suffix = SHORT_MNEM_SUFFIX; |
| 665 | else |
| 666 | suffix = LONG_MNEM_SUFFIX; |
| 667 | break; |
| 668 | |
| 669 | case O_fword_ptr: |
| 670 | i.types[this_operand].bitfield.fword = 1; |
| 671 | if (current_templates->start->name[0] == 'l' |
| 672 | && current_templates->start->name[2] == 's' |
| 673 | && current_templates->start->name[3] == 0) |
| 674 | suffix = LONG_MNEM_SUFFIX; |
| 675 | else if (!got_a_float) |
| 676 | { |
| 677 | if (flag_code == CODE_16BIT) |
| 678 | add_prefix (DATA_PREFIX_OPCODE); |
| 679 | suffix = LONG_DOUBLE_MNEM_SUFFIX; |
| 680 | } |
| 681 | break; |
| 682 | |
| 683 | case O_qword_ptr: /* O_mmword_ptr */ |
| 684 | i.types[this_operand].bitfield.qword = 1; |
| 685 | if (current_templates->start->base_opcode == 0x62 /* bound */ |
| 686 | || got_a_float == 1) /* "f..." */ |
| 687 | suffix = LONG_MNEM_SUFFIX; |
| 688 | else |
| 689 | suffix = QWORD_MNEM_SUFFIX; |
| 690 | break; |
| 691 | |
| 692 | case O_tbyte_ptr: |
| 693 | i.types[this_operand].bitfield.tbyte = 1; |
| 694 | if (got_a_float == 1) |
| 695 | suffix = LONG_DOUBLE_MNEM_SUFFIX; |
| 696 | else if ((current_templates->start->operand_types[0].bitfield.fword |
| 697 | || current_templates->start->operand_types[0].bitfield.tbyte |
| 698 | || current_templates->start->opcode_modifier.jump == JUMP_DWORD |
| 699 | || current_templates->start->opcode_modifier.jump == JUMP) |
| 700 | && flag_code == CODE_64BIT) |
| 701 | suffix = QWORD_MNEM_SUFFIX; /* l[fgs]s, [ls][gi]dt, call, jmp */ |
| 702 | else |
| 703 | i.types[this_operand].bitfield.byte = 1; /* cause an error */ |
| 704 | break; |
| 705 | |
| 706 | case O_oword_ptr: /* O_xmmword_ptr */ |
| 707 | i.types[this_operand].bitfield.xmmword = 1; |
| 708 | break; |
| 709 | |
| 710 | case O_ymmword_ptr: |
| 711 | i.types[this_operand].bitfield.ymmword = 1; |
| 712 | break; |
| 713 | |
| 714 | case O_zmmword_ptr: |
| 715 | i.types[this_operand].bitfield.zmmword = 1; |
| 716 | break; |
| 717 | |
| 718 | case O_far_ptr: |
| 719 | suffix = LONG_DOUBLE_MNEM_SUFFIX; |
| 720 | /* FALLTHROUGH */ |
| 721 | case O_near_ptr: |
| 722 | if (current_templates->start->opcode_modifier.jump != JUMP |
| 723 | && current_templates->start->opcode_modifier.jump != JUMP_DWORD) |
| 724 | { |
| 725 | /* cause an error */ |
| 726 | i.types[this_operand].bitfield.byte = 1; |
| 727 | i.types[this_operand].bitfield.tbyte = 1; |
| 728 | suffix = i.suffix; |
| 729 | } |
| 730 | break; |
| 731 | |
| 732 | default: |
| 733 | BAD_CASE (intel_state.op_modifier); |
| 734 | break; |
| 735 | } |
| 736 | |
| 737 | if (!i.suffix) |
| 738 | i.suffix = suffix; |
| 739 | else if (i.suffix != suffix) |
| 740 | { |
| 741 | as_bad (_("conflicting operand size modifiers")); |
| 742 | return 0; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /* Operands for jump/call need special consideration. */ |
| 747 | if (current_templates->start->opcode_modifier.jump == JUMP |
| 748 | || current_templates->start->opcode_modifier.jump == JUMP_DWORD |
| 749 | || current_templates->start->opcode_modifier.jump == JUMP_INTERSEGMENT) |
| 750 | { |
| 751 | bfd_boolean jumpabsolute = FALSE; |
| 752 | |
| 753 | if (i.op[this_operand].regs |
| 754 | || intel_state.base |
| 755 | || intel_state.index |
| 756 | || intel_state.is_mem > 1) |
| 757 | jumpabsolute = TRUE; |
| 758 | else |
| 759 | switch (intel_state.op_modifier) |
| 760 | { |
| 761 | case O_near_ptr: |
| 762 | if (intel_state.seg) |
| 763 | jumpabsolute = TRUE; |
| 764 | else |
| 765 | intel_state.is_mem = 1; |
| 766 | break; |
| 767 | case O_far_ptr: |
| 768 | case O_absent: |
| 769 | if (!intel_state.seg) |
| 770 | { |
| 771 | intel_state.is_mem = 1; |
| 772 | if (intel_state.op_modifier == O_absent) |
| 773 | { |
| 774 | if (intel_state.is_indirect == 1) |
| 775 | jumpabsolute = TRUE; |
| 776 | break; |
| 777 | } |
| 778 | as_bad (_("cannot infer the segment part of the operand")); |
| 779 | return 0; |
| 780 | } |
| 781 | else if (S_GET_SEGMENT (intel_state.seg) == reg_section) |
| 782 | jumpabsolute = TRUE; |
| 783 | else |
| 784 | { |
| 785 | i386_operand_type types; |
| 786 | |
| 787 | if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS) |
| 788 | { |
| 789 | as_bad (_("at most %d immediate operands are allowed"), |
| 790 | MAX_IMMEDIATE_OPERANDS); |
| 791 | return 0; |
| 792 | } |
| 793 | expP = &im_expressions[i.imm_operands++]; |
| 794 | memset (expP, 0, sizeof(*expP)); |
| 795 | expP->X_op = O_symbol; |
| 796 | expP->X_add_symbol = intel_state.seg; |
| 797 | i.op[this_operand].imms = expP; |
| 798 | |
| 799 | resolve_expression (expP); |
| 800 | operand_type_set (&types, ~0); |
| 801 | if (!i386_finalize_immediate (S_GET_SEGMENT (intel_state.seg), |
| 802 | expP, types, operand_string)) |
| 803 | return 0; |
| 804 | if (i.operands < MAX_OPERANDS) |
| 805 | { |
| 806 | this_operand = i.operands++; |
| 807 | i.types[this_operand].bitfield.unspecified = 1; |
| 808 | } |
| 809 | if (suffix == LONG_DOUBLE_MNEM_SUFFIX) |
| 810 | i.suffix = 0; |
| 811 | intel_state.seg = NULL; |
| 812 | intel_state.is_mem = 0; |
| 813 | } |
| 814 | break; |
| 815 | default: |
| 816 | jumpabsolute = TRUE; |
| 817 | break; |
| 818 | } |
| 819 | if (jumpabsolute) |
| 820 | { |
| 821 | i.jumpabsolute = TRUE; |
| 822 | intel_state.is_mem |= 1; |
| 823 | } |
| 824 | } |
| 825 | else if (intel_state.seg) |
| 826 | intel_state.is_mem |= 1; |
| 827 | |
| 828 | if (i.op[this_operand].regs) |
| 829 | { |
| 830 | i386_operand_type temp; |
| 831 | |
| 832 | /* Register operand. */ |
| 833 | if (intel_state.base || intel_state.index || intel_state.seg) |
| 834 | { |
| 835 | as_bad (_("invalid operand")); |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | temp = i.op[this_operand].regs->reg_type; |
| 840 | temp.bitfield.baseindex = 0; |
| 841 | i.types[this_operand] = operand_type_or (i.types[this_operand], |
| 842 | temp); |
| 843 | i.types[this_operand].bitfield.unspecified = 0; |
| 844 | ++i.reg_operands; |
| 845 | } |
| 846 | else if (intel_state.base |
| 847 | || intel_state.index |
| 848 | || intel_state.seg |
| 849 | || intel_state.is_mem) |
| 850 | { |
| 851 | /* Memory operand. */ |
| 852 | if (i.mem_operands == 1 && !maybe_adjust_templates ()) |
| 853 | return 0; |
| 854 | if ((int) i.mem_operands |
| 855 | >= 2 - !current_templates->start->opcode_modifier.isstring) |
| 856 | { |
| 857 | /* Handle |
| 858 | |
| 859 | call 0x9090,0x90909090 |
| 860 | lcall 0x9090,0x90909090 |
| 861 | jmp 0x9090,0x90909090 |
| 862 | ljmp 0x9090,0x90909090 |
| 863 | */ |
| 864 | |
| 865 | if ((current_templates->start->opcode_modifier.jump == JUMP_INTERSEGMENT |
| 866 | || current_templates->start->opcode_modifier.jump == JUMP_DWORD |
| 867 | || current_templates->start->opcode_modifier.jump == JUMP) |
| 868 | && this_operand == 1 |
| 869 | && intel_state.seg == NULL |
| 870 | && i.mem_operands == 1 |
| 871 | && i.disp_operands == 1 |
| 872 | && intel_state.op_modifier == O_absent) |
| 873 | { |
| 874 | /* Try to process the first operand as immediate, */ |
| 875 | this_operand = 0; |
| 876 | if (i386_finalize_immediate (exp_seg, i.op[0].imms, |
| 877 | intel_state.reloc_types, |
| 878 | NULL)) |
| 879 | { |
| 880 | this_operand = 1; |
| 881 | expP = &im_expressions[0]; |
| 882 | i.op[this_operand].imms = expP; |
| 883 | *expP = exp; |
| 884 | |
| 885 | /* Try to process the second operand as immediate, */ |
| 886 | if (i386_finalize_immediate (exp_seg, expP, |
| 887 | intel_state.reloc_types, |
| 888 | NULL)) |
| 889 | { |
| 890 | i.mem_operands = 0; |
| 891 | i.disp_operands = 0; |
| 892 | i.imm_operands = 2; |
| 893 | i.flags[0] &= ~Operand_Mem; |
| 894 | i.types[0].bitfield.disp16 = 0; |
| 895 | i.types[0].bitfield.disp32 = 0; |
| 896 | i.types[0].bitfield.disp32s = 0; |
| 897 | return 1; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | as_bad (_("too many memory references for `%s'"), |
| 903 | current_templates->start->name); |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | /* Swap base and index in 16-bit memory operands like |
| 908 | [si+bx]. Since i386_index_check is also used in AT&T |
| 909 | mode we have to do this here. */ |
| 910 | if (intel_state.base |
| 911 | && intel_state.index |
| 912 | && intel_state.base->reg_type.bitfield.word |
| 913 | && intel_state.index->reg_type.bitfield.word |
| 914 | && intel_state.base->reg_num >= 6 |
| 915 | && intel_state.index->reg_num < 6) |
| 916 | { |
| 917 | i.base_reg = intel_state.index; |
| 918 | i.index_reg = intel_state.base; |
| 919 | } |
| 920 | else |
| 921 | { |
| 922 | i.base_reg = intel_state.base; |
| 923 | i.index_reg = intel_state.index; |
| 924 | } |
| 925 | |
| 926 | if (i.base_reg || i.index_reg) |
| 927 | i.types[this_operand].bitfield.baseindex = 1; |
| 928 | |
| 929 | expP = &disp_expressions[i.disp_operands]; |
| 930 | memcpy (expP, &exp, sizeof(exp)); |
| 931 | resolve_expression (expP); |
| 932 | |
| 933 | if (expP->X_op != O_constant |
| 934 | || expP->X_add_number |
| 935 | || !i.types[this_operand].bitfield.baseindex) |
| 936 | { |
| 937 | i.op[this_operand].disps = expP; |
| 938 | i.disp_operands++; |
| 939 | |
| 940 | i386_addressing_mode (); |
| 941 | |
| 942 | if (flag_code == CODE_64BIT) |
| 943 | { |
| 944 | if (!i.prefix[ADDR_PREFIX]) |
| 945 | { |
| 946 | i.types[this_operand].bitfield.disp64 = 1; |
| 947 | i.types[this_operand].bitfield.disp32s = 1; |
| 948 | } |
| 949 | else |
| 950 | i.types[this_operand].bitfield.disp32 = 1; |
| 951 | } |
| 952 | else if (!i.prefix[ADDR_PREFIX] ^ (flag_code == CODE_16BIT)) |
| 953 | i.types[this_operand].bitfield.disp32 = 1; |
| 954 | else |
| 955 | i.types[this_operand].bitfield.disp16 = 1; |
| 956 | |
| 957 | #if defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT) |
| 958 | /* |
| 959 | * exp_seg is used only for verification in |
| 960 | * i386_finalize_displacement, and we can end up seeing reg_section |
| 961 | * here - but we know we removed all registers from the expression |
| 962 | * (or error-ed on any remaining ones) in i386_intel_simplify. I |
| 963 | * consider the check in i386_finalize_displacement bogus anyway, in |
| 964 | * particular because it doesn't allow for expr_section, so I'd |
| 965 | * rather see that check (and the similar one in |
| 966 | * i386_finalize_immediate) use SEG_NORMAL(), but not being an a.out |
| 967 | * expert I can't really say whether that would have other bad side |
| 968 | * effects. |
| 969 | */ |
| 970 | if (OUTPUT_FLAVOR == bfd_target_aout_flavour |
| 971 | && exp_seg == reg_section) |
| 972 | exp_seg = expP->X_op != O_constant ? undefined_section |
| 973 | : absolute_section; |
| 974 | #endif |
| 975 | |
| 976 | if (!i386_finalize_displacement (exp_seg, expP, |
| 977 | intel_state.reloc_types, |
| 978 | operand_string)) |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | if (intel_state.seg) |
| 983 | { |
| 984 | for (ret = check_none; ; ret = operand_check) |
| 985 | { |
| 986 | expP = symbol_get_value_expression (intel_state.seg); |
| 987 | if (expP->X_op != O_full_ptr |
| 988 | || symbol_get_value_expression (expP->X_op_symbol)->X_op |
| 989 | != O_register) |
| 990 | break; |
| 991 | intel_state.seg = expP->X_add_symbol; |
| 992 | } |
| 993 | if (expP->X_op != O_register) |
| 994 | { |
| 995 | as_bad (_("segment register name expected")); |
| 996 | return 0; |
| 997 | } |
| 998 | if (i386_regtab[expP->X_add_number].reg_type.bitfield.class != SReg) |
| 999 | { |
| 1000 | as_bad (_("invalid use of register")); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | switch (ret) |
| 1004 | { |
| 1005 | case check_error: |
| 1006 | as_bad (_("redundant segment overrides")); |
| 1007 | return 0; |
| 1008 | case check_warning: |
| 1009 | as_warn (_("redundant segment overrides")); |
| 1010 | break; |
| 1011 | } |
| 1012 | switch (i386_regtab[expP->X_add_number].reg_num) |
| 1013 | { |
| 1014 | case 0: i.seg[i.mem_operands] = &es; break; |
| 1015 | case 1: i.seg[i.mem_operands] = &cs; break; |
| 1016 | case 2: i.seg[i.mem_operands] = &ss; break; |
| 1017 | case 3: i.seg[i.mem_operands] = &ds; break; |
| 1018 | case 4: i.seg[i.mem_operands] = &fs; break; |
| 1019 | case 5: i.seg[i.mem_operands] = &gs; break; |
| 1020 | case RegFlat: i.seg[i.mem_operands] = NULL; break; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | if (!i386_index_check (operand_string)) |
| 1025 | return 0; |
| 1026 | |
| 1027 | i.flags[this_operand] |= Operand_Mem; |
| 1028 | if (i.mem_operands == 0) |
| 1029 | i.memop1_string = xstrdup (operand_string); |
| 1030 | ++i.mem_operands; |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | /* Immediate. */ |
| 1035 | if (i.imm_operands >= MAX_IMMEDIATE_OPERANDS) |
| 1036 | { |
| 1037 | as_bad (_("at most %d immediate operands are allowed"), |
| 1038 | MAX_IMMEDIATE_OPERANDS); |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | expP = &im_expressions[i.imm_operands++]; |
| 1043 | i.op[this_operand].imms = expP; |
| 1044 | *expP = exp; |
| 1045 | |
| 1046 | return i386_finalize_immediate (exp_seg, expP, intel_state.reloc_types, |
| 1047 | operand_string); |
| 1048 | } |
| 1049 | |
| 1050 | return 1; |
| 1051 | } |