libtcc1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* TCC runtime library.
  2. Parts of this code are (c) 2002 Fabrice Bellard
  3. Copyright (C) 1987, 1988, 1992, 1994, 1995 Free Software Foundation, Inc.
  4. This file is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. In addition to the permissions in the GNU General Public License, the
  9. Free Software Foundation gives you unlimited permission to link the
  10. compiled version of this file into combinations with other programs,
  11. and to distribute those combinations without any restriction coming
  12. from the use of this file. (The General Public License restrictions
  13. do apply in other respects; for example, they cover modification of
  14. the file, and distribution when not linked into a combine
  15. executable.)
  16. This file is distributed in the hope that it will be useful, but
  17. WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; see the file COPYING. If not, write to
  22. the Free Software Foundation, 59 Temple Place - Suite 330,
  23. Boston, MA 02111-1307, USA.
  24. */
  25. #define W_TYPE_SIZE 32
  26. #define BITS_PER_UNIT 8
  27. typedef int Wtype;
  28. typedef unsigned int UWtype;
  29. typedef unsigned int USItype;
  30. typedef long long DWtype;
  31. typedef unsigned long long UDWtype;
  32. struct DWstruct {
  33. Wtype low, high;
  34. };
  35. typedef union
  36. {
  37. struct DWstruct s;
  38. DWtype ll;
  39. } DWunion;
  40. typedef long double XFtype;
  41. #define WORD_SIZE (sizeof (Wtype) * BITS_PER_UNIT)
  42. #define HIGH_WORD_COEFF (((UDWtype) 1) << WORD_SIZE)
  43. /* the following deal with IEEE single-precision numbers */
  44. #define EXCESS 126
  45. #define SIGNBIT 0x80000000
  46. #define HIDDEN (1 << 23)
  47. #define SIGN(fp) ((fp) & SIGNBIT)
  48. #define EXP(fp) (((fp) >> 23) & 0xFF)
  49. #define MANT(fp) (((fp) & 0x7FFFFF) | HIDDEN)
  50. #define PACK(s,e,m) ((s) | ((e) << 23) | (m))
  51. /* the following deal with IEEE double-precision numbers */
  52. #define EXCESSD 1022
  53. #define HIDDEND (1 << 20)
  54. #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
  55. #define SIGND(fp) ((fp.l.upper) & SIGNBIT)
  56. #define MANTD(fp) (((((fp.l.upper) & 0xFFFFF) | HIDDEND) << 10) | \
  57. (fp.l.lower >> 22))
  58. #define HIDDEND_LL ((long long)1 << 52)
  59. #define MANTD_LL(fp) ((fp.ll & (HIDDEND_LL-1)) | HIDDEND_LL)
  60. #define PACKD_LL(s,e,m) (((long long)((s)+((e)<<20))<<32)|(m))
  61. /* the following deal with x86 long double-precision numbers */
  62. #define EXCESSLD 16382
  63. #define EXPLD(fp) (fp.l.upper & 0x7fff)
  64. #define SIGNLD(fp) ((fp.l.upper) & 0x8000)
  65. /* only for x86 */
  66. union ldouble_long {
  67. long double ld;
  68. struct {
  69. unsigned long long lower;
  70. unsigned short upper;
  71. } l;
  72. };
  73. union double_long {
  74. double d;
  75. #if 1
  76. struct {
  77. unsigned int lower;
  78. int upper;
  79. } l;
  80. #else
  81. struct {
  82. int upper;
  83. unsigned int lower;
  84. } l;
  85. #endif
  86. long long ll;
  87. };
  88. union float_long {
  89. float f;
  90. unsigned int l;
  91. };
  92. /* XXX: we don't support several builtin supports for now */
  93. #if !defined __x86_64__ && !defined __arm__
  94. /* XXX: use gcc/tcc intrinsic ? */
  95. #if defined __i386__
  96. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  97. __asm__ ("subl %5,%1\n\tsbbl %3,%0" \
  98. : "=r" ((USItype) (sh)), \
  99. "=&r" ((USItype) (sl)) \
  100. : "0" ((USItype) (ah)), \
  101. "g" ((USItype) (bh)), \
  102. "1" ((USItype) (al)), \
  103. "g" ((USItype) (bl)))
  104. #define umul_ppmm(w1, w0, u, v) \
  105. __asm__ ("mull %3" \
  106. : "=a" ((USItype) (w0)), \
  107. "=d" ((USItype) (w1)) \
  108. : "%0" ((USItype) (u)), \
  109. "rm" ((USItype) (v)))
  110. #define udiv_qrnnd(q, r, n1, n0, dv) \
  111. __asm__ ("divl %4" \
  112. : "=a" ((USItype) (q)), \
  113. "=d" ((USItype) (r)) \
  114. : "0" ((USItype) (n0)), \
  115. "1" ((USItype) (n1)), \
  116. "rm" ((USItype) (dv)))
  117. #define count_leading_zeros(count, x) \
  118. do { \
  119. USItype __cbtmp; \
  120. __asm__ ("bsrl %1,%0" \
  121. : "=r" (__cbtmp) : "rm" ((USItype) (x))); \
  122. (count) = __cbtmp ^ 31; \
  123. } while (0)
  124. #else
  125. #error unsupported CPU type
  126. #endif
  127. /* most of this code is taken from libgcc2.c from gcc */
  128. static UDWtype __udivmoddi4 (UDWtype n, UDWtype d, UDWtype *rp)
  129. {
  130. DWunion ww;
  131. DWunion nn, dd;
  132. DWunion rr;
  133. UWtype d0, d1, n0, n1, n2;
  134. UWtype q0, q1;
  135. UWtype b, bm;
  136. nn.ll = n;
  137. dd.ll = d;
  138. d0 = dd.s.low;
  139. d1 = dd.s.high;
  140. n0 = nn.s.low;
  141. n1 = nn.s.high;
  142. #if !defined(UDIV_NEEDS_NORMALIZATION)
  143. if (d1 == 0)
  144. {
  145. if (d0 > n1)
  146. {
  147. /* 0q = nn / 0D */
  148. udiv_qrnnd (q0, n0, n1, n0, d0);
  149. q1 = 0;
  150. /* Remainder in n0. */
  151. }
  152. else
  153. {
  154. /* qq = NN / 0d */
  155. if (d0 == 0)
  156. d0 = 1 / d0; /* Divide intentionally by zero. */
  157. udiv_qrnnd (q1, n1, 0, n1, d0);
  158. udiv_qrnnd (q0, n0, n1, n0, d0);
  159. /* Remainder in n0. */
  160. }
  161. if (rp != 0)
  162. {
  163. rr.s.low = n0;
  164. rr.s.high = 0;
  165. *rp = rr.ll;
  166. }
  167. }
  168. #else /* UDIV_NEEDS_NORMALIZATION */
  169. if (d1 == 0)
  170. {
  171. if (d0 > n1)
  172. {
  173. /* 0q = nn / 0D */
  174. count_leading_zeros (bm, d0);
  175. if (bm != 0)
  176. {
  177. /* Normalize, i.e. make the most significant bit of the
  178. denominator set. */
  179. d0 = d0 << bm;
  180. n1 = (n1 << bm) | (n0 >> (W_TYPE_SIZE - bm));
  181. n0 = n0 << bm;
  182. }
  183. udiv_qrnnd (q0, n0, n1, n0, d0);
  184. q1 = 0;
  185. /* Remainder in n0 >> bm. */
  186. }
  187. else
  188. {
  189. /* qq = NN / 0d */
  190. if (d0 == 0)
  191. d0 = 1 / d0; /* Divide intentionally by zero. */
  192. count_leading_zeros (bm, d0);
  193. if (bm == 0)
  194. {
  195. /* From (n1 >= d0) /\ (the most significant bit of d0 is set),
  196. conclude (the most significant bit of n1 is set) /\ (the
  197. leading quotient digit q1 = 1).
  198. This special case is necessary, not an optimization.
  199. (Shifts counts of W_TYPE_SIZE are undefined.) */
  200. n1 -= d0;
  201. q1 = 1;
  202. }
  203. else
  204. {
  205. /* Normalize. */
  206. b = W_TYPE_SIZE - bm;
  207. d0 = d0 << bm;
  208. n2 = n1 >> b;
  209. n1 = (n1 << bm) | (n0 >> b);
  210. n0 = n0 << bm;
  211. udiv_qrnnd (q1, n1, n2, n1, d0);
  212. }
  213. /* n1 != d0... */
  214. udiv_qrnnd (q0, n0, n1, n0, d0);
  215. /* Remainder in n0 >> bm. */
  216. }
  217. if (rp != 0)
  218. {
  219. rr.s.low = n0 >> bm;
  220. rr.s.high = 0;
  221. *rp = rr.ll;
  222. }
  223. }
  224. #endif /* UDIV_NEEDS_NORMALIZATION */
  225. else
  226. {
  227. if (d1 > n1)
  228. {
  229. /* 00 = nn / DD */
  230. q0 = 0;
  231. q1 = 0;
  232. /* Remainder in n1n0. */
  233. if (rp != 0)
  234. {
  235. rr.s.low = n0;
  236. rr.s.high = n1;
  237. *rp = rr.ll;
  238. }
  239. }
  240. else
  241. {
  242. /* 0q = NN / dd */
  243. count_leading_zeros (bm, d1);
  244. if (bm == 0)
  245. {
  246. /* From (n1 >= d1) /\ (the most significant bit of d1 is set),
  247. conclude (the most significant bit of n1 is set) /\ (the
  248. quotient digit q0 = 0 or 1).
  249. This special case is necessary, not an optimization. */
  250. /* The condition on the next line takes advantage of that
  251. n1 >= d1 (true due to program flow). */
  252. if (n1 > d1 || n0 >= d0)
  253. {
  254. q0 = 1;
  255. sub_ddmmss (n1, n0, n1, n0, d1, d0);
  256. }
  257. else
  258. q0 = 0;
  259. q1 = 0;
  260. if (rp != 0)
  261. {
  262. rr.s.low = n0;
  263. rr.s.high = n1;
  264. *rp = rr.ll;
  265. }
  266. }
  267. else
  268. {
  269. UWtype m1, m0;
  270. /* Normalize. */
  271. b = W_TYPE_SIZE - bm;
  272. d1 = (d1 << bm) | (d0 >> b);
  273. d0 = d0 << bm;
  274. n2 = n1 >> b;
  275. n1 = (n1 << bm) | (n0 >> b);
  276. n0 = n0 << bm;
  277. udiv_qrnnd (q0, n1, n2, n1, d1);
  278. umul_ppmm (m1, m0, q0, d0);
  279. if (m1 > n1 || (m1 == n1 && m0 > n0))
  280. {
  281. q0--;
  282. sub_ddmmss (m1, m0, m1, m0, d1, d0);
  283. }
  284. q1 = 0;
  285. /* Remainder in (n1n0 - m1m0) >> bm. */
  286. if (rp != 0)
  287. {
  288. sub_ddmmss (n1, n0, n1, n0, m1, m0);
  289. rr.s.low = (n1 << b) | (n0 >> bm);
  290. rr.s.high = n1 >> bm;
  291. *rp = rr.ll;
  292. }
  293. }
  294. }
  295. }
  296. ww.s.low = q0;
  297. ww.s.high = q1;
  298. return ww.ll;
  299. }
  300. #define __negdi2(a) (-(a))
  301. long long __divdi3(long long u, long long v)
  302. {
  303. int c = 0;
  304. DWunion uu, vv;
  305. DWtype w;
  306. uu.ll = u;
  307. vv.ll = v;
  308. if (uu.s.high < 0) {
  309. c = ~c;
  310. uu.ll = __negdi2 (uu.ll);
  311. }
  312. if (vv.s.high < 0) {
  313. c = ~c;
  314. vv.ll = __negdi2 (vv.ll);
  315. }
  316. w = __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) 0);
  317. if (c)
  318. w = __negdi2 (w);
  319. return w;
  320. }
  321. long long __moddi3(long long u, long long v)
  322. {
  323. int c = 0;
  324. DWunion uu, vv;
  325. DWtype w;
  326. uu.ll = u;
  327. vv.ll = v;
  328. if (uu.s.high < 0) {
  329. c = ~c;
  330. uu.ll = __negdi2 (uu.ll);
  331. }
  332. if (vv.s.high < 0)
  333. vv.ll = __negdi2 (vv.ll);
  334. __udivmoddi4 (uu.ll, vv.ll, (UDWtype *) &w);
  335. if (c)
  336. w = __negdi2 (w);
  337. return w;
  338. }
  339. unsigned long long __udivdi3(unsigned long long u, unsigned long long v)
  340. {
  341. return __udivmoddi4 (u, v, (UDWtype *) 0);
  342. }
  343. unsigned long long __umoddi3(unsigned long long u, unsigned long long v)
  344. {
  345. UDWtype w;
  346. __udivmoddi4 (u, v, &w);
  347. return w;
  348. }
  349. /* XXX: fix tcc's code generator to do this instead */
  350. long long __ashrdi3(long long a, int b)
  351. {
  352. #ifdef __TINYC__
  353. DWunion u;
  354. u.ll = a;
  355. if (b >= 32) {
  356. u.s.low = u.s.high >> (b - 32);
  357. u.s.high = u.s.high >> 31;
  358. } else if (b != 0) {
  359. u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b));
  360. u.s.high = u.s.high >> b;
  361. }
  362. return u.ll;
  363. #else
  364. return a >> b;
  365. #endif
  366. }
  367. /* XXX: fix tcc's code generator to do this instead */
  368. unsigned long long __lshrdi3(unsigned long long a, int b)
  369. {
  370. #ifdef __TINYC__
  371. DWunion u;
  372. u.ll = a;
  373. if (b >= 32) {
  374. u.s.low = (unsigned)u.s.high >> (b - 32);
  375. u.s.high = 0;
  376. } else if (b != 0) {
  377. u.s.low = ((unsigned)u.s.low >> b) | (u.s.high << (32 - b));
  378. u.s.high = (unsigned)u.s.high >> b;
  379. }
  380. return u.ll;
  381. #else
  382. return a >> b;
  383. #endif
  384. }
  385. /* XXX: fix tcc's code generator to do this instead */
  386. long long __ashldi3(long long a, int b)
  387. {
  388. #ifdef __TINYC__
  389. DWunion u;
  390. u.ll = a;
  391. if (b >= 32) {
  392. u.s.high = (unsigned)u.s.low << (b - 32);
  393. u.s.low = 0;
  394. } else if (b != 0) {
  395. u.s.high = ((unsigned)u.s.high << b) | ((unsigned)u.s.low >> (32 - b));
  396. u.s.low = (unsigned)u.s.low << b;
  397. }
  398. return u.ll;
  399. #else
  400. return a << b;
  401. #endif
  402. }
  403. #endif /* !__x86_64__ */
  404. /* XXX: fix tcc's code generator to do this instead */
  405. float __floatundisf(unsigned long long a)
  406. {
  407. DWunion uu;
  408. XFtype r;
  409. uu.ll = a;
  410. if (uu.s.high >= 0) {
  411. return (float)uu.ll;
  412. } else {
  413. r = (XFtype)uu.ll;
  414. r += 18446744073709551616.0;
  415. return (float)r;
  416. }
  417. }
  418. double __floatundidf(unsigned long long a)
  419. {
  420. DWunion uu;
  421. XFtype r;
  422. uu.ll = a;
  423. if (uu.s.high >= 0) {
  424. return (double)uu.ll;
  425. } else {
  426. r = (XFtype)uu.ll;
  427. r += 18446744073709551616.0;
  428. return (double)r;
  429. }
  430. }
  431. long double __floatundixf(unsigned long long a)
  432. {
  433. DWunion uu;
  434. XFtype r;
  435. uu.ll = a;
  436. if (uu.s.high >= 0) {
  437. return (long double)uu.ll;
  438. } else {
  439. r = (XFtype)uu.ll;
  440. r += 18446744073709551616.0;
  441. return (long double)r;
  442. }
  443. }
  444. unsigned long long __fixunssfdi (float a1)
  445. {
  446. register union float_long fl1;
  447. register int exp;
  448. register unsigned long l;
  449. fl1.f = a1;
  450. if (fl1.l == 0)
  451. return (0);
  452. exp = EXP (fl1.l) - EXCESS - 24;
  453. l = MANT(fl1.l);
  454. if (exp >= 41)
  455. return (unsigned long long)-1;
  456. else if (exp >= 0)
  457. return (unsigned long long)l << exp;
  458. else if (exp >= -23)
  459. return l >> -exp;
  460. else
  461. return 0;
  462. }
  463. long long __fixsfdi (float a1)
  464. {
  465. long long ret; int s;
  466. ret = __fixunssfdi((s = a1 >= 0) ? a1 : -a1);
  467. return s ? ret : -ret;
  468. }
  469. unsigned long long __fixunsdfdi (double a1)
  470. {
  471. register union double_long dl1;
  472. register int exp;
  473. register unsigned long long l;
  474. dl1.d = a1;
  475. if (dl1.ll == 0)
  476. return (0);
  477. exp = EXPD (dl1) - EXCESSD - 53;
  478. l = MANTD_LL(dl1);
  479. if (exp >= 12)
  480. return (unsigned long long)-1;
  481. else if (exp >= 0)
  482. return l << exp;
  483. else if (exp >= -52)
  484. return l >> -exp;
  485. else
  486. return 0;
  487. }
  488. long long __fixdfdi (double a1)
  489. {
  490. long long ret; int s;
  491. ret = __fixunsdfdi((s = a1 >= 0) ? a1 : -a1);
  492. return s ? ret : -ret;
  493. }
  494. #ifndef __arm__
  495. unsigned long long __fixunsxfdi (long double a1)
  496. {
  497. register union ldouble_long dl1;
  498. register int exp;
  499. register unsigned long long l;
  500. dl1.ld = a1;
  501. if (dl1.l.lower == 0 && dl1.l.upper == 0)
  502. return (0);
  503. exp = EXPLD (dl1) - EXCESSLD - 64;
  504. l = dl1.l.lower;
  505. if (exp > 0)
  506. return (unsigned long long)-1;
  507. else if (exp >= -63)
  508. return l >> -exp;
  509. else
  510. return 0;
  511. }
  512. long long __fixxfdi (long double a1)
  513. {
  514. long long ret; int s;
  515. ret = __fixunsxfdi((s = a1 >= 0) ? a1 : -a1);
  516. return s ? ret : -ret;
  517. }
  518. #endif /* !ARM */