intnum.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * Integer number functions.
  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 <ctype.h>
  29. #include <limits.h>
  30. #include "coretype.h"
  31. #include "bitvect.h"
  32. #include "file.h"
  33. #include "errwarn.h"
  34. #include "intnum.h"
  35. /* "Native" "word" size for intnum calculations. */
  36. #define BITVECT_NATIVE_SIZE 256
  37. struct yasm_intnum {
  38. union val {
  39. long l; /* integer value (for integers <32 bits) */
  40. wordptr bv; /* bit vector (for integers >=32 bits) */
  41. } val;
  42. enum { INTNUM_L, INTNUM_BV } type;
  43. };
  44. /* static bitvect used for conversions */
  45. static /*@only@*/ wordptr conv_bv;
  46. /* static bitvects used for computation */
  47. static /*@only@*/ wordptr result, spare, op1static, op2static;
  48. static /*@only@*/ BitVector_from_Dec_static_data *from_dec_data;
  49. void
  50. yasm_intnum_initialize(void)
  51. {
  52. conv_bv = BitVector_Create(BITVECT_NATIVE_SIZE, FALSE);
  53. result = BitVector_Create(BITVECT_NATIVE_SIZE, FALSE);
  54. spare = BitVector_Create(BITVECT_NATIVE_SIZE, FALSE);
  55. op1static = BitVector_Create(BITVECT_NATIVE_SIZE, FALSE);
  56. op2static = BitVector_Create(BITVECT_NATIVE_SIZE, FALSE);
  57. from_dec_data = BitVector_from_Dec_static_Boot(BITVECT_NATIVE_SIZE);
  58. }
  59. void
  60. yasm_intnum_cleanup(void)
  61. {
  62. BitVector_from_Dec_static_Shutdown(from_dec_data);
  63. BitVector_Destroy(op2static);
  64. BitVector_Destroy(op1static);
  65. BitVector_Destroy(spare);
  66. BitVector_Destroy(result);
  67. BitVector_Destroy(conv_bv);
  68. }
  69. /* Compress a bitvector into intnum storage.
  70. * If saved as a bitvector, clones the passed bitvector.
  71. * Can modify the passed bitvector.
  72. */
  73. static void
  74. intnum_frombv(/*@out@*/ yasm_intnum *intn, wordptr bv)
  75. {
  76. if (Set_Max(bv) < 31) {
  77. intn->type = INTNUM_L;
  78. intn->val.l = (long)BitVector_Chunk_Read(bv, 31, 0);
  79. } else if (BitVector_msb_(bv)) {
  80. /* Negative, negate and see if we'll fit into a long. */
  81. unsigned long ul;
  82. BitVector_Negate(bv, bv);
  83. if (Set_Max(bv) >= 32 ||
  84. ((ul = BitVector_Chunk_Read(bv, 32, 0)) & 0x80000000)) {
  85. /* too negative */
  86. BitVector_Negate(bv, bv);
  87. intn->type = INTNUM_BV;
  88. intn->val.bv = BitVector_Clone(bv);
  89. } else {
  90. intn->type = INTNUM_L;
  91. intn->val.l = -((long)ul);
  92. }
  93. } else {
  94. intn->type = INTNUM_BV;
  95. intn->val.bv = BitVector_Clone(bv);
  96. }
  97. }
  98. /* If intnum is a BV, returns its bitvector directly.
  99. * If not, converts into passed bv and returns that instead.
  100. */
  101. static wordptr
  102. intnum_tobv(/*@returned@*/ wordptr bv, const yasm_intnum *intn)
  103. {
  104. if (intn->type == INTNUM_BV)
  105. return intn->val.bv;
  106. BitVector_Empty(bv);
  107. if (intn->val.l >= 0)
  108. BitVector_Chunk_Store(bv, 32, 0, (unsigned long)intn->val.l);
  109. else {
  110. BitVector_Chunk_Store(bv, 32, 0, (unsigned long)-intn->val.l);
  111. BitVector_Negate(bv, bv);
  112. }
  113. return bv;
  114. }
  115. yasm_intnum *
  116. yasm_intnum_create_dec(char *str)
  117. {
  118. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  119. switch (BitVector_from_Dec_static(from_dec_data, conv_bv,
  120. (unsigned char *)str)) {
  121. case ErrCode_Pars:
  122. yasm_error_set(YASM_ERROR_VALUE, N_("invalid decimal literal"));
  123. break;
  124. case ErrCode_Ovfl:
  125. yasm_error_set(YASM_ERROR_OVERFLOW,
  126. N_("Numeric constant too large for internal format"));
  127. break;
  128. default:
  129. break;
  130. }
  131. intnum_frombv(intn, conv_bv);
  132. return intn;
  133. }
  134. yasm_intnum *
  135. yasm_intnum_create_bin(char *str)
  136. {
  137. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  138. switch (BitVector_from_Bin(conv_bv, (unsigned char *)str)) {
  139. case ErrCode_Pars:
  140. yasm_error_set(YASM_ERROR_VALUE, N_("invalid binary literal"));
  141. break;
  142. case ErrCode_Ovfl:
  143. yasm_error_set(YASM_ERROR_OVERFLOW,
  144. N_("Numeric constant too large for internal format"));
  145. break;
  146. default:
  147. break;
  148. }
  149. intnum_frombv(intn, conv_bv);
  150. return intn;
  151. }
  152. yasm_intnum *
  153. yasm_intnum_create_oct(char *str)
  154. {
  155. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  156. switch (BitVector_from_Oct(conv_bv, (unsigned char *)str)) {
  157. case ErrCode_Pars:
  158. yasm_error_set(YASM_ERROR_VALUE, N_("invalid octal literal"));
  159. break;
  160. case ErrCode_Ovfl:
  161. yasm_error_set(YASM_ERROR_OVERFLOW,
  162. N_("Numeric constant too large for internal format"));
  163. break;
  164. default:
  165. break;
  166. }
  167. intnum_frombv(intn, conv_bv);
  168. return intn;
  169. }
  170. yasm_intnum *
  171. yasm_intnum_create_hex(char *str)
  172. {
  173. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  174. switch (BitVector_from_Hex(conv_bv, (unsigned char *)str)) {
  175. case ErrCode_Pars:
  176. yasm_error_set(YASM_ERROR_VALUE, N_("invalid hex literal"));
  177. break;
  178. case ErrCode_Ovfl:
  179. yasm_error_set(YASM_ERROR_OVERFLOW,
  180. N_("Numeric constant too large for internal format"));
  181. break;
  182. default:
  183. break;
  184. }
  185. intnum_frombv(intn, conv_bv);
  186. return intn;
  187. }
  188. /*@-usedef -compdef -uniondef@*/
  189. yasm_intnum *
  190. yasm_intnum_create_charconst_nasm(const char *str)
  191. {
  192. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  193. size_t len = strlen(str);
  194. if(len*8 > BITVECT_NATIVE_SIZE)
  195. yasm_error_set(YASM_ERROR_OVERFLOW,
  196. N_("Character constant too large for internal format"));
  197. /* be conservative in choosing bitvect in case MSB is set */
  198. if (len > 3) {
  199. BitVector_Empty(conv_bv);
  200. intn->type = INTNUM_BV;
  201. } else {
  202. intn->val.l = 0;
  203. intn->type = INTNUM_L;
  204. }
  205. switch (len) {
  206. case 3:
  207. intn->val.l |= ((unsigned long)str[2]) & 0xff;
  208. intn->val.l <<= 8;
  209. /*@fallthrough@*/
  210. case 2:
  211. intn->val.l |= ((unsigned long)str[1]) & 0xff;
  212. intn->val.l <<= 8;
  213. /*@fallthrough@*/
  214. case 1:
  215. intn->val.l |= ((unsigned long)str[0]) & 0xff;
  216. case 0:
  217. break;
  218. default:
  219. /* >=32 bit conversion */
  220. while (len) {
  221. BitVector_Move_Left(conv_bv, 8);
  222. BitVector_Chunk_Store(conv_bv, 8, 0,
  223. ((unsigned long)str[--len]) & 0xff);
  224. }
  225. intn->val.bv = BitVector_Clone(conv_bv);
  226. }
  227. return intn;
  228. }
  229. yasm_intnum *
  230. yasm_intnum_create_charconst_tasm(const char *str)
  231. {
  232. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  233. size_t len = strlen(str);
  234. size_t i;
  235. if(len*8 > BITVECT_NATIVE_SIZE)
  236. yasm_error_set(YASM_ERROR_OVERFLOW,
  237. N_("Character constant too large for internal format"));
  238. /* be conservative in choosing bitvect in case MSB is set */
  239. if (len > 3) {
  240. BitVector_Empty(conv_bv);
  241. intn->type = INTNUM_BV;
  242. } else {
  243. intn->val.l = 0;
  244. intn->type = INTNUM_L;
  245. }
  246. /* tasm uses big endian notation */
  247. i = 0;
  248. switch (len) {
  249. case 3:
  250. intn->val.l |= ((unsigned long)str[i++]) & 0xff;
  251. intn->val.l <<= 8;
  252. /*@fallthrough@*/
  253. case 2:
  254. intn->val.l |= ((unsigned long)str[i++]) & 0xff;
  255. intn->val.l <<= 8;
  256. /*@fallthrough@*/
  257. case 1:
  258. intn->val.l |= ((unsigned long)str[i++]) & 0xff;
  259. case 0:
  260. break;
  261. default:
  262. /* >=32 bit conversion */
  263. while (i < len) {
  264. BitVector_Chunk_Store(conv_bv, 8, (len-i-1)*8,
  265. ((unsigned long)str[i]) & 0xff);
  266. i++;
  267. }
  268. intn->val.bv = BitVector_Clone(conv_bv);
  269. }
  270. return intn;
  271. }
  272. /*@=usedef =compdef =uniondef@*/
  273. yasm_intnum *
  274. yasm_intnum_create_uint(unsigned long i)
  275. {
  276. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  277. if (i > LONG_MAX) {
  278. /* Too big, store as bitvector */
  279. intn->val.bv = BitVector_Create(BITVECT_NATIVE_SIZE, TRUE);
  280. intn->type = INTNUM_BV;
  281. BitVector_Chunk_Store(intn->val.bv, 32, 0, i);
  282. } else {
  283. intn->val.l = (long)i;
  284. intn->type = INTNUM_L;
  285. }
  286. return intn;
  287. }
  288. yasm_intnum *
  289. yasm_intnum_create_int(long i)
  290. {
  291. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  292. intn->val.l = i;
  293. intn->type = INTNUM_L;
  294. return intn;
  295. }
  296. yasm_intnum *
  297. yasm_intnum_create_leb128(const unsigned char *ptr, int sign,
  298. unsigned long *size)
  299. {
  300. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  301. const unsigned char *ptr_orig = ptr;
  302. unsigned long i = 0;
  303. BitVector_Empty(conv_bv);
  304. for (;;) {
  305. BitVector_Chunk_Store(conv_bv, 7, i, *ptr);
  306. i += 7;
  307. if ((*ptr & 0x80) != 0x80)
  308. break;
  309. ptr++;
  310. }
  311. *size = (unsigned long)(ptr-ptr_orig)+1;
  312. if(i > BITVECT_NATIVE_SIZE)
  313. yasm_error_set(YASM_ERROR_OVERFLOW,
  314. N_("Numeric constant too large for internal format"));
  315. else if (sign && (*ptr & 0x40) == 0x40)
  316. BitVector_Interval_Fill(conv_bv, i, BITVECT_NATIVE_SIZE-1);
  317. intnum_frombv(intn, conv_bv);
  318. return intn;
  319. }
  320. yasm_intnum *
  321. yasm_intnum_create_sized(unsigned char *ptr, int sign, size_t srcsize,
  322. int bigendian)
  323. {
  324. yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum));
  325. unsigned long i = 0;
  326. if (srcsize*8 > BITVECT_NATIVE_SIZE)
  327. yasm_error_set(YASM_ERROR_OVERFLOW,
  328. N_("Numeric constant too large for internal format"));
  329. /* Read the buffer into a bitvect */
  330. BitVector_Empty(conv_bv);
  331. if (bigendian) {
  332. /* TODO */
  333. yasm_internal_error(N_("big endian not implemented"));
  334. } else {
  335. for (i = 0; i < srcsize; i++)
  336. BitVector_Chunk_Store(conv_bv, 8, i*8, ptr[i]);
  337. }
  338. /* Sign extend if needed */
  339. if (srcsize*8 < BITVECT_NATIVE_SIZE && sign && (ptr[i-1] & 0x80) == 0x80)
  340. BitVector_Interval_Fill(conv_bv, i*8, BITVECT_NATIVE_SIZE-1);
  341. intnum_frombv(intn, conv_bv);
  342. return intn;
  343. }
  344. yasm_intnum *
  345. yasm_intnum_copy(const yasm_intnum *intn)
  346. {
  347. yasm_intnum *n = yasm_xmalloc(sizeof(yasm_intnum));
  348. switch (intn->type) {
  349. case INTNUM_L:
  350. n->val.l = intn->val.l;
  351. break;
  352. case INTNUM_BV:
  353. n->val.bv = BitVector_Clone(intn->val.bv);
  354. break;
  355. }
  356. n->type = intn->type;
  357. return n;
  358. }
  359. void
  360. yasm_intnum_destroy(yasm_intnum *intn)
  361. {
  362. if (intn->type == INTNUM_BV)
  363. BitVector_Destroy(intn->val.bv);
  364. yasm_xfree(intn);
  365. }
  366. /*@-nullderef -nullpass -branchstate@*/
  367. int
  368. yasm_intnum_calc(yasm_intnum *acc, yasm_expr_op op, yasm_intnum *operand)
  369. {
  370. boolean carry = 0;
  371. wordptr op1, op2 = NULL;
  372. N_int count;
  373. /* Always do computations with in full bit vector.
  374. * Bit vector results must be calculated through intermediate storage.
  375. */
  376. op1 = intnum_tobv(op1static, acc);
  377. if (operand)
  378. op2 = intnum_tobv(op2static, operand);
  379. if (!operand && op != YASM_EXPR_NEG && op != YASM_EXPR_NOT &&
  380. op != YASM_EXPR_LNOT) {
  381. yasm_error_set(YASM_ERROR_ARITHMETIC,
  382. N_("operation needs an operand"));
  383. BitVector_Empty(result);
  384. return 1;
  385. }
  386. /* A operation does a bitvector computation if result is allocated. */
  387. switch (op) {
  388. case YASM_EXPR_ADD:
  389. BitVector_add(result, op1, op2, &carry);
  390. break;
  391. case YASM_EXPR_SUB:
  392. BitVector_sub(result, op1, op2, &carry);
  393. break;
  394. case YASM_EXPR_MUL:
  395. BitVector_Multiply(result, op1, op2);
  396. break;
  397. case YASM_EXPR_DIV:
  398. /* TODO: make sure op1 and op2 are unsigned */
  399. if (BitVector_is_empty(op2)) {
  400. yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero"));
  401. BitVector_Empty(result);
  402. return 1;
  403. } else
  404. BitVector_Divide(result, op1, op2, spare);
  405. break;
  406. case YASM_EXPR_SIGNDIV:
  407. if (BitVector_is_empty(op2)) {
  408. yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero"));
  409. BitVector_Empty(result);
  410. return 1;
  411. } else
  412. BitVector_Divide(result, op1, op2, spare);
  413. break;
  414. case YASM_EXPR_MOD:
  415. /* TODO: make sure op1 and op2 are unsigned */
  416. if (BitVector_is_empty(op2)) {
  417. yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero"));
  418. BitVector_Empty(result);
  419. return 1;
  420. } else
  421. BitVector_Divide(spare, op1, op2, result);
  422. break;
  423. case YASM_EXPR_SIGNMOD:
  424. if (BitVector_is_empty(op2)) {
  425. yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero"));
  426. BitVector_Empty(result);
  427. return 1;
  428. } else
  429. BitVector_Divide(spare, op1, op2, result);
  430. break;
  431. case YASM_EXPR_NEG:
  432. BitVector_Negate(result, op1);
  433. break;
  434. case YASM_EXPR_NOT:
  435. Set_Complement(result, op1);
  436. break;
  437. case YASM_EXPR_OR:
  438. Set_Union(result, op1, op2);
  439. break;
  440. case YASM_EXPR_AND:
  441. Set_Intersection(result, op1, op2);
  442. break;
  443. case YASM_EXPR_XOR:
  444. Set_ExclusiveOr(result, op1, op2);
  445. break;
  446. case YASM_EXPR_XNOR:
  447. Set_ExclusiveOr(result, op1, op2);
  448. Set_Complement(result, result);
  449. break;
  450. case YASM_EXPR_NOR:
  451. Set_Union(result, op1, op2);
  452. Set_Complement(result, result);
  453. break;
  454. case YASM_EXPR_SHL:
  455. if (operand->type == INTNUM_L && operand->val.l >= 0) {
  456. BitVector_Copy(result, op1);
  457. BitVector_Move_Left(result, (N_int)operand->val.l);
  458. } else /* don't even bother, just zero result */
  459. BitVector_Empty(result);
  460. break;
  461. case YASM_EXPR_SHR:
  462. if (operand->type == INTNUM_L && operand->val.l >= 0) {
  463. BitVector_Copy(result, op1);
  464. carry = BitVector_msb_(op1);
  465. count = (N_int)operand->val.l;
  466. while (count-- > 0)
  467. BitVector_shift_right(result, carry);
  468. } else /* don't even bother, just zero result */
  469. BitVector_Empty(result);
  470. break;
  471. case YASM_EXPR_LOR:
  472. BitVector_Empty(result);
  473. BitVector_LSB(result, !BitVector_is_empty(op1) ||
  474. !BitVector_is_empty(op2));
  475. break;
  476. case YASM_EXPR_LAND:
  477. BitVector_Empty(result);
  478. BitVector_LSB(result, !BitVector_is_empty(op1) &&
  479. !BitVector_is_empty(op2));
  480. break;
  481. case YASM_EXPR_LNOT:
  482. BitVector_Empty(result);
  483. BitVector_LSB(result, BitVector_is_empty(op1));
  484. break;
  485. case YASM_EXPR_LXOR:
  486. BitVector_Empty(result);
  487. BitVector_LSB(result, !BitVector_is_empty(op1) ^
  488. !BitVector_is_empty(op2));
  489. break;
  490. case YASM_EXPR_LXNOR:
  491. BitVector_Empty(result);
  492. BitVector_LSB(result, !(!BitVector_is_empty(op1) ^
  493. !BitVector_is_empty(op2)));
  494. break;
  495. case YASM_EXPR_LNOR:
  496. BitVector_Empty(result);
  497. BitVector_LSB(result, !(!BitVector_is_empty(op1) ||
  498. !BitVector_is_empty(op2)));
  499. break;
  500. case YASM_EXPR_EQ:
  501. BitVector_Empty(result);
  502. BitVector_LSB(result, BitVector_equal(op1, op2));
  503. break;
  504. case YASM_EXPR_LT:
  505. BitVector_Empty(result);
  506. BitVector_LSB(result, BitVector_Compare(op1, op2) < 0);
  507. break;
  508. case YASM_EXPR_GT:
  509. BitVector_Empty(result);
  510. BitVector_LSB(result, BitVector_Compare(op1, op2) > 0);
  511. break;
  512. case YASM_EXPR_LE:
  513. BitVector_Empty(result);
  514. BitVector_LSB(result, BitVector_Compare(op1, op2) <= 0);
  515. break;
  516. case YASM_EXPR_GE:
  517. BitVector_Empty(result);
  518. BitVector_LSB(result, BitVector_Compare(op1, op2) >= 0);
  519. break;
  520. case YASM_EXPR_NE:
  521. BitVector_Empty(result);
  522. BitVector_LSB(result, !BitVector_equal(op1, op2));
  523. break;
  524. case YASM_EXPR_SEG:
  525. yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"),
  526. "SEG");
  527. break;
  528. case YASM_EXPR_WRT:
  529. yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"),
  530. "WRT");
  531. break;
  532. case YASM_EXPR_SEGOFF:
  533. yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"),
  534. ":");
  535. break;
  536. case YASM_EXPR_IDENT:
  537. if (result)
  538. BitVector_Copy(result, op1);
  539. break;
  540. default:
  541. yasm_error_set(YASM_ERROR_ARITHMETIC,
  542. N_("invalid operation in intnum calculation"));
  543. BitVector_Empty(result);
  544. return 1;
  545. }
  546. /* Try to fit the result into 32 bits if possible */
  547. if (acc->type == INTNUM_BV)
  548. BitVector_Destroy(acc->val.bv);
  549. intnum_frombv(acc, result);
  550. return 0;
  551. }
  552. /*@=nullderef =nullpass =branchstate@*/
  553. int
  554. yasm_intnum_compare(const yasm_intnum *intn1, const yasm_intnum *intn2)
  555. {
  556. wordptr op1, op2;
  557. if (intn1->type == INTNUM_L && intn2->type == INTNUM_L) {
  558. if (intn1->val.l < intn2->val.l)
  559. return -1;
  560. if (intn1->val.l > intn2->val.l)
  561. return 1;
  562. return 0;
  563. }
  564. op1 = intnum_tobv(op1static, intn1);
  565. op2 = intnum_tobv(op2static, intn2);
  566. return BitVector_Compare(op1, op2);
  567. }
  568. void
  569. yasm_intnum_zero(yasm_intnum *intn)
  570. {
  571. yasm_intnum_set_int(intn, 0);
  572. }
  573. void
  574. yasm_intnum_set(yasm_intnum *intn, const yasm_intnum *val)
  575. {
  576. if (intn->type == val->type) {
  577. switch (val->type) {
  578. case INTNUM_L:
  579. intn->val.l = val->val.l;
  580. break;
  581. case INTNUM_BV:
  582. BitVector_Copy(intn->val.bv, val->val.bv);
  583. break;
  584. }
  585. } else {
  586. switch (val->type) {
  587. case INTNUM_L:
  588. BitVector_Destroy(intn->val.bv);
  589. intn->val.l = val->val.l;
  590. break;
  591. case INTNUM_BV:
  592. intn->val.bv = BitVector_Clone(val->val.bv);
  593. break;
  594. }
  595. intn->type = val->type;
  596. }
  597. }
  598. void
  599. yasm_intnum_set_uint(yasm_intnum *intn, unsigned long val)
  600. {
  601. if (val > LONG_MAX) {
  602. if (intn->type != INTNUM_BV) {
  603. intn->val.bv = BitVector_Create(BITVECT_NATIVE_SIZE, TRUE);
  604. intn->type = INTNUM_BV;
  605. }
  606. BitVector_Chunk_Store(intn->val.bv, 32, 0, val);
  607. } else {
  608. if (intn->type == INTNUM_BV) {
  609. BitVector_Destroy(intn->val.bv);
  610. intn->type = INTNUM_L;
  611. }
  612. intn->val.l = (long)val;
  613. }
  614. }
  615. void
  616. yasm_intnum_set_int(yasm_intnum *intn, long val)
  617. {
  618. if (intn->type == INTNUM_BV)
  619. BitVector_Destroy(intn->val.bv);
  620. intn->type = INTNUM_L;
  621. intn->val.l = val;
  622. }
  623. int
  624. yasm_intnum_is_zero(const yasm_intnum *intn)
  625. {
  626. return (intn->type == INTNUM_L && intn->val.l == 0);
  627. }
  628. int
  629. yasm_intnum_is_pos1(const yasm_intnum *intn)
  630. {
  631. return (intn->type == INTNUM_L && intn->val.l == 1);
  632. }
  633. int
  634. yasm_intnum_is_neg1(const yasm_intnum *intn)
  635. {
  636. return (intn->type == INTNUM_L && intn->val.l == -1);
  637. }
  638. int
  639. yasm_intnum_sign(const yasm_intnum *intn)
  640. {
  641. if (intn->type == INTNUM_L) {
  642. if (intn->val.l == 0)
  643. return 0;
  644. else if (intn->val.l < 0)
  645. return -1;
  646. else
  647. return 1;
  648. } else
  649. return BitVector_Sign(intn->val.bv);
  650. }
  651. unsigned long
  652. yasm_intnum_get_uint(const yasm_intnum *intn)
  653. {
  654. switch (intn->type) {
  655. case INTNUM_L:
  656. if (intn->val.l < 0)
  657. return 0;
  658. return (unsigned long)intn->val.l;
  659. case INTNUM_BV:
  660. if (BitVector_msb_(intn->val.bv))
  661. return 0;
  662. if (Set_Max(intn->val.bv) > 32)
  663. return ULONG_MAX;
  664. return BitVector_Chunk_Read(intn->val.bv, 32, 0);
  665. default:
  666. yasm_internal_error(N_("unknown intnum type"));
  667. /*@notreached@*/
  668. return 0;
  669. }
  670. }
  671. long
  672. yasm_intnum_get_int(const yasm_intnum *intn)
  673. {
  674. switch (intn->type) {
  675. case INTNUM_L:
  676. return intn->val.l;
  677. case INTNUM_BV:
  678. if (BitVector_msb_(intn->val.bv)) {
  679. /* it's negative: negate the bitvector to get a positive
  680. * number, then negate the positive number.
  681. */
  682. unsigned long ul;
  683. BitVector_Negate(conv_bv, intn->val.bv);
  684. if (Set_Max(conv_bv) >= 32) {
  685. /* too negative */
  686. return LONG_MIN;
  687. }
  688. ul = BitVector_Chunk_Read(conv_bv, 32, 0);
  689. /* check for too negative */
  690. return (ul & 0x80000000) ? LONG_MIN : -((long)ul);
  691. }
  692. /* it's positive, and since it's a BV, it must be >0x7FFFFFFF */
  693. return LONG_MAX;
  694. default:
  695. yasm_internal_error(N_("unknown intnum type"));
  696. /*@notreached@*/
  697. return 0;
  698. }
  699. }
  700. void
  701. yasm_intnum_get_sized(const yasm_intnum *intn, unsigned char *ptr,
  702. size_t destsize, size_t valsize, int shift,
  703. int bigendian, int warn)
  704. {
  705. wordptr op1 = op1static, op2;
  706. unsigned char *buf;
  707. unsigned int len;
  708. size_t rshift = shift < 0 ? (size_t)(-shift) : 0;
  709. int carry_in;
  710. /* Currently don't support destinations larger than our native size */
  711. if (destsize*8 > BITVECT_NATIVE_SIZE)
  712. yasm_internal_error(N_("destination too large"));
  713. /* General size warnings */
  714. if (warn<0 && !yasm_intnum_check_size(intn, valsize, rshift, 1))
  715. yasm_warn_set(YASM_WARN_GENERAL,
  716. N_("value does not fit in signed %d bit field"),
  717. valsize);
  718. if (warn>0 && !yasm_intnum_check_size(intn, valsize, rshift, 2))
  719. yasm_warn_set(YASM_WARN_GENERAL,
  720. N_("value does not fit in %d bit field"), valsize);
  721. /* Read the original data into a bitvect */
  722. if (bigendian) {
  723. /* TODO */
  724. yasm_internal_error(N_("big endian not implemented"));
  725. } else
  726. BitVector_Block_Store(op1, ptr, (N_int)destsize);
  727. /* If not already a bitvect, convert value to be written to a bitvect */
  728. op2 = intnum_tobv(op2static, intn);
  729. /* Check low bits if right shifting and warnings enabled */
  730. if (warn && rshift > 0) {
  731. BitVector_Copy(conv_bv, op2);
  732. BitVector_Move_Left(conv_bv, (N_int)(BITVECT_NATIVE_SIZE-rshift));
  733. if (!BitVector_is_empty(conv_bv))
  734. yasm_warn_set(YASM_WARN_GENERAL,
  735. N_("misaligned value, truncating to boundary"));
  736. }
  737. /* Shift right if needed */
  738. if (rshift > 0) {
  739. carry_in = BitVector_msb_(op2);
  740. while (rshift-- > 0)
  741. BitVector_shift_right(op2, carry_in);
  742. shift = 0;
  743. }
  744. /* Write the new value into the destination bitvect */
  745. BitVector_Interval_Copy(op1, op2, (unsigned int)shift, 0, (N_int)valsize);
  746. /* Write out the new data */
  747. buf = BitVector_Block_Read(op1, &len);
  748. if (bigendian) {
  749. /* TODO */
  750. yasm_internal_error(N_("big endian not implemented"));
  751. } else
  752. memcpy(ptr, buf, destsize);
  753. yasm_xfree(buf);
  754. }
  755. /* Return 1 if okay size, 0 if not */
  756. int
  757. yasm_intnum_check_size(const yasm_intnum *intn, size_t size, size_t rshift,
  758. int rangetype)
  759. {
  760. wordptr val;
  761. /* If not already a bitvect, convert value to a bitvect */
  762. if (intn->type == INTNUM_BV) {
  763. if (rshift > 0) {
  764. val = conv_bv;
  765. BitVector_Copy(val, intn->val.bv);
  766. } else
  767. val = intn->val.bv;
  768. } else
  769. val = intnum_tobv(conv_bv, intn);
  770. if (size >= BITVECT_NATIVE_SIZE)
  771. return 1;
  772. if (rshift > 0) {
  773. int carry_in = BitVector_msb_(val);
  774. while (rshift-- > 0)
  775. BitVector_shift_right(val, carry_in);
  776. }
  777. if (rangetype > 0) {
  778. if (BitVector_msb_(val)) {
  779. /* it's negative */
  780. int retval;
  781. BitVector_Negate(conv_bv, val);
  782. BitVector_dec(conv_bv, conv_bv);
  783. retval = Set_Max(conv_bv) < (long)size-1;
  784. return retval;
  785. }
  786. if (rangetype == 1)
  787. size--;
  788. }
  789. return (Set_Max(val) < (long)size);
  790. }
  791. int
  792. yasm_intnum_in_range(const yasm_intnum *intn, long low, long high)
  793. {
  794. wordptr val = intnum_tobv(result, intn);
  795. wordptr lval = op1static;
  796. wordptr hval = op2static;
  797. /* Convert high and low to bitvects */
  798. BitVector_Empty(lval);
  799. if (low >= 0)
  800. BitVector_Chunk_Store(lval, 32, 0, (unsigned long)low);
  801. else {
  802. BitVector_Chunk_Store(lval, 32, 0, (unsigned long)(-low));
  803. BitVector_Negate(lval, lval);
  804. }
  805. BitVector_Empty(hval);
  806. if (high >= 0)
  807. BitVector_Chunk_Store(hval, 32, 0, (unsigned long)high);
  808. else {
  809. BitVector_Chunk_Store(hval, 32, 0, (unsigned long)(-high));
  810. BitVector_Negate(hval, hval);
  811. }
  812. /* Compare! */
  813. return (BitVector_Compare(val, lval) >= 0
  814. && BitVector_Compare(val, hval) <= 0);
  815. }
  816. static unsigned long
  817. get_leb128(wordptr val, unsigned char *ptr, int sign)
  818. {
  819. unsigned long i, size;
  820. unsigned char *ptr_orig = ptr;
  821. if (sign) {
  822. /* Signed mode */
  823. if (BitVector_msb_(val)) {
  824. /* Negative */
  825. BitVector_Negate(conv_bv, val);
  826. size = Set_Max(conv_bv)+2;
  827. } else {
  828. /* Positive */
  829. size = Set_Max(val)+2;
  830. }
  831. } else {
  832. /* Unsigned mode */
  833. size = Set_Max(val)+1;
  834. }
  835. /* Positive/Unsigned write */
  836. for (i=0; i<size; i += 7) {
  837. *ptr = (unsigned char)BitVector_Chunk_Read(val, 7, i);
  838. *ptr |= 0x80;
  839. ptr++;
  840. }
  841. *(ptr-1) &= 0x7F; /* Clear MSB of last byte */
  842. return (unsigned long)(ptr-ptr_orig);
  843. }
  844. static unsigned long
  845. size_leb128(wordptr val, int sign)
  846. {
  847. if (sign) {
  848. /* Signed mode */
  849. if (BitVector_msb_(val)) {
  850. /* Negative */
  851. BitVector_Negate(conv_bv, val);
  852. return (Set_Max(conv_bv)+8)/7;
  853. } else {
  854. /* Positive */
  855. return (Set_Max(val)+8)/7;
  856. }
  857. } else {
  858. /* Unsigned mode */
  859. return (Set_Max(val)+7)/7;
  860. }
  861. }
  862. unsigned long
  863. yasm_intnum_get_leb128(const yasm_intnum *intn, unsigned char *ptr, int sign)
  864. {
  865. wordptr val;
  866. /* Shortcut 0 */
  867. if (intn->type == INTNUM_L && intn->val.l == 0) {
  868. *ptr = 0;
  869. return 1;
  870. }
  871. /* If not already a bitvect, convert value to be written to a bitvect */
  872. val = intnum_tobv(op1static, intn);
  873. return get_leb128(val, ptr, sign);
  874. }
  875. unsigned long
  876. yasm_intnum_size_leb128(const yasm_intnum *intn, int sign)
  877. {
  878. wordptr val;
  879. /* Shortcut 0 */
  880. if (intn->type == INTNUM_L && intn->val.l == 0) {
  881. return 1;
  882. }
  883. /* If not already a bitvect, convert value to a bitvect */
  884. val = intnum_tobv(op1static, intn);
  885. return size_leb128(val, sign);
  886. }
  887. unsigned long
  888. yasm_get_sleb128(long v, unsigned char *ptr)
  889. {
  890. wordptr val = op1static;
  891. /* Shortcut 0 */
  892. if (v == 0) {
  893. *ptr = 0;
  894. return 1;
  895. }
  896. BitVector_Empty(val);
  897. if (v >= 0)
  898. BitVector_Chunk_Store(val, 32, 0, (unsigned long)v);
  899. else {
  900. BitVector_Chunk_Store(val, 32, 0, (unsigned long)(-v));
  901. BitVector_Negate(val, val);
  902. }
  903. return get_leb128(val, ptr, 1);
  904. }
  905. unsigned long
  906. yasm_size_sleb128(long v)
  907. {
  908. wordptr val = op1static;
  909. if (v == 0)
  910. return 1;
  911. BitVector_Empty(val);
  912. if (v >= 0)
  913. BitVector_Chunk_Store(val, 32, 0, (unsigned long)v);
  914. else {
  915. BitVector_Chunk_Store(val, 32, 0, (unsigned long)(-v));
  916. BitVector_Negate(val, val);
  917. }
  918. return size_leb128(val, 1);
  919. }
  920. unsigned long
  921. yasm_get_uleb128(unsigned long v, unsigned char *ptr)
  922. {
  923. wordptr val = op1static;
  924. /* Shortcut 0 */
  925. if (v == 0) {
  926. *ptr = 0;
  927. return 1;
  928. }
  929. BitVector_Empty(val);
  930. BitVector_Chunk_Store(val, 32, 0, v);
  931. return get_leb128(val, ptr, 0);
  932. }
  933. unsigned long
  934. yasm_size_uleb128(unsigned long v)
  935. {
  936. wordptr val = op1static;
  937. if (v == 0)
  938. return 1;
  939. BitVector_Empty(val);
  940. BitVector_Chunk_Store(val, 32, 0, v);
  941. return size_leb128(val, 0);
  942. }
  943. char *
  944. yasm_intnum_get_str(const yasm_intnum *intn)
  945. {
  946. unsigned char *s;
  947. switch (intn->type) {
  948. case INTNUM_L:
  949. s = yasm_xmalloc(16);
  950. sprintf((char *)s, "%ld", intn->val.l);
  951. return (char *)s;
  952. break;
  953. case INTNUM_BV:
  954. return (char *)BitVector_to_Dec(intn->val.bv);
  955. break;
  956. }
  957. /*@notreached@*/
  958. return NULL;
  959. }
  960. void
  961. yasm_intnum_print(const yasm_intnum *intn, FILE *f)
  962. {
  963. unsigned char *s;
  964. switch (intn->type) {
  965. case INTNUM_L:
  966. fprintf(f, "0x%lx", intn->val.l);
  967. break;
  968. case INTNUM_BV:
  969. s = BitVector_to_Hex(intn->val.bv);
  970. fprintf(f, "0x%s", (char *)s);
  971. yasm_xfree(s);
  972. break;
  973. }
  974. }