Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* expr.c -operands, expressions- |
250d07de | 2 | Copyright (C) 1987-2021 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
6 | GAS is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
ec2655a6 | 8 | the Free Software Foundation; either version 3, or (at your option) |
252b5132 RH |
9 | any later version. |
10 | ||
11 | GAS is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GAS; see the file COPYING. If not, write to the Free | |
4b4da160 NC |
18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 | 20 | |
929b12bc KH |
21 | /* This is really a branch office of as-read.c. I split it out to clearly |
22 | distinguish the world of expressions from the world of statements. | |
23 | (It also gives smaller files to re-compile.) | |
24 | Here, "operand"s are of expressions, not instructions. */ | |
252b5132 | 25 | |
252b5132 RH |
26 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
27 | ||
28 | #include "as.h" | |
3882b010 | 29 | #include "safe-ctype.h" |
252b5132 | 30 | |
d85733c8 | 31 | #include <limits.h> |
d85733c8 JB |
32 | #ifndef CHAR_BIT |
33 | #define CHAR_BIT 8 | |
34 | #endif | |
35 | ||
5b7c81bd | 36 | bool literal_prefix_dollar_hex = false; |
e2d15955 | 37 | |
dd625418 | 38 | static void clean_up_expression (expressionS * expressionP); |
252b5132 | 39 | |
252b5132 RH |
40 | /* We keep a mapping of expression symbols to file positions, so that |
41 | we can provide better error messages. */ | |
42 | ||
e6c774b4 | 43 | struct expr_symbol_line { |
252b5132 RH |
44 | struct expr_symbol_line *next; |
45 | symbolS *sym; | |
3b4dbbbf | 46 | const char *file; |
252b5132 RH |
47 | unsigned int line; |
48 | }; | |
49 | ||
50 | static struct expr_symbol_line *expr_symbol_lines; | |
51 | \f | |
52 | /* Build a dummy symbol to hold a complex expression. This is how we | |
53 | build expressions up out of other expressions. The symbol is put | |
54 | into the fake section expr_section. */ | |
55 | ||
56 | symbolS * | |
dd625418 | 57 | make_expr_symbol (expressionS *expressionP) |
252b5132 RH |
58 | { |
59 | expressionS zero; | |
252b5132 RH |
60 | symbolS *symbolP; |
61 | struct expr_symbol_line *n; | |
62 | ||
63 | if (expressionP->X_op == O_symbol | |
64 | && expressionP->X_add_number == 0) | |
65 | return expressionP->X_add_symbol; | |
66 | ||
67 | if (expressionP->X_op == O_big) | |
68 | { | |
69 | /* This won't work, because the actual value is stored in | |
02000999 AM |
70 | generic_floating_point_number or generic_bignum, and we are |
71 | going to lose it if we haven't already. */ | |
252b5132 | 72 | if (expressionP->X_add_number > 0) |
0e389e77 | 73 | as_bad (_("bignum invalid")); |
252b5132 | 74 | else |
0e389e77 | 75 | as_bad (_("floating point number invalid")); |
252b5132 RH |
76 | zero.X_op = O_constant; |
77 | zero.X_add_number = 0; | |
78 | zero.X_unsigned = 0; | |
956a6ba3 | 79 | zero.X_extrabit = 0; |
252b5132 RH |
80 | clean_up_expression (&zero); |
81 | expressionP = &zero; | |
82 | } | |
83 | ||
252b5132 RH |
84 | /* Putting constant symbols in absolute_section rather than |
85 | expr_section is convenient for the old a.out code, for which | |
86 | S_GET_SEGMENT does not always retrieve the value put in by | |
87 | S_SET_SEGMENT. */ | |
756d1d01 | 88 | symbolP = symbol_create (FAKE_LABEL_NAME, |
252b5132 RH |
89 | (expressionP->X_op == O_constant |
90 | ? absolute_section | |
5a918ce7 JB |
91 | : expressionP->X_op == O_register |
92 | ? reg_section | |
93 | : expr_section), | |
e01e1cee | 94 | &zero_address_frag, 0); |
49309057 | 95 | symbol_set_value_expression (symbolP, expressionP); |
252b5132 RH |
96 | |
97 | if (expressionP->X_op == O_constant) | |
6386f3a7 | 98 | resolve_symbol_value (symbolP); |
252b5132 | 99 | |
325801bd | 100 | n = XNEW (struct expr_symbol_line); |
252b5132 | 101 | n->sym = symbolP; |
3b4dbbbf | 102 | n->file = as_where (&n->line); |
252b5132 RH |
103 | n->next = expr_symbol_lines; |
104 | expr_symbol_lines = n; | |
105 | ||
106 | return symbolP; | |
107 | } | |
108 | ||
109 | /* Return the file and line number for an expr symbol. Return | |
110 | non-zero if something was found, 0 if no information is known for | |
111 | the symbol. */ | |
112 | ||
113 | int | |
3b4dbbbf | 114 | expr_symbol_where (symbolS *sym, const char **pfile, unsigned int *pline) |
252b5132 | 115 | { |
ed9e98c2 | 116 | struct expr_symbol_line *l; |
252b5132 RH |
117 | |
118 | for (l = expr_symbol_lines; l != NULL; l = l->next) | |
119 | { | |
120 | if (l->sym == sym) | |
121 | { | |
122 | *pfile = l->file; | |
123 | *pline = l->line; | |
124 | return 1; | |
125 | } | |
126 | } | |
127 | ||
128 | return 0; | |
129 | } | |
130 | \f | |
131 | /* Utilities for building expressions. | |
132 | Since complex expressions are recorded as symbols for use in other | |
133 | expressions these return a symbolS * and not an expressionS *. | |
134 | These explicitly do not take an "add_number" argument. */ | |
135 | /* ??? For completeness' sake one might want expr_build_symbol. | |
136 | It would just return its argument. */ | |
137 | ||
138 | /* Build an expression for an unsigned constant. | |
139 | The corresponding one for signed constants is missing because | |
140 | there's currently no need for it. One could add an unsigned_p flag | |
141 | but that seems more clumsy. */ | |
142 | ||
143 | symbolS * | |
dd625418 | 144 | expr_build_uconstant (offsetT value) |
252b5132 RH |
145 | { |
146 | expressionS e; | |
147 | ||
148 | e.X_op = O_constant; | |
149 | e.X_add_number = value; | |
150 | e.X_unsigned = 1; | |
956a6ba3 | 151 | e.X_extrabit = 0; |
252b5132 RH |
152 | return make_expr_symbol (&e); |
153 | } | |
154 | ||
252b5132 RH |
155 | /* Build an expression for the current location ('.'). */ |
156 | ||
157 | symbolS * | |
dd625418 | 158 | expr_build_dot (void) |
252b5132 RH |
159 | { |
160 | expressionS e; | |
161 | ||
162 | current_location (&e); | |
e66a3432 | 163 | return symbol_clone_if_forward_ref (make_expr_symbol (&e)); |
252b5132 RH |
164 | } |
165 | \f | |
929b12bc KH |
166 | /* Build any floating-point literal here. |
167 | Also build any bignum literal here. */ | |
252b5132 RH |
168 | |
169 | /* Seems atof_machine can backscan through generic_bignum and hit whatever | |
170 | happens to be loaded before it in memory. And its way too complicated | |
171 | for me to fix right. Thus a hack. JF: Just make generic_bignum bigger, | |
172 | and never write into the early words, thus they'll always be zero. | |
173 | I hate Dean's floating-point code. Bleh. */ | |
174 | LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6]; | |
e6c774b4 KH |
175 | |
176 | FLONUM_TYPE generic_floating_point_number = { | |
bc4466dc KH |
177 | &generic_bignum[6], /* low. (JF: Was 0) */ |
178 | &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */ | |
179 | 0, /* leader. */ | |
180 | 0, /* exponent. */ | |
181 | 0 /* sign. */ | |
252b5132 | 182 | }; |
929b12bc | 183 | |
252b5132 RH |
184 | \f |
185 | static void | |
dd625418 | 186 | floating_constant (expressionS *expressionP) |
252b5132 | 187 | { |
929b12bc | 188 | /* input_line_pointer -> floating-point constant. */ |
252b5132 RH |
189 | int error_code; |
190 | ||
191 | error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS, | |
192 | &generic_floating_point_number); | |
193 | ||
194 | if (error_code) | |
195 | { | |
196 | if (error_code == ERROR_EXPONENT_OVERFLOW) | |
197 | { | |
0e389e77 | 198 | as_bad (_("bad floating-point constant: exponent overflow")); |
252b5132 RH |
199 | } |
200 | else | |
201 | { | |
0e389e77 AM |
202 | as_bad (_("bad floating-point constant: unknown error code=%d"), |
203 | error_code); | |
252b5132 RH |
204 | } |
205 | } | |
206 | expressionP->X_op = O_big; | |
929b12bc KH |
207 | /* input_line_pointer -> just after constant, which may point to |
208 | whitespace. */ | |
252b5132 RH |
209 | expressionP->X_add_number = -1; |
210 | } | |
211 | ||
32d71569 | 212 | uint32_t |
dd625418 | 213 | generic_bignum_to_int32 (void) |
252b5132 | 214 | { |
32d71569 AM |
215 | return ((((uint32_t) generic_bignum[1] & LITTLENUM_MASK) |
216 | << LITTLENUM_NUMBER_OF_BITS) | |
217 | | ((uint32_t) generic_bignum[0] & LITTLENUM_MASK)); | |
252b5132 RH |
218 | } |
219 | ||
32d71569 | 220 | uint64_t |
dd625418 | 221 | generic_bignum_to_int64 (void) |
252b5132 | 222 | { |
32d71569 AM |
223 | return ((((((((uint64_t) generic_bignum[3] & LITTLENUM_MASK) |
224 | << LITTLENUM_NUMBER_OF_BITS) | |
225 | | ((uint64_t) generic_bignum[2] & LITTLENUM_MASK)) | |
226 | << LITTLENUM_NUMBER_OF_BITS) | |
227 | | ((uint64_t) generic_bignum[1] & LITTLENUM_MASK)) | |
228 | << LITTLENUM_NUMBER_OF_BITS) | |
229 | | ((uint64_t) generic_bignum[0] & LITTLENUM_MASK)); | |
252b5132 | 230 | } |
252b5132 RH |
231 | |
232 | static void | |
dd625418 | 233 | integer_constant (int radix, expressionS *expressionP) |
252b5132 | 234 | { |
929b12bc | 235 | char *start; /* Start of number. */ |
252b5132 RH |
236 | char *suffix = NULL; |
237 | char c; | |
929b12bc KH |
238 | valueT number; /* Offset or (absolute) value. */ |
239 | short int digit; /* Value of next digit in current radix. */ | |
240 | short int maxdig = 0; /* Highest permitted digit value. */ | |
241 | int too_many_digits = 0; /* If we see >= this number of. */ | |
242 | char *name; /* Points to name of symbol. */ | |
243 | symbolS *symbolP; /* Points to symbol. */ | |
252b5132 | 244 | |
929b12bc | 245 | int small; /* True if fits in 32 bits. */ |
252b5132 | 246 | |
929b12bc | 247 | /* May be bignum, or may fit in 32 bits. */ |
252b5132 RH |
248 | /* Most numbers fit into 32 bits, and we want this case to be fast. |
249 | so we pretend it will fit into 32 bits. If, after making up a 32 | |
250 | bit number, we realise that we have scanned more digits than | |
251 | comfortably fit into 32 bits, we re-scan the digits coding them | |
252 | into a bignum. For decimal and octal numbers we are | |
253 | conservative: Some numbers may be assumed bignums when in fact | |
254 | they do fit into 32 bits. Numbers of any radix can have excess | |
255 | leading zeros: We strive to recognise this and cast them back | |
256 | into 32 bits. We must check that the bignum really is more than | |
257 | 32 bits, and change it back to a 32-bit number if it fits. The | |
258 | number we are looking for is expected to be positive, but if it | |
259 | fits into 32 bits as an unsigned number, we let it be a 32-bit | |
929b12bc | 260 | number. The cavalier approach is for speed in ordinary cases. */ |
252b5132 RH |
261 | /* This has been extended for 64 bits. We blindly assume that if |
262 | you're compiling in 64-bit mode, the target is a 64-bit machine. | |
263 | This should be cleaned up. */ | |
264 | ||
265 | #ifdef BFD64 | |
266 | #define valuesize 64 | |
267 | #else /* includes non-bfd case, mostly */ | |
268 | #define valuesize 32 | |
269 | #endif | |
270 | ||
c14c7a8a AM |
271 | if (is_end_of_line[(unsigned char) *input_line_pointer]) |
272 | { | |
273 | expressionP->X_op = O_absent; | |
274 | return; | |
275 | } | |
276 | ||
f805106c | 277 | if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0) |
252b5132 RH |
278 | { |
279 | int flt = 0; | |
280 | ||
281 | /* In MRI mode, the number may have a suffix indicating the | |
02000999 AM |
282 | radix. For that matter, it might actually be a floating |
283 | point constant. */ | |
3882b010 | 284 | for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++) |
252b5132 RH |
285 | { |
286 | if (*suffix == 'e' || *suffix == 'E') | |
287 | flt = 1; | |
288 | } | |
289 | ||
290 | if (suffix == input_line_pointer) | |
291 | { | |
292 | radix = 10; | |
293 | suffix = NULL; | |
294 | } | |
295 | else | |
296 | { | |
297 | c = *--suffix; | |
3882b010 | 298 | c = TOUPPER (c); |
fee9cbc7 AM |
299 | /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB, |
300 | we distinguish between 'B' and 'b'. This is the case for | |
301 | Z80. */ | |
302 | if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B') | |
252b5132 RH |
303 | radix = 2; |
304 | else if (c == 'D') | |
305 | radix = 10; | |
306 | else if (c == 'O' || c == 'Q') | |
307 | radix = 8; | |
308 | else if (c == 'H') | |
309 | radix = 16; | |
310 | else if (suffix[1] == '.' || c == 'E' || flt) | |
311 | { | |
312 | floating_constant (expressionP); | |
313 | return; | |
314 | } | |
315 | else | |
316 | { | |
317 | radix = 10; | |
318 | suffix = NULL; | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | switch (radix) | |
324 | { | |
325 | case 2: | |
326 | maxdig = 2; | |
327 | too_many_digits = valuesize + 1; | |
328 | break; | |
329 | case 8: | |
330 | maxdig = radix = 8; | |
331 | too_many_digits = (valuesize + 2) / 3 + 1; | |
332 | break; | |
333 | case 16: | |
334 | maxdig = radix = 16; | |
335 | too_many_digits = (valuesize + 3) / 4 + 1; | |
336 | break; | |
337 | case 10: | |
338 | maxdig = radix = 10; | |
929b12bc | 339 | too_many_digits = (valuesize + 11) / 4; /* Very rough. */ |
252b5132 RH |
340 | } |
341 | #undef valuesize | |
342 | start = input_line_pointer; | |
343 | c = *input_line_pointer++; | |
344 | for (number = 0; | |
345 | (digit = hex_value (c)) < maxdig; | |
346 | c = *input_line_pointer++) | |
347 | { | |
348 | number = number * radix + digit; | |
349 | } | |
929b12bc KH |
350 | /* c contains character after number. */ |
351 | /* input_line_pointer->char after c. */ | |
252b5132 RH |
352 | small = (input_line_pointer - start - 1) < too_many_digits; |
353 | ||
929b12bc | 354 | if (radix == 16 && c == '_') |
252b5132 RH |
355 | { |
356 | /* This is literal of the form 0x333_0_12345678_1. | |
02000999 | 357 | This example is equivalent to 0x00000333000000001234567800000001. */ |
252b5132 RH |
358 | |
359 | int num_little_digits = 0; | |
360 | int i; | |
929b12bc | 361 | input_line_pointer = start; /* -> 1st digit. */ |
252b5132 RH |
362 | |
363 | know (LITTLENUM_NUMBER_OF_BITS == 16); | |
364 | ||
929b12bc | 365 | for (c = '_'; c == '_'; num_little_digits += 2) |
252b5132 RH |
366 | { |
367 | ||
929b12bc KH |
368 | /* Convert one 64-bit word. */ |
369 | int ndigit = 0; | |
252b5132 RH |
370 | number = 0; |
371 | for (c = *input_line_pointer++; | |
372 | (digit = hex_value (c)) < maxdig; | |
373 | c = *(input_line_pointer++)) | |
374 | { | |
375 | number = number * radix + digit; | |
376 | ndigit++; | |
377 | } | |
378 | ||
379 | /* Check for 8 digit per word max. */ | |
929b12bc | 380 | if (ndigit > 8) |
0e389e77 | 381 | as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word")); |
252b5132 | 382 | |
929b12bc KH |
383 | /* Add this chunk to the bignum. |
384 | Shift things down 2 little digits. */ | |
252b5132 | 385 | know (LITTLENUM_NUMBER_OF_BITS == 16); |
929b12bc KH |
386 | for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1); |
387 | i >= 2; | |
388 | i--) | |
389 | generic_bignum[i] = generic_bignum[i - 2]; | |
252b5132 | 390 | |
929b12bc | 391 | /* Add the new digits as the least significant new ones. */ |
252b5132 RH |
392 | generic_bignum[0] = number & 0xffffffff; |
393 | generic_bignum[1] = number >> 16; | |
394 | } | |
395 | ||
929b12bc | 396 | /* Again, c is char after number, input_line_pointer->after c. */ |
252b5132 RH |
397 | |
398 | if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1) | |
399 | num_little_digits = SIZE_OF_LARGE_NUMBER - 1; | |
400 | ||
9c2799c2 | 401 | gas_assert (num_little_digits >= 4); |
252b5132 RH |
402 | |
403 | if (num_little_digits != 8) | |
0e389e77 | 404 | as_bad (_("a bignum with underscores must have exactly 4 words")); |
252b5132 RH |
405 | |
406 | /* We might have some leading zeros. These can be trimmed to give | |
929b12bc KH |
407 | us a change to fit this constant into a small number. */ |
408 | while (generic_bignum[num_little_digits - 1] == 0 | |
409 | && num_little_digits > 1) | |
252b5132 | 410 | num_little_digits--; |
929b12bc | 411 | |
252b5132 RH |
412 | if (num_little_digits <= 2) |
413 | { | |
929b12bc | 414 | /* will fit into 32 bits. */ |
252b5132 RH |
415 | number = generic_bignum_to_int32 (); |
416 | small = 1; | |
417 | } | |
418 | #ifdef BFD64 | |
419 | else if (num_little_digits <= 4) | |
420 | { | |
421 | /* Will fit into 64 bits. */ | |
422 | number = generic_bignum_to_int64 (); | |
423 | small = 1; | |
424 | } | |
425 | #endif | |
426 | else | |
427 | { | |
428 | small = 0; | |
929b12bc KH |
429 | |
430 | /* Number of littlenums in the bignum. */ | |
431 | number = num_little_digits; | |
252b5132 RH |
432 | } |
433 | } | |
434 | else if (!small) | |
435 | { | |
929b12bc KH |
436 | /* We saw a lot of digits. manufacture a bignum the hard way. */ |
437 | LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */ | |
438 | LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */ | |
252b5132 RH |
439 | long carry; |
440 | ||
441 | leader = generic_bignum; | |
442 | generic_bignum[0] = 0; | |
443 | generic_bignum[1] = 0; | |
444 | generic_bignum[2] = 0; | |
445 | generic_bignum[3] = 0; | |
929b12bc | 446 | input_line_pointer = start; /* -> 1st digit. */ |
252b5132 | 447 | c = *input_line_pointer++; |
929b12bc | 448 | for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++) |
252b5132 | 449 | { |
929b12bc | 450 | for (pointer = generic_bignum; pointer <= leader; pointer++) |
252b5132 RH |
451 | { |
452 | long work; | |
453 | ||
454 | work = carry + radix * *pointer; | |
455 | *pointer = work & LITTLENUM_MASK; | |
456 | carry = work >> LITTLENUM_NUMBER_OF_BITS; | |
457 | } | |
458 | if (carry) | |
459 | { | |
460 | if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1) | |
461 | { | |
929b12bc | 462 | /* Room to grow a longer bignum. */ |
252b5132 RH |
463 | *++leader = carry; |
464 | } | |
465 | } | |
466 | } | |
929b12bc KH |
467 | /* Again, c is char after number. */ |
468 | /* input_line_pointer -> after c. */ | |
252b5132 RH |
469 | know (LITTLENUM_NUMBER_OF_BITS == 16); |
470 | if (leader < generic_bignum + 2) | |
471 | { | |
929b12bc | 472 | /* Will fit into 32 bits. */ |
252b5132 RH |
473 | number = generic_bignum_to_int32 (); |
474 | small = 1; | |
475 | } | |
476 | #ifdef BFD64 | |
477 | else if (leader < generic_bignum + 4) | |
478 | { | |
479 | /* Will fit into 64 bits. */ | |
480 | number = generic_bignum_to_int64 (); | |
481 | small = 1; | |
482 | } | |
483 | #endif | |
484 | else | |
485 | { | |
929b12bc KH |
486 | /* Number of littlenums in the bignum. */ |
487 | number = leader - generic_bignum + 1; | |
252b5132 RH |
488 | } |
489 | } | |
490 | ||
60bcf0fa NC |
491 | if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) |
492 | && suffix != NULL | |
f805106c | 493 | && input_line_pointer - 1 == suffix) |
252b5132 RH |
494 | c = *input_line_pointer++; |
495 | ||
e140100a NC |
496 | #ifndef tc_allow_U_suffix |
497 | #define tc_allow_U_suffix 1 | |
498 | #endif | |
499 | /* PR 19910: Look for, and ignore, a U suffix to the number. */ | |
500 | if (tc_allow_U_suffix && (c == 'U' || c == 'u')) | |
501 | c = * input_line_pointer++; | |
502 | ||
86b80085 NC |
503 | #ifndef tc_allow_L_suffix |
504 | #define tc_allow_L_suffix 1 | |
505 | #endif | |
506 | /* PR 20732: Look for, and ignore, a L or LL suffix to the number. */ | |
507 | if (tc_allow_L_suffix) | |
508 | while (c == 'L' || c == 'l') | |
509 | c = * input_line_pointer++; | |
510 | ||
252b5132 RH |
511 | if (small) |
512 | { | |
929b12bc KH |
513 | /* Here with number, in correct radix. c is the next char. |
514 | Note that unlike un*x, we allow "011f" "0x9f" to both mean | |
515 | the same as the (conventional) "9f". | |
516 | This is simply easier than checking for strict canonical | |
517 | form. Syntax sux! */ | |
252b5132 RH |
518 | |
519 | if (LOCAL_LABELS_FB && c == 'b') | |
520 | { | |
929b12bc KH |
521 | /* Backward ref to local label. |
522 | Because it is backward, expect it to be defined. */ | |
252b5132 RH |
523 | /* Construct a local label. */ |
524 | name = fb_label_name ((int) number, 0); | |
525 | ||
929b12bc | 526 | /* Seen before, or symbol is defined: OK. */ |
252b5132 RH |
527 | symbolP = symbol_find (name); |
528 | if ((symbolP != NULL) && (S_IS_DEFINED (symbolP))) | |
529 | { | |
929b12bc KH |
530 | /* Local labels are never absolute. Don't waste time |
531 | checking absoluteness. */ | |
252b5132 RH |
532 | know (SEG_NORMAL (S_GET_SEGMENT (symbolP))); |
533 | ||
534 | expressionP->X_op = O_symbol; | |
535 | expressionP->X_add_symbol = symbolP; | |
536 | } | |
537 | else | |
538 | { | |
929b12bc | 539 | /* Either not seen or not defined. */ |
252b5132 RH |
540 | /* @@ Should print out the original string instead of |
541 | the parsed number. */ | |
0e389e77 | 542 | as_bad (_("backward ref to unknown label \"%d:\""), |
252b5132 RH |
543 | (int) number); |
544 | expressionP->X_op = O_constant; | |
545 | } | |
546 | ||
547 | expressionP->X_add_number = 0; | |
548 | } /* case 'b' */ | |
549 | else if (LOCAL_LABELS_FB && c == 'f') | |
550 | { | |
929b12bc KH |
551 | /* Forward reference. Expect symbol to be undefined or |
552 | unknown. undefined: seen it before. unknown: never seen | |
553 | it before. | |
554 | ||
555 | Construct a local label name, then an undefined symbol. | |
556 | Don't create a xseg frag for it: caller may do that. | |
557 | Just return it as never seen before. */ | |
252b5132 RH |
558 | name = fb_label_name ((int) number, 1); |
559 | symbolP = symbol_find_or_make (name); | |
929b12bc | 560 | /* We have no need to check symbol properties. */ |
252b5132 | 561 | #ifndef many_segments |
929b12bc | 562 | /* Since "know" puts its arg into a "string", we |
252b5132 RH |
563 | can't have newlines in the argument. */ |
564 | know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section); | |
565 | #endif | |
566 | expressionP->X_op = O_symbol; | |
567 | expressionP->X_add_symbol = symbolP; | |
568 | expressionP->X_add_number = 0; | |
569 | } /* case 'f' */ | |
570 | else if (LOCAL_LABELS_DOLLAR && c == '$') | |
571 | { | |
572 | /* If the dollar label is *currently* defined, then this is just | |
573 | another reference to it. If it is not *currently* defined, | |
574 | then this is a fresh instantiation of that number, so create | |
575 | it. */ | |
576 | ||
577 | if (dollar_label_defined ((long) number)) | |
578 | { | |
579 | name = dollar_label_name ((long) number, 0); | |
580 | symbolP = symbol_find (name); | |
581 | know (symbolP != NULL); | |
582 | } | |
583 | else | |
584 | { | |
585 | name = dollar_label_name ((long) number, 1); | |
586 | symbolP = symbol_find_or_make (name); | |
587 | } | |
588 | ||
589 | expressionP->X_op = O_symbol; | |
590 | expressionP->X_add_symbol = symbolP; | |
591 | expressionP->X_add_number = 0; | |
592 | } /* case '$' */ | |
593 | else | |
594 | { | |
595 | expressionP->X_op = O_constant; | |
252b5132 | 596 | expressionP->X_add_number = number; |
bc4466dc | 597 | input_line_pointer--; /* Restore following character. */ |
929b12bc | 598 | } /* Really just a number. */ |
252b5132 RH |
599 | } |
600 | else | |
601 | { | |
bc4466dc | 602 | /* Not a small number. */ |
252b5132 | 603 | expressionP->X_op = O_big; |
929b12bc KH |
604 | expressionP->X_add_number = number; /* Number of littlenums. */ |
605 | input_line_pointer--; /* -> char following number. */ | |
252b5132 RH |
606 | } |
607 | } | |
608 | ||
609 | /* Parse an MRI multi character constant. */ | |
610 | ||
611 | static void | |
dd625418 | 612 | mri_char_constant (expressionS *expressionP) |
252b5132 RH |
613 | { |
614 | int i; | |
615 | ||
616 | if (*input_line_pointer == '\'' | |
617 | && input_line_pointer[1] != '\'') | |
618 | { | |
619 | expressionP->X_op = O_constant; | |
620 | expressionP->X_add_number = 0; | |
621 | return; | |
622 | } | |
623 | ||
624 | /* In order to get the correct byte ordering, we must build the | |
625 | number in reverse. */ | |
626 | for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--) | |
627 | { | |
628 | int j; | |
629 | ||
630 | generic_bignum[i] = 0; | |
631 | for (j = 0; j < CHARS_PER_LITTLENUM; j++) | |
632 | { | |
633 | if (*input_line_pointer == '\'') | |
634 | { | |
635 | if (input_line_pointer[1] != '\'') | |
636 | break; | |
637 | ++input_line_pointer; | |
638 | } | |
639 | generic_bignum[i] <<= 8; | |
640 | generic_bignum[i] += *input_line_pointer; | |
641 | ++input_line_pointer; | |
642 | } | |
643 | ||
644 | if (i < SIZE_OF_LARGE_NUMBER - 1) | |
645 | { | |
646 | /* If there is more than one littlenum, left justify the | |
02000999 AM |
647 | last one to make it match the earlier ones. If there is |
648 | only one, we can just use the value directly. */ | |
252b5132 RH |
649 | for (; j < CHARS_PER_LITTLENUM; j++) |
650 | generic_bignum[i] <<= 8; | |
651 | } | |
652 | ||
653 | if (*input_line_pointer == '\'' | |
654 | && input_line_pointer[1] != '\'') | |
655 | break; | |
656 | } | |
657 | ||
658 | if (i < 0) | |
659 | { | |
0e389e77 | 660 | as_bad (_("character constant too large")); |
252b5132 RH |
661 | i = 0; |
662 | } | |
663 | ||
664 | if (i > 0) | |
665 | { | |
666 | int c; | |
667 | int j; | |
668 | ||
669 | c = SIZE_OF_LARGE_NUMBER - i; | |
670 | for (j = 0; j < c; j++) | |
671 | generic_bignum[j] = generic_bignum[i + j]; | |
672 | i = c; | |
673 | } | |
674 | ||
675 | know (LITTLENUM_NUMBER_OF_BITS == 16); | |
676 | if (i > 2) | |
677 | { | |
678 | expressionP->X_op = O_big; | |
679 | expressionP->X_add_number = i; | |
680 | } | |
681 | else | |
682 | { | |
683 | expressionP->X_op = O_constant; | |
684 | if (i < 2) | |
685 | expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK; | |
686 | else | |
687 | expressionP->X_add_number = | |
688 | (((generic_bignum[1] & LITTLENUM_MASK) | |
689 | << LITTLENUM_NUMBER_OF_BITS) | |
690 | | (generic_bignum[0] & LITTLENUM_MASK)); | |
691 | } | |
692 | ||
693 | /* Skip the final closing quote. */ | |
694 | ++input_line_pointer; | |
695 | } | |
696 | ||
697 | /* Return an expression representing the current location. This | |
698 | handles the magic symbol `.'. */ | |
699 | ||
b7adb16d | 700 | void |
dd625418 | 701 | current_location (expressionS *expressionp) |
252b5132 RH |
702 | { |
703 | if (now_seg == absolute_section) | |
704 | { | |
705 | expressionp->X_op = O_constant; | |
706 | expressionp->X_add_number = abs_section_offset; | |
707 | } | |
708 | else | |
709 | { | |
252b5132 | 710 | expressionp->X_op = O_symbol; |
4a826962 | 711 | expressionp->X_add_symbol = &dot_symbol; |
252b5132 RH |
712 | expressionp->X_add_number = 0; |
713 | } | |
714 | } | |
715 | ||
929b12bc KH |
716 | /* In: Input_line_pointer points to 1st char of operand, which may |
717 | be a space. | |
718 | ||
3b37fd66 | 719 | Out: An expressionS. |
929b12bc KH |
720 | The operand may have been empty: in this case X_op == O_absent. |
721 | Input_line_pointer->(next non-blank) char after operand. */ | |
252b5132 RH |
722 | |
723 | static segT | |
9497f5ac | 724 | operand (expressionS *expressionP, enum expr_mode mode) |
252b5132 RH |
725 | { |
726 | char c; | |
929b12bc KH |
727 | symbolS *symbolP; /* Points to symbol. */ |
728 | char *name; /* Points to name of symbol. */ | |
252b5132 RH |
729 | segT segment; |
730 | ||
731 | /* All integers are regarded as unsigned unless they are negated. | |
732 | This is because the only thing which cares whether a number is | |
733 | unsigned is the code in emit_expr which extends constants into | |
734 | bignums. It should only sign extend negative numbers, so that | |
735 | something like ``.quad 0x80000000'' is not sign extended even | |
736 | though it appears negative if valueT is 32 bits. */ | |
737 | expressionP->X_unsigned = 1; | |
956a6ba3 | 738 | expressionP->X_extrabit = 0; |
252b5132 | 739 | |
929b12bc | 740 | /* Digits, assume it is a bignum. */ |
252b5132 | 741 | |
929b12bc | 742 | SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */ |
bc4466dc | 743 | c = *input_line_pointer++; /* input_line_pointer -> past char in c. */ |
252b5132 | 744 | |
b75c0c92 AM |
745 | if (is_end_of_line[(unsigned char) c]) |
746 | goto eol; | |
747 | ||
252b5132 RH |
748 | switch (c) |
749 | { | |
750 | case '1': | |
751 | case '2': | |
752 | case '3': | |
753 | case '4': | |
754 | case '5': | |
755 | case '6': | |
756 | case '7': | |
757 | case '8': | |
758 | case '9': | |
759 | input_line_pointer--; | |
760 | ||
60bcf0fa | 761 | integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) |
929b12bc | 762 | ? 0 : 10, |
411863a4 | 763 | expressionP); |
252b5132 RH |
764 | break; |
765 | ||
977e771a FCE |
766 | #ifdef LITERAL_PREFIXPERCENT_BIN |
767 | case '%': | |
768 | integer_constant (2, expressionP); | |
769 | break; | |
770 | #endif | |
771 | ||
252b5132 | 772 | case '0': |
929b12bc | 773 | /* Non-decimal radix. */ |
252b5132 | 774 | |
f805106c | 775 | if (NUMBERS_WITH_SUFFIX || flag_m68k_mri) |
252b5132 RH |
776 | { |
777 | char *s; | |
778 | ||
02000999 | 779 | /* Check for a hex or float constant. */ |
252b5132 RH |
780 | for (s = input_line_pointer; hex_p (*s); s++) |
781 | ; | |
02000999 | 782 | if (*s == 'h' || *s == 'H' || *input_line_pointer == '.') |
252b5132 RH |
783 | { |
784 | --input_line_pointer; | |
785 | integer_constant (0, expressionP); | |
786 | break; | |
787 | } | |
929b12bc | 788 | } |
252b5132 RH |
789 | c = *input_line_pointer; |
790 | switch (c) | |
791 | { | |
792 | case 'o': | |
793 | case 'O': | |
794 | case 'q': | |
795 | case 'Q': | |
796 | case '8': | |
797 | case '9': | |
f805106c | 798 | if (NUMBERS_WITH_SUFFIX || flag_m68k_mri) |
252b5132 RH |
799 | { |
800 | integer_constant (0, expressionP); | |
801 | break; | |
802 | } | |
803 | /* Fall through. */ | |
804 | default: | |
805 | default_case: | |
806 | if (c && strchr (FLT_CHARS, c)) | |
807 | { | |
808 | input_line_pointer++; | |
809 | floating_constant (expressionP); | |
3882b010 | 810 | expressionP->X_add_number = - TOLOWER (c); |
252b5132 RH |
811 | } |
812 | else | |
813 | { | |
929b12bc | 814 | /* The string was only zero. */ |
252b5132 RH |
815 | expressionP->X_op = O_constant; |
816 | expressionP->X_add_number = 0; | |
817 | } | |
818 | ||
819 | break; | |
820 | ||
821 | case 'x': | |
822 | case 'X': | |
ab266a97 | 823 | if (flag_m68k_mri) |
252b5132 RH |
824 | goto default_case; |
825 | input_line_pointer++; | |
826 | integer_constant (16, expressionP); | |
827 | break; | |
828 | ||
829 | case 'b': | |
c14c7a8a AM |
830 | if (LOCAL_LABELS_FB && !flag_m68k_mri |
831 | && input_line_pointer[1] != '0' | |
832 | && input_line_pointer[1] != '1') | |
252b5132 | 833 | { |
c14c7a8a AM |
834 | /* Parse this as a back reference to label 0. */ |
835 | input_line_pointer--; | |
836 | integer_constant (10, expressionP); | |
837 | break; | |
252b5132 | 838 | } |
c14c7a8a | 839 | /* Otherwise, parse this as a binary number. */ |
252b5132 RH |
840 | /* Fall through. */ |
841 | case 'B': | |
c14c7a8a AM |
842 | if (input_line_pointer[1] == '0' |
843 | || input_line_pointer[1] == '1') | |
844 | { | |
845 | input_line_pointer++; | |
846 | integer_constant (2, expressionP); | |
847 | break; | |
848 | } | |
6dc19fc4 | 849 | if (flag_m68k_mri || NUMBERS_WITH_SUFFIX) |
c14c7a8a AM |
850 | input_line_pointer++; |
851 | goto default_case; | |
252b5132 RH |
852 | |
853 | case '0': | |
854 | case '1': | |
855 | case '2': | |
856 | case '3': | |
857 | case '4': | |
858 | case '5': | |
859 | case '6': | |
860 | case '7': | |
6dc19fc4 | 861 | integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX) |
929b12bc KH |
862 | ? 0 : 8, |
863 | expressionP); | |
252b5132 RH |
864 | break; |
865 | ||
866 | case 'f': | |
867 | if (LOCAL_LABELS_FB) | |
868 | { | |
19c2883a AM |
869 | int is_label = 1; |
870 | ||
252b5132 RH |
871 | /* If it says "0f" and it could possibly be a floating point |
872 | number, make it one. Otherwise, make it a local label, | |
873 | and try to deal with parsing the rest later. */ | |
19c2883a AM |
874 | if (!is_end_of_line[(unsigned char) input_line_pointer[1]] |
875 | && strchr (FLT_CHARS, 'f') != NULL) | |
876 | { | |
877 | char *cp = input_line_pointer + 1; | |
878 | ||
879 | atof_generic (&cp, ".", EXP_CHARS, | |
880 | &generic_floating_point_number); | |
881 | ||
882 | /* Was nothing parsed, or does it look like an | |
883 | expression? */ | |
884 | is_label = (cp == input_line_pointer + 1 | |
885 | || (cp == input_line_pointer + 2 | |
886 | && (cp[-1] == '-' || cp[-1] == '+')) | |
887 | || *cp == 'f' | |
888 | || *cp == 'b'); | |
889 | } | |
890 | if (is_label) | |
891 | { | |
892 | input_line_pointer--; | |
893 | integer_constant (10, expressionP); | |
894 | break; | |
895 | } | |
252b5132 | 896 | } |
19c2883a | 897 | /* Fall through. */ |
252b5132 RH |
898 | |
899 | case 'd': | |
900 | case 'D': | |
6dc19fc4 | 901 | if (flag_m68k_mri || NUMBERS_WITH_SUFFIX) |
252b5132 RH |
902 | { |
903 | integer_constant (0, expressionP); | |
904 | break; | |
905 | } | |
906 | /* Fall through. */ | |
907 | case 'F': | |
908 | case 'r': | |
909 | case 'e': | |
910 | case 'E': | |
911 | case 'g': | |
912 | case 'G': | |
913 | input_line_pointer++; | |
914 | floating_constant (expressionP); | |
3882b010 | 915 | expressionP->X_add_number = - TOLOWER (c); |
252b5132 RH |
916 | break; |
917 | ||
918 | case '$': | |
919 | if (LOCAL_LABELS_DOLLAR) | |
920 | { | |
921 | integer_constant (10, expressionP); | |
922 | break; | |
923 | } | |
924 | else | |
925 | goto default_case; | |
926 | } | |
927 | ||
928 | break; | |
929 | ||
b585bc2c | 930 | #ifndef NEED_INDEX_OPERATOR |
252b5132 | 931 | case '[': |
fcaed75e JB |
932 | # ifdef md_need_index_operator |
933 | if (md_need_index_operator()) | |
934 | goto de_fault; | |
935 | # endif | |
b585bc2c | 936 | #endif |
fcddde94 | 937 | /* Fall through. */ |
fcaed75e | 938 | case '(': |
929b12bc | 939 | /* Didn't begin with digit & not a name. */ |
259af69e | 940 | segment = expr (0, expressionP, mode); |
929b12bc | 941 | /* expression () will pass trailing whitespace. */ |
f7c88872 AM |
942 | if ((c == '(' && *input_line_pointer != ')') |
943 | || (c == '[' && *input_line_pointer != ']')) | |
e140100a NC |
944 | { |
945 | if (* input_line_pointer) | |
946 | as_bad (_("found '%c', expected: '%c'"), | |
947 | * input_line_pointer, c == '(' ? ')' : ']'); | |
948 | else | |
949 | as_bad (_("missing '%c'"), c == '(' ? ')' : ']'); | |
950 | } | |
f7c88872 | 951 | else |
929b12bc | 952 | input_line_pointer++; |
252b5132 | 953 | SKIP_WHITESPACE (); |
929b12bc | 954 | /* Here with input_line_pointer -> char after "(...)". */ |
252b5132 RH |
955 | return segment; |
956 | ||
abd63a32 | 957 | #ifdef TC_M68K |
252b5132 RH |
958 | case 'E': |
959 | if (! flag_m68k_mri || *input_line_pointer != '\'') | |
960 | goto de_fault; | |
961 | as_bad (_("EBCDIC constants are not supported")); | |
962 | /* Fall through. */ | |
963 | case 'A': | |
964 | if (! flag_m68k_mri || *input_line_pointer != '\'') | |
965 | goto de_fault; | |
966 | ++input_line_pointer; | |
abd63a32 | 967 | #endif |
fcddde94 | 968 | /* Fall through. */ |
252b5132 RH |
969 | case '\'': |
970 | if (! flag_m68k_mri) | |
971 | { | |
972 | /* Warning: to conform to other people's assemblers NO | |
929b12bc | 973 | ESCAPEMENT is permitted for a single quote. The next |
252b5132 | 974 | character, parity errors and all, is taken as the value |
929b12bc | 975 | of the operand. VERY KINKY. */ |
252b5132 RH |
976 | expressionP->X_op = O_constant; |
977 | expressionP->X_add_number = *input_line_pointer++; | |
978 | break; | |
979 | } | |
980 | ||
981 | mri_char_constant (expressionP); | |
982 | break; | |
983 | ||
abd63a32 | 984 | #ifdef TC_M68K |
252b5132 RH |
985 | case '"': |
986 | /* Double quote is the bitwise not operator in MRI mode. */ | |
987 | if (! flag_m68k_mri) | |
988 | goto de_fault; | |
abd63a32 | 989 | #endif |
fcddde94 | 990 | /* Fall through. */ |
252b5132 | 991 | case '~': |
929b12bc | 992 | /* '~' is permitted to start a label on the Delta. */ |
252b5132 RH |
993 | if (is_name_beginner (c)) |
994 | goto isname; | |
1a0670f3 | 995 | /* Fall through. */ |
252b5132 RH |
996 | case '!': |
997 | case '-': | |
43c3ab55 | 998 | case '+': |
252b5132 | 999 | { |
fcaed75e JB |
1000 | #ifdef md_operator |
1001 | unary: | |
1002 | #endif | |
9497f5ac | 1003 | operand (expressionP, mode); |
252b5132 RH |
1004 | if (expressionP->X_op == O_constant) |
1005 | { | |
929b12bc | 1006 | /* input_line_pointer -> char after operand. */ |
252b5132 RH |
1007 | if (c == '-') |
1008 | { | |
65879393 AM |
1009 | expressionP->X_add_number |
1010 | = - (addressT) expressionP->X_add_number; | |
929b12bc KH |
1011 | /* Notice: '-' may overflow: no warning is given. |
1012 | This is compatible with other people's | |
1013 | assemblers. Sigh. */ | |
252b5132 | 1014 | expressionP->X_unsigned = 0; |
956a6ba3 JB |
1015 | if (expressionP->X_add_number) |
1016 | expressionP->X_extrabit ^= 1; | |
252b5132 RH |
1017 | } |
1018 | else if (c == '~' || c == '"') | |
32d71569 AM |
1019 | { |
1020 | expressionP->X_add_number = ~ expressionP->X_add_number; | |
1021 | expressionP->X_extrabit ^= 1; | |
1022 | } | |
43c3ab55 | 1023 | else if (c == '!') |
32d71569 AM |
1024 | { |
1025 | expressionP->X_add_number = ! expressionP->X_add_number; | |
1026 | expressionP->X_unsigned = 1; | |
1027 | expressionP->X_extrabit = 0; | |
1028 | } | |
252b5132 | 1029 | } |
02000999 AM |
1030 | else if (expressionP->X_op == O_big |
1031 | && expressionP->X_add_number <= 0 | |
1032 | && c == '-' | |
1033 | && (generic_floating_point_number.sign == '+' | |
1034 | || generic_floating_point_number.sign == 'P')) | |
1035 | { | |
1036 | /* Negative flonum (eg, -1.000e0). */ | |
1037 | if (generic_floating_point_number.sign == '+') | |
1038 | generic_floating_point_number.sign = '-'; | |
1039 | else | |
1040 | generic_floating_point_number.sign = 'N'; | |
1041 | } | |
b2221023 MM |
1042 | else if (expressionP->X_op == O_big |
1043 | && expressionP->X_add_number > 0) | |
1044 | { | |
1045 | int i; | |
1046 | ||
1047 | if (c == '~' || c == '-') | |
1048 | { | |
1049 | for (i = 0; i < expressionP->X_add_number; ++i) | |
1050 | generic_bignum[i] = ~generic_bignum[i]; | |
93d90f46 AM |
1051 | |
1052 | /* Extend the bignum to at least the size of .octa. */ | |
1053 | if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER) | |
1054 | { | |
1055 | expressionP->X_add_number = SIZE_OF_LARGE_NUMBER; | |
1056 | for (; i < expressionP->X_add_number; ++i) | |
1057 | generic_bignum[i] = ~(LITTLENUM_TYPE) 0; | |
1058 | } | |
1059 | ||
b2221023 MM |
1060 | if (c == '-') |
1061 | for (i = 0; i < expressionP->X_add_number; ++i) | |
1062 | { | |
1063 | generic_bignum[i] += 1; | |
1064 | if (generic_bignum[i]) | |
1065 | break; | |
1066 | } | |
1067 | } | |
1068 | else if (c == '!') | |
1069 | { | |
b2221023 | 1070 | for (i = 0; i < expressionP->X_add_number; ++i) |
93d90f46 AM |
1071 | if (generic_bignum[i] != 0) |
1072 | break; | |
1073 | expressionP->X_add_number = i >= expressionP->X_add_number; | |
1074 | expressionP->X_op = O_constant; | |
1075 | expressionP->X_unsigned = 1; | |
956a6ba3 | 1076 | expressionP->X_extrabit = 0; |
b2221023 MM |
1077 | } |
1078 | } | |
252b5132 RH |
1079 | else if (expressionP->X_op != O_illegal |
1080 | && expressionP->X_op != O_absent) | |
1081 | { | |
43c3ab55 JB |
1082 | if (c != '+') |
1083 | { | |
1084 | expressionP->X_add_symbol = make_expr_symbol (expressionP); | |
1085 | if (c == '-') | |
1086 | expressionP->X_op = O_uminus; | |
1087 | else if (c == '~' || c == '"') | |
1088 | expressionP->X_op = O_bit_not; | |
1089 | else | |
1090 | expressionP->X_op = O_logical_not; | |
1091 | expressionP->X_add_number = 0; | |
1092 | } | |
252b5132 RH |
1093 | } |
1094 | else | |
1095 | as_warn (_("Unary operator %c ignored because bad operand follows"), | |
1096 | c); | |
1097 | } | |
1098 | break; | |
1099 | ||
e2d15955 JD |
1100 | #if !defined (DOLLAR_DOT) && !defined (TC_M68K) |
1101 | case '$': | |
1102 | if (literal_prefix_dollar_hex) | |
1103 | { | |
1104 | /* $L is the start of a local label, not a hex constant. */ | |
1105 | if (* input_line_pointer == 'L') | |
1106 | goto isname; | |
1107 | integer_constant (16, expressionP); | |
1108 | } | |
1109 | else | |
1110 | { | |
1111 | goto isname; | |
1112 | } | |
1113 | break; | |
1114 | #else | |
252b5132 | 1115 | case '$': |
929b12bc | 1116 | /* '$' is the program counter when in MRI mode, or when |
02000999 | 1117 | DOLLAR_DOT is defined. */ |
252b5132 RH |
1118 | #ifndef DOLLAR_DOT |
1119 | if (! flag_m68k_mri) | |
1120 | goto de_fault; | |
1121 | #endif | |
4fa6945e | 1122 | if (DOLLAR_AMBIGU && hex_p (*input_line_pointer)) |
252b5132 | 1123 | { |
4fa6945e NC |
1124 | /* In MRI mode and on Z80, '$' is also used as the prefix |
1125 | for a hexadecimal constant. */ | |
252b5132 RH |
1126 | integer_constant (16, expressionP); |
1127 | break; | |
1128 | } | |
1129 | ||
1130 | if (is_part_of_name (*input_line_pointer)) | |
1131 | goto isname; | |
1132 | ||
1133 | current_location (expressionP); | |
1134 | break; | |
abd63a32 | 1135 | #endif |
252b5132 RH |
1136 | |
1137 | case '.': | |
1138 | if (!is_part_of_name (*input_line_pointer)) | |
1139 | { | |
1140 | current_location (expressionP); | |
1141 | break; | |
1142 | } | |
1143 | else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0 | |
1144 | && ! is_part_of_name (input_line_pointer[8])) | |
1145 | || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0 | |
1146 | && ! is_part_of_name (input_line_pointer[7]))) | |
1147 | { | |
1148 | int start; | |
1149 | ||
1150 | start = (input_line_pointer[1] == 't' | |
1151 | || input_line_pointer[1] == 'T'); | |
1152 | input_line_pointer += start ? 8 : 7; | |
1153 | SKIP_WHITESPACE (); | |
b0c53498 JB |
1154 | |
1155 | /* Cover for the as_bad () invocations below. */ | |
1156 | expressionP->X_op = O_absent; | |
1157 | ||
252b5132 RH |
1158 | if (*input_line_pointer != '(') |
1159 | as_bad (_("syntax error in .startof. or .sizeof.")); | |
1160 | else | |
1161 | { | |
1162 | char *buf; | |
1163 | ||
1164 | ++input_line_pointer; | |
1165 | SKIP_WHITESPACE (); | |
d02603dc | 1166 | c = get_symbol_name (& name); |
b0c53498 JB |
1167 | if (! *name) |
1168 | { | |
1169 | as_bad (_("expected symbol name")); | |
1170 | (void) restore_line_pointer (c); | |
1171 | if (c != ')') | |
1172 | ignore_rest_of_line (); | |
1173 | else | |
1174 | ++input_line_pointer; | |
1175 | break; | |
1176 | } | |
252b5132 | 1177 | |
29a2809e TS |
1178 | buf = concat (start ? ".startof." : ".sizeof.", name, |
1179 | (char *) NULL); | |
252b5132 RH |
1180 | symbolP = symbol_make (buf); |
1181 | free (buf); | |
1182 | ||
1183 | expressionP->X_op = O_symbol; | |
1184 | expressionP->X_add_symbol = symbolP; | |
1185 | expressionP->X_add_number = 0; | |
1186 | ||
1187 | *input_line_pointer = c; | |
d02603dc | 1188 | SKIP_WHITESPACE_AFTER_NAME (); |
252b5132 RH |
1189 | if (*input_line_pointer != ')') |
1190 | as_bad (_("syntax error in .startof. or .sizeof.")); | |
1191 | else | |
1192 | ++input_line_pointer; | |
1193 | } | |
1194 | break; | |
1195 | } | |
1196 | else | |
1197 | { | |
1198 | goto isname; | |
1199 | } | |
b75c0c92 | 1200 | |
252b5132 | 1201 | case ',': |
252b5132 | 1202 | eol: |
929b12bc | 1203 | /* Can't imagine any other kind of operand. */ |
252b5132 RH |
1204 | expressionP->X_op = O_absent; |
1205 | input_line_pointer--; | |
1206 | break; | |
1207 | ||
abd63a32 | 1208 | #ifdef TC_M68K |
252b5132 RH |
1209 | case '%': |
1210 | if (! flag_m68k_mri) | |
1211 | goto de_fault; | |
1212 | integer_constant (2, expressionP); | |
1213 | break; | |
1214 | ||
1215 | case '@': | |
1216 | if (! flag_m68k_mri) | |
1217 | goto de_fault; | |
1218 | integer_constant (8, expressionP); | |
1219 | break; | |
1220 | ||
1221 | case ':': | |
1222 | if (! flag_m68k_mri) | |
1223 | goto de_fault; | |
1224 | ||
1225 | /* In MRI mode, this is a floating point constant represented | |
02000999 | 1226 | using hexadecimal digits. */ |
252b5132 RH |
1227 | |
1228 | ++input_line_pointer; | |
1229 | integer_constant (16, expressionP); | |
1230 | break; | |
1231 | ||
1232 | case '*': | |
1233 | if (! flag_m68k_mri || is_part_of_name (*input_line_pointer)) | |
1234 | goto de_fault; | |
1235 | ||
1236 | current_location (expressionP); | |
1237 | break; | |
abd63a32 | 1238 | #endif |
252b5132 RH |
1239 | |
1240 | default: | |
fcaed75e | 1241 | #if defined(md_need_index_operator) || defined(TC_M68K) |
252b5132 | 1242 | de_fault: |
abd63a32 | 1243 | #endif |
d02603dc | 1244 | if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */ |
252b5132 | 1245 | { |
929b12bc KH |
1246 | /* Identifier begins here. |
1247 | This is kludged for speed, so code is repeated. */ | |
252b5132 | 1248 | isname: |
d02603dc NC |
1249 | -- input_line_pointer; |
1250 | c = get_symbol_name (&name); | |
252b5132 | 1251 | |
fcaed75e JB |
1252 | #ifdef md_operator |
1253 | { | |
1e9cc1c2 | 1254 | operatorT op = md_operator (name, 1, &c); |
fcaed75e | 1255 | |
1e9cc1c2 | 1256 | switch (op) |
fcaed75e JB |
1257 | { |
1258 | case O_uminus: | |
d02603dc | 1259 | restore_line_pointer (c); |
fcaed75e JB |
1260 | c = '-'; |
1261 | goto unary; | |
1262 | case O_bit_not: | |
d02603dc | 1263 | restore_line_pointer (c); |
fcaed75e JB |
1264 | c = '~'; |
1265 | goto unary; | |
1266 | case O_logical_not: | |
d02603dc | 1267 | restore_line_pointer (c); |
fcaed75e JB |
1268 | c = '!'; |
1269 | goto unary; | |
1270 | case O_illegal: | |
1271 | as_bad (_("invalid use of operator \"%s\""), name); | |
1272 | break; | |
1273 | default: | |
1274 | break; | |
1275 | } | |
d02603dc | 1276 | |
1e9cc1c2 | 1277 | if (op != O_absent && op != O_illegal) |
fcaed75e | 1278 | { |
d02603dc | 1279 | restore_line_pointer (c); |
fcaed75e JB |
1280 | expr (9, expressionP, mode); |
1281 | expressionP->X_add_symbol = make_expr_symbol (expressionP); | |
1282 | expressionP->X_op_symbol = NULL; | |
1283 | expressionP->X_add_number = 0; | |
1e9cc1c2 | 1284 | expressionP->X_op = op; |
fcaed75e JB |
1285 | break; |
1286 | } | |
1287 | } | |
1288 | #endif | |
1289 | ||
252b5132 RH |
1290 | #ifdef md_parse_name |
1291 | /* This is a hook for the backend to parse certain names | |
02000999 AM |
1292 | specially in certain contexts. If a name always has a |
1293 | specific value, it can often be handled by simply | |
1294 | entering it in the symbol table. */ | |
9497f5ac | 1295 | if (md_parse_name (name, expressionP, mode, &c)) |
252b5132 | 1296 | { |
d02603dc | 1297 | restore_line_pointer (c); |
252b5132 RH |
1298 | break; |
1299 | } | |
1300 | #endif | |
1301 | ||
252b5132 RH |
1302 | symbolP = symbol_find_or_make (name); |
1303 | ||
1304 | /* If we have an absolute symbol or a reg, then we know its | |
1305 | value now. */ | |
1306 | segment = S_GET_SEGMENT (symbolP); | |
c969da64 RS |
1307 | if (mode != expr_defer |
1308 | && segment == absolute_section | |
1309 | && !S_FORCE_RELOC (symbolP, 0)) | |
252b5132 RH |
1310 | { |
1311 | expressionP->X_op = O_constant; | |
1312 | expressionP->X_add_number = S_GET_VALUE (symbolP); | |
1313 | } | |
9497f5ac | 1314 | else if (mode != expr_defer && segment == reg_section) |
252b5132 RH |
1315 | { |
1316 | expressionP->X_op = O_register; | |
1317 | expressionP->X_add_number = S_GET_VALUE (symbolP); | |
1318 | } | |
1319 | else | |
1320 | { | |
1321 | expressionP->X_op = O_symbol; | |
1322 | expressionP->X_add_symbol = symbolP; | |
1323 | expressionP->X_add_number = 0; | |
1324 | } | |
d02603dc NC |
1325 | |
1326 | restore_line_pointer (c); | |
252b5132 RH |
1327 | } |
1328 | else | |
1329 | { | |
1330 | /* Let the target try to parse it. Success is indicated by changing | |
1331 | the X_op field to something other than O_absent and pointing | |
927781e2 | 1332 | input_line_pointer past the expression. If it can't parse the |
252b5132 RH |
1333 | expression, X_op and input_line_pointer should be unchanged. */ |
1334 | expressionP->X_op = O_absent; | |
1335 | --input_line_pointer; | |
1336 | md_operand (expressionP); | |
1337 | if (expressionP->X_op == O_absent) | |
1338 | { | |
1339 | ++input_line_pointer; | |
0e389e77 | 1340 | as_bad (_("bad expression")); |
252b5132 RH |
1341 | expressionP->X_op = O_constant; |
1342 | expressionP->X_add_number = 0; | |
1343 | } | |
1344 | } | |
1345 | break; | |
1346 | } | |
1347 | ||
929b12bc KH |
1348 | /* It is more 'efficient' to clean up the expressionS when they are |
1349 | created. Doing it here saves lines of code. */ | |
252b5132 | 1350 | clean_up_expression (expressionP); |
14c1428b | 1351 | SKIP_ALL_WHITESPACE (); /* -> 1st char after operand. */ |
252b5132 RH |
1352 | know (*input_line_pointer != ' '); |
1353 | ||
1354 | /* The PA port needs this information. */ | |
1355 | if (expressionP->X_add_symbol) | |
49309057 | 1356 | symbol_mark_used (expressionP->X_add_symbol); |
252b5132 | 1357 | |
3df4e177 MR |
1358 | if (mode != expr_defer) |
1359 | { | |
1360 | expressionP->X_add_symbol | |
1361 | = symbol_clone_if_forward_ref (expressionP->X_add_symbol); | |
1362 | expressionP->X_op_symbol | |
1363 | = symbol_clone_if_forward_ref (expressionP->X_op_symbol); | |
1364 | } | |
9497f5ac | 1365 | |
252b5132 RH |
1366 | switch (expressionP->X_op) |
1367 | { | |
1368 | default: | |
1369 | return absolute_section; | |
1370 | case O_symbol: | |
1371 | return S_GET_SEGMENT (expressionP->X_add_symbol); | |
1372 | case O_register: | |
1373 | return reg_section; | |
1374 | } | |
929b12bc | 1375 | } |
252b5132 | 1376 | \f |
929b12bc KH |
1377 | /* Internal. Simplify a struct expression for use by expr (). */ |
1378 | ||
3b37fd66 | 1379 | /* In: address of an expressionS. |
929b12bc KH |
1380 | The X_op field of the expressionS may only take certain values. |
1381 | Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT. | |
1382 | ||
1383 | Out: expressionS may have been modified: | |
929b12bc | 1384 | Unused fields zeroed to help expr (). */ |
252b5132 RH |
1385 | |
1386 | static void | |
dd625418 | 1387 | clean_up_expression (expressionS *expressionP) |
252b5132 RH |
1388 | { |
1389 | switch (expressionP->X_op) | |
1390 | { | |
1391 | case O_illegal: | |
1392 | case O_absent: | |
1393 | expressionP->X_add_number = 0; | |
1394 | /* Fall through. */ | |
1395 | case O_big: | |
1396 | case O_constant: | |
1397 | case O_register: | |
1398 | expressionP->X_add_symbol = NULL; | |
1399 | /* Fall through. */ | |
1400 | case O_symbol: | |
1401 | case O_uminus: | |
1402 | case O_bit_not: | |
1403 | expressionP->X_op_symbol = NULL; | |
1404 | break; | |
252b5132 RH |
1405 | default: |
1406 | break; | |
1407 | } | |
1408 | } | |
1409 | \f | |
929b12bc KH |
1410 | /* Expression parser. */ |
1411 | ||
1412 | /* We allow an empty expression, and just assume (absolute,0) silently. | |
1413 | Unary operators and parenthetical expressions are treated as operands. | |
1414 | As usual, Q==quantity==operand, O==operator, X==expression mnemonics. | |
1415 | ||
3b37fd66 | 1416 | We used to do an aho/ullman shift-reduce parser, but the logic got so |
929b12bc KH |
1417 | warped that I flushed it and wrote a recursive-descent parser instead. |
1418 | Now things are stable, would anybody like to write a fast parser? | |
1419 | Most expressions are either register (which does not even reach here) | |
1420 | or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common. | |
1421 | So I guess it doesn't really matter how inefficient more complex expressions | |
1422 | are parsed. | |
1423 | ||
1424 | After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK. | |
1425 | Also, we have consumed any leading or trailing spaces (operand does that) | |
1426 | and done all intervening operators. | |
1427 | ||
1428 | This returns the segment of the result, which will be | |
1429 | absolute_section or the segment of a symbol. */ | |
252b5132 RH |
1430 | |
1431 | #undef __ | |
1432 | #define __ O_illegal | |
fb7ccfc1 AM |
1433 | #ifndef O_SINGLE_EQ |
1434 | #define O_SINGLE_EQ O_illegal | |
1435 | #endif | |
252b5132 | 1436 | |
b041f888 KH |
1437 | /* Maps ASCII -> operators. */ |
1438 | static const operatorT op_encoding[256] = { | |
252b5132 RH |
1439 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, |
1440 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1441 | ||
1442 | __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __, | |
1443 | __, __, O_multiply, O_add, __, O_subtract, __, O_divide, | |
1444 | __, __, __, __, __, __, __, __, | |
fb7ccfc1 | 1445 | __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __, |
252b5132 RH |
1446 | __, __, __, __, __, __, __, __, |
1447 | __, __, __, __, __, __, __, __, | |
1448 | __, __, __, __, __, __, __, __, | |
b585bc2c RH |
1449 | __, __, __, |
1450 | #ifdef NEED_INDEX_OPERATOR | |
1451 | O_index, | |
1452 | #else | |
1453 | __, | |
1454 | #endif | |
1455 | __, __, O_bit_exclusive_or, __, | |
252b5132 RH |
1456 | __, __, __, __, __, __, __, __, |
1457 | __, __, __, __, __, __, __, __, | |
1458 | __, __, __, __, __, __, __, __, | |
1459 | __, __, __, __, O_bit_inclusive_or, __, __, __, | |
1460 | ||
1461 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1462 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1463 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1464 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1465 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1466 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1467 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1468 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ | |
1469 | }; | |
1470 | ||
929b12bc KH |
1471 | /* Rank Examples |
1472 | 0 operand, (expression) | |
1473 | 1 || | |
1474 | 2 && | |
07a7a145 | 1475 | 3 == <> < <= >= > |
929b12bc KH |
1476 | 4 + - |
1477 | 5 used for * / % in MRI mode | |
1478 | 6 & ^ ! | | |
1479 | 7 * / % << >> | |
1480 | 8 unary - unary ~ | |
1481 | */ | |
1035ad42 | 1482 | static operator_rankT op_rank[O_max] = { |
252b5132 RH |
1483 | 0, /* O_illegal */ |
1484 | 0, /* O_absent */ | |
1485 | 0, /* O_constant */ | |
1486 | 0, /* O_symbol */ | |
1487 | 0, /* O_symbol_rva */ | |
1488 | 0, /* O_register */ | |
16887944 | 1489 | 0, /* O_big */ |
b585bc2c RH |
1490 | 9, /* O_uminus */ |
1491 | 9, /* O_bit_not */ | |
1492 | 9, /* O_logical_not */ | |
1493 | 8, /* O_multiply */ | |
1494 | 8, /* O_divide */ | |
1495 | 8, /* O_modulus */ | |
1496 | 8, /* O_left_shift */ | |
1497 | 8, /* O_right_shift */ | |
1498 | 7, /* O_bit_inclusive_or */ | |
1499 | 7, /* O_bit_or_not */ | |
1500 | 7, /* O_bit_exclusive_or */ | |
1501 | 7, /* O_bit_and */ | |
1502 | 5, /* O_add */ | |
1503 | 5, /* O_subtract */ | |
1504 | 4, /* O_eq */ | |
1505 | 4, /* O_ne */ | |
1506 | 4, /* O_lt */ | |
1507 | 4, /* O_le */ | |
1508 | 4, /* O_ge */ | |
1509 | 4, /* O_gt */ | |
1510 | 3, /* O_logical_and */ | |
1511 | 2, /* O_logical_or */ | |
1512 | 1, /* O_index */ | |
252b5132 RH |
1513 | }; |
1514 | ||
1515 | /* Unfortunately, in MRI mode for the m68k, multiplication and | |
1516 | division have lower precedence than the bit wise operators. This | |
1517 | function sets the operator precedences correctly for the current | |
1518 | mode. Also, MRI uses a different bit_not operator, and this fixes | |
1519 | that as well. */ | |
1520 | ||
16887944 AM |
1521 | #define STANDARD_MUL_PRECEDENCE 8 |
1522 | #define MRI_MUL_PRECEDENCE 6 | |
252b5132 RH |
1523 | |
1524 | void | |
dd625418 | 1525 | expr_set_precedence (void) |
252b5132 RH |
1526 | { |
1527 | if (flag_m68k_mri) | |
1528 | { | |
1529 | op_rank[O_multiply] = MRI_MUL_PRECEDENCE; | |
1530 | op_rank[O_divide] = MRI_MUL_PRECEDENCE; | |
1531 | op_rank[O_modulus] = MRI_MUL_PRECEDENCE; | |
1532 | } | |
1533 | else | |
1534 | { | |
1535 | op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE; | |
1536 | op_rank[O_divide] = STANDARD_MUL_PRECEDENCE; | |
1537 | op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE; | |
1538 | } | |
1539 | } | |
1540 | ||
fcaed75e | 1541 | void |
1e9cc1c2 | 1542 | expr_set_rank (operatorT op, operator_rankT rank) |
fcaed75e | 1543 | { |
1e9cc1c2 NC |
1544 | gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank)); |
1545 | op_rank[op] = rank; | |
fcaed75e JB |
1546 | } |
1547 | ||
252b5132 RH |
1548 | /* Initialize the expression parser. */ |
1549 | ||
1550 | void | |
dd625418 | 1551 | expr_begin (void) |
252b5132 RH |
1552 | { |
1553 | expr_set_precedence (); | |
1554 | ||
1555 | /* Verify that X_op field is wide enough. */ | |
1556 | { | |
1557 | expressionS e; | |
1558 | e.X_op = O_max; | |
9c2799c2 | 1559 | gas_assert (e.X_op == O_max); |
252b5132 RH |
1560 | } |
1561 | } | |
1562 | \f | |
6fad6acb AM |
1563 | /* Return the encoding for the operator at INPUT_LINE_POINTER, and |
1564 | sets NUM_CHARS to the number of characters in the operator. | |
1565 | Does not advance INPUT_LINE_POINTER. */ | |
252b5132 RH |
1566 | |
1567 | static inline operatorT | |
1e9cc1c2 | 1568 | operatorf (int *num_chars) |
252b5132 RH |
1569 | { |
1570 | int c; | |
1571 | operatorT ret; | |
1572 | ||
1573 | c = *input_line_pointer & 0xff; | |
6fad6acb | 1574 | *num_chars = 1; |
252b5132 | 1575 | |
b75c0c92 AM |
1576 | if (is_end_of_line[c]) |
1577 | return O_illegal; | |
1578 | ||
fcaed75e JB |
1579 | #ifdef md_operator |
1580 | if (is_name_beginner (c)) | |
1581 | { | |
d02603dc NC |
1582 | char *name; |
1583 | char ec = get_symbol_name (& name); | |
fcaed75e | 1584 | |
91d6fa6a | 1585 | ret = md_operator (name, 2, &ec); |
fcaed75e JB |
1586 | switch (ret) |
1587 | { | |
1588 | case O_absent: | |
91d6fa6a | 1589 | *input_line_pointer = ec; |
fcaed75e JB |
1590 | input_line_pointer = name; |
1591 | break; | |
1592 | case O_uminus: | |
1593 | case O_bit_not: | |
1594 | case O_logical_not: | |
1595 | as_bad (_("invalid use of operator \"%s\""), name); | |
1596 | ret = O_illegal; | |
1597 | /* FALLTHROUGH */ | |
1598 | default: | |
91d6fa6a | 1599 | *input_line_pointer = ec; |
fcaed75e JB |
1600 | *num_chars = input_line_pointer - name; |
1601 | input_line_pointer = name; | |
1602 | return ret; | |
1603 | } | |
1604 | } | |
1605 | #endif | |
1606 | ||
252b5132 RH |
1607 | switch (c) |
1608 | { | |
1609 | default: | |
fcaed75e JB |
1610 | ret = op_encoding[c]; |
1611 | #ifdef md_operator | |
1612 | if (ret == O_illegal) | |
1613 | { | |
1614 | char *start = input_line_pointer; | |
1615 | ||
1616 | ret = md_operator (NULL, 2, NULL); | |
1617 | if (ret != O_illegal) | |
1618 | *num_chars = input_line_pointer - start; | |
1619 | input_line_pointer = start; | |
1620 | } | |
1621 | #endif | |
1622 | return ret; | |
252b5132 | 1623 | |
3e4caed2 NS |
1624 | case '+': |
1625 | case '-': | |
8c9f705e | 1626 | return op_encoding[c]; |
3e4caed2 | 1627 | |
252b5132 RH |
1628 | case '<': |
1629 | switch (input_line_pointer[1]) | |
1630 | { | |
1631 | default: | |
1632 | return op_encoding[c]; | |
1633 | case '<': | |
1634 | ret = O_left_shift; | |
1635 | break; | |
1636 | case '>': | |
1637 | ret = O_ne; | |
1638 | break; | |
1639 | case '=': | |
1640 | ret = O_le; | |
1641 | break; | |
1642 | } | |
6fad6acb | 1643 | *num_chars = 2; |
252b5132 RH |
1644 | return ret; |
1645 | ||
1646 | case '=': | |
1647 | if (input_line_pointer[1] != '=') | |
1648 | return op_encoding[c]; | |
1649 | ||
6fad6acb | 1650 | *num_chars = 2; |
252b5132 RH |
1651 | return O_eq; |
1652 | ||
1653 | case '>': | |
1654 | switch (input_line_pointer[1]) | |
1655 | { | |
1656 | default: | |
1657 | return op_encoding[c]; | |
1658 | case '>': | |
1659 | ret = O_right_shift; | |
1660 | break; | |
1661 | case '=': | |
1662 | ret = O_ge; | |
1663 | break; | |
1664 | } | |
6fad6acb | 1665 | *num_chars = 2; |
252b5132 RH |
1666 | return ret; |
1667 | ||
1668 | case '!': | |
723a8472 | 1669 | switch (input_line_pointer[1]) |
252b5132 | 1670 | { |
723a8472 NC |
1671 | case '!': |
1672 | /* We accept !! as equivalent to ^ for MRI compatibility. */ | |
1673 | *num_chars = 2; | |
1674 | return O_bit_exclusive_or; | |
1675 | case '=': | |
1676 | /* We accept != as equivalent to <>. */ | |
1677 | *num_chars = 2; | |
1678 | return O_ne; | |
1679 | default: | |
252b5132 RH |
1680 | if (flag_m68k_mri) |
1681 | return O_bit_inclusive_or; | |
1682 | return op_encoding[c]; | |
1683 | } | |
252b5132 RH |
1684 | |
1685 | case '|': | |
1686 | if (input_line_pointer[1] != '|') | |
1687 | return op_encoding[c]; | |
1688 | ||
6fad6acb | 1689 | *num_chars = 2; |
252b5132 RH |
1690 | return O_logical_or; |
1691 | ||
1692 | case '&': | |
1693 | if (input_line_pointer[1] != '&') | |
1694 | return op_encoding[c]; | |
1695 | ||
6fad6acb | 1696 | *num_chars = 2; |
252b5132 RH |
1697 | return O_logical_and; |
1698 | } | |
1699 | ||
929b12bc | 1700 | /* NOTREACHED */ |
252b5132 RH |
1701 | } |
1702 | ||
956a6ba3 JB |
1703 | /* Implement "word-size + 1 bit" addition for |
1704 | {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This | |
1705 | is used so that the full range of unsigned word values and the full range of | |
1706 | signed word values can be represented in an O_constant expression, which is | |
1707 | useful e.g. for .sleb128 directives. */ | |
1708 | ||
4455e9ad | 1709 | void |
956a6ba3 JB |
1710 | add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit) |
1711 | { | |
1712 | valueT ures = resultP->X_add_number; | |
1713 | valueT uamount = amount; | |
1714 | ||
c77a6ef6 | 1715 | resultP->X_add_number += uamount; |
956a6ba3 JB |
1716 | |
1717 | resultP->X_extrabit ^= rhs_highbit; | |
1718 | ||
1719 | if (ures + uamount < ures) | |
1720 | resultP->X_extrabit ^= 1; | |
1721 | } | |
1722 | ||
1723 | /* Similarly, for subtraction. */ | |
1724 | ||
4455e9ad | 1725 | void |
956a6ba3 JB |
1726 | subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit) |
1727 | { | |
1728 | valueT ures = resultP->X_add_number; | |
1729 | valueT uamount = amount; | |
1730 | ||
c77a6ef6 | 1731 | resultP->X_add_number -= uamount; |
956a6ba3 JB |
1732 | |
1733 | resultP->X_extrabit ^= rhs_highbit; | |
1734 | ||
1735 | if (ures < uamount) | |
1736 | resultP->X_extrabit ^= 1; | |
1737 | } | |
1738 | ||
252b5132 RH |
1739 | /* Parse an expression. */ |
1740 | ||
1741 | segT | |
dd625418 | 1742 | expr (int rankarg, /* Larger # is higher rank. */ |
9497f5ac NC |
1743 | expressionS *resultP, /* Deliver result here. */ |
1744 | enum expr_mode mode /* Controls behavior. */) | |
252b5132 | 1745 | { |
0561a208 | 1746 | operator_rankT rank = (operator_rankT) rankarg; |
252b5132 RH |
1747 | segT retval; |
1748 | expressionS right; | |
1749 | operatorT op_left; | |
1750 | operatorT op_right; | |
6fad6acb | 1751 | int op_chars; |
252b5132 | 1752 | |
db557034 | 1753 | know (rankarg >= 0); |
252b5132 | 1754 | |
26346241 AM |
1755 | /* Save the value of dot for the fixup code. */ |
1756 | if (rank == 0) | |
8e723a10 CLT |
1757 | { |
1758 | dot_value = frag_now_fix (); | |
1759 | dot_frag = frag_now; | |
1760 | } | |
26346241 | 1761 | |
9497f5ac | 1762 | retval = operand (resultP, mode); |
252b5132 | 1763 | |
929b12bc KH |
1764 | /* operand () gobbles spaces. */ |
1765 | know (*input_line_pointer != ' '); | |
252b5132 | 1766 | |
1e9cc1c2 | 1767 | op_left = operatorf (&op_chars); |
252b5132 RH |
1768 | while (op_left != O_illegal && op_rank[(int) op_left] > rank) |
1769 | { | |
1770 | segT rightseg; | |
08ea7020 | 1771 | offsetT frag_off; |
252b5132 | 1772 | |
6fad6acb | 1773 | input_line_pointer += op_chars; /* -> after operator. */ |
252b5132 | 1774 | |
34a7d6c6 | 1775 | right.X_md = 0; |
9497f5ac | 1776 | rightseg = expr (op_rank[(int) op_left], &right, mode); |
252b5132 RH |
1777 | if (right.X_op == O_absent) |
1778 | { | |
1779 | as_warn (_("missing operand; zero assumed")); | |
1780 | right.X_op = O_constant; | |
1781 | right.X_add_number = 0; | |
1782 | right.X_add_symbol = NULL; | |
1783 | right.X_op_symbol = NULL; | |
1784 | } | |
1785 | ||
1786 | know (*input_line_pointer != ' '); | |
1787 | ||
b585bc2c RH |
1788 | if (op_left == O_index) |
1789 | { | |
1790 | if (*input_line_pointer != ']') | |
1791 | as_bad ("missing right bracket"); | |
1792 | else | |
1793 | { | |
1794 | ++input_line_pointer; | |
1795 | SKIP_WHITESPACE (); | |
1796 | } | |
1797 | } | |
1798 | ||
1e9cc1c2 | 1799 | op_right = operatorf (&op_chars); |
252b5132 | 1800 | |
fcaed75e | 1801 | know (op_right == O_illegal || op_left == O_index |
929b12bc | 1802 | || op_rank[(int) op_right] <= op_rank[(int) op_left]); |
fcaed75e JB |
1803 | know ((int) op_left >= (int) O_multiply); |
1804 | #ifndef md_operator | |
1805 | know ((int) op_left <= (int) O_index); | |
1806 | #else | |
1807 | know ((int) op_left < (int) O_max); | |
1808 | #endif | |
252b5132 | 1809 | |
929b12bc KH |
1810 | /* input_line_pointer->after right-hand quantity. */ |
1811 | /* left-hand quantity in resultP. */ | |
1812 | /* right-hand quantity in right. */ | |
1813 | /* operator in op_left. */ | |
252b5132 RH |
1814 | |
1815 | if (resultP->X_op == O_big) | |
1816 | { | |
1817 | if (resultP->X_add_number > 0) | |
1818 | as_warn (_("left operand is a bignum; integer 0 assumed")); | |
1819 | else | |
1820 | as_warn (_("left operand is a float; integer 0 assumed")); | |
1821 | resultP->X_op = O_constant; | |
1822 | resultP->X_add_number = 0; | |
1823 | resultP->X_add_symbol = NULL; | |
1824 | resultP->X_op_symbol = NULL; | |
1825 | } | |
1826 | if (right.X_op == O_big) | |
1827 | { | |
1828 | if (right.X_add_number > 0) | |
1829 | as_warn (_("right operand is a bignum; integer 0 assumed")); | |
1830 | else | |
1831 | as_warn (_("right operand is a float; integer 0 assumed")); | |
1832 | right.X_op = O_constant; | |
1833 | right.X_add_number = 0; | |
1834 | right.X_add_symbol = NULL; | |
1835 | right.X_op_symbol = NULL; | |
1836 | } | |
1837 | ||
e4c2619a AM |
1838 | if (mode == expr_defer |
1839 | && ((resultP->X_add_symbol != NULL | |
1840 | && S_IS_FORWARD_REF (resultP->X_add_symbol)) | |
1841 | || (right.X_add_symbol != NULL | |
1842 | && S_IS_FORWARD_REF (right.X_add_symbol)))) | |
1843 | goto general; | |
1844 | ||
252b5132 | 1845 | /* Optimize common cases. */ |
800eeca4 JW |
1846 | #ifdef md_optimize_expr |
1847 | if (md_optimize_expr (resultP, op_left, &right)) | |
1848 | { | |
929b12bc KH |
1849 | /* Skip. */ |
1850 | ; | |
800eeca4 JW |
1851 | } |
1852 | else | |
1853 | #endif | |
5a918ce7 JB |
1854 | #ifndef md_register_arithmetic |
1855 | # define md_register_arithmetic 1 | |
1856 | #endif | |
1857 | if (op_left == O_add && right.X_op == O_constant | |
1858 | && (md_register_arithmetic || resultP->X_op != O_register)) | |
252b5132 RH |
1859 | { |
1860 | /* X + constant. */ | |
956a6ba3 | 1861 | add_to_result (resultP, right.X_add_number, right.X_extrabit); |
252b5132 RH |
1862 | } |
1863 | /* This case comes up in PIC code. */ | |
1864 | else if (op_left == O_subtract | |
1865 | && right.X_op == O_symbol | |
1866 | && resultP->X_op == O_symbol | |
99630778 | 1867 | && retval == rightseg |
bfff1642 NC |
1868 | #ifdef md_allow_local_subtract |
1869 | && md_allow_local_subtract (resultP, & right, rightseg) | |
1870 | #endif | |
c969da64 RS |
1871 | && ((SEG_NORMAL (rightseg) |
1872 | && !S_FORCE_RELOC (resultP->X_add_symbol, 0) | |
1873 | && !S_FORCE_RELOC (right.X_add_symbol, 0)) | |
99630778 AM |
1874 | || right.X_add_symbol == resultP->X_add_symbol) |
1875 | && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol), | |
1876 | symbol_get_frag (right.X_add_symbol), | |
1877 | &frag_off)) | |
252b5132 | 1878 | { |
956a6ba3 JB |
1879 | offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol) |
1880 | - S_GET_VALUE (right.X_add_symbol); | |
1881 | subtract_from_result (resultP, right.X_add_number, right.X_extrabit); | |
1882 | subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0); | |
1883 | add_to_result (resultP, symval_diff, symval_diff < 0); | |
252b5132 RH |
1884 | resultP->X_op = O_constant; |
1885 | resultP->X_add_symbol = 0; | |
1886 | } | |
5a918ce7 JB |
1887 | else if (op_left == O_subtract && right.X_op == O_constant |
1888 | && (md_register_arithmetic || resultP->X_op != O_register)) | |
252b5132 RH |
1889 | { |
1890 | /* X - constant. */ | |
956a6ba3 | 1891 | subtract_from_result (resultP, right.X_add_number, right.X_extrabit); |
252b5132 | 1892 | } |
5a918ce7 JB |
1893 | else if (op_left == O_add && resultP->X_op == O_constant |
1894 | && (md_register_arithmetic || right.X_op != O_register)) | |
252b5132 RH |
1895 | { |
1896 | /* Constant + X. */ | |
1897 | resultP->X_op = right.X_op; | |
1898 | resultP->X_add_symbol = right.X_add_symbol; | |
1899 | resultP->X_op_symbol = right.X_op_symbol; | |
956a6ba3 | 1900 | add_to_result (resultP, right.X_add_number, right.X_extrabit); |
252b5132 RH |
1901 | retval = rightseg; |
1902 | } | |
1903 | else if (resultP->X_op == O_constant && right.X_op == O_constant) | |
1904 | { | |
1905 | /* Constant OP constant. */ | |
1906 | offsetT v = right.X_add_number; | |
1907 | if (v == 0 && (op_left == O_divide || op_left == O_modulus)) | |
1908 | { | |
1909 | as_warn (_("division by zero")); | |
1910 | v = 1; | |
1911 | } | |
d85733c8 JB |
1912 | if ((valueT) v >= sizeof(valueT) * CHAR_BIT |
1913 | && (op_left == O_left_shift || op_left == O_right_shift)) | |
1914 | { | |
1915 | as_warn_value_out_of_range (_("shift count"), v, 0, | |
1916 | sizeof(valueT) * CHAR_BIT - 1, | |
1917 | NULL, 0); | |
1918 | resultP->X_add_number = v = 0; | |
1919 | } | |
252b5132 RH |
1920 | switch (op_left) |
1921 | { | |
fcaed75e | 1922 | default: goto general; |
252b5132 RH |
1923 | case O_multiply: resultP->X_add_number *= v; break; |
1924 | case O_divide: resultP->X_add_number /= v; break; | |
1925 | case O_modulus: resultP->X_add_number %= v; break; | |
b2f386b9 AM |
1926 | case O_left_shift: |
1927 | /* We always use unsigned shifts. According to the ISO | |
1928 | C standard, left shift of a signed type having a | |
1929 | negative value is undefined behaviour, and right | |
1930 | shift of a signed type having negative value is | |
1931 | implementation defined. Left shift of a signed type | |
1932 | when the result overflows is also undefined | |
1933 | behaviour. So don't trigger ubsan warnings or rely | |
1934 | on characteristics of the compiler. */ | |
1935 | resultP->X_add_number | |
1936 | = (valueT) resultP->X_add_number << (valueT) v; | |
1937 | break; | |
252b5132 | 1938 | case O_right_shift: |
b2f386b9 AM |
1939 | resultP->X_add_number |
1940 | = (valueT) resultP->X_add_number >> (valueT) v; | |
252b5132 RH |
1941 | break; |
1942 | case O_bit_inclusive_or: resultP->X_add_number |= v; break; | |
1943 | case O_bit_or_not: resultP->X_add_number |= ~v; break; | |
1944 | case O_bit_exclusive_or: resultP->X_add_number ^= v; break; | |
1945 | case O_bit_and: resultP->X_add_number &= v; break; | |
7b383517 BE |
1946 | /* Constant + constant (O_add) is handled by the |
1947 | previous if statement for constant + X, so is omitted | |
1948 | here. */ | |
956a6ba3 JB |
1949 | case O_subtract: |
1950 | subtract_from_result (resultP, v, 0); | |
1951 | break; | |
252b5132 RH |
1952 | case O_eq: |
1953 | resultP->X_add_number = | |
958b5f01 | 1954 | resultP->X_add_number == v ? ~ (offsetT) 0 : 0; |
252b5132 RH |
1955 | break; |
1956 | case O_ne: | |
1957 | resultP->X_add_number = | |
958b5f01 | 1958 | resultP->X_add_number != v ? ~ (offsetT) 0 : 0; |
252b5132 RH |
1959 | break; |
1960 | case O_lt: | |
1961 | resultP->X_add_number = | |
958b5f01 | 1962 | resultP->X_add_number < v ? ~ (offsetT) 0 : 0; |
252b5132 RH |
1963 | break; |
1964 | case O_le: | |
1965 | resultP->X_add_number = | |
958b5f01 | 1966 | resultP->X_add_number <= v ? ~ (offsetT) 0 : 0; |
252b5132 RH |
1967 | break; |
1968 | case O_ge: | |
1969 | resultP->X_add_number = | |
958b5f01 | 1970 | resultP->X_add_number >= v ? ~ (offsetT) 0 : 0; |
252b5132 RH |
1971 | break; |
1972 | case O_gt: | |
1973 | resultP->X_add_number = | |
958b5f01 | 1974 | resultP->X_add_number > v ? ~ (offsetT) 0 : 0; |
252b5132 RH |
1975 | break; |
1976 | case O_logical_and: | |
1977 | resultP->X_add_number = resultP->X_add_number && v; | |
1978 | break; | |
1979 | case O_logical_or: | |
1980 | resultP->X_add_number = resultP->X_add_number || v; | |
1981 | break; | |
1982 | } | |
1983 | } | |
1984 | else if (resultP->X_op == O_symbol | |
1985 | && right.X_op == O_symbol | |
1986 | && (op_left == O_add | |
1987 | || op_left == O_subtract | |
1988 | || (resultP->X_add_number == 0 | |
1989 | && right.X_add_number == 0))) | |
1990 | { | |
1991 | /* Symbol OP symbol. */ | |
1992 | resultP->X_op = op_left; | |
1993 | resultP->X_op_symbol = right.X_add_symbol; | |
1994 | if (op_left == O_add) | |
956a6ba3 | 1995 | add_to_result (resultP, right.X_add_number, right.X_extrabit); |
252b5132 | 1996 | else if (op_left == O_subtract) |
e0890092 | 1997 | { |
956a6ba3 JB |
1998 | subtract_from_result (resultP, right.X_add_number, |
1999 | right.X_extrabit); | |
c969da64 RS |
2000 | if (retval == rightseg |
2001 | && SEG_NORMAL (retval) | |
2002 | && !S_FORCE_RELOC (resultP->X_add_symbol, 0) | |
2003 | && !S_FORCE_RELOC (right.X_add_symbol, 0)) | |
e0890092 AM |
2004 | { |
2005 | retval = absolute_section; | |
2006 | rightseg = absolute_section; | |
2007 | } | |
2008 | } | |
252b5132 RH |
2009 | } |
2010 | else | |
2011 | { | |
fcaed75e | 2012 | general: |
252b5132 RH |
2013 | /* The general case. */ |
2014 | resultP->X_add_symbol = make_expr_symbol (resultP); | |
2015 | resultP->X_op_symbol = make_expr_symbol (&right); | |
2016 | resultP->X_op = op_left; | |
2017 | resultP->X_add_number = 0; | |
2018 | resultP->X_unsigned = 1; | |
956a6ba3 | 2019 | resultP->X_extrabit = 0; |
252b5132 RH |
2020 | } |
2021 | ||
e0890092 AM |
2022 | if (retval != rightseg) |
2023 | { | |
1e0f6894 AM |
2024 | if (retval == undefined_section) |
2025 | ; | |
2026 | else if (rightseg == undefined_section) | |
2027 | retval = rightseg; | |
2028 | else if (retval == expr_section) | |
2029 | ; | |
2030 | else if (rightseg == expr_section) | |
2031 | retval = rightseg; | |
2032 | else if (retval == reg_section) | |
2033 | ; | |
2034 | else if (rightseg == reg_section) | |
2035 | retval = rightseg; | |
2036 | else if (rightseg == absolute_section) | |
2037 | ; | |
2038 | else if (retval == absolute_section) | |
2039 | retval = rightseg; | |
e0890092 | 2040 | #ifdef DIFF_EXPR_OK |
1e0f6894 AM |
2041 | else if (op_left == O_subtract) |
2042 | ; | |
e0890092 | 2043 | #endif |
1e0f6894 | 2044 | else |
e0890092 AM |
2045 | as_bad (_("operation combines symbols in different segments")); |
2046 | } | |
2047 | ||
252b5132 | 2048 | op_left = op_right; |
929b12bc | 2049 | } /* While next operator is >= this rank. */ |
252b5132 RH |
2050 | |
2051 | /* The PA port needs this information. */ | |
2052 | if (resultP->X_add_symbol) | |
49309057 | 2053 | symbol_mark_used (resultP->X_add_symbol); |
252b5132 | 2054 | |
9497f5ac NC |
2055 | if (rank == 0 && mode == expr_evaluate) |
2056 | resolve_expression (resultP); | |
2057 | ||
252b5132 RH |
2058 | return resultP->X_op == O_constant ? absolute_section : retval; |
2059 | } | |
9497f5ac NC |
2060 | |
2061 | /* Resolve an expression without changing any symbols/sub-expressions | |
2062 | used. */ | |
2063 | ||
2064 | int | |
2065 | resolve_expression (expressionS *expressionP) | |
2066 | { | |
2067 | /* Help out with CSE. */ | |
2068 | valueT final_val = expressionP->X_add_number; | |
2069 | symbolS *add_symbol = expressionP->X_add_symbol; | |
14610ad1 | 2070 | symbolS *orig_add_symbol = add_symbol; |
9497f5ac NC |
2071 | symbolS *op_symbol = expressionP->X_op_symbol; |
2072 | operatorT op = expressionP->X_op; | |
2073 | valueT left, right; | |
2074 | segT seg_left, seg_right; | |
2075 | fragS *frag_left, *frag_right; | |
08ea7020 | 2076 | offsetT frag_off; |
9497f5ac NC |
2077 | |
2078 | switch (op) | |
2079 | { | |
2080 | default: | |
2081 | return 0; | |
2082 | ||
2083 | case O_constant: | |
2084 | case O_register: | |
2085 | left = 0; | |
2086 | break; | |
2087 | ||
2088 | case O_symbol: | |
2089 | case O_symbol_rva: | |
2e1e12b1 | 2090 | if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)) |
9497f5ac NC |
2091 | return 0; |
2092 | ||
2093 | break; | |
2094 | ||
2095 | case O_uminus: | |
2096 | case O_bit_not: | |
2097 | case O_logical_not: | |
2e1e12b1 | 2098 | if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)) |
9497f5ac NC |
2099 | return 0; |
2100 | ||
2101 | if (seg_left != absolute_section) | |
2102 | return 0; | |
2103 | ||
2104 | if (op == O_logical_not) | |
2105 | left = !left; | |
2106 | else if (op == O_uminus) | |
2107 | left = -left; | |
2108 | else | |
2109 | left = ~left; | |
2110 | op = O_constant; | |
2111 | break; | |
2112 | ||
2113 | case O_multiply: | |
2114 | case O_divide: | |
2115 | case O_modulus: | |
2116 | case O_left_shift: | |
2117 | case O_right_shift: | |
2118 | case O_bit_inclusive_or: | |
2119 | case O_bit_or_not: | |
2120 | case O_bit_exclusive_or: | |
2121 | case O_bit_and: | |
2122 | case O_add: | |
2123 | case O_subtract: | |
2124 | case O_eq: | |
2125 | case O_ne: | |
2126 | case O_lt: | |
2127 | case O_le: | |
2128 | case O_ge: | |
2129 | case O_gt: | |
2130 | case O_logical_and: | |
2131 | case O_logical_or: | |
2e1e12b1 JB |
2132 | if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left) |
2133 | || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right)) | |
9497f5ac NC |
2134 | return 0; |
2135 | ||
2136 | /* Simplify addition or subtraction of a constant by folding the | |
2137 | constant into X_add_number. */ | |
2138 | if (op == O_add) | |
2139 | { | |
2140 | if (seg_right == absolute_section) | |
2141 | { | |
2142 | final_val += right; | |
2143 | op = O_symbol; | |
2144 | break; | |
2145 | } | |
2146 | else if (seg_left == absolute_section) | |
2147 | { | |
2148 | final_val += left; | |
2149 | left = right; | |
2150 | seg_left = seg_right; | |
2e1e12b1 | 2151 | add_symbol = op_symbol; |
14610ad1 | 2152 | orig_add_symbol = expressionP->X_op_symbol; |
9497f5ac NC |
2153 | op = O_symbol; |
2154 | break; | |
2155 | } | |
2156 | } | |
2157 | else if (op == O_subtract) | |
2158 | { | |
2159 | if (seg_right == absolute_section) | |
2160 | { | |
2161 | final_val -= right; | |
2162 | op = O_symbol; | |
2163 | break; | |
2164 | } | |
2165 | } | |
2166 | ||
2167 | /* Equality and non-equality tests are permitted on anything. | |
2168 | Subtraction, and other comparison operators are permitted if | |
2e1e12b1 JB |
2169 | both operands are in the same section. |
2170 | Shifts by constant zero are permitted on anything. | |
2171 | Multiplies, bit-ors, and bit-ands with constant zero are | |
2172 | permitted on anything. | |
2173 | Multiplies and divides by constant one are permitted on | |
2174 | anything. | |
2175 | Binary operations with both operands being the same register | |
2176 | or undefined symbol are permitted if the result doesn't depend | |
2177 | on the input value. | |
2178 | Otherwise, both operands must be absolute. We already handled | |
2179 | the case of addition or subtraction of a constant above. */ | |
99630778 | 2180 | frag_off = 0; |
9497f5ac NC |
2181 | if (!(seg_left == absolute_section |
2182 | && seg_right == absolute_section) | |
2183 | && !(op == O_eq || op == O_ne) | |
2184 | && !((op == O_subtract | |
2185 | || op == O_lt || op == O_le || op == O_ge || op == O_gt) | |
2186 | && seg_left == seg_right | |
99630778 | 2187 | && (finalize_syms |
38c3873e AO |
2188 | || frag_offset_fixed_p (frag_left, frag_right, &frag_off) |
2189 | || (op == O_gt | |
2190 | && frag_gtoffset_p (left, frag_left, | |
2191 | right, frag_right, &frag_off))) | |
2e1e12b1 JB |
2192 | && (seg_left != reg_section || left == right) |
2193 | && (seg_left != undefined_section || add_symbol == op_symbol))) | |
2194 | { | |
2195 | if ((seg_left == absolute_section && left == 0) | |
2196 | || (seg_right == absolute_section && right == 0)) | |
2197 | { | |
2198 | if (op == O_bit_exclusive_or || op == O_bit_inclusive_or) | |
2199 | { | |
14610ad1 | 2200 | if (!(seg_right == absolute_section && right == 0)) |
2e1e12b1 JB |
2201 | { |
2202 | seg_left = seg_right; | |
2203 | left = right; | |
2204 | add_symbol = op_symbol; | |
14610ad1 | 2205 | orig_add_symbol = expressionP->X_op_symbol; |
2e1e12b1 JB |
2206 | } |
2207 | op = O_symbol; | |
2208 | break; | |
2209 | } | |
2210 | else if (op == O_left_shift || op == O_right_shift) | |
2211 | { | |
14610ad1 | 2212 | if (!(seg_left == absolute_section && left == 0)) |
2e1e12b1 JB |
2213 | { |
2214 | op = O_symbol; | |
2215 | break; | |
2216 | } | |
2217 | } | |
2218 | else if (op != O_multiply | |
2219 | && op != O_bit_or_not && op != O_bit_and) | |
2220 | return 0; | |
2221 | } | |
2222 | else if (op == O_multiply | |
2223 | && seg_left == absolute_section && left == 1) | |
2224 | { | |
2225 | seg_left = seg_right; | |
2226 | left = right; | |
2227 | add_symbol = op_symbol; | |
14610ad1 | 2228 | orig_add_symbol = expressionP->X_op_symbol; |
2e1e12b1 JB |
2229 | op = O_symbol; |
2230 | break; | |
2231 | } | |
2232 | else if ((op == O_multiply || op == O_divide) | |
2233 | && seg_right == absolute_section && right == 1) | |
2234 | { | |
2235 | op = O_symbol; | |
2236 | break; | |
2237 | } | |
14610ad1 AM |
2238 | else if (!(left == right |
2239 | && ((seg_left == reg_section && seg_right == reg_section) | |
2240 | || (seg_left == undefined_section | |
2241 | && seg_right == undefined_section | |
2242 | && add_symbol == op_symbol)))) | |
2e1e12b1 JB |
2243 | return 0; |
2244 | else if (op == O_bit_and || op == O_bit_inclusive_or) | |
2245 | { | |
2246 | op = O_symbol; | |
2247 | break; | |
2248 | } | |
2249 | else if (op != O_bit_exclusive_or && op != O_bit_or_not) | |
2250 | return 0; | |
2251 | } | |
9497f5ac | 2252 | |
99630778 | 2253 | right += frag_off / OCTETS_PER_BYTE; |
9497f5ac NC |
2254 | switch (op) |
2255 | { | |
2256 | case O_add: left += right; break; | |
2257 | case O_subtract: left -= right; break; | |
2258 | case O_multiply: left *= right; break; | |
2259 | case O_divide: | |
2260 | if (right == 0) | |
2261 | return 0; | |
2262 | left = (offsetT) left / (offsetT) right; | |
2263 | break; | |
2264 | case O_modulus: | |
2265 | if (right == 0) | |
2266 | return 0; | |
2267 | left = (offsetT) left % (offsetT) right; | |
2268 | break; | |
2269 | case O_left_shift: left <<= right; break; | |
2270 | case O_right_shift: left >>= right; break; | |
2271 | case O_bit_inclusive_or: left |= right; break; | |
2272 | case O_bit_or_not: left |= ~right; break; | |
2273 | case O_bit_exclusive_or: left ^= right; break; | |
2274 | case O_bit_and: left &= right; break; | |
2275 | case O_eq: | |
2276 | case O_ne: | |
2277 | left = (left == right | |
2278 | && seg_left == seg_right | |
2279 | && (finalize_syms || frag_left == frag_right) | |
2e1e12b1 | 2280 | && (seg_left != undefined_section |
9497f5ac NC |
2281 | || add_symbol == op_symbol) |
2282 | ? ~ (valueT) 0 : 0); | |
2283 | if (op == O_ne) | |
2284 | left = ~left; | |
2285 | break; | |
2286 | case O_lt: | |
2287 | left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0; | |
2288 | break; | |
2289 | case O_le: | |
2290 | left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0; | |
2291 | break; | |
2292 | case O_ge: | |
2293 | left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0; | |
2294 | break; | |
2295 | case O_gt: | |
2296 | left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0; | |
2297 | break; | |
2298 | case O_logical_and: left = left && right; break; | |
2299 | case O_logical_or: left = left || right; break; | |
2300 | default: abort (); | |
2301 | } | |
2302 | ||
2303 | op = O_constant; | |
2304 | break; | |
2305 | } | |
2306 | ||
2307 | if (op == O_symbol) | |
2308 | { | |
2309 | if (seg_left == absolute_section) | |
2310 | op = O_constant; | |
2311 | else if (seg_left == reg_section && final_val == 0) | |
2312 | op = O_register; | |
087d837e | 2313 | else if (!symbol_same_p (add_symbol, orig_add_symbol)) |
2e1e12b1 JB |
2314 | final_val += left; |
2315 | expressionP->X_add_symbol = add_symbol; | |
9497f5ac NC |
2316 | } |
2317 | expressionP->X_op = op; | |
2318 | ||
2319 | if (op == O_constant || op == O_register) | |
2320 | final_val += left; | |
2321 | expressionP->X_add_number = final_val; | |
2322 | ||
2323 | return 1; | |
2324 | } | |
252b5132 | 2325 | \f |
929b12bc KH |
2326 | /* This lives here because it belongs equally in expr.c & read.c. |
2327 | expr.c is just a branch office read.c anyway, and putting it | |
2328 | here lessens the crowd at read.c. | |
2329 | ||
d02603dc NC |
2330 | Assume input_line_pointer is at start of symbol name, or the |
2331 | start of a double quote enclosed symbol name. | |
929b12bc | 2332 | Advance input_line_pointer past symbol name. |
d02603dc NC |
2333 | Turn that character into a '\0', returning its former value, |
2334 | which may be the closing double quote. | |
929b12bc | 2335 | This allows a string compare (RMS wants symbol names to be strings) |
d02603dc | 2336 | of the symbol name. |
929b12bc KH |
2337 | There will always be a char following symbol name, because all good |
2338 | lines end in end-of-line. */ | |
2339 | ||
252b5132 | 2340 | char |
d02603dc | 2341 | get_symbol_name (char ** ilp_return) |
252b5132 RH |
2342 | { |
2343 | char c; | |
2344 | ||
d02603dc | 2345 | * ilp_return = input_line_pointer; |
2469b3c5 | 2346 | /* We accept FAKE_LABEL_CHAR in a name in case this is being called with a |
252b5132 | 2347 | constructed string. */ |
2469b3c5 JW |
2348 | if (is_name_beginner (c = *input_line_pointer++) |
2349 | || (input_from_string && c == FAKE_LABEL_CHAR)) | |
58b5739a RH |
2350 | { |
2351 | while (is_part_of_name (c = *input_line_pointer++) | |
2469b3c5 | 2352 | || (input_from_string && c == FAKE_LABEL_CHAR)) |
58b5739a RH |
2353 | ; |
2354 | if (is_name_ender (c)) | |
2355 | c = *input_line_pointer++; | |
2356 | } | |
d02603dc NC |
2357 | else if (c == '"') |
2358 | { | |
5b7c81bd | 2359 | bool backslash_seen; |
d02603dc NC |
2360 | |
2361 | * ilp_return = input_line_pointer; | |
2362 | do | |
2363 | { | |
2364 | backslash_seen = c == '\\'; | |
2365 | c = * input_line_pointer ++; | |
2366 | } | |
2367 | while (c != 0 && (c != '"' || backslash_seen)); | |
2368 | ||
2369 | if (c == 0) | |
2370 | as_warn (_("missing closing '\"'")); | |
2371 | } | |
252b5132 | 2372 | *--input_line_pointer = 0; |
d02603dc NC |
2373 | return c; |
2374 | } | |
2375 | ||
2376 | /* Replace the NUL character pointed to by input_line_pointer | |
2377 | with C. If C is \" then advance past it. Return the character | |
2378 | now pointed to by input_line_pointer. */ | |
2379 | ||
2380 | char | |
2381 | restore_line_pointer (char c) | |
2382 | { | |
2383 | * input_line_pointer = c; | |
2384 | if (c == '"') | |
2385 | c = * ++ input_line_pointer; | |
2386 | return c; | |
252b5132 RH |
2387 | } |
2388 | ||
252b5132 | 2389 | unsigned int |
dd625418 | 2390 | get_single_number (void) |
252b5132 RH |
2391 | { |
2392 | expressionS exp; | |
9497f5ac | 2393 | operand (&exp, expr_normal); |
252b5132 | 2394 | return exp.X_add_number; |
252b5132 | 2395 | } |