tc-i386-intel.c 26 KB

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