/contrib/binutils/gas/expr.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 2179 lines · 1879 code · 107 blank · 193 comment · 241 complexity · 060f0fc161e83cd6f9c7e7d81967b295 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* expr.c -operands, expressions-
  2. Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
  3. 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
  4. Free Software Foundation, Inc.
  5. This file is part of GAS, the GNU Assembler.
  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.
  10. GAS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GAS; see the file COPYING. If not, write to the Free
  16. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* This is really a branch office of as-read.c. I split it out to clearly
  19. distinguish the world of expressions from the world of statements.
  20. (It also gives smaller files to re-compile.)
  21. Here, "operand"s are of expressions, not instructions. */
  22. #define min(a, b) ((a) < (b) ? (a) : (b))
  23. #include "as.h"
  24. #include "safe-ctype.h"
  25. #include "obstack.h"
  26. static void floating_constant (expressionS * expressionP);
  27. static valueT generic_bignum_to_int32 (void);
  28. #ifdef BFD64
  29. static valueT generic_bignum_to_int64 (void);
  30. #endif
  31. static void integer_constant (int radix, expressionS * expressionP);
  32. static void mri_char_constant (expressionS *);
  33. static void current_location (expressionS *);
  34. static void clean_up_expression (expressionS * expressionP);
  35. static segT operand (expressionS *, enum expr_mode);
  36. static operatorT operator (int *);
  37. extern const char EXP_CHARS[], FLT_CHARS[];
  38. /* We keep a mapping of expression symbols to file positions, so that
  39. we can provide better error messages. */
  40. struct expr_symbol_line {
  41. struct expr_symbol_line *next;
  42. symbolS *sym;
  43. char *file;
  44. unsigned int line;
  45. };
  46. static struct expr_symbol_line *expr_symbol_lines;
  47. /* Build a dummy symbol to hold a complex expression. This is how we
  48. build expressions up out of other expressions. The symbol is put
  49. into the fake section expr_section. */
  50. symbolS *
  51. make_expr_symbol (expressionS *expressionP)
  52. {
  53. expressionS zero;
  54. symbolS *symbolP;
  55. struct expr_symbol_line *n;
  56. if (expressionP->X_op == O_symbol
  57. && expressionP->X_add_number == 0)
  58. return expressionP->X_add_symbol;
  59. if (expressionP->X_op == O_big)
  60. {
  61. /* This won't work, because the actual value is stored in
  62. generic_floating_point_number or generic_bignum, and we are
  63. going to lose it if we haven't already. */
  64. if (expressionP->X_add_number > 0)
  65. as_bad (_("bignum invalid"));
  66. else
  67. as_bad (_("floating point number invalid"));
  68. zero.X_op = O_constant;
  69. zero.X_add_number = 0;
  70. zero.X_unsigned = 0;
  71. clean_up_expression (&zero);
  72. expressionP = &zero;
  73. }
  74. /* Putting constant symbols in absolute_section rather than
  75. expr_section is convenient for the old a.out code, for which
  76. S_GET_SEGMENT does not always retrieve the value put in by
  77. S_SET_SEGMENT. */
  78. symbolP = symbol_create (FAKE_LABEL_NAME,
  79. (expressionP->X_op == O_constant
  80. ? absolute_section
  81. : expr_section),
  82. 0, &zero_address_frag);
  83. symbol_set_value_expression (symbolP, expressionP);
  84. if (expressionP->X_op == O_constant)
  85. resolve_symbol_value (symbolP);
  86. n = (struct expr_symbol_line *) xmalloc (sizeof *n);
  87. n->sym = symbolP;
  88. as_where (&n->file, &n->line);
  89. n->next = expr_symbol_lines;
  90. expr_symbol_lines = n;
  91. return symbolP;
  92. }
  93. /* Return the file and line number for an expr symbol. Return
  94. non-zero if something was found, 0 if no information is known for
  95. the symbol. */
  96. int
  97. expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
  98. {
  99. register struct expr_symbol_line *l;
  100. for (l = expr_symbol_lines; l != NULL; l = l->next)
  101. {
  102. if (l->sym == sym)
  103. {
  104. *pfile = l->file;
  105. *pline = l->line;
  106. return 1;
  107. }
  108. }
  109. return 0;
  110. }
  111. /* Utilities for building expressions.
  112. Since complex expressions are recorded as symbols for use in other
  113. expressions these return a symbolS * and not an expressionS *.
  114. These explicitly do not take an "add_number" argument. */
  115. /* ??? For completeness' sake one might want expr_build_symbol.
  116. It would just return its argument. */
  117. /* Build an expression for an unsigned constant.
  118. The corresponding one for signed constants is missing because
  119. there's currently no need for it. One could add an unsigned_p flag
  120. but that seems more clumsy. */
  121. symbolS *
  122. expr_build_uconstant (offsetT value)
  123. {
  124. expressionS e;
  125. e.X_op = O_constant;
  126. e.X_add_number = value;
  127. e.X_unsigned = 1;
  128. return make_expr_symbol (&e);
  129. }
  130. /* Build an expression for the current location ('.'). */
  131. symbolS *
  132. expr_build_dot (void)
  133. {
  134. expressionS e;
  135. current_location (&e);
  136. return make_expr_symbol (&e);
  137. }
  138. /* Build any floating-point literal here.
  139. Also build any bignum literal here. */
  140. /* Seems atof_machine can backscan through generic_bignum and hit whatever
  141. happens to be loaded before it in memory. And its way too complicated
  142. for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
  143. and never write into the early words, thus they'll always be zero.
  144. I hate Dean's floating-point code. Bleh. */
  145. LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
  146. FLONUM_TYPE generic_floating_point_number = {
  147. &generic_bignum[6], /* low. (JF: Was 0) */
  148. &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
  149. 0, /* leader. */
  150. 0, /* exponent. */
  151. 0 /* sign. */
  152. };
  153. static void
  154. floating_constant (expressionS *expressionP)
  155. {
  156. /* input_line_pointer -> floating-point constant. */
  157. int error_code;
  158. error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
  159. &generic_floating_point_number);
  160. if (error_code)
  161. {
  162. if (error_code == ERROR_EXPONENT_OVERFLOW)
  163. {
  164. as_bad (_("bad floating-point constant: exponent overflow"));
  165. }
  166. else
  167. {
  168. as_bad (_("bad floating-point constant: unknown error code=%d"),
  169. error_code);
  170. }
  171. }
  172. expressionP->X_op = O_big;
  173. /* input_line_pointer -> just after constant, which may point to
  174. whitespace. */
  175. expressionP->X_add_number = -1;
  176. }
  177. static valueT
  178. generic_bignum_to_int32 (void)
  179. {
  180. valueT number =
  181. ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
  182. | (generic_bignum[0] & LITTLENUM_MASK);
  183. number &= 0xffffffff;
  184. return number;
  185. }
  186. #ifdef BFD64
  187. static valueT
  188. generic_bignum_to_int64 (void)
  189. {
  190. valueT number =
  191. ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
  192. << LITTLENUM_NUMBER_OF_BITS)
  193. | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
  194. << LITTLENUM_NUMBER_OF_BITS)
  195. | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
  196. << LITTLENUM_NUMBER_OF_BITS)
  197. | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
  198. return number;
  199. }
  200. #endif
  201. static void
  202. integer_constant (int radix, expressionS *expressionP)
  203. {
  204. char *start; /* Start of number. */
  205. char *suffix = NULL;
  206. char c;
  207. valueT number; /* Offset or (absolute) value. */
  208. short int digit; /* Value of next digit in current radix. */
  209. short int maxdig = 0; /* Highest permitted digit value. */
  210. int too_many_digits = 0; /* If we see >= this number of. */
  211. char *name; /* Points to name of symbol. */
  212. symbolS *symbolP; /* Points to symbol. */
  213. int small; /* True if fits in 32 bits. */
  214. /* May be bignum, or may fit in 32 bits. */
  215. /* Most numbers fit into 32 bits, and we want this case to be fast.
  216. so we pretend it will fit into 32 bits. If, after making up a 32
  217. bit number, we realise that we have scanned more digits than
  218. comfortably fit into 32 bits, we re-scan the digits coding them
  219. into a bignum. For decimal and octal numbers we are
  220. conservative: Some numbers may be assumed bignums when in fact
  221. they do fit into 32 bits. Numbers of any radix can have excess
  222. leading zeros: We strive to recognise this and cast them back
  223. into 32 bits. We must check that the bignum really is more than
  224. 32 bits, and change it back to a 32-bit number if it fits. The
  225. number we are looking for is expected to be positive, but if it
  226. fits into 32 bits as an unsigned number, we let it be a 32-bit
  227. number. The cavalier approach is for speed in ordinary cases. */
  228. /* This has been extended for 64 bits. We blindly assume that if
  229. you're compiling in 64-bit mode, the target is a 64-bit machine.
  230. This should be cleaned up. */
  231. #ifdef BFD64
  232. #define valuesize 64
  233. #else /* includes non-bfd case, mostly */
  234. #define valuesize 32
  235. #endif
  236. if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
  237. {
  238. int flt = 0;
  239. /* In MRI mode, the number may have a suffix indicating the
  240. radix. For that matter, it might actually be a floating
  241. point constant. */
  242. for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
  243. {
  244. if (*suffix == 'e' || *suffix == 'E')
  245. flt = 1;
  246. }
  247. if (suffix == input_line_pointer)
  248. {
  249. radix = 10;
  250. suffix = NULL;
  251. }
  252. else
  253. {
  254. c = *--suffix;
  255. c = TOUPPER (c);
  256. /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
  257. we distinguish between 'B' and 'b'. This is the case for
  258. Z80. */
  259. if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
  260. radix = 2;
  261. else if (c == 'D')
  262. radix = 10;
  263. else if (c == 'O' || c == 'Q')
  264. radix = 8;
  265. else if (c == 'H')
  266. radix = 16;
  267. else if (suffix[1] == '.' || c == 'E' || flt)
  268. {
  269. floating_constant (expressionP);
  270. return;
  271. }
  272. else
  273. {
  274. radix = 10;
  275. suffix = NULL;
  276. }
  277. }
  278. }
  279. switch (radix)
  280. {
  281. case 2:
  282. maxdig = 2;
  283. too_many_digits = valuesize + 1;
  284. break;
  285. case 8:
  286. maxdig = radix = 8;
  287. too_many_digits = (valuesize + 2) / 3 + 1;
  288. break;
  289. case 16:
  290. maxdig = radix = 16;
  291. too_many_digits = (valuesize + 3) / 4 + 1;
  292. break;
  293. case 10:
  294. maxdig = radix = 10;
  295. too_many_digits = (valuesize + 11) / 4; /* Very rough. */
  296. }
  297. #undef valuesize
  298. start = input_line_pointer;
  299. c = *input_line_pointer++;
  300. for (number = 0;
  301. (digit = hex_value (c)) < maxdig;
  302. c = *input_line_pointer++)
  303. {
  304. number = number * radix + digit;
  305. }
  306. /* c contains character after number. */
  307. /* input_line_pointer->char after c. */
  308. small = (input_line_pointer - start - 1) < too_many_digits;
  309. if (radix == 16 && c == '_')
  310. {
  311. /* This is literal of the form 0x333_0_12345678_1.
  312. This example is equivalent to 0x00000333000000001234567800000001. */
  313. int num_little_digits = 0;
  314. int i;
  315. input_line_pointer = start; /* -> 1st digit. */
  316. know (LITTLENUM_NUMBER_OF_BITS == 16);
  317. for (c = '_'; c == '_'; num_little_digits += 2)
  318. {
  319. /* Convert one 64-bit word. */
  320. int ndigit = 0;
  321. number = 0;
  322. for (c = *input_line_pointer++;
  323. (digit = hex_value (c)) < maxdig;
  324. c = *(input_line_pointer++))
  325. {
  326. number = number * radix + digit;
  327. ndigit++;
  328. }
  329. /* Check for 8 digit per word max. */
  330. if (ndigit > 8)
  331. as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
  332. /* Add this chunk to the bignum.
  333. Shift things down 2 little digits. */
  334. know (LITTLENUM_NUMBER_OF_BITS == 16);
  335. for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
  336. i >= 2;
  337. i--)
  338. generic_bignum[i] = generic_bignum[i - 2];
  339. /* Add the new digits as the least significant new ones. */
  340. generic_bignum[0] = number & 0xffffffff;
  341. generic_bignum[1] = number >> 16;
  342. }
  343. /* Again, c is char after number, input_line_pointer->after c. */
  344. if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
  345. num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
  346. assert (num_little_digits >= 4);
  347. if (num_little_digits != 8)
  348. as_bad (_("a bignum with underscores must have exactly 4 words"));
  349. /* We might have some leading zeros. These can be trimmed to give
  350. us a change to fit this constant into a small number. */
  351. while (generic_bignum[num_little_digits - 1] == 0
  352. && num_little_digits > 1)
  353. num_little_digits--;
  354. if (num_little_digits <= 2)
  355. {
  356. /* will fit into 32 bits. */
  357. number = generic_bignum_to_int32 ();
  358. small = 1;
  359. }
  360. #ifdef BFD64
  361. else if (num_little_digits <= 4)
  362. {
  363. /* Will fit into 64 bits. */
  364. number = generic_bignum_to_int64 ();
  365. small = 1;
  366. }
  367. #endif
  368. else
  369. {
  370. small = 0;
  371. /* Number of littlenums in the bignum. */
  372. number = num_little_digits;
  373. }
  374. }
  375. else if (!small)
  376. {
  377. /* We saw a lot of digits. manufacture a bignum the hard way. */
  378. LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
  379. LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
  380. long carry;
  381. leader = generic_bignum;
  382. generic_bignum[0] = 0;
  383. generic_bignum[1] = 0;
  384. generic_bignum[2] = 0;
  385. generic_bignum[3] = 0;
  386. input_line_pointer = start; /* -> 1st digit. */
  387. c = *input_line_pointer++;
  388. for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
  389. {
  390. for (pointer = generic_bignum; pointer <= leader; pointer++)
  391. {
  392. long work;
  393. work = carry + radix * *pointer;
  394. *pointer = work & LITTLENUM_MASK;
  395. carry = work >> LITTLENUM_NUMBER_OF_BITS;
  396. }
  397. if (carry)
  398. {
  399. if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
  400. {
  401. /* Room to grow a longer bignum. */
  402. *++leader = carry;
  403. }
  404. }
  405. }
  406. /* Again, c is char after number. */
  407. /* input_line_pointer -> after c. */
  408. know (LITTLENUM_NUMBER_OF_BITS == 16);
  409. if (leader < generic_bignum + 2)
  410. {
  411. /* Will fit into 32 bits. */
  412. number = generic_bignum_to_int32 ();
  413. small = 1;
  414. }
  415. #ifdef BFD64
  416. else if (leader < generic_bignum + 4)
  417. {
  418. /* Will fit into 64 bits. */
  419. number = generic_bignum_to_int64 ();
  420. small = 1;
  421. }
  422. #endif
  423. else
  424. {
  425. /* Number of littlenums in the bignum. */
  426. number = leader - generic_bignum + 1;
  427. }
  428. }
  429. if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  430. && suffix != NULL
  431. && input_line_pointer - 1 == suffix)
  432. c = *input_line_pointer++;
  433. if (small)
  434. {
  435. /* Here with number, in correct radix. c is the next char.
  436. Note that unlike un*x, we allow "011f" "0x9f" to both mean
  437. the same as the (conventional) "9f".
  438. This is simply easier than checking for strict canonical
  439. form. Syntax sux! */
  440. if (LOCAL_LABELS_FB && c == 'b')
  441. {
  442. /* Backward ref to local label.
  443. Because it is backward, expect it to be defined. */
  444. /* Construct a local label. */
  445. name = fb_label_name ((int) number, 0);
  446. /* Seen before, or symbol is defined: OK. */
  447. symbolP = symbol_find (name);
  448. if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
  449. {
  450. /* Local labels are never absolute. Don't waste time
  451. checking absoluteness. */
  452. know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
  453. expressionP->X_op = O_symbol;
  454. expressionP->X_add_symbol = symbolP;
  455. }
  456. else
  457. {
  458. /* Either not seen or not defined. */
  459. /* @@ Should print out the original string instead of
  460. the parsed number. */
  461. as_bad (_("backward ref to unknown label \"%d:\""),
  462. (int) number);
  463. expressionP->X_op = O_constant;
  464. }
  465. expressionP->X_add_number = 0;
  466. } /* case 'b' */
  467. else if (LOCAL_LABELS_FB && c == 'f')
  468. {
  469. /* Forward reference. Expect symbol to be undefined or
  470. unknown. undefined: seen it before. unknown: never seen
  471. it before.
  472. Construct a local label name, then an undefined symbol.
  473. Don't create a xseg frag for it: caller may do that.
  474. Just return it as never seen before. */
  475. name = fb_label_name ((int) number, 1);
  476. symbolP = symbol_find_or_make (name);
  477. /* We have no need to check symbol properties. */
  478. #ifndef many_segments
  479. /* Since "know" puts its arg into a "string", we
  480. can't have newlines in the argument. */
  481. know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
  482. #endif
  483. expressionP->X_op = O_symbol;
  484. expressionP->X_add_symbol = symbolP;
  485. expressionP->X_add_number = 0;
  486. } /* case 'f' */
  487. else if (LOCAL_LABELS_DOLLAR && c == '$')
  488. {
  489. /* If the dollar label is *currently* defined, then this is just
  490. another reference to it. If it is not *currently* defined,
  491. then this is a fresh instantiation of that number, so create
  492. it. */
  493. if (dollar_label_defined ((long) number))
  494. {
  495. name = dollar_label_name ((long) number, 0);
  496. symbolP = symbol_find (name);
  497. know (symbolP != NULL);
  498. }
  499. else
  500. {
  501. name = dollar_label_name ((long) number, 1);
  502. symbolP = symbol_find_or_make (name);
  503. }
  504. expressionP->X_op = O_symbol;
  505. expressionP->X_add_symbol = symbolP;
  506. expressionP->X_add_number = 0;
  507. } /* case '$' */
  508. else
  509. {
  510. expressionP->X_op = O_constant;
  511. expressionP->X_add_number = number;
  512. input_line_pointer--; /* Restore following character. */
  513. } /* Really just a number. */
  514. }
  515. else
  516. {
  517. /* Not a small number. */
  518. expressionP->X_op = O_big;
  519. expressionP->X_add_number = number; /* Number of littlenums. */
  520. input_line_pointer--; /* -> char following number. */
  521. }
  522. }
  523. /* Parse an MRI multi character constant. */
  524. static void
  525. mri_char_constant (expressionS *expressionP)
  526. {
  527. int i;
  528. if (*input_line_pointer == '\''
  529. && input_line_pointer[1] != '\'')
  530. {
  531. expressionP->X_op = O_constant;
  532. expressionP->X_add_number = 0;
  533. return;
  534. }
  535. /* In order to get the correct byte ordering, we must build the
  536. number in reverse. */
  537. for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
  538. {
  539. int j;
  540. generic_bignum[i] = 0;
  541. for (j = 0; j < CHARS_PER_LITTLENUM; j++)
  542. {
  543. if (*input_line_pointer == '\'')
  544. {
  545. if (input_line_pointer[1] != '\'')
  546. break;
  547. ++input_line_pointer;
  548. }
  549. generic_bignum[i] <<= 8;
  550. generic_bignum[i] += *input_line_pointer;
  551. ++input_line_pointer;
  552. }
  553. if (i < SIZE_OF_LARGE_NUMBER - 1)
  554. {
  555. /* If there is more than one littlenum, left justify the
  556. last one to make it match the earlier ones. If there is
  557. only one, we can just use the value directly. */
  558. for (; j < CHARS_PER_LITTLENUM; j++)
  559. generic_bignum[i] <<= 8;
  560. }
  561. if (*input_line_pointer == '\''
  562. && input_line_pointer[1] != '\'')
  563. break;
  564. }
  565. if (i < 0)
  566. {
  567. as_bad (_("character constant too large"));
  568. i = 0;
  569. }
  570. if (i > 0)
  571. {
  572. int c;
  573. int j;
  574. c = SIZE_OF_LARGE_NUMBER - i;
  575. for (j = 0; j < c; j++)
  576. generic_bignum[j] = generic_bignum[i + j];
  577. i = c;
  578. }
  579. know (LITTLENUM_NUMBER_OF_BITS == 16);
  580. if (i > 2)
  581. {
  582. expressionP->X_op = O_big;
  583. expressionP->X_add_number = i;
  584. }
  585. else
  586. {
  587. expressionP->X_op = O_constant;
  588. if (i < 2)
  589. expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
  590. else
  591. expressionP->X_add_number =
  592. (((generic_bignum[1] & LITTLENUM_MASK)
  593. << LITTLENUM_NUMBER_OF_BITS)
  594. | (generic_bignum[0] & LITTLENUM_MASK));
  595. }
  596. /* Skip the final closing quote. */
  597. ++input_line_pointer;
  598. }
  599. /* Return an expression representing the current location. This
  600. handles the magic symbol `.'. */
  601. static void
  602. current_location (expressionS *expressionp)
  603. {
  604. if (now_seg == absolute_section)
  605. {
  606. expressionp->X_op = O_constant;
  607. expressionp->X_add_number = abs_section_offset;
  608. }
  609. else
  610. {
  611. expressionp->X_op = O_symbol;
  612. expressionp->X_add_symbol = symbol_temp_new_now ();
  613. expressionp->X_add_number = 0;
  614. }
  615. }
  616. /* In: Input_line_pointer points to 1st char of operand, which may
  617. be a space.
  618. Out: An expressionS.
  619. The operand may have been empty: in this case X_op == O_absent.
  620. Input_line_pointer->(next non-blank) char after operand. */
  621. static segT
  622. operand (expressionS *expressionP, enum expr_mode mode)
  623. {
  624. char c;
  625. symbolS *symbolP; /* Points to symbol. */
  626. char *name; /* Points to name of symbol. */
  627. segT segment;
  628. /* All integers are regarded as unsigned unless they are negated.
  629. This is because the only thing which cares whether a number is
  630. unsigned is the code in emit_expr which extends constants into
  631. bignums. It should only sign extend negative numbers, so that
  632. something like ``.quad 0x80000000'' is not sign extended even
  633. though it appears negative if valueT is 32 bits. */
  634. expressionP->X_unsigned = 1;
  635. /* Digits, assume it is a bignum. */
  636. SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
  637. c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
  638. if (is_end_of_line[(unsigned char) c])
  639. goto eol;
  640. switch (c)
  641. {
  642. case '1':
  643. case '2':
  644. case '3':
  645. case '4':
  646. case '5':
  647. case '6':
  648. case '7':
  649. case '8':
  650. case '9':
  651. input_line_pointer--;
  652. integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  653. ? 0 : 10,
  654. expressionP);
  655. break;
  656. #ifdef LITERAL_PREFIXDOLLAR_HEX
  657. case '$':
  658. /* $L is the start of a local label, not a hex constant. */
  659. if (* input_line_pointer == 'L')
  660. goto isname;
  661. integer_constant (16, expressionP);
  662. break;
  663. #endif
  664. #ifdef LITERAL_PREFIXPERCENT_BIN
  665. case '%':
  666. integer_constant (2, expressionP);
  667. break;
  668. #endif
  669. case '0':
  670. /* Non-decimal radix. */
  671. if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  672. {
  673. char *s;
  674. /* Check for a hex or float constant. */
  675. for (s = input_line_pointer; hex_p (*s); s++)
  676. ;
  677. if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
  678. {
  679. --input_line_pointer;
  680. integer_constant (0, expressionP);
  681. break;
  682. }
  683. }
  684. c = *input_line_pointer;
  685. switch (c)
  686. {
  687. case 'o':
  688. case 'O':
  689. case 'q':
  690. case 'Q':
  691. case '8':
  692. case '9':
  693. if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  694. {
  695. integer_constant (0, expressionP);
  696. break;
  697. }
  698. /* Fall through. */
  699. default:
  700. default_case:
  701. if (c && strchr (FLT_CHARS, c))
  702. {
  703. input_line_pointer++;
  704. floating_constant (expressionP);
  705. expressionP->X_add_number = - TOLOWER (c);
  706. }
  707. else
  708. {
  709. /* The string was only zero. */
  710. expressionP->X_op = O_constant;
  711. expressionP->X_add_number = 0;
  712. }
  713. break;
  714. case 'x':
  715. case 'X':
  716. if (flag_m68k_mri)
  717. goto default_case;
  718. input_line_pointer++;
  719. integer_constant (16, expressionP);
  720. break;
  721. case 'b':
  722. if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
  723. {
  724. /* This code used to check for '+' and '-' here, and, in
  725. some conditions, fall through to call
  726. integer_constant. However, that didn't make sense,
  727. as integer_constant only accepts digits. */
  728. /* Some of our code elsewhere does permit digits greater
  729. than the expected base; for consistency, do the same
  730. here. */
  731. if (input_line_pointer[1] < '0'
  732. || input_line_pointer[1] > '9')
  733. {
  734. /* Parse this as a back reference to label 0. */
  735. input_line_pointer--;
  736. integer_constant (10, expressionP);
  737. break;
  738. }
  739. /* Otherwise, parse this as a binary number. */
  740. }
  741. /* Fall through. */
  742. case 'B':
  743. input_line_pointer++;
  744. if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
  745. goto default_case;
  746. integer_constant (2, expressionP);
  747. break;
  748. case '0':
  749. case '1':
  750. case '2':
  751. case '3':
  752. case '4':
  753. case '5':
  754. case '6':
  755. case '7':
  756. integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
  757. ? 0 : 8,
  758. expressionP);
  759. break;
  760. case 'f':
  761. if (LOCAL_LABELS_FB)
  762. {
  763. /* If it says "0f" and it could possibly be a floating point
  764. number, make it one. Otherwise, make it a local label,
  765. and try to deal with parsing the rest later. */
  766. if (!input_line_pointer[1]
  767. || (is_end_of_line[0xff & input_line_pointer[1]])
  768. || strchr (FLT_CHARS, 'f') == NULL)
  769. goto is_0f_label;
  770. {
  771. char *cp = input_line_pointer + 1;
  772. int r = atof_generic (&cp, ".", EXP_CHARS,
  773. &generic_floating_point_number);
  774. switch (r)
  775. {
  776. case 0:
  777. case ERROR_EXPONENT_OVERFLOW:
  778. if (*cp == 'f' || *cp == 'b')
  779. /* Looks like a difference expression. */
  780. goto is_0f_label;
  781. else if (cp == input_line_pointer + 1)
  782. /* No characters has been accepted -- looks like
  783. end of operand. */
  784. goto is_0f_label;
  785. else
  786. goto is_0f_float;
  787. default:
  788. as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
  789. r);
  790. }
  791. }
  792. /* Okay, now we've sorted it out. We resume at one of these
  793. two labels, depending on what we've decided we're probably
  794. looking at. */
  795. is_0f_label:
  796. input_line_pointer--;
  797. integer_constant (10, expressionP);
  798. break;
  799. is_0f_float:
  800. /* Fall through. */
  801. ;
  802. }
  803. case 'd':
  804. case 'D':
  805. if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
  806. {
  807. integer_constant (0, expressionP);
  808. break;
  809. }
  810. /* Fall through. */
  811. case 'F':
  812. case 'r':
  813. case 'e':
  814. case 'E':
  815. case 'g':
  816. case 'G':
  817. input_line_pointer++;
  818. floating_constant (expressionP);
  819. expressionP->X_add_number = - TOLOWER (c);
  820. break;
  821. case '$':
  822. if (LOCAL_LABELS_DOLLAR)
  823. {
  824. integer_constant (10, expressionP);
  825. break;
  826. }
  827. else
  828. goto default_case;
  829. }
  830. break;
  831. case '(':
  832. #ifndef NEED_INDEX_OPERATOR
  833. case '[':
  834. #endif
  835. /* Didn't begin with digit & not a name. */
  836. if (mode != expr_defer)
  837. segment = expression (expressionP);
  838. else
  839. segment = deferred_expression (expressionP);
  840. /* expression () will pass trailing whitespace. */
  841. if ((c == '(' && *input_line_pointer != ')')
  842. || (c == '[' && *input_line_pointer != ']'))
  843. as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
  844. else
  845. input_line_pointer++;
  846. SKIP_WHITESPACE ();
  847. /* Here with input_line_pointer -> char after "(...)". */
  848. return segment;
  849. #ifdef TC_M68K
  850. case 'E':
  851. if (! flag_m68k_mri || *input_line_pointer != '\'')
  852. goto de_fault;
  853. as_bad (_("EBCDIC constants are not supported"));
  854. /* Fall through. */
  855. case 'A':
  856. if (! flag_m68k_mri || *input_line_pointer != '\'')
  857. goto de_fault;
  858. ++input_line_pointer;
  859. /* Fall through. */
  860. #endif
  861. case '\'':
  862. if (! flag_m68k_mri)
  863. {
  864. /* Warning: to conform to other people's assemblers NO
  865. ESCAPEMENT is permitted for a single quote. The next
  866. character, parity errors and all, is taken as the value
  867. of the operand. VERY KINKY. */
  868. expressionP->X_op = O_constant;
  869. expressionP->X_add_number = *input_line_pointer++;
  870. break;
  871. }
  872. mri_char_constant (expressionP);
  873. break;
  874. #ifdef TC_M68K
  875. case '"':
  876. /* Double quote is the bitwise not operator in MRI mode. */
  877. if (! flag_m68k_mri)
  878. goto de_fault;
  879. /* Fall through. */
  880. #endif
  881. case '~':
  882. /* '~' is permitted to start a label on the Delta. */
  883. if (is_name_beginner (c))
  884. goto isname;
  885. case '!':
  886. case '-':
  887. case '+':
  888. {
  889. operand (expressionP, mode);
  890. if (expressionP->X_op == O_constant)
  891. {
  892. /* input_line_pointer -> char after operand. */
  893. if (c == '-')
  894. {
  895. expressionP->X_add_number = - expressionP->X_add_number;
  896. /* Notice: '-' may overflow: no warning is given.
  897. This is compatible with other people's
  898. assemblers. Sigh. */
  899. expressionP->X_unsigned = 0;
  900. }
  901. else if (c == '~' || c == '"')
  902. expressionP->X_add_number = ~ expressionP->X_add_number;
  903. else if (c == '!')
  904. expressionP->X_add_number = ! expressionP->X_add_number;
  905. }
  906. else if (expressionP->X_op == O_big
  907. && expressionP->X_add_number <= 0
  908. && c == '-'
  909. && (generic_floating_point_number.sign == '+'
  910. || generic_floating_point_number.sign == 'P'))
  911. {
  912. /* Negative flonum (eg, -1.000e0). */
  913. if (generic_floating_point_number.sign == '+')
  914. generic_floating_point_number.sign = '-';
  915. else
  916. generic_floating_point_number.sign = 'N';
  917. }
  918. else if (expressionP->X_op == O_big
  919. && expressionP->X_add_number > 0)
  920. {
  921. int i;
  922. if (c == '~' || c == '-')
  923. {
  924. for (i = 0; i < expressionP->X_add_number; ++i)
  925. generic_bignum[i] = ~generic_bignum[i];
  926. if (c == '-')
  927. for (i = 0; i < expressionP->X_add_number; ++i)
  928. {
  929. generic_bignum[i] += 1;
  930. if (generic_bignum[i])
  931. break;
  932. }
  933. }
  934. else if (c == '!')
  935. {
  936. int nonzero = 0;
  937. for (i = 0; i < expressionP->X_add_number; ++i)
  938. {
  939. if (generic_bignum[i])
  940. nonzero = 1;
  941. generic_bignum[i] = 0;
  942. }
  943. generic_bignum[0] = nonzero;
  944. }
  945. }
  946. else if (expressionP->X_op != O_illegal
  947. && expressionP->X_op != O_absent)
  948. {
  949. if (c != '+')
  950. {
  951. expressionP->X_add_symbol = make_expr_symbol (expressionP);
  952. if (c == '-')
  953. expressionP->X_op = O_uminus;
  954. else if (c == '~' || c == '"')
  955. expressionP->X_op = O_bit_not;
  956. else
  957. expressionP->X_op = O_logical_not;
  958. expressionP->X_add_number = 0;
  959. }
  960. }
  961. else
  962. as_warn (_("Unary operator %c ignored because bad operand follows"),
  963. c);
  964. }
  965. break;
  966. #if defined (DOLLAR_DOT) || defined (TC_M68K)
  967. case '$':
  968. /* '$' is the program counter when in MRI mode, or when
  969. DOLLAR_DOT is defined. */
  970. #ifndef DOLLAR_DOT
  971. if (! flag_m68k_mri)
  972. goto de_fault;
  973. #endif
  974. if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
  975. {
  976. /* In MRI mode and on Z80, '$' is also used as the prefix
  977. for a hexadecimal constant. */
  978. integer_constant (16, expressionP);
  979. break;
  980. }
  981. if (is_part_of_name (*input_line_pointer))
  982. goto isname;
  983. current_location (expressionP);
  984. break;
  985. #endif
  986. case '.':
  987. if (!is_part_of_name (*input_line_pointer))
  988. {
  989. current_location (expressionP);
  990. break;
  991. }
  992. else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
  993. && ! is_part_of_name (input_line_pointer[8]))
  994. || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
  995. && ! is_part_of_name (input_line_pointer[7])))
  996. {
  997. int start;
  998. start = (input_line_pointer[1] == 't'
  999. || input_line_pointer[1] == 'T');
  1000. input_line_pointer += start ? 8 : 7;
  1001. SKIP_WHITESPACE ();
  1002. if (*input_line_pointer != '(')
  1003. as_bad (_("syntax error in .startof. or .sizeof."));
  1004. else
  1005. {
  1006. char *buf;
  1007. ++input_line_pointer;
  1008. SKIP_WHITESPACE ();
  1009. name = input_line_pointer;
  1010. c = get_symbol_end ();
  1011. buf = (char *) xmalloc (strlen (name) + 10);
  1012. if (start)
  1013. sprintf (buf, ".startof.%s", name);
  1014. else
  1015. sprintf (buf, ".sizeof.%s", name);
  1016. symbolP = symbol_make (buf);
  1017. free (buf);
  1018. expressionP->X_op = O_symbol;
  1019. expressionP->X_add_symbol = symbolP;
  1020. expressionP->X_add_number = 0;
  1021. *input_line_pointer = c;
  1022. SKIP_WHITESPACE ();
  1023. if (*input_line_pointer != ')')
  1024. as_bad (_("syntax error in .startof. or .sizeof."));
  1025. else
  1026. ++input_line_pointer;
  1027. }
  1028. break;
  1029. }
  1030. else
  1031. {
  1032. goto isname;
  1033. }
  1034. case ',':
  1035. eol:
  1036. /* Can't imagine any other kind of operand. */
  1037. expressionP->X_op = O_absent;
  1038. input_line_pointer--;
  1039. break;
  1040. #ifdef TC_M68K
  1041. case '%':
  1042. if (! flag_m68k_mri)
  1043. goto de_fault;
  1044. integer_constant (2, expressionP);
  1045. break;
  1046. case '@':
  1047. if (! flag_m68k_mri)
  1048. goto de_fault;
  1049. integer_constant (8, expressionP);
  1050. break;
  1051. case ':':
  1052. if (! flag_m68k_mri)
  1053. goto de_fault;
  1054. /* In MRI mode, this is a floating point constant represented
  1055. using hexadecimal digits. */
  1056. ++input_line_pointer;
  1057. integer_constant (16, expressionP);
  1058. break;
  1059. case '*':
  1060. if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
  1061. goto de_fault;
  1062. current_location (expressionP);
  1063. break;
  1064. #endif
  1065. default:
  1066. #ifdef TC_M68K
  1067. de_fault:
  1068. #endif
  1069. if (is_name_beginner (c)) /* Here if did not begin with a digit. */
  1070. {
  1071. /* Identifier begins here.
  1072. This is kludged for speed, so code is repeated. */
  1073. isname:
  1074. name = --input_line_pointer;
  1075. c = get_symbol_end ();
  1076. #ifdef md_parse_name
  1077. /* This is a hook for the backend to parse certain names
  1078. specially in certain contexts. If a name always has a
  1079. specific value, it can often be handled by simply
  1080. entering it in the symbol table. */
  1081. if (md_parse_name (name, expressionP, mode, &c))
  1082. {
  1083. *input_line_pointer = c;
  1084. break;
  1085. }
  1086. #endif
  1087. #ifdef TC_I960
  1088. /* The MRI i960 assembler permits
  1089. lda sizeof code,g13
  1090. FIXME: This should use md_parse_name. */
  1091. if (flag_mri
  1092. && (strcasecmp (name, "sizeof") == 0
  1093. || strcasecmp (name, "startof") == 0))
  1094. {
  1095. int start;
  1096. char *buf;
  1097. start = (name[1] == 't'
  1098. || name[1] == 'T');
  1099. *input_line_pointer = c;
  1100. SKIP_WHITESPACE ();
  1101. name = input_line_pointer;
  1102. c = get_symbol_end ();
  1103. buf = (char *) xmalloc (strlen (name) + 10);
  1104. if (start)
  1105. sprintf (buf, ".startof.%s", name);
  1106. else
  1107. sprintf (buf, ".sizeof.%s", name);
  1108. symbolP = symbol_make (buf);
  1109. free (buf);
  1110. expressionP->X_op = O_symbol;
  1111. expressionP->X_add_symbol = symbolP;
  1112. expressionP->X_add_number = 0;
  1113. *input_line_pointer = c;
  1114. SKIP_WHITESPACE ();
  1115. break;
  1116. }
  1117. #endif
  1118. symbolP = symbol_find_or_make (name);
  1119. /* If we have an absolute symbol or a reg, then we know its
  1120. value now. */
  1121. segment = S_GET_SEGMENT (symbolP);
  1122. if (mode != expr_defer && segment == absolute_section)
  1123. {
  1124. expressionP->X_op = O_constant;
  1125. expressionP->X_add_number = S_GET_VALUE (symbolP);
  1126. }
  1127. else if (mode != expr_defer && segment == reg_section)
  1128. {
  1129. expressionP->X_op = O_register;
  1130. expressionP->X_add_number = S_GET_VALUE (symbolP);
  1131. }
  1132. else
  1133. {
  1134. expressionP->X_op = O_symbol;
  1135. expressionP->X_add_symbol = symbolP;
  1136. expressionP->X_add_number = 0;
  1137. }
  1138. *input_line_pointer = c;
  1139. }
  1140. else
  1141. {
  1142. /* Let the target try to parse it. Success is indicated by changing
  1143. the X_op field to something other than O_absent and pointing
  1144. input_line_pointer past the expression. If it can't parse the
  1145. expression, X_op and input_line_pointer should be unchanged. */
  1146. expressionP->X_op = O_absent;
  1147. --input_line_pointer;
  1148. md_operand (expressionP);
  1149. if (expressionP->X_op == O_absent)
  1150. {
  1151. ++input_line_pointer;
  1152. as_bad (_("bad expression"));
  1153. expressionP->X_op = O_constant;
  1154. expressionP->X_add_number = 0;
  1155. }
  1156. }
  1157. break;
  1158. }
  1159. /* It is more 'efficient' to clean up the expressionS when they are
  1160. created. Doing it here saves lines of code. */
  1161. clean_up_expression (expressionP);
  1162. SKIP_WHITESPACE (); /* -> 1st char after operand. */
  1163. know (*input_line_pointer != ' ');
  1164. /* The PA port needs this information. */
  1165. if (expressionP->X_add_symbol)
  1166. symbol_mark_used (expressionP->X_add_symbol);
  1167. expressionP->X_add_symbol = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
  1168. expressionP->X_op_symbol = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
  1169. switch (expressionP->X_op)
  1170. {
  1171. default:
  1172. return absolute_section;
  1173. case O_symbol:
  1174. return S_GET_SEGMENT (expressionP->X_add_symbol);
  1175. case O_register:
  1176. return reg_section;
  1177. }
  1178. }
  1179. /* Internal. Simplify a struct expression for use by expr (). */
  1180. /* In: address of an expressionS.
  1181. The X_op field of the expressionS may only take certain values.
  1182. Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
  1183. Out: expressionS may have been modified:
  1184. Unused fields zeroed to help expr (). */
  1185. static void
  1186. clean_up_expression (expressionS *expressionP)
  1187. {
  1188. switch (expressionP->X_op)
  1189. {
  1190. case O_illegal:
  1191. case O_absent:
  1192. expressionP->X_add_number = 0;
  1193. /* Fall through. */
  1194. case O_big:
  1195. case O_constant:
  1196. case O_register:
  1197. expressionP->X_add_symbol = NULL;
  1198. /* Fall through. */
  1199. case O_symbol:
  1200. case O_uminus:
  1201. case O_bit_not:
  1202. expressionP->X_op_symbol = NULL;
  1203. break;
  1204. default:
  1205. break;
  1206. }
  1207. }
  1208. /* Expression parser. */
  1209. /* We allow an empty expression, and just assume (absolute,0) silently.
  1210. Unary operators and parenthetical expressions are treated as operands.
  1211. As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
  1212. We used to do an aho/ullman shift-reduce parser, but the logic got so
  1213. warped that I flushed it and wrote a recursive-descent parser instead.
  1214. Now things are stable, would anybody like to write a fast parser?
  1215. Most expressions are either register (which does not even reach here)
  1216. or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
  1217. So I guess it doesn't really matter how inefficient more complex expressions
  1218. are parsed.
  1219. After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
  1220. Also, we have consumed any leading or trailing spaces (operand does that)
  1221. and done all intervening operators.
  1222. This returns the segment of the result, which will be
  1223. absolute_section or the segment of a symbol. */
  1224. #undef __
  1225. #define __ O_illegal
  1226. #ifndef O_SINGLE_EQ
  1227. #define O_SINGLE_EQ O_illegal
  1228. #endif
  1229. /* Maps ASCII -> operators. */
  1230. static const operatorT op_encoding[256] = {
  1231. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1232. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1233. __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
  1234. __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
  1235. __, __, __, __, __, __, __, __,
  1236. __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
  1237. __, __, __, __, __, __, __, __,
  1238. __, __, __, __, __, __, __, __,
  1239. __, __, __, __, __, __, __, __,
  1240. __, __, __,
  1241. #ifdef NEED_INDEX_OPERATOR
  1242. O_index,
  1243. #else
  1244. __,
  1245. #endif
  1246. __, __, O_bit_exclusive_or, __,
  1247. __, __, __, __, __, __, __, __,
  1248. __, __, __, __, __, __, __, __,
  1249. __, __, __, __, __, __, __, __,
  1250. __, __, __, __, O_bit_inclusive_or, __, __, __,
  1251. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1252. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1253. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1254. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1255. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1256. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1257. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1258. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
  1259. };
  1260. /* Rank Examples
  1261. 0 operand, (expression)
  1262. 1 ||
  1263. 2 &&
  1264. 3 == <> < <= >= >
  1265. 4 + -
  1266. 5 used for * / % in MRI mode
  1267. 6 & ^ ! |
  1268. 7 * / % << >>
  1269. 8 unary - unary ~
  1270. */
  1271. static operator_rankT op_rank[] = {
  1272. 0, /* O_illegal */
  1273. 0, /* O_absent */
  1274. 0, /* O_constant */
  1275. 0, /* O_symbol */
  1276. 0, /* O_symbol_rva */
  1277. 0, /* O_register */
  1278. 0, /* O_big */
  1279. 9, /* O_uminus */
  1280. 9, /* O_bit_not */
  1281. 9, /* O_logical_not */
  1282. 8, /* O_multiply */
  1283. 8, /* O_divide */
  1284. 8, /* O_modulus */
  1285. 8, /* O_left_shift */
  1286. 8, /* O_right_shift */
  1287. 7, /* O_bit_inclusive_or */
  1288. 7, /* O_bit_or_not */
  1289. 7, /* O_bit_exclusive_or */
  1290. 7, /* O_bit_and */
  1291. 5, /* O_add */
  1292. 5, /* O_subtract */
  1293. 4, /* O_eq */
  1294. 4, /* O_ne */
  1295. 4, /* O_lt */
  1296. 4, /* O_le */
  1297. 4, /* O_ge */
  1298. 4, /* O_gt */
  1299. 3, /* O_logical_and */
  1300. 2, /* O_logical_or */
  1301. 1, /* O_index */
  1302. 0, /* O_md1 */
  1303. 0, /* O_md2 */
  1304. 0, /* O_md3 */
  1305. 0, /* O_md4 */
  1306. 0, /* O_md5 */
  1307. 0, /* O_md6 */
  1308. 0, /* O_md7 */
  1309. 0, /* O_md8 */
  1310. 0, /* O_md9 */
  1311. 0, /* O_md10 */
  1312. 0, /* O_md11 */
  1313. 0, /* O_md12 */
  1314. 0, /* O_md13 */
  1315. 0, /* O_md14 */
  1316. 0, /* O_md15 */
  1317. 0, /* O_md16 */
  1318. };
  1319. /* Unfortunately, in MRI mode for the m68k, multiplication and
  1320. division have lower precedence than the bit wise operators. This
  1321. function sets the operator precedences correctly for the current
  1322. mode. Also, MRI uses a different bit_not operator, and this fixes
  1323. that as well. */
  1324. #define STANDARD_MUL_PRECEDENCE 8
  1325. #define MRI_MUL_PRECEDENCE 6
  1326. void
  1327. expr_set_precedence (void)
  1328. {
  1329. if (flag_m68k_mri)
  1330. {
  1331. op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
  1332. op_rank[O_divide] = MRI_MUL_PRECEDENCE;
  1333. op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
  1334. }
  1335. else
  1336. {
  1337. op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
  1338. op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
  1339. op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
  1340. }
  1341. }
  1342. /* Initialize the expression parser. */
  1343. void
  1344. expr_begin (void)
  1345. {
  1346. expr_set_precedence ();
  1347. /* Verify that X_op field is wide enough. */
  1348. {
  1349. expressionS e;
  1350. e.X_op = O_max;
  1351. assert (e.X_op == O_max);
  1352. }
  1353. }
  1354. /* Return the encoding for the operator at INPUT_LINE_POINTER, and
  1355. sets NUM_CHARS to the number of characters in the operator.
  1356. Does not advance INPUT_LINE_POINTER. */
  1357. static inline operatorT
  1358. operator (int *num_chars)
  1359. {
  1360. int c;
  1361. operatorT ret;
  1362. c = *input_line_pointer & 0xff;
  1363. *num_chars = 1;
  1364. if (is_end_of_line[c])
  1365. return O_illegal;
  1366. switch (c)
  1367. {
  1368. default:
  1369. return op_encoding[c];
  1370. case '+':
  1371. case '-':
  1372. return op_encoding[c];
  1373. case '<':
  1374. switch (input_line_pointer[1])
  1375. {
  1376. default:
  1377. return op_encoding[c];
  1378. case '<':
  1379. ret = O_left_shift;
  1380. break;
  1381. case '>':
  1382. ret = O_ne;
  1383. break;
  1384. case '=':
  1385. ret = O_le;
  1386. break;
  1387. }
  1388. *num_chars = 2;
  1389. return ret;
  1390. case '=':
  1391. if (input_line_pointer[1] != '=')
  1392. return op_encoding[c];
  1393. *num_chars = 2;
  1394. return O_eq;
  1395. case '>':
  1396. switch (input_line_pointer[1])
  1397. {
  1398. default:
  1399. return op_encoding[c];
  1400. case '>':
  1401. ret = O_right_shift;
  1402. break;
  1403. case '=':
  1404. ret = O_ge;
  1405. break;
  1406. }
  1407. *num_chars = 2;
  1408. return ret;
  1409. case '!':
  1410. switch (input_line_pointer[1])
  1411. {
  1412. case '!':
  1413. /* We accept !! as equivalent to ^ for MRI compatibility. */
  1414. *num_chars = 2;
  1415. return O_bit_exclusive_or;
  1416. case '=':
  1417. /* We accept != as equivalent to <>. */
  1418. *num_chars = 2;
  1419. return O_ne;
  1420. default:
  1421. if (flag_m68k_mri)
  1422. return O_bit_inclusive_or;
  1423. return op_encoding[c];
  1424. }
  1425. case '|':
  1426. if (input_line_pointer[1] != '|')
  1427. return op_encoding[c];
  1428. *num_chars = 2;
  1429. return O_logical_or;
  1430. case '&':
  1431. if (input_line_pointer[1] != '&')
  1432. return op_encoding[c];
  1433. *num_chars = 2;
  1434. return O_logical_and;
  1435. }
  1436. /* NOTREACHED */
  1437. }
  1438. /* Parse an expression. */
  1439. segT
  1440. expr (int rankarg, /* Larger # is higher rank. */
  1441. expressionS *resultP, /* Deliver result here. */
  1442. enum expr_mode mode /* Controls behavior. */)
  1443. {
  1444. operator_rankT rank = (operator_rankT) rankarg;
  1445. segT retval;
  1446. expressionS right;
  1447. operatorT op_left;
  1448. operatorT op_right;
  1449. int op_chars;
  1450. know (rankarg >= 0);
  1451. /* Save the value of dot for the fixup code. */
  1452. if (rank == 0)
  1453. dot_value = frag_now_fix ();
  1454. retval = operand (resultP, mode);
  1455. /* operand () gobbles spaces. */
  1456. know (*input_line_pointer != ' ');
  1457. op_left = operator (&op_chars);
  1458. while (op_left != O_illegal && op_rank[(int) op_left] > rank)
  1459. {
  1460. segT rightseg;
  1461. bfd_vma frag_off;
  1462. input_line_pointer += op_chars; /* -> after operator. */
  1463. rightseg = expr (op_rank[(int) op_left], &right, mode);
  1464. if (right.X_op == O_absent)
  1465. {
  1466. as_warn (_("missing operand; zero assumed"));
  1467. right.X_op = O_constant;
  1468. right.X_add_number = 0;
  1469. right.X_add_symbol = NULL;
  1470. right.X_op_symbol = NULL;
  1471. }
  1472. know (*input_line_pointer != ' ');
  1473. if (op_left == O_index)
  1474. {
  1475. if (*input_line_pointer != ']')
  1476. as_bad ("missing right bracket");
  1477. else
  1478. {
  1479. ++input_line_pointer;
  1480. SKIP_WHITESPACE ();
  1481. }
  1482. }
  1483. op_right = operator (&op_chars);
  1484. know (op_right == O_illegal
  1485. || op_rank[(int) op_right] <= op_rank[(int) op_left]);
  1486. know ((int) op_left >= (int) O_multiply
  1487. && (int) op_left <= (int) O_index);
  1488. /* input_line_pointer->after right-hand quantity. */
  1489. /* left-hand quantity in resultP. */
  1490. /* right-hand quantity in right. */
  1491. /* operator in op_left. */
  1492. if (resultP->X_op == O_big)
  1493. {
  1494. if (resultP->X_add_number > 0)
  1495. as_warn (_("left operand is a bignum; integer 0 assumed"));
  1496. else
  1497. as_warn (_("left operand is a float; integer 0 assumed"));
  1498. resultP->X_op = O_constant;
  1499. resultP->X_add_number = 0;
  1500. resultP->X_add_symbol = NULL;
  1501. resultP->X_op_symbol = NULL;
  1502. }
  1503. if (right.X_op == O_big)
  1504. {
  1505. if (right.X_add_number > 0)
  1506. as_warn (_("right operand is a bignum; integer 0 assumed"));
  1507. else
  1508. as_warn (_("right operand is a float; integer 0 assumed"));
  1509. right.X_op = O_constant;
  1510. right.X_add_number = 0;
  1511. right.X_add_symbol = NULL;
  1512. right.X_op_symbol = NULL;
  1513. }
  1514. /* Optimize common cases. */
  1515. #ifdef md_optimize_expr
  1516. if (md_optimize_expr (resultP, op_left, &right))
  1517. {
  1518. /* Skip. */
  1519. ;
  1520. }
  1521. else
  1522. #endif
  1523. if (op_left == O_add && right.X_op == O_constant)
  1524. {
  1525. /* X + constant. */
  1526. resultP->X_add_number += right.X_add_number;
  1527. }
  1528. /* This case comes up in PIC code. */
  1529. else if (op_left == O_subtract
  1530. && right.X_op == O_symbol
  1531. && resultP->X_op == O_symbol
  1532. && retval == rightseg
  1533. && (SEG_NORMAL (rightseg)
  1534. || right.X_add_symbol == resultP->X_add_symbol)
  1535. && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
  1536. symbol_get_frag (right.X_add_symbol),
  1537. &frag_off))
  1538. {
  1539. resultP->X_add_number -= right.X_add_number;
  1540. resultP->X_add_number -= frag_off / OCTETS_PER_BYTE;
  1541. resultP->X_add_number += (S_GET_VALUE (resultP->X_add_symbol)
  1542. - S_GET_VALUE (right.X_add_symbol));
  1543. resultP->X_op = O_constant;
  1544. resultP->X_add_symbol = 0;
  1545. }
  1546. else if (op_left == O_subtract && right.X_op == O_constant)
  1547. {
  1548. /* X - constant. */
  1549. resultP->X_add_number -= right.X_add_number;
  1550. }
  1551. else if (op_left == O_add && resultP->X_op == O_constant)
  1552. {
  1553. /* Constant + X. */
  1554. resultP->X_op = right.X_op;
  1555. resultP->X_add_symbol = right.X_add_symbol;
  1556. resultP->X_op_symbol = right.X_op_symbol;
  1557. resultP->X_add_number += right.X_add_number;
  1558. retval = rightseg;
  1559. }
  1560. else if (resultP->X_op == O_constant && right.X_op == O_constant)
  1561. {
  1562. /* Constant OP constant. */
  1563. offsetT v = right.X_add_number;
  1564. if (v == 0 && (op_left == O_divide || op_left == O_modulus))
  1565. {
  1566. as_warn (_("division by zero"));
  1567. v = 1;
  1568. }
  1569. switch (op_left)
  1570. {
  1571. default: abort ();
  1572. case O_multiply: resultP->X_add_number *= v; break;
  1573. case O_divide: resultP->X_add_number /= v; break;
  1574. case O_modulus: resultP->X_add_number %= v; break;
  1575. case O_left_shift: resultP->X_add_number <<= v; break;
  1576. case O_right_shift:
  1577. /* We always use unsigned shifts, to avoid relying on
  1578. characteristics of the compiler used to compile gas. */
  1579. resultP->X_add_number =
  1580. (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
  1581. break;
  1582. case O_bit_inclusive_or: resultP->X_add_number |= v; break;
  1583. case O_bit_or_not: resultP->X_add_number |= ~v; break;
  1584. case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
  1585. case O_bit_and: resultP->X_add_number &= v; break;
  1586. /* Constant + constant (O_add) is handled by the
  1587. previous if statement for constant + X, so is omitted
  1588. here. */
  1589. case O_subtract: resultP->X_add_number -= v; break;
  1590. case O_eq:
  1591. resultP->X_add_number =
  1592. resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
  1593. break;
  1594. case O_ne:
  1595. resultP->X_add_number =
  1596. resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
  1597. break;
  1598. case O_lt:
  1599. resultP->X_add_number =
  1600. resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
  1601. break;
  1602. case O_le:
  1603. resultP->X_add_number =
  1604. resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
  1605. break;
  1606. case O_ge:
  1607. resultP->X_add_number =
  1608. resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
  1609. break;
  1610. case O_gt:
  1611. resultP->X_add_number =
  1612. resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
  1613. break;
  1614. case O_logical_and:
  1615. resultP->X_add_number = resultP->X_add_number && v;
  1616. break;
  1617. case O_logical_or:
  1618. resultP->X_add_number = resultP->X_add_number || v;
  1619. break;
  1620. }
  1621. }
  1622. else if (resultP->X_op == O_symbol
  1623. && right.X_op == O_symbol
  1624. && (op_left == O_add
  1625. || op_left == O_subtract
  1626. || (resultP->X_add_number == 0
  1627. && right.X_add_number == 0)))
  1628. {
  1629. /* Symbol OP symbol. */
  1630. resultP->X_op = op_left;
  1631. resultP->X_op_symbol = right.X_add_symbol;
  1632. if (op_left == O_add)
  1633. resultP->X_add_number += right.X_add_number;
  1634. else if (op_left == O_subtract)
  1635. {
  1636. resultP->X_add_number -= right.X_add_number;
  1637. if (retval == rightseg && SEG_NORMAL (retval))
  1638. {
  1639. retval = absolute_section;
  1640. rightseg = absolute_section;
  1641. }
  1642. }
  1643. }
  1644. else
  1645. {
  1646. /* The general case. */
  1647. resultP->X_add_symbol = make_expr_symbol (resultP);
  1648. resultP->X_op_symbol = make_expr_symbol (&right);
  1649. resultP->X_op = op_left;
  1650. resultP->X_add_number = 0;
  1651. resultP->X_unsigned = 1;
  1652. }
  1653. if (retval != rightseg)
  1654. {
  1655. if (! SEG_NORMAL (retval))
  1656. {
  1657. if (retval != undefined_section || SEG_NORMAL (rightseg))
  1658. retval = rightseg;
  1659. }
  1660. else if (SEG_NORMAL (rightseg)
  1661. #ifdef DIFF_EXPR_OK
  1662. && op_left != O_subtract
  1663. #endif
  1664. )
  1665. as_bad (_("operation combines symbols in different segments"));
  1666. }
  1667. op_left = op_right;
  1668. } /* While next operator is >= this rank. */
  1669. /* The PA port needs this information.…