expr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /* expr.c -operands, expressions-
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. * This is really a branch office of as-read.c. I split it out to clearly
  17. * distinguish the world of expressions from the world of statements.
  18. * (It also gives smaller files to re-compile.)
  19. * Here, "operand"s are of expressions, not instructions.
  20. */
  21. #include <ctype.h>
  22. #include "as.h"
  23. #include "flonum.h"
  24. #include "read.h"
  25. #include "struc-symbol.h"
  26. #include "expr.h"
  27. #include "obstack.h"
  28. #include "symbols.h"
  29. static void clean_up_expression(); /* Internal. */
  30. extern char EXP_CHARS[]; /* JF hide MD floating pt stuff all the same place */
  31. extern char FLT_CHARS[];
  32. #ifdef SUN_ASM_SYNTAX
  33. extern int local_label_defined[];
  34. #endif
  35. /*
  36. * Build any floating-point literal here.
  37. * Also build any bignum literal here.
  38. */
  39. LITTLENUM_TYPE generic_buffer [6]; /* JF this is a hack */
  40. /* Seems atof_machine can backscan through generic_bignum and hit whatever
  41. happens to be loaded before it in memory. And its way too complicated
  42. for me to fix right. Thus a hack
  43. */
  44. LITTLENUM_TYPE generic_bignum [SIZE_OF_LARGE_NUMBER];
  45. FLONUM_TYPE generic_floating_point_number =
  46. {
  47. & generic_bignum [0], /* low */
  48. & generic_bignum [SIZE_OF_LARGE_NUMBER - 1], /* high */
  49. 0, /* leader */
  50. 0, /* exponent */
  51. 0 /* sign */
  52. };
  53. /* If nonzero, we've been asked to assemble nan, +inf or -inf */
  54. int generic_floating_point_magic;
  55. /*
  56. * Summary of operand().
  57. *
  58. * in: Input_line_pointer points to 1st char of operand, which may
  59. * be a space.
  60. *
  61. * out: A expressionS. X_seg determines how to understand the rest of the
  62. * expressionS.
  63. * The operand may have been empty: in this case X_seg == SEG_NONE.
  64. * Input_line_pointer -> (next non-blank) char after operand.
  65. *
  66. */
  67. static segT
  68. operand (expressionP)
  69. register expressionS * expressionP;
  70. {
  71. register char c;
  72. register char *name; /* points to name of symbol */
  73. register struct symbol * symbolP; /* Points to symbol */
  74. extern char hex_value[]; /* In hex_value.c */
  75. char *local_label_name();
  76. SKIP_WHITESPACE(); /* Leading whitespace is part of operand. */
  77. c = * input_line_pointer ++; /* Input_line_pointer -> past char in c. */
  78. if (isdigit(c))
  79. {
  80. register valueT number; /* offset or (absolute) value */
  81. register short int digit; /* value of next digit in current radix */
  82. /* invented for humans only, hope */
  83. /* optimising compiler flushes it! */
  84. register short int radix; /* 8, 10 or 16 */
  85. /* 0 means we saw start of a floating- */
  86. /* point constant. */
  87. register short int maxdig;/* Highest permitted digit value. */
  88. register int too_many_digits; /* If we see >= this number of */
  89. /* digits, assume it is a bignum. */
  90. register char * digit_2; /* -> 2nd digit of number. */
  91. int small; /* TRUE if fits in 32 bits. */
  92. if (c=='0')
  93. { /* non-decimal radix */
  94. if ((c = * input_line_pointer ++)=='x' || c=='X')
  95. {
  96. c = * input_line_pointer ++; /* read past "0x" or "0X" */
  97. maxdig = radix = 16;
  98. too_many_digits = 9;
  99. }
  100. else
  101. {
  102. /* If it says '0f' and the line ends or it DOESN'T look like
  103. a floating point #, its a local label ref. DTRT */
  104. if(c=='f' && (! *input_line_pointer ||
  105. (!index("+-.0123456789",*input_line_pointer) &&
  106. !index(EXP_CHARS,*input_line_pointer))))
  107. {
  108. maxdig = radix = 10;
  109. too_many_digits = 11;
  110. c='0';
  111. input_line_pointer-=2;
  112. }
  113. else if (c && index (FLT_CHARS,c))
  114. {
  115. radix = 0; /* Start of floating-point constant. */
  116. /* input_line_pointer -> 1st char of number. */
  117. expressionP -> X_add_number = - (isupper(c) ? tolower(c) : c);
  118. }
  119. else
  120. { /* By elimination, assume octal radix. */
  121. radix = 8;
  122. maxdig = 10; /* Un*x sux. Compatibility. */
  123. too_many_digits = 11;
  124. }
  125. }
  126. /* c == char after "0" or "0x" or "0X" or "0e" etc.*/
  127. }
  128. else
  129. {
  130. maxdig = radix = 10;
  131. too_many_digits = 11;
  132. }
  133. if (radix)
  134. { /* Fixed-point integer constant. */
  135. /* May be bignum, or may fit in 32 bits. */
  136. /*
  137. * Most numbers fit into 32 bits, and we want this case to be fast.
  138. * So we pretend it will fit into 32 bits. If, after making up a 32
  139. * bit number, we realise that we have scanned more digits than
  140. * comfortably fit into 32 bits, we re-scan the digits coding
  141. * them into a bignum. For decimal and octal numbers we are conservative: some
  142. * numbers may be assumed bignums when in fact they do fit into 32 bits.
  143. * Numbers of any radix can have excess leading zeros: we strive
  144. * to recognise this and cast them back into 32 bits.
  145. * We must check that the bignum really is more than 32
  146. * bits, and change it back to a 32-bit number if it fits.
  147. * The number we are looking for is expected to be positive, but
  148. * if it fits into 32 bits as an unsigned number, we let it be a 32-bit
  149. * number. The cavalier approach is for speed in ordinary cases.
  150. */
  151. digit_2 = input_line_pointer;
  152. for (number=0; (digit=hex_value[c])<maxdig; c = * input_line_pointer ++)
  153. {
  154. number = number * radix + digit;
  155. }
  156. /* C contains character after number. */
  157. /* Input_line_pointer -> char after C. */
  158. small = input_line_pointer - digit_2 < too_many_digits;
  159. if ( ! small)
  160. {
  161. /*
  162. * We saw a lot of digits. Manufacture a bignum the hard way.
  163. */
  164. LITTLENUM_TYPE * leader; /* -> high order littlenum of the bignum. */
  165. LITTLENUM_TYPE * pointer; /* -> littlenum we are frobbing now. */
  166. long int carry;
  167. leader = generic_bignum;
  168. generic_bignum [0] = 0;
  169. /* We could just use digit_2, but lets be mnemonic. */
  170. input_line_pointer = -- digit_2; /* -> 1st digit. */
  171. c = *input_line_pointer ++;
  172. for (; (carry = hex_value [c]) < maxdig; c = * input_line_pointer ++)
  173. {
  174. for (pointer = generic_bignum;
  175. pointer <= leader;
  176. pointer ++)
  177. {
  178. long int work;
  179. work = carry + radix * * pointer;
  180. * pointer = work & LITTLENUM_MASK;
  181. carry = work >> LITTLENUM_NUMBER_OF_BITS;
  182. }
  183. if (carry)
  184. {
  185. if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
  186. { /* Room to grow a longer bignum. */
  187. * ++ leader = carry;
  188. }
  189. }
  190. }
  191. /* Again, C is char after number, */
  192. /* input_line_pointer -> after C. */
  193. know( BITS_PER_INT == 32 );
  194. know( LITTLENUM_NUMBER_OF_BITS == 16 );
  195. /* Hence the constant "2" in the next line. */
  196. if (leader < generic_bignum + 2)
  197. { /* Will fit into 32 bits. */
  198. number =
  199. ( (generic_bignum [1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS )
  200. | (generic_bignum [0] & LITTLENUM_MASK);
  201. small = TRUE;
  202. }
  203. else
  204. {
  205. number = leader - generic_bignum + 1; /* Number of littlenums in the bignum. */
  206. }
  207. }
  208. if (small)
  209. {
  210. /*
  211. * Here with number, in correct radix. c is the next char.
  212. * Note that unlike Un*x, we allow "011f" "0x9f" to
  213. * both mean the same as the (conventional) "9f". This is simply easier
  214. * than checking for strict canonical form. Syntax sux!
  215. */
  216. if (number<10)
  217. {
  218. #ifdef SUN_ASM_SYNTAX
  219. if (c=='b' || (c=='$' && local_label_defined[number])
  220. #else
  221. if (c=='b')
  222. #endif
  223. {
  224. /*
  225. * Backward ref to local label.
  226. * Because it is backward, expect it to be DEFINED.
  227. */
  228. /*
  229. * Construct a local label.
  230. */
  231. name = local_label_name ((int)number, 0);
  232. if ( (symbolP = symbol_table_lookup(name)) /* seen before */
  233. && (symbolP -> sy_type & N_TYPE) != N_UNDF /* symbol is defined: OK */
  234. )
  235. { /* Expected path: symbol defined. */
  236. /* Local labels are never absolute. Don't waste time checking absoluteness. */
  237. know( (symbolP -> sy_type & N_TYPE) == N_DATA
  238. || (symbolP -> sy_type & N_TYPE) == N_TEXT );
  239. expressionP -> X_add_symbol = symbolP;
  240. expressionP -> X_add_number = 0;
  241. expressionP -> X_seg = N_TYPE_seg [symbolP -> sy_type];
  242. }
  243. else
  244. { /* Either not seen or not defined. */
  245. as_warn( "Backw. ref to unknown label \"%d:\", 0 assumed.",
  246. number
  247. );
  248. expressionP -> X_add_number = 0;
  249. expressionP -> X_seg = SEG_ABSOLUTE;
  250. }
  251. }
  252. else
  253. {
  254. #ifdef SUN_ASM_SYNTAX
  255. if (c=='f' || (c=='$' && !local_label_defined[number]))
  256. #else
  257. if (c=='f')
  258. #endif
  259. {
  260. /*
  261. * Forward reference. Expect symbol to be undefined or
  262. * unknown. Undefined: seen it before. Unknown: never seen
  263. * it in this pass.
  264. * Construct a local label name, then an undefined symbol.
  265. * Don't create a XSEG frag for it: caller may do that.
  266. * Just return it as never seen before.
  267. */
  268. name = local_label_name ((int)number, 1);
  269. if ( symbolP = symbol_table_lookup( name ))
  270. {
  271. /* We have no need to check symbol properties. */
  272. know( (symbolP -> sy_type & N_TYPE) == N_UNDF
  273. || (symbolP -> sy_type & N_TYPE) == N_DATA
  274. || (symbolP -> sy_type & N_TYPE) == N_TEXT);
  275. }
  276. else
  277. {
  278. symbolP = symbol_new (name, N_UNDF, 0,0,0, & zero_address_frag);
  279. symbol_table_insert (symbolP);
  280. }
  281. expressionP -> X_add_symbol = symbolP;
  282. expressionP -> X_seg = SEG_UNKNOWN;
  283. expressionP -> X_subtract_symbol = NULL;
  284. expressionP -> X_add_number = 0;
  285. }
  286. else
  287. { /* Really a number, not a local label. */
  288. expressionP -> X_add_number = number;
  289. expressionP -> X_seg = SEG_ABSOLUTE;
  290. input_line_pointer --; /* Restore following character. */
  291. } /* if (c=='f') */
  292. } /* if (c=='b') */
  293. }
  294. else
  295. { /* Really a number. */
  296. expressionP -> X_add_number = number;
  297. expressionP -> X_seg = SEG_ABSOLUTE;
  298. input_line_pointer --; /* Restore following character. */
  299. } /* if (number<10) */
  300. }
  301. else
  302. {
  303. expressionP -> X_add_number = number;
  304. expressionP -> X_seg = SEG_BIG;
  305. input_line_pointer --; /* -> char following number. */
  306. } /* if (small) */
  307. } /* (If integer constant) */
  308. else
  309. { /* input_line_pointer -> */
  310. /* floating-point constant. */
  311. int error_code;
  312. error_code = atof_generic
  313. (& input_line_pointer, ".", EXP_CHARS,
  314. & generic_floating_point_number);
  315. if (error_code)
  316. {
  317. if (error_code == ERROR_EXPONENT_OVERFLOW)
  318. {
  319. as_warn( "Bad floating-point constant: exponent overflow, probably assembling junk" );
  320. }
  321. else
  322. {
  323. as_warn( "Bad floating-point constant: unknown error code=%d.", error_code);
  324. }
  325. }
  326. expressionP -> X_seg = SEG_BIG;
  327. /* input_line_pointer -> just after constant, */
  328. /* which may point to whitespace. */
  329. know( expressionP -> X_add_number < 0 ); /* < 0 means "floating point". */
  330. } /* if (not floating-point constant) */
  331. }
  332. else if(c=='.' && !is_part_of_name(*input_line_pointer)) {
  333. extern struct obstack frags;
  334. /*
  335. JF: '.' is pseudo symbol with value of current location in current
  336. segment. . .
  337. */
  338. symbolP = symbol_new("L0\001",
  339. (unsigned char)(seg_N_TYPE[(int)now_seg]),
  340. 0,
  341. 0,
  342. (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
  343. frag_now);
  344. expressionP->X_add_number=0;
  345. expressionP->X_add_symbol=symbolP;
  346. expressionP->X_seg = now_seg;
  347. } else if ( is_name_beginner(c) ) /* here if did not begin with a digit */
  348. {
  349. /*
  350. * Identifier begins here.
  351. * This is kludged for speed, so code is repeated.
  352. */
  353. name = -- input_line_pointer;
  354. c = get_symbol_end();
  355. symbolP = symbol_table_lookup(name);
  356. if (symbolP)
  357. {
  358. /*
  359. * If we have an absolute symbol, then we know it's value now.
  360. */
  361. register segT seg;
  362. seg = N_TYPE_seg [(int) symbolP -> sy_type & N_TYPE];
  363. if ((expressionP -> X_seg = seg) == SEG_ABSOLUTE )
  364. {
  365. expressionP -> X_add_number = symbolP -> sy_value;
  366. }
  367. else
  368. {
  369. expressionP -> X_add_number = 0;
  370. expressionP -> X_add_symbol = symbolP;
  371. }
  372. }
  373. else
  374. {
  375. expressionP -> X_add_symbol
  376. = symbolP
  377. = symbol_new (name, N_UNDF, 0,0,0, & zero_address_frag);
  378. expressionP -> X_add_number = 0;
  379. expressionP -> X_seg = SEG_UNKNOWN;
  380. symbol_table_insert (symbolP);
  381. }
  382. * input_line_pointer = c;
  383. expressionP -> X_subtract_symbol = NULL;
  384. }
  385. else if (c=='(')/* didn't begin with digit & not a name */
  386. {
  387. (void)expression( expressionP );
  388. /* Expression() will pass trailing whitespace */
  389. if ( * input_line_pointer ++ != ')' )
  390. {
  391. as_warn( "Missing ')' assumed");
  392. input_line_pointer --;
  393. }
  394. /* here with input_line_pointer -> char after "(...)" */
  395. }
  396. else if ( c=='~' || c=='-' )
  397. { /* unary operator: hope for SEG_ABSOLUTE */
  398. switch(operand (expressionP)) {
  399. case SEG_ABSOLUTE:
  400. /* input_line_pointer -> char after operand */
  401. if ( c=='-' )
  402. {
  403. expressionP -> X_add_number = - expressionP -> X_add_number;
  404. /*
  405. * Notice: '-' may overflow: no warning is given. This is compatible
  406. * with other people's assemblers. Sigh.
  407. */
  408. }
  409. else
  410. {
  411. expressionP -> X_add_number = ~ expressionP -> X_add_number;
  412. }
  413. break;
  414. case SEG_TEXT:
  415. case SEG_DATA:
  416. case SEG_BSS:
  417. case SEG_PASS1:
  418. case SEG_UNKNOWN:
  419. if(c=='-') { /* JF I hope this hack works */
  420. expressionP->X_subtract_symbol=expressionP->X_add_symbol;
  421. expressionP->X_add_symbol=0;
  422. expressionP->X_seg=SEG_DIFFERENCE;
  423. break;
  424. }
  425. default: /* unary on non-absolute is unsuported */
  426. as_warn("Unary operator %c ignored because bad operand follows", c);
  427. break;
  428. /* Expression undisturbed from operand(). */
  429. }
  430. }
  431. else if (c=='\'')
  432. {
  433. /*
  434. * Warning: to conform to other people's assemblers NO ESCAPEMENT is permitted
  435. * for a single quote. The next character, parity errors and all, is taken
  436. * as the value of the operand. VERY KINKY.
  437. */
  438. expressionP -> X_add_number = * input_line_pointer ++;
  439. expressionP -> X_seg = SEG_ABSOLUTE;
  440. }
  441. else
  442. {
  443. /* can't imagine any other kind of operand */
  444. expressionP -> X_seg = SEG_NONE;
  445. input_line_pointer --;
  446. }
  447. /*
  448. * It is more 'efficient' to clean up the expressions when they are created.
  449. * Doing it here saves lines of code.
  450. */
  451. clean_up_expression (expressionP);
  452. SKIP_WHITESPACE(); /* -> 1st char after operand. */
  453. know( * input_line_pointer != ' ' );
  454. return (expressionP -> X_seg);
  455. } /* operand */
  456. /* Internal. Simplify a struct expression for use by expr() */
  457. /*
  458. * In: address of a expressionS.
  459. * The X_seg field of the expressionS may only take certain values.
  460. * Now, we permit SEG_PASS1 to make code smaller & faster.
  461. * Elsewise we waste time special-case testing. Sigh. Ditto SEG_NONE.
  462. * Out: expressionS may have been modified:
  463. * 'foo-foo' symbol references cancelled to 0,
  464. * which changes X_seg from SEG_DIFFERENCE to SEG_ABSOLUTE;
  465. * Unused fields zeroed to help expr().
  466. */
  467. static void
  468. clean_up_expression (expressionP)
  469. register expressionS * expressionP;
  470. {
  471. switch (expressionP -> X_seg)
  472. {
  473. case SEG_NONE:
  474. case SEG_PASS1:
  475. expressionP -> X_add_symbol = NULL;
  476. expressionP -> X_subtract_symbol = NULL;
  477. expressionP -> X_add_number = 0;
  478. break;
  479. case SEG_BIG:
  480. case SEG_ABSOLUTE:
  481. expressionP -> X_subtract_symbol = NULL;
  482. expressionP -> X_add_symbol = NULL;
  483. break;
  484. case SEG_TEXT:
  485. case SEG_DATA:
  486. case SEG_BSS:
  487. case SEG_UNKNOWN:
  488. expressionP -> X_subtract_symbol = NULL;
  489. break;
  490. case SEG_DIFFERENCE:
  491. /*
  492. * It does not hurt to 'cancel' NULL==NULL
  493. * when comparing symbols for 'eq'ness.
  494. * It is faster to re-cancel them to NULL
  495. * than to check for this special case.
  496. */
  497. if (expressionP -> X_subtract_symbol == expressionP -> X_add_symbol)
  498. {
  499. expressionP -> X_subtract_symbol = NULL;
  500. expressionP -> X_add_symbol = NULL;
  501. expressionP -> X_seg = SEG_ABSOLUTE;
  502. }
  503. break;
  504. default:
  505. BAD_CASE( expressionP -> X_seg);
  506. break;
  507. }
  508. }
  509. /*
  510. * expr_part ()
  511. *
  512. * Internal. Made a function because this code is used in 2 places.
  513. * Generate error or correct X_?????_symbol of expressionS.
  514. */
  515. /*
  516. * symbol_1 += symbol_2 ... well ... sort of.
  517. */
  518. static segT
  519. expr_part (symbol_1_PP, symbol_2_P)
  520. struct symbol ** symbol_1_PP;
  521. struct symbol * symbol_2_P;
  522. {
  523. segT return_value;
  524. know( (* symbol_1_PP) == NULL
  525. || ((* symbol_1_PP) -> sy_type & N_TYPE) == N_TEXT
  526. || ((* symbol_1_PP) -> sy_type & N_TYPE) == N_DATA
  527. || ((* symbol_1_PP) -> sy_type & N_TYPE) == N_BSS
  528. || ((* symbol_1_PP) -> sy_type & N_TYPE) == N_UNDF
  529. );
  530. know( symbol_2_P == NULL
  531. || (symbol_2_P -> sy_type & N_TYPE) == N_TEXT
  532. || (symbol_2_P -> sy_type & N_TYPE) == N_DATA
  533. || (symbol_2_P -> sy_type & N_TYPE) == N_BSS
  534. || (symbol_2_P -> sy_type & N_TYPE) == N_UNDF
  535. );
  536. if (* symbol_1_PP)
  537. {
  538. if (((* symbol_1_PP) -> sy_type & N_TYPE) == N_UNDF)
  539. {
  540. if (symbol_2_P)
  541. {
  542. return_value = SEG_PASS1;
  543. * symbol_1_PP = NULL;
  544. }
  545. else
  546. {
  547. know( ((* symbol_1_PP) -> sy_type & N_TYPE) == N_UNDF)
  548. return_value = SEG_UNKNOWN;
  549. }
  550. }
  551. else
  552. {
  553. if (symbol_2_P)
  554. {
  555. if ((symbol_2_P -> sy_type & N_TYPE) == N_UNDF)
  556. {
  557. * symbol_1_PP = NULL;
  558. return_value = SEG_PASS1;
  559. }
  560. else
  561. {
  562. /* {seg1} - {seg2} */
  563. as_warn( "Expression too complex, 2 symbols forgotten: \"%s\" \"%s\"",
  564. (* symbol_1_PP) -> sy_name, symbol_2_P -> sy_name );
  565. * symbol_1_PP = NULL;
  566. return_value = SEG_ABSOLUTE;
  567. }
  568. }
  569. else
  570. {
  571. return_value = N_TYPE_seg [(* symbol_1_PP) -> sy_type & N_TYPE];
  572. }
  573. }
  574. }
  575. else
  576. { /* (* symbol_1_PP) == NULL */
  577. if (symbol_2_P)
  578. {
  579. * symbol_1_PP = symbol_2_P;
  580. return_value = N_TYPE_seg [(symbol_2_P) -> sy_type & N_TYPE];
  581. }
  582. else
  583. {
  584. * symbol_1_PP = NULL;
  585. return_value = SEG_ABSOLUTE;
  586. }
  587. }
  588. know( return_value == SEG_ABSOLUTE
  589. || return_value == SEG_TEXT
  590. || return_value == SEG_DATA
  591. || return_value == SEG_BSS
  592. || return_value == SEG_UNKNOWN
  593. || return_value == SEG_PASS1
  594. );
  595. know( (* symbol_1_PP) == NULL
  596. || ((* symbol_1_PP) -> sy_type & N_TYPE) == seg_N_TYPE [(int) return_value] );
  597. return (return_value);
  598. } /* expr_part() */
  599. /* Expression parser. */
  600. /*
  601. * We allow an empty expression, and just assume (absolute,0) silently.
  602. * Unary operators and parenthetical expressions are treated as operands.
  603. * As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
  604. *
  605. * We used to do a aho/ullman shift-reduce parser, but the logic got so
  606. * warped that I flushed it and wrote a recursive-descent parser instead.
  607. * Now things are stable, would anybody like to write a fast parser?
  608. * Most expressions are either register (which does not even reach here)
  609. * or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
  610. * So I guess it doesn't really matter how inefficient more complex expressions
  611. * are parsed.
  612. *
  613. * After expr(RANK,resultP) input_line_pointer -> operator of rank <= RANK.
  614. * Also, we have consumed any leading or trailing spaces (operand does that)
  615. * and done all intervening operators.
  616. */
  617. typedef enum
  618. {
  619. O_illegal, /* (0) what we get for illegal op */
  620. O_multiply, /* (1) * */
  621. O_divide, /* (2) / */
  622. O_modulus, /* (3) % */
  623. O_left_shift, /* (4) < */
  624. O_right_shift, /* (5) > */
  625. O_bit_inclusive_or, /* (6) | */
  626. O_bit_or_not, /* (7) ! */
  627. O_bit_exclusive_or, /* (8) ^ */
  628. O_bit_and, /* (9) & */
  629. O_add, /* (10) + */
  630. O_subtract /* (11) - */
  631. }
  632. operatorT;
  633. #define __ O_illegal
  634. static operatorT op_encoding [256] = { /* maps ASCII -> operators */
  635. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  636. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  637. __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
  638. __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
  639. __, __, __, __, __, __, __, __,
  640. __, __, __, __, O_left_shift, __, O_right_shift, __,
  641. __, __, __, __, __, __, __, __,
  642. __, __, __, __, __, __, __, __,
  643. __, __, __, __, __, __, __, __,
  644. __, __, __, __, __, __, O_bit_exclusive_or, __,
  645. __, __, __, __, __, __, __, __,
  646. __, __, __, __, __, __, __, __,
  647. __, __, __, __, __, __, __, __,
  648. __, __, __, __, O_bit_inclusive_or, __, __, __,
  649. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  650. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  651. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  652. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  653. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  654. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  655. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  656. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
  657. };
  658. /*
  659. * Rank Examples
  660. * 0 operand, (expression)
  661. * 1 + -
  662. * 2 & ^ ! |
  663. * 3 * / % < >
  664. */
  665. typedef char operator_rankT;
  666. static operator_rankT
  667. op_rank [] = { 0, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1 };
  668. segT /* Return resultP -> X_seg. */
  669. expr (rank, resultP)
  670. register operator_rankT rank; /* Larger # is higher rank. */
  671. register expressionS * resultP; /* Deliver result here. */
  672. {
  673. expressionS right;
  674. register operatorT op_left;
  675. register char c_left; /* 1st operator character. */
  676. register operatorT op_right;
  677. register char c_right;
  678. know( rank >= 0 );
  679. (void)operand (resultP);
  680. know( * input_line_pointer != ' ' ); /* Operand() gobbles spaces. */
  681. c_left = * input_line_pointer; /* Potential operator character. */
  682. op_left = op_encoding [c_left];
  683. while (op_left != O_illegal && op_rank [(int) op_left] > rank)
  684. {
  685. input_line_pointer ++; /* -> after 1st character of operator. */
  686. /* Operators "<<" and ">>" have 2 characters. */
  687. if (* input_line_pointer == c_left && (c_left == '<' || c_left == '>') )
  688. {
  689. input_line_pointer ++;
  690. } /* -> after operator. */
  691. if (SEG_NONE == expr (op_rank[(int) op_left], &right))
  692. {
  693. as_warn("Missing operand value assumed absolute 0.");
  694. resultP -> X_add_number = 0;
  695. resultP -> X_subtract_symbol = NULL;
  696. resultP -> X_add_symbol = NULL;
  697. resultP -> X_seg = SEG_ABSOLUTE;
  698. }
  699. know( * input_line_pointer != ' ' );
  700. c_right = * input_line_pointer;
  701. op_right = op_encoding [c_right];
  702. if (* input_line_pointer == c_right && (c_right == '<' || c_right == '>') )
  703. {
  704. input_line_pointer ++;
  705. } /* -> after operator. */
  706. know( (int) op_right == 0
  707. || op_rank [(int) op_right] <= op_rank[(int) op_left] );
  708. /* input_line_pointer -> after right-hand quantity. */
  709. /* left-hand quantity in resultP */
  710. /* right-hand quantity in right. */
  711. /* operator in op_left. */
  712. if ( resultP -> X_seg == SEG_PASS1 || right . X_seg == SEG_PASS1 )
  713. {
  714. resultP -> X_seg = SEG_PASS1;
  715. }
  716. else
  717. {
  718. if ( resultP -> X_seg == SEG_BIG )
  719. {
  720. as_warn( "Left operand of %c is a %s. Integer 0 assumed.",
  721. c_left, resultP -> X_add_number > 0 ? "bignum" : "float");
  722. resultP -> X_seg = SEG_ABSOLUTE;
  723. resultP -> X_add_symbol = 0;
  724. resultP -> X_subtract_symbol = 0;
  725. resultP -> X_add_number = 0;
  726. }
  727. if ( right . X_seg == SEG_BIG )
  728. {
  729. as_warn( "Right operand of %c is a %s. Integer 0 assumed.",
  730. c_left, right . X_add_number > 0 ? "bignum" : "float");
  731. right . X_seg = SEG_ABSOLUTE;
  732. right . X_add_symbol = 0;
  733. right . X_subtract_symbol = 0;
  734. right . X_add_number = 0;
  735. }
  736. if ( op_left == O_subtract )
  737. {
  738. /*
  739. * Convert - into + by exchanging symbols and negating number.
  740. * I know -infinity can't be negated in 2's complement:
  741. * but then it can't be subtracted either. This trick
  742. * does not cause any further inaccuracy.
  743. */
  744. register struct symbol * symbolP;
  745. right . X_add_number = - right . X_add_number;
  746. symbolP = right . X_add_symbol;
  747. right . X_add_symbol = right . X_subtract_symbol;
  748. right . X_subtract_symbol = symbolP;
  749. if (symbolP)
  750. {
  751. right . X_seg = SEG_DIFFERENCE;
  752. }
  753. op_left = O_add;
  754. }
  755. if ( op_left == O_add )
  756. {
  757. segT seg1;
  758. segT seg2;
  759. know( resultP -> X_seg == SEG_DATA
  760. || resultP -> X_seg == SEG_TEXT
  761. || resultP -> X_seg == SEG_BSS
  762. || resultP -> X_seg == SEG_UNKNOWN
  763. || resultP -> X_seg == SEG_DIFFERENCE
  764. || resultP -> X_seg == SEG_ABSOLUTE
  765. || resultP -> X_seg == SEG_PASS1
  766. );
  767. know( right . X_seg == SEG_DATA
  768. || right . X_seg == SEG_TEXT
  769. || right . X_seg == SEG_BSS
  770. || right . X_seg == SEG_UNKNOWN
  771. || right . X_seg == SEG_DIFFERENCE
  772. || right . X_seg == SEG_ABSOLUTE
  773. || right . X_seg == SEG_PASS1
  774. );
  775. clean_up_expression (& right);
  776. clean_up_expression (resultP);
  777. seg1 = expr_part (& resultP -> X_add_symbol, right . X_add_symbol);
  778. seg2 = expr_part (& resultP -> X_subtract_symbol, right . X_subtract_symbol);
  779. if ( seg1 == SEG_PASS1
  780. || seg2 == SEG_PASS1)
  781. {
  782. need_pass_2 = TRUE;
  783. resultP -> X_seg = SEG_PASS1;
  784. }
  785. else
  786. {
  787. if (seg2 == SEG_ABSOLUTE)
  788. {
  789. resultP -> X_seg = seg1;
  790. }
  791. else
  792. {
  793. know( seg2 != SEG_ABSOLUTE );
  794. know( resultP -> X_subtract_symbol );
  795. if ( seg1 != SEG_UNKNOWN
  796. && seg1 != SEG_ABSOLUTE
  797. && seg2 != SEG_UNKNOWN
  798. && seg1 != seg2)
  799. {
  800. know( seg1 == SEG_TEXT || seg1 == SEG_DATA || seg1== SEG_BSS );
  801. know( seg2 == SEG_TEXT || seg2 == SEG_DATA || seg2== SEG_BSS );
  802. know( resultP -> X_add_symbol );
  803. know( resultP -> X_subtract_symbol );
  804. as_warn("Expression too complex: forgetting %s - %s",
  805. resultP -> X_add_symbol -> sy_name,
  806. resultP -> X_subtract_symbol -> sy_name);
  807. resultP -> X_seg = SEG_ABSOLUTE;
  808. /* Clean_up_expression() will do the rest. */
  809. }
  810. else
  811. {
  812. resultP -> X_seg = SEG_DIFFERENCE;
  813. } /* If relocation too complex. */
  814. } /* If seg2 == SEG_ABSOLUTE. */
  815. } /* If need pass 2. */
  816. resultP -> X_add_number += right . X_add_number;
  817. clean_up_expression (resultP);
  818. }
  819. else
  820. { /* Not +. */
  821. if ( resultP -> X_seg == SEG_UNKNOWN || right . X_seg == SEG_UNKNOWN )
  822. {
  823. resultP -> X_seg = SEG_PASS1;
  824. need_pass_2 = TRUE;
  825. }
  826. else
  827. {
  828. resultP -> X_subtract_symbol = NULL;
  829. resultP -> X_add_symbol = NULL;
  830. /* Will be SEG_ABSOLUTE. */
  831. if ( resultP -> X_seg != SEG_ABSOLUTE || right . X_seg != SEG_ABSOLUTE )
  832. {
  833. as_warn( "Relocation error. Absolute 0 assumed.");
  834. resultP -> X_seg = SEG_ABSOLUTE;
  835. resultP -> X_add_number = 0;
  836. }
  837. else
  838. {
  839. switch ( op_left )
  840. {
  841. case O_bit_inclusive_or:
  842. resultP -> X_add_number |= right . X_add_number;
  843. break;
  844. case O_modulus:
  845. if (right . X_add_number)
  846. {
  847. resultP -> X_add_number %= right . X_add_number;
  848. }
  849. else
  850. {
  851. as_warn( "Division by 0. 0 assumed." );
  852. resultP -> X_add_number = 0;
  853. }
  854. break;
  855. case O_bit_and:
  856. resultP -> X_add_number &= right . X_add_number;
  857. break;
  858. case O_multiply:
  859. resultP -> X_add_number *= right . X_add_number;
  860. break;
  861. case O_divide:
  862. if (right . X_add_number)
  863. {
  864. resultP -> X_add_number /= right . X_add_number;
  865. }
  866. else
  867. {
  868. as_warn( "Division by 0. 0 assumed." );
  869. resultP -> X_add_number = 0;
  870. }
  871. break;
  872. case O_left_shift:
  873. resultP -> X_add_number <<= right . X_add_number;
  874. break;
  875. case O_right_shift:
  876. resultP -> X_add_number >>= right . X_add_number;
  877. break;
  878. case O_bit_exclusive_or:
  879. resultP -> X_add_number ^= right . X_add_number;
  880. break;
  881. case O_bit_or_not:
  882. resultP -> X_add_number |= ~ right . X_add_number;
  883. break;
  884. default:
  885. BAD_CASE( op_left );
  886. break;
  887. } /* switch(operator) */
  888. }
  889. } /* If we have to force need_pass_2. */
  890. } /* If operator was +. */
  891. } /* If we didn't set need_pass_2. */
  892. op_left = op_right;
  893. } /* While next operator is >= this rank. */
  894. return (resultP -> X_seg);
  895. }
  896. /*
  897. * get_symbol_end()
  898. *
  899. * This lives here because it belongs equally in expr.c & read.c.
  900. * Expr.c is just a branch office read.c anyway, and putting it
  901. * here lessens the crowd at read.c.
  902. *
  903. * Assume input_line_pointer is at start of symbol name.
  904. * Advance input_line_pointer past symbol name.
  905. * Turn that character into a '\0', returning its former value.
  906. * This allows a string compare (RMS wants symbol names to be strings)
  907. * of the symbol name.
  908. * There will always be a char following symbol name, because all good
  909. * lines end in end-of-line.
  910. */
  911. char
  912. get_symbol_end()
  913. {
  914. register char c;
  915. while ( is_part_of_name( c = * input_line_pointer ++ ) )
  916. ;
  917. * -- input_line_pointer = 0;
  918. return (c);
  919. }
  920. /* end: expr.c */