expr.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385
  1. /* expr.c -operands, expressions-
  2. Copyright (C) 1987-2015 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 3, 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 the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  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. #define min(a, b) ((a) < (b) ? (a) : (b))
  21. #include "as.h"
  22. #include "safe-ctype.h"
  23. #ifdef HAVE_LIMITS_H
  24. #include <limits.h>
  25. #endif
  26. #ifndef CHAR_BIT
  27. #define CHAR_BIT 8
  28. #endif
  29. static void floating_constant (expressionS * expressionP);
  30. static valueT generic_bignum_to_int32 (void);
  31. #ifdef BFD64
  32. static valueT generic_bignum_to_int64 (void);
  33. #endif
  34. static void integer_constant (int radix, expressionS * expressionP);
  35. static void mri_char_constant (expressionS *);
  36. static void clean_up_expression (expressionS * expressionP);
  37. static segT operand (expressionS *, enum expr_mode);
  38. static operatorT operatorf (int *);
  39. extern const char EXP_CHARS[], FLT_CHARS[];
  40. /* We keep a mapping of expression symbols to file positions, so that
  41. we can provide better error messages. */
  42. struct expr_symbol_line {
  43. struct expr_symbol_line *next;
  44. symbolS *sym;
  45. char *file;
  46. unsigned int line;
  47. };
  48. static struct expr_symbol_line *expr_symbol_lines;
  49. /* Build a dummy symbol to hold a complex expression. This is how we
  50. build expressions up out of other expressions. The symbol is put
  51. into the fake section expr_section. */
  52. symbolS *
  53. make_expr_symbol (expressionS *expressionP)
  54. {
  55. expressionS zero;
  56. symbolS *symbolP;
  57. struct expr_symbol_line *n;
  58. if (expressionP->X_op == O_symbol
  59. && expressionP->X_add_number == 0)
  60. return expressionP->X_add_symbol;
  61. if (expressionP->X_op == O_big)
  62. {
  63. /* This won't work, because the actual value is stored in
  64. generic_floating_point_number or generic_bignum, and we are
  65. going to lose it if we haven't already. */
  66. if (expressionP->X_add_number > 0)
  67. as_bad (_("bignum invalid"));
  68. else
  69. as_bad (_("floating point number invalid"));
  70. zero.X_op = O_constant;
  71. zero.X_add_number = 0;
  72. zero.X_unsigned = 0;
  73. zero.X_extrabit = 0;
  74. clean_up_expression (&zero);
  75. expressionP = &zero;
  76. }
  77. /* Putting constant symbols in absolute_section rather than
  78. expr_section is convenient for the old a.out code, for which
  79. S_GET_SEGMENT does not always retrieve the value put in by
  80. S_SET_SEGMENT. */
  81. symbolP = symbol_create (FAKE_LABEL_NAME,
  82. (expressionP->X_op == O_constant
  83. ? absolute_section
  84. : expressionP->X_op == O_register
  85. ? reg_section
  86. : expr_section),
  87. 0, &zero_address_frag);
  88. symbol_set_value_expression (symbolP, expressionP);
  89. if (expressionP->X_op == O_constant)
  90. resolve_symbol_value (symbolP);
  91. n = (struct expr_symbol_line *) xmalloc (sizeof *n);
  92. n->sym = symbolP;
  93. as_where (&n->file, &n->line);
  94. n->next = expr_symbol_lines;
  95. expr_symbol_lines = n;
  96. return symbolP;
  97. }
  98. /* Return the file and line number for an expr symbol. Return
  99. non-zero if something was found, 0 if no information is known for
  100. the symbol. */
  101. int
  102. expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
  103. {
  104. struct expr_symbol_line *l;
  105. for (l = expr_symbol_lines; l != NULL; l = l->next)
  106. {
  107. if (l->sym == sym)
  108. {
  109. *pfile = l->file;
  110. *pline = l->line;
  111. return 1;
  112. }
  113. }
  114. return 0;
  115. }
  116. /* Utilities for building expressions.
  117. Since complex expressions are recorded as symbols for use in other
  118. expressions these return a symbolS * and not an expressionS *.
  119. These explicitly do not take an "add_number" argument. */
  120. /* ??? For completeness' sake one might want expr_build_symbol.
  121. It would just return its argument. */
  122. /* Build an expression for an unsigned constant.
  123. The corresponding one for signed constants is missing because
  124. there's currently no need for it. One could add an unsigned_p flag
  125. but that seems more clumsy. */
  126. symbolS *
  127. expr_build_uconstant (offsetT value)
  128. {
  129. expressionS e;
  130. e.X_op = O_constant;
  131. e.X_add_number = value;
  132. e.X_unsigned = 1;
  133. e.X_extrabit = 0;
  134. return make_expr_symbol (&e);
  135. }
  136. /* Build an expression for the current location ('.'). */
  137. symbolS *
  138. expr_build_dot (void)
  139. {
  140. expressionS e;
  141. current_location (&e);
  142. return symbol_clone_if_forward_ref (make_expr_symbol (&e));
  143. }
  144. /* Build any floating-point literal here.
  145. Also build any bignum literal here. */
  146. /* Seems atof_machine can backscan through generic_bignum and hit whatever
  147. happens to be loaded before it in memory. And its way too complicated
  148. for me to fix right. Thus a hack. JF: Just make generic_bignum bigger,
  149. and never write into the early words, thus they'll always be zero.
  150. I hate Dean's floating-point code. Bleh. */
  151. LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
  152. FLONUM_TYPE generic_floating_point_number = {
  153. &generic_bignum[6], /* low. (JF: Was 0) */
  154. &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high. JF: (added +6) */
  155. 0, /* leader. */
  156. 0, /* exponent. */
  157. 0 /* sign. */
  158. };
  159. static void
  160. floating_constant (expressionS *expressionP)
  161. {
  162. /* input_line_pointer -> floating-point constant. */
  163. int error_code;
  164. error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
  165. &generic_floating_point_number);
  166. if (error_code)
  167. {
  168. if (error_code == ERROR_EXPONENT_OVERFLOW)
  169. {
  170. as_bad (_("bad floating-point constant: exponent overflow"));
  171. }
  172. else
  173. {
  174. as_bad (_("bad floating-point constant: unknown error code=%d"),
  175. error_code);
  176. }
  177. }
  178. expressionP->X_op = O_big;
  179. /* input_line_pointer -> just after constant, which may point to
  180. whitespace. */
  181. expressionP->X_add_number = -1;
  182. }
  183. static valueT
  184. generic_bignum_to_int32 (void)
  185. {
  186. valueT number =
  187. ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
  188. | (generic_bignum[0] & LITTLENUM_MASK);
  189. number &= 0xffffffff;
  190. return number;
  191. }
  192. #ifdef BFD64
  193. static valueT
  194. generic_bignum_to_int64 (void)
  195. {
  196. valueT number =
  197. ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
  198. << LITTLENUM_NUMBER_OF_BITS)
  199. | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
  200. << LITTLENUM_NUMBER_OF_BITS)
  201. | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
  202. << LITTLENUM_NUMBER_OF_BITS)
  203. | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
  204. return number;
  205. }
  206. #endif
  207. static void
  208. integer_constant (int radix, expressionS *expressionP)
  209. {
  210. char *start; /* Start of number. */
  211. char *suffix = NULL;
  212. char c;
  213. valueT number; /* Offset or (absolute) value. */
  214. short int digit; /* Value of next digit in current radix. */
  215. short int maxdig = 0; /* Highest permitted digit value. */
  216. int too_many_digits = 0; /* If we see >= this number of. */
  217. char *name; /* Points to name of symbol. */
  218. symbolS *symbolP; /* Points to symbol. */
  219. int small; /* True if fits in 32 bits. */
  220. /* May be bignum, or may fit in 32 bits. */
  221. /* Most numbers fit into 32 bits, and we want this case to be fast.
  222. so we pretend it will fit into 32 bits. If, after making up a 32
  223. bit number, we realise that we have scanned more digits than
  224. comfortably fit into 32 bits, we re-scan the digits coding them
  225. into a bignum. For decimal and octal numbers we are
  226. conservative: Some numbers may be assumed bignums when in fact
  227. they do fit into 32 bits. Numbers of any radix can have excess
  228. leading zeros: We strive to recognise this and cast them back
  229. into 32 bits. We must check that the bignum really is more than
  230. 32 bits, and change it back to a 32-bit number if it fits. The
  231. number we are looking for is expected to be positive, but if it
  232. fits into 32 bits as an unsigned number, we let it be a 32-bit
  233. number. The cavalier approach is for speed in ordinary cases. */
  234. /* This has been extended for 64 bits. We blindly assume that if
  235. you're compiling in 64-bit mode, the target is a 64-bit machine.
  236. This should be cleaned up. */
  237. #ifdef BFD64
  238. #define valuesize 64
  239. #else /* includes non-bfd case, mostly */
  240. #define valuesize 32
  241. #endif
  242. if (is_end_of_line[(unsigned char) *input_line_pointer])
  243. {
  244. expressionP->X_op = O_absent;
  245. return;
  246. }
  247. if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
  248. {
  249. int flt = 0;
  250. /* In MRI mode, the number may have a suffix indicating the
  251. radix. For that matter, it might actually be a floating
  252. point constant. */
  253. for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
  254. {
  255. if (*suffix == 'e' || *suffix == 'E')
  256. flt = 1;
  257. }
  258. if (suffix == input_line_pointer)
  259. {
  260. radix = 10;
  261. suffix = NULL;
  262. }
  263. else
  264. {
  265. c = *--suffix;
  266. c = TOUPPER (c);
  267. /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
  268. we distinguish between 'B' and 'b'. This is the case for
  269. Z80. */
  270. if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
  271. radix = 2;
  272. else if (c == 'D')
  273. radix = 10;
  274. else if (c == 'O' || c == 'Q')
  275. radix = 8;
  276. else if (c == 'H')
  277. radix = 16;
  278. else if (suffix[1] == '.' || c == 'E' || flt)
  279. {
  280. floating_constant (expressionP);
  281. return;
  282. }
  283. else
  284. {
  285. radix = 10;
  286. suffix = NULL;
  287. }
  288. }
  289. }
  290. switch (radix)
  291. {
  292. case 2:
  293. maxdig = 2;
  294. too_many_digits = valuesize + 1;
  295. break;
  296. case 8:
  297. maxdig = radix = 8;
  298. too_many_digits = (valuesize + 2) / 3 + 1;
  299. break;
  300. case 16:
  301. maxdig = radix = 16;
  302. too_many_digits = (valuesize + 3) / 4 + 1;
  303. break;
  304. case 10:
  305. maxdig = radix = 10;
  306. too_many_digits = (valuesize + 11) / 4; /* Very rough. */
  307. }
  308. #undef valuesize
  309. start = input_line_pointer;
  310. c = *input_line_pointer++;
  311. for (number = 0;
  312. (digit = hex_value (c)) < maxdig;
  313. c = *input_line_pointer++)
  314. {
  315. number = number * radix + digit;
  316. }
  317. /* c contains character after number. */
  318. /* input_line_pointer->char after c. */
  319. small = (input_line_pointer - start - 1) < too_many_digits;
  320. if (radix == 16 && c == '_')
  321. {
  322. /* This is literal of the form 0x333_0_12345678_1.
  323. This example is equivalent to 0x00000333000000001234567800000001. */
  324. int num_little_digits = 0;
  325. int i;
  326. input_line_pointer = start; /* -> 1st digit. */
  327. know (LITTLENUM_NUMBER_OF_BITS == 16);
  328. for (c = '_'; c == '_'; num_little_digits += 2)
  329. {
  330. /* Convert one 64-bit word. */
  331. int ndigit = 0;
  332. number = 0;
  333. for (c = *input_line_pointer++;
  334. (digit = hex_value (c)) < maxdig;
  335. c = *(input_line_pointer++))
  336. {
  337. number = number * radix + digit;
  338. ndigit++;
  339. }
  340. /* Check for 8 digit per word max. */
  341. if (ndigit > 8)
  342. as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
  343. /* Add this chunk to the bignum.
  344. Shift things down 2 little digits. */
  345. know (LITTLENUM_NUMBER_OF_BITS == 16);
  346. for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
  347. i >= 2;
  348. i--)
  349. generic_bignum[i] = generic_bignum[i - 2];
  350. /* Add the new digits as the least significant new ones. */
  351. generic_bignum[0] = number & 0xffffffff;
  352. generic_bignum[1] = number >> 16;
  353. }
  354. /* Again, c is char after number, input_line_pointer->after c. */
  355. if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
  356. num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
  357. gas_assert (num_little_digits >= 4);
  358. if (num_little_digits != 8)
  359. as_bad (_("a bignum with underscores must have exactly 4 words"));
  360. /* We might have some leading zeros. These can be trimmed to give
  361. us a change to fit this constant into a small number. */
  362. while (generic_bignum[num_little_digits - 1] == 0
  363. && num_little_digits > 1)
  364. num_little_digits--;
  365. if (num_little_digits <= 2)
  366. {
  367. /* will fit into 32 bits. */
  368. number = generic_bignum_to_int32 ();
  369. small = 1;
  370. }
  371. #ifdef BFD64
  372. else if (num_little_digits <= 4)
  373. {
  374. /* Will fit into 64 bits. */
  375. number = generic_bignum_to_int64 ();
  376. small = 1;
  377. }
  378. #endif
  379. else
  380. {
  381. small = 0;
  382. /* Number of littlenums in the bignum. */
  383. number = num_little_digits;
  384. }
  385. }
  386. else if (!small)
  387. {
  388. /* We saw a lot of digits. manufacture a bignum the hard way. */
  389. LITTLENUM_TYPE *leader; /* -> high order littlenum of the bignum. */
  390. LITTLENUM_TYPE *pointer; /* -> littlenum we are frobbing now. */
  391. long carry;
  392. leader = generic_bignum;
  393. generic_bignum[0] = 0;
  394. generic_bignum[1] = 0;
  395. generic_bignum[2] = 0;
  396. generic_bignum[3] = 0;
  397. input_line_pointer = start; /* -> 1st digit. */
  398. c = *input_line_pointer++;
  399. for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
  400. {
  401. for (pointer = generic_bignum; pointer <= leader; pointer++)
  402. {
  403. long work;
  404. work = carry + radix * *pointer;
  405. *pointer = work & LITTLENUM_MASK;
  406. carry = work >> LITTLENUM_NUMBER_OF_BITS;
  407. }
  408. if (carry)
  409. {
  410. if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
  411. {
  412. /* Room to grow a longer bignum. */
  413. *++leader = carry;
  414. }
  415. }
  416. }
  417. /* Again, c is char after number. */
  418. /* input_line_pointer -> after c. */
  419. know (LITTLENUM_NUMBER_OF_BITS == 16);
  420. if (leader < generic_bignum + 2)
  421. {
  422. /* Will fit into 32 bits. */
  423. number = generic_bignum_to_int32 ();
  424. small = 1;
  425. }
  426. #ifdef BFD64
  427. else if (leader < generic_bignum + 4)
  428. {
  429. /* Will fit into 64 bits. */
  430. number = generic_bignum_to_int64 ();
  431. small = 1;
  432. }
  433. #endif
  434. else
  435. {
  436. /* Number of littlenums in the bignum. */
  437. number = leader - generic_bignum + 1;
  438. }
  439. }
  440. if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  441. && suffix != NULL
  442. && input_line_pointer - 1 == suffix)
  443. c = *input_line_pointer++;
  444. if (small)
  445. {
  446. /* Here with number, in correct radix. c is the next char.
  447. Note that unlike un*x, we allow "011f" "0x9f" to both mean
  448. the same as the (conventional) "9f".
  449. This is simply easier than checking for strict canonical
  450. form. Syntax sux! */
  451. if (LOCAL_LABELS_FB && c == 'b')
  452. {
  453. /* Backward ref to local label.
  454. Because it is backward, expect it to be defined. */
  455. /* Construct a local label. */
  456. name = fb_label_name ((int) number, 0);
  457. /* Seen before, or symbol is defined: OK. */
  458. symbolP = symbol_find (name);
  459. if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
  460. {
  461. /* Local labels are never absolute. Don't waste time
  462. checking absoluteness. */
  463. know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
  464. expressionP->X_op = O_symbol;
  465. expressionP->X_add_symbol = symbolP;
  466. }
  467. else
  468. {
  469. /* Either not seen or not defined. */
  470. /* @@ Should print out the original string instead of
  471. the parsed number. */
  472. as_bad (_("backward ref to unknown label \"%d:\""),
  473. (int) number);
  474. expressionP->X_op = O_constant;
  475. }
  476. expressionP->X_add_number = 0;
  477. } /* case 'b' */
  478. else if (LOCAL_LABELS_FB && c == 'f')
  479. {
  480. /* Forward reference. Expect symbol to be undefined or
  481. unknown. undefined: seen it before. unknown: never seen
  482. it before.
  483. Construct a local label name, then an undefined symbol.
  484. Don't create a xseg frag for it: caller may do that.
  485. Just return it as never seen before. */
  486. name = fb_label_name ((int) number, 1);
  487. symbolP = symbol_find_or_make (name);
  488. /* We have no need to check symbol properties. */
  489. #ifndef many_segments
  490. /* Since "know" puts its arg into a "string", we
  491. can't have newlines in the argument. */
  492. know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
  493. #endif
  494. expressionP->X_op = O_symbol;
  495. expressionP->X_add_symbol = symbolP;
  496. expressionP->X_add_number = 0;
  497. } /* case 'f' */
  498. else if (LOCAL_LABELS_DOLLAR && c == '$')
  499. {
  500. /* If the dollar label is *currently* defined, then this is just
  501. another reference to it. If it is not *currently* defined,
  502. then this is a fresh instantiation of that number, so create
  503. it. */
  504. if (dollar_label_defined ((long) number))
  505. {
  506. name = dollar_label_name ((long) number, 0);
  507. symbolP = symbol_find (name);
  508. know (symbolP != NULL);
  509. }
  510. else
  511. {
  512. name = dollar_label_name ((long) number, 1);
  513. symbolP = symbol_find_or_make (name);
  514. }
  515. expressionP->X_op = O_symbol;
  516. expressionP->X_add_symbol = symbolP;
  517. expressionP->X_add_number = 0;
  518. } /* case '$' */
  519. else
  520. {
  521. expressionP->X_op = O_constant;
  522. expressionP->X_add_number = number;
  523. input_line_pointer--; /* Restore following character. */
  524. } /* Really just a number. */
  525. }
  526. else
  527. {
  528. /* Not a small number. */
  529. expressionP->X_op = O_big;
  530. expressionP->X_add_number = number; /* Number of littlenums. */
  531. input_line_pointer--; /* -> char following number. */
  532. }
  533. }
  534. /* Parse an MRI multi character constant. */
  535. static void
  536. mri_char_constant (expressionS *expressionP)
  537. {
  538. int i;
  539. if (*input_line_pointer == '\''
  540. && input_line_pointer[1] != '\'')
  541. {
  542. expressionP->X_op = O_constant;
  543. expressionP->X_add_number = 0;
  544. return;
  545. }
  546. /* In order to get the correct byte ordering, we must build the
  547. number in reverse. */
  548. for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
  549. {
  550. int j;
  551. generic_bignum[i] = 0;
  552. for (j = 0; j < CHARS_PER_LITTLENUM; j++)
  553. {
  554. if (*input_line_pointer == '\'')
  555. {
  556. if (input_line_pointer[1] != '\'')
  557. break;
  558. ++input_line_pointer;
  559. }
  560. generic_bignum[i] <<= 8;
  561. generic_bignum[i] += *input_line_pointer;
  562. ++input_line_pointer;
  563. }
  564. if (i < SIZE_OF_LARGE_NUMBER - 1)
  565. {
  566. /* If there is more than one littlenum, left justify the
  567. last one to make it match the earlier ones. If there is
  568. only one, we can just use the value directly. */
  569. for (; j < CHARS_PER_LITTLENUM; j++)
  570. generic_bignum[i] <<= 8;
  571. }
  572. if (*input_line_pointer == '\''
  573. && input_line_pointer[1] != '\'')
  574. break;
  575. }
  576. if (i < 0)
  577. {
  578. as_bad (_("character constant too large"));
  579. i = 0;
  580. }
  581. if (i > 0)
  582. {
  583. int c;
  584. int j;
  585. c = SIZE_OF_LARGE_NUMBER - i;
  586. for (j = 0; j < c; j++)
  587. generic_bignum[j] = generic_bignum[i + j];
  588. i = c;
  589. }
  590. know (LITTLENUM_NUMBER_OF_BITS == 16);
  591. if (i > 2)
  592. {
  593. expressionP->X_op = O_big;
  594. expressionP->X_add_number = i;
  595. }
  596. else
  597. {
  598. expressionP->X_op = O_constant;
  599. if (i < 2)
  600. expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
  601. else
  602. expressionP->X_add_number =
  603. (((generic_bignum[1] & LITTLENUM_MASK)
  604. << LITTLENUM_NUMBER_OF_BITS)
  605. | (generic_bignum[0] & LITTLENUM_MASK));
  606. }
  607. /* Skip the final closing quote. */
  608. ++input_line_pointer;
  609. }
  610. /* Return an expression representing the current location. This
  611. handles the magic symbol `.'. */
  612. void
  613. current_location (expressionS *expressionp)
  614. {
  615. if (now_seg == absolute_section)
  616. {
  617. expressionp->X_op = O_constant;
  618. expressionp->X_add_number = abs_section_offset;
  619. }
  620. else
  621. {
  622. expressionp->X_op = O_symbol;
  623. expressionp->X_add_symbol = &dot_symbol;
  624. expressionp->X_add_number = 0;
  625. }
  626. }
  627. /* In: Input_line_pointer points to 1st char of operand, which may
  628. be a space.
  629. Out: An expressionS.
  630. The operand may have been empty: in this case X_op == O_absent.
  631. Input_line_pointer->(next non-blank) char after operand. */
  632. static segT
  633. operand (expressionS *expressionP, enum expr_mode mode)
  634. {
  635. char c;
  636. symbolS *symbolP; /* Points to symbol. */
  637. char *name; /* Points to name of symbol. */
  638. segT segment;
  639. /* All integers are regarded as unsigned unless they are negated.
  640. This is because the only thing which cares whether a number is
  641. unsigned is the code in emit_expr which extends constants into
  642. bignums. It should only sign extend negative numbers, so that
  643. something like ``.quad 0x80000000'' is not sign extended even
  644. though it appears negative if valueT is 32 bits. */
  645. expressionP->X_unsigned = 1;
  646. expressionP->X_extrabit = 0;
  647. /* Digits, assume it is a bignum. */
  648. SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
  649. c = *input_line_pointer++; /* input_line_pointer -> past char in c. */
  650. if (is_end_of_line[(unsigned char) c])
  651. goto eol;
  652. switch (c)
  653. {
  654. case '1':
  655. case '2':
  656. case '3':
  657. case '4':
  658. case '5':
  659. case '6':
  660. case '7':
  661. case '8':
  662. case '9':
  663. input_line_pointer--;
  664. integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  665. ? 0 : 10,
  666. expressionP);
  667. break;
  668. #ifdef LITERAL_PREFIXDOLLAR_HEX
  669. case '$':
  670. /* $L is the start of a local label, not a hex constant. */
  671. if (* input_line_pointer == 'L')
  672. goto isname;
  673. integer_constant (16, expressionP);
  674. break;
  675. #endif
  676. #ifdef LITERAL_PREFIXPERCENT_BIN
  677. case '%':
  678. integer_constant (2, expressionP);
  679. break;
  680. #endif
  681. case '0':
  682. /* Non-decimal radix. */
  683. if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  684. {
  685. char *s;
  686. /* Check for a hex or float constant. */
  687. for (s = input_line_pointer; hex_p (*s); s++)
  688. ;
  689. if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
  690. {
  691. --input_line_pointer;
  692. integer_constant (0, expressionP);
  693. break;
  694. }
  695. }
  696. c = *input_line_pointer;
  697. switch (c)
  698. {
  699. case 'o':
  700. case 'O':
  701. case 'q':
  702. case 'Q':
  703. case '8':
  704. case '9':
  705. if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
  706. {
  707. integer_constant (0, expressionP);
  708. break;
  709. }
  710. /* Fall through. */
  711. default:
  712. default_case:
  713. if (c && strchr (FLT_CHARS, c))
  714. {
  715. input_line_pointer++;
  716. floating_constant (expressionP);
  717. expressionP->X_add_number = - TOLOWER (c);
  718. }
  719. else
  720. {
  721. /* The string was only zero. */
  722. expressionP->X_op = O_constant;
  723. expressionP->X_add_number = 0;
  724. }
  725. break;
  726. case 'x':
  727. case 'X':
  728. if (flag_m68k_mri)
  729. goto default_case;
  730. input_line_pointer++;
  731. integer_constant (16, expressionP);
  732. break;
  733. case 'b':
  734. if (LOCAL_LABELS_FB && !flag_m68k_mri
  735. && input_line_pointer[1] != '0'
  736. && input_line_pointer[1] != '1')
  737. {
  738. /* Parse this as a back reference to label 0. */
  739. input_line_pointer--;
  740. integer_constant (10, expressionP);
  741. break;
  742. }
  743. /* Otherwise, parse this as a binary number. */
  744. /* Fall through. */
  745. case 'B':
  746. if (input_line_pointer[1] == '0'
  747. || input_line_pointer[1] == '1')
  748. {
  749. input_line_pointer++;
  750. integer_constant (2, expressionP);
  751. break;
  752. }
  753. if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
  754. input_line_pointer++;
  755. goto default_case;
  756. case '0':
  757. case '1':
  758. case '2':
  759. case '3':
  760. case '4':
  761. case '5':
  762. case '6':
  763. case '7':
  764. integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
  765. ? 0 : 8,
  766. expressionP);
  767. break;
  768. case 'f':
  769. if (LOCAL_LABELS_FB)
  770. {
  771. int is_label = 1;
  772. /* If it says "0f" and it could possibly be a floating point
  773. number, make it one. Otherwise, make it a local label,
  774. and try to deal with parsing the rest later. */
  775. if (!is_end_of_line[(unsigned char) input_line_pointer[1]]
  776. && strchr (FLT_CHARS, 'f') != NULL)
  777. {
  778. char *cp = input_line_pointer + 1;
  779. atof_generic (&cp, ".", EXP_CHARS,
  780. &generic_floating_point_number);
  781. /* Was nothing parsed, or does it look like an
  782. expression? */
  783. is_label = (cp == input_line_pointer + 1
  784. || (cp == input_line_pointer + 2
  785. && (cp[-1] == '-' || cp[-1] == '+'))
  786. || *cp == 'f'
  787. || *cp == 'b');
  788. }
  789. if (is_label)
  790. {
  791. input_line_pointer--;
  792. integer_constant (10, expressionP);
  793. break;
  794. }
  795. }
  796. /* Fall through. */
  797. case 'd':
  798. case 'D':
  799. if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
  800. {
  801. integer_constant (0, expressionP);
  802. break;
  803. }
  804. /* Fall through. */
  805. case 'F':
  806. case 'r':
  807. case 'e':
  808. case 'E':
  809. case 'g':
  810. case 'G':
  811. input_line_pointer++;
  812. floating_constant (expressionP);
  813. expressionP->X_add_number = - TOLOWER (c);
  814. break;
  815. case '$':
  816. if (LOCAL_LABELS_DOLLAR)
  817. {
  818. integer_constant (10, expressionP);
  819. break;
  820. }
  821. else
  822. goto default_case;
  823. }
  824. break;
  825. #ifndef NEED_INDEX_OPERATOR
  826. case '[':
  827. # ifdef md_need_index_operator
  828. if (md_need_index_operator())
  829. goto de_fault;
  830. # endif
  831. /* FALLTHROUGH */
  832. #endif
  833. case '(':
  834. /* Didn't begin with digit & not a name. */
  835. segment = expr (0, expressionP, mode);
  836. /* expression () will pass trailing whitespace. */
  837. if ((c == '(' && *input_line_pointer != ')')
  838. || (c == '[' && *input_line_pointer != ']'))
  839. as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
  840. else
  841. input_line_pointer++;
  842. SKIP_WHITESPACE ();
  843. /* Here with input_line_pointer -> char after "(...)". */
  844. return segment;
  845. #ifdef TC_M68K
  846. case 'E':
  847. if (! flag_m68k_mri || *input_line_pointer != '\'')
  848. goto de_fault;
  849. as_bad (_("EBCDIC constants are not supported"));
  850. /* Fall through. */
  851. case 'A':
  852. if (! flag_m68k_mri || *input_line_pointer != '\'')
  853. goto de_fault;
  854. ++input_line_pointer;
  855. /* Fall through. */
  856. #endif
  857. case '\'':
  858. if (! flag_m68k_mri)
  859. {
  860. /* Warning: to conform to other people's assemblers NO
  861. ESCAPEMENT is permitted for a single quote. The next
  862. character, parity errors and all, is taken as the value
  863. of the operand. VERY KINKY. */
  864. expressionP->X_op = O_constant;
  865. expressionP->X_add_number = *input_line_pointer++;
  866. break;
  867. }
  868. mri_char_constant (expressionP);
  869. break;
  870. #ifdef TC_M68K
  871. case '"':
  872. /* Double quote is the bitwise not operator in MRI mode. */
  873. if (! flag_m68k_mri)
  874. goto de_fault;
  875. /* Fall through. */
  876. #endif
  877. case '~':
  878. /* '~' is permitted to start a label on the Delta. */
  879. if (is_name_beginner (c))
  880. goto isname;
  881. case '!':
  882. case '-':
  883. case '+':
  884. {
  885. #ifdef md_operator
  886. unary:
  887. #endif
  888. operand (expressionP, mode);
  889. if (expressionP->X_op == O_constant)
  890. {
  891. /* input_line_pointer -> char after operand. */
  892. if (c == '-')
  893. {
  894. expressionP->X_add_number
  895. = - (addressT) 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. if (expressionP->X_add_number)
  901. expressionP->X_extrabit ^= 1;
  902. }
  903. else if (c == '~' || c == '"')
  904. expressionP->X_add_number = ~ expressionP->X_add_number;
  905. else if (c == '!')
  906. expressionP->X_add_number = ! expressionP->X_add_number;
  907. }
  908. else if (expressionP->X_op == O_big
  909. && expressionP->X_add_number <= 0
  910. && c == '-'
  911. && (generic_floating_point_number.sign == '+'
  912. || generic_floating_point_number.sign == 'P'))
  913. {
  914. /* Negative flonum (eg, -1.000e0). */
  915. if (generic_floating_point_number.sign == '+')
  916. generic_floating_point_number.sign = '-';
  917. else
  918. generic_floating_point_number.sign = 'N';
  919. }
  920. else if (expressionP->X_op == O_big
  921. && expressionP->X_add_number > 0)
  922. {
  923. int i;
  924. if (c == '~' || c == '-')
  925. {
  926. for (i = 0; i < expressionP->X_add_number; ++i)
  927. generic_bignum[i] = ~generic_bignum[i];
  928. /* Extend the bignum to at least the size of .octa. */
  929. if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
  930. {
  931. expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
  932. for (; i < expressionP->X_add_number; ++i)
  933. generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
  934. }
  935. if (c == '-')
  936. for (i = 0; i < expressionP->X_add_number; ++i)
  937. {
  938. generic_bignum[i] += 1;
  939. if (generic_bignum[i])
  940. break;
  941. }
  942. }
  943. else if (c == '!')
  944. {
  945. for (i = 0; i < expressionP->X_add_number; ++i)
  946. if (generic_bignum[i] != 0)
  947. break;
  948. expressionP->X_add_number = i >= expressionP->X_add_number;
  949. expressionP->X_op = O_constant;
  950. expressionP->X_unsigned = 1;
  951. expressionP->X_extrabit = 0;
  952. }
  953. }
  954. else if (expressionP->X_op != O_illegal
  955. && expressionP->X_op != O_absent)
  956. {
  957. if (c != '+')
  958. {
  959. expressionP->X_add_symbol = make_expr_symbol (expressionP);
  960. if (c == '-')
  961. expressionP->X_op = O_uminus;
  962. else if (c == '~' || c == '"')
  963. expressionP->X_op = O_bit_not;
  964. else
  965. expressionP->X_op = O_logical_not;
  966. expressionP->X_add_number = 0;
  967. }
  968. }
  969. else
  970. as_warn (_("Unary operator %c ignored because bad operand follows"),
  971. c);
  972. }
  973. break;
  974. #if defined (DOLLAR_DOT) || defined (TC_M68K)
  975. case '$':
  976. /* '$' is the program counter when in MRI mode, or when
  977. DOLLAR_DOT is defined. */
  978. #ifndef DOLLAR_DOT
  979. if (! flag_m68k_mri)
  980. goto de_fault;
  981. #endif
  982. if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
  983. {
  984. /* In MRI mode and on Z80, '$' is also used as the prefix
  985. for a hexadecimal constant. */
  986. integer_constant (16, expressionP);
  987. break;
  988. }
  989. if (is_part_of_name (*input_line_pointer))
  990. goto isname;
  991. current_location (expressionP);
  992. break;
  993. #endif
  994. case '.':
  995. if (!is_part_of_name (*input_line_pointer))
  996. {
  997. current_location (expressionP);
  998. break;
  999. }
  1000. else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
  1001. && ! is_part_of_name (input_line_pointer[8]))
  1002. || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
  1003. && ! is_part_of_name (input_line_pointer[7])))
  1004. {
  1005. int start;
  1006. start = (input_line_pointer[1] == 't'
  1007. || input_line_pointer[1] == 'T');
  1008. input_line_pointer += start ? 8 : 7;
  1009. SKIP_WHITESPACE ();
  1010. if (*input_line_pointer != '(')
  1011. as_bad (_("syntax error in .startof. or .sizeof."));
  1012. else
  1013. {
  1014. char *buf;
  1015. ++input_line_pointer;
  1016. SKIP_WHITESPACE ();
  1017. c = get_symbol_name (& name);
  1018. buf = (char *) xmalloc (strlen (name) + 10);
  1019. if (start)
  1020. sprintf (buf, ".startof.%s", name);
  1021. else
  1022. sprintf (buf, ".sizeof.%s", name);
  1023. symbolP = symbol_make (buf);
  1024. free (buf);
  1025. expressionP->X_op = O_symbol;
  1026. expressionP->X_add_symbol = symbolP;
  1027. expressionP->X_add_number = 0;
  1028. *input_line_pointer = c;
  1029. SKIP_WHITESPACE_AFTER_NAME ();
  1030. if (*input_line_pointer != ')')
  1031. as_bad (_("syntax error in .startof. or .sizeof."));
  1032. else
  1033. ++input_line_pointer;
  1034. }
  1035. break;
  1036. }
  1037. else
  1038. {
  1039. goto isname;
  1040. }
  1041. case ',':
  1042. eol:
  1043. /* Can't imagine any other kind of operand. */
  1044. expressionP->X_op = O_absent;
  1045. input_line_pointer--;
  1046. break;
  1047. #ifdef TC_M68K
  1048. case '%':
  1049. if (! flag_m68k_mri)
  1050. goto de_fault;
  1051. integer_constant (2, expressionP);
  1052. break;
  1053. case '@':
  1054. if (! flag_m68k_mri)
  1055. goto de_fault;
  1056. integer_constant (8, expressionP);
  1057. break;
  1058. case ':':
  1059. if (! flag_m68k_mri)
  1060. goto de_fault;
  1061. /* In MRI mode, this is a floating point constant represented
  1062. using hexadecimal digits. */
  1063. ++input_line_pointer;
  1064. integer_constant (16, expressionP);
  1065. break;
  1066. case '*':
  1067. if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
  1068. goto de_fault;
  1069. current_location (expressionP);
  1070. break;
  1071. #endif
  1072. default:
  1073. #if defined(md_need_index_operator) || defined(TC_M68K)
  1074. de_fault:
  1075. #endif
  1076. if (is_name_beginner (c) || c == '"') /* Here if did not begin with a digit. */
  1077. {
  1078. /* Identifier begins here.
  1079. This is kludged for speed, so code is repeated. */
  1080. isname:
  1081. -- input_line_pointer;
  1082. c = get_symbol_name (&name);
  1083. #ifdef md_operator
  1084. {
  1085. operatorT op = md_operator (name, 1, &c);
  1086. switch (op)
  1087. {
  1088. case O_uminus:
  1089. restore_line_pointer (c);
  1090. c = '-';
  1091. goto unary;
  1092. case O_bit_not:
  1093. restore_line_pointer (c);
  1094. c = '~';
  1095. goto unary;
  1096. case O_logical_not:
  1097. restore_line_pointer (c);
  1098. c = '!';
  1099. goto unary;
  1100. case O_illegal:
  1101. as_bad (_("invalid use of operator \"%s\""), name);
  1102. break;
  1103. default:
  1104. break;
  1105. }
  1106. if (op != O_absent && op != O_illegal)
  1107. {
  1108. restore_line_pointer (c);
  1109. expr (9, expressionP, mode);
  1110. expressionP->X_add_symbol = make_expr_symbol (expressionP);
  1111. expressionP->X_op_symbol = NULL;
  1112. expressionP->X_add_number = 0;
  1113. expressionP->X_op = op;
  1114. break;
  1115. }
  1116. }
  1117. #endif
  1118. #ifdef md_parse_name
  1119. /* This is a hook for the backend to parse certain names
  1120. specially in certain contexts. If a name always has a
  1121. specific value, it can often be handled by simply
  1122. entering it in the symbol table. */
  1123. if (md_parse_name (name, expressionP, mode, &c))
  1124. {
  1125. restore_line_pointer (c);
  1126. break;
  1127. }
  1128. #endif
  1129. #ifdef TC_I960
  1130. /* The MRI i960 assembler permits
  1131. lda sizeof code,g13
  1132. FIXME: This should use md_parse_name. */
  1133. if (flag_mri
  1134. && (strcasecmp (name, "sizeof") == 0
  1135. || strcasecmp (name, "startof") == 0))
  1136. {
  1137. int start;
  1138. char *buf;
  1139. start = (name[1] == 't'
  1140. || name[1] == 'T');
  1141. *input_line_pointer = c;
  1142. SKIP_WHITESPACE_AFTER_NAME ();
  1143. c = get_symbol_name (& name);
  1144. buf = (char *) xmalloc (strlen (name) + 10);
  1145. if (start)
  1146. sprintf (buf, ".startof.%s", name);
  1147. else
  1148. sprintf (buf, ".sizeof.%s", name);
  1149. symbolP = symbol_make (buf);
  1150. free (buf);
  1151. expressionP->X_op = O_symbol;
  1152. expressionP->X_add_symbol = symbolP;
  1153. expressionP->X_add_number = 0;
  1154. *input_line_pointer = c;
  1155. SKIP_WHITESPACE_AFTER_NAME ();
  1156. break;
  1157. }
  1158. #endif
  1159. symbolP = symbol_find_or_make (name);
  1160. /* If we have an absolute symbol or a reg, then we know its
  1161. value now. */
  1162. segment = S_GET_SEGMENT (symbolP);
  1163. if (mode != expr_defer
  1164. && segment == absolute_section
  1165. && !S_FORCE_RELOC (symbolP, 0))
  1166. {
  1167. expressionP->X_op = O_constant;
  1168. expressionP->X_add_number = S_GET_VALUE (symbolP);
  1169. }
  1170. else if (mode != expr_defer && segment == reg_section)
  1171. {
  1172. expressionP->X_op = O_register;
  1173. expressionP->X_add_number = S_GET_VALUE (symbolP);
  1174. }
  1175. else
  1176. {
  1177. expressionP->X_op = O_symbol;
  1178. expressionP->X_add_symbol = symbolP;
  1179. expressionP->X_add_number = 0;
  1180. }
  1181. restore_line_pointer (c);
  1182. }
  1183. else
  1184. {
  1185. /* Let the target try to parse it. Success is indicated by changing
  1186. the X_op field to something other than O_absent and pointing
  1187. input_line_pointer past the expression. If it can't parse the
  1188. expression, X_op and input_line_pointer should be unchanged. */
  1189. expressionP->X_op = O_absent;
  1190. --input_line_pointer;
  1191. md_operand (expressionP);
  1192. if (expressionP->X_op == O_absent)
  1193. {
  1194. ++input_line_pointer;
  1195. as_bad (_("bad expression"));
  1196. expressionP->X_op = O_constant;
  1197. expressionP->X_add_number = 0;
  1198. }
  1199. }
  1200. break;
  1201. }
  1202. /* It is more 'efficient' to clean up the expressionS when they are
  1203. created. Doing it here saves lines of code. */
  1204. clean_up_expression (expressionP);
  1205. SKIP_WHITESPACE (); /* -> 1st char after operand. */
  1206. know (*input_line_pointer != ' ');
  1207. /* The PA port needs this information. */
  1208. if (expressionP->X_add_symbol)
  1209. symbol_mark_used (expressionP->X_add_symbol);
  1210. if (mode != expr_defer)
  1211. {
  1212. expressionP->X_add_symbol
  1213. = symbol_clone_if_forward_ref (expressionP->X_add_symbol);
  1214. expressionP->X_op_symbol
  1215. = symbol_clone_if_forward_ref (expressionP->X_op_symbol);
  1216. }
  1217. switch (expressionP->X_op)
  1218. {
  1219. default:
  1220. return absolute_section;
  1221. case O_symbol:
  1222. return S_GET_SEGMENT (expressionP->X_add_symbol);
  1223. case O_register:
  1224. return reg_section;
  1225. }
  1226. }
  1227. /* Internal. Simplify a struct expression for use by expr (). */
  1228. /* In: address of an expressionS.
  1229. The X_op field of the expressionS may only take certain values.
  1230. Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
  1231. Out: expressionS may have been modified:
  1232. Unused fields zeroed to help expr (). */
  1233. static void
  1234. clean_up_expression (expressionS *expressionP)
  1235. {
  1236. switch (expressionP->X_op)
  1237. {
  1238. case O_illegal:
  1239. case O_absent:
  1240. expressionP->X_add_number = 0;
  1241. /* Fall through. */
  1242. case O_big:
  1243. case O_constant:
  1244. case O_register:
  1245. expressionP->X_add_symbol = NULL;
  1246. /* Fall through. */
  1247. case O_symbol:
  1248. case O_uminus:
  1249. case O_bit_not:
  1250. expressionP->X_op_symbol = NULL;
  1251. break;
  1252. default:
  1253. break;
  1254. }
  1255. }
  1256. /* Expression parser. */
  1257. /* We allow an empty expression, and just assume (absolute,0) silently.
  1258. Unary operators and parenthetical expressions are treated as operands.
  1259. As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
  1260. We used to do an aho/ullman shift-reduce parser, but the logic got so
  1261. warped that I flushed it and wrote a recursive-descent parser instead.
  1262. Now things are stable, would anybody like to write a fast parser?
  1263. Most expressions are either register (which does not even reach here)
  1264. or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
  1265. So I guess it doesn't really matter how inefficient more complex expressions
  1266. are parsed.
  1267. After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
  1268. Also, we have consumed any leading or trailing spaces (operand does that)
  1269. and done all intervening operators.
  1270. This returns the segment of the result, which will be
  1271. absolute_section or the segment of a symbol. */
  1272. #undef __
  1273. #define __ O_illegal
  1274. #ifndef O_SINGLE_EQ
  1275. #define O_SINGLE_EQ O_illegal
  1276. #endif
  1277. /* Maps ASCII -> operators. */
  1278. static const operatorT op_encoding[256] = {
  1279. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1280. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1281. __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
  1282. __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
  1283. __, __, __, __, __, __, __, __,
  1284. __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
  1285. __, __, __, __, __, __, __, __,
  1286. __, __, __, __, __, __, __, __,
  1287. __, __, __, __, __, __, __, __,
  1288. __, __, __,
  1289. #ifdef NEED_INDEX_OPERATOR
  1290. O_index,
  1291. #else
  1292. __,
  1293. #endif
  1294. __, __, O_bit_exclusive_or, __,
  1295. __, __, __, __, __, __, __, __,
  1296. __, __, __, __, __, __, __, __,
  1297. __, __, __, __, __, __, __, __,
  1298. __, __, __, __, O_bit_inclusive_or, __, __, __,
  1299. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1300. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1301. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1302. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1303. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1304. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1305. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
  1306. __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
  1307. };
  1308. /* Rank Examples
  1309. 0 operand, (expression)
  1310. 1 ||
  1311. 2 &&
  1312. 3 == <> < <= >= >
  1313. 4 + -
  1314. 5 used for * / % in MRI mode
  1315. 6 & ^ ! |
  1316. 7 * / % << >>
  1317. 8 unary - unary ~
  1318. */
  1319. static operator_rankT op_rank[O_max] = {
  1320. 0, /* O_illegal */
  1321. 0, /* O_absent */
  1322. 0, /* O_constant */
  1323. 0, /* O_symbol */
  1324. 0, /* O_symbol_rva */
  1325. 0, /* O_register */
  1326. 0, /* O_big */
  1327. 9, /* O_uminus */
  1328. 9, /* O_bit_not */
  1329. 9, /* O_logical_not */
  1330. 8, /* O_multiply */
  1331. 8, /* O_divide */
  1332. 8, /* O_modulus */
  1333. 8, /* O_left_shift */
  1334. 8, /* O_right_shift */
  1335. 7, /* O_bit_inclusive_or */
  1336. 7, /* O_bit_or_not */
  1337. 7, /* O_bit_exclusive_or */
  1338. 7, /* O_bit_and */
  1339. 5, /* O_add */
  1340. 5, /* O_subtract */
  1341. 4, /* O_eq */
  1342. 4, /* O_ne */
  1343. 4, /* O_lt */
  1344. 4, /* O_le */
  1345. 4, /* O_ge */
  1346. 4, /* O_gt */
  1347. 3, /* O_logical_and */
  1348. 2, /* O_logical_or */
  1349. 1, /* O_index */
  1350. };
  1351. /* Unfortunately, in MRI mode for the m68k, multiplication and
  1352. division have lower precedence than the bit wise operators. This
  1353. function sets the operator precedences correctly for the current
  1354. mode. Also, MRI uses a different bit_not operator, and this fixes
  1355. that as well. */
  1356. #define STANDARD_MUL_PRECEDENCE 8
  1357. #define MRI_MUL_PRECEDENCE 6
  1358. void
  1359. expr_set_precedence (void)
  1360. {
  1361. if (flag_m68k_mri)
  1362. {
  1363. op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
  1364. op_rank[O_divide] = MRI_MUL_PRECEDENCE;
  1365. op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
  1366. }
  1367. else
  1368. {
  1369. op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
  1370. op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
  1371. op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
  1372. }
  1373. }
  1374. void
  1375. expr_set_rank (operatorT op, operator_rankT rank)
  1376. {
  1377. gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
  1378. op_rank[op] = rank;
  1379. }
  1380. /* Initialize the expression parser. */
  1381. void
  1382. expr_begin (void)
  1383. {
  1384. expr_set_precedence ();
  1385. /* Verify that X_op field is wide enough. */
  1386. {
  1387. expressionS e;
  1388. e.X_op = O_max;
  1389. gas_assert (e.X_op == O_max);
  1390. }
  1391. }
  1392. /* Return the encoding for the operator at INPUT_LINE_POINTER, and
  1393. sets NUM_CHARS to the number of characters in the operator.
  1394. Does not advance INPUT_LINE_POINTER. */
  1395. static inline operatorT
  1396. operatorf (int *num_chars)
  1397. {
  1398. int c;
  1399. operatorT ret;
  1400. c = *input_line_pointer & 0xff;
  1401. *num_chars = 1;
  1402. if (is_end_of_line[c])
  1403. return O_illegal;
  1404. #ifdef md_operator
  1405. if (is_name_beginner (c))
  1406. {
  1407. char *name;
  1408. char ec = get_symbol_name (& name);
  1409. ret = md_operator (name, 2, &ec);
  1410. switch (ret)
  1411. {
  1412. case O_absent:
  1413. *input_line_pointer = ec;
  1414. input_line_pointer = name;
  1415. break;
  1416. case O_uminus:
  1417. case O_bit_not:
  1418. case O_logical_not:
  1419. as_bad (_("invalid use of operator \"%s\""), name);
  1420. ret = O_illegal;
  1421. /* FALLTHROUGH */
  1422. default:
  1423. *input_line_pointer = ec;
  1424. *num_chars = input_line_pointer - name;
  1425. input_line_pointer = name;
  1426. return ret;
  1427. }
  1428. }
  1429. #endif
  1430. switch (c)
  1431. {
  1432. default:
  1433. ret = op_encoding[c];
  1434. #ifdef md_operator
  1435. if (ret == O_illegal)
  1436. {
  1437. char *start = input_line_pointer;
  1438. ret = md_operator (NULL, 2, NULL);
  1439. if (ret != O_illegal)
  1440. *num_chars = input_line_pointer - start;
  1441. input_line_pointer = start;
  1442. }
  1443. #endif
  1444. return ret;
  1445. case '+':
  1446. case '-':
  1447. return op_encoding[c];
  1448. case '<':
  1449. switch (input_line_pointer[1])
  1450. {
  1451. default:
  1452. return op_encoding[c];
  1453. case '<':
  1454. ret = O_left_shift;
  1455. break;
  1456. case '>':
  1457. ret = O_ne;
  1458. break;
  1459. case '=':
  1460. ret = O_le;
  1461. break;
  1462. }
  1463. *num_chars = 2;
  1464. return ret;
  1465. case '=':
  1466. if (input_line_pointer[1] != '=')
  1467. return op_encoding[c];
  1468. *num_chars = 2;
  1469. return O_eq;
  1470. case '>':
  1471. switch (input_line_pointer[1])
  1472. {
  1473. default:
  1474. return op_encoding[c];
  1475. case '>':
  1476. ret = O_right_shift;
  1477. break;
  1478. case '=':
  1479. ret = O_ge;
  1480. break;
  1481. }
  1482. *num_chars = 2;
  1483. return ret;
  1484. case '!':
  1485. switch (input_line_pointer[1])
  1486. {
  1487. case '!':
  1488. /* We accept !! as equivalent to ^ for MRI compatibility. */
  1489. *num_chars = 2;
  1490. return O_bit_exclusive_or;
  1491. case '=':
  1492. /* We accept != as equivalent to <>. */
  1493. *num_chars = 2;
  1494. return O_ne;
  1495. default:
  1496. if (flag_m68k_mri)
  1497. return O_bit_inclusive_or;
  1498. return op_encoding[c];
  1499. }
  1500. case '|':
  1501. if (input_line_pointer[1] != '|')
  1502. return op_encoding[c];
  1503. *num_chars = 2;
  1504. return O_logical_or;
  1505. case '&':
  1506. if (input_line_pointer[1] != '&')
  1507. return op_encoding[c];
  1508. *num_chars = 2;
  1509. return O_logical_and;
  1510. }
  1511. /* NOTREACHED */
  1512. }
  1513. /* Implement "word-size + 1 bit" addition for
  1514. {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}. This
  1515. is used so that the full range of unsigned word values and the full range of
  1516. signed word values can be represented in an O_constant expression, which is
  1517. useful e.g. for .sleb128 directives. */
  1518. void
  1519. add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
  1520. {
  1521. valueT ures = resultP->X_add_number;
  1522. valueT uamount = amount;
  1523. resultP->X_add_number += amount;
  1524. resultP->X_extrabit ^= rhs_highbit;
  1525. if (ures + uamount < ures)
  1526. resultP->X_extrabit ^= 1;
  1527. }
  1528. /* Similarly, for subtraction. */
  1529. void
  1530. subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
  1531. {
  1532. valueT ures = resultP->X_add_number;
  1533. valueT uamount = amount;
  1534. resultP->X_add_number -= amount;
  1535. resultP->X_extrabit ^= rhs_highbit;
  1536. if (ures < uamount)
  1537. resultP->X_extrabit ^= 1;
  1538. }
  1539. /* Parse an expression. */
  1540. segT
  1541. expr (int rankarg, /* Larger # is higher rank. */
  1542. expressionS *resultP, /* Deliver result here. */
  1543. enum expr_mode mode /* Controls behavior. */)
  1544. {
  1545. operator_rankT rank = (operator_rankT) rankarg;
  1546. segT retval;
  1547. expressionS right;
  1548. operatorT op_left;
  1549. operatorT op_right;
  1550. int op_chars;
  1551. know (rankarg >= 0);
  1552. /* Save the value of dot for the fixup code. */
  1553. if (rank == 0)
  1554. {
  1555. dot_value = frag_now_fix ();
  1556. dot_frag = frag_now;
  1557. }
  1558. retval = operand (resultP, mode);
  1559. /* operand () gobbles spaces. */
  1560. know (*input_line_pointer != ' ');
  1561. op_left = operatorf (&op_chars);
  1562. while (op_left != O_illegal && op_rank[(int) op_left] > rank)
  1563. {
  1564. segT rightseg;
  1565. offsetT frag_off;
  1566. input_line_pointer += op_chars; /* -> after operator. */
  1567. right.X_md = 0;
  1568. rightseg = expr (op_rank[(int) op_left], &right, mode);
  1569. if (right.X_op == O_absent)
  1570. {
  1571. as_warn (_("missing operand; zero assumed"));
  1572. right.X_op = O_constant;
  1573. right.X_add_number = 0;
  1574. right.X_add_symbol = NULL;
  1575. right.X_op_symbol = NULL;
  1576. }
  1577. know (*input_line_pointer != ' ');
  1578. if (op_left == O_index)
  1579. {
  1580. if (*input_line_pointer != ']')
  1581. as_bad ("missing right bracket");
  1582. else
  1583. {
  1584. ++input_line_pointer;
  1585. SKIP_WHITESPACE ();
  1586. }
  1587. }
  1588. op_right = operatorf (&op_chars);
  1589. know (op_right == O_illegal || op_left == O_index
  1590. || op_rank[(int) op_right] <= op_rank[(int) op_left]);
  1591. know ((int) op_left >= (int) O_multiply);
  1592. #ifndef md_operator
  1593. know ((int) op_left <= (int) O_index);
  1594. #else
  1595. know ((int) op_left < (int) O_max);
  1596. #endif
  1597. /* input_line_pointer->after right-hand quantity. */
  1598. /* left-hand quantity in resultP. */
  1599. /* right-hand quantity in right. */
  1600. /* operator in op_left. */
  1601. if (resultP->X_op == O_big)
  1602. {
  1603. if (resultP->X_add_number > 0)
  1604. as_warn (_("left operand is a bignum; integer 0 assumed"));
  1605. else
  1606. as_warn (_("left operand is a float; integer 0 assumed"));
  1607. resultP->X_op = O_constant;
  1608. resultP->X_add_number = 0;
  1609. resultP->X_add_symbol = NULL;
  1610. resultP->X_op_symbol = NULL;
  1611. }
  1612. if (right.X_op == O_big)
  1613. {
  1614. if (right.X_add_number > 0)
  1615. as_warn (_("right operand is a bignum; integer 0 assumed"));
  1616. else
  1617. as_warn (_("right operand is a float; integer 0 assumed"));
  1618. right.X_op = O_constant;
  1619. right.X_add_number = 0;
  1620. right.X_add_symbol = NULL;
  1621. right.X_op_symbol = NULL;
  1622. }
  1623. /* Optimize common cases. */
  1624. #ifdef md_optimize_expr
  1625. if (md_optimize_expr (resultP, op_left, &right))
  1626. {
  1627. /* Skip. */
  1628. ;
  1629. }
  1630. else
  1631. #endif
  1632. #ifndef md_register_arithmetic
  1633. # define md_register_arithmetic 1
  1634. #endif
  1635. if (op_left == O_add && right.X_op == O_constant
  1636. && (md_register_arithmetic || resultP->X_op != O_register))
  1637. {
  1638. /* X + constant. */
  1639. add_to_result (resultP, right.X_add_number, right.X_extrabit);
  1640. }
  1641. /* This case comes up in PIC code. */
  1642. else if (op_left == O_subtract
  1643. && right.X_op == O_symbol
  1644. && resultP->X_op == O_symbol
  1645. && retval == rightseg
  1646. #ifdef md_allow_local_subtract
  1647. && md_allow_local_subtract (resultP, & right, rightseg)
  1648. #endif
  1649. && ((SEG_NORMAL (rightseg)
  1650. && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
  1651. && !S_FORCE_RELOC (right.X_add_symbol, 0))
  1652. || right.X_add_symbol == resultP->X_add_symbol)
  1653. && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
  1654. symbol_get_frag (right.X_add_symbol),
  1655. &frag_off))
  1656. {
  1657. offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
  1658. - S_GET_VALUE (right.X_add_symbol);
  1659. subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
  1660. subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
  1661. add_to_result (resultP, symval_diff, symval_diff < 0);
  1662. resultP->X_op = O_constant;
  1663. resultP->X_add_symbol = 0;
  1664. }
  1665. else if (op_left == O_subtract && right.X_op == O_constant
  1666. && (md_register_arithmetic || resultP->X_op != O_register))
  1667. {
  1668. /* X - constant. */
  1669. subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
  1670. }
  1671. else if (op_left == O_add && resultP->X_op == O_constant
  1672. && (md_register_arithmetic || right.X_op != O_register))
  1673. {
  1674. /* Constant + X. */
  1675. resultP->X_op = right.X_op;
  1676. resultP->X_add_symbol = right.X_add_symbol;
  1677. resultP->X_op_symbol = right.X_op_symbol;
  1678. add_to_result (resultP, right.X_add_number, right.X_extrabit);
  1679. retval = rightseg;
  1680. }
  1681. else if (resultP->X_op == O_constant && right.X_op == O_constant)
  1682. {
  1683. /* Constant OP constant. */
  1684. offsetT v = right.X_add_number;
  1685. if (v == 0 && (op_left == O_divide || op_left == O_modulus))
  1686. {
  1687. as_warn (_("division by zero"));
  1688. v = 1;
  1689. }
  1690. if ((valueT) v >= sizeof(valueT) * CHAR_BIT
  1691. && (op_left == O_left_shift || op_left == O_right_shift))
  1692. {
  1693. as_warn_value_out_of_range (_("shift count"), v, 0,
  1694. sizeof(valueT) * CHAR_BIT - 1,
  1695. NULL, 0);
  1696. resultP->X_add_number = v = 0;
  1697. }
  1698. switch (op_left)
  1699. {
  1700. default: goto general;
  1701. case O_multiply: resultP->X_add_number *= v; break;
  1702. case O_divide: resultP->X_add_number /= v; break;
  1703. case O_modulus: resultP->X_add_number %= v; break;
  1704. case O_left_shift: resultP->X_add_number <<= v; break;
  1705. case O_right_shift:
  1706. /* We always use unsigned shifts, to avoid relying on
  1707. characteristics of the compiler used to compile gas. */
  1708. resultP->X_add_number =
  1709. (offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
  1710. break;
  1711. case O_bit_inclusive_or: resultP->X_add_number |= v; break;
  1712. case O_bit_or_not: resultP->X_add_number |= ~v; break;
  1713. case O_bit_exclusive_or: resultP->X_add_number ^= v; break;
  1714. case O_bit_and: resultP->X_add_number &= v; break;
  1715. /* Constant + constant (O_add) is handled by the
  1716. previous if statement for constant + X, so is omitted
  1717. here. */
  1718. case O_subtract:
  1719. subtract_from_result (resultP, v, 0);
  1720. break;
  1721. case O_eq:
  1722. resultP->X_add_number =
  1723. resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
  1724. break;
  1725. case O_ne:
  1726. resultP->X_add_number =
  1727. resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
  1728. break;
  1729. case O_lt:
  1730. resultP->X_add_number =
  1731. resultP->X_add_number < v ? ~ (offsetT) 0 : 0;
  1732. break;
  1733. case O_le:
  1734. resultP->X_add_number =
  1735. resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
  1736. break;
  1737. case O_ge:
  1738. resultP->X_add_number =
  1739. resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
  1740. break;
  1741. case O_gt:
  1742. resultP->X_add_number =
  1743. resultP->X_add_number > v ? ~ (offsetT) 0 : 0;
  1744. break;
  1745. case O_logical_and:
  1746. resultP->X_add_number = resultP->X_add_number && v;
  1747. break;
  1748. case O_logical_or:
  1749. resultP->X_add_number = resultP->X_add_number || v;
  1750. break;
  1751. }
  1752. }
  1753. else if (resultP->X_op == O_symbol
  1754. && right.X_op == O_symbol
  1755. && (op_left == O_add
  1756. || op_left == O_subtract
  1757. || (resultP->X_add_number == 0
  1758. && right.X_add_number == 0)))
  1759. {
  1760. /* Symbol OP symbol. */
  1761. resultP->X_op = op_left;
  1762. resultP->X_op_symbol = right.X_add_symbol;
  1763. if (op_left == O_add)
  1764. add_to_result (resultP, right.X_add_number, right.X_extrabit);
  1765. else if (op_left == O_subtract)
  1766. {
  1767. subtract_from_result (resultP, right.X_add_number,
  1768. right.X_extrabit);
  1769. if (retval == rightseg
  1770. && SEG_NORMAL (retval)
  1771. && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
  1772. && !S_FORCE_RELOC (right.X_add_symbol, 0))
  1773. {
  1774. retval = absolute_section;
  1775. rightseg = absolute_section;
  1776. }
  1777. }
  1778. }
  1779. else
  1780. {
  1781. general:
  1782. /* The general case. */
  1783. resultP->X_add_symbol = make_expr_symbol (resultP);
  1784. resultP->X_op_symbol = make_expr_symbol (&right);
  1785. resultP->X_op = op_left;
  1786. resultP->X_add_number = 0;
  1787. resultP->X_unsigned = 1;
  1788. resultP->X_extrabit = 0;
  1789. }
  1790. if (retval != rightseg)
  1791. {
  1792. if (retval == undefined_section)
  1793. ;
  1794. else if (rightseg == undefined_section)
  1795. retval = rightseg;
  1796. else if (retval == expr_section)
  1797. ;
  1798. else if (rightseg == expr_section)
  1799. retval = rightseg;
  1800. else if (retval == reg_section)
  1801. ;
  1802. else if (rightseg == reg_section)
  1803. retval = rightseg;
  1804. else if (rightseg == absolute_section)
  1805. ;
  1806. else if (retval == absolute_section)
  1807. retval = rightseg;
  1808. #ifdef DIFF_EXPR_OK
  1809. else if (op_left == O_subtract)
  1810. ;
  1811. #endif
  1812. else
  1813. as_bad (_("operation combines symbols in different segments"));
  1814. }
  1815. op_left = op_right;
  1816. } /* While next operator is >= this rank. */
  1817. /* The PA port needs this information. */
  1818. if (resultP->X_add_symbol)
  1819. symbol_mark_used (resultP->X_add_symbol);
  1820. if (rank == 0 && mode == expr_evaluate)
  1821. resolve_expression (resultP);
  1822. return resultP->X_op == O_constant ? absolute_section : retval;
  1823. }
  1824. /* Resolve an expression without changing any symbols/sub-expressions
  1825. used. */
  1826. int
  1827. resolve_expression (expressionS *expressionP)
  1828. {
  1829. /* Help out with CSE. */
  1830. valueT final_val = expressionP->X_add_number;
  1831. symbolS *add_symbol = expressionP->X_add_symbol;
  1832. symbolS *orig_add_symbol = add_symbol;
  1833. symbolS *op_symbol = expressionP->X_op_symbol;
  1834. operatorT op = expressionP->X_op;
  1835. valueT left, right;
  1836. segT seg_left, seg_right;
  1837. fragS *frag_left, *frag_right;
  1838. offsetT frag_off;
  1839. switch (op)
  1840. {
  1841. default:
  1842. return 0;
  1843. case O_constant:
  1844. case O_register:
  1845. left = 0;
  1846. break;
  1847. case O_symbol:
  1848. case O_symbol_rva:
  1849. if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
  1850. return 0;
  1851. break;
  1852. case O_uminus:
  1853. case O_bit_not:
  1854. case O_logical_not:
  1855. if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
  1856. return 0;
  1857. if (seg_left != absolute_section)
  1858. return 0;
  1859. if (op == O_logical_not)
  1860. left = !left;
  1861. else if (op == O_uminus)
  1862. left = -left;
  1863. else
  1864. left = ~left;
  1865. op = O_constant;
  1866. break;
  1867. case O_multiply:
  1868. case O_divide:
  1869. case O_modulus:
  1870. case O_left_shift:
  1871. case O_right_shift:
  1872. case O_bit_inclusive_or:
  1873. case O_bit_or_not:
  1874. case O_bit_exclusive_or:
  1875. case O_bit_and:
  1876. case O_add:
  1877. case O_subtract:
  1878. case O_eq:
  1879. case O_ne:
  1880. case O_lt:
  1881. case O_le:
  1882. case O_ge:
  1883. case O_gt:
  1884. case O_logical_and:
  1885. case O_logical_or:
  1886. if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
  1887. || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
  1888. return 0;
  1889. /* Simplify addition or subtraction of a constant by folding the
  1890. constant into X_add_number. */
  1891. if (op == O_add)
  1892. {
  1893. if (seg_right == absolute_section)
  1894. {
  1895. final_val += right;
  1896. op = O_symbol;
  1897. break;
  1898. }
  1899. else if (seg_left == absolute_section)
  1900. {
  1901. final_val += left;
  1902. left = right;
  1903. seg_left = seg_right;
  1904. add_symbol = op_symbol;
  1905. orig_add_symbol = expressionP->X_op_symbol;
  1906. op = O_symbol;
  1907. break;
  1908. }
  1909. }
  1910. else if (op == O_subtract)
  1911. {
  1912. if (seg_right == absolute_section)
  1913. {
  1914. final_val -= right;
  1915. op = O_symbol;
  1916. break;
  1917. }
  1918. }
  1919. /* Equality and non-equality tests are permitted on anything.
  1920. Subtraction, and other comparison operators are permitted if
  1921. both operands are in the same section.
  1922. Shifts by constant zero are permitted on anything.
  1923. Multiplies, bit-ors, and bit-ands with constant zero are
  1924. permitted on anything.
  1925. Multiplies and divides by constant one are permitted on
  1926. anything.
  1927. Binary operations with both operands being the same register
  1928. or undefined symbol are permitted if the result doesn't depend
  1929. on the input value.
  1930. Otherwise, both operands must be absolute. We already handled
  1931. the case of addition or subtraction of a constant above. */
  1932. frag_off = 0;
  1933. if (!(seg_left == absolute_section
  1934. && seg_right == absolute_section)
  1935. && !(op == O_eq || op == O_ne)
  1936. && !((op == O_subtract
  1937. || op == O_lt || op == O_le || op == O_ge || op == O_gt)
  1938. && seg_left == seg_right
  1939. && (finalize_syms
  1940. || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
  1941. && (seg_left != reg_section || left == right)
  1942. && (seg_left != undefined_section || add_symbol == op_symbol)))
  1943. {
  1944. if ((seg_left == absolute_section && left == 0)
  1945. || (seg_right == absolute_section && right == 0))
  1946. {
  1947. if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
  1948. {
  1949. if (!(seg_right == absolute_section && right == 0))
  1950. {
  1951. seg_left = seg_right;
  1952. left = right;
  1953. add_symbol = op_symbol;
  1954. orig_add_symbol = expressionP->X_op_symbol;
  1955. }
  1956. op = O_symbol;
  1957. break;
  1958. }
  1959. else if (op == O_left_shift || op == O_right_shift)
  1960. {
  1961. if (!(seg_left == absolute_section && left == 0))
  1962. {
  1963. op = O_symbol;
  1964. break;
  1965. }
  1966. }
  1967. else if (op != O_multiply
  1968. && op != O_bit_or_not && op != O_bit_and)
  1969. return 0;
  1970. }
  1971. else if (op == O_multiply
  1972. && seg_left == absolute_section && left == 1)
  1973. {
  1974. seg_left = seg_right;
  1975. left = right;
  1976. add_symbol = op_symbol;
  1977. orig_add_symbol = expressionP->X_op_symbol;
  1978. op = O_symbol;
  1979. break;
  1980. }
  1981. else if ((op == O_multiply || op == O_divide)
  1982. && seg_right == absolute_section && right == 1)
  1983. {
  1984. op = O_symbol;
  1985. break;
  1986. }
  1987. else if (!(left == right
  1988. && ((seg_left == reg_section && seg_right == reg_section)
  1989. || (seg_left == undefined_section
  1990. && seg_right == undefined_section
  1991. && add_symbol == op_symbol))))
  1992. return 0;
  1993. else if (op == O_bit_and || op == O_bit_inclusive_or)
  1994. {
  1995. op = O_symbol;
  1996. break;
  1997. }
  1998. else if (op != O_bit_exclusive_or && op != O_bit_or_not)
  1999. return 0;
  2000. }
  2001. right += frag_off / OCTETS_PER_BYTE;
  2002. switch (op)
  2003. {
  2004. case O_add: left += right; break;
  2005. case O_subtract: left -= right; break;
  2006. case O_multiply: left *= right; break;
  2007. case O_divide:
  2008. if (right == 0)
  2009. return 0;
  2010. left = (offsetT) left / (offsetT) right;
  2011. break;
  2012. case O_modulus:
  2013. if (right == 0)
  2014. return 0;
  2015. left = (offsetT) left % (offsetT) right;
  2016. break;
  2017. case O_left_shift: left <<= right; break;
  2018. case O_right_shift: left >>= right; break;
  2019. case O_bit_inclusive_or: left |= right; break;
  2020. case O_bit_or_not: left |= ~right; break;
  2021. case O_bit_exclusive_or: left ^= right; break;
  2022. case O_bit_and: left &= right; break;
  2023. case O_eq:
  2024. case O_ne:
  2025. left = (left == right
  2026. && seg_left == seg_right
  2027. && (finalize_syms || frag_left == frag_right)
  2028. && (seg_left != undefined_section
  2029. || add_symbol == op_symbol)
  2030. ? ~ (valueT) 0 : 0);
  2031. if (op == O_ne)
  2032. left = ~left;
  2033. break;
  2034. case O_lt:
  2035. left = (offsetT) left < (offsetT) right ? ~ (valueT) 0 : 0;
  2036. break;
  2037. case O_le:
  2038. left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
  2039. break;
  2040. case O_ge:
  2041. left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
  2042. break;
  2043. case O_gt:
  2044. left = (offsetT) left > (offsetT) right ? ~ (valueT) 0 : 0;
  2045. break;
  2046. case O_logical_and: left = left && right; break;
  2047. case O_logical_or: left = left || right; break;
  2048. default: abort ();
  2049. }
  2050. op = O_constant;
  2051. break;
  2052. }
  2053. if (op == O_symbol)
  2054. {
  2055. if (seg_left == absolute_section)
  2056. op = O_constant;
  2057. else if (seg_left == reg_section && final_val == 0)
  2058. op = O_register;
  2059. else if (!symbol_same_p (add_symbol, orig_add_symbol))
  2060. final_val += left;
  2061. expressionP->X_add_symbol = add_symbol;
  2062. }
  2063. expressionP->X_op = op;
  2064. if (op == O_constant || op == O_register)
  2065. final_val += left;
  2066. expressionP->X_add_number = final_val;
  2067. return 1;
  2068. }
  2069. /* This lives here because it belongs equally in expr.c & read.c.
  2070. expr.c is just a branch office read.c anyway, and putting it
  2071. here lessens the crowd at read.c.
  2072. Assume input_line_pointer is at start of symbol name, or the
  2073. start of a double quote enclosed symbol name.
  2074. Advance input_line_pointer past symbol name.
  2075. Turn that character into a '\0', returning its former value,
  2076. which may be the closing double quote.
  2077. This allows a string compare (RMS wants symbol names to be strings)
  2078. of the symbol name.
  2079. There will always be a char following symbol name, because all good
  2080. lines end in end-of-line. */
  2081. char
  2082. get_symbol_name (char ** ilp_return)
  2083. {
  2084. char c;
  2085. * ilp_return = input_line_pointer;
  2086. /* We accept \001 in a name in case this is being called with a
  2087. constructed string. */
  2088. if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
  2089. {
  2090. while (is_part_of_name (c = *input_line_pointer++)
  2091. || c == '\001')
  2092. ;
  2093. if (is_name_ender (c))
  2094. c = *input_line_pointer++;
  2095. }
  2096. else if (c == '"')
  2097. {
  2098. bfd_boolean backslash_seen;
  2099. * ilp_return = input_line_pointer;
  2100. do
  2101. {
  2102. backslash_seen = c == '\\';
  2103. c = * input_line_pointer ++;
  2104. }
  2105. while (c != 0 && (c != '"' || backslash_seen));
  2106. if (c == 0)
  2107. as_warn (_("missing closing '\"'"));
  2108. }
  2109. *--input_line_pointer = 0;
  2110. return c;
  2111. }
  2112. /* Replace the NUL character pointed to by input_line_pointer
  2113. with C. If C is \" then advance past it. Return the character
  2114. now pointed to by input_line_pointer. */
  2115. char
  2116. restore_line_pointer (char c)
  2117. {
  2118. * input_line_pointer = c;
  2119. if (c == '"')
  2120. c = * ++ input_line_pointer;
  2121. return c;
  2122. }
  2123. unsigned int
  2124. get_single_number (void)
  2125. {
  2126. expressionS exp;
  2127. operand (&exp, expr_normal);
  2128. return exp.X_add_number;
  2129. }