Commit | Line | Data |
---|---|---|
fecd2382 | 1 | /* expr.c -operands, expressions- |
f2f7d044 | 2 | Copyright (C) 1987, 1990, 1991, 1992, 1993 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 | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, 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" | |
31 | ||
32 | #include "obstack.h" | |
33 | ||
f2f7d044 | 34 | static void clean_up_expression PARAMS ((expressionS * expressionP)); |
5ac34ac3 ILT |
35 | static symbolS *make_expr_symbol PARAMS ((expressionS * expressionP)); |
36 | ||
f2f7d044 | 37 | extern const char EXP_CHARS[], FLT_CHARS[]; |
5ac34ac3 ILT |
38 | \f |
39 | /* Build a dummy symbol to hold a complex expression. This is how we | |
40 | build expressions up out of other expressions. The symbol is put | |
41 | into the fake section expr_section. */ | |
42 | ||
43 | static symbolS * | |
44 | make_expr_symbol (expressionP) | |
45 | expressionS *expressionP; | |
46 | { | |
47 | const char *fake; | |
48 | symbolS *symbolP; | |
fecd2382 | 49 | |
5ac34ac3 ILT |
50 | /* FIXME: This should be something which decode_local_label_name |
51 | will handle. */ | |
52 | #ifdef DOT_LABEL_PREFIX | |
53 | fake = ".L0\001"; | |
54 | #else | |
55 | fake = "L0\001"; | |
56 | #endif | |
57 | /* Putting constant symbols in absolute_section rather than | |
58 | expr_section is convenient for the old a.out code, for which | |
59 | S_GET_SEGMENT does not always retrieve the value put in by | |
60 | S_SET_SEGMENT. */ | |
61 | symbolP = symbol_new (fake, | |
62 | (expressionP->X_op == O_constant | |
63 | ? absolute_section | |
64 | : expr_section), | |
65 | 0, &zero_address_frag); | |
66 | symbolP->sy_value = *expressionP; | |
67 | return symbolP; | |
68 | } | |
69 | \f | |
fecd2382 RP |
70 | /* |
71 | * Build any floating-point literal here. | |
72 | * Also build any bignum literal here. | |
73 | */ | |
74 | ||
fecd2382 RP |
75 | /* Seems atof_machine can backscan through generic_bignum and hit whatever |
76 | happens to be loaded before it in memory. And its way too complicated | |
77 | for me to fix right. Thus a hack. JF: Just make generic_bignum bigger, | |
78 | and never write into the early words, thus they'll always be zero. | |
f2f7d044 | 79 | I hate Dean's floating-point code. Bleh. */ |
2ed83a59 KR |
80 | LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6]; |
81 | FLONUM_TYPE generic_floating_point_number = | |
fecd2382 | 82 | { |
2ed83a59 KR |
83 | &generic_bignum[6], /* low (JF: Was 0) */ |
84 | &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high JF: (added +6) */ | |
85 | 0, /* leader */ | |
86 | 0, /* exponent */ | |
87 | 0 /* sign */ | |
88 | }; | |
fecd2382 RP |
89 | /* If nonzero, we've been asked to assemble nan, +inf or -inf */ |
90 | int generic_floating_point_magic; | |
91 | \f | |
dae92eab | 92 | void |
2ed83a59 KR |
93 | floating_constant (expressionP) |
94 | expressionS *expressionP; | |
c593cf41 SC |
95 | { |
96 | /* input_line_pointer->*/ | |
97 | /* floating-point constant. */ | |
98 | int error_code; | |
99 | ||
100 | error_code = atof_generic | |
2ed83a59 KR |
101 | (&input_line_pointer, ".", EXP_CHARS, |
102 | &generic_floating_point_number); | |
c593cf41 SC |
103 | |
104 | if (error_code) | |
c593cf41 | 105 | { |
2ed83a59 KR |
106 | if (error_code == ERROR_EXPONENT_OVERFLOW) |
107 | { | |
108 | as_bad ("bad floating-point constant: exponent overflow, probably assembling junk"); | |
109 | } | |
110 | else | |
111 | { | |
112 | as_bad ("bad floating-point constant: unknown error code=%d.", error_code); | |
113 | } | |
c593cf41 | 114 | } |
5ac34ac3 | 115 | expressionP->X_op = O_big; |
c593cf41 SC |
116 | /* input_line_pointer->just after constant, */ |
117 | /* which may point to whitespace. */ | |
2ed83a59 | 118 | expressionP->X_add_number = -1; |
c593cf41 SC |
119 | } |
120 | ||
dae92eab | 121 | void |
2ed83a59 KR |
122 | integer_constant (radix, expressionP) |
123 | int radix; | |
124 | expressionS *expressionP; | |
c593cf41 | 125 | { |
dae92eab | 126 | char *digit_2; /*->2nd digit of number. */ |
c593cf41 | 127 | char c; |
2ed83a59 | 128 | |
dae92eab KR |
129 | valueT number; /* offset or (absolute) value */ |
130 | short int digit; /* value of next digit in current radix */ | |
131 | short int maxdig = 0;/* highest permitted digit value. */ | |
132 | int too_many_digits = 0; /* if we see >= this number of */ | |
133 | char *name; /* points to name of symbol */ | |
134 | symbolS *symbolP; /* points to symbol */ | |
2ed83a59 KR |
135 | |
136 | int small; /* true if fits in 32 bits. */ | |
f2f7d044 | 137 | extern const char hex_value[]; /* in hex_value.c */ |
2ed83a59 | 138 | |
dae92eab KR |
139 | /* May be bignum, or may fit in 32 bits. */ |
140 | /* Most numbers fit into 32 bits, and we want this case to be fast. | |
141 | so we pretend it will fit into 32 bits. If, after making up a 32 | |
142 | bit number, we realise that we have scanned more digits than | |
143 | comfortably fit into 32 bits, we re-scan the digits coding them | |
144 | into a bignum. For decimal and octal numbers we are | |
145 | conservative: Some numbers may be assumed bignums when in fact | |
146 | they do fit into 32 bits. Numbers of any radix can have excess | |
147 | leading zeros: We strive to recognise this and cast them back | |
148 | into 32 bits. We must check that the bignum really is more than | |
149 | 32 bits, and change it back to a 32-bit number if it fits. The | |
150 | number we are looking for is expected to be positive, but if it | |
151 | fits into 32 bits as an unsigned number, we let it be a 32-bit | |
152 | number. The cavalier approach is for speed in ordinary cases. */ | |
58d4951d ILT |
153 | /* This has been extended for 64 bits. We blindly assume that if |
154 | you're compiling in 64-bit mode, the target is a 64-bit machine. | |
155 | This should be cleaned up. */ | |
156 | ||
157 | #ifdef BFD64 | |
158 | #define valuesize 64 | |
159 | #else /* includes non-bfd case, mostly */ | |
160 | #define valuesize 32 | |
161 | #endif | |
2ed83a59 KR |
162 | |
163 | switch (radix) | |
f8701a3f | 164 | { |
f8701a3f SC |
165 | case 2: |
166 | maxdig = 2; | |
58d4951d | 167 | too_many_digits = valuesize + 1; |
f8701a3f SC |
168 | break; |
169 | case 8: | |
170 | maxdig = radix = 8; | |
58d4951d | 171 | too_many_digits = (valuesize + 2) / 3; |
f8701a3f SC |
172 | break; |
173 | case 16: | |
f8701a3f | 174 | maxdig = radix = 16; |
58d4951d | 175 | too_many_digits = (valuesize + 3) / 4; |
f8701a3f SC |
176 | break; |
177 | case 10: | |
178 | maxdig = radix = 10; | |
58d4951d | 179 | too_many_digits = (valuesize + 12) / 4; /* very rough */ |
f8701a3f | 180 | } |
58d4951d | 181 | #undef valuesize |
c593cf41 SC |
182 | c = *input_line_pointer; |
183 | input_line_pointer++; | |
184 | digit_2 = input_line_pointer; | |
58d4951d ILT |
185 | for (number = 0; |
186 | (digit = hex_value[(unsigned char) c]) < maxdig; | |
187 | c = *input_line_pointer++) | |
f8701a3f SC |
188 | { |
189 | number = number * radix + digit; | |
190 | } | |
c593cf41 SC |
191 | /* c contains character after number. */ |
192 | /* input_line_pointer->char after c. */ | |
193 | small = input_line_pointer - digit_2 < too_many_digits; | |
2ed83a59 | 194 | if (!small) |
c593cf41 | 195 | { |
f8701a3f SC |
196 | /* |
197 | * we saw a lot of digits. manufacture a bignum the hard way. | |
198 | */ | |
2ed83a59 KR |
199 | LITTLENUM_TYPE *leader; /*->high order littlenum of the bignum. */ |
200 | LITTLENUM_TYPE *pointer; /*->littlenum we are frobbing now. */ | |
f8701a3f | 201 | long carry; |
2ed83a59 | 202 | |
f8701a3f | 203 | leader = generic_bignum; |
2ed83a59 KR |
204 | generic_bignum[0] = 0; |
205 | generic_bignum[1] = 0; | |
f8701a3f | 206 | /* we could just use digit_2, but lets be mnemonic. */ |
2ed83a59 | 207 | input_line_pointer = --digit_2; /*->1st digit. */ |
f8701a3f | 208 | c = *input_line_pointer++; |
58d4951d ILT |
209 | for (; |
210 | (carry = hex_value[(unsigned char) c]) < maxdig; | |
211 | c = *input_line_pointer++) | |
f8701a3f SC |
212 | { |
213 | for (pointer = generic_bignum; | |
214 | pointer <= leader; | |
215 | pointer++) | |
216 | { | |
217 | long work; | |
2ed83a59 KR |
218 | |
219 | work = carry + radix * *pointer; | |
f8701a3f SC |
220 | *pointer = work & LITTLENUM_MASK; |
221 | carry = work >> LITTLENUM_NUMBER_OF_BITS; | |
222 | } | |
223 | if (carry) | |
224 | { | |
225 | if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1) | |
2ed83a59 | 226 | { /* room to grow a longer bignum. */ |
f8701a3f SC |
227 | *++leader = carry; |
228 | } | |
229 | } | |
230 | } | |
231 | /* again, c is char after number, */ | |
232 | /* input_line_pointer->after c. */ | |
2ed83a59 KR |
233 | know (sizeof (int) * 8 == 32); |
234 | know (LITTLENUM_NUMBER_OF_BITS == 16); | |
f8701a3f SC |
235 | /* hence the constant "2" in the next line. */ |
236 | if (leader < generic_bignum + 2) | |
2ed83a59 | 237 | { /* will fit into 32 bits. */ |
f8701a3f | 238 | number = |
2ed83a59 KR |
239 | ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS) |
240 | | (generic_bignum[0] & LITTLENUM_MASK); | |
f8701a3f SC |
241 | small = 1; |
242 | } | |
243 | else | |
244 | { | |
2ed83a59 | 245 | number = leader - generic_bignum + 1; /* number of littlenums in the bignum. */ |
c593cf41 | 246 | } |
c593cf41 | 247 | } |
2ed83a59 KR |
248 | if (small) |
249 | { | |
f8701a3f | 250 | /* |
2ed83a59 KR |
251 | * here with number, in correct radix. c is the next char. |
252 | * note that unlike un*x, we allow "011f" "0x9f" to | |
253 | * both mean the same as the (conventional) "9f". this is simply easier | |
254 | * than checking for strict canonical form. syntax sux! | |
f8701a3f | 255 | */ |
2ed83a59 KR |
256 | |
257 | switch (c) | |
258 | { | |
259 | ||
260 | #ifdef LOCAL_LABELS_FB | |
261 | case 'b': | |
262 | { | |
263 | /* | |
264 | * backward ref to local label. | |
265 | * because it is backward, expect it to be defined. | |
266 | */ | |
267 | /* Construct a local label. */ | |
268 | name = fb_label_name ((int) number, 0); | |
269 | ||
270 | /* seen before, or symbol is defined: ok */ | |
271 | symbolP = symbol_find (name); | |
272 | if ((symbolP != NULL) && (S_IS_DEFINED (symbolP))) | |
273 | { | |
274 | ||
275 | /* local labels are never absolute. don't waste time | |
276 | checking absoluteness. */ | |
277 | know (SEG_NORMAL (S_GET_SEGMENT (symbolP))); | |
278 | ||
5ac34ac3 | 279 | expressionP->X_op = O_symbol; |
2ed83a59 | 280 | expressionP->X_add_symbol = symbolP; |
2ed83a59 KR |
281 | |
282 | } | |
283 | else | |
dae92eab KR |
284 | { |
285 | /* either not seen or not defined. */ | |
286 | /* @@ Should print out the original string instead of | |
287 | the parsed number. */ | |
288 | as_bad ("backw. ref to unknown label \"%d:\", 0 assumed.", | |
289 | (int) number); | |
5ac34ac3 | 290 | expressionP->X_op = O_constant; |
2ed83a59 KR |
291 | } |
292 | ||
293 | expressionP->X_add_number = 0; | |
294 | break; | |
295 | } /* case 'b' */ | |
296 | ||
297 | case 'f': | |
298 | { | |
299 | /* | |
300 | * forward reference. expect symbol to be undefined or | |
301 | * unknown. undefined: seen it before. unknown: never seen | |
302 | * it before. | |
303 | * construct a local label name, then an undefined symbol. | |
304 | * don't create a xseg frag for it: caller may do that. | |
305 | * just return it as never seen before. | |
306 | */ | |
307 | name = fb_label_name ((int) number, 1); | |
308 | symbolP = symbol_find_or_make (name); | |
309 | /* we have no need to check symbol properties. */ | |
c593cf41 | 310 | #ifndef many_segments |
2ed83a59 KR |
311 | /* since "know" puts its arg into a "string", we |
312 | can't have newlines in the argument. */ | |
f2f7d044 | 313 | know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section); |
c593cf41 | 314 | #endif |
5ac34ac3 | 315 | expressionP->X_op = O_symbol; |
2ed83a59 | 316 | expressionP->X_add_symbol = symbolP; |
2ed83a59 KR |
317 | expressionP->X_add_number = 0; |
318 | ||
319 | break; | |
320 | } /* case 'f' */ | |
321 | ||
f8701a3f | 322 | #endif /* LOCAL_LABELS_FB */ |
2ed83a59 | 323 | |
f8701a3f | 324 | #ifdef LOCAL_LABELS_DOLLAR |
f8701a3f | 325 | |
2ed83a59 KR |
326 | case '$': |
327 | { | |
328 | ||
329 | /* If the dollar label is *currently* defined, then this is just | |
330 | another reference to it. If it is not *currently* defined, | |
331 | then this is a fresh instantiation of that number, so create | |
332 | it. */ | |
333 | ||
334 | if (dollar_label_defined (number)) | |
335 | { | |
336 | name = dollar_label_name (number, 0); | |
337 | symbolP = symbol_find (name); | |
338 | know (symbolP != NULL); | |
339 | } | |
340 | else | |
341 | { | |
342 | name = dollar_label_name (number, 1); | |
343 | symbolP = symbol_find_or_make (name); | |
344 | } | |
345 | ||
5ac34ac3 | 346 | expressionP->X_op = O_symbol; |
2ed83a59 KR |
347 | expressionP->X_add_symbol = symbolP; |
348 | expressionP->X_add_number = 0; | |
2ed83a59 KR |
349 | |
350 | break; | |
351 | } /* case '$' */ | |
352 | ||
f8701a3f | 353 | #endif /* LOCAL_LABELS_DOLLAR */ |
2ed83a59 KR |
354 | |
355 | default: | |
356 | { | |
5ac34ac3 | 357 | expressionP->X_op = O_constant; |
2ed83a59 | 358 | expressionP->X_add_number = number; |
2ed83a59 KR |
359 | input_line_pointer--; /* restore following character. */ |
360 | break; | |
361 | } /* really just a number */ | |
362 | ||
363 | } /* switch on char following the number */ | |
364 | ||
365 | ||
366 | } | |
367 | else | |
dae92eab KR |
368 | { |
369 | /* not a small number */ | |
5ac34ac3 | 370 | expressionP->X_op = O_big; |
c593cf41 | 371 | expressionP->X_add_number = number; |
2ed83a59 | 372 | input_line_pointer--; /*->char following number. */ |
dae92eab | 373 | } |
2ed83a59 | 374 | } /* integer_constant() */ |
c593cf41 SC |
375 | |
376 | ||
fecd2382 RP |
377 | /* |
378 | * Summary of operand(). | |
379 | * | |
380 | * in: Input_line_pointer points to 1st char of operand, which may | |
381 | * be a space. | |
382 | * | |
5ac34ac3 ILT |
383 | * out: A expressionS. |
384 | * The operand may have been empty: in this case X_op == O_absent. | |
fecd2382 | 385 | * Input_line_pointer->(next non-blank) char after operand. |
fecd2382 | 386 | */ |
c593cf41 | 387 | |
fecd2382 | 388 | static segT |
c593cf41 | 389 | operand (expressionP) |
dae92eab | 390 | expressionS *expressionP; |
fecd2382 | 391 | { |
dae92eab KR |
392 | char c; |
393 | symbolS *symbolP; /* points to symbol */ | |
394 | char *name; /* points to name of symbol */ | |
58d4951d | 395 | segT segment; |
c593cf41 SC |
396 | |
397 | /* digits, assume it is a bignum. */ | |
398 | ||
2ed83a59 KR |
399 | SKIP_WHITESPACE (); /* leading whitespace is part of operand. */ |
400 | c = *input_line_pointer++; /* input_line_pointer->past char in c. */ | |
c593cf41 SC |
401 | |
402 | switch (c) | |
fecd2382 | 403 | { |
2ed83a59 KR |
404 | #ifdef MRI |
405 | case '%': | |
406 | integer_constant (2, expressionP); | |
c593cf41 | 407 | break; |
2ed83a59 KR |
408 | case '@': |
409 | integer_constant (8, expressionP); | |
c593cf41 | 410 | break; |
2ed83a59 KR |
411 | case '$': |
412 | integer_constant (16, expressionP); | |
c593cf41 | 413 | break; |
2ed83a59 | 414 | #endif |
c593cf41 SC |
415 | case '1': |
416 | case '2': | |
417 | case '3': | |
418 | case '4': | |
419 | case '5': | |
420 | case '6': | |
421 | case '7': | |
2ed83a59 KR |
422 | case '8': |
423 | case '9': | |
424 | input_line_pointer--; | |
425 | ||
426 | integer_constant (10, expressionP); | |
c593cf41 SC |
427 | break; |
428 | ||
2ed83a59 KR |
429 | case '0': |
430 | /* non-decimal radix */ | |
431 | ||
2ed83a59 KR |
432 | c = *input_line_pointer; |
433 | switch (c) | |
434 | { | |
435 | ||
436 | default: | |
437 | if (c && strchr (FLT_CHARS, c)) | |
438 | { | |
439 | input_line_pointer++; | |
440 | floating_constant (expressionP); | |
441 | } | |
442 | else | |
443 | { | |
444 | /* The string was only zero */ | |
5ac34ac3 | 445 | expressionP->X_op = O_constant; |
2ed83a59 | 446 | expressionP->X_add_number = 0; |
2ed83a59 KR |
447 | } |
448 | ||
449 | break; | |
450 | ||
451 | case 'x': | |
452 | case 'X': | |
453 | input_line_pointer++; | |
454 | integer_constant (16, expressionP); | |
455 | break; | |
456 | ||
457 | case 'b': | |
458 | #ifdef LOCAL_LABELS_FB | |
5ac34ac3 ILT |
459 | /* FIXME: This seems to be nonsense. At this point we know |
460 | for sure that *input_line_pointer is 'b'. So why are we | |
461 | checking it? What is this code supposed to do? */ | |
2ed83a59 KR |
462 | if (!*input_line_pointer |
463 | || (!strchr ("+-.0123456789", *input_line_pointer) | |
464 | && !strchr (EXP_CHARS, *input_line_pointer))) | |
465 | { | |
466 | input_line_pointer--; | |
467 | integer_constant (10, expressionP); | |
468 | break; | |
469 | } | |
470 | #endif | |
471 | case 'B': | |
472 | input_line_pointer++; | |
473 | integer_constant (2, expressionP); | |
474 | break; | |
475 | ||
476 | case '0': | |
477 | case '1': | |
478 | case '2': | |
479 | case '3': | |
480 | case '4': | |
481 | case '5': | |
482 | case '6': | |
483 | case '7': | |
484 | integer_constant (8, expressionP); | |
485 | break; | |
486 | ||
487 | case 'f': | |
488 | #ifdef LOCAL_LABELS_FB | |
489 | /* if it says '0f' and the line ends or it doesn't look like | |
d841bc49 | 490 | a floating point #, its a local label ref. dtrt */ |
2ed83a59 | 491 | /* likewise for the b's. xoxorich. */ |
5ac34ac3 ILT |
492 | /* FIXME: As in the 'b' case, we know that the |
493 | *input_line_pointer is 'f'. What is this code really | |
494 | trying to do? */ | |
2ed83a59 KR |
495 | if (c == 'f' |
496 | && (!*input_line_pointer || | |
497 | (!strchr ("+-.0123456789", *input_line_pointer) && | |
498 | !strchr (EXP_CHARS, *input_line_pointer)))) | |
499 | { | |
500 | input_line_pointer -= 1; | |
501 | integer_constant (10, expressionP); | |
502 | break; | |
503 | } | |
504 | #endif | |
505 | ||
506 | case 'd': | |
507 | case 'D': | |
508 | case 'F': | |
509 | case 'r': | |
510 | case 'e': | |
511 | case 'E': | |
512 | case 'g': | |
513 | case 'G': | |
514 | ||
515 | input_line_pointer++; | |
516 | floating_constant (expressionP); | |
f2f7d044 | 517 | expressionP->X_add_number = -(isupper (c) ? tolower (c) : c); |
2ed83a59 KR |
518 | break; |
519 | ||
520 | #ifdef LOCAL_LABELS_DOLLAR | |
521 | case '$': | |
522 | integer_constant (10, expressionP); | |
523 | break; | |
524 | #endif | |
525 | } | |
526 | ||
c593cf41 | 527 | break; |
5ac34ac3 | 528 | |
2ed83a59 KR |
529 | case '(': |
530 | /* didn't begin with digit & not a name */ | |
58d4951d | 531 | segment = expression (expressionP); |
5ac34ac3 ILT |
532 | /* Expression() will pass trailing whitespace */ |
533 | if (*input_line_pointer++ != ')') | |
534 | { | |
535 | as_bad ("Missing ')' assumed"); | |
536 | input_line_pointer--; | |
537 | } | |
538 | /* here with input_line_pointer->char after "(...)" */ | |
58d4951d | 539 | return segment; |
c593cf41 | 540 | |
2ed83a59 | 541 | case '\'': |
d841bc49 KR |
542 | /* Warning: to conform to other people's assemblers NO ESCAPEMENT is |
543 | permitted for a single quote. The next character, parity errors and | |
544 | all, is taken as the value of the operand. VERY KINKY. */ | |
5ac34ac3 | 545 | expressionP->X_op = O_constant; |
2ed83a59 | 546 | expressionP->X_add_number = *input_line_pointer++; |
2ed83a59 KR |
547 | break; |
548 | ||
49864cfa | 549 | case '+': |
58d4951d | 550 | (void) operand (expressionP); |
49864cfa KR |
551 | break; |
552 | ||
2ed83a59 KR |
553 | case '~': |
554 | case '-': | |
2ed83a59 | 555 | { |
5ac34ac3 ILT |
556 | operand (expressionP); |
557 | if (expressionP->X_op == O_constant) | |
2ed83a59 | 558 | { |
2ed83a59 KR |
559 | /* input_line_pointer -> char after operand */ |
560 | if (c == '-') | |
561 | { | |
5ac34ac3 | 562 | expressionP->X_add_number = - expressionP->X_add_number; |
d841bc49 KR |
563 | /* Notice: '-' may overflow: no warning is given. This is |
564 | compatible with other people's assemblers. Sigh. */ | |
2ed83a59 KR |
565 | } |
566 | else | |
5ac34ac3 | 567 | expressionP->X_add_number = ~ expressionP->X_add_number; |
f2f7d044 | 568 | } |
5ac34ac3 ILT |
569 | else if (expressionP->X_op != O_illegal |
570 | && expressionP->X_op != O_absent) | |
f2f7d044 | 571 | { |
5ac34ac3 | 572 | expressionP->X_add_symbol = make_expr_symbol (expressionP); |
2ed83a59 | 573 | if (c == '-') |
5ac34ac3 | 574 | expressionP->X_op = O_uminus; |
f2f7d044 | 575 | else |
5ac34ac3 ILT |
576 | expressionP->X_op = O_bit_not; |
577 | expressionP->X_add_number = 0; | |
c593cf41 | 578 | } |
f2f7d044 | 579 | else |
5ac34ac3 ILT |
580 | as_warn ("Unary operator %c ignored because bad operand follows", |
581 | c); | |
c593cf41 | 582 | } |
2ed83a59 KR |
583 | break; |
584 | ||
585 | case '.': | |
586 | if (!is_part_of_name (*input_line_pointer)) | |
587 | { | |
5ac34ac3 | 588 | const char *fake; |
2ed83a59 | 589 | |
85825401 ILT |
590 | /* JF: '.' is pseudo symbol with value of current location |
591 | in current segment. */ | |
592 | #ifdef DOT_LABEL_PREFIX | |
593 | fake = ".L0\001"; | |
594 | #else | |
595 | fake = "L0\001"; | |
596 | #endif | |
597 | symbolP = symbol_new (fake, | |
2ed83a59 | 598 | now_seg, |
5ac34ac3 | 599 | (valueT) frag_now_fix (), |
2ed83a59 | 600 | frag_now); |
c593cf41 | 601 | |
5ac34ac3 | 602 | expressionP->X_op = O_symbol; |
2ed83a59 | 603 | expressionP->X_add_symbol = symbolP; |
5ac34ac3 | 604 | expressionP->X_add_number = 0; |
2ed83a59 | 605 | break; |
2ed83a59 KR |
606 | } |
607 | else | |
608 | { | |
609 | goto isname; | |
2ed83a59 KR |
610 | } |
611 | case ',': | |
612 | case '\n': | |
f2f7d044 | 613 | case '\0': |
0bd77bc4 | 614 | eol: |
2ed83a59 | 615 | /* can't imagine any other kind of operand */ |
5ac34ac3 | 616 | expressionP->X_op = O_absent; |
2ed83a59 KR |
617 | input_line_pointer--; |
618 | md_operand (expressionP); | |
619 | break; | |
0bd77bc4 | 620 | |
2ed83a59 | 621 | default: |
58d4951d | 622 | if (is_end_of_line[(unsigned char) c]) |
0bd77bc4 | 623 | goto eol; |
2ed83a59 KR |
624 | if (is_name_beginner (c)) /* here if did not begin with a digit */ |
625 | { | |
626 | /* | |
d841bc49 KR |
627 | * Identifier begins here. |
628 | * This is kludged for speed, so code is repeated. | |
629 | */ | |
2ed83a59 KR |
630 | isname: |
631 | name = --input_line_pointer; | |
632 | c = get_symbol_end (); | |
633 | symbolP = symbol_find_or_make (name); | |
5ac34ac3 ILT |
634 | |
635 | /* If we have an absolute symbol or a reg, then we know its | |
636 | value now. */ | |
58d4951d ILT |
637 | segment = S_GET_SEGMENT (symbolP); |
638 | if (segment == absolute_section) | |
5ac34ac3 ILT |
639 | { |
640 | expressionP->X_op = O_constant; | |
641 | expressionP->X_add_number = S_GET_VALUE (symbolP); | |
642 | } | |
58d4951d | 643 | else if (segment == reg_section) |
5ac34ac3 ILT |
644 | { |
645 | expressionP->X_op = O_register; | |
646 | expressionP->X_add_number = S_GET_VALUE (symbolP); | |
647 | } | |
f2f7d044 | 648 | else |
2ed83a59 | 649 | { |
5ac34ac3 | 650 | expressionP->X_op = O_symbol; |
2ed83a59 | 651 | expressionP->X_add_symbol = symbolP; |
5ac34ac3 | 652 | expressionP->X_add_number = 0; |
2ed83a59 KR |
653 | } |
654 | *input_line_pointer = c; | |
2ed83a59 KR |
655 | } |
656 | else | |
657 | { | |
658 | as_bad ("Bad expression"); | |
5ac34ac3 | 659 | expressionP->X_op = O_constant; |
2ed83a59 | 660 | expressionP->X_add_number = 0; |
2ed83a59 | 661 | } |
c593cf41 | 662 | } |
c593cf41 | 663 | |
c593cf41 SC |
664 | /* |
665 | * It is more 'efficient' to clean up the expressionS when they are created. | |
666 | * Doing it here saves lines of code. | |
667 | */ | |
668 | clean_up_expression (expressionP); | |
2ed83a59 KR |
669 | SKIP_WHITESPACE (); /*->1st char after operand. */ |
670 | know (*input_line_pointer != ' '); | |
58d4951d ILT |
671 | |
672 | switch (expressionP->X_op) | |
673 | { | |
674 | default: | |
675 | return absolute_section; | |
676 | case O_symbol: | |
677 | return S_GET_SEGMENT (expressionP->X_add_symbol); | |
678 | case O_register: | |
679 | return reg_section; | |
680 | } | |
2ed83a59 | 681 | } /* operand() */ |
fecd2382 RP |
682 | \f |
683 | /* Internal. Simplify a struct expression for use by expr() */ | |
684 | ||
685 | /* | |
686 | * In: address of a expressionS. | |
5ac34ac3 | 687 | * The X_op field of the expressionS may only take certain values. |
fecd2382 RP |
688 | * Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT. |
689 | * Out: expressionS may have been modified: | |
690 | * 'foo-foo' symbol references cancelled to 0, | |
5ac34ac3 | 691 | * which changes X_op from O_subtract to O_constant. |
fecd2382 RP |
692 | * Unused fields zeroed to help expr(). |
693 | */ | |
694 | ||
695 | static void | |
c593cf41 | 696 | clean_up_expression (expressionP) |
dae92eab | 697 | expressionS *expressionP; |
fecd2382 | 698 | { |
5ac34ac3 | 699 | switch (expressionP->X_op) |
2ed83a59 | 700 | { |
5ac34ac3 ILT |
701 | case O_illegal: |
702 | case O_absent: | |
2ed83a59 | 703 | expressionP->X_add_number = 0; |
5ac34ac3 ILT |
704 | /* Fall through. */ |
705 | case O_big: | |
706 | case O_constant: | |
707 | case O_register: | |
2ed83a59 | 708 | expressionP->X_add_symbol = NULL; |
5ac34ac3 ILT |
709 | /* Fall through. */ |
710 | case O_symbol: | |
711 | case O_uminus: | |
712 | case O_bit_not: | |
713 | expressionP->X_op_symbol = NULL; | |
714 | break; | |
715 | case O_subtract: | |
716 | if (expressionP->X_op_symbol == expressionP->X_add_symbol | |
717 | || ((expressionP->X_op_symbol->sy_frag | |
718 | == expressionP->X_add_symbol->sy_frag) | |
ffffc8fb | 719 | && SEG_NORMAL (S_GET_SEGMENT (expressionP->X_add_symbol)) |
5ac34ac3 | 720 | && (S_GET_VALUE (expressionP->X_op_symbol) |
49864cfa | 721 | == S_GET_VALUE (expressionP->X_add_symbol)))) |
2ed83a59 | 722 | { |
5ac34ac3 | 723 | expressionP->X_op = O_constant; |
2ed83a59 | 724 | expressionP->X_add_symbol = NULL; |
5ac34ac3 | 725 | expressionP->X_op_symbol = NULL; |
fecd2382 | 726 | } |
5ac34ac3 ILT |
727 | break; |
728 | default: | |
729 | break; | |
fecd2382 | 730 | } |
f2f7d044 | 731 | } |
fecd2382 RP |
732 | \f |
733 | /* Expression parser. */ | |
734 | ||
735 | /* | |
736 | * We allow an empty expression, and just assume (absolute,0) silently. | |
737 | * Unary operators and parenthetical expressions are treated as operands. | |
738 | * As usual, Q==quantity==operand, O==operator, X==expression mnemonics. | |
739 | * | |
740 | * We used to do a aho/ullman shift-reduce parser, but the logic got so | |
741 | * warped that I flushed it and wrote a recursive-descent parser instead. | |
742 | * Now things are stable, would anybody like to write a fast parser? | |
743 | * Most expressions are either register (which does not even reach here) | |
744 | * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common. | |
745 | * So I guess it doesn't really matter how inefficient more complex expressions | |
746 | * are parsed. | |
747 | * | |
748 | * After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK. | |
749 | * Also, we have consumed any leading or trailing spaces (operand does that) | |
750 | * and done all intervening operators. | |
5ac34ac3 ILT |
751 | * |
752 | * This returns the segment of the result, which will be | |
753 | * absolute_section or the segment of a symbol. | |
fecd2382 RP |
754 | */ |
755 | ||
49864cfa | 756 | #undef __ |
fecd2382 RP |
757 | #define __ O_illegal |
758 | ||
2ed83a59 KR |
759 | static const operatorT op_encoding[256] = |
760 | { /* maps ASCII->operators */ | |
761 | ||
762 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
763 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
764 | ||
765 | __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __, | |
766 | __, __, O_multiply, O_add, __, O_subtract, __, O_divide, | |
767 | __, __, __, __, __, __, __, __, | |
768 | __, __, __, __, O_left_shift, __, O_right_shift, __, | |
769 | __, __, __, __, __, __, __, __, | |
770 | __, __, __, __, __, __, __, __, | |
771 | __, __, __, __, __, __, __, __, | |
772 | __, __, __, __, __, __, O_bit_exclusive_or, __, | |
773 | __, __, __, __, __, __, __, __, | |
774 | __, __, __, __, __, __, __, __, | |
775 | __, __, __, __, __, __, __, __, | |
776 | __, __, __, __, O_bit_inclusive_or, __, __, __, | |
777 | ||
778 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
779 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
780 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
781 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
782 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
783 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
784 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, | |
785 | __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __ | |
786 | }; | |
fecd2382 RP |
787 | |
788 | ||
789 | /* | |
790 | * Rank Examples | |
791 | * 0 operand, (expression) | |
792 | * 1 + - | |
793 | * 2 & ^ ! | | |
794 | * 3 * / % << >> | |
5ac34ac3 | 795 | * 4 unary - unary ~ |
fecd2382 | 796 | */ |
5ac34ac3 ILT |
797 | static const operator_rankT op_rank[] = |
798 | { | |
799 | 0, /* O_illegal */ | |
800 | 0, /* O_absent */ | |
801 | 0, /* O_constant */ | |
802 | 0, /* O_symbol */ | |
803 | 0, /* O_register */ | |
804 | 0, /* O_bit */ | |
805 | 4, /* O_uminus */ | |
806 | 4, /* O_bit_now */ | |
807 | 3, /* O_multiply */ | |
808 | 3, /* O_divide */ | |
809 | 3, /* O_modulus */ | |
810 | 3, /* O_left_shift */ | |
811 | 3, /* O_right_shift */ | |
812 | 2, /* O_bit_inclusive_or */ | |
813 | 2, /* O_bit_or_not */ | |
814 | 2, /* O_bit_exclusive_or */ | |
815 | 2, /* O_bit_and */ | |
816 | 1, /* O_add */ | |
817 | 1, /* O_subtract */ | |
818 | }; | |
fecd2382 | 819 | \f |
5ac34ac3 | 820 | segT |
2ed83a59 | 821 | expr (rank, resultP) |
dae92eab KR |
822 | operator_rankT rank; /* Larger # is higher rank. */ |
823 | expressionS *resultP; /* Deliver result here. */ | |
fecd2382 | 824 | { |
5ac34ac3 | 825 | segT retval; |
2ed83a59 | 826 | expressionS right; |
dae92eab KR |
827 | operatorT op_left; |
828 | char c_left; /* 1st operator character. */ | |
829 | operatorT op_right; | |
830 | char c_right; | |
c593cf41 | 831 | |
2ed83a59 | 832 | know (rank >= 0); |
5ac34ac3 ILT |
833 | |
834 | retval = operand (resultP); | |
835 | ||
2ed83a59 | 836 | know (*input_line_pointer != ' '); /* Operand() gobbles spaces. */ |
5ac34ac3 | 837 | |
2ed83a59 | 838 | c_left = *input_line_pointer; /* Potential operator character. */ |
58d4951d | 839 | op_left = op_encoding[(unsigned char) c_left]; |
2ed83a59 | 840 | while (op_left != O_illegal && op_rank[(int) op_left] > rank) |
fecd2382 | 841 | { |
5ac34ac3 ILT |
842 | segT rightseg; |
843 | ||
2ed83a59 KR |
844 | input_line_pointer++; /*->after 1st character of operator. */ |
845 | /* Operators "<<" and ">>" have 2 characters. */ | |
846 | if (*input_line_pointer == c_left && (c_left == '<' || c_left == '>')) | |
5ac34ac3 ILT |
847 | ++input_line_pointer; |
848 | ||
849 | rightseg = expr (op_rank[(int) op_left], &right); | |
850 | if (right.X_op == O_absent) | |
fecd2382 | 851 | { |
5ac34ac3 ILT |
852 | as_warn ("missing operand; zero assumed"); |
853 | right.X_op = O_constant; | |
854 | right.X_add_number = 0; | |
2ed83a59 | 855 | resultP->X_add_symbol = NULL; |
5ac34ac3 | 856 | resultP->X_op_symbol = NULL; |
fecd2382 | 857 | } |
5ac34ac3 | 858 | |
2ed83a59 | 859 | know (*input_line_pointer != ' '); |
5ac34ac3 | 860 | |
58d4951d ILT |
861 | if (retval == undefined_section) |
862 | { | |
863 | if (SEG_NORMAL (rightseg)) | |
864 | retval = rightseg; | |
865 | } | |
866 | else if (! SEG_NORMAL (retval)) | |
5ac34ac3 ILT |
867 | retval = rightseg; |
868 | else if (SEG_NORMAL (rightseg) | |
869 | && retval != rightseg) | |
870 | as_bad ("operation combines symbols in different segments"); | |
871 | ||
2ed83a59 | 872 | c_right = *input_line_pointer; |
58d4951d | 873 | op_right = op_encoding[(unsigned char) c_right]; |
2ed83a59 | 874 | if (*input_line_pointer == c_right && (c_right == '<' || c_right == '>')) |
5ac34ac3 ILT |
875 | ++input_line_pointer; |
876 | ||
877 | know (op_right == O_illegal || op_rank[(int) op_right] <= op_rank[(int) op_left]); | |
878 | know ((int) op_left >= (int) O_multiply && (int) op_left <= (int) O_subtract); | |
879 | ||
c593cf41 SC |
880 | /* input_line_pointer->after right-hand quantity. */ |
881 | /* left-hand quantity in resultP */ | |
882 | /* right-hand quantity in right. */ | |
883 | /* operator in op_left. */ | |
5ac34ac3 ILT |
884 | |
885 | if (resultP->X_op == O_big) | |
fecd2382 | 886 | { |
5ac34ac3 ILT |
887 | as_warn ("left operand of %c is a %s; integer 0 assumed", |
888 | c_left, resultP->X_add_number > 0 ? "bignum" : "float"); | |
889 | resultP->X_op = O_constant; | |
890 | resultP->X_add_number = 0; | |
891 | resultP->X_add_symbol = NULL; | |
892 | resultP->X_op_symbol = NULL; | |
fecd2382 | 893 | } |
5ac34ac3 | 894 | if (right.X_op == O_big) |
fecd2382 | 895 | { |
5ac34ac3 ILT |
896 | as_warn ("right operand of %c is a %s; integer 0 assumed", |
897 | c_left, right.X_add_number > 0 ? "bignum" : "float"); | |
898 | right.X_op = O_constant; | |
899 | right.X_add_number = 0; | |
900 | right.X_add_symbol = NULL; | |
901 | right.X_op_symbol = NULL; | |
902 | } | |
903 | ||
904 | /* Optimize common cases. */ | |
905 | if (op_left == O_add && right.X_op == O_constant) | |
906 | { | |
907 | /* X + constant. */ | |
908 | resultP->X_add_number += right.X_add_number; | |
909 | } | |
910 | else if (op_left == O_subtract && right.X_op == O_constant) | |
911 | { | |
912 | /* X - constant. */ | |
913 | resultP->X_add_number -= right.X_add_number; | |
914 | } | |
915 | else if (op_left == O_add && resultP->X_op == O_constant) | |
916 | { | |
917 | /* Constant + X. */ | |
918 | resultP->X_op = right.X_op; | |
919 | resultP->X_add_symbol = right.X_add_symbol; | |
920 | resultP->X_op_symbol = right.X_op_symbol; | |
921 | resultP->X_add_number += right.X_add_number; | |
922 | retval = rightseg; | |
923 | } | |
924 | else if (resultP->X_op == O_constant && right.X_op == O_constant) | |
925 | { | |
926 | /* Constant OP constant. */ | |
927 | offsetT v = right.X_add_number; | |
928 | if (v == 0 && (op_left == O_divide || op_left == O_modulus)) | |
fecd2382 | 929 | { |
5ac34ac3 ILT |
930 | as_warn ("division by zero"); |
931 | v = 1; | |
fecd2382 | 932 | } |
5ac34ac3 | 933 | switch (op_left) |
fecd2382 | 934 | { |
5ac34ac3 ILT |
935 | case O_multiply: resultP->X_add_number *= v; break; |
936 | case O_divide: resultP->X_add_number /= v; break; | |
937 | case O_modulus: resultP->X_add_number %= v; break; | |
938 | case O_left_shift: resultP->X_add_number <<= v; break; | |
939 | case O_right_shift: resultP->X_add_number >>= v; break; | |
940 | case O_bit_inclusive_or: resultP->X_add_number |= v; break; | |
941 | case O_bit_or_not: resultP->X_add_number |= ~v; break; | |
942 | case O_bit_exclusive_or: resultP->X_add_number ^= v; break; | |
943 | case O_bit_and: resultP->X_add_number &= v; break; | |
944 | case O_add: resultP->X_add_number += v; break; | |
945 | case O_subtract: resultP->X_add_number -= v; break; | |
946 | default: abort (); | |
fecd2382 | 947 | } |
5ac34ac3 ILT |
948 | } |
949 | else if (resultP->X_op == O_symbol | |
950 | && right.X_op == O_symbol | |
951 | && (op_left == O_add | |
952 | || op_left == O_subtract | |
953 | || (resultP->X_add_number == 0 | |
954 | && right.X_add_number == 0))) | |
955 | { | |
956 | /* Symbol OP symbol. */ | |
957 | resultP->X_op = op_left; | |
958 | resultP->X_op_symbol = right.X_add_symbol; | |
c593cf41 | 959 | if (op_left == O_add) |
5ac34ac3 ILT |
960 | resultP->X_add_number += right.X_add_number; |
961 | else if (op_left == O_subtract) | |
962 | resultP->X_add_number -= right.X_add_number; | |
963 | } | |
964 | else | |
965 | { | |
966 | /* The general case. */ | |
967 | resultP->X_add_symbol = make_expr_symbol (resultP); | |
968 | resultP->X_op_symbol = make_expr_symbol (&right); | |
969 | resultP->X_op = op_left; | |
970 | resultP->X_add_number = 0; | |
971 | } | |
972 | ||
2ed83a59 | 973 | op_left = op_right; |
fecd2382 | 974 | } /* While next operator is >= this rank. */ |
5ac34ac3 ILT |
975 | |
976 | return resultP->X_op == O_constant ? absolute_section : retval; | |
fecd2382 RP |
977 | } |
978 | \f | |
979 | /* | |
980 | * get_symbol_end() | |
981 | * | |
982 | * This lives here because it belongs equally in expr.c & read.c. | |
983 | * Expr.c is just a branch office read.c anyway, and putting it | |
984 | * here lessens the crowd at read.c. | |
985 | * | |
986 | * Assume input_line_pointer is at start of symbol name. | |
987 | * Advance input_line_pointer past symbol name. | |
988 | * Turn that character into a '\0', returning its former value. | |
989 | * This allows a string compare (RMS wants symbol names to be strings) | |
990 | * of the symbol name. | |
991 | * There will always be a char following symbol name, because all good | |
992 | * lines end in end-of-line. | |
993 | */ | |
994 | char | |
2ed83a59 | 995 | get_symbol_end () |
fecd2382 | 996 | { |
dae92eab | 997 | char c; |
2ed83a59 KR |
998 | |
999 | while (is_part_of_name (c = *input_line_pointer++)) | |
1000 | ; | |
1001 | *--input_line_pointer = 0; | |
1002 | return (c); | |
fecd2382 RP |
1003 | } |
1004 | ||
a39116f1 | 1005 | |
2ed83a59 KR |
1006 | unsigned int |
1007 | get_single_number () | |
a39116f1 | 1008 | { |
2ed83a59 KR |
1009 | expressionS exp; |
1010 | operand (&exp); | |
1011 | return exp.X_add_number; | |
1012 | ||
a39116f1 | 1013 | } |
2ed83a59 | 1014 | |
8b228fe9 | 1015 | /* end of expr.c */ |