eval.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * eval.c expression evaluator for the Netwide Assembler
  35. */
  36. #include "compiler.h"
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <stddef.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include "nasm.h"
  43. #include "nasmlib.h"
  44. #include "ilog2.h"
  45. #include "error.h"
  46. #include "eval.h"
  47. #include "labels.h"
  48. #include "float.h"
  49. #include "assemble.h"
  50. #define TEMPEXPRS_DELTA 128
  51. #define TEMPEXPR_DELTA 8
  52. static scanner scan; /* Address of scanner routine */
  53. static expr **tempexprs = NULL;
  54. static int ntempexprs;
  55. static int tempexprs_size = 0;
  56. static expr *tempexpr;
  57. static int ntempexpr;
  58. static int tempexpr_size;
  59. static struct tokenval *tokval; /* The current token */
  60. static int i; /* The t_type of tokval */
  61. static void *scpriv;
  62. static int *opflags;
  63. static struct eval_hints *hint;
  64. static int64_t deadman;
  65. /*
  66. * Unimportant cleanup is done to avoid confusing people who are trying
  67. * to debug real memory leaks
  68. */
  69. void eval_cleanup(void)
  70. {
  71. while (ntempexprs)
  72. nasm_free(tempexprs[--ntempexprs]);
  73. nasm_free(tempexprs);
  74. }
  75. /*
  76. * Construct a temporary expression.
  77. */
  78. static void begintemp(void)
  79. {
  80. tempexpr = NULL;
  81. tempexpr_size = ntempexpr = 0;
  82. }
  83. static void addtotemp(int32_t type, int64_t value)
  84. {
  85. while (ntempexpr >= tempexpr_size) {
  86. tempexpr_size += TEMPEXPR_DELTA;
  87. tempexpr = nasm_realloc(tempexpr,
  88. tempexpr_size * sizeof(*tempexpr));
  89. }
  90. tempexpr[ntempexpr].type = type;
  91. tempexpr[ntempexpr++].value = value;
  92. }
  93. static expr *finishtemp(void)
  94. {
  95. addtotemp(0L, 0L); /* terminate */
  96. while (ntempexprs >= tempexprs_size) {
  97. tempexprs_size += TEMPEXPRS_DELTA;
  98. tempexprs = nasm_realloc(tempexprs,
  99. tempexprs_size * sizeof(*tempexprs));
  100. }
  101. return tempexprs[ntempexprs++] = tempexpr;
  102. }
  103. /*
  104. * Add two vector datatypes. We have some bizarre behaviour on far-
  105. * absolute segment types: we preserve them during addition _only_
  106. * if one of the segments is a truly pure scalar.
  107. */
  108. static expr *add_vectors(expr * p, expr * q)
  109. {
  110. int preserve;
  111. preserve = is_really_simple(p) || is_really_simple(q);
  112. begintemp();
  113. while (p->type && q->type &&
  114. p->type < EXPR_SEGBASE + SEG_ABS &&
  115. q->type < EXPR_SEGBASE + SEG_ABS) {
  116. int lasttype;
  117. if (p->type > q->type) {
  118. addtotemp(q->type, q->value);
  119. lasttype = q++->type;
  120. } else if (p->type < q->type) {
  121. addtotemp(p->type, p->value);
  122. lasttype = p++->type;
  123. } else { /* *p and *q have same type */
  124. int64_t sum = p->value + q->value;
  125. if (sum) {
  126. addtotemp(p->type, sum);
  127. if (hint)
  128. hint->type = EAH_SUMMED;
  129. }
  130. lasttype = p->type;
  131. p++, q++;
  132. }
  133. if (lasttype == EXPR_UNKNOWN) {
  134. return finishtemp();
  135. }
  136. }
  137. while (p->type && (preserve || p->type < EXPR_SEGBASE + SEG_ABS)) {
  138. addtotemp(p->type, p->value);
  139. p++;
  140. }
  141. while (q->type && (preserve || q->type < EXPR_SEGBASE + SEG_ABS)) {
  142. addtotemp(q->type, q->value);
  143. q++;
  144. }
  145. return finishtemp();
  146. }
  147. /*
  148. * Multiply a vector by a scalar. Strip far-absolute segment part
  149. * if present.
  150. *
  151. * Explicit treatment of UNKNOWN is not required in this routine,
  152. * since it will silently do the Right Thing anyway.
  153. *
  154. * If `affect_hints' is set, we also change the hint type to
  155. * NOTBASE if a MAKEBASE hint points at a register being
  156. * multiplied. This allows [eax*1+ebx] to hint EBX rather than EAX
  157. * as the base register.
  158. */
  159. static expr *scalar_mult(expr * vect, int64_t scalar, int affect_hints)
  160. {
  161. expr *p = vect;
  162. while (p->type && p->type < EXPR_SEGBASE + SEG_ABS) {
  163. p->value = scalar * (p->value);
  164. if (hint && hint->type == EAH_MAKEBASE &&
  165. p->type == hint->base && affect_hints)
  166. hint->type = EAH_NOTBASE;
  167. p++;
  168. }
  169. p->type = 0;
  170. return vect;
  171. }
  172. static expr *scalarvect(int64_t scalar)
  173. {
  174. begintemp();
  175. addtotemp(EXPR_SIMPLE, scalar);
  176. return finishtemp();
  177. }
  178. static expr *unknown_expr(void)
  179. {
  180. begintemp();
  181. addtotemp(EXPR_UNKNOWN, 1L);
  182. return finishtemp();
  183. }
  184. /*
  185. * The SEG operator: calculate the segment part of a relocatable
  186. * value. Return NULL, as usual, if an error occurs. Report the
  187. * error too.
  188. */
  189. static expr *segment_part(expr * e)
  190. {
  191. int32_t seg;
  192. if (is_unknown(e))
  193. return unknown_expr();
  194. if (!is_reloc(e)) {
  195. nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
  196. return NULL;
  197. }
  198. seg = reloc_seg(e);
  199. if (seg == NO_SEG) {
  200. nasm_error(ERR_NONFATAL, "cannot apply SEG to a non-relocatable value");
  201. return NULL;
  202. } else if (seg & SEG_ABS) {
  203. return scalarvect(seg & ~SEG_ABS);
  204. } else if (seg & 1) {
  205. nasm_error(ERR_NONFATAL, "SEG applied to something which"
  206. " is already a segment base");
  207. return NULL;
  208. } else {
  209. int32_t base = ofmt->segbase(seg + 1);
  210. begintemp();
  211. addtotemp((base == NO_SEG ? EXPR_UNKNOWN : EXPR_SEGBASE + base),
  212. 1L);
  213. return finishtemp();
  214. }
  215. }
  216. /*
  217. * Recursive-descent parser. Called with a single boolean operand,
  218. * which is true if the evaluation is critical (i.e. unresolved
  219. * symbols are an error condition). Must update the global `i' to
  220. * reflect the token after the parsed string. May return NULL.
  221. *
  222. * evaluate() should report its own errors: on return it is assumed
  223. * that if NULL has been returned, the error has already been
  224. * reported.
  225. */
  226. /*
  227. * Grammar parsed is:
  228. *
  229. * expr : bexpr [ WRT expr6 ]
  230. * bexpr : rexp0 or expr0 depending on relative-mode setting
  231. * rexp0 : rexp1 [ {||} rexp1...]
  232. * rexp1 : rexp2 [ {^^} rexp2...]
  233. * rexp2 : rexp3 [ {&&} rexp3...]
  234. * rexp3 : expr0 [ {=,==,<>,!=,<,>,<=,>=} expr0 ]
  235. * expr0 : expr1 [ {|} expr1...]
  236. * expr1 : expr2 [ {^} expr2...]
  237. * expr2 : expr3 [ {&} expr3...]
  238. * expr3 : expr4 [ {<<,>>} expr4...]
  239. * expr4 : expr5 [ {+,-} expr5...]
  240. * expr5 : expr6 [ {*,/,%,//,%%} expr6...]
  241. * expr6 : { ~,+,-,IFUNC,SEG } expr6
  242. * | (bexpr)
  243. * | symbol
  244. * | $
  245. * | number
  246. */
  247. static expr *rexp0(int), *rexp1(int), *rexp2(int), *rexp3(int);
  248. static expr *expr0(int), *expr1(int), *expr2(int), *expr3(int);
  249. static expr *expr4(int), *expr5(int), *expr6(int);
  250. static expr *(*bexpr) (int);
  251. static expr *rexp0(int critical)
  252. {
  253. expr *e, *f;
  254. e = rexp1(critical);
  255. if (!e)
  256. return NULL;
  257. while (i == TOKEN_DBL_OR) {
  258. i = scan(scpriv, tokval);
  259. f = rexp1(critical);
  260. if (!f)
  261. return NULL;
  262. if (!(is_simple(e) || is_just_unknown(e)) ||
  263. !(is_simple(f) || is_just_unknown(f))) {
  264. nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
  265. " scalar values");
  266. }
  267. if (is_just_unknown(e) || is_just_unknown(f))
  268. e = unknown_expr();
  269. else
  270. e = scalarvect((int64_t)(reloc_value(e) || reloc_value(f)));
  271. }
  272. return e;
  273. }
  274. static expr *rexp1(int critical)
  275. {
  276. expr *e, *f;
  277. e = rexp2(critical);
  278. if (!e)
  279. return NULL;
  280. while (i == TOKEN_DBL_XOR) {
  281. i = scan(scpriv, tokval);
  282. f = rexp2(critical);
  283. if (!f)
  284. return NULL;
  285. if (!(is_simple(e) || is_just_unknown(e)) ||
  286. !(is_simple(f) || is_just_unknown(f))) {
  287. nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
  288. " scalar values");
  289. }
  290. if (is_just_unknown(e) || is_just_unknown(f))
  291. e = unknown_expr();
  292. else
  293. e = scalarvect((int64_t)(!reloc_value(e) ^ !reloc_value(f)));
  294. }
  295. return e;
  296. }
  297. static expr *rexp2(int critical)
  298. {
  299. expr *e, *f;
  300. e = rexp3(critical);
  301. if (!e)
  302. return NULL;
  303. while (i == TOKEN_DBL_AND) {
  304. i = scan(scpriv, tokval);
  305. f = rexp3(critical);
  306. if (!f)
  307. return NULL;
  308. if (!(is_simple(e) || is_just_unknown(e)) ||
  309. !(is_simple(f) || is_just_unknown(f))) {
  310. nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
  311. " scalar values");
  312. }
  313. if (is_just_unknown(e) || is_just_unknown(f))
  314. e = unknown_expr();
  315. else
  316. e = scalarvect((int64_t)(reloc_value(e) && reloc_value(f)));
  317. }
  318. return e;
  319. }
  320. static expr *rexp3(int critical)
  321. {
  322. expr *e, *f;
  323. int64_t v;
  324. e = expr0(critical);
  325. if (!e)
  326. return NULL;
  327. while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT ||
  328. i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) {
  329. int j = i;
  330. i = scan(scpriv, tokval);
  331. f = expr0(critical);
  332. if (!f)
  333. return NULL;
  334. e = add_vectors(e, scalar_mult(f, -1L, false));
  335. switch (j) {
  336. case TOKEN_EQ:
  337. case TOKEN_NE:
  338. if (is_unknown(e))
  339. v = -1; /* means unknown */
  340. else if (!is_really_simple(e) || reloc_value(e) != 0)
  341. v = (j == TOKEN_NE); /* unequal, so return true if NE */
  342. else
  343. v = (j == TOKEN_EQ); /* equal, so return true if EQ */
  344. break;
  345. default:
  346. if (is_unknown(e))
  347. v = -1; /* means unknown */
  348. else if (!is_really_simple(e)) {
  349. nasm_error(ERR_NONFATAL,
  350. "`%s': operands differ by a non-scalar",
  351. (j == TOKEN_LE ? "<=" : j == TOKEN_LT ? "<" : j ==
  352. TOKEN_GE ? ">=" : ">"));
  353. v = 0; /* must set it to _something_ */
  354. } else {
  355. int64_t vv = reloc_value(e);
  356. if (vv == 0)
  357. v = (j == TOKEN_LE || j == TOKEN_GE);
  358. else if (vv > 0)
  359. v = (j == TOKEN_GE || j == TOKEN_GT);
  360. else /* vv < 0 */
  361. v = (j == TOKEN_LE || j == TOKEN_LT);
  362. }
  363. break;
  364. }
  365. if (v == -1)
  366. e = unknown_expr();
  367. else
  368. e = scalarvect(v);
  369. }
  370. return e;
  371. }
  372. static expr *expr0(int critical)
  373. {
  374. expr *e, *f;
  375. e = expr1(critical);
  376. if (!e)
  377. return NULL;
  378. while (i == '|') {
  379. i = scan(scpriv, tokval);
  380. f = expr1(critical);
  381. if (!f)
  382. return NULL;
  383. if (!(is_simple(e) || is_just_unknown(e)) ||
  384. !(is_simple(f) || is_just_unknown(f))) {
  385. nasm_error(ERR_NONFATAL, "`|' operator may only be applied to"
  386. " scalar values");
  387. }
  388. if (is_just_unknown(e) || is_just_unknown(f))
  389. e = unknown_expr();
  390. else
  391. e = scalarvect(reloc_value(e) | reloc_value(f));
  392. }
  393. return e;
  394. }
  395. static expr *expr1(int critical)
  396. {
  397. expr *e, *f;
  398. e = expr2(critical);
  399. if (!e)
  400. return NULL;
  401. while (i == '^') {
  402. i = scan(scpriv, tokval);
  403. f = expr2(critical);
  404. if (!f)
  405. return NULL;
  406. if (!(is_simple(e) || is_just_unknown(e)) ||
  407. !(is_simple(f) || is_just_unknown(f))) {
  408. nasm_error(ERR_NONFATAL, "`^' operator may only be applied to"
  409. " scalar values");
  410. }
  411. if (is_just_unknown(e) || is_just_unknown(f))
  412. e = unknown_expr();
  413. else
  414. e = scalarvect(reloc_value(e) ^ reloc_value(f));
  415. }
  416. return e;
  417. }
  418. static expr *expr2(int critical)
  419. {
  420. expr *e, *f;
  421. e = expr3(critical);
  422. if (!e)
  423. return NULL;
  424. while (i == '&') {
  425. i = scan(scpriv, tokval);
  426. f = expr3(critical);
  427. if (!f)
  428. return NULL;
  429. if (!(is_simple(e) || is_just_unknown(e)) ||
  430. !(is_simple(f) || is_just_unknown(f))) {
  431. nasm_error(ERR_NONFATAL, "`&' operator may only be applied to"
  432. " scalar values");
  433. }
  434. if (is_just_unknown(e) || is_just_unknown(f))
  435. e = unknown_expr();
  436. else
  437. e = scalarvect(reloc_value(e) & reloc_value(f));
  438. }
  439. return e;
  440. }
  441. static expr *expr3(int critical)
  442. {
  443. expr *e, *f;
  444. e = expr4(critical);
  445. if (!e)
  446. return NULL;
  447. while (i == TOKEN_SHL || i == TOKEN_SHR) {
  448. int j = i;
  449. i = scan(scpriv, tokval);
  450. f = expr4(critical);
  451. if (!f)
  452. return NULL;
  453. if (!(is_simple(e) || is_just_unknown(e)) ||
  454. !(is_simple(f) || is_just_unknown(f))) {
  455. nasm_error(ERR_NONFATAL, "shift operator may only be applied to"
  456. " scalar values");
  457. } else if (is_just_unknown(e) || is_just_unknown(f)) {
  458. e = unknown_expr();
  459. } else
  460. switch (j) {
  461. case TOKEN_SHL:
  462. e = scalarvect(reloc_value(e) << reloc_value(f));
  463. break;
  464. case TOKEN_SHR:
  465. e = scalarvect(((uint64_t)reloc_value(e)) >>
  466. reloc_value(f));
  467. break;
  468. }
  469. }
  470. return e;
  471. }
  472. static expr *expr4(int critical)
  473. {
  474. expr *e, *f;
  475. e = expr5(critical);
  476. if (!e)
  477. return NULL;
  478. while (i == '+' || i == '-') {
  479. int j = i;
  480. i = scan(scpriv, tokval);
  481. f = expr5(critical);
  482. if (!f)
  483. return NULL;
  484. switch (j) {
  485. case '+':
  486. e = add_vectors(e, f);
  487. break;
  488. case '-':
  489. e = add_vectors(e, scalar_mult(f, -1L, false));
  490. break;
  491. }
  492. }
  493. return e;
  494. }
  495. static expr *expr5(int critical)
  496. {
  497. expr *e, *f;
  498. e = expr6(critical);
  499. if (!e)
  500. return NULL;
  501. while (i == '*' || i == '/' || i == '%' ||
  502. i == TOKEN_SDIV || i == TOKEN_SMOD) {
  503. int j = i;
  504. i = scan(scpriv, tokval);
  505. f = expr6(critical);
  506. if (!f)
  507. return NULL;
  508. if (j != '*' && (!(is_simple(e) || is_just_unknown(e)) ||
  509. !(is_simple(f) || is_just_unknown(f)))) {
  510. nasm_error(ERR_NONFATAL, "division operator may only be applied to"
  511. " scalar values");
  512. return NULL;
  513. }
  514. if (j != '*' && !is_just_unknown(f) && reloc_value(f) == 0) {
  515. nasm_error(ERR_NONFATAL, "division by zero");
  516. return NULL;
  517. }
  518. switch (j) {
  519. case '*':
  520. if (is_simple(e))
  521. e = scalar_mult(f, reloc_value(e), true);
  522. else if (is_simple(f))
  523. e = scalar_mult(e, reloc_value(f), true);
  524. else if (is_just_unknown(e) && is_just_unknown(f))
  525. e = unknown_expr();
  526. else {
  527. nasm_error(ERR_NONFATAL, "unable to multiply two "
  528. "non-scalar objects");
  529. return NULL;
  530. }
  531. break;
  532. case '/':
  533. if (is_just_unknown(e) || is_just_unknown(f))
  534. e = unknown_expr();
  535. else
  536. e = scalarvect(((uint64_t)reloc_value(e)) /
  537. ((uint64_t)reloc_value(f)));
  538. break;
  539. case '%':
  540. if (is_just_unknown(e) || is_just_unknown(f))
  541. e = unknown_expr();
  542. else
  543. e = scalarvect(((uint64_t)reloc_value(e)) %
  544. ((uint64_t)reloc_value(f)));
  545. break;
  546. case TOKEN_SDIV:
  547. if (is_just_unknown(e) || is_just_unknown(f))
  548. e = unknown_expr();
  549. else
  550. e = scalarvect(((int64_t)reloc_value(e)) /
  551. ((int64_t)reloc_value(f)));
  552. break;
  553. case TOKEN_SMOD:
  554. if (is_just_unknown(e) || is_just_unknown(f))
  555. e = unknown_expr();
  556. else
  557. e = scalarvect(((int64_t)reloc_value(e)) %
  558. ((int64_t)reloc_value(f)));
  559. break;
  560. }
  561. }
  562. return e;
  563. }
  564. static expr *eval_floatize(enum floatize type)
  565. {
  566. uint8_t result[16], *p; /* Up to 128 bits */
  567. static const struct {
  568. int bytes, start, len;
  569. } formats[] = {
  570. { 1, 0, 1 }, /* FLOAT_8 */
  571. { 2, 0, 2 }, /* FLOAT_16 */
  572. { 4, 0, 4 }, /* FLOAT_32 */
  573. { 8, 0, 8 }, /* FLOAT_64 */
  574. { 10, 0, 8 }, /* FLOAT_80M */
  575. { 10, 8, 2 }, /* FLOAT_80E */
  576. { 16, 0, 8 }, /* FLOAT_128L */
  577. { 16, 8, 8 }, /* FLOAT_128H */
  578. };
  579. int sign = 1;
  580. int64_t val;
  581. int j;
  582. i = scan(scpriv, tokval);
  583. if (i != '(') {
  584. nasm_error(ERR_NONFATAL, "expecting `('");
  585. return NULL;
  586. }
  587. i = scan(scpriv, tokval);
  588. if (i == '-' || i == '+') {
  589. sign = (i == '-') ? -1 : 1;
  590. i = scan(scpriv, tokval);
  591. }
  592. if (i != TOKEN_FLOAT) {
  593. nasm_error(ERR_NONFATAL, "expecting floating-point number");
  594. return NULL;
  595. }
  596. if (!float_const(tokval->t_charptr, sign, result, formats[type].bytes))
  597. return NULL;
  598. i = scan(scpriv, tokval);
  599. if (i != ')') {
  600. nasm_error(ERR_NONFATAL, "expecting `)'");
  601. return NULL;
  602. }
  603. p = result+formats[type].start+formats[type].len;
  604. val = 0;
  605. for (j = formats[type].len; j; j--) {
  606. p--;
  607. val = (val << 8) + *p;
  608. }
  609. begintemp();
  610. addtotemp(EXPR_SIMPLE, val);
  611. i = scan(scpriv, tokval);
  612. return finishtemp();
  613. }
  614. static expr *eval_strfunc(enum strfunc type)
  615. {
  616. char *string;
  617. size_t string_len;
  618. int64_t val;
  619. bool parens, rn_warn;
  620. parens = false;
  621. i = scan(scpriv, tokval);
  622. if (i == '(') {
  623. parens = true;
  624. i = scan(scpriv, tokval);
  625. }
  626. if (i != TOKEN_STR) {
  627. nasm_error(ERR_NONFATAL, "expecting string");
  628. return NULL;
  629. }
  630. string_len = string_transform(tokval->t_charptr, tokval->t_inttwo,
  631. &string, type);
  632. if (string_len == (size_t)-1) {
  633. nasm_error(ERR_NONFATAL, "invalid string for transform");
  634. return NULL;
  635. }
  636. val = readstrnum(string, string_len, &rn_warn);
  637. if (parens) {
  638. i = scan(scpriv, tokval);
  639. if (i != ')') {
  640. nasm_error(ERR_NONFATAL, "expecting `)'");
  641. return NULL;
  642. }
  643. }
  644. if (rn_warn)
  645. nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
  646. begintemp();
  647. addtotemp(EXPR_SIMPLE, val);
  648. i = scan(scpriv, tokval);
  649. return finishtemp();
  650. }
  651. static int64_t eval_ifunc(int64_t val, enum ifunc func)
  652. {
  653. int errtype;
  654. uint64_t uval = (uint64_t)val;
  655. int64_t rv;
  656. switch (func) {
  657. case IFUNC_ILOG2E:
  658. case IFUNC_ILOG2W:
  659. errtype = (func == IFUNC_ILOG2E) ? ERR_NONFATAL : ERR_WARNING;
  660. if (!is_power2(uval))
  661. nasm_error(errtype, "ilog2 argument is not a power of two");
  662. /* fall through */
  663. case IFUNC_ILOG2F:
  664. rv = ilog2_64(uval);
  665. break;
  666. case IFUNC_ILOG2C:
  667. rv = (uval < 2) ? 0 : ilog2_64(uval-1) + 1;
  668. break;
  669. default:
  670. nasm_panic(0, "invalid IFUNC token %d", func);
  671. rv = 0;
  672. break;
  673. }
  674. return rv;
  675. }
  676. static expr *expr6(int critical)
  677. {
  678. int32_t type;
  679. expr *e;
  680. int32_t label_seg;
  681. int64_t label_ofs;
  682. int64_t tmpval;
  683. bool rn_warn;
  684. const char *scope;
  685. if (++deadman > nasm_limit[LIMIT_EVAL]) {
  686. nasm_error(ERR_NONFATAL, "expression too long");
  687. return NULL;
  688. }
  689. switch (i) {
  690. case '-':
  691. i = scan(scpriv, tokval);
  692. e = expr6(critical);
  693. if (!e)
  694. return NULL;
  695. return scalar_mult(e, -1L, false);
  696. case '+':
  697. i = scan(scpriv, tokval);
  698. return expr6(critical);
  699. case '~':
  700. i = scan(scpriv, tokval);
  701. e = expr6(critical);
  702. if (!e)
  703. return NULL;
  704. if (is_just_unknown(e))
  705. return unknown_expr();
  706. else if (!is_simple(e)) {
  707. nasm_error(ERR_NONFATAL, "`~' operator may only be applied to"
  708. " scalar values");
  709. return NULL;
  710. }
  711. return scalarvect(~reloc_value(e));
  712. case '!':
  713. i = scan(scpriv, tokval);
  714. e = expr6(critical);
  715. if (!e)
  716. return NULL;
  717. if (is_just_unknown(e))
  718. return unknown_expr();
  719. else if (!is_simple(e)) {
  720. nasm_error(ERR_NONFATAL, "`!' operator may only be applied to"
  721. " scalar values");
  722. return NULL;
  723. }
  724. return scalarvect(!reloc_value(e));
  725. case TOKEN_IFUNC:
  726. {
  727. enum ifunc func = tokval->t_integer;
  728. i = scan(scpriv, tokval);
  729. e = expr6(critical);
  730. if (!e)
  731. return NULL;
  732. if (is_just_unknown(e))
  733. return unknown_expr();
  734. else if (!is_simple(e)) {
  735. nasm_error(ERR_NONFATAL, "function may only be applied to"
  736. " scalar values");
  737. return NULL;
  738. }
  739. return scalarvect(eval_ifunc(reloc_value(e), func));
  740. }
  741. case TOKEN_SEG:
  742. i = scan(scpriv, tokval);
  743. e = expr6(critical);
  744. if (!e)
  745. return NULL;
  746. e = segment_part(e);
  747. if (!e)
  748. return NULL;
  749. if (is_unknown(e) && critical) {
  750. nasm_error(ERR_NONFATAL, "unable to determine segment base");
  751. return NULL;
  752. }
  753. return e;
  754. case TOKEN_FLOATIZE:
  755. return eval_floatize(tokval->t_integer);
  756. case TOKEN_STRFUNC:
  757. return eval_strfunc(tokval->t_integer);
  758. case '(':
  759. i = scan(scpriv, tokval);
  760. e = bexpr(critical);
  761. if (!e)
  762. return NULL;
  763. if (i != ')') {
  764. nasm_error(ERR_NONFATAL, "expecting `)'");
  765. return NULL;
  766. }
  767. i = scan(scpriv, tokval);
  768. return e;
  769. case TOKEN_NUM:
  770. case TOKEN_STR:
  771. case TOKEN_REG:
  772. case TOKEN_ID:
  773. case TOKEN_INSN: /* Opcodes that occur here are really labels */
  774. case TOKEN_HERE:
  775. case TOKEN_BASE:
  776. case TOKEN_DECORATOR:
  777. begintemp();
  778. switch (i) {
  779. case TOKEN_NUM:
  780. addtotemp(EXPR_SIMPLE, tokval->t_integer);
  781. break;
  782. case TOKEN_STR:
  783. tmpval = readstrnum(tokval->t_charptr, tokval->t_inttwo, &rn_warn);
  784. if (rn_warn)
  785. nasm_error(ERR_WARNING|ERR_PASS1, "character constant too long");
  786. addtotemp(EXPR_SIMPLE, tmpval);
  787. break;
  788. case TOKEN_REG:
  789. addtotemp(tokval->t_integer, 1L);
  790. if (hint && hint->type == EAH_NOHINT)
  791. hint->base = tokval->t_integer, hint->type = EAH_MAKEBASE;
  792. break;
  793. case TOKEN_ID:
  794. case TOKEN_INSN:
  795. case TOKEN_HERE:
  796. case TOKEN_BASE:
  797. /*
  798. * If !location.known, this indicates that no
  799. * symbol, Here or Base references are valid because we
  800. * are in preprocess-only mode.
  801. */
  802. if (!location.known) {
  803. nasm_error(ERR_NONFATAL,
  804. "%s not supported in preprocess-only mode",
  805. (i == TOKEN_HERE ? "`$'" :
  806. i == TOKEN_BASE ? "`$$'" :
  807. "symbol references"));
  808. addtotemp(EXPR_UNKNOWN, 1L);
  809. break;
  810. }
  811. type = EXPR_SIMPLE; /* might get overridden by UNKNOWN */
  812. if (i == TOKEN_BASE) {
  813. label_seg = in_absolute ? absolute.segment : location.segment;
  814. label_ofs = 0;
  815. } else if (i == TOKEN_HERE) {
  816. label_seg = in_absolute ? absolute.segment : location.segment;
  817. label_ofs = in_absolute ? absolute.offset : location.offset;
  818. } else {
  819. if (!lookup_label(tokval->t_charptr, &label_seg, &label_ofs)) {
  820. scope = local_scope(tokval->t_charptr);
  821. if (critical == 2) {
  822. nasm_error(ERR_NONFATAL, "symbol `%s%s' undefined",
  823. scope,tokval->t_charptr);
  824. return NULL;
  825. } else if (critical == 1) {
  826. nasm_error(ERR_NONFATAL,
  827. "symbol `%s%s' not defined before use",
  828. scope,tokval->t_charptr);
  829. return NULL;
  830. } else {
  831. if (opflags)
  832. *opflags |= OPFLAG_FORWARD;
  833. type = EXPR_UNKNOWN;
  834. label_seg = NO_SEG;
  835. label_ofs = 1;
  836. }
  837. }
  838. if (opflags && is_extern(tokval->t_charptr))
  839. *opflags |= OPFLAG_EXTERN;
  840. }
  841. addtotemp(type, label_ofs);
  842. if (label_seg != NO_SEG)
  843. addtotemp(EXPR_SEGBASE + label_seg, 1L);
  844. break;
  845. case TOKEN_DECORATOR:
  846. addtotemp(EXPR_RDSAE, tokval->t_integer);
  847. break;
  848. }
  849. i = scan(scpriv, tokval);
  850. return finishtemp();
  851. default:
  852. nasm_error(ERR_NONFATAL, "expression syntax error");
  853. return NULL;
  854. }
  855. }
  856. expr *evaluate(scanner sc, void *scprivate, struct tokenval *tv,
  857. int *fwref, int critical, struct eval_hints *hints)
  858. {
  859. expr *e;
  860. expr *f = NULL;
  861. deadman = 0;
  862. hint = hints;
  863. if (hint)
  864. hint->type = EAH_NOHINT;
  865. if (critical & CRITICAL) {
  866. critical &= ~CRITICAL;
  867. bexpr = rexp0;
  868. } else
  869. bexpr = expr0;
  870. scan = sc;
  871. scpriv = scprivate;
  872. tokval = tv;
  873. opflags = fwref;
  874. if (tokval->t_type == TOKEN_INVALID)
  875. i = scan(scpriv, tokval);
  876. else
  877. i = tokval->t_type;
  878. while (ntempexprs) /* initialize temporary storage */
  879. nasm_free(tempexprs[--ntempexprs]);
  880. e = bexpr(critical);
  881. if (!e)
  882. return NULL;
  883. if (i == TOKEN_WRT) {
  884. i = scan(scpriv, tokval); /* eat the WRT */
  885. f = expr6(critical);
  886. if (!f)
  887. return NULL;
  888. }
  889. e = scalar_mult(e, 1L, false); /* strip far-absolute segment part */
  890. if (f) {
  891. expr *g;
  892. if (is_just_unknown(f))
  893. g = unknown_expr();
  894. else {
  895. int64_t value;
  896. begintemp();
  897. if (!is_reloc(f)) {
  898. nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
  899. return NULL;
  900. }
  901. value = reloc_seg(f);
  902. if (value == NO_SEG)
  903. value = reloc_value(f) | SEG_ABS;
  904. else if (!(value & SEG_ABS) && !(value % 2) && critical) {
  905. nasm_error(ERR_NONFATAL, "invalid right-hand operand to WRT");
  906. return NULL;
  907. }
  908. addtotemp(EXPR_WRT, value);
  909. g = finishtemp();
  910. }
  911. e = add_vectors(e, g);
  912. }
  913. return e;
  914. }