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