cc_helper.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Helpers for lazy condition code handling
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "cpu.h"
  20. #include "exec/helper-proto.h"
  21. static uint32_t compute_all_flags(CPUSPARCState *env)
  22. {
  23. return env->psr & PSR_ICC;
  24. }
  25. static uint32_t compute_C_flags(CPUSPARCState *env)
  26. {
  27. return env->psr & PSR_CARRY;
  28. }
  29. static inline uint32_t get_NZ_icc(int32_t dst)
  30. {
  31. uint32_t ret = 0;
  32. if (dst == 0) {
  33. ret = PSR_ZERO;
  34. } else if (dst < 0) {
  35. ret = PSR_NEG;
  36. }
  37. return ret;
  38. }
  39. #ifdef TARGET_SPARC64
  40. static uint32_t compute_all_flags_xcc(CPUSPARCState *env)
  41. {
  42. return env->xcc & PSR_ICC;
  43. }
  44. static uint32_t compute_C_flags_xcc(CPUSPARCState *env)
  45. {
  46. return env->xcc & PSR_CARRY;
  47. }
  48. static inline uint32_t get_NZ_xcc(target_long dst)
  49. {
  50. uint32_t ret = 0;
  51. if (!dst) {
  52. ret = PSR_ZERO;
  53. } else if (dst < 0) {
  54. ret = PSR_NEG;
  55. }
  56. return ret;
  57. }
  58. #endif
  59. static inline uint32_t get_V_div_icc(target_ulong src2)
  60. {
  61. uint32_t ret = 0;
  62. if (src2 != 0) {
  63. ret = PSR_OVF;
  64. }
  65. return ret;
  66. }
  67. static uint32_t compute_all_div(CPUSPARCState *env)
  68. {
  69. uint32_t ret;
  70. ret = get_NZ_icc(CC_DST);
  71. ret |= get_V_div_icc(CC_SRC2);
  72. return ret;
  73. }
  74. static uint32_t compute_C_div(CPUSPARCState *env)
  75. {
  76. return 0;
  77. }
  78. static inline uint32_t get_C_add_icc(uint32_t dst, uint32_t src1)
  79. {
  80. uint32_t ret = 0;
  81. if (dst < src1) {
  82. ret = PSR_CARRY;
  83. }
  84. return ret;
  85. }
  86. static inline uint32_t get_C_addx_icc(uint32_t dst, uint32_t src1,
  87. uint32_t src2)
  88. {
  89. uint32_t ret = 0;
  90. if (((src1 & src2) | (~dst & (src1 | src2))) & (1U << 31)) {
  91. ret = PSR_CARRY;
  92. }
  93. return ret;
  94. }
  95. static inline uint32_t get_V_add_icc(uint32_t dst, uint32_t src1,
  96. uint32_t src2)
  97. {
  98. uint32_t ret = 0;
  99. if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1U << 31)) {
  100. ret = PSR_OVF;
  101. }
  102. return ret;
  103. }
  104. #ifdef TARGET_SPARC64
  105. static inline uint32_t get_C_add_xcc(target_ulong dst, target_ulong src1)
  106. {
  107. uint32_t ret = 0;
  108. if (dst < src1) {
  109. ret = PSR_CARRY;
  110. }
  111. return ret;
  112. }
  113. static inline uint32_t get_C_addx_xcc(target_ulong dst, target_ulong src1,
  114. target_ulong src2)
  115. {
  116. uint32_t ret = 0;
  117. if (((src1 & src2) | (~dst & (src1 | src2))) & (1ULL << 63)) {
  118. ret = PSR_CARRY;
  119. }
  120. return ret;
  121. }
  122. static inline uint32_t get_V_add_xcc(target_ulong dst, target_ulong src1,
  123. target_ulong src2)
  124. {
  125. uint32_t ret = 0;
  126. if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1ULL << 63)) {
  127. ret = PSR_OVF;
  128. }
  129. return ret;
  130. }
  131. static uint32_t compute_all_add_xcc(CPUSPARCState *env)
  132. {
  133. uint32_t ret;
  134. ret = get_NZ_xcc(CC_DST);
  135. ret |= get_C_add_xcc(CC_DST, CC_SRC);
  136. ret |= get_V_add_xcc(CC_DST, CC_SRC, CC_SRC2);
  137. return ret;
  138. }
  139. static uint32_t compute_C_add_xcc(CPUSPARCState *env)
  140. {
  141. return get_C_add_xcc(CC_DST, CC_SRC);
  142. }
  143. #endif
  144. static uint32_t compute_all_add(CPUSPARCState *env)
  145. {
  146. uint32_t ret;
  147. ret = get_NZ_icc(CC_DST);
  148. ret |= get_C_add_icc(CC_DST, CC_SRC);
  149. ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
  150. return ret;
  151. }
  152. static uint32_t compute_C_add(CPUSPARCState *env)
  153. {
  154. return get_C_add_icc(CC_DST, CC_SRC);
  155. }
  156. #ifdef TARGET_SPARC64
  157. static uint32_t compute_all_addx_xcc(CPUSPARCState *env)
  158. {
  159. uint32_t ret;
  160. ret = get_NZ_xcc(CC_DST);
  161. ret |= get_C_addx_xcc(CC_DST, CC_SRC, CC_SRC2);
  162. ret |= get_V_add_xcc(CC_DST, CC_SRC, CC_SRC2);
  163. return ret;
  164. }
  165. static uint32_t compute_C_addx_xcc(CPUSPARCState *env)
  166. {
  167. uint32_t ret;
  168. ret = get_C_addx_xcc(CC_DST, CC_SRC, CC_SRC2);
  169. return ret;
  170. }
  171. #endif
  172. static uint32_t compute_all_addx(CPUSPARCState *env)
  173. {
  174. uint32_t ret;
  175. ret = get_NZ_icc(CC_DST);
  176. ret |= get_C_addx_icc(CC_DST, CC_SRC, CC_SRC2);
  177. ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
  178. return ret;
  179. }
  180. static uint32_t compute_C_addx(CPUSPARCState *env)
  181. {
  182. uint32_t ret;
  183. ret = get_C_addx_icc(CC_DST, CC_SRC, CC_SRC2);
  184. return ret;
  185. }
  186. static inline uint32_t get_V_tag_icc(target_ulong src1, target_ulong src2)
  187. {
  188. uint32_t ret = 0;
  189. if ((src1 | src2) & 0x3) {
  190. ret = PSR_OVF;
  191. }
  192. return ret;
  193. }
  194. static uint32_t compute_all_tadd(CPUSPARCState *env)
  195. {
  196. uint32_t ret;
  197. ret = get_NZ_icc(CC_DST);
  198. ret |= get_C_add_icc(CC_DST, CC_SRC);
  199. ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
  200. ret |= get_V_tag_icc(CC_SRC, CC_SRC2);
  201. return ret;
  202. }
  203. static uint32_t compute_all_taddtv(CPUSPARCState *env)
  204. {
  205. uint32_t ret;
  206. ret = get_NZ_icc(CC_DST);
  207. ret |= get_C_add_icc(CC_DST, CC_SRC);
  208. return ret;
  209. }
  210. static inline uint32_t get_C_sub_icc(uint32_t src1, uint32_t src2)
  211. {
  212. uint32_t ret = 0;
  213. if (src1 < src2) {
  214. ret = PSR_CARRY;
  215. }
  216. return ret;
  217. }
  218. static inline uint32_t get_C_subx_icc(uint32_t dst, uint32_t src1,
  219. uint32_t src2)
  220. {
  221. uint32_t ret = 0;
  222. if (((~src1 & src2) | (dst & (~src1 | src2))) & (1U << 31)) {
  223. ret = PSR_CARRY;
  224. }
  225. return ret;
  226. }
  227. static inline uint32_t get_V_sub_icc(uint32_t dst, uint32_t src1,
  228. uint32_t src2)
  229. {
  230. uint32_t ret = 0;
  231. if (((src1 ^ src2) & (src1 ^ dst)) & (1U << 31)) {
  232. ret = PSR_OVF;
  233. }
  234. return ret;
  235. }
  236. #ifdef TARGET_SPARC64
  237. static inline uint32_t get_C_sub_xcc(target_ulong src1, target_ulong src2)
  238. {
  239. uint32_t ret = 0;
  240. if (src1 < src2) {
  241. ret = PSR_CARRY;
  242. }
  243. return ret;
  244. }
  245. static inline uint32_t get_C_subx_xcc(target_ulong dst, target_ulong src1,
  246. target_ulong src2)
  247. {
  248. uint32_t ret = 0;
  249. if (((~src1 & src2) | (dst & (~src1 | src2))) & (1ULL << 63)) {
  250. ret = PSR_CARRY;
  251. }
  252. return ret;
  253. }
  254. static inline uint32_t get_V_sub_xcc(target_ulong dst, target_ulong src1,
  255. target_ulong src2)
  256. {
  257. uint32_t ret = 0;
  258. if (((src1 ^ src2) & (src1 ^ dst)) & (1ULL << 63)) {
  259. ret = PSR_OVF;
  260. }
  261. return ret;
  262. }
  263. static uint32_t compute_all_sub_xcc(CPUSPARCState *env)
  264. {
  265. uint32_t ret;
  266. ret = get_NZ_xcc(CC_DST);
  267. ret |= get_C_sub_xcc(CC_SRC, CC_SRC2);
  268. ret |= get_V_sub_xcc(CC_DST, CC_SRC, CC_SRC2);
  269. return ret;
  270. }
  271. static uint32_t compute_C_sub_xcc(CPUSPARCState *env)
  272. {
  273. return get_C_sub_xcc(CC_SRC, CC_SRC2);
  274. }
  275. #endif
  276. static uint32_t compute_all_sub(CPUSPARCState *env)
  277. {
  278. uint32_t ret;
  279. ret = get_NZ_icc(CC_DST);
  280. ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
  281. ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
  282. return ret;
  283. }
  284. static uint32_t compute_C_sub(CPUSPARCState *env)
  285. {
  286. return get_C_sub_icc(CC_SRC, CC_SRC2);
  287. }
  288. #ifdef TARGET_SPARC64
  289. static uint32_t compute_all_subx_xcc(CPUSPARCState *env)
  290. {
  291. uint32_t ret;
  292. ret = get_NZ_xcc(CC_DST);
  293. ret |= get_C_subx_xcc(CC_DST, CC_SRC, CC_SRC2);
  294. ret |= get_V_sub_xcc(CC_DST, CC_SRC, CC_SRC2);
  295. return ret;
  296. }
  297. static uint32_t compute_C_subx_xcc(CPUSPARCState *env)
  298. {
  299. uint32_t ret;
  300. ret = get_C_subx_xcc(CC_DST, CC_SRC, CC_SRC2);
  301. return ret;
  302. }
  303. #endif
  304. static uint32_t compute_all_subx(CPUSPARCState *env)
  305. {
  306. uint32_t ret;
  307. ret = get_NZ_icc(CC_DST);
  308. ret |= get_C_subx_icc(CC_DST, CC_SRC, CC_SRC2);
  309. ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
  310. return ret;
  311. }
  312. static uint32_t compute_C_subx(CPUSPARCState *env)
  313. {
  314. uint32_t ret;
  315. ret = get_C_subx_icc(CC_DST, CC_SRC, CC_SRC2);
  316. return ret;
  317. }
  318. static uint32_t compute_all_tsub(CPUSPARCState *env)
  319. {
  320. uint32_t ret;
  321. ret = get_NZ_icc(CC_DST);
  322. ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
  323. ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
  324. ret |= get_V_tag_icc(CC_SRC, CC_SRC2);
  325. return ret;
  326. }
  327. static uint32_t compute_all_tsubtv(CPUSPARCState *env)
  328. {
  329. uint32_t ret;
  330. ret = get_NZ_icc(CC_DST);
  331. ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
  332. return ret;
  333. }
  334. static uint32_t compute_all_logic(CPUSPARCState *env)
  335. {
  336. return get_NZ_icc(CC_DST);
  337. }
  338. static uint32_t compute_C_logic(CPUSPARCState *env)
  339. {
  340. return 0;
  341. }
  342. #ifdef TARGET_SPARC64
  343. static uint32_t compute_all_logic_xcc(CPUSPARCState *env)
  344. {
  345. return get_NZ_xcc(CC_DST);
  346. }
  347. #endif
  348. typedef struct CCTable {
  349. uint32_t (*compute_all)(CPUSPARCState *env); /* return all the flags */
  350. uint32_t (*compute_c)(CPUSPARCState *env); /* return the C flag */
  351. } CCTable;
  352. static const CCTable icc_table[CC_OP_NB] = {
  353. /* CC_OP_DYNAMIC should never happen */
  354. [CC_OP_FLAGS] = { compute_all_flags, compute_C_flags },
  355. [CC_OP_DIV] = { compute_all_div, compute_C_div },
  356. [CC_OP_ADD] = { compute_all_add, compute_C_add },
  357. [CC_OP_ADDX] = { compute_all_addx, compute_C_addx },
  358. [CC_OP_TADD] = { compute_all_tadd, compute_C_add },
  359. [CC_OP_TADDTV] = { compute_all_taddtv, compute_C_add },
  360. [CC_OP_SUB] = { compute_all_sub, compute_C_sub },
  361. [CC_OP_SUBX] = { compute_all_subx, compute_C_subx },
  362. [CC_OP_TSUB] = { compute_all_tsub, compute_C_sub },
  363. [CC_OP_TSUBTV] = { compute_all_tsubtv, compute_C_sub },
  364. [CC_OP_LOGIC] = { compute_all_logic, compute_C_logic },
  365. };
  366. #ifdef TARGET_SPARC64
  367. static const CCTable xcc_table[CC_OP_NB] = {
  368. /* CC_OP_DYNAMIC should never happen */
  369. [CC_OP_FLAGS] = { compute_all_flags_xcc, compute_C_flags_xcc },
  370. [CC_OP_DIV] = { compute_all_logic_xcc, compute_C_logic },
  371. [CC_OP_ADD] = { compute_all_add_xcc, compute_C_add_xcc },
  372. [CC_OP_ADDX] = { compute_all_addx_xcc, compute_C_addx_xcc },
  373. [CC_OP_TADD] = { compute_all_add_xcc, compute_C_add_xcc },
  374. [CC_OP_TADDTV] = { compute_all_add_xcc, compute_C_add_xcc },
  375. [CC_OP_SUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
  376. [CC_OP_SUBX] = { compute_all_subx_xcc, compute_C_subx_xcc },
  377. [CC_OP_TSUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
  378. [CC_OP_TSUBTV] = { compute_all_sub_xcc, compute_C_sub_xcc },
  379. [CC_OP_LOGIC] = { compute_all_logic_xcc, compute_C_logic },
  380. };
  381. #endif
  382. void helper_compute_psr(CPUSPARCState *env)
  383. {
  384. uint32_t new_psr;
  385. new_psr = icc_table[CC_OP].compute_all(env);
  386. env->psr = new_psr;
  387. #ifdef TARGET_SPARC64
  388. new_psr = xcc_table[CC_OP].compute_all(env);
  389. env->xcc = new_psr;
  390. #endif
  391. CC_OP = CC_OP_FLAGS;
  392. }
  393. uint32_t helper_compute_C_icc(CPUSPARCState *env)
  394. {
  395. uint32_t ret;
  396. ret = icc_table[CC_OP].compute_c(env) >> PSR_CARRY_SHIFT;
  397. return ret;
  398. }