x86expr.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /*
  2. * x86 expression handling
  3. *
  4. * Copyright (C) 2001-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #include <libyasm.h>
  29. #include "x86arch.h"
  30. typedef struct x86_checkea_reg3264_data {
  31. int *regs; /* total multiplier for each reg */
  32. unsigned char vsib_mode;
  33. unsigned char bits;
  34. unsigned char addrsize;
  35. } x86_checkea_reg3264_data;
  36. /* Only works if ei->type == EXPR_REG (doesn't check).
  37. * Overwrites ei with intnum of 0 (to eliminate regs from the final expr).
  38. */
  39. static /*@null@*/ /*@dependent@*/ int *
  40. x86_expr_checkea_get_reg3264(yasm_expr__item *ei, int *regnum,
  41. /*returned*/ void *d)
  42. {
  43. x86_checkea_reg3264_data *data = d;
  44. switch ((x86_expritem_reg_size)(ei->data.reg & ~0xFUL)) {
  45. case X86_REG32:
  46. if (data->addrsize != 32)
  47. return 0;
  48. *regnum = (unsigned int)(ei->data.reg & 0xF);
  49. break;
  50. case X86_REG64:
  51. if (data->addrsize != 64)
  52. return 0;
  53. *regnum = (unsigned int)(ei->data.reg & 0xF);
  54. break;
  55. case X86_XMMREG:
  56. if (data->vsib_mode != 1)
  57. return 0;
  58. if (data->bits != 64 && (ei->data.reg & 0x8) == 0x8)
  59. return 0;
  60. *regnum = 17+(unsigned int)(ei->data.reg & 0xF);
  61. break;
  62. case X86_YMMREG:
  63. if (data->vsib_mode != 2)
  64. return 0;
  65. if (data->bits != 64 && (ei->data.reg & 0x8) == 0x8)
  66. return 0;
  67. *regnum = 17+(unsigned int)(ei->data.reg & 0xF);
  68. break;
  69. case X86_RIP:
  70. if (data->bits != 64)
  71. return 0;
  72. *regnum = 16;
  73. break;
  74. default:
  75. return 0;
  76. }
  77. /* overwrite with 0 to eliminate register from displacement expr */
  78. ei->type = YASM_EXPR_INT;
  79. ei->data.intn = yasm_intnum_create_uint(0);
  80. /* we're okay */
  81. return &data->regs[*regnum];
  82. }
  83. typedef struct x86_checkea_reg16_data {
  84. int bx, si, di, bp; /* total multiplier for each reg */
  85. } x86_checkea_reg16_data;
  86. /* Only works if ei->type == EXPR_REG (doesn't check).
  87. * Overwrites ei with intnum of 0 (to eliminate regs from the final expr).
  88. */
  89. static /*@null@*/ int *
  90. x86_expr_checkea_get_reg16(yasm_expr__item *ei, int *regnum, void *d)
  91. {
  92. x86_checkea_reg16_data *data = d;
  93. /* in order: ax,cx,dx,bx,sp,bp,si,di */
  94. /*@-nullassign@*/
  95. static int *reg16[8] = {0,0,0,0,0,0,0,0};
  96. /*@=nullassign@*/
  97. reg16[3] = &data->bx;
  98. reg16[5] = &data->bp;
  99. reg16[6] = &data->si;
  100. reg16[7] = &data->di;
  101. /* don't allow 32-bit registers */
  102. if ((ei->data.reg & ~0xFUL) != X86_REG16)
  103. return 0;
  104. /* & 7 for sanity check */
  105. *regnum = (unsigned int)(ei->data.reg & 0x7);
  106. /* only allow BX, SI, DI, BP */
  107. if (!reg16[*regnum])
  108. return 0;
  109. /* overwrite with 0 to eliminate register from displacement expr */
  110. ei->type = YASM_EXPR_INT;
  111. ei->data.intn = yasm_intnum_create_uint(0);
  112. /* we're okay */
  113. return reg16[*regnum];
  114. }
  115. /* Distribute over registers to help bring them to the topmost level of e.
  116. * Also check for illegal operations against registers.
  117. * Returns 0 if something was illegal, 1 if legal and nothing in e changed,
  118. * and 2 if legal and e needs to be simplified.
  119. *
  120. * Only half joking: Someday make this/checkea able to accept crazy things
  121. * like: (bx+di)*(bx+di)-bx*bx-2*bx*di-di*di+di? Probably not: NASM never
  122. * accepted such things, and it's doubtful such an expn is valid anyway
  123. * (even though the above one is). But even macros would be hard-pressed
  124. * to generate something like this.
  125. *
  126. * e must already have been simplified for this function to work properly
  127. * (as it doesn't think things like SUB are valid).
  128. *
  129. * IMPLEMENTATION NOTE: About the only thing this function really needs to
  130. * "distribute" is: (non-float-expn or intnum) * (sum expn of registers).
  131. *
  132. * TODO: Clean up this code, make it easier to understand.
  133. */
  134. static int
  135. x86_expr_checkea_distcheck_reg(yasm_expr **ep, unsigned int bits)
  136. {
  137. yasm_expr *e = *ep;
  138. int i;
  139. int havereg = -1, havereg_expr = -1;
  140. int retval = 1; /* default to legal, no changes */
  141. for (i=0; i<e->numterms; i++) {
  142. switch (e->terms[i].type) {
  143. case YASM_EXPR_REG:
  144. /* Check op to make sure it's valid to use w/register. */
  145. switch (e->op) {
  146. case YASM_EXPR_MUL:
  147. /* Check for reg*reg */
  148. if (havereg != -1)
  149. return 0;
  150. break;
  151. case YASM_EXPR_ADD:
  152. case YASM_EXPR_IDENT:
  153. break;
  154. default:
  155. return 0;
  156. }
  157. havereg = i;
  158. break;
  159. case YASM_EXPR_FLOAT:
  160. /* Floats not allowed. */
  161. return 0;
  162. case YASM_EXPR_EXPR:
  163. if (yasm_expr__contains(e->terms[i].data.expn,
  164. YASM_EXPR_REG)) {
  165. int ret2;
  166. /* Check op to make sure it's valid to use w/register. */
  167. if (e->op != YASM_EXPR_ADD && e->op != YASM_EXPR_MUL)
  168. return 0;
  169. /* Check for reg*reg */
  170. if (e->op == YASM_EXPR_MUL && havereg != -1)
  171. return 0;
  172. havereg = i;
  173. havereg_expr = i;
  174. /* Recurse to check lower levels */
  175. ret2 =
  176. x86_expr_checkea_distcheck_reg(&e->terms[i].data.expn,
  177. bits);
  178. if (ret2 == 0)
  179. return 0;
  180. if (ret2 == 2)
  181. retval = 2;
  182. } else if (yasm_expr__contains(e->terms[i].data.expn,
  183. YASM_EXPR_FLOAT))
  184. return 0; /* Disallow floats */
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. /* just exit if no registers were used */
  191. if (havereg == -1)
  192. return retval;
  193. /* Distribute */
  194. if (e->op == YASM_EXPR_MUL && havereg_expr != -1) {
  195. yasm_expr *ne;
  196. retval = 2; /* we're going to change it */
  197. /* The reg expn *must* be EXPR_ADD at this point. Sanity check. */
  198. if (e->terms[havereg_expr].type != YASM_EXPR_EXPR ||
  199. e->terms[havereg_expr].data.expn->op != YASM_EXPR_ADD)
  200. yasm_internal_error(N_("Register expression not ADD or EXPN"));
  201. /* Iterate over each term in reg expn */
  202. for (i=0; i<e->terms[havereg_expr].data.expn->numterms; i++) {
  203. /* Copy everything EXCEPT havereg_expr term into new expression */
  204. ne = yasm_expr__copy_except(e, havereg_expr);
  205. assert(ne != NULL);
  206. /* Copy reg expr term into uncopied (empty) term in new expn */
  207. ne->terms[havereg_expr] =
  208. e->terms[havereg_expr].data.expn->terms[i]; /* struct copy */
  209. /* Overwrite old reg expr term with new expn */
  210. e->terms[havereg_expr].data.expn->terms[i].type = YASM_EXPR_EXPR;
  211. e->terms[havereg_expr].data.expn->terms[i].data.expn = ne;
  212. }
  213. /* Replace e with expanded reg expn */
  214. ne = e->terms[havereg_expr].data.expn;
  215. e->terms[havereg_expr].type = YASM_EXPR_NONE; /* don't delete it! */
  216. yasm_expr_destroy(e); /* but everything else */
  217. e = ne;
  218. /*@-onlytrans@*/
  219. *ep = ne;
  220. /*@=onlytrans@*/
  221. }
  222. return retval;
  223. }
  224. /* Simplify and determine if expression is superficially valid:
  225. * Valid expr should be [(int-equiv expn)]+[reg*(int-equiv expn)+...]
  226. * where the [...] parts are optional.
  227. *
  228. * Don't simplify out constant identities if we're looking for an indexreg: we
  229. * may need the multiplier for determining what the indexreg is!
  230. *
  231. * Returns 1 if invalid register usage, 2 if unable to determine all values,
  232. * and 0 if all values successfully determined and saved in data.
  233. */
  234. static int
  235. x86_expr_checkea_getregusage(yasm_expr **ep, /*@null@*/ int *indexreg,
  236. int *pcrel, unsigned int bits, void *data,
  237. int *(*get_reg)(yasm_expr__item *ei, int *regnum, void *d))
  238. {
  239. int i;
  240. int *reg;
  241. int regnum;
  242. int indexval = 0;
  243. int indexmult = 0;
  244. yasm_expr *e, *wrt;
  245. /*@-unqualifiedtrans@*/
  246. *ep = yasm_expr__level_tree(*ep, 1, 1, indexreg == 0, 0, NULL, NULL);
  247. /* Check for WRT rip first */
  248. wrt = yasm_expr_extract_wrt(ep);
  249. if (wrt && wrt->op == YASM_EXPR_IDENT &&
  250. wrt->terms[0].type == YASM_EXPR_REG) {
  251. if (bits != 64) { /* only valid in 64-bit mode */
  252. yasm_expr_destroy(wrt);
  253. return 1;
  254. }
  255. reg = get_reg(&wrt->terms[0], &regnum, data);
  256. if (!reg || regnum != 16) { /* only accept rip */
  257. yasm_expr_destroy(wrt);
  258. return 1;
  259. }
  260. (*reg)++;
  261. /* Delete WRT. Set pcrel to 1 to indicate to x86
  262. * bytecode code to do PC-relative displacement transform.
  263. */
  264. *pcrel = 1;
  265. yasm_expr_destroy(wrt);
  266. } else if (wrt) {
  267. yasm_expr_destroy(wrt);
  268. return 1;
  269. }
  270. /*@=unqualifiedtrans@*/
  271. assert(*ep != NULL);
  272. e = *ep;
  273. switch (x86_expr_checkea_distcheck_reg(ep, bits)) {
  274. case 0:
  275. return 1;
  276. case 2:
  277. /* Need to simplify again */
  278. *ep = yasm_expr__level_tree(*ep, 1, 1, indexreg == 0, 0, NULL,
  279. NULL);
  280. e = *ep;
  281. break;
  282. default:
  283. break;
  284. }
  285. switch (e->op) {
  286. case YASM_EXPR_ADD:
  287. /* Prescan for non-int multipliers against a reg.
  288. * This is invalid due to the optimizer structure.
  289. */
  290. for (i=0; i<e->numterms; i++)
  291. if (e->terms[i].type == YASM_EXPR_EXPR) {
  292. yasm_expr__order_terms(e->terms[i].data.expn);
  293. if (e->terms[i].data.expn->terms[0].type ==
  294. YASM_EXPR_REG) {
  295. if (e->terms[i].data.expn->numterms > 2)
  296. return 1;
  297. if (e->terms[i].data.expn->terms[1].type !=
  298. YASM_EXPR_INT)
  299. return 1;
  300. }
  301. }
  302. /*@fallthrough@*/
  303. case YASM_EXPR_IDENT:
  304. /* Check each term for register (and possible multiplier). */
  305. for (i=0; i<e->numterms; i++) {
  306. if (e->terms[i].type == YASM_EXPR_REG) {
  307. reg = get_reg(&e->terms[i], &regnum, data);
  308. if (!reg)
  309. return 1;
  310. (*reg)++;
  311. /* Let last, largest multipler win indexreg */
  312. if (indexreg && *reg > 0 && indexval <= *reg &&
  313. !indexmult) {
  314. *indexreg = regnum;
  315. indexval = *reg;
  316. }
  317. } else if (e->terms[i].type == YASM_EXPR_EXPR) {
  318. /* Already ordered from ADD above, just grab the value.
  319. * Sanity check for EXPR_INT.
  320. */
  321. if (e->terms[i].data.expn->terms[0].type ==
  322. YASM_EXPR_REG) {
  323. long delta;
  324. if (e->terms[i].data.expn->terms[1].type !=
  325. YASM_EXPR_INT)
  326. yasm_internal_error(
  327. N_("Non-integer value in reg expn"));
  328. reg = get_reg(&e->terms[i].data.expn->terms[0],
  329. &regnum, data);
  330. if (!reg)
  331. return 1;
  332. delta = yasm_intnum_get_int(
  333. e->terms[i].data.expn->terms[1].data.intn);
  334. (*reg) += delta;
  335. /* Let last, largest multipler win indexreg */
  336. if (indexreg && delta > 0 && indexval <= *reg) {
  337. *indexreg = regnum;
  338. indexval = *reg;
  339. indexmult = 1;
  340. } else if (indexreg && *indexreg == regnum &&
  341. delta < 0 && *reg <= 1) {
  342. *indexreg = -1;
  343. indexval = 0;
  344. indexmult = 0;
  345. }
  346. }
  347. }
  348. }
  349. break;
  350. case YASM_EXPR_MUL:
  351. /* Here, too, check for non-int multipliers against a reg. */
  352. yasm_expr__order_terms(e);
  353. if (e->terms[0].type == YASM_EXPR_REG) {
  354. long delta;
  355. if (e->numterms > 2)
  356. return 1;
  357. if (e->terms[1].type != YASM_EXPR_INT)
  358. return 1;
  359. reg = get_reg(&e->terms[0], &regnum, data);
  360. if (!reg)
  361. return 1;
  362. delta = yasm_intnum_get_int(e->terms[1].data.intn);
  363. (*reg) += delta;
  364. if (indexreg)
  365. {
  366. if (delta < 0 && *reg <= 1)
  367. {
  368. *indexreg = -1;
  369. indexval = 0;
  370. indexmult = 0;
  371. }
  372. else
  373. *indexreg = regnum;
  374. }
  375. }
  376. break;
  377. case YASM_EXPR_SEGOFF:
  378. /* No registers are allowed on either side. */
  379. if (yasm_expr__contains(e, YASM_EXPR_REG))
  380. return 1;
  381. break;
  382. default:
  383. /* Should never get here! */
  384. yasm_internal_error(N_("unexpected expr op"));
  385. }
  386. /* Simplify expr, which is now really just the displacement. This
  387. * should get rid of the 0's we put in for registers in the callback.
  388. */
  389. *ep = yasm_expr_simplify(*ep, 0);
  390. /* e = *ep; */
  391. return 0;
  392. }
  393. /* Calculate the displacement length, if possible.
  394. * Takes several extra inputs so it can be used by both 32-bit and 16-bit
  395. * expressions:
  396. * wordsize=16 for 16-bit, =32 for 32-bit.
  397. * noreg=1 if the *ModRM byte* has no registers used.
  398. * dispreq=1 if a displacement value is *required* (even if =0).
  399. * Returns 0 if successfully calculated, 1 if not.
  400. */
  401. /*@-nullstate@*/
  402. static int
  403. x86_checkea_calc_displen(x86_effaddr *x86_ea, unsigned int wordsize, int noreg,
  404. int dispreq)
  405. {
  406. /*@null@*/ /*@only@*/ yasm_intnum *num;
  407. x86_ea->valid_modrm = 0; /* default to not yet valid */
  408. switch (x86_ea->ea.disp.size) {
  409. case 0:
  410. break;
  411. /* If not 0, the displacement length was forced; set the Mod bits
  412. * appropriately and we're done with the ModRM byte.
  413. */
  414. case 8:
  415. /* Byte is only a valid override if there are registers in the
  416. * EA. With no registers, we must have a 16/32 value.
  417. */
  418. if (noreg) {
  419. yasm_warn_set(YASM_WARN_IMPLICIT_SIZE_OVERRIDE,
  420. N_("invalid displacement size; fixed"));
  421. x86_ea->ea.disp.size = wordsize;
  422. } else
  423. x86_ea->modrm |= 0100;
  424. x86_ea->valid_modrm = 1;
  425. return 0;
  426. case 16:
  427. case 32:
  428. /* Don't allow changing displacement different from BITS setting
  429. * directly; require an address-size override to change it.
  430. */
  431. if (wordsize != x86_ea->ea.disp.size) {
  432. yasm_error_set(YASM_ERROR_VALUE,
  433. N_("invalid effective address (displacement size)"));
  434. return 1;
  435. }
  436. if (!noreg)
  437. x86_ea->modrm |= 0200;
  438. x86_ea->valid_modrm = 1;
  439. return 0;
  440. default:
  441. /* we shouldn't ever get any other size! */
  442. yasm_internal_error(N_("strange EA displacement size"));
  443. }
  444. /* The displacement length hasn't been forced (or the forcing wasn't
  445. * valid), try to determine what it is.
  446. */
  447. if (noreg) {
  448. /* No register in ModRM expression, so it must be disp16/32,
  449. * and as the Mod bits are set to 0 by the caller, we're done
  450. * with the ModRM byte.
  451. */
  452. x86_ea->ea.disp.size = wordsize;
  453. x86_ea->valid_modrm = 1;
  454. return 0;
  455. }
  456. if (dispreq) {
  457. /* for BP/EBP, there *must* be a displacement value, but we
  458. * may not know the size (8 or 16/32) for sure right now.
  459. */
  460. x86_ea->ea.need_nonzero_len = 1;
  461. }
  462. if (x86_ea->ea.disp.rel) {
  463. /* Relative displacement; basically all object formats need non-byte
  464. * for relocation here, so just do that. (TODO: handle this
  465. * differently?)
  466. */
  467. x86_ea->ea.disp.size = wordsize;
  468. x86_ea->modrm |= 0200;
  469. x86_ea->valid_modrm = 1;
  470. return 0;
  471. }
  472. /* At this point there's 3 possibilities for the displacement:
  473. * - None (if =0)
  474. * - signed 8 bit (if in -128 to 127 range)
  475. * - 16/32 bit (word size)
  476. * For now, check intnum value right now; if it's not 0,
  477. * assume 8 bit and set up for allowing 16 bit later.
  478. * FIXME: The complex expression equaling zero is probably a rare case,
  479. * so we ignore it for now.
  480. */
  481. num = yasm_value_get_intnum(&x86_ea->ea.disp, NULL, 0);
  482. if (!num) {
  483. /* Still has unknown values. */
  484. x86_ea->ea.need_nonzero_len = 1;
  485. x86_ea->modrm |= 0100;
  486. x86_ea->valid_modrm = 1;
  487. return 0;
  488. }
  489. /* Figure out what size displacement we will have. */
  490. if (yasm_intnum_is_zero(num) && !x86_ea->ea.need_nonzero_len) {
  491. /* If we know that the displacement is 0 right now,
  492. * go ahead and delete the expr and make it so no
  493. * displacement value is included in the output.
  494. * The Mod bits of ModRM are set to 0 above, and
  495. * we're done with the ModRM byte!
  496. */
  497. yasm_value_delete(&x86_ea->ea.disp);
  498. x86_ea->ea.need_disp = 0;
  499. } else if (yasm_intnum_in_range(num, -128, 127)) {
  500. /* It fits into a signed byte */
  501. x86_ea->ea.disp.size = 8;
  502. x86_ea->modrm |= 0100;
  503. } else {
  504. /* It's a 16/32-bit displacement */
  505. x86_ea->ea.disp.size = wordsize;
  506. x86_ea->modrm |= 0200;
  507. }
  508. x86_ea->valid_modrm = 1; /* We're done with ModRM */
  509. yasm_intnum_destroy(num);
  510. return 0;
  511. }
  512. /*@=nullstate@*/
  513. static int
  514. x86_expr_checkea_getregsize_callback(yasm_expr__item *ei, void *d)
  515. {
  516. unsigned char *addrsize = (unsigned char *)d;
  517. if (ei->type == YASM_EXPR_REG) {
  518. switch ((x86_expritem_reg_size)(ei->data.reg & ~0xFUL)) {
  519. case X86_REG16:
  520. *addrsize = 16;
  521. break;
  522. case X86_REG32:
  523. *addrsize = 32;
  524. break;
  525. case X86_REG64:
  526. case X86_RIP:
  527. *addrsize = 64;
  528. break;
  529. default:
  530. return 0;
  531. }
  532. return 1;
  533. } else
  534. return 0;
  535. }
  536. int
  537. yasm_x86__expr_checkea(x86_effaddr *x86_ea, unsigned char *addrsize,
  538. unsigned int bits, int address16_op, unsigned char *rex,
  539. yasm_bytecode *bc)
  540. {
  541. int retval;
  542. if (*addrsize == 0) {
  543. /* we need to figure out the address size from what we know about:
  544. * - the displacement length
  545. * - what registers are used in the expression
  546. * - the bits setting
  547. */
  548. switch (x86_ea->ea.disp.size) {
  549. case 16:
  550. /* must be 16-bit */
  551. *addrsize = 16;
  552. break;
  553. case 64:
  554. /* We have to support this for the MemOffs case, but it's
  555. * otherwise illegal. It's also illegal in non-64-bit mode.
  556. */
  557. if (x86_ea->need_modrm || x86_ea->need_sib) {
  558. yasm_error_set(YASM_ERROR_VALUE,
  559. N_("invalid effective address (displacement size)"));
  560. return 1;
  561. }
  562. *addrsize = 64;
  563. break;
  564. case 32:
  565. /* Must be 32-bit in 16-bit or 32-bit modes. In 64-bit mode,
  566. * we don't know unless we look at the registers, except in the
  567. * MemOffs case (see the end of this function).
  568. */
  569. if (bits != 64 || (!x86_ea->need_modrm && !x86_ea->need_sib)) {
  570. *addrsize = 32;
  571. break;
  572. }
  573. /*@fallthrough@*/
  574. default:
  575. /* If SIB is required, but we're in 16-bit mode, set to 32. */
  576. if (bits == 16 && x86_ea->need_sib == 1) {
  577. *addrsize = 32;
  578. break;
  579. }
  580. /* check for use of 16 or 32-bit registers; if none are used
  581. * default to bits setting.
  582. */
  583. if (!x86_ea->ea.disp.abs ||
  584. !yasm_expr__traverse_leaves_in(x86_ea->ea.disp.abs,
  585. addrsize, x86_expr_checkea_getregsize_callback))
  586. *addrsize = bits;
  587. /* TODO: Add optional warning here if switched address size
  588. * from bits setting just by register use.. eg [ax] in
  589. * 32-bit mode would generate a warning.
  590. */
  591. }
  592. }
  593. if ((*addrsize == 32 || *addrsize == 64) &&
  594. ((x86_ea->need_modrm && !x86_ea->valid_modrm) ||
  595. (x86_ea->need_sib && !x86_ea->valid_sib))) {
  596. int i;
  597. unsigned char low3;
  598. enum {
  599. REG3264_NONE = -1,
  600. REG3264_EAX = 0,
  601. REG3264_ECX,
  602. REG3264_EDX,
  603. REG3264_EBX,
  604. REG3264_ESP,
  605. REG3264_EBP,
  606. REG3264_ESI,
  607. REG3264_EDI,
  608. REG64_R8,
  609. REG64_R9,
  610. REG64_R10,
  611. REG64_R11,
  612. REG64_R12,
  613. REG64_R13,
  614. REG64_R14,
  615. REG64_R15,
  616. REG64_RIP,
  617. SIMDREGS
  618. };
  619. int reg3264mult[33] =
  620. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  621. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  622. x86_checkea_reg3264_data reg3264_data;
  623. int basereg = REG3264_NONE; /* "base" register (for SIB) */
  624. int indexreg = REG3264_NONE; /* "index" register (for SIB) */
  625. int regcount = 17; /* normally don't check SIMD regs */
  626. if (x86_ea->vsib_mode != 0)
  627. regcount = 33;
  628. /* We can only do 64-bit addresses in 64-bit mode. */
  629. if (*addrsize == 64 && bits != 64) {
  630. yasm_error_set(YASM_ERROR_TYPE,
  631. N_("invalid effective address (64-bit in non-64-bit mode)"));
  632. return 1;
  633. }
  634. if (x86_ea->ea.pc_rel && bits != 64) {
  635. yasm_warn_set(YASM_WARN_GENERAL,
  636. N_("RIP-relative directive ignored in non-64-bit mode"));
  637. x86_ea->ea.pc_rel = 0;
  638. }
  639. reg3264_data.regs = reg3264mult;
  640. reg3264_data.vsib_mode = x86_ea->vsib_mode;
  641. reg3264_data.bits = bits;
  642. reg3264_data.addrsize = *addrsize;
  643. if (x86_ea->ea.disp.abs) {
  644. int pcrel = 0;
  645. switch (x86_expr_checkea_getregusage
  646. (&x86_ea->ea.disp.abs, &indexreg, &pcrel, bits,
  647. &reg3264_data, x86_expr_checkea_get_reg3264)) {
  648. case 1:
  649. yasm_error_set(YASM_ERROR_VALUE,
  650. N_("invalid effective address"));
  651. return 1;
  652. case 2:
  653. if (pcrel)
  654. yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1);
  655. return 2;
  656. default:
  657. if (pcrel)
  658. yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1);
  659. break;
  660. }
  661. }
  662. /* If indexreg mult is 0, discard it.
  663. * This is possible because of the way indexreg is found in
  664. * expr_checkea_getregusage().
  665. */
  666. if (indexreg != REG3264_NONE && reg3264mult[indexreg] == 0)
  667. indexreg = REG3264_NONE;
  668. /* Find a basereg (*1, but not indexreg), if there is one.
  669. * Also, if an indexreg hasn't been assigned, try to find one.
  670. * Meanwhile, check to make sure there's no negative register mults.
  671. */
  672. for (i=0; i<regcount; i++) {
  673. if (reg3264mult[i] < 0) {
  674. yasm_error_set(YASM_ERROR_VALUE,
  675. N_("invalid effective address"));
  676. return 1;
  677. }
  678. if (i != indexreg && reg3264mult[i] == 1 &&
  679. basereg == REG3264_NONE)
  680. basereg = i;
  681. else if (indexreg == REG3264_NONE && reg3264mult[i] > 0)
  682. indexreg = i;
  683. }
  684. if (x86_ea->vsib_mode != 0) {
  685. /* For VSIB, the SIMD register needs to go into the indexreg.
  686. * Also check basereg (must be a GPR if present) and indexreg
  687. * (must be a SIMD register).
  688. */
  689. if (basereg >= SIMDREGS &&
  690. (indexreg == REG3264_NONE || reg3264mult[indexreg] == 1)) {
  691. int temp = basereg;
  692. basereg = indexreg;
  693. indexreg = temp;
  694. }
  695. if (basereg >= REG64_RIP || indexreg < SIMDREGS) {
  696. yasm_error_set(YASM_ERROR_VALUE,
  697. N_("invalid effective address"));
  698. return 1;
  699. }
  700. } else if (indexreg != REG3264_NONE && basereg == REG3264_NONE)
  701. /* Handle certain special cases of indexreg mults when basereg is
  702. * empty.
  703. */
  704. switch (reg3264mult[indexreg]) {
  705. case 1:
  706. /* Only optimize this way if nosplit wasn't specified */
  707. if (!x86_ea->ea.nosplit) {
  708. basereg = indexreg;
  709. indexreg = -1;
  710. }
  711. break;
  712. case 2:
  713. /* Only split if nosplit wasn't specified */
  714. if (!x86_ea->ea.nosplit) {
  715. basereg = indexreg;
  716. reg3264mult[indexreg] = 1;
  717. }
  718. break;
  719. case 3:
  720. case 5:
  721. case 9:
  722. basereg = indexreg;
  723. reg3264mult[indexreg]--;
  724. break;
  725. }
  726. /* Make sure there's no other registers than the basereg and indexreg
  727. * we just found.
  728. */
  729. for (i=0; i<regcount; i++)
  730. if (i != basereg && i != indexreg && reg3264mult[i] != 0) {
  731. yasm_error_set(YASM_ERROR_VALUE,
  732. N_("invalid effective address"));
  733. return 1;
  734. }
  735. /* Check the index multiplier value for validity if present. */
  736. if (indexreg != REG3264_NONE && reg3264mult[indexreg] != 1 &&
  737. reg3264mult[indexreg] != 2 && reg3264mult[indexreg] != 4 &&
  738. reg3264mult[indexreg] != 8) {
  739. yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address"));
  740. return 1;
  741. }
  742. /* ESP is not a legal indexreg. */
  743. if (indexreg == REG3264_ESP) {
  744. /* If mult>1 or basereg is ESP also, there's no way to make it
  745. * legal.
  746. */
  747. if (reg3264mult[REG3264_ESP] > 1 || basereg == REG3264_ESP) {
  748. yasm_error_set(YASM_ERROR_VALUE,
  749. N_("invalid effective address"));
  750. return 1;
  751. }
  752. /* If mult==1 and basereg is not ESP, swap indexreg w/basereg. */
  753. indexreg = basereg;
  754. basereg = REG3264_ESP;
  755. }
  756. /* RIP is only legal if it's the ONLY register used. */
  757. if (indexreg == REG64_RIP ||
  758. (basereg == REG64_RIP && indexreg != REG3264_NONE)) {
  759. yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address"));
  760. return 1;
  761. }
  762. /* At this point, we know the base and index registers and that the
  763. * memory expression is (essentially) valid. Now build the ModRM and
  764. * (optional) SIB bytes.
  765. */
  766. /* If we're supposed to be RIP-relative and there's no register
  767. * usage, change to RIP-relative.
  768. */
  769. if (basereg == REG3264_NONE && indexreg == REG3264_NONE &&
  770. x86_ea->ea.pc_rel) {
  771. basereg = REG64_RIP;
  772. yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1);
  773. }
  774. /* First determine R/M (Mod is later determined from disp size) */
  775. x86_ea->need_modrm = 1; /* we always need ModRM */
  776. if (basereg == REG3264_NONE && indexreg == REG3264_NONE) {
  777. /* Just a disp32: in 64-bit mode the RM encoding is used for RIP
  778. * offset addressing, so we need to use the SIB form instead.
  779. */
  780. if (bits == 64) {
  781. x86_ea->modrm |= 4;
  782. x86_ea->need_sib = 1;
  783. } else {
  784. x86_ea->modrm |= 5;
  785. x86_ea->sib = 0;
  786. x86_ea->valid_sib = 0;
  787. x86_ea->need_sib = 0;
  788. }
  789. } else if (basereg == REG64_RIP) {
  790. x86_ea->modrm |= 5;
  791. x86_ea->sib = 0;
  792. x86_ea->valid_sib = 0;
  793. x86_ea->need_sib = 0;
  794. /* RIP always requires a 32-bit displacement */
  795. x86_ea->valid_modrm = 1;
  796. x86_ea->ea.disp.size = 32;
  797. return 0;
  798. } else if (indexreg == REG3264_NONE) {
  799. /* basereg only */
  800. /* Don't need to go to the full effort of determining what type
  801. * of register basereg is, as x86_set_rex_from_reg doesn't pay
  802. * much attention.
  803. */
  804. if (yasm_x86__set_rex_from_reg(rex, &low3,
  805. (unsigned int)(X86_REG64 | basereg),
  806. bits, X86_REX_B))
  807. return 1;
  808. x86_ea->modrm |= low3;
  809. /* we don't need an SIB *unless* basereg is ESP or R12 */
  810. if (basereg == REG3264_ESP || basereg == REG64_R12)
  811. x86_ea->need_sib = 1;
  812. else {
  813. x86_ea->sib = 0;
  814. x86_ea->valid_sib = 0;
  815. x86_ea->need_sib = 0;
  816. }
  817. } else {
  818. /* index or both base and index */
  819. x86_ea->modrm |= 4;
  820. x86_ea->need_sib = 1;
  821. }
  822. /* Determine SIB if needed */
  823. if (x86_ea->need_sib == 1) {
  824. x86_ea->sib = 0; /* start with 0 */
  825. /* Special case: no basereg */
  826. if (basereg == REG3264_NONE)
  827. x86_ea->sib |= 5;
  828. else {
  829. if (yasm_x86__set_rex_from_reg(rex, &low3, (unsigned int)
  830. (X86_REG64 | basereg), bits,
  831. X86_REX_B))
  832. return 1;
  833. x86_ea->sib |= low3;
  834. }
  835. /* Put in indexreg, checking for none case */
  836. if (indexreg == REG3264_NONE)
  837. x86_ea->sib |= 040;
  838. /* Any scale field is valid, just leave at 0. */
  839. else {
  840. if (indexreg >= SIMDREGS) {
  841. if (yasm_x86__set_rex_from_reg(rex, &low3,
  842. (unsigned int)(X86_XMMREG | (indexreg-SIMDREGS)),
  843. bits, X86_REX_X))
  844. return 1;
  845. } else {
  846. if (yasm_x86__set_rex_from_reg(rex, &low3,
  847. (unsigned int)(X86_REG64 | indexreg),
  848. bits, X86_REX_X))
  849. return 1;
  850. }
  851. x86_ea->sib |= low3 << 3;
  852. /* Set scale field, 1 case -> 0, so don't bother. */
  853. switch (reg3264mult[indexreg]) {
  854. case 2:
  855. x86_ea->sib |= 0100;
  856. break;
  857. case 4:
  858. x86_ea->sib |= 0200;
  859. break;
  860. case 8:
  861. x86_ea->sib |= 0300;
  862. break;
  863. }
  864. }
  865. x86_ea->valid_sib = 1; /* Done with SIB */
  866. }
  867. /* Calculate displacement length (if possible) */
  868. retval = x86_checkea_calc_displen
  869. (x86_ea, 32, basereg == REG3264_NONE,
  870. basereg == REG3264_EBP || basereg == REG64_R13);
  871. return retval;
  872. } else if (*addrsize == 16 && x86_ea->need_modrm && !x86_ea->valid_modrm) {
  873. static const unsigned char modrm16[16] = {
  874. 0006 /* disp16 */, 0007 /* [BX] */, 0004 /* [SI] */,
  875. 0000 /* [BX+SI] */, 0005 /* [DI] */, 0001 /* [BX+DI] */,
  876. 0377 /* invalid */, 0377 /* invalid */, 0006 /* [BP]+d */,
  877. 0377 /* invalid */, 0002 /* [BP+SI] */, 0377 /* invalid */,
  878. 0003 /* [BP+DI] */, 0377 /* invalid */, 0377 /* invalid */,
  879. 0377 /* invalid */
  880. };
  881. x86_checkea_reg16_data reg16mult = {0, 0, 0, 0};
  882. enum {
  883. HAVE_NONE = 0,
  884. HAVE_BX = 1<<0,
  885. HAVE_SI = 1<<1,
  886. HAVE_DI = 1<<2,
  887. HAVE_BP = 1<<3
  888. } havereg = HAVE_NONE;
  889. /* 64-bit mode does not allow 16-bit addresses */
  890. if (bits == 64 && !address16_op) {
  891. yasm_error_set(YASM_ERROR_TYPE,
  892. N_("16-bit addresses not supported in 64-bit mode"));
  893. return 1;
  894. }
  895. /* 16-bit cannot have SIB */
  896. x86_ea->sib = 0;
  897. x86_ea->valid_sib = 0;
  898. x86_ea->need_sib = 0;
  899. if (x86_ea->ea.disp.abs) {
  900. int pcrel = 0;
  901. switch (x86_expr_checkea_getregusage
  902. (&x86_ea->ea.disp.abs, (int *)NULL, &pcrel, bits,
  903. &reg16mult, x86_expr_checkea_get_reg16)) {
  904. case 1:
  905. yasm_error_set(YASM_ERROR_VALUE,
  906. N_("invalid effective address"));
  907. return 1;
  908. case 2:
  909. if (pcrel)
  910. yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1);
  911. return 2;
  912. default:
  913. if (pcrel)
  914. yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1);
  915. break;
  916. }
  917. }
  918. /* reg multipliers not 0 or 1 are illegal. */
  919. if (reg16mult.bx & ~1 || reg16mult.si & ~1 || reg16mult.di & ~1 ||
  920. reg16mult.bp & ~1) {
  921. yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address"));
  922. return 1;
  923. }
  924. /* Set havereg appropriately */
  925. if (reg16mult.bx > 0)
  926. havereg |= HAVE_BX;
  927. if (reg16mult.si > 0)
  928. havereg |= HAVE_SI;
  929. if (reg16mult.di > 0)
  930. havereg |= HAVE_DI;
  931. if (reg16mult.bp > 0)
  932. havereg |= HAVE_BP;
  933. /* Check the modrm value for invalid combinations. */
  934. if (modrm16[havereg] & 0070) {
  935. yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address"));
  936. return 1;
  937. }
  938. /* Set ModRM byte for registers */
  939. x86_ea->modrm |= modrm16[havereg];
  940. /* Calculate displacement length (if possible) */
  941. retval = x86_checkea_calc_displen
  942. (x86_ea, 16, havereg == HAVE_NONE, havereg == HAVE_BP);
  943. return retval;
  944. } else if (!x86_ea->need_modrm && !x86_ea->need_sib) {
  945. /* Special case for MOV MemOffs opcode: displacement but no modrm. */
  946. switch (*addrsize) {
  947. case 64:
  948. if (bits != 64) {
  949. yasm_error_set(YASM_ERROR_TYPE,
  950. N_("invalid effective address (64-bit in non-64-bit mode)"));
  951. return 1;
  952. }
  953. x86_ea->ea.disp.size = 64;
  954. break;
  955. case 32:
  956. x86_ea->ea.disp.size = 32;
  957. break;
  958. case 16:
  959. /* 64-bit mode does not allow 16-bit addresses */
  960. if (bits == 64 && !address16_op) {
  961. yasm_error_set(YASM_ERROR_TYPE,
  962. N_("16-bit addresses not supported in 64-bit mode"));
  963. return 1;
  964. }
  965. x86_ea->ea.disp.size = 16;
  966. break;
  967. }
  968. }
  969. return 0;
  970. }
  971. int
  972. yasm_x86__floatnum_tobytes(yasm_arch *arch, const yasm_floatnum *flt,
  973. unsigned char *buf, size_t destsize, size_t valsize,
  974. size_t shift, int warn)
  975. {
  976. if (!yasm_floatnum_check_size(flt, valsize)) {
  977. yasm_error_set(YASM_ERROR_FLOATING_POINT,
  978. N_("invalid floating point constant size"));
  979. return 1;
  980. }
  981. yasm_floatnum_get_sized(flt, buf, destsize, valsize, shift, 0, warn);
  982. return 0;
  983. }