]>
Commit | Line | Data |
---|---|---|
fecd2382 | 1 | /* expr.c -operands, expressions- |
d90f530b | 2 | Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
2ed83a59 | 3 | |
a39116f1 | 4 | This file is part of GAS, the GNU Assembler. |
2ed83a59 | 5 | |
a39116f1 RP |
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 2, or (at your option) | |
9 | any later version. | |
2ed83a59 | 10 | |
a39116f1 RP |
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. | |
2ed83a59 | 15 | |
a39116f1 RP |
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 | |
219deb70 | 18 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
fecd2382 RP |
19 | |
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 | ||
27 | #include <ctype.h> | |
28 | #include <string.h> | |
29 | ||
30 | #include "as.h" | |
3a762a0b | 31 | #include "libiberty.h" |
fecd2382 RP |
32 | #include "obstack.h" |
33 | ||
d4c8cbd8 JL |
34 | static void floating_constant PARAMS ((expressionS * expressionP)); |
35 | static void integer_constant PARAMS ((int radix, expressionS * expressionP)); | |
219deb70 | 36 | static void mri_char_constant PARAMS ((expressionS *)); |
52e1cf9d | 37 | static void current_location PARAMS ((expressionS *)); |
f2f7d044 | 38 | static void clean_up_expression PARAMS ((expressionS * expressionP)); |
5ac34ac3 | 39 | |
f2f7d044 | 40 | extern const char EXP_CHARS[], FLT_CHARS[]; |
5ac34ac3 ILT |
41 | \f |
42 | /* Build a dummy symbol to hold a complex expression. This is how we | |
43 | build expressions up out of other expressions. The symbol is put | |
44 | into the fake section expr_section. */ | |
45 | ||
3a762a0b | 46 | symbolS * |
5ac34ac3 ILT |
47 | make_expr_symbol (expressionP) |
48 | expressionS *expressionP; | |
49 | { | |
50 | const char *fake; | |
51 | symbolS *symbolP; | |
fecd2382 | 52 | |
d90f530b KR |
53 | if (expressionP->X_op == O_symbol |
54 | && expressionP->X_add_number == 0) | |
55 | return expressionP->X_add_symbol; | |
56 | ||
5ac34ac3 ILT |
57 | /* FIXME: This should be something which decode_local_label_name |
58 | will handle. */ | |
d4c8cbd8 JL |
59 | fake = FAKE_LABEL_NAME; |
60 | ||
5ac34ac3 ILT |
61 | /* Putting constant symbols in absolute_section rather than |
62 | expr_section is convenient for the old a.out code, for which | |
63 | S_GET_SEGMENT does not always retrieve the value put in by | |
64 | S_SET_SEGMENT. */ | |
3a762a0b KR |
65 | symbolP = symbol_create (fake, |
66 | (expressionP->X_op == O_constant | |
67 | ? absolute_section | |
68 | : expr_section), | |
69 | 0, &zero_address_frag); | |
5ac34ac3 | 70 | symbolP->sy_value = *expressionP; |
d90f530b KR |
71 | |
72 | if (expressionP->X_op == O_constant) | |
73 | resolve_symbol_value (symbolP); | |
74 | ||
5ac34ac3 ILT |
75 | return symbolP; |
76 | } | |
77 | \f | |
fecd2382 RP |
78 | /* |
79 | * Build any floating-point literal here. | |
80 | * Also build any bignum literal here. | |
81 | */ | |
82 | ||
fecd2382 RP |
83 | /* Seems atof_machine can backscan through generic_bignum and hit whatever |
84 | happens to be loaded before it in memory. And its way too complicated | |
85 | for me to fix right. Thus a hack. JF: Just make generic_bignum bigger, | |
86 | and never write into the early words, thus they'll always be zero. | |
f2f7d044 | 87 | I hate Dean's floating-point code. Bleh. */ |
2ed83a59 KR |
88 | LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6]; |
89 | FLONUM_TYPE generic_floating_point_number = | |
fecd2382 | 90 | { |
2ed83a59 | 91 | &generic_bignum[6], /* low (JF: Was 0) */ |
351878df | 92 | &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */ |
2ed83a59 KR |
93 | 0, /* leader */ |
94 | 0, /* exponent */ | |
95 | 0 /* sign */ | |
96 | }; | |
fecd2382 RP |
97 | /* If nonzero, we've been asked to assemble nan, +inf or -inf */ |
98 | int generic_floating_point_magic; | |
99 | \f | |
d4c8cbd8 | 100 | static void |
2ed83a59 KR |
101 | floating_constant (expressionP) |
102 | expressionS *expressionP; | |
c593cf41 SC |
103 | { |
104 | /* input_line_pointer->*/ | |
105 | /* floating-point constant. */ | |
106 | int error_code; | |
107 | ||
351878df KR |
108 | error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS, |
109 | &generic_floating_point_number); | |
c593cf41 SC |
110 | |
111 | if (error_code) | |
c593cf41 | 112 | { |
2ed83a59 KR |
113 | if (error_code == ERROR_EXPONENT_OVERFLOW) |
114 | { | |
115 | as_bad ("bad floating-point constant: exponent overflow, probably assembling junk"); | |
116 | } | |
117 | else | |
118 | { | |
119 | as_bad ("bad floating-point constant: unknown error code=%d.", error_code); | |
120 | } | |
c593cf41 | 121 | } |
5ac34ac3 | 122 | expressionP->X_op = O_big; |
c593cf41 SC |
123 | /* input_line_pointer->just after constant, */ |
124 | /* which may point to whitespace. */ | |
2ed83a59 | 125 | expressionP->X_add_number = -1; |
c593cf41 SC |
126 | } |
127 | ||
d4c8cbd8 | 128 | static void |
2ed83a59 KR |
129 | integer_constant (radix, expressionP) |
130 | int radix; | |
131 | expressionS *expressionP; | |
c593cf41 | 132 | { |
7691379e | 133 | char *start; /* start of number. */ |
219deb70 | 134 | char *suffix = NULL; |
c593cf41 | 135 | char c; |
dae92eab KR |
136 | valueT number; /* offset or (absolute) value */ |
137 | short int digit; /* value of next digit in current radix */ | |
138 | short int maxdig = 0;/* highest permitted digit value. */ | |
139 | int too_many_digits = 0; /* if we see >= this number of */ | |
140 | char *name; /* points to name of symbol */ | |
141 | symbolS *symbolP; /* points to symbol */ | |
2ed83a59 KR |
142 | |
143 | int small; /* true if fits in 32 bits. */ | |
2ed83a59 | 144 | |
dae92eab KR |
145 | /* May be bignum, or may fit in 32 bits. */ |
146 | /* Most numbers fit into 32 bits, and we want this case to be fast. | |
147 | so we pretend it will fit into 32 bits. If, after making up a 32 | |
148 | bit number, we realise that we have scanned more digits than | |
149 | comfortably fit into 32 bits, we re-scan the digits coding them | |
150 | into a bignum. For decimal and octal numbers we are | |
151 | conservative: Some numbers may be assumed bignums when in fact | |
152 | they do fit into 32 bits. Numbers of any radix can have excess | |
153 | leading zeros: We strive to recognise this and cast them back | |
154 | into 32 bits. We must check that the bignum really is more than | |
155 | 32 bits, and change it back to a 32-bit number if it fits. The | |
156 | number we are looking for is expected to be positive, but if it | |
157 | fits into 32 bits as an unsigned number, we let it be a 32-bit | |
158 | number. The cavalier approach is for speed in ordinary cases. */ | |
58d4951d ILT |
159 | /* This has been extended for 64 bits. We blindly assume that if |
160 | you're compiling in 64-bit mode, the target is a 64-bit machine. | |
161 | This should be cleaned up. */ | |
162 | ||
163 | #ifdef BFD64 | |
164 | #define valuesize 64 | |
165 | #else /* includes non-bfd case, mostly */ | |
166 | #define valuesize 32 | |
167 | #endif | |
2ed83a59 | 168 | |
219deb70 ILT |
169 | if (flag_mri && radix == 0) |
170 | { | |
171 | int flt = 0; | |
172 | ||
173 | /* In MRI mode, the number may have a suffix indicating the | |
174 | radix. For that matter, it might actually be a floating | |
175 | point constant. */ | |
176 | for (suffix = input_line_pointer; isalnum (*suffix); suffix++) | |
177 | { | |
178 | if (*suffix == 'e' || *suffix == 'E') | |
179 | flt = 1; | |
180 | } | |
181 | ||
182 | if (suffix == input_line_pointer) | |
183 | { | |
184 | radix = 10; | |
185 | suffix = NULL; | |
186 | } | |
187 | else | |
188 | { | |
189 | c = *--suffix; | |
190 | if (islower (c)) | |
191 | c = toupper (c); | |
192 | if (c == 'B') | |
193 | radix = 2; | |
194 | else if (c == 'D') | |
195 | radix = 10; | |
196 | else if (c == 'O' || c == 'Q') | |
197 | radix = 8; | |
198 | else if (c == 'H') | |
199 | radix = 16; | |
200 | else if (suffix[1] == '.' || c == 'E' || flt) | |
201 | { | |
202 | floating_constant (expressionP); | |
203 | return; | |
204 | } | |
205 | else | |
206 | { | |
207 | radix = 10; | |
208 | suffix = NULL; | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
2ed83a59 | 213 | switch (radix) |
f8701a3f | 214 | { |
f8701a3f SC |
215 | case 2: |
216 | maxdig = 2; | |
58d4951d | 217 | too_many_digits = valuesize + 1; |
f8701a3f SC |
218 | break; |
219 | case 8: | |
220 | maxdig = radix = 8; | |
7691379e | 221 | too_many_digits = (valuesize + 2) / 3 + 1; |
f8701a3f SC |
222 | break; |
223 | case 16: | |
f8701a3f | 224 | maxdig = radix = 16; |
7691379e | 225 | too_many_digits = (valuesize + 3) / 4 + 1; |
f8701a3f SC |
226 | break; |
227 | case 10: | |
228 | maxdig = radix = 10; | |
58d4951d | 229 | too_many_digits = (valuesize + 12) / 4; /* very rough */ |
f8701a3f | 230 | } |
58d4951d | 231 | #undef valuesize |
7691379e KR |
232 | start = input_line_pointer; |
233 | c = *input_line_pointer++; | |
58d4951d | 234 | for (number = 0; |
3a762a0b | 235 | (digit = hex_value (c)) < maxdig; |
58d4951d | 236 | c = *input_line_pointer++) |
f8701a3f SC |
237 | { |
238 | number = number * radix + digit; | |
239 | } | |
c593cf41 SC |
240 | /* c contains character after number. */ |
241 | /* input_line_pointer->char after c. */ | |
7691379e | 242 | small = (input_line_pointer - start - 1) < too_many_digits; |
2ed83a59 | 243 | if (!small) |
c593cf41 | 244 | { |
f8701a3f SC |
245 | /* |
246 | * we saw a lot of digits. manufacture a bignum the hard way. | |
247 | */ | |
2ed83a59 KR |
248 | LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */ |
249 | LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */ | |
f8701a3f | 250 | long carry; |
2ed83a59 | 251 | |
f8701a3f | 252 | leader = generic_bignum; |
2ed83a59 KR |
253 | generic_bignum[0] = 0; |
254 | generic_bignum[1] = 0; | |
7691379e | 255 | input_line_pointer = start; /*->1st digit. */ |
f8701a3f | 256 | c = *input_line_pointer++; |
58d4951d | 257 | for (; |
3a762a0b | 258 | (carry = hex_value (c)) < maxdig; |
58d4951d | 259 | c = *input_line_pointer++) |
f8701a3f SC |
260 | { |
261 | for (pointer = generic_bignum; | |
262 | pointer <= leader; | |
263 | pointer++) | |
264 | { | |
265 | long work; | |
2ed83a59 KR |
266 | |
267 | work = carry + radix * *pointer; | |
f8701a3f SC |
268 | *pointer = work & LITTLENUM_MASK; |
269 | carry = work >> LITTLENUM_NUMBER_OF_BITS; | |
270 | } | |
271 | if (carry) | |
272 | { | |
273 | if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1) | |
351878df KR |
274 | { |
275 | /* room to grow a longer bignum. */ | |
f8701a3f SC |
276 | *++leader = carry; |
277 | } | |
278 | } | |
279 | } | |
280 | /* again, c is char after number, */ | |
281 | /* input_line_pointer->after c. */ | |
2ed83a59 | 282 | know (LITTLENUM_NUMBER_OF_BITS == 16); |
7691379e | 283 | if (leader < generic_bignum + 2) |
351878df KR |
284 | { |
285 | /* will fit into 32 bits. */ | |
f8701a3f | 286 | number = |
2ed83a59 KR |
287 | ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS) |
288 | | (generic_bignum[0] & LITTLENUM_MASK); | |
f8701a3f SC |
289 | small = 1; |
290 | } | |
291 | else | |
292 | { | |
2ed83a59 | 293 | number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */ |
c593cf41 | 294 | } |
c593cf41 | 295 | } |
219deb70 ILT |
296 | |
297 | if (flag_mri && suffix != NULL && input_line_pointer - 1 == suffix) | |
298 | c = *input_line_pointer++; | |
299 | ||
2ed83a59 KR |
300 | if (small) |
301 | { | |
f8701a3f | 302 | /* |
2ed83a59 KR |
303 | * here with number, in correct radix. c is the next char. |
304 | * note that unlike un*x, we allow "011f" "0x9f" to | |
305 | * both mean the same as the (conventional) "9f". this is simply easier | |
306 | * than checking for strict canonical form. syntax sux! | |
f8701a3f | 307 | */ |
2ed83a59 | 308 | |
219deb70 | 309 | if (LOCAL_LABELS_FB && c == 'b') |
2ed83a59 | 310 | { |
219deb70 ILT |
311 | /* |
312 | * backward ref to local label. | |
313 | * because it is backward, expect it to be defined. | |
314 | */ | |
315 | /* Construct a local label. */ | |
316 | name = fb_label_name ((int) number, 0); | |
2ed83a59 | 317 | |
219deb70 ILT |
318 | /* seen before, or symbol is defined: ok */ |
319 | symbolP = symbol_find (name); | |
320 | if ((symbolP != NULL) && (S_IS_DEFINED (symbolP))) | |
321 | { | |
322 | /* local labels are never absolute. don't waste time | |
323 | checking absoluteness. */ | |
324 | know (SEG_NORMAL (S_GET_SEGMENT (symbolP))); | |
2ed83a59 | 325 | |
219deb70 ILT |
326 | expressionP->X_op = O_symbol; |
327 | expressionP->X_add_symbol = symbolP; | |
328 | } | |
329 | else | |
330 | { | |
331 | /* either not seen or not defined. */ | |
332 | /* @@ Should print out the original string instead of | |
333 | the parsed number. */ | |
334 | as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.", | |
335 | (int) number); | |
336 | expressionP->X_op = O_constant; | |
337 | } | |
2ed83a59 | 338 | |
219deb70 ILT |
339 | expressionP->X_add_number = 0; |
340 | } /* case 'b' */ | |
341 | else if (LOCAL_LABELS_FB && c == 'f') | |
342 | { | |
343 | /* | |
344 | * forward reference. expect symbol to be undefined or | |
345 | * unknown. undefined: seen it before. unknown: never seen | |
346 | * it before. | |
347 | * construct a local label name, then an undefined symbol. | |
348 | * don't create a xseg frag for it: caller may do that. | |
349 | * just return it as never seen before. | |
350 | */ | |
351 | name = fb_label_name ((int) number, 1); | |
352 | symbolP = symbol_find_or_make (name); | |
353 | /* we have no need to check symbol properties. */ | |
c593cf41 | 354 | #ifndef many_segments |
219deb70 ILT |
355 | /* since "know" puts its arg into a "string", we |
356 | can't have newlines in the argument. */ | |
357 | know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section); | |
c593cf41 | 358 | #endif |
219deb70 ILT |
359 | expressionP->X_op = O_symbol; |
360 | expressionP->X_add_symbol = symbolP; | |
361 | expressionP->X_add_number = 0; | |
362 | } /* case 'f' */ | |
363 | else if (LOCAL_LABELS_DOLLAR && c == '$') | |
364 | { | |
365 | /* If the dollar label is *currently* defined, then this is just | |
366 | another reference to it. If it is not *currently* defined, | |
367 | then this is a fresh instantiation of that number, so create | |
368 | it. */ | |
2ed83a59 | 369 | |
219deb70 ILT |
370 | if (dollar_label_defined ((long) number)) |
371 | { | |
372 | name = dollar_label_name ((long) number, 0); | |
373 | symbolP = symbol_find (name); | |
374 | know (symbolP != NULL); | |
375 | } | |
376 | else | |
377 | { | |
378 | name = dollar_label_name ((long) number, 1); | |
379 | symbolP = symbol_find_or_make (name); | |
380 | } | |
2ed83a59 | 381 | |
219deb70 ILT |
382 | expressionP->X_op = O_symbol; |
383 | expressionP->X_add_symbol = symbolP; | |
384 | expressionP->X_add_number = 0; | |
385 | } /* case '$' */ | |
386 | else | |
387 | { | |
388 | expressionP->X_op = O_constant; | |
389 | #ifdef TARGET_WORD_SIZE | |
390 | /* Sign extend NUMBER. */ | |
391 | number |= (-(number >> (TARGET_WORD_SIZE - 1))) << (TARGET_WORD_SIZE - 1); | |
392 | #endif | |
393 | expressionP->X_add_number = number; | |
394 | input_line_pointer--; /* restore following character. */ | |
395 | } /* really just a number */ | |
396 | } | |
397 | else | |
398 | { | |
399 | /* not a small number */ | |
400 | expressionP->X_op = O_big; | |
401 | expressionP->X_add_number = number; /* number of littlenums */ | |
402 | input_line_pointer--; /*->char following number. */ | |
403 | } | |
404 | } | |
2ed83a59 | 405 | |
219deb70 | 406 | /* Parse an MRI multi character constant. */ |
f8701a3f | 407 | |
219deb70 ILT |
408 | static void |
409 | mri_char_constant (expressionP) | |
410 | expressionS *expressionP; | |
411 | { | |
412 | int i; | |
2ed83a59 | 413 | |
219deb70 ILT |
414 | if (*input_line_pointer == '\'' |
415 | && input_line_pointer[1] != '\'') | |
416 | { | |
417 | expressionP->X_op = O_constant; | |
418 | expressionP->X_add_number = 0; | |
419 | return; | |
420 | } | |
2ed83a59 | 421 | |
219deb70 ILT |
422 | /* In order to get the correct byte ordering, we must build the |
423 | number in reverse. */ | |
424 | for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--) | |
425 | { | |
426 | int j; | |
2ed83a59 | 427 | |
219deb70 ILT |
428 | generic_bignum[i] = 0; |
429 | for (j = 0; j < CHARS_PER_LITTLENUM; j++) | |
430 | { | |
431 | if (*input_line_pointer == '\'') | |
432 | { | |
433 | if (input_line_pointer[1] != '\'') | |
434 | break; | |
435 | ++input_line_pointer; | |
436 | } | |
437 | generic_bignum[i] <<= 8; | |
438 | generic_bignum[i] += *input_line_pointer; | |
439 | ++input_line_pointer; | |
440 | } | |
2ed83a59 | 441 | |
219deb70 ILT |
442 | if (i < SIZE_OF_LARGE_NUMBER - 1) |
443 | { | |
444 | /* If there is more than one littlenum, left justify the | |
445 | last one to make it match the earlier ones. If there is | |
446 | only one, we can just use the value directly. */ | |
447 | for (; j < CHARS_PER_LITTLENUM; j++) | |
448 | generic_bignum[i] <<= 8; | |
449 | } | |
2ed83a59 | 450 | |
219deb70 ILT |
451 | if (*input_line_pointer == '\'' |
452 | && input_line_pointer[1] != '\'') | |
453 | break; | |
454 | } | |
2ed83a59 | 455 | |
219deb70 ILT |
456 | if (i < 0) |
457 | { | |
458 | as_bad ("Character constant too large"); | |
459 | i = 0; | |
460 | } | |
2ed83a59 | 461 | |
219deb70 ILT |
462 | if (i > 0) |
463 | { | |
464 | int c; | |
465 | int j; | |
2ed83a59 | 466 | |
219deb70 ILT |
467 | c = SIZE_OF_LARGE_NUMBER - i; |
468 | for (j = 0; j < c; j++) | |
469 | generic_bignum[j] = generic_bignum[i + j]; | |
470 | i = c; | |
2ed83a59 | 471 | } |
219deb70 ILT |
472 | |
473 | know (LITTLENUM_NUMBER_OF_BITS == 16); | |
474 | if (i > 2) | |
dae92eab | 475 | { |
5ac34ac3 | 476 | expressionP->X_op = O_big; |
219deb70 ILT |
477 | expressionP->X_add_number = i; |
478 | } | |
479 | else | |
480 | { | |
481 | expressionP->X_op = O_constant; | |
482 | if (i < 2) | |
483 | expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK; | |
484 | else | |
485 | expressionP->X_add_number = | |
486 | (((generic_bignum[1] & LITTLENUM_MASK) | |
487 | << LITTLENUM_NUMBER_OF_BITS) | |
488 | | (generic_bignum[0] & LITTLENUM_MASK)); | |
dae92eab | 489 | } |
c593cf41 | 490 | |
219deb70 ILT |
491 | /* Skip the final closing quote. */ |
492 | ++input_line_pointer; | |
493 | } | |
c593cf41 | 494 | |
52e1cf9d ILT |
495 | /* Return an expression representing the current location. This |
496 | handles the magic symbol `.'. */ | |
497 | ||
498 | static void | |
499 | current_location (expressionp) | |
500 | expressionS *expressionp; | |
501 | { | |
502 | if (now_seg == absolute_section) | |
503 | { | |
504 | expressionp->X_op = O_constant; | |
505 | expressionp->X_add_number = abs_section_offset; | |
506 | } | |
507 | else | |
508 | { | |
509 | symbolS *symbolp; | |
510 | ||
511 | symbolp = symbol_new (FAKE_LABEL_NAME, now_seg, | |
512 | (valueT) frag_now_fix (), | |
513 | frag_now); | |
514 | expressionp->X_op = O_symbol; | |
515 | expressionp->X_add_symbol = symbolp; | |
516 | expressionp->X_add_number = 0; | |
517 | } | |
518 | } | |
519 | ||
fecd2382 RP |
520 | /* |
521 | * Summary of operand(). | |
522 | * | |
523 | * in: Input_line_pointer points to 1st char of operand, which may | |
524 | * be a space. | |
525 | * | |
5ac34ac3 ILT |
526 | * out: A expressionS. |
527 | * The operand may have been empty: in this case X_op == O_absent. | |
fecd2382 | 528 | * Input_line_pointer->(next non-blank) char after operand. |
fecd2382 | 529 | */ |
c593cf41 | 530 | |
fecd2382 | 531 | static segT |
c593cf41 | 532 | operand (expressionP) |
dae92eab | 533 | expressionS *expressionP; |
fecd2382 | 534 | { |
dae92eab KR |
535 | char c; |
536 | symbolS *symbolP; /* points to symbol */ | |
537 | char *name; /* points to name of symbol */ | |
58d4951d | 538 | segT segment; |
c593cf41 | 539 | |
d4c8cbd8 JL |
540 | /* All integers are regarded as unsigned unless they are negated. |
541 | This is because the only thing which cares whether a number is | |
542 | unsigned is the code in emit_expr which extends constants into | |
543 | bignums. It should only sign extend negative numbers, so that | |
544 | something like ``.quad 0x80000000'' is not sign extended even | |
545 | though it appears negative if valueT is 32 bits. */ | |
546 | expressionP->X_unsigned = 1; | |
547 | ||
c593cf41 SC |
548 | /* digits, assume it is a bignum. */ |
549 | ||
2ed83a59 KR |
550 | SKIP_WHITESPACE (); /* leading whitespace is part of operand. */ |
551 | c = *input_line_pointer++; /* input_line_pointer->past char in c. */ | |
c593cf41 SC |
552 | |
553 | switch (c) | |
fecd2382 | 554 | { |
c593cf41 SC |
555 | case '1': |
556 | case '2': | |
557 | case '3': | |
558 | case '4': | |
559 | case '5': | |
560 | case '6': | |
561 | case '7': | |
2ed83a59 KR |
562 | case '8': |
563 | case '9': | |
564 | input_line_pointer--; | |
565 | ||
219deb70 | 566 | integer_constant (flag_mri ? 0 : 10, expressionP); |
c593cf41 SC |
567 | break; |
568 | ||
2ed83a59 KR |
569 | case '0': |
570 | /* non-decimal radix */ | |
571 | ||
219deb70 ILT |
572 | if (flag_mri) |
573 | { | |
574 | char *s; | |
575 | ||
576 | /* Check for a hex constant. */ | |
577 | for (s = input_line_pointer; hex_p (*s); s++) | |
578 | ; | |
579 | if (*s == 'h' || *s == 'H') | |
580 | { | |
581 | --input_line_pointer; | |
582 | integer_constant (0, expressionP); | |
583 | break; | |
584 | } | |
585 | } | |
586 | ||
2ed83a59 KR |
587 | c = *input_line_pointer; |
588 | switch (c) | |
589 | { | |
3dce804d ILT |
590 | case '8': |
591 | case '9': | |
592 | if (flag_mri) | |
593 | { | |
594 | integer_constant (0, expressionP); | |
595 | break; | |
596 | } | |
597 | /* Fall through. */ | |
2ed83a59 | 598 | default: |
219deb70 | 599 | default_case: |
2ed83a59 KR |
600 | if (c && strchr (FLT_CHARS, c)) |
601 | { | |
602 | input_line_pointer++; | |
603 | floating_constant (expressionP); | |
7691379e | 604 | expressionP->X_add_number = -(isupper (c) ? tolower (c) : c); |
2ed83a59 KR |
605 | } |
606 | else | |
607 | { | |
608 | /* The string was only zero */ | |
5ac34ac3 | 609 | expressionP->X_op = O_constant; |
2ed83a59 | 610 | expressionP->X_add_number = 0; |
2ed83a59 KR |
611 | } |
612 | ||
613 | break; | |
614 | ||
615 | case 'x': | |
616 | case 'X': | |
219deb70 ILT |
617 | if (flag_mri) |
618 | goto default_case; | |
2ed83a59 KR |
619 | input_line_pointer++; |
620 | integer_constant (16, expressionP); | |
621 | break; | |
622 | ||
623 | case 'b': | |
219deb70 | 624 | if (LOCAL_LABELS_FB) |
2ed83a59 | 625 | { |
219deb70 ILT |
626 | switch (input_line_pointer[1]) |
627 | { | |
628 | case '+': | |
629 | case '-': | |
630 | /* If unambiguously a difference expression, treat | |
631 | it as one by indicating a label; otherwise, it's | |
632 | always a binary number. */ | |
633 | { | |
634 | char *cp = input_line_pointer + 1; | |
635 | while (strchr ("0123456789", *++cp)) | |
636 | ; | |
637 | if (*cp == 'b' || *cp == 'f') | |
638 | goto is_0b_label; | |
639 | } | |
640 | goto is_0b_binary; | |
641 | case '0': case '1': | |
642 | /* Some of our code elsewhere does permit digits | |
643 | greater than the expected base; for consistency, | |
644 | do the same here. */ | |
645 | case '2': case '3': case '4': case '5': | |
646 | case '6': case '7': case '8': case '9': | |
647 | goto is_0b_binary; | |
648 | case 0: | |
d90f530b | 649 | goto is_0b_label; |
219deb70 ILT |
650 | default: |
651 | goto is_0b_label; | |
652 | } | |
653 | is_0b_label: | |
654 | input_line_pointer--; | |
655 | integer_constant (10, expressionP); | |
656 | break; | |
657 | is_0b_binary: | |
658 | ; | |
2ed83a59 | 659 | } |
2ed83a59 KR |
660 | case 'B': |
661 | input_line_pointer++; | |
219deb70 ILT |
662 | if (flag_mri) |
663 | goto default_case; | |
2ed83a59 KR |
664 | integer_constant (2, expressionP); |
665 | break; | |
666 | ||
667 | case '0': | |
668 | case '1': | |
669 | case '2': | |
670 | case '3': | |
671 | case '4': | |
672 | case '5': | |
673 | case '6': | |
674 | case '7': | |
219deb70 | 675 | integer_constant (flag_mri ? 0 : 8, expressionP); |
2ed83a59 KR |
676 | break; |
677 | ||
678 | case 'f': | |
219deb70 ILT |
679 | if (LOCAL_LABELS_FB) |
680 | { | |
681 | /* If it says "0f" and it could possibly be a floating point | |
682 | number, make it one. Otherwise, make it a local label, | |
683 | and try to deal with parsing the rest later. */ | |
684 | if (!input_line_pointer[1] | |
685 | || (is_end_of_line[0xff & input_line_pointer[1]])) | |
686 | goto is_0f_label; | |
d90f530b | 687 | { |
219deb70 ILT |
688 | char *cp = input_line_pointer + 1; |
689 | int r = atof_generic (&cp, ".", EXP_CHARS, | |
690 | &generic_floating_point_number); | |
691 | switch (r) | |
692 | { | |
693 | case 0: | |
694 | case ERROR_EXPONENT_OVERFLOW: | |
695 | if (*cp == 'f' || *cp == 'b') | |
696 | /* looks like a difference expression */ | |
697 | goto is_0f_label; | |
698 | else | |
699 | goto is_0f_float; | |
700 | default: | |
701 | as_fatal ("expr.c(operand): bad atof_generic return val %d", | |
702 | r); | |
703 | } | |
d90f530b | 704 | } |
d90f530b | 705 | |
219deb70 ILT |
706 | /* Okay, now we've sorted it out. We resume at one of these |
707 | two labels, depending on what we've decided we're probably | |
708 | looking at. */ | |
709 | is_0f_label: | |
710 | input_line_pointer--; | |
711 | integer_constant (10, expressionP); | |
712 | break; | |
713 | ||
714 | is_0f_float: | |
715 | /* fall through */ | |
716 | ; | |
717 | } | |
2ed83a59 KR |
718 | |
719 | case 'd': | |
720 | case 'D': | |
721 | case 'F': | |
722 | case 'r': | |
723 | case 'e': | |
724 | case 'E': | |
725 | case 'g': | |
726 | case 'G': | |
2ed83a59 KR |
727 | input_line_pointer++; |
728 | floating_constant (expressionP); | |
f2f7d044 | 729 | expressionP->X_add_number = -(isupper (c) ? tolower (c) : c); |
2ed83a59 KR |
730 | break; |
731 | ||
2ed83a59 | 732 | case '$': |
219deb70 ILT |
733 | if (LOCAL_LABELS_DOLLAR) |
734 | { | |
735 | integer_constant (10, expressionP); | |
736 | break; | |
737 | } | |
738 | else | |
739 | goto default_case; | |
2ed83a59 KR |
740 | } |
741 | ||
c593cf41 | 742 | break; |
5ac34ac3 | 743 | |
2ed83a59 | 744 | case '(': |
d90f530b | 745 | case '[': |
2ed83a59 | 746 | /* didn't begin with digit & not a name */ |
58d4951d | 747 | segment = expression (expressionP); |
5ac34ac3 | 748 | /* Expression() will pass trailing whitespace */ |
219deb70 ILT |
749 | if ((c == '(' && *input_line_pointer++ != ')') |
750 | || (c == '[' && *input_line_pointer++ != ']')) | |
5ac34ac3 ILT |
751 | { |
752 | as_bad ("Missing ')' assumed"); | |
753 | input_line_pointer--; | |
754 | } | |
755 | /* here with input_line_pointer->char after "(...)" */ | |
58d4951d | 756 | return segment; |
c593cf41 | 757 | |
219deb70 ILT |
758 | case 'E': |
759 | if (! flag_mri || *input_line_pointer != '\'') | |
760 | goto de_fault; | |
761 | as_bad ("EBCDIC constants are not supported"); | |
762 | /* Fall through. */ | |
763 | case 'A': | |
764 | if (! flag_mri || *input_line_pointer != '\'') | |
765 | goto de_fault; | |
766 | ++input_line_pointer; | |
767 | /* Fall through. */ | |
2ed83a59 | 768 | case '\'': |
219deb70 ILT |
769 | if (! flag_mri) |
770 | { | |
771 | /* Warning: to conform to other people's assemblers NO | |
772 | ESCAPEMENT is permitted for a single quote. The next | |
773 | character, parity errors and all, is taken as the value | |
774 | of the operand. VERY KINKY. */ | |
775 | expressionP->X_op = O_constant; | |
776 | expressionP->X_add_number = *input_line_pointer++; | |
777 | break; | |
778 | } | |
779 | ||
780 | mri_char_constant (expressionP); | |
2ed83a59 KR |
781 | break; |
782 | ||
49864cfa | 783 | case '+': |
58d4951d | 784 | (void) operand (expressionP); |
49864cfa KR |
785 | break; |
786 | ||
219deb70 ILT |
787 | case '"': |
788 | /* Double quote is the logical not operator in MRI mode. */ | |
789 | if (! flag_mri) | |
790 | goto de_fault; | |
791 | /* Fall through. */ | |
2ed83a59 KR |
792 | case '~': |
793 | case '-': | |
2ed83a59 | 794 | { |
5ac34ac3 ILT |
795 | operand (expressionP); |
796 | if (expressionP->X_op == O_constant) | |
2ed83a59 | 797 | { |
2ed83a59 KR |
798 | /* input_line_pointer -> char after operand */ |
799 | if (c == '-') | |
800 | { | |
5ac34ac3 | 801 | expressionP->X_add_number = - expressionP->X_add_number; |
d841bc49 KR |
802 | /* Notice: '-' may overflow: no warning is given. This is |
803 | compatible with other people's assemblers. Sigh. */ | |
d4c8cbd8 | 804 | expressionP->X_unsigned = 0; |
2ed83a59 KR |
805 | } |
806 | else | |
5ac34ac3 | 807 | expressionP->X_add_number = ~ expressionP->X_add_number; |
f2f7d044 | 808 | } |
5ac34ac3 ILT |
809 | else if (expressionP->X_op != O_illegal |
810 | && expressionP->X_op != O_absent) | |
f2f7d044 | 811 | { |
5ac34ac3 | 812 | expressionP->X_add_symbol = make_expr_symbol (expressionP); |
2ed83a59 | 813 | if (c == '-') |
5ac34ac3 | 814 | expressionP->X_op = O_uminus; |
f2f7d044 | 815 | else |
5ac34ac3 ILT |
816 | expressionP->X_op = O_bit_not; |
817 | expressionP->X_add_number = 0; | |
c593cf41 | 818 | } |
f2f7d044 | 819 | else |
5ac34ac3 ILT |
820 | as_warn ("Unary operator %c ignored because bad operand follows", |
821 | c); | |
c593cf41 | 822 | } |
2ed83a59 KR |
823 | break; |
824 | ||
d90f530b | 825 | case '$': |
219deb70 ILT |
826 | /* $ is the program counter when in MRI mode, or when DOLLAR_DOT |
827 | is defined. */ | |
828 | #ifndef DOLLAR_DOT | |
829 | if (! flag_mri) | |
830 | goto de_fault; | |
d90f530b | 831 | #endif |
219deb70 ILT |
832 | if (flag_mri && hex_p (*input_line_pointer)) |
833 | { | |
834 | /* In MRI mode, $ is also used as the prefix for a | |
835 | hexadecimal constant. */ | |
836 | integer_constant (16, expressionP); | |
837 | break; | |
838 | } | |
52e1cf9d ILT |
839 | |
840 | if (is_part_of_name (*input_line_pointer)) | |
841 | goto isname; | |
842 | ||
843 | current_location (expressionP); | |
844 | break; | |
845 | ||
219deb70 | 846 | case '.': |
2ed83a59 KR |
847 | if (!is_part_of_name (*input_line_pointer)) |
848 | { | |
52e1cf9d | 849 | current_location (expressionP); |
2ed83a59 | 850 | break; |
2ed83a59 | 851 | } |
3dce804d ILT |
852 | else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0 |
853 | && ! is_part_of_name (input_line_pointer[8])) | |
854 | || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0 | |
855 | && ! is_part_of_name (input_line_pointer[7]))) | |
856 | { | |
857 | int start; | |
858 | ||
859 | start = (input_line_pointer[1] == 't' | |
860 | || input_line_pointer[1] == 'T'); | |
861 | input_line_pointer += start ? 8 : 7; | |
862 | SKIP_WHITESPACE (); | |
863 | if (*input_line_pointer != '(') | |
864 | as_bad ("syntax error in .startof. or .sizeof."); | |
865 | else | |
866 | { | |
867 | char *buf; | |
868 | ||
869 | ++input_line_pointer; | |
870 | SKIP_WHITESPACE (); | |
871 | name = input_line_pointer; | |
872 | c = get_symbol_end (); | |
873 | ||
874 | buf = (char *) xmalloc (strlen (name) + 10); | |
875 | if (start) | |
876 | sprintf (buf, ".startof.%s", name); | |
877 | else | |
878 | sprintf (buf, ".sizeof.%s", name); | |
879 | symbolP = symbol_make (buf); | |
880 | free (buf); | |
881 | ||
882 | expressionP->X_op = O_symbol; | |
883 | expressionP->X_add_symbol = symbolP; | |
884 | expressionP->X_add_number = 0; | |
885 | ||
886 | *input_line_pointer = c; | |
887 | SKIP_WHITESPACE (); | |
888 | if (*input_line_pointer != ')') | |
889 | as_bad ("syntax error in .startof. or .sizeof."); | |
890 | else | |
891 | ++input_line_pointer; | |
892 | } | |
893 | break; | |
894 | } | |
2ed83a59 KR |
895 | else |
896 | { | |
897 | goto isname; | |
2ed83a59 KR |
898 | } |
899 | case ',': | |
900 | case '\n': | |
f2f7d044 | 901 | case '\0': |
0bd77bc4 | 902 | eol: |
2ed83a59 | 903 | /* can't imagine any other kind of operand */ |
5ac34ac3 | 904 | expressionP->X_op = O_absent; |
2ed83a59 | 905 | input_line_pointer--; |
219deb70 ILT |
906 | break; |
907 | ||
908 | case '%': | |
909 | if (! flag_mri) | |
910 | goto de_fault; | |
911 | integer_constant (2, expressionP); | |
912 | break; | |
913 | ||
914 | case '@': | |
915 | if (! flag_mri) | |
916 | goto de_fault; | |
917 | integer_constant (8, expressionP); | |
918 | break; | |
919 | ||
920 | case ':': | |
921 | if (! flag_mri) | |
922 | goto de_fault; | |
923 | ||
924 | /* In MRI mode, this is a floating point constant represented | |
925 | using hexadecimal digits. */ | |
926 | ||
927 | ++input_line_pointer; | |
928 | integer_constant (16, expressionP); | |
2ed83a59 | 929 | break; |
0bd77bc4 | 930 | |
52e1cf9d ILT |
931 | case '*': |
932 | if (! flag_mri || is_part_of_name (*input_line_pointer)) | |
933 | goto de_fault; | |
934 | ||
935 | current_location (expressionP); | |
936 | break; | |
937 | ||
2ed83a59 | 938 | default: |
219deb70 | 939 | de_fault: |
58d4951d | 940 | if (is_end_of_line[(unsigned char) c]) |
0bd77bc4 | 941 | goto eol; |
2ed83a59 KR |
942 | if (is_name_beginner (c)) /* here if did not begin with a digit */ |
943 | { | |
944 | /* | |
d841bc49 KR |
945 | * Identifier begins here. |
946 | * This is kludged for speed, so code is repeated. | |
947 | */ | |
2ed83a59 KR |
948 | isname: |
949 | name = --input_line_pointer; | |
950 | c = get_symbol_end (); | |
951 | symbolP = symbol_find_or_make (name); | |
5ac34ac3 ILT |
952 | |
953 | /* If we have an absolute symbol or a reg, then we know its | |
954 | value now. */ | |
58d4951d ILT |
955 | segment = S_GET_SEGMENT (symbolP); |
956 | if (segment == absolute_section) | |
5ac34ac3 ILT |
957 | { |
958 | expressionP->X_op = O_constant; | |
959 | expressionP->X_add_number = S_GET_VALUE (symbolP); | |
960 | } | |
58d4951d | 961 | else if (segment == reg_section) |
5ac34ac3 ILT |
962 | { |
963 | expressionP->X_op = O_register; | |
964 | expressionP->X_add_number = S_GET_VALUE (symbolP); | |
965 | } | |
f2f7d044 | 966 | else |
2ed83a59 | 967 | { |
5ac34ac3 | 968 | expressionP->X_op = O_symbol; |
2ed83a59 | 969 | expressionP->X_add_symbol = symbolP; |
5ac34ac3 | 970 | expressionP->X_add_number = 0; |
2ed83a59 KR |
971 | } |
972 | *input_line_pointer = c; | |
2ed83a59 KR |
973 | } |
974 | else | |
975 | { | |
219deb70 ILT |
976 | /* Let the target try to parse it. Success is indicated by changing |
977 | the X_op field to something other than O_absent and pointing | |
978 | input_line_pointer passed the expression. If it can't parse the | |
979 | expression, X_op and input_line_pointer should be unchanged. */ | |
980 | expressionP->X_op = O_absent; | |
981 | --input_line_pointer; | |
982 | md_operand (expressionP); | |
983 | if (expressionP->X_op == O_absent) | |
984 | { | |
985 | ++input_line_pointer; | |
986 | as_bad ("Bad expression"); | |
987 | expressionP->X_op = O_constant; | |
988 | expressionP->X_add_number = 0; | |
989 | } | |
2ed83a59 | 990 | } |
219deb70 | 991 | break; |
c593cf41 | 992 | } |
c593cf41 | 993 | |
c593cf41 SC |
994 | /* |
995 | * It is more 'efficient' to clean up the expressionS when they are created. | |
996 | * Doing it here saves lines of code. | |
997 | */ | |
998 | clean_up_expression (expressionP); | |
2ed83a59 KR |
999 | SKIP_WHITESPACE (); /*->1st char after operand. */ |
1000 | know (*input_line_pointer != ' '); | |
58d4951d | 1001 | |
009dc5e1 JL |
1002 | /* The PA port needs this information. */ |
1003 | if (expressionP->X_add_symbol) | |
1004 | expressionP->X_add_symbol->sy_used = 1; | |
1005 | ||
58d4951d ILT |
1006 | switch (expressionP->X_op) |
1007 | { | |
1008 | default: | |
1009 | return absolute_section; | |
1010 | case O_symbol: | |
1011 | return S_GET_SEGMENT (expressionP->X_add_symbol); | |
1012 | case O_register: | |
1013 | return reg_section; | |
1014 | } | |
2ed83a59 | 1015 | } /* operand() */ |
fecd2382 RP |
1016 | \f |
1017 | /* Internal. Simplify a struct expression for use by expr() */ | |
1018 | ||
1019 | /* | |
1020 | * In: address of a expressionS. | |
5ac34ac3 | 1021 | * The X_op field of the expressionS may only take certain values. |
fecd2382 RP |
1022 | * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT. |
1023 | * Out: expressionS may have been modified: | |
1024 | * 'foo-foo' symbol references cancelled to 0, | |
5ac34ac3 | 1025 | * which changes X_op from O_subtract to O_constant. |
fecd2382 RP |
1026 | * Unused fields zeroed to help expr(). |
1027 | */ | |
1028 | ||
1029 | static void | |
c593cf41 | 1030 | clean_up_expression (expressionP) |
dae92eab | 1031 | expressionS *expressionP; |
fecd2382 | 1032 | { |
5ac34ac3 | 1033 | switch (expressionP->X_op) |
2ed83a59 | 1034 | { |
5ac34ac3 ILT |
1035 | case O_illegal: |
1036 | case O_absent: | |
2ed83a59 | 1037 | expressionP->X_add_number = 0; |
5ac34ac3 ILT |
1038 | /* Fall through. */ |
1039 | case O_big: | |
1040 | case O_constant: | |
1041 | case O_register: | |
2ed83a59 | 1042 | expressionP->X_add_symbol = NULL; |
5ac34ac3 ILT |
1043 | /* Fall through. */ |
1044 | case O_symbol: | |
1045 | case O_uminus: | |
1046 | case O_bit_not: | |
1047 | expressionP->X_op_symbol = NULL; | |
1048 | break; | |
1049 | case O_subtract: | |
1050 | if (expressionP->X_op_symbol == expressionP->X_add_symbol | |
1051 | || ((expressionP->X_op_symbol->sy_frag | |
1052 | == expressionP->X_add_symbol->sy_frag) | |
ffffc8fb | 1053 | && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol)) |
5ac34ac3 | 1054 | && (S_GET_VALUE (expressionP->X_op_symbol) |
49864cfa | 1055 | == S_GET_VALUE (expressionP->X_add_symbol)))) |
2ed83a59 | 1056 | { |
3a762a0b KR |
1057 | addressT diff = (S_GET_VALUE (expressionP->X_add_symbol) |
1058 | - S_GET_VALUE (expressionP->X_op_symbol)); | |
d90f530b | 1059 | |
5ac34ac3 | 1060 | expressionP->X_op = O_constant; |
2ed83a59 | 1061 | expressionP->X_add_symbol = NULL; |
5ac34ac3 | 1062 | expressionP->X_op_symbol = NULL; |
d90f530b | 1063 | expressionP->X_add_number += diff; |
fecd2382 | 1064 | } |
5ac34ac3 ILT |
1065 | break; |
1066 | default: | |
1067 | break; | |
fecd2382 | 1068 | } |
f2f7d044 | 1069 | } |
fecd2382 RP |
1070 | \f |
1071 | /* Expression parser. */ | |
1072 | ||
1073 | /* | |
1074 | * We allow an empty expression, and just assume (absolute,0) silently. | |
1075 | * Unary operators and parenthetical expressions are treated as operands. | |
1076 | * As usual, Q==quantity==operand, O==operator, X==expression mnemonics. | |
1077 | * | |
1078 | * We used to do a aho/ullman shift-reduce parser, but the logic got so | |
1079 | * warped that I flushed it and wrote a recursive-descent parser instead. | |
1080 | * Now things are stable, would anybody like to write a fast parser? | |
1081 | * Most expressions are either register (which does not even reach here) | |
1082 | * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common. | |
1083 | * So I guess it doesn't really matter how inefficient more complex expressions | |
1084 | * are parsed. | |
1085 | * | |
1086 | * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK. | |
1087 | * Also, we have consumed any leading or trailing spaces (operand does that) | |
1088 | * and done all intervening operators. | |
5ac34ac3 ILT |
1089 | * |
1090 | * This returns the segment of the result, which will be | |
1091 | * absolute_section or the segment of a symbol. | |
fecd2382 RP |
1092 | */ |
1093 | ||
49864cfa | 1094 | #undef __ |
fecd2382 RP |
1095 | #define __ O_illegal |
1096 | ||
3dce804d | 1097 | static operatorT op_encoding[256] = |
2ed83a59 KR |
1098 | { /* maps ASCII->operators */ |
1099 | ||
1100 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1101 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1102 | ||
3dce804d | 1103 | __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __, |
2ed83a59 KR |
1104 | __, __, O_multiply, O_add, __, O_subtract, __, O_divide, |
1105 | __, __, __, __, __, __, __, __, | |
219deb70 | 1106 | __, __, __, __, O_lt, __, O_gt, __, |
2ed83a59 KR |
1107 | __, __, __, __, __, __, __, __, |
1108 | __, __, __, __, __, __, __, __, | |
1109 | __, __, __, __, __, __, __, __, | |
1110 | __, __, __, __, __, __, O_bit_exclusive_or, __, | |
1111 | __, __, __, __, __, __, __, __, | |
1112 | __, __, __, __, __, __, __, __, | |
1113 | __, __, __, __, __, __, __, __, | |
1114 | __, __, __, __, O_bit_inclusive_or, __, __, __, | |
1115 | ||
1116 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1117 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1118 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1119 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1120 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1121 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1122 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
1123 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ | |
1124 | }; | |
fecd2382 RP |
1125 | |
1126 | ||
1127 | /* | |
1128 | * Rank Examples | |
1129 | * 0 operand, (expression) | |
219deb70 ILT |
1130 | * 1 = <> < <= >= > |
1131 | * 2 + - | |
1132 | * 3 used for * / % in MRI mode | |
1133 | * 4 & ^ ! | | |
1134 | * 5 * / % << >> | |
1135 | * 6 unary - unary ~ | |
fecd2382 | 1136 | */ |
219deb70 | 1137 | static operator_rankT op_rank[] = |
5ac34ac3 ILT |
1138 | { |
1139 | 0, /* O_illegal */ | |
1140 | 0, /* O_absent */ | |
1141 | 0, /* O_constant */ | |
1142 | 0, /* O_symbol */ | |
1143 | 0, /* O_register */ | |
1144 | 0, /* O_bit */ | |
219deb70 ILT |
1145 | 6, /* O_uminus */ |
1146 | 6, /* O_bit_not */ | |
1147 | 5, /* O_multiply */ | |
1148 | 5, /* O_divide */ | |
1149 | 5, /* O_modulus */ | |
1150 | 5, /* O_left_shift */ | |
1151 | 5, /* O_right_shift */ | |
1152 | 4, /* O_bit_inclusive_or */ | |
1153 | 4, /* O_bit_or_not */ | |
1154 | 4, /* O_bit_exclusive_or */ | |
1155 | 4, /* O_bit_and */ | |
1156 | 2, /* O_add */ | |
1157 | 2, /* O_subtract */ | |
1158 | 1, /* O_eq */ | |
1159 | 1, /* O_ne */ | |
1160 | 1, /* O_lt */ | |
1161 | 1, /* O_le */ | |
1162 | 1, /* O_ge */ | |
1163 | 1 /* O_gt */ | |
5ac34ac3 | 1164 | }; |
219deb70 ILT |
1165 | |
1166 | /* Initialize the expression parser. */ | |
1167 | ||
1168 | void | |
1169 | expr_begin () | |
1170 | { | |
1171 | /* In MRI mode, multiplication and division have lower precedence | |
1172 | than the bit wise operators. */ | |
1173 | if (flag_mri) | |
1174 | { | |
1175 | op_rank[O_multiply] = 3; | |
1176 | op_rank[O_divide] = 3; | |
1177 | op_rank[O_modulus] = 3; | |
3dce804d | 1178 | op_encoding['"'] = O_bit_not; |
219deb70 ILT |
1179 | } |
1180 | } | |
fecd2382 | 1181 | \f |
219deb70 ILT |
1182 | /* Return the encoding for the operator at INPUT_LINE_POINTER. |
1183 | Advance INPUT_LINE_POINTER to the last character in the operator | |
1184 | (i.e., don't change it for a single character operator). */ | |
1185 | ||
1186 | static inline operatorT | |
1187 | operator () | |
1188 | { | |
1189 | int c; | |
1190 | operatorT ret; | |
1191 | ||
1192 | c = *input_line_pointer; | |
1193 | ||
1194 | switch (c) | |
1195 | { | |
1196 | default: | |
1197 | return op_encoding[c]; | |
1198 | ||
1199 | case '<': | |
1200 | switch (input_line_pointer[1]) | |
1201 | { | |
1202 | default: | |
1203 | return op_encoding[c]; | |
1204 | case '<': | |
1205 | ret = O_left_shift; | |
1206 | break; | |
1207 | case '>': | |
1208 | ret = O_ne; | |
1209 | break; | |
1210 | case '=': | |
1211 | ret = O_le; | |
1212 | break; | |
1213 | } | |
1214 | ++input_line_pointer; | |
1215 | return ret; | |
1216 | ||
1217 | case '>': | |
1218 | switch (input_line_pointer[1]) | |
1219 | { | |
1220 | default: | |
1221 | return op_encoding[c]; | |
1222 | case '>': | |
1223 | ret = O_right_shift; | |
1224 | break; | |
1225 | case '=': | |
1226 | ret = O_ge; | |
1227 | break; | |
1228 | } | |
1229 | ++input_line_pointer; | |
1230 | return ret; | |
1231 | ||
1232 | case '!': | |
1233 | /* We accept !! as equivalent to ^ for MRI compatibility. */ | |
1234 | if (input_line_pointer[1] != '!') | |
1235 | { | |
1236 | if (flag_mri) | |
1237 | return O_bit_inclusive_or; | |
1238 | return op_encoding[c]; | |
1239 | } | |
1240 | ++input_line_pointer; | |
1241 | return O_bit_exclusive_or; | |
1242 | } | |
1243 | ||
1244 | /*NOTREACHED*/ | |
1245 | } | |
1246 | ||
1247 | /* Parse an expression. */ | |
1248 | ||
5ac34ac3 | 1249 | segT |
2ed83a59 | 1250 | expr (rank, resultP) |
dae92eab KR |
1251 | operator_rankT rank; /* Larger # is higher rank. */ |
1252 | expressionS *resultP; /* Deliver result here. */ | |
fecd2382 | 1253 | { |
5ac34ac3 | 1254 | segT retval; |
2ed83a59 | 1255 | expressionS right; |
dae92eab | 1256 | operatorT op_left; |
dae92eab | 1257 | operatorT op_right; |
c593cf41 | 1258 | |
2ed83a59 | 1259 | know (rank >= 0); |
5ac34ac3 ILT |
1260 | |
1261 | retval = operand (resultP); | |
1262 | ||
2ed83a59 | 1263 | know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */ |
5ac34ac3 | 1264 | |
219deb70 | 1265 | op_left = operator (); |
2ed83a59 | 1266 | while (op_left != O_illegal && op_rank[(int) op_left] > rank) |
fecd2382 | 1267 | { |
5ac34ac3 ILT |
1268 | segT rightseg; |
1269 | ||
2ed83a59 | 1270 | input_line_pointer++; /*->after 1st character of operator. */ |
5ac34ac3 ILT |
1271 | |
1272 | rightseg = expr (op_rank[(int) op_left], &right); | |
1273 | if (right.X_op == O_absent) | |
fecd2382 | 1274 | { |
5ac34ac3 ILT |
1275 | as_warn ("missing operand; zero assumed"); |
1276 | right.X_op = O_constant; | |
1277 | right.X_add_number = 0; | |
d4c8cbd8 JL |
1278 | right.X_add_symbol = NULL; |
1279 | right.X_op_symbol = NULL; | |
fecd2382 | 1280 | } |
5ac34ac3 | 1281 | |
2ed83a59 | 1282 | know (*input_line_pointer != ' '); |
5ac34ac3 | 1283 | |
58d4951d ILT |
1284 | if (retval == undefined_section) |
1285 | { | |
1286 | if (SEG_NORMAL (rightseg)) | |
1287 | retval = rightseg; | |
1288 | } | |
1289 | else if (! SEG_NORMAL (retval)) | |
5ac34ac3 ILT |
1290 | retval = rightseg; |
1291 | else if (SEG_NORMAL (rightseg) | |
d4c8cbd8 JL |
1292 | && retval != rightseg |
1293 | #ifdef DIFF_EXPR_OK | |
1294 | && op_left != O_subtract | |
1295 | #endif | |
1296 | ) | |
5ac34ac3 ILT |
1297 | as_bad ("operation combines symbols in different segments"); |
1298 | ||
219deb70 | 1299 | op_right = operator (); |
5ac34ac3 ILT |
1300 | |
1301 | know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]); | |
52e1cf9d | 1302 | know ((int) op_left >= (int) O_multiply && (int) op_left <= (int) O_gt); |
5ac34ac3 | 1303 | |
c593cf41 SC |
1304 | /* input_line_pointer->after right-hand quantity. */ |
1305 | /* left-hand quantity in resultP */ | |
1306 | /* right-hand quantity in right. */ | |
1307 | /* operator in op_left. */ | |
5ac34ac3 ILT |
1308 | |
1309 | if (resultP->X_op == O_big) | |
fecd2382 | 1310 | { |
219deb70 ILT |
1311 | as_warn ("left operand is a %s; integer 0 assumed", |
1312 | resultP->X_add_number > 0 ? "bignum" : "float"); | |
5ac34ac3 ILT |
1313 | resultP->X_op = O_constant; |
1314 | resultP->X_add_number = 0; | |
1315 | resultP->X_add_symbol = NULL; | |
1316 | resultP->X_op_symbol = NULL; | |
fecd2382 | 1317 | } |
5ac34ac3 | 1318 | if (right.X_op == O_big) |
fecd2382 | 1319 | { |
219deb70 ILT |
1320 | as_warn ("right operand is a %s; integer 0 assumed", |
1321 | right.X_add_number > 0 ? "bignum" : "float"); | |
5ac34ac3 ILT |
1322 | right.X_op = O_constant; |
1323 | right.X_add_number = 0; | |
1324 | right.X_add_symbol = NULL; | |
1325 | right.X_op_symbol = NULL; | |
1326 | } | |
1327 | ||
1328 | /* Optimize common cases. */ | |
219deb70 | 1329 | if (op_left == O_add && right.X_op == O_constant) |
5ac34ac3 ILT |
1330 | { |
1331 | /* X + constant. */ | |
1332 | resultP->X_add_number += right.X_add_number; | |
1333 | } | |
d90f530b KR |
1334 | /* This case comes up in PIC code. */ |
1335 | else if (op_left == O_subtract | |
1336 | && right.X_op == O_symbol | |
1337 | && resultP->X_op == O_symbol | |
1338 | && (right.X_add_symbol->sy_frag | |
fcacfef6 JL |
1339 | == resultP->X_add_symbol->sy_frag) |
1340 | && SEG_NORMAL (S_GET_SEGMENT (right.X_add_symbol))) | |
1341 | ||
d90f530b KR |
1342 | { |
1343 | resultP->X_add_number += right.X_add_number; | |
1344 | resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol) | |
1345 | - S_GET_VALUE (right.X_add_symbol)); | |
1346 | resultP->X_op = O_constant; | |
1347 | resultP->X_add_symbol = 0; | |
1348 | } | |
5ac34ac3 ILT |
1349 | else if (op_left == O_subtract && right.X_op == O_constant) |
1350 | { | |
1351 | /* X - constant. */ | |
1352 | resultP->X_add_number -= right.X_add_number; | |
1353 | } | |
1354 | else if (op_left == O_add && resultP->X_op == O_constant) | |
1355 | { | |
1356 | /* Constant + X. */ | |
1357 | resultP->X_op = right.X_op; | |
1358 | resultP->X_add_symbol = right.X_add_symbol; | |
1359 | resultP->X_op_symbol = right.X_op_symbol; | |
1360 | resultP->X_add_number += right.X_add_number; | |
1361 | retval = rightseg; | |
1362 | } | |
1363 | else if (resultP->X_op == O_constant && right.X_op == O_constant) | |
1364 | { | |
1365 | /* Constant OP constant. */ | |
1366 | offsetT v = right.X_add_number; | |
1367 | if (v == 0 && (op_left == O_divide || op_left == O_modulus)) | |
fecd2382 | 1368 | { |
5ac34ac3 ILT |
1369 | as_warn ("division by zero"); |
1370 | v = 1; | |
fecd2382 | 1371 | } |
5ac34ac3 | 1372 | switch (op_left) |
fecd2382 | 1373 | { |
219deb70 | 1374 | default: abort (); |
5ac34ac3 ILT |
1375 | case O_multiply: resultP->X_add_number *= v; break; |
1376 | case O_divide: resultP->X_add_number /= v; break; | |
1377 | case O_modulus: resultP->X_add_number %= v; break; | |
1378 | case O_left_shift: resultP->X_add_number <<= v; break; | |
1379 | case O_right_shift: resultP->X_add_number >>= v; break; | |
1380 | case O_bit_inclusive_or: resultP->X_add_number |= v; break; | |
1381 | case O_bit_or_not: resultP->X_add_number |= ~v; break; | |
1382 | case O_bit_exclusive_or: resultP->X_add_number ^= v; break; | |
1383 | case O_bit_and: resultP->X_add_number &= v; break; | |
1384 | case O_add: resultP->X_add_number += v; break; | |
1385 | case O_subtract: resultP->X_add_number -= v; break; | |
219deb70 ILT |
1386 | case O_eq: |
1387 | resultP->X_add_number = | |
1388 | resultP->X_add_number == v ? ~ (offsetT) 0 : 0; | |
1389 | break; | |
1390 | case O_ne: | |
1391 | resultP->X_add_number = | |
1392 | resultP->X_add_number != v ? ~ (offsetT) 0 : 0; | |
1393 | break; | |
1394 | case O_lt: | |
1395 | resultP->X_add_number = | |
1396 | resultP->X_add_number < v ? ~ (offsetT) 0 : 0; | |
1397 | break; | |
1398 | case O_le: | |
1399 | resultP->X_add_number = | |
1400 | resultP->X_add_number <= v ? ~ (offsetT) 0 : 0; | |
1401 | break; | |
1402 | case O_ge: | |
1403 | resultP->X_add_number = | |
1404 | resultP->X_add_number >= v ? ~ (offsetT) 0 : 0; | |
1405 | break; | |
1406 | case O_gt: | |
1407 | resultP->X_add_number = | |
1408 | resultP->X_add_number > v ? ~ (offsetT) 0 : 0; | |
1409 | break; | |
fecd2382 | 1410 | } |
5ac34ac3 ILT |
1411 | } |
1412 | else if (resultP->X_op == O_symbol | |
1413 | && right.X_op == O_symbol | |
1414 | && (op_left == O_add | |
1415 | || op_left == O_subtract | |
1416 | || (resultP->X_add_number == 0 | |
1417 | && right.X_add_number == 0))) | |
1418 | { | |
1419 | /* Symbol OP symbol. */ | |
1420 | resultP->X_op = op_left; | |
1421 | resultP->X_op_symbol = right.X_add_symbol; | |
c593cf41 | 1422 | if (op_left == O_add) |
5ac34ac3 ILT |
1423 | resultP->X_add_number += right.X_add_number; |
1424 | else if (op_left == O_subtract) | |
1425 | resultP->X_add_number -= right.X_add_number; | |
1426 | } | |
1427 | else | |
1428 | { | |
1429 | /* The general case. */ | |
1430 | resultP->X_add_symbol = make_expr_symbol (resultP); | |
1431 | resultP->X_op_symbol = make_expr_symbol (&right); | |
1432 | resultP->X_op = op_left; | |
1433 | resultP->X_add_number = 0; | |
d4c8cbd8 | 1434 | resultP->X_unsigned = 1; |
5ac34ac3 | 1435 | } |
351878df | 1436 | |
2ed83a59 | 1437 | op_left = op_right; |
fecd2382 | 1438 | } /* While next operator is >= this rank. */ |
5ac34ac3 | 1439 | |
009dc5e1 JL |
1440 | /* The PA port needs this information. */ |
1441 | if (resultP->X_add_symbol) | |
1442 | resultP->X_add_symbol->sy_used = 1; | |
1443 | ||
5ac34ac3 | 1444 | return resultP->X_op == O_constant ? absolute_section : retval; |
fecd2382 RP |
1445 | } |
1446 | \f | |
1447 | /* | |
1448 | * get_symbol_end() | |
1449 | * | |
1450 | * This lives here because it belongs equally in expr.c & read.c. | |
1451 | * Expr.c is just a branch office read.c anyway, and putting it | |
1452 | * here lessens the crowd at read.c. | |
1453 | * | |
1454 | * Assume input_line_pointer is at start of symbol name. | |
1455 | * Advance input_line_pointer past symbol name. | |
1456 | * Turn that character into a '\0', returning its former value. | |
1457 | * This allows a string compare (RMS wants symbol names to be strings) | |
1458 | * of the symbol name. | |
1459 | * There will always be a char following symbol name, because all good | |
1460 | * lines end in end-of-line. | |
1461 | */ | |
1462 | char | |
2ed83a59 | 1463 | get_symbol_end () |
fecd2382 | 1464 | { |
dae92eab | 1465 | char c; |
2ed83a59 | 1466 | |
3dce804d ILT |
1467 | /* We accept \001 in a name in case this is being called with a |
1468 | constructed string. */ | |
1469 | while (is_part_of_name (c = *input_line_pointer++) | |
1470 | || c == '\001') | |
2ed83a59 KR |
1471 | ; |
1472 | *--input_line_pointer = 0; | |
1473 | return (c); | |
fecd2382 RP |
1474 | } |
1475 | ||
a39116f1 | 1476 | |
351878df | 1477 | unsigned int |
2ed83a59 | 1478 | get_single_number () |
a39116f1 | 1479 | { |
2ed83a59 KR |
1480 | expressionS exp; |
1481 | operand (&exp); | |
1482 | return exp.X_add_number; | |
1483 | ||
a39116f1 | 1484 | } |
2ed83a59 | 1485 | |
8b228fe9 | 1486 | /* end of expr.c */ |