jit_emit_numberic.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "jit_emit_numberic.h"
  6. #include "jit_emit_exception.h"
  7. #include "jit_emit_control.h"
  8. #include "jit_emit_function.h"
  9. #include "../jit_frontend.h"
  10. #include "../jit_codegen.h"
  11. #define PUSH_INT(v) \
  12. do { \
  13. if (is_i32) \
  14. PUSH_I32(v); \
  15. else \
  16. PUSH_I64(v); \
  17. } while (0)
  18. #define POP_INT(v) \
  19. do { \
  20. if (is_i32) \
  21. POP_I32(v); \
  22. else \
  23. POP_I64(v); \
  24. } while (0)
  25. #define PUSH_FLOAT(v) \
  26. do { \
  27. if (is_f32) \
  28. PUSH_F32(v); \
  29. else \
  30. PUSH_F64(v); \
  31. } while (0)
  32. #define POP_FLOAT(v) \
  33. do { \
  34. if (is_f32) \
  35. POP_F32(v); \
  36. else \
  37. POP_F64(v); \
  38. } while (0)
  39. #define DEF_INT_UNARY_OP(op, err) \
  40. do { \
  41. JitReg res, operand; \
  42. POP_INT(operand); \
  43. if (!(res = op)) { \
  44. if (err) \
  45. jit_set_last_error(cc, err); \
  46. goto fail; \
  47. } \
  48. PUSH_INT(res); \
  49. } while (0)
  50. #define DEF_INT_BINARY_OP(op, err) \
  51. do { \
  52. JitReg res, left, right; \
  53. POP_INT(right); \
  54. POP_INT(left); \
  55. if (!(res = op)) { \
  56. if (err) \
  57. jit_set_last_error(cc, err); \
  58. goto fail; \
  59. } \
  60. PUSH_INT(res); \
  61. } while (0)
  62. #define DEF_FP_UNARY_OP(op, err) \
  63. do { \
  64. JitReg res, operand; \
  65. POP_FLOAT(operand); \
  66. if (!(res = op)) { \
  67. if (err) \
  68. jit_set_last_error(cc, err); \
  69. goto fail; \
  70. } \
  71. PUSH_FLOAT(res); \
  72. } while (0)
  73. #define DEF_FP_BINARY_OP(op, err) \
  74. do { \
  75. JitReg res, left, right; \
  76. POP_FLOAT(right); \
  77. POP_FLOAT(left); \
  78. if (!(res = op)) { \
  79. if (err) \
  80. jit_set_last_error(cc, err); \
  81. goto fail; \
  82. } \
  83. PUSH_FLOAT(res); \
  84. } while (0)
  85. static uint32
  86. clz32(uint32 type)
  87. {
  88. uint32 num = 0;
  89. if (type == 0)
  90. return 32;
  91. while (!(type & 0x80000000)) {
  92. num++;
  93. type <<= 1;
  94. }
  95. return num;
  96. }
  97. static uint64
  98. clz64(uint64 type)
  99. {
  100. uint32 num = 0;
  101. if (type == 0)
  102. return 64;
  103. while (!(type & 0x8000000000000000LL)) {
  104. num++;
  105. type <<= 1;
  106. }
  107. return num;
  108. }
  109. static uint32
  110. ctz32(uint32 type)
  111. {
  112. uint32 num = 0;
  113. if (type == 0)
  114. return 32;
  115. while (!(type & 1)) {
  116. num++;
  117. type >>= 1;
  118. }
  119. return num;
  120. }
  121. static uint64
  122. ctz64(uint64 type)
  123. {
  124. uint32 num = 0;
  125. if (type == 0)
  126. return 64;
  127. while (!(type & 1)) {
  128. num++;
  129. type >>= 1;
  130. }
  131. return num;
  132. }
  133. static uint32
  134. popcnt32(uint32 u)
  135. {
  136. uint32 ret = 0;
  137. while (u) {
  138. u = (u & (u - 1));
  139. ret++;
  140. }
  141. return ret;
  142. }
  143. static uint64
  144. popcnt64(uint64 u)
  145. {
  146. uint32 ret = 0;
  147. while (u) {
  148. u = (u & (u - 1));
  149. ret++;
  150. }
  151. return ret;
  152. }
  153. bool
  154. jit_compile_op_i32_clz(JitCompContext *cc)
  155. {
  156. JitReg value, res;
  157. POP_I32(value);
  158. if (jit_reg_is_const(value)) {
  159. uint32 i32 = jit_cc_get_const_I32(cc, value);
  160. PUSH_I32(NEW_CONST(I32, clz32(i32)));
  161. return true;
  162. }
  163. res = jit_cc_new_reg_I32(cc);
  164. GEN_INSN(CLZ, res, value);
  165. PUSH_I32(res);
  166. return true;
  167. fail:
  168. return false;
  169. }
  170. bool
  171. jit_compile_op_i32_ctz(JitCompContext *cc)
  172. {
  173. JitReg value, res = jit_cc_new_reg_I32(cc);
  174. POP_I32(value);
  175. if (jit_reg_is_const(value)) {
  176. uint32 i32 = jit_cc_get_const_I32(cc, value);
  177. PUSH_I32(NEW_CONST(I32, ctz32(i32)));
  178. return true;
  179. }
  180. res = jit_cc_new_reg_I32(cc);
  181. GEN_INSN(CTZ, res, value);
  182. PUSH_I32(res);
  183. return true;
  184. fail:
  185. return false;
  186. }
  187. bool
  188. jit_compile_op_i32_popcnt(JitCompContext *cc)
  189. {
  190. JitReg value, res;
  191. POP_I32(value);
  192. if (jit_reg_is_const(value)) {
  193. uint32 i32 = jit_cc_get_const_I32(cc, value);
  194. PUSH_I32(NEW_CONST(I32, popcnt32(i32)));
  195. return true;
  196. }
  197. res = jit_cc_new_reg_I32(cc);
  198. GEN_INSN(POPCNT, res, value);
  199. PUSH_I32(res);
  200. return true;
  201. fail:
  202. return false;
  203. }
  204. bool
  205. jit_compile_op_i64_clz(JitCompContext *cc)
  206. {
  207. JitReg value, res;
  208. POP_I64(value);
  209. if (jit_reg_is_const(value)) {
  210. uint64 i64 = jit_cc_get_const_I64(cc, value);
  211. PUSH_I64(NEW_CONST(I64, clz64(i64)));
  212. return true;
  213. }
  214. res = jit_cc_new_reg_I64(cc);
  215. GEN_INSN(CLZ, res, value);
  216. PUSH_I64(res);
  217. return true;
  218. fail:
  219. return false;
  220. }
  221. bool
  222. jit_compile_op_i64_ctz(JitCompContext *cc)
  223. {
  224. JitReg value, res;
  225. POP_I64(value);
  226. if (jit_reg_is_const(value)) {
  227. uint64 i64 = jit_cc_get_const_I64(cc, value);
  228. PUSH_I64(NEW_CONST(I64, ctz64(i64)));
  229. return true;
  230. }
  231. res = jit_cc_new_reg_I64(cc);
  232. GEN_INSN(CTZ, res, value);
  233. PUSH_I64(res);
  234. return true;
  235. fail:
  236. return false;
  237. }
  238. bool
  239. jit_compile_op_i64_popcnt(JitCompContext *cc)
  240. {
  241. JitReg value, res;
  242. POP_I64(value);
  243. if (jit_reg_is_const(value)) {
  244. uint64 i64 = jit_cc_get_const_I64(cc, value);
  245. PUSH_I64(NEW_CONST(I64, popcnt64(i64)));
  246. return true;
  247. }
  248. res = jit_cc_new_reg_I64(cc);
  249. GEN_INSN(POPCNT, res, value);
  250. PUSH_I64(res);
  251. return true;
  252. fail:
  253. return false;
  254. }
  255. #define IS_CONST_ALL_ONE(val, is_i32) \
  256. (jit_reg_is_const(val) \
  257. && ((is_i32 && jit_cc_get_const_I32(cc, val) == -1) \
  258. || (!is_i32 && jit_cc_get_const_I64(cc, val) == -1LL)))
  259. #define IS_CONST_ZERO(val) \
  260. (jit_reg_is_const(val) \
  261. && ((is_i32 && jit_cc_get_const_I32(cc, val) == 0) \
  262. || (!is_i32 && jit_cc_get_const_I64(cc, val) == 0)))
  263. /* macros for integer binary operations (ibinop) */
  264. #if defined(__GNUC__)
  265. #define NO_SANITIZER_INTEGER \
  266. __attribute__((no_sanitize("signed-integer-overflow")))
  267. #else
  268. #define NO_SANITIZER_INTEGER
  269. #endif
  270. #define __DEF_BI_INT_CONST_OPS(bits, opname, op) \
  271. NO_SANITIZER_INTEGER \
  272. static int##bits do_i##bits##_const_##opname(int##bits lhs, int##bits rhs) \
  273. { \
  274. return lhs op rhs; \
  275. }
  276. #define DEF_BI_INT_CONST_OPS(opname, op) \
  277. __DEF_BI_INT_CONST_OPS(32, opname, op) \
  278. __DEF_BI_INT_CONST_OPS(64, opname, op)
  279. #define DEF_UNI_INT_CONST_OPS(opname) \
  280. static JitReg compile_int_##opname##_consts( \
  281. JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  282. typedef JitReg (*uni_const_handler)(JitCompContext *, JitReg, JitReg, bool);
  283. typedef int32 (*bin_i32_consts_handler)(int32, int32);
  284. typedef int64 (*bin_i64_consts_handler)(int64, int64);
  285. /* ibinopt for integer binary operations */
  286. static JitReg
  287. compile_op_ibinopt_const(JitCompContext *cc, JitReg left, JitReg right,
  288. bool is_i32, uni_const_handler handle_one_const,
  289. bin_i32_consts_handler handle_two_i32_const,
  290. bin_i64_consts_handler handle_two_i64_const)
  291. {
  292. JitReg res;
  293. if (jit_reg_is_const(left) && jit_reg_is_const(right)) {
  294. if (is_i32) {
  295. int32 left_val = jit_cc_get_const_I32(cc, left);
  296. int32 right_val = jit_cc_get_const_I32(cc, right);
  297. res = NEW_CONST(I32, handle_two_i32_const(left_val, right_val));
  298. }
  299. else {
  300. int64 left_val = jit_cc_get_const_I64(cc, left);
  301. int64 right_val = jit_cc_get_const_I64(cc, right);
  302. res = NEW_CONST(I64, handle_two_i64_const(left_val, right_val));
  303. }
  304. goto shortcut;
  305. }
  306. if (jit_reg_is_const(left) || jit_reg_is_const(right)) {
  307. res = handle_one_const(cc, left, right, is_i32);
  308. if (res)
  309. goto shortcut;
  310. }
  311. return 0;
  312. shortcut:
  313. return res;
  314. }
  315. #define CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, opname) \
  316. compile_op_ibinopt_const(cc, left, right, is_i32, \
  317. compile_int_##opname##_consts, \
  318. do_i32_const_##opname, do_i64_const_##opname)
  319. DEF_UNI_INT_CONST_OPS(add)
  320. {
  321. /* If one of the operands is 0, just return the other */
  322. if (IS_CONST_ZERO(left))
  323. return right;
  324. if (IS_CONST_ZERO(right))
  325. return left;
  326. return 0;
  327. }
  328. DEF_BI_INT_CONST_OPS(add, +)
  329. static JitReg
  330. compile_int_add(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  331. {
  332. JitReg res;
  333. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, add);
  334. if (res)
  335. goto shortcut;
  336. /* Build add */
  337. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  338. GEN_INSN(ADD, res, left, right);
  339. shortcut:
  340. return res;
  341. }
  342. DEF_UNI_INT_CONST_OPS(sub)
  343. {
  344. /* If the right operand is 0, just return the left */
  345. if (IS_CONST_ZERO(right))
  346. return left;
  347. return 0;
  348. }
  349. DEF_BI_INT_CONST_OPS(sub, -)
  350. static JitReg
  351. compile_int_sub(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  352. {
  353. JitReg res;
  354. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, sub);
  355. if (res)
  356. goto shortcut;
  357. /* Build sub */
  358. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  359. GEN_INSN(SUB, res, left, right);
  360. shortcut:
  361. return res;
  362. }
  363. DEF_UNI_INT_CONST_OPS(mul)
  364. {
  365. /* If one of the operands is 0, just return constant 0 */
  366. if (IS_CONST_ZERO(left) || IS_CONST_ZERO(right))
  367. return is_i32 ? NEW_CONST(I32, 0) : NEW_CONST(I64, 0);
  368. return 0;
  369. }
  370. static int32
  371. do_i32_const_mul(int32 lhs, int32 rhs)
  372. {
  373. return (int32)((uint64)lhs * (uint64)rhs);
  374. }
  375. static int64
  376. do_i64_const_mul(int64 lhs, int64 rhs)
  377. {
  378. return (int64)((uint64)lhs * (uint64)rhs);
  379. }
  380. static JitReg
  381. compile_int_mul(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  382. {
  383. JitReg res;
  384. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, mul);
  385. if (res)
  386. goto shortcut;
  387. /* Build mul */
  388. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  389. GEN_INSN(MUL, res, left, right);
  390. shortcut:
  391. return res;
  392. }
  393. static bool
  394. compile_int_div_no_check(JitCompContext *cc, IntArithmetic arith_op,
  395. bool is_i32, JitReg left, JitReg right, JitReg res)
  396. {
  397. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  398. JitReg eax_hreg = jit_codegen_get_hreg_by_name("eax");
  399. JitReg edx_hreg = jit_codegen_get_hreg_by_name("edx");
  400. JitReg rax_hreg = jit_codegen_get_hreg_by_name("rax");
  401. JitReg rdx_hreg = jit_codegen_get_hreg_by_name("rdx");
  402. #endif
  403. if (jit_reg_is_const(right) && jit_reg_is_const(left)) {
  404. if (INT_DIV_S == arith_op || INT_REM_S == arith_op) {
  405. if (is_i32) {
  406. int32 lhs = jit_cc_get_const_I32(cc, left);
  407. int32 rhs = jit_cc_get_const_I32(cc, right);
  408. if (INT_DIV_S == arith_op) {
  409. res = NEW_CONST(I32, lhs / rhs);
  410. }
  411. else {
  412. res = NEW_CONST(I32, lhs % rhs);
  413. }
  414. PUSH_I32(res);
  415. return true;
  416. }
  417. else {
  418. int64 lhs = jit_cc_get_const_I64(cc, left);
  419. int64 rhs = jit_cc_get_const_I64(cc, right);
  420. if (INT_DIV_S == arith_op) {
  421. res = NEW_CONST(I64, lhs / rhs);
  422. }
  423. else {
  424. res = NEW_CONST(I64, lhs % rhs);
  425. }
  426. PUSH_I64(res);
  427. return true;
  428. }
  429. }
  430. else {
  431. if (is_i32) {
  432. uint32 lhs = (uint32)jit_cc_get_const_I32(cc, left);
  433. uint32 rhs = (uint32)jit_cc_get_const_I32(cc, right);
  434. if (INT_DIV_U == arith_op) {
  435. res = NEW_CONST(I32, lhs / rhs);
  436. }
  437. else {
  438. res = NEW_CONST(I32, lhs % rhs);
  439. }
  440. PUSH_I32(res);
  441. return true;
  442. }
  443. else {
  444. uint64 lhs = (uint64)jit_cc_get_const_I64(cc, left);
  445. uint64 rhs = (uint64)jit_cc_get_const_I64(cc, right);
  446. if (INT_DIV_U == arith_op) {
  447. res = NEW_CONST(I64, lhs / rhs);
  448. }
  449. else {
  450. res = NEW_CONST(I64, lhs % rhs);
  451. }
  452. PUSH_I64(res);
  453. return true;
  454. }
  455. }
  456. }
  457. switch (arith_op) {
  458. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  459. case INT_DIV_S:
  460. case INT_DIV_U:
  461. {
  462. JitInsn *insn = NULL, *insn1 = NULL;
  463. if (is_i32) {
  464. GEN_INSN(MOV, eax_hreg, left);
  465. if (arith_op == INT_DIV_S)
  466. insn = GEN_INSN(DIV_S, eax_hreg, eax_hreg, right);
  467. else
  468. insn = GEN_INSN(DIV_U, eax_hreg, eax_hreg, right);
  469. }
  470. else {
  471. GEN_INSN(MOV, rax_hreg, left);
  472. if (arith_op == INT_DIV_S)
  473. insn = GEN_INSN(DIV_S, rax_hreg, rax_hreg, right);
  474. else
  475. insn = GEN_INSN(DIV_U, rax_hreg, rax_hreg, right);
  476. }
  477. if (!insn) {
  478. goto fail;
  479. }
  480. if (!jit_lock_reg_in_insn(cc, insn, eax_hreg)
  481. || !jit_lock_reg_in_insn(cc, insn, edx_hreg)) {
  482. goto fail;
  483. }
  484. if (is_i32) {
  485. res = jit_cc_new_reg_I32(cc);
  486. insn1 = jit_insn_new_MOV(res, eax_hreg);
  487. }
  488. else {
  489. res = jit_cc_new_reg_I64(cc);
  490. insn1 = jit_insn_new_MOV(res, rax_hreg);
  491. }
  492. if (!insn1) {
  493. jit_set_last_error(cc, "generate insn failed");
  494. goto fail;
  495. }
  496. jit_insn_insert_after(insn, insn1);
  497. break;
  498. }
  499. case INT_REM_S:
  500. case INT_REM_U:
  501. {
  502. JitInsn *insn = NULL, *insn1 = NULL;
  503. if (is_i32) {
  504. GEN_INSN(MOV, eax_hreg, left);
  505. if (arith_op == INT_REM_S)
  506. insn = GEN_INSN(REM_S, edx_hreg, eax_hreg, right);
  507. else
  508. insn = GEN_INSN(REM_U, edx_hreg, eax_hreg, right);
  509. }
  510. else {
  511. GEN_INSN(MOV, rax_hreg, left);
  512. if (arith_op == INT_REM_S)
  513. insn = GEN_INSN(REM_S, rdx_hreg, rax_hreg, right);
  514. else
  515. insn = GEN_INSN(REM_U, rdx_hreg, rax_hreg, right);
  516. }
  517. if (!insn) {
  518. goto fail;
  519. }
  520. if (!jit_lock_reg_in_insn(cc, insn, eax_hreg)
  521. || !jit_lock_reg_in_insn(cc, insn, edx_hreg)) {
  522. goto fail;
  523. }
  524. if (is_i32) {
  525. res = jit_cc_new_reg_I32(cc);
  526. insn1 = jit_insn_new_MOV(res, edx_hreg);
  527. }
  528. else {
  529. res = jit_cc_new_reg_I64(cc);
  530. insn1 = jit_insn_new_MOV(res, rdx_hreg);
  531. }
  532. if (!insn1) {
  533. jit_set_last_error(cc, "generate insn failed");
  534. goto fail;
  535. }
  536. jit_insn_insert_after(insn, insn1);
  537. break;
  538. }
  539. #else
  540. case INT_DIV_S:
  541. GEN_INSN(DIV_S, res, left, right);
  542. break;
  543. case INT_DIV_U:
  544. GEN_INSN(DIV_U, res, left, right);
  545. break;
  546. case INT_REM_S:
  547. GEN_INSN(REM_S, res, left, right);
  548. break;
  549. case INT_REM_U:
  550. GEN_INSN(REM_U, res, left, right);
  551. break;
  552. #endif /* defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) */
  553. default:
  554. bh_assert(0);
  555. return false;
  556. }
  557. if (is_i32)
  558. PUSH_I32(res);
  559. else
  560. PUSH_I64(res);
  561. return true;
  562. fail:
  563. return false;
  564. }
  565. static bool
  566. compile_int_div(JitCompContext *cc, IntArithmetic arith_op, bool is_i32,
  567. uint8 **p_frame_ip)
  568. {
  569. JitReg left, right, res;
  570. bh_assert(arith_op == INT_DIV_S || arith_op == INT_DIV_U
  571. || arith_op == INT_REM_S || arith_op == INT_REM_U);
  572. if (is_i32) {
  573. POP_I32(right);
  574. POP_I32(left);
  575. res = jit_cc_new_reg_I32(cc);
  576. }
  577. else {
  578. POP_I64(right);
  579. POP_I64(left);
  580. res = jit_cc_new_reg_I64(cc);
  581. }
  582. if (jit_reg_is_const(right)) {
  583. int64 right_val = is_i32 ? (int64)jit_cc_get_const_I32(cc, right)
  584. : jit_cc_get_const_I64(cc, right);
  585. switch (right_val) {
  586. case 0:
  587. {
  588. /* Directly throw exception if divided by zero */
  589. if (!(jit_emit_exception(cc, EXCE_INTEGER_DIVIDE_BY_ZERO,
  590. JIT_OP_JMP, 0, NULL)))
  591. goto fail;
  592. return jit_handle_next_reachable_block(cc, p_frame_ip);
  593. }
  594. case 1:
  595. {
  596. if (arith_op == INT_DIV_S || arith_op == INT_DIV_U) {
  597. if (is_i32)
  598. PUSH_I32(left);
  599. else
  600. PUSH_I64(left);
  601. }
  602. else {
  603. if (is_i32)
  604. PUSH_I32(NEW_CONST(I32, 0));
  605. else
  606. PUSH_I64(NEW_CONST(I64, 0));
  607. }
  608. return true;
  609. }
  610. case -1:
  611. {
  612. if (arith_op == INT_DIV_S) {
  613. if (is_i32)
  614. GEN_INSN(CMP, cc->cmp_reg, left,
  615. NEW_CONST(I32, INT32_MIN));
  616. else
  617. GEN_INSN(CMP, cc->cmp_reg, left,
  618. NEW_CONST(I64, INT64_MIN));
  619. /* Throw integer overflow exception if left is
  620. INT32_MIN or INT64_MIN */
  621. if (!(jit_emit_exception(cc, EXCE_INTEGER_OVERFLOW,
  622. JIT_OP_BEQ, cc->cmp_reg, NULL)))
  623. goto fail;
  624. /* Push -(left) to stack */
  625. GEN_INSN(NEG, res, left);
  626. if (is_i32)
  627. PUSH_I32(res);
  628. else
  629. PUSH_I64(res);
  630. return true;
  631. }
  632. else if (arith_op == INT_REM_S) {
  633. if (is_i32)
  634. PUSH_I32(NEW_CONST(I32, 0));
  635. else
  636. PUSH_I64(NEW_CONST(I64, 0));
  637. return true;
  638. }
  639. else {
  640. /* Build default div and rem */
  641. return compile_int_div_no_check(cc, arith_op, is_i32, left,
  642. right, res);
  643. }
  644. }
  645. default:
  646. {
  647. /* Build default div and rem */
  648. return compile_int_div_no_check(cc, arith_op, is_i32, left,
  649. right, res);
  650. }
  651. }
  652. }
  653. else {
  654. JitReg cmp1 = jit_cc_new_reg_I32(cc);
  655. JitReg cmp2 = jit_cc_new_reg_I32(cc);
  656. GEN_INSN(CMP, cc->cmp_reg, right,
  657. is_i32 ? NEW_CONST(I32, 0) : NEW_CONST(I64, 0));
  658. /* Throw integer divided by zero exception if right is zero */
  659. if (!(jit_emit_exception(cc, EXCE_INTEGER_DIVIDE_BY_ZERO, JIT_OP_BEQ,
  660. cc->cmp_reg, NULL)))
  661. goto fail;
  662. switch (arith_op) {
  663. case INT_DIV_S:
  664. {
  665. /* Check integer overflow */
  666. GEN_INSN(CMP, cc->cmp_reg, left,
  667. is_i32 ? NEW_CONST(I32, INT32_MIN)
  668. : NEW_CONST(I64, INT64_MIN));
  669. GEN_INSN(SELECTEQ, cmp1, cc->cmp_reg, NEW_CONST(I32, 1),
  670. NEW_CONST(I32, 0));
  671. GEN_INSN(CMP, cc->cmp_reg, right,
  672. is_i32 ? NEW_CONST(I32, -1) : NEW_CONST(I64, -1LL));
  673. GEN_INSN(SELECTEQ, cmp2, cc->cmp_reg, NEW_CONST(I32, 1),
  674. NEW_CONST(I32, 0));
  675. GEN_INSN(AND, cmp1, cmp1, cmp2);
  676. GEN_INSN(CMP, cc->cmp_reg, cmp1, NEW_CONST(I32, 1));
  677. /* Throw integer overflow exception if left is INT32_MIN or
  678. INT64_MIN, and right is -1 */
  679. if (!(jit_emit_exception(cc, EXCE_INTEGER_OVERFLOW, JIT_OP_BEQ,
  680. cc->cmp_reg, NULL)))
  681. goto fail;
  682. /* Build default div and rem */
  683. return compile_int_div_no_check(cc, arith_op, is_i32, left,
  684. right, res);
  685. }
  686. case INT_REM_S:
  687. {
  688. JitReg left1 =
  689. is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  690. GEN_INSN(CMP, cc->cmp_reg, right,
  691. is_i32 ? NEW_CONST(I32, -1) : NEW_CONST(I64, -1LL));
  692. /* Don't generate `SELECTEQ left, cmp_reg, 0, left` since
  693. left might be const, use left1 instead */
  694. if (is_i32)
  695. GEN_INSN(SELECTEQ, left1, cc->cmp_reg, NEW_CONST(I32, 0),
  696. left);
  697. else
  698. GEN_INSN(SELECTEQ, left1, cc->cmp_reg, NEW_CONST(I64, 0),
  699. left);
  700. /* Build default div and rem */
  701. return compile_int_div_no_check(cc, arith_op, is_i32, left1,
  702. right, res);
  703. }
  704. default:
  705. {
  706. /* Build default div and rem */
  707. return compile_int_div_no_check(cc, arith_op, is_i32, left,
  708. right, res);
  709. }
  710. }
  711. }
  712. fail:
  713. return false;
  714. }
  715. static bool
  716. compile_op_int_arithmetic(JitCompContext *cc, IntArithmetic arith_op,
  717. bool is_i32, uint8 **p_frame_ip)
  718. {
  719. switch (arith_op) {
  720. case INT_ADD:
  721. DEF_INT_BINARY_OP(compile_int_add(cc, left, right, is_i32),
  722. "compile int add fail.");
  723. return true;
  724. case INT_SUB:
  725. DEF_INT_BINARY_OP(compile_int_sub(cc, left, right, is_i32),
  726. "compile int sub fail.");
  727. return true;
  728. case INT_MUL:
  729. DEF_INT_BINARY_OP(compile_int_mul(cc, left, right, is_i32),
  730. "compile int mul fail.");
  731. return true;
  732. case INT_DIV_S:
  733. case INT_DIV_U:
  734. case INT_REM_S:
  735. case INT_REM_U:
  736. return compile_int_div(cc, arith_op, is_i32, p_frame_ip);
  737. default:
  738. bh_assert(0);
  739. return false;
  740. }
  741. fail:
  742. return false;
  743. }
  744. bool
  745. jit_compile_op_i32_arithmetic(JitCompContext *cc, IntArithmetic arith_op,
  746. uint8 **p_frame_ip)
  747. {
  748. return compile_op_int_arithmetic(cc, arith_op, true, p_frame_ip);
  749. }
  750. bool
  751. jit_compile_op_i64_arithmetic(JitCompContext *cc, IntArithmetic arith_op,
  752. uint8 **p_frame_ip)
  753. {
  754. return compile_op_int_arithmetic(cc, arith_op, false, p_frame_ip);
  755. }
  756. DEF_UNI_INT_CONST_OPS(and)
  757. {
  758. JitReg res;
  759. if (IS_CONST_ZERO(left) || IS_CONST_ZERO(right)) {
  760. res = is_i32 ? NEW_CONST(I32, 0) : NEW_CONST(I64, 0);
  761. goto shortcut;
  762. }
  763. if (IS_CONST_ALL_ONE(left, is_i32)) {
  764. res = right;
  765. goto shortcut;
  766. }
  767. if (IS_CONST_ALL_ONE(right, is_i32)) {
  768. res = left;
  769. goto shortcut;
  770. }
  771. return 0;
  772. shortcut:
  773. return res;
  774. }
  775. DEF_BI_INT_CONST_OPS(and, &)
  776. static JitReg
  777. compile_int_and(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  778. {
  779. JitReg res;
  780. /* shortcuts */
  781. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, and);
  782. if (res)
  783. goto shortcut;
  784. /* do and */
  785. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  786. GEN_INSN(AND, res, left, right);
  787. shortcut:
  788. return res;
  789. }
  790. DEF_UNI_INT_CONST_OPS(or)
  791. {
  792. JitReg res;
  793. if (IS_CONST_ZERO(left)) {
  794. res = right;
  795. goto shortcut;
  796. }
  797. if (IS_CONST_ZERO(right)) {
  798. res = left;
  799. goto shortcut;
  800. }
  801. if (IS_CONST_ALL_ONE(left, is_i32) || IS_CONST_ALL_ONE(right, is_i32)) {
  802. res = is_i32 ? NEW_CONST(I32, -1) : NEW_CONST(I64, -1LL);
  803. goto shortcut;
  804. }
  805. return 0;
  806. shortcut:
  807. return res;
  808. }
  809. DEF_BI_INT_CONST_OPS(or, |)
  810. static JitReg
  811. compile_int_or(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  812. {
  813. JitReg res;
  814. /* shortcuts */
  815. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, or);
  816. if (res)
  817. goto shortcut;
  818. /* do or */
  819. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  820. GEN_INSN(OR, res, left, right);
  821. shortcut:
  822. return res;
  823. }
  824. DEF_UNI_INT_CONST_OPS(xor)
  825. {
  826. if (IS_CONST_ZERO(left))
  827. return right;
  828. if (IS_CONST_ZERO(right))
  829. return left;
  830. return 0;
  831. }
  832. DEF_BI_INT_CONST_OPS(xor, ^)
  833. static JitReg
  834. compile_int_xor(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  835. {
  836. JitReg res;
  837. /* shortcuts */
  838. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, xor);
  839. if (res)
  840. goto shortcut;
  841. /* do xor */
  842. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  843. GEN_INSN(XOR, res, left, right);
  844. shortcut:
  845. return res;
  846. }
  847. static bool
  848. compile_op_int_bitwise(JitCompContext *cc, IntBitwise arith_op, bool is_i32)
  849. {
  850. JitReg left, right, res;
  851. POP_INT(right);
  852. POP_INT(left);
  853. switch (arith_op) {
  854. case INT_AND:
  855. {
  856. res = compile_int_and(cc, left, right, is_i32);
  857. break;
  858. }
  859. case INT_OR:
  860. {
  861. res = compile_int_or(cc, left, right, is_i32);
  862. break;
  863. }
  864. case INT_XOR:
  865. {
  866. res = compile_int_xor(cc, left, right, is_i32);
  867. break;
  868. }
  869. default:
  870. {
  871. bh_assert(0);
  872. goto fail;
  873. }
  874. }
  875. PUSH_INT(res);
  876. return true;
  877. fail:
  878. return false;
  879. }
  880. bool
  881. jit_compile_op_i32_bitwise(JitCompContext *cc, IntBitwise bitwise_op)
  882. {
  883. return compile_op_int_bitwise(cc, bitwise_op, true);
  884. }
  885. bool
  886. jit_compile_op_i64_bitwise(JitCompContext *cc, IntBitwise bitwise_op)
  887. {
  888. return compile_op_int_bitwise(cc, bitwise_op, false);
  889. }
  890. DEF_UNI_INT_CONST_OPS(shl)
  891. {
  892. if (IS_CONST_ZERO(right) || IS_CONST_ZERO(left)) {
  893. return left;
  894. }
  895. if (jit_reg_is_const(right)) {
  896. JitReg res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  897. GEN_INSN(SHL, res, left, right);
  898. return res;
  899. }
  900. return 0;
  901. }
  902. DEF_UNI_INT_CONST_OPS(shrs)
  903. {
  904. if (IS_CONST_ZERO(right) || IS_CONST_ZERO(left)
  905. || IS_CONST_ALL_ONE(left, is_i32)) {
  906. return left;
  907. }
  908. if (jit_reg_is_const(right)) {
  909. JitReg res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  910. GEN_INSN(SHRS, res, left, right);
  911. return res;
  912. }
  913. return 0;
  914. }
  915. DEF_UNI_INT_CONST_OPS(shru)
  916. {
  917. if (IS_CONST_ZERO(right) || IS_CONST_ZERO(left)) {
  918. return left;
  919. }
  920. if (jit_reg_is_const(right)) {
  921. JitReg res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  922. GEN_INSN(SHRU, res, left, right);
  923. return res;
  924. }
  925. return 0;
  926. }
  927. static int32
  928. do_i32_const_shl(int32 lhs, int32 rhs)
  929. {
  930. rhs &= 31;
  931. return (int32)((uint32)lhs << (uint32)rhs);
  932. }
  933. static int64
  934. do_i64_const_shl(int64 lhs, int64 rhs)
  935. {
  936. rhs &= 63LL;
  937. return (uint64)lhs << (uint64)rhs;
  938. }
  939. DEF_BI_INT_CONST_OPS(shrs, >>)
  940. static int32
  941. do_i32_const_shru(int32 lhs, int32 rhs)
  942. {
  943. rhs &= 31;
  944. return (uint32)lhs >> rhs;
  945. }
  946. static int64
  947. do_i64_const_shru(int64 lhs, int64 rhs)
  948. {
  949. rhs &= 63LL;
  950. return (uint64)lhs >> rhs;
  951. }
  952. typedef enum { SHL, SHRS, SHRU, ROTL, ROTR } SHIFT_OP;
  953. static JitReg
  954. compile_int_shift_modulo(JitCompContext *cc, JitReg rhs, bool is_i32,
  955. SHIFT_OP op)
  956. {
  957. JitReg res;
  958. if (jit_reg_is_const(rhs)) {
  959. if (is_i32) {
  960. int32 val = jit_cc_get_const_I32(cc, rhs);
  961. val = val & 0x1f;
  962. res = NEW_CONST(I32, val);
  963. }
  964. else {
  965. int64 val = jit_cc_get_const_I64(cc, rhs);
  966. val = val & 0x3f;
  967. res = NEW_CONST(I64, val);
  968. }
  969. }
  970. else {
  971. if (op == ROTL || op == ROTR) {
  972. /* No need to generate AND insn as the result
  973. is same for rotate shift */
  974. res = rhs;
  975. }
  976. else if (is_i32) {
  977. res = jit_cc_new_reg_I32(cc);
  978. GEN_INSN(AND, res, rhs, NEW_CONST(I32, 0x1f));
  979. }
  980. else {
  981. res = jit_cc_new_reg_I64(cc);
  982. GEN_INSN(AND, res, rhs, NEW_CONST(I64, 0x3f));
  983. }
  984. }
  985. return res;
  986. }
  987. static JitReg
  988. mov_left_to_reg(JitCompContext *cc, bool is_i32, JitReg left)
  989. {
  990. JitReg res = left;
  991. /* left needs to be a variable */
  992. if (jit_reg_is_const(left)) {
  993. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  994. GEN_INSN(MOV, res, left);
  995. }
  996. return res;
  997. }
  998. static JitReg
  999. compile_int_shl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  1000. {
  1001. JitReg res;
  1002. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1003. JitReg ecx_hreg = jit_codegen_get_hreg_by_name("ecx");
  1004. JitReg rcx_hreg = jit_codegen_get_hreg_by_name("rcx");
  1005. JitInsn *insn = NULL;
  1006. #endif
  1007. right = compile_int_shift_modulo(cc, right, is_i32, SHL);
  1008. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, shl);
  1009. if (res)
  1010. goto shortcut;
  1011. left = mov_left_to_reg(cc, is_i32, left);
  1012. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  1013. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1014. GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
  1015. insn = GEN_INSN(SHL, res, left, is_i32 ? ecx_hreg : rcx_hreg);
  1016. if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
  1017. goto fail;
  1018. }
  1019. #else
  1020. GEN_INSN(SHL, res, left, right);
  1021. if (jit_get_last_error(cc)) {
  1022. goto fail;
  1023. }
  1024. #endif
  1025. shortcut:
  1026. return res;
  1027. fail:
  1028. return (JitReg)0;
  1029. }
  1030. static JitReg
  1031. compile_int_shrs(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  1032. {
  1033. JitReg res;
  1034. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1035. JitReg ecx_hreg = jit_codegen_get_hreg_by_name("ecx");
  1036. JitReg rcx_hreg = jit_codegen_get_hreg_by_name("rcx");
  1037. JitInsn *insn = NULL;
  1038. #endif
  1039. right = compile_int_shift_modulo(cc, right, is_i32, SHRS);
  1040. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, shrs);
  1041. if (res)
  1042. goto shortcut;
  1043. left = mov_left_to_reg(cc, is_i32, left);
  1044. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  1045. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1046. GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
  1047. insn = GEN_INSN(SHRS, res, left, is_i32 ? ecx_hreg : rcx_hreg);
  1048. if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
  1049. goto fail;
  1050. }
  1051. #else
  1052. GEN_INSN(SHRS, res, left, right);
  1053. if (jit_get_last_error(cc)) {
  1054. goto fail;
  1055. }
  1056. #endif
  1057. shortcut:
  1058. return res;
  1059. fail:
  1060. return (JitReg)0;
  1061. }
  1062. static JitReg
  1063. compile_int_shru(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  1064. {
  1065. JitReg res;
  1066. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1067. JitReg ecx_hreg = jit_codegen_get_hreg_by_name("ecx");
  1068. JitReg rcx_hreg = jit_codegen_get_hreg_by_name("rcx");
  1069. JitInsn *insn = NULL;
  1070. #endif
  1071. right = compile_int_shift_modulo(cc, right, is_i32, SHRU);
  1072. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, shru);
  1073. if (res)
  1074. goto shortcut;
  1075. left = mov_left_to_reg(cc, is_i32, left);
  1076. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  1077. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1078. GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
  1079. insn = GEN_INSN(SHRU, res, left, is_i32 ? ecx_hreg : rcx_hreg);
  1080. if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
  1081. goto fail;
  1082. }
  1083. #else
  1084. GEN_INSN(SHRU, res, left, right);
  1085. if (jit_get_last_error(cc)) {
  1086. goto fail;
  1087. }
  1088. #endif
  1089. shortcut:
  1090. return res;
  1091. fail:
  1092. return (JitReg)0;
  1093. }
  1094. DEF_UNI_INT_CONST_OPS(rotl)
  1095. {
  1096. if (IS_CONST_ZERO(right) || IS_CONST_ZERO(left)
  1097. || IS_CONST_ALL_ONE(left, is_i32))
  1098. return left;
  1099. if (jit_reg_is_const(right)) {
  1100. JitReg res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  1101. GEN_INSN(ROTL, res, left, right);
  1102. return res;
  1103. }
  1104. return 0;
  1105. }
  1106. static int32
  1107. do_i32_const_rotl(int32 lhs, int32 rhs)
  1108. {
  1109. uint32 n = (uint32)lhs;
  1110. uint32 d = (uint32)rhs;
  1111. return (n << d) | (n >> (32 - d));
  1112. }
  1113. static int64
  1114. do_i64_const_rotl(int64 lhs, int64 rhs)
  1115. {
  1116. uint64 n = (uint64)lhs;
  1117. uint64 d = (uint64)rhs;
  1118. return (n << d) | (n >> (64 - d));
  1119. }
  1120. static JitReg
  1121. compile_int_rotl(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  1122. {
  1123. JitReg res;
  1124. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1125. JitReg ecx_hreg = jit_codegen_get_hreg_by_name("ecx");
  1126. JitReg rcx_hreg = jit_codegen_get_hreg_by_name("rcx");
  1127. JitInsn *insn = NULL;
  1128. #endif
  1129. right = compile_int_shift_modulo(cc, right, is_i32, ROTL);
  1130. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, rotl);
  1131. if (res)
  1132. goto shortcut;
  1133. left = mov_left_to_reg(cc, is_i32, left);
  1134. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  1135. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1136. GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
  1137. insn = GEN_INSN(ROTL, res, left, is_i32 ? ecx_hreg : rcx_hreg);
  1138. if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
  1139. goto fail;
  1140. }
  1141. #else
  1142. GEN_INSN(ROTL, res, left, right);
  1143. if (jit_get_last_error(cc)) {
  1144. goto fail;
  1145. }
  1146. #endif
  1147. shortcut:
  1148. return res;
  1149. fail:
  1150. return (JitReg)0;
  1151. }
  1152. DEF_UNI_INT_CONST_OPS(rotr)
  1153. {
  1154. if (IS_CONST_ZERO(right) || IS_CONST_ZERO(left)
  1155. || IS_CONST_ALL_ONE(left, is_i32))
  1156. return left;
  1157. if (jit_reg_is_const(right)) {
  1158. JitReg res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  1159. GEN_INSN(ROTR, res, left, right);
  1160. return res;
  1161. }
  1162. return 0;
  1163. }
  1164. static int32
  1165. do_i32_const_rotr(int32 lhs, int32 rhs)
  1166. {
  1167. uint32 n = (uint32)lhs;
  1168. uint32 d = (uint32)rhs;
  1169. return (n >> d) | (n << (32 - d));
  1170. }
  1171. static int64
  1172. do_i64_const_rotr(int64 lhs, int64 rhs)
  1173. {
  1174. uint64 n = (uint64)lhs;
  1175. uint64 d = (uint64)rhs;
  1176. return (n >> d) | (n << (64 - d));
  1177. }
  1178. static JitReg
  1179. compile_int_rotr(JitCompContext *cc, JitReg left, JitReg right, bool is_i32)
  1180. {
  1181. JitReg res;
  1182. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1183. JitReg ecx_hreg = jit_codegen_get_hreg_by_name("ecx");
  1184. JitReg rcx_hreg = jit_codegen_get_hreg_by_name("rcx");
  1185. JitInsn *insn = NULL;
  1186. #endif
  1187. right = compile_int_shift_modulo(cc, right, is_i32, ROTR);
  1188. res = CHECK_AND_PROCESS_INT_CONSTS(cc, left, right, is_i32, rotr);
  1189. if (res)
  1190. goto shortcut;
  1191. left = mov_left_to_reg(cc, is_i32, left);
  1192. res = is_i32 ? jit_cc_new_reg_I32(cc) : jit_cc_new_reg_I64(cc);
  1193. #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  1194. GEN_INSN(MOV, is_i32 ? ecx_hreg : rcx_hreg, right);
  1195. insn = GEN_INSN(ROTR, res, left, is_i32 ? ecx_hreg : rcx_hreg);
  1196. if (jit_get_last_error(cc) || !jit_lock_reg_in_insn(cc, insn, ecx_hreg)) {
  1197. goto fail;
  1198. }
  1199. #else
  1200. GEN_INSN(ROTR, res, left, right);
  1201. if (jit_get_last_error(cc)) {
  1202. goto fail;
  1203. }
  1204. #endif
  1205. shortcut:
  1206. return res;
  1207. fail:
  1208. return (JitReg)0;
  1209. }
  1210. static bool
  1211. compile_op_int_shift(JitCompContext *cc, IntShift shift_op, bool is_i32)
  1212. {
  1213. JitReg left, right, res;
  1214. POP_INT(right);
  1215. POP_INT(left);
  1216. switch (shift_op) {
  1217. case INT_SHL:
  1218. {
  1219. res = compile_int_shl(cc, left, right, is_i32);
  1220. break;
  1221. }
  1222. case INT_SHR_S:
  1223. {
  1224. res = compile_int_shrs(cc, left, right, is_i32);
  1225. break;
  1226. }
  1227. case INT_SHR_U:
  1228. {
  1229. res = compile_int_shru(cc, left, right, is_i32);
  1230. break;
  1231. }
  1232. case INT_ROTL:
  1233. {
  1234. res = compile_int_rotl(cc, left, right, is_i32);
  1235. break;
  1236. }
  1237. case INT_ROTR:
  1238. {
  1239. res = compile_int_rotr(cc, left, right, is_i32);
  1240. break;
  1241. }
  1242. default:
  1243. {
  1244. bh_assert(0);
  1245. goto fail;
  1246. }
  1247. }
  1248. PUSH_INT(res);
  1249. return true;
  1250. fail:
  1251. return false;
  1252. }
  1253. bool
  1254. jit_compile_op_i32_shift(JitCompContext *cc, IntShift shift_op)
  1255. {
  1256. return compile_op_int_shift(cc, shift_op, true);
  1257. }
  1258. bool
  1259. jit_compile_op_i64_shift(JitCompContext *cc, IntShift shift_op)
  1260. {
  1261. return compile_op_int_shift(cc, shift_op, false);
  1262. }
  1263. static float32
  1264. negf(float32 f32)
  1265. {
  1266. return -f32;
  1267. }
  1268. static float64
  1269. neg(float64 f64)
  1270. {
  1271. return -f64;
  1272. }
  1273. static bool
  1274. compile_op_float_math(JitCompContext *cc, FloatMath math_op, bool is_f32)
  1275. {
  1276. JitReg value, res;
  1277. void *func = NULL;
  1278. if (is_f32)
  1279. res = jit_cc_new_reg_F32(cc);
  1280. else
  1281. res = jit_cc_new_reg_F64(cc);
  1282. if (is_f32)
  1283. POP_F32(value);
  1284. else
  1285. POP_F64(value);
  1286. switch (math_op) {
  1287. case FLOAT_ABS:
  1288. /* TODO: andps 0x7fffffffffffffff */
  1289. func = is_f32 ? (void *)fabsf : (void *)fabs;
  1290. break;
  1291. case FLOAT_NEG:
  1292. /* TODO: xorps 0x8000000000000000 */
  1293. func = is_f32 ? (void *)negf : (void *)neg;
  1294. break;
  1295. case FLOAT_CEIL:
  1296. func = is_f32 ? (void *)ceilf : (void *)ceil;
  1297. break;
  1298. case FLOAT_FLOOR:
  1299. func = is_f32 ? (void *)floorf : (void *)floor;
  1300. break;
  1301. case FLOAT_TRUNC:
  1302. func = is_f32 ? (void *)truncf : (void *)trunc;
  1303. break;
  1304. case FLOAT_NEAREST:
  1305. func = is_f32 ? (void *)rintf : (void *)rint;
  1306. break;
  1307. case FLOAT_SQRT:
  1308. func = is_f32 ? (void *)sqrtf : (void *)sqrt;
  1309. break;
  1310. default:
  1311. bh_assert(0);
  1312. goto fail;
  1313. }
  1314. if (!jit_emit_callnative(cc, func, res, &value, 1)) {
  1315. goto fail;
  1316. }
  1317. if (is_f32)
  1318. PUSH_F32(res);
  1319. else
  1320. PUSH_F64(res);
  1321. return true;
  1322. fail:
  1323. return false;
  1324. }
  1325. bool
  1326. jit_compile_op_f32_math(JitCompContext *cc, FloatMath math_op)
  1327. {
  1328. return compile_op_float_math(cc, math_op, true);
  1329. }
  1330. bool
  1331. jit_compile_op_f64_math(JitCompContext *cc, FloatMath math_op)
  1332. {
  1333. return compile_op_float_math(cc, math_op, false);
  1334. }
  1335. static float32
  1336. f32_min(float32 a, float32 b)
  1337. {
  1338. if (isnan(a) || isnan(b))
  1339. return NAN;
  1340. else if (a == 0 && a == b)
  1341. return signbit(a) ? a : b;
  1342. else
  1343. return a > b ? b : a;
  1344. }
  1345. static float32
  1346. f32_max(float32 a, float32 b)
  1347. {
  1348. if (isnan(a) || isnan(b))
  1349. return NAN;
  1350. else if (a == 0 && a == b)
  1351. return signbit(a) ? b : a;
  1352. else
  1353. return a > b ? a : b;
  1354. }
  1355. static float64
  1356. f64_min(float64 a, float64 b)
  1357. {
  1358. if (isnan(a) || isnan(b))
  1359. return NAN;
  1360. else if (a == 0 && a == b)
  1361. return signbit(a) ? a : b;
  1362. else
  1363. return a > b ? b : a;
  1364. }
  1365. static float64
  1366. f64_max(float64 a, float64 b)
  1367. {
  1368. if (isnan(a) || isnan(b))
  1369. return NAN;
  1370. else if (a == 0 && a == b)
  1371. return signbit(a) ? b : a;
  1372. else
  1373. return a > b ? a : b;
  1374. }
  1375. static bool
  1376. compile_op_float_min_max(JitCompContext *cc, FloatArithmetic arith_op,
  1377. bool is_f32, JitReg lhs, JitReg rhs, JitReg *out)
  1378. {
  1379. JitReg res, args[2];
  1380. void *func;
  1381. res = is_f32 ? jit_cc_new_reg_F32(cc) : jit_cc_new_reg_F64(cc);
  1382. if (arith_op == FLOAT_MIN)
  1383. func = is_f32 ? (void *)f32_min : (void *)f64_min;
  1384. else
  1385. func = is_f32 ? (void *)f32_max : (void *)f64_max;
  1386. args[0] = lhs;
  1387. args[1] = rhs;
  1388. if (!jit_emit_callnative(cc, func, res, args, 2))
  1389. return false;
  1390. *out = res;
  1391. return true;
  1392. }
  1393. static bool
  1394. compile_op_float_arithmetic(JitCompContext *cc, FloatArithmetic arith_op,
  1395. bool is_f32)
  1396. {
  1397. JitReg lhs, rhs, res;
  1398. if (is_f32) {
  1399. POP_F32(rhs);
  1400. POP_F32(lhs);
  1401. res = jit_cc_new_reg_F32(cc);
  1402. }
  1403. else {
  1404. POP_F64(rhs);
  1405. POP_F64(lhs);
  1406. res = jit_cc_new_reg_F64(cc);
  1407. }
  1408. switch (arith_op) {
  1409. case FLOAT_ADD:
  1410. {
  1411. GEN_INSN(ADD, res, lhs, rhs);
  1412. break;
  1413. }
  1414. case FLOAT_SUB:
  1415. {
  1416. GEN_INSN(SUB, res, lhs, rhs);
  1417. break;
  1418. }
  1419. case FLOAT_MUL:
  1420. {
  1421. GEN_INSN(MUL, res, lhs, rhs);
  1422. break;
  1423. }
  1424. case FLOAT_DIV:
  1425. {
  1426. GEN_INSN(DIV_S, res, lhs, rhs);
  1427. break;
  1428. }
  1429. case FLOAT_MIN:
  1430. case FLOAT_MAX:
  1431. {
  1432. if (!compile_op_float_min_max(cc, arith_op, is_f32, lhs, rhs, &res))
  1433. goto fail;
  1434. break;
  1435. }
  1436. default:
  1437. {
  1438. bh_assert(0);
  1439. goto fail;
  1440. }
  1441. }
  1442. if (is_f32)
  1443. PUSH_F32(res);
  1444. else
  1445. PUSH_F64(res);
  1446. return true;
  1447. fail:
  1448. return false;
  1449. }
  1450. bool
  1451. jit_compile_op_f32_arithmetic(JitCompContext *cc, FloatArithmetic arith_op)
  1452. {
  1453. return compile_op_float_arithmetic(cc, arith_op, true);
  1454. }
  1455. bool
  1456. jit_compile_op_f64_arithmetic(JitCompContext *cc, FloatArithmetic arith_op)
  1457. {
  1458. return compile_op_float_arithmetic(cc, arith_op, false);
  1459. }
  1460. bool
  1461. jit_compile_op_f32_copysign(JitCompContext *cc)
  1462. {
  1463. JitReg res;
  1464. JitReg args[2] = { 0 };
  1465. POP_F32(args[1]);
  1466. POP_F32(args[0]);
  1467. res = jit_cc_new_reg_F32(cc);
  1468. if (!jit_emit_callnative(cc, copysignf, res, args, 2))
  1469. goto fail;
  1470. PUSH_F32(res);
  1471. return true;
  1472. fail:
  1473. return false;
  1474. }
  1475. bool
  1476. jit_compile_op_f64_copysign(JitCompContext *cc)
  1477. {
  1478. JitReg res;
  1479. JitReg args[2] = { 0 };
  1480. POP_F64(args[1]);
  1481. POP_F64(args[0]);
  1482. res = jit_cc_new_reg_F64(cc);
  1483. if (!jit_emit_callnative(cc, copysign, res, args, 2))
  1484. goto fail;
  1485. PUSH_F64(res);
  1486. return true;
  1487. fail:
  1488. return false;
  1489. }