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