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