JITArithmetic.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. /*
  2. * Copyright (C) 2008 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #if ENABLE(JIT)
  27. #include "JIT.h"
  28. #include "CodeBlock.h"
  29. #include "JITInlines.h"
  30. #include "JITStubCall.h"
  31. #include "JITStubs.h"
  32. #include "JSArray.h"
  33. #include "JSFunction.h"
  34. #include "Interpreter.h"
  35. #include "Operations.h"
  36. #include "ResultType.h"
  37. #include "SamplingTool.h"
  38. #ifndef NDEBUG
  39. #include <stdio.h>
  40. #endif
  41. using namespace std;
  42. namespace JSC {
  43. void JIT::emit_op_jless(Instruction* currentInstruction)
  44. {
  45. unsigned op1 = currentInstruction[1].u.operand;
  46. unsigned op2 = currentInstruction[2].u.operand;
  47. unsigned target = currentInstruction[3].u.operand;
  48. emit_compareAndJump(op_jless, op1, op2, target, LessThan);
  49. }
  50. void JIT::emit_op_jlesseq(Instruction* currentInstruction)
  51. {
  52. unsigned op1 = currentInstruction[1].u.operand;
  53. unsigned op2 = currentInstruction[2].u.operand;
  54. unsigned target = currentInstruction[3].u.operand;
  55. emit_compareAndJump(op_jlesseq, op1, op2, target, LessThanOrEqual);
  56. }
  57. void JIT::emit_op_jgreater(Instruction* currentInstruction)
  58. {
  59. unsigned op1 = currentInstruction[1].u.operand;
  60. unsigned op2 = currentInstruction[2].u.operand;
  61. unsigned target = currentInstruction[3].u.operand;
  62. emit_compareAndJump(op_jgreater, op1, op2, target, GreaterThan);
  63. }
  64. void JIT::emit_op_jgreatereq(Instruction* currentInstruction)
  65. {
  66. unsigned op1 = currentInstruction[1].u.operand;
  67. unsigned op2 = currentInstruction[2].u.operand;
  68. unsigned target = currentInstruction[3].u.operand;
  69. emit_compareAndJump(op_jgreatereq, op1, op2, target, GreaterThanOrEqual);
  70. }
  71. void JIT::emit_op_jnless(Instruction* currentInstruction)
  72. {
  73. unsigned op1 = currentInstruction[1].u.operand;
  74. unsigned op2 = currentInstruction[2].u.operand;
  75. unsigned target = currentInstruction[3].u.operand;
  76. emit_compareAndJump(op_jnless, op1, op2, target, GreaterThanOrEqual);
  77. }
  78. void JIT::emit_op_jnlesseq(Instruction* currentInstruction)
  79. {
  80. unsigned op1 = currentInstruction[1].u.operand;
  81. unsigned op2 = currentInstruction[2].u.operand;
  82. unsigned target = currentInstruction[3].u.operand;
  83. emit_compareAndJump(op_jnlesseq, op1, op2, target, GreaterThan);
  84. }
  85. void JIT::emit_op_jngreater(Instruction* currentInstruction)
  86. {
  87. unsigned op1 = currentInstruction[1].u.operand;
  88. unsigned op2 = currentInstruction[2].u.operand;
  89. unsigned target = currentInstruction[3].u.operand;
  90. emit_compareAndJump(op_jngreater, op1, op2, target, LessThanOrEqual);
  91. }
  92. void JIT::emit_op_jngreatereq(Instruction* currentInstruction)
  93. {
  94. unsigned op1 = currentInstruction[1].u.operand;
  95. unsigned op2 = currentInstruction[2].u.operand;
  96. unsigned target = currentInstruction[3].u.operand;
  97. emit_compareAndJump(op_jngreatereq, op1, op2, target, LessThan);
  98. }
  99. void JIT::emitSlow_op_jless(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  100. {
  101. unsigned op1 = currentInstruction[1].u.operand;
  102. unsigned op2 = currentInstruction[2].u.operand;
  103. unsigned target = currentInstruction[3].u.operand;
  104. emit_compareAndJumpSlow(op1, op2, target, DoubleLessThan, cti_op_jless, false, iter);
  105. }
  106. void JIT::emitSlow_op_jlesseq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  107. {
  108. unsigned op1 = currentInstruction[1].u.operand;
  109. unsigned op2 = currentInstruction[2].u.operand;
  110. unsigned target = currentInstruction[3].u.operand;
  111. emit_compareAndJumpSlow(op1, op2, target, DoubleLessThanOrEqual, cti_op_jlesseq, false, iter);
  112. }
  113. void JIT::emitSlow_op_jgreater(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  114. {
  115. unsigned op1 = currentInstruction[1].u.operand;
  116. unsigned op2 = currentInstruction[2].u.operand;
  117. unsigned target = currentInstruction[3].u.operand;
  118. emit_compareAndJumpSlow(op1, op2, target, DoubleGreaterThan, cti_op_jgreater, false, iter);
  119. }
  120. void JIT::emitSlow_op_jgreatereq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  121. {
  122. unsigned op1 = currentInstruction[1].u.operand;
  123. unsigned op2 = currentInstruction[2].u.operand;
  124. unsigned target = currentInstruction[3].u.operand;
  125. emit_compareAndJumpSlow(op1, op2, target, DoubleGreaterThanOrEqual, cti_op_jgreatereq, false, iter);
  126. }
  127. void JIT::emitSlow_op_jnless(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  128. {
  129. unsigned op1 = currentInstruction[1].u.operand;
  130. unsigned op2 = currentInstruction[2].u.operand;
  131. unsigned target = currentInstruction[3].u.operand;
  132. emit_compareAndJumpSlow(op1, op2, target, DoubleGreaterThanOrEqualOrUnordered, cti_op_jless, true, iter);
  133. }
  134. void JIT::emitSlow_op_jnlesseq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  135. {
  136. unsigned op1 = currentInstruction[1].u.operand;
  137. unsigned op2 = currentInstruction[2].u.operand;
  138. unsigned target = currentInstruction[3].u.operand;
  139. emit_compareAndJumpSlow(op1, op2, target, DoubleGreaterThanOrUnordered, cti_op_jlesseq, true, iter);
  140. }
  141. void JIT::emitSlow_op_jngreater(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  142. {
  143. unsigned op1 = currentInstruction[1].u.operand;
  144. unsigned op2 = currentInstruction[2].u.operand;
  145. unsigned target = currentInstruction[3].u.operand;
  146. emit_compareAndJumpSlow(op1, op2, target, DoubleLessThanOrEqualOrUnordered, cti_op_jgreater, true, iter);
  147. }
  148. void JIT::emitSlow_op_jngreatereq(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  149. {
  150. unsigned op1 = currentInstruction[1].u.operand;
  151. unsigned op2 = currentInstruction[2].u.operand;
  152. unsigned target = currentInstruction[3].u.operand;
  153. emit_compareAndJumpSlow(op1, op2, target, DoubleLessThanOrUnordered, cti_op_jgreatereq, true, iter);
  154. }
  155. #if USE(JSVALUE64)
  156. void JIT::emit_op_negate(Instruction* currentInstruction)
  157. {
  158. unsigned dst = currentInstruction[1].u.operand;
  159. unsigned src = currentInstruction[2].u.operand;
  160. emitGetVirtualRegister(src, regT0);
  161. Jump srcNotInt = emitJumpIfNotImmediateInteger(regT0);
  162. addSlowCase(branchTest32(Zero, regT0, TrustedImm32(0x7fffffff)));
  163. neg32(regT0);
  164. emitFastArithReTagImmediate(regT0, regT0);
  165. Jump end = jump();
  166. srcNotInt.link(this);
  167. emitJumpSlowCaseIfNotImmediateNumber(regT0);
  168. move(TrustedImm64((int64_t)0x8000000000000000ull), regT1);
  169. xor64(regT1, regT0);
  170. end.link(this);
  171. emitPutVirtualRegister(dst);
  172. }
  173. void JIT::emitSlow_op_negate(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  174. {
  175. unsigned dst = currentInstruction[1].u.operand;
  176. linkSlowCase(iter); // 0x7fffffff check
  177. linkSlowCase(iter); // double check
  178. JITStubCall stubCall(this, cti_op_negate);
  179. stubCall.addArgument(regT0);
  180. stubCall.call(dst);
  181. }
  182. void JIT::emit_op_lshift(Instruction* currentInstruction)
  183. {
  184. unsigned result = currentInstruction[1].u.operand;
  185. unsigned op1 = currentInstruction[2].u.operand;
  186. unsigned op2 = currentInstruction[3].u.operand;
  187. emitGetVirtualRegisters(op1, regT0, op2, regT2);
  188. // FIXME: would we be better using 'emitJumpSlowCaseIfNotImmediateIntegers'? - we *probably* ought to be consistent.
  189. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  190. emitJumpSlowCaseIfNotImmediateInteger(regT2);
  191. emitFastArithImmToInt(regT0);
  192. emitFastArithImmToInt(regT2);
  193. lshift32(regT2, regT0);
  194. emitFastArithReTagImmediate(regT0, regT0);
  195. emitPutVirtualRegister(result);
  196. }
  197. void JIT::emitSlow_op_lshift(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  198. {
  199. unsigned result = currentInstruction[1].u.operand;
  200. unsigned op1 = currentInstruction[2].u.operand;
  201. unsigned op2 = currentInstruction[3].u.operand;
  202. UNUSED_PARAM(op1);
  203. UNUSED_PARAM(op2);
  204. linkSlowCase(iter);
  205. linkSlowCase(iter);
  206. JITStubCall stubCall(this, cti_op_lshift);
  207. stubCall.addArgument(regT0);
  208. stubCall.addArgument(regT2);
  209. stubCall.call(result);
  210. }
  211. void JIT::emit_op_rshift(Instruction* currentInstruction)
  212. {
  213. unsigned result = currentInstruction[1].u.operand;
  214. unsigned op1 = currentInstruction[2].u.operand;
  215. unsigned op2 = currentInstruction[3].u.operand;
  216. if (isOperandConstantImmediateInt(op2)) {
  217. // isOperandConstantImmediateInt(op2) => 1 SlowCase
  218. emitGetVirtualRegister(op1, regT0);
  219. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  220. // Mask with 0x1f as per ecma-262 11.7.2 step 7.
  221. rshift32(Imm32(getConstantOperandImmediateInt(op2) & 0x1f), regT0);
  222. } else {
  223. emitGetVirtualRegisters(op1, regT0, op2, regT2);
  224. if (supportsFloatingPointTruncate()) {
  225. Jump lhsIsInt = emitJumpIfImmediateInteger(regT0);
  226. // supportsFloatingPoint() && USE(JSVALUE64) => 3 SlowCases
  227. addSlowCase(emitJumpIfNotImmediateNumber(regT0));
  228. add64(tagTypeNumberRegister, regT0);
  229. move64ToDouble(regT0, fpRegT0);
  230. addSlowCase(branchTruncateDoubleToInt32(fpRegT0, regT0));
  231. lhsIsInt.link(this);
  232. emitJumpSlowCaseIfNotImmediateInteger(regT2);
  233. } else {
  234. // !supportsFloatingPoint() => 2 SlowCases
  235. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  236. emitJumpSlowCaseIfNotImmediateInteger(regT2);
  237. }
  238. emitFastArithImmToInt(regT2);
  239. rshift32(regT2, regT0);
  240. }
  241. emitFastArithIntToImmNoCheck(regT0, regT0);
  242. emitPutVirtualRegister(result);
  243. }
  244. void JIT::emitSlow_op_rshift(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  245. {
  246. unsigned result = currentInstruction[1].u.operand;
  247. unsigned op1 = currentInstruction[2].u.operand;
  248. unsigned op2 = currentInstruction[3].u.operand;
  249. JITStubCall stubCall(this, cti_op_rshift);
  250. if (isOperandConstantImmediateInt(op2)) {
  251. linkSlowCase(iter);
  252. stubCall.addArgument(regT0);
  253. stubCall.addArgument(op2, regT2);
  254. } else {
  255. if (supportsFloatingPointTruncate()) {
  256. linkSlowCase(iter);
  257. linkSlowCase(iter);
  258. linkSlowCase(iter);
  259. // We're reloading op1 to regT0 as we can no longer guarantee that
  260. // we have not munged the operand. It may have already been shifted
  261. // correctly, but it still will not have been tagged.
  262. stubCall.addArgument(op1, regT0);
  263. stubCall.addArgument(regT2);
  264. } else {
  265. linkSlowCase(iter);
  266. linkSlowCase(iter);
  267. stubCall.addArgument(regT0);
  268. stubCall.addArgument(regT2);
  269. }
  270. }
  271. stubCall.call(result);
  272. }
  273. void JIT::emit_op_urshift(Instruction* currentInstruction)
  274. {
  275. unsigned dst = currentInstruction[1].u.operand;
  276. unsigned op1 = currentInstruction[2].u.operand;
  277. unsigned op2 = currentInstruction[3].u.operand;
  278. // Slow case of urshift makes assumptions about what registers hold the
  279. // shift arguments, so any changes must be updated there as well.
  280. if (isOperandConstantImmediateInt(op2)) {
  281. emitGetVirtualRegister(op1, regT0);
  282. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  283. emitFastArithImmToInt(regT0);
  284. int shift = getConstantOperand(op2).asInt32();
  285. if (shift)
  286. urshift32(Imm32(shift & 0x1f), regT0);
  287. // unsigned shift < 0 or shift = k*2^32 may result in (essentially)
  288. // a toUint conversion, which can result in a value we can represent
  289. // as an immediate int.
  290. if (shift < 0 || !(shift & 31))
  291. addSlowCase(branch32(LessThan, regT0, TrustedImm32(0)));
  292. emitFastArithReTagImmediate(regT0, regT0);
  293. emitPutVirtualRegister(dst, regT0);
  294. return;
  295. }
  296. emitGetVirtualRegisters(op1, regT0, op2, regT1);
  297. if (!isOperandConstantImmediateInt(op1))
  298. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  299. emitJumpSlowCaseIfNotImmediateInteger(regT1);
  300. emitFastArithImmToInt(regT0);
  301. emitFastArithImmToInt(regT1);
  302. urshift32(regT1, regT0);
  303. addSlowCase(branch32(LessThan, regT0, TrustedImm32(0)));
  304. emitFastArithReTagImmediate(regT0, regT0);
  305. emitPutVirtualRegister(dst, regT0);
  306. }
  307. void JIT::emitSlow_op_urshift(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  308. {
  309. unsigned dst = currentInstruction[1].u.operand;
  310. unsigned op1 = currentInstruction[2].u.operand;
  311. unsigned op2 = currentInstruction[3].u.operand;
  312. if (isOperandConstantImmediateInt(op2)) {
  313. int shift = getConstantOperand(op2).asInt32();
  314. // op1 = regT0
  315. linkSlowCase(iter); // int32 check
  316. if (supportsFloatingPointTruncate()) {
  317. JumpList failures;
  318. failures.append(emitJumpIfNotImmediateNumber(regT0)); // op1 is not a double
  319. add64(tagTypeNumberRegister, regT0);
  320. move64ToDouble(regT0, fpRegT0);
  321. failures.append(branchTruncateDoubleToInt32(fpRegT0, regT0));
  322. if (shift)
  323. urshift32(Imm32(shift & 0x1f), regT0);
  324. if (shift < 0 || !(shift & 31))
  325. failures.append(branch32(LessThan, regT0, TrustedImm32(0)));
  326. emitFastArithReTagImmediate(regT0, regT0);
  327. emitPutVirtualRegister(dst, regT0);
  328. emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_rshift));
  329. failures.link(this);
  330. }
  331. if (shift < 0 || !(shift & 31))
  332. linkSlowCase(iter); // failed to box in hot path
  333. } else {
  334. // op1 = regT0
  335. // op2 = regT1
  336. if (!isOperandConstantImmediateInt(op1)) {
  337. linkSlowCase(iter); // int32 check -- op1 is not an int
  338. if (supportsFloatingPointTruncate()) {
  339. JumpList failures;
  340. failures.append(emitJumpIfNotImmediateNumber(regT0)); // op1 is not a double
  341. add64(tagTypeNumberRegister, regT0);
  342. move64ToDouble(regT0, fpRegT0);
  343. failures.append(branchTruncateDoubleToInt32(fpRegT0, regT0));
  344. failures.append(emitJumpIfNotImmediateInteger(regT1)); // op2 is not an int
  345. emitFastArithImmToInt(regT1);
  346. urshift32(regT1, regT0);
  347. failures.append(branch32(LessThan, regT0, TrustedImm32(0)));
  348. emitFastArithReTagImmediate(regT0, regT0);
  349. emitPutVirtualRegister(dst, regT0);
  350. emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_rshift));
  351. failures.link(this);
  352. }
  353. }
  354. linkSlowCase(iter); // int32 check - op2 is not an int
  355. linkSlowCase(iter); // Can't represent unsigned result as an immediate
  356. }
  357. JITStubCall stubCall(this, cti_op_urshift);
  358. stubCall.addArgument(op1, regT0);
  359. stubCall.addArgument(op2, regT1);
  360. stubCall.call(dst);
  361. }
  362. void JIT::emit_compareAndJump(OpcodeID, unsigned op1, unsigned op2, unsigned target, RelationalCondition condition)
  363. {
  364. // We generate inline code for the following cases in the fast path:
  365. // - int immediate to constant int immediate
  366. // - constant int immediate to int immediate
  367. // - int immediate to int immediate
  368. #if !ENABLE(DETACHED_JIT)
  369. if (isOperandConstantImmediateChar(op1)) {
  370. emitGetVirtualRegister(op2, regT0);
  371. addSlowCase(emitJumpIfNotJSCell(regT0));
  372. JumpList failures;
  373. emitLoadCharacterString(regT0, regT0, failures);
  374. addSlowCase(failures);
  375. addJump(branch32(commute(condition), regT0, Imm32(asString(getConstantOperand(op1))->tryGetValue()[0])), target);
  376. return;
  377. }
  378. if (isOperandConstantImmediateChar(op2)) {
  379. emitGetVirtualRegister(op1, regT0);
  380. addSlowCase(emitJumpIfNotJSCell(regT0));
  381. JumpList failures;
  382. emitLoadCharacterString(regT0, regT0, failures);
  383. addSlowCase(failures);
  384. addJump(branch32(condition, regT0, Imm32(asString(getConstantOperand(op2))->tryGetValue()[0])), target);
  385. return;
  386. }
  387. #endif
  388. if (isOperandConstantImmediateInt(op2)) {
  389. emitGetVirtualRegister(op1, regT0);
  390. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  391. int32_t op2imm = getConstantOperandImmediateInt(op2);
  392. addJump(branch32(condition, regT0, Imm32(op2imm)), target);
  393. } else if (isOperandConstantImmediateInt(op1)) {
  394. emitGetVirtualRegister(op2, regT1);
  395. emitJumpSlowCaseIfNotImmediateInteger(regT1);
  396. int32_t op1imm = getConstantOperandImmediateInt(op1);
  397. addJump(branch32(commute(condition), regT1, Imm32(op1imm)), target);
  398. } else {
  399. emitGetVirtualRegisters(op1, regT0, op2, regT1);
  400. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  401. emitJumpSlowCaseIfNotImmediateInteger(regT1);
  402. addJump(branch32(condition, regT0, regT1), target);
  403. }
  404. }
  405. void JIT::emit_compareAndJumpSlow(unsigned op1, unsigned op2, unsigned target, DoubleCondition condition, int (JIT_STUB *stub)(STUB_ARGS_DECLARATION), bool invert, Vector<SlowCaseEntry>::iterator& iter)
  406. {
  407. COMPILE_ASSERT(OPCODE_LENGTH(op_jless) == OPCODE_LENGTH(op_jlesseq), OPCODE_LENGTH_op_jlesseq_equals_op_jless);
  408. COMPILE_ASSERT(OPCODE_LENGTH(op_jless) == OPCODE_LENGTH(op_jnless), OPCODE_LENGTH_op_jnless_equals_op_jless);
  409. COMPILE_ASSERT(OPCODE_LENGTH(op_jless) == OPCODE_LENGTH(op_jnlesseq), OPCODE_LENGTH_op_jnlesseq_equals_op_jless);
  410. COMPILE_ASSERT(OPCODE_LENGTH(op_jless) == OPCODE_LENGTH(op_jgreater), OPCODE_LENGTH_op_jgreater_equals_op_jless);
  411. COMPILE_ASSERT(OPCODE_LENGTH(op_jless) == OPCODE_LENGTH(op_jgreatereq), OPCODE_LENGTH_op_jgreatereq_equals_op_jless);
  412. COMPILE_ASSERT(OPCODE_LENGTH(op_jless) == OPCODE_LENGTH(op_jngreater), OPCODE_LENGTH_op_jngreater_equals_op_jless);
  413. COMPILE_ASSERT(OPCODE_LENGTH(op_jless) == OPCODE_LENGTH(op_jngreatereq), OPCODE_LENGTH_op_jngreatereq_equals_op_jless);
  414. // We generate inline code for the following cases in the slow path:
  415. // - floating-point number to constant int immediate
  416. // - constant int immediate to floating-point number
  417. // - floating-point number to floating-point number.
  418. #if !ENABLE(DETACHED_JIT)
  419. if (isOperandConstantImmediateChar(op1) || isOperandConstantImmediateChar(op2)) {
  420. linkSlowCase(iter);
  421. linkSlowCase(iter);
  422. linkSlowCase(iter);
  423. linkSlowCase(iter);
  424. JITStubCall stubCall(this, stub);
  425. stubCall.addArgument(op1, regT0);
  426. stubCall.addArgument(op2, regT1);
  427. stubCall.call();
  428. emitJumpSlowToHot(branchTest32(invert ? Zero : NonZero, regT0), target);
  429. return;
  430. }
  431. #endif
  432. if (isOperandConstantImmediateInt(op2)) {
  433. linkSlowCase(iter);
  434. if (supportsFloatingPoint()) {
  435. Jump fail1 = emitJumpIfNotImmediateNumber(regT0);
  436. add64(tagTypeNumberRegister, regT0);
  437. move64ToDouble(regT0, fpRegT0);
  438. int32_t op2imm = getConstantOperand(op2).asInt32();
  439. move(Imm32(op2imm), regT1);
  440. convertInt32ToDouble(regT1, fpRegT1);
  441. emitJumpSlowToHot(branchDouble(condition, fpRegT0, fpRegT1), target);
  442. emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_jless));
  443. fail1.link(this);
  444. }
  445. JITStubCall stubCall(this, stub);
  446. stubCall.addArgument(regT0);
  447. stubCall.addArgument(op2, regT2);
  448. stubCall.call();
  449. emitJumpSlowToHot(branchTest32(invert ? Zero : NonZero, regT0), target);
  450. } else if (isOperandConstantImmediateInt(op1)) {
  451. linkSlowCase(iter);
  452. if (supportsFloatingPoint()) {
  453. Jump fail1 = emitJumpIfNotImmediateNumber(regT1);
  454. add64(tagTypeNumberRegister, regT1);
  455. move64ToDouble(regT1, fpRegT1);
  456. int32_t op1imm = getConstantOperand(op1).asInt32();
  457. move(Imm32(op1imm), regT0);
  458. convertInt32ToDouble(regT0, fpRegT0);
  459. emitJumpSlowToHot(branchDouble(condition, fpRegT0, fpRegT1), target);
  460. emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_jless));
  461. fail1.link(this);
  462. }
  463. JITStubCall stubCall(this, stub);
  464. stubCall.addArgument(op1, regT2);
  465. stubCall.addArgument(regT1);
  466. stubCall.call();
  467. emitJumpSlowToHot(branchTest32(invert ? Zero : NonZero, regT0), target);
  468. } else {
  469. linkSlowCase(iter);
  470. if (supportsFloatingPoint()) {
  471. Jump fail1 = emitJumpIfNotImmediateNumber(regT0);
  472. Jump fail2 = emitJumpIfNotImmediateNumber(regT1);
  473. Jump fail3 = emitJumpIfImmediateInteger(regT1);
  474. add64(tagTypeNumberRegister, regT0);
  475. add64(tagTypeNumberRegister, regT1);
  476. move64ToDouble(regT0, fpRegT0);
  477. move64ToDouble(regT1, fpRegT1);
  478. emitJumpSlowToHot(branchDouble(condition, fpRegT0, fpRegT1), target);
  479. emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_jless));
  480. fail1.link(this);
  481. fail2.link(this);
  482. fail3.link(this);
  483. }
  484. linkSlowCase(iter);
  485. JITStubCall stubCall(this, stub);
  486. stubCall.addArgument(regT0);
  487. stubCall.addArgument(regT1);
  488. stubCall.call();
  489. emitJumpSlowToHot(branchTest32(invert ? Zero : NonZero, regT0), target);
  490. }
  491. }
  492. void JIT::emit_op_bitand(Instruction* currentInstruction)
  493. {
  494. unsigned result = currentInstruction[1].u.operand;
  495. unsigned op1 = currentInstruction[2].u.operand;
  496. unsigned op2 = currentInstruction[3].u.operand;
  497. if (isOperandConstantImmediateInt(op1)) {
  498. emitGetVirtualRegister(op2, regT0);
  499. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  500. int32_t imm = getConstantOperandImmediateInt(op1);
  501. and64(Imm32(imm), regT0);
  502. if (imm >= 0)
  503. emitFastArithIntToImmNoCheck(regT0, regT0);
  504. } else if (isOperandConstantImmediateInt(op2)) {
  505. emitGetVirtualRegister(op1, regT0);
  506. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  507. int32_t imm = getConstantOperandImmediateInt(op2);
  508. and64(Imm32(imm), regT0);
  509. if (imm >= 0)
  510. emitFastArithIntToImmNoCheck(regT0, regT0);
  511. } else {
  512. emitGetVirtualRegisters(op1, regT0, op2, regT1);
  513. and64(regT1, regT0);
  514. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  515. }
  516. emitPutVirtualRegister(result);
  517. }
  518. void JIT::emitSlow_op_bitand(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  519. {
  520. unsigned result = currentInstruction[1].u.operand;
  521. unsigned op1 = currentInstruction[2].u.operand;
  522. unsigned op2 = currentInstruction[3].u.operand;
  523. linkSlowCase(iter);
  524. if (isOperandConstantImmediateInt(op1)) {
  525. JITStubCall stubCall(this, cti_op_bitand);
  526. stubCall.addArgument(op1, regT2);
  527. stubCall.addArgument(regT0);
  528. stubCall.call(result);
  529. } else if (isOperandConstantImmediateInt(op2)) {
  530. JITStubCall stubCall(this, cti_op_bitand);
  531. stubCall.addArgument(regT0);
  532. stubCall.addArgument(op2, regT2);
  533. stubCall.call(result);
  534. } else {
  535. JITStubCall stubCall(this, cti_op_bitand);
  536. stubCall.addArgument(op1, regT2);
  537. stubCall.addArgument(regT1);
  538. stubCall.call(result);
  539. }
  540. }
  541. void JIT::emit_op_inc(Instruction* currentInstruction)
  542. {
  543. unsigned srcDst = currentInstruction[1].u.operand;
  544. emitGetVirtualRegister(srcDst, regT0);
  545. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  546. addSlowCase(branchAdd32(Overflow, TrustedImm32(1), regT0));
  547. emitFastArithIntToImmNoCheck(regT0, regT0);
  548. emitPutVirtualRegister(srcDst);
  549. }
  550. void JIT::emitSlow_op_inc(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  551. {
  552. unsigned srcDst = currentInstruction[1].u.operand;
  553. Jump notImm = getSlowCase(iter);
  554. linkSlowCase(iter);
  555. emitGetVirtualRegister(srcDst, regT0);
  556. notImm.link(this);
  557. JITStubCall stubCall(this, cti_op_inc);
  558. stubCall.addArgument(regT0);
  559. stubCall.call(srcDst);
  560. }
  561. void JIT::emit_op_dec(Instruction* currentInstruction)
  562. {
  563. unsigned srcDst = currentInstruction[1].u.operand;
  564. emitGetVirtualRegister(srcDst, regT0);
  565. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  566. addSlowCase(branchSub32(Overflow, TrustedImm32(1), regT0));
  567. emitFastArithIntToImmNoCheck(regT0, regT0);
  568. emitPutVirtualRegister(srcDst);
  569. }
  570. void JIT::emitSlow_op_dec(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  571. {
  572. unsigned srcDst = currentInstruction[1].u.operand;
  573. Jump notImm = getSlowCase(iter);
  574. linkSlowCase(iter);
  575. emitGetVirtualRegister(srcDst, regT0);
  576. notImm.link(this);
  577. JITStubCall stubCall(this, cti_op_dec);
  578. stubCall.addArgument(regT0);
  579. stubCall.call(srcDst);
  580. }
  581. /* ------------------------------ BEGIN: OP_MOD ------------------------------ */
  582. #if CPU(X86) || CPU(X86_64)
  583. void JIT::emit_op_mod(Instruction* currentInstruction)
  584. {
  585. unsigned result = currentInstruction[1].u.operand;
  586. unsigned op1 = currentInstruction[2].u.operand;
  587. unsigned op2 = currentInstruction[3].u.operand;
  588. // Make sure registers are correct for x86 IDIV instructions.
  589. ASSERT(regT0 == X86Registers::eax);
  590. ASSERT(regT1 == X86Registers::edx);
  591. ASSERT(regT2 == X86Registers::ecx);
  592. emitGetVirtualRegisters(op1, regT3, op2, regT2);
  593. emitJumpSlowCaseIfNotImmediateInteger(regT3);
  594. emitJumpSlowCaseIfNotImmediateInteger(regT2);
  595. move(regT3, regT0);
  596. addSlowCase(branchTest32(Zero, regT2));
  597. Jump denominatorNotNeg1 = branch32(NotEqual, regT2, TrustedImm32(-1));
  598. addSlowCase(branch32(Equal, regT0, TrustedImm32(-2147483647-1)));
  599. denominatorNotNeg1.link(this);
  600. m_assembler.cdq();
  601. m_assembler.idivl_r(regT2);
  602. Jump numeratorPositive = branch32(GreaterThanOrEqual, regT3, TrustedImm32(0));
  603. addSlowCase(branchTest32(Zero, regT1));
  604. numeratorPositive.link(this);
  605. emitFastArithReTagImmediate(regT1, regT0);
  606. emitPutVirtualRegister(result);
  607. }
  608. void JIT::emitSlow_op_mod(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  609. {
  610. unsigned result = currentInstruction[1].u.operand;
  611. linkSlowCase(iter);
  612. linkSlowCase(iter);
  613. linkSlowCase(iter);
  614. linkSlowCase(iter);
  615. linkSlowCase(iter);
  616. JITStubCall stubCall(this, cti_op_mod);
  617. stubCall.addArgument(regT3);
  618. stubCall.addArgument(regT2);
  619. stubCall.call(result);
  620. }
  621. #else // CPU(X86) || CPU(X86_64)
  622. void JIT::emit_op_mod(Instruction* currentInstruction)
  623. {
  624. unsigned result = currentInstruction[1].u.operand;
  625. unsigned op1 = currentInstruction[2].u.operand;
  626. unsigned op2 = currentInstruction[3].u.operand;
  627. JITStubCall stubCall(this, cti_op_mod);
  628. stubCall.addArgument(op1, regT2);
  629. stubCall.addArgument(op2, regT2);
  630. stubCall.call(result);
  631. }
  632. void JIT::emitSlow_op_mod(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  633. {
  634. RELEASE_ASSERT_NOT_REACHED();
  635. }
  636. #endif // CPU(X86) || CPU(X86_64)
  637. /* ------------------------------ END: OP_MOD ------------------------------ */
  638. /* ------------------------------ BEGIN: USE(JSVALUE64) (OP_ADD, OP_SUB, OP_MUL) ------------------------------ */
  639. void JIT::compileBinaryArithOp(OpcodeID opcodeID, unsigned, unsigned op1, unsigned op2, OperandTypes)
  640. {
  641. emitGetVirtualRegisters(op1, regT0, op2, regT1);
  642. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  643. emitJumpSlowCaseIfNotImmediateInteger(regT1);
  644. #if ENABLE(VALUE_PROFILER)
  645. RareCaseProfile* profile = m_codeBlock->addSpecialFastCaseProfile(m_bytecodeOffset);
  646. #endif
  647. if (opcodeID == op_add)
  648. addSlowCase(branchAdd32(Overflow, regT1, regT0));
  649. else if (opcodeID == op_sub)
  650. addSlowCase(branchSub32(Overflow, regT1, regT0));
  651. else {
  652. ASSERT(opcodeID == op_mul);
  653. #if ENABLE(VALUE_PROFILER)
  654. if (shouldEmitProfiling()) {
  655. // We want to be able to measure if this is taking the slow case just
  656. // because of negative zero. If this produces positive zero, then we
  657. // don't want the slow case to be taken because that will throw off
  658. // speculative compilation.
  659. move(regT0, regT2);
  660. addSlowCase(branchMul32(Overflow, regT1, regT2));
  661. JumpList done;
  662. done.append(branchTest32(NonZero, regT2));
  663. Jump negativeZero = branch32(LessThan, regT0, TrustedImm32(0));
  664. done.append(branch32(GreaterThanOrEqual, regT1, TrustedImm32(0)));
  665. negativeZero.link(this);
  666. // We only get here if we have a genuine negative zero. Record this,
  667. // so that the speculative JIT knows that we failed speculation
  668. // because of a negative zero.
  669. add32(TrustedImm32(1), AbsoluteAddress(&profile->m_counter));
  670. addSlowCase(jump());
  671. done.link(this);
  672. move(regT2, regT0);
  673. } else {
  674. addSlowCase(branchMul32(Overflow, regT1, regT0));
  675. addSlowCase(branchTest32(Zero, regT0));
  676. }
  677. #else
  678. addSlowCase(branchMul32(Overflow, regT1, regT0));
  679. addSlowCase(branchTest32(Zero, regT0));
  680. #endif
  681. }
  682. emitFastArithIntToImmNoCheck(regT0, regT0);
  683. }
  684. void JIT::compileBinaryArithOpSlowCase(OpcodeID opcodeID, Vector<SlowCaseEntry>::iterator& iter, unsigned result, unsigned op1, unsigned op2, OperandTypes types, bool op1HasImmediateIntFastCase, bool op2HasImmediateIntFastCase)
  685. {
  686. // We assume that subtracting TagTypeNumber is equivalent to adding DoubleEncodeOffset.
  687. COMPILE_ASSERT(((TagTypeNumber + DoubleEncodeOffset) == 0), TagTypeNumber_PLUS_DoubleEncodeOffset_EQUALS_0);
  688. Jump notImm1;
  689. Jump notImm2;
  690. if (op1HasImmediateIntFastCase) {
  691. notImm2 = getSlowCase(iter);
  692. } else if (op2HasImmediateIntFastCase) {
  693. notImm1 = getSlowCase(iter);
  694. } else {
  695. notImm1 = getSlowCase(iter);
  696. notImm2 = getSlowCase(iter);
  697. }
  698. linkSlowCase(iter); // Integer overflow case - we could handle this in JIT code, but this is likely rare.
  699. if (opcodeID == op_mul && !op1HasImmediateIntFastCase && !op2HasImmediateIntFastCase) // op_mul has an extra slow case to handle 0 * negative number.
  700. linkSlowCase(iter);
  701. emitGetVirtualRegister(op1, regT0);
  702. Label stubFunctionCall(this);
  703. JITStubCall stubCall(this, opcodeID == op_add ? cti_op_add : opcodeID == op_sub ? cti_op_sub : cti_op_mul);
  704. if (op1HasImmediateIntFastCase || op2HasImmediateIntFastCase) {
  705. emitGetVirtualRegister(op1, regT0);
  706. emitGetVirtualRegister(op2, regT1);
  707. }
  708. stubCall.addArgument(regT0);
  709. stubCall.addArgument(regT1);
  710. stubCall.call(result);
  711. Jump end = jump();
  712. if (op1HasImmediateIntFastCase) {
  713. notImm2.link(this);
  714. if (!types.second().definitelyIsNumber())
  715. emitJumpIfNotImmediateNumber(regT0).linkTo(stubFunctionCall, this);
  716. emitGetVirtualRegister(op1, regT1);
  717. convertInt32ToDouble(regT1, fpRegT1);
  718. add64(tagTypeNumberRegister, regT0);
  719. move64ToDouble(regT0, fpRegT2);
  720. } else if (op2HasImmediateIntFastCase) {
  721. notImm1.link(this);
  722. if (!types.first().definitelyIsNumber())
  723. emitJumpIfNotImmediateNumber(regT0).linkTo(stubFunctionCall, this);
  724. emitGetVirtualRegister(op2, regT1);
  725. convertInt32ToDouble(regT1, fpRegT1);
  726. add64(tagTypeNumberRegister, regT0);
  727. move64ToDouble(regT0, fpRegT2);
  728. } else {
  729. // if we get here, eax is not an int32, edx not yet checked.
  730. notImm1.link(this);
  731. if (!types.first().definitelyIsNumber())
  732. emitJumpIfNotImmediateNumber(regT0).linkTo(stubFunctionCall, this);
  733. if (!types.second().definitelyIsNumber())
  734. emitJumpIfNotImmediateNumber(regT1).linkTo(stubFunctionCall, this);
  735. add64(tagTypeNumberRegister, regT0);
  736. move64ToDouble(regT0, fpRegT1);
  737. Jump op2isDouble = emitJumpIfNotImmediateInteger(regT1);
  738. convertInt32ToDouble(regT1, fpRegT2);
  739. Jump op2wasInteger = jump();
  740. // if we get here, eax IS an int32, edx is not.
  741. notImm2.link(this);
  742. if (!types.second().definitelyIsNumber())
  743. emitJumpIfNotImmediateNumber(regT1).linkTo(stubFunctionCall, this);
  744. convertInt32ToDouble(regT0, fpRegT1);
  745. op2isDouble.link(this);
  746. add64(tagTypeNumberRegister, regT1);
  747. move64ToDouble(regT1, fpRegT2);
  748. op2wasInteger.link(this);
  749. }
  750. if (opcodeID == op_add)
  751. addDouble(fpRegT2, fpRegT1);
  752. else if (opcodeID == op_sub)
  753. subDouble(fpRegT2, fpRegT1);
  754. else if (opcodeID == op_mul)
  755. mulDouble(fpRegT2, fpRegT1);
  756. else {
  757. ASSERT(opcodeID == op_div);
  758. divDouble(fpRegT2, fpRegT1);
  759. }
  760. moveDoubleTo64(fpRegT1, regT0);
  761. sub64(tagTypeNumberRegister, regT0);
  762. emitPutVirtualRegister(result, regT0);
  763. end.link(this);
  764. }
  765. void JIT::emit_op_add(Instruction* currentInstruction)
  766. {
  767. unsigned result = currentInstruction[1].u.operand;
  768. unsigned op1 = currentInstruction[2].u.operand;
  769. unsigned op2 = currentInstruction[3].u.operand;
  770. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  771. if (!types.first().mightBeNumber() || !types.second().mightBeNumber()) {
  772. addSlowCase();
  773. JITStubCall stubCall(this, cti_op_add);
  774. stubCall.addArgument(op1, regT2);
  775. stubCall.addArgument(op2, regT2);
  776. stubCall.call(result);
  777. return;
  778. }
  779. if (isOperandConstantImmediateInt(op1)) {
  780. emitGetVirtualRegister(op2, regT0);
  781. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  782. addSlowCase(branchAdd32(Overflow, regT0, Imm32(getConstantOperandImmediateInt(op1)), regT1));
  783. emitFastArithIntToImmNoCheck(regT1, regT0);
  784. } else if (isOperandConstantImmediateInt(op2)) {
  785. emitGetVirtualRegister(op1, regT0);
  786. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  787. addSlowCase(branchAdd32(Overflow, regT0, Imm32(getConstantOperandImmediateInt(op2)), regT1));
  788. emitFastArithIntToImmNoCheck(regT1, regT0);
  789. } else
  790. compileBinaryArithOp(op_add, result, op1, op2, types);
  791. emitPutVirtualRegister(result);
  792. }
  793. void JIT::emitSlow_op_add(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  794. {
  795. unsigned result = currentInstruction[1].u.operand;
  796. unsigned op1 = currentInstruction[2].u.operand;
  797. unsigned op2 = currentInstruction[3].u.operand;
  798. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  799. if (!types.first().mightBeNumber() || !types.second().mightBeNumber()) {
  800. linkDummySlowCase(iter);
  801. return;
  802. }
  803. bool op1HasImmediateIntFastCase = isOperandConstantImmediateInt(op1);
  804. bool op2HasImmediateIntFastCase = !op1HasImmediateIntFastCase && isOperandConstantImmediateInt(op2);
  805. compileBinaryArithOpSlowCase(op_add, iter, result, op1, op2, types, op1HasImmediateIntFastCase, op2HasImmediateIntFastCase);
  806. }
  807. void JIT::emit_op_mul(Instruction* currentInstruction)
  808. {
  809. unsigned result = currentInstruction[1].u.operand;
  810. unsigned op1 = currentInstruction[2].u.operand;
  811. unsigned op2 = currentInstruction[3].u.operand;
  812. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  813. // For now, only plant a fast int case if the constant operand is greater than zero.
  814. int32_t value;
  815. if (isOperandConstantImmediateInt(op1) && ((value = getConstantOperandImmediateInt(op1)) > 0)) {
  816. #if ENABLE(VALUE_PROFILER)
  817. // Add a special fast case profile because the DFG JIT will expect one.
  818. m_codeBlock->addSpecialFastCaseProfile(m_bytecodeOffset);
  819. #endif
  820. emitGetVirtualRegister(op2, regT0);
  821. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  822. addSlowCase(branchMul32(Overflow, Imm32(value), regT0, regT1));
  823. emitFastArithReTagImmediate(regT1, regT0);
  824. } else if (isOperandConstantImmediateInt(op2) && ((value = getConstantOperandImmediateInt(op2)) > 0)) {
  825. #if ENABLE(VALUE_PROFILER)
  826. // Add a special fast case profile because the DFG JIT will expect one.
  827. m_codeBlock->addSpecialFastCaseProfile(m_bytecodeOffset);
  828. #endif
  829. emitGetVirtualRegister(op1, regT0);
  830. emitJumpSlowCaseIfNotImmediateInteger(regT0);
  831. addSlowCase(branchMul32(Overflow, Imm32(value), regT0, regT1));
  832. emitFastArithReTagImmediate(regT1, regT0);
  833. } else
  834. compileBinaryArithOp(op_mul, result, op1, op2, types);
  835. emitPutVirtualRegister(result);
  836. }
  837. void JIT::emitSlow_op_mul(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  838. {
  839. unsigned result = currentInstruction[1].u.operand;
  840. unsigned op1 = currentInstruction[2].u.operand;
  841. unsigned op2 = currentInstruction[3].u.operand;
  842. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  843. bool op1HasImmediateIntFastCase = isOperandConstantImmediateInt(op1) && getConstantOperandImmediateInt(op1) > 0;
  844. bool op2HasImmediateIntFastCase = !op1HasImmediateIntFastCase && isOperandConstantImmediateInt(op2) && getConstantOperandImmediateInt(op2) > 0;
  845. compileBinaryArithOpSlowCase(op_mul, iter, result, op1, op2, types, op1HasImmediateIntFastCase, op2HasImmediateIntFastCase);
  846. }
  847. void JIT::emit_op_div(Instruction* currentInstruction)
  848. {
  849. unsigned dst = currentInstruction[1].u.operand;
  850. unsigned op1 = currentInstruction[2].u.operand;
  851. unsigned op2 = currentInstruction[3].u.operand;
  852. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  853. if (isOperandConstantImmediateDouble(op1)) {
  854. emitGetVirtualRegister(op1, regT0);
  855. add64(tagTypeNumberRegister, regT0);
  856. move64ToDouble(regT0, fpRegT0);
  857. } else if (isOperandConstantImmediateInt(op1)) {
  858. emitLoadInt32ToDouble(op1, fpRegT0);
  859. } else {
  860. emitGetVirtualRegister(op1, regT0);
  861. if (!types.first().definitelyIsNumber())
  862. emitJumpSlowCaseIfNotImmediateNumber(regT0);
  863. Jump notInt = emitJumpIfNotImmediateInteger(regT0);
  864. convertInt32ToDouble(regT0, fpRegT0);
  865. Jump skipDoubleLoad = jump();
  866. notInt.link(this);
  867. add64(tagTypeNumberRegister, regT0);
  868. move64ToDouble(regT0, fpRegT0);
  869. skipDoubleLoad.link(this);
  870. }
  871. if (isOperandConstantImmediateDouble(op2)) {
  872. emitGetVirtualRegister(op2, regT1);
  873. add64(tagTypeNumberRegister, regT1);
  874. move64ToDouble(regT1, fpRegT1);
  875. } else if (isOperandConstantImmediateInt(op2)) {
  876. emitLoadInt32ToDouble(op2, fpRegT1);
  877. } else {
  878. emitGetVirtualRegister(op2, regT1);
  879. if (!types.second().definitelyIsNumber())
  880. emitJumpSlowCaseIfNotImmediateNumber(regT1);
  881. Jump notInt = emitJumpIfNotImmediateInteger(regT1);
  882. convertInt32ToDouble(regT1, fpRegT1);
  883. Jump skipDoubleLoad = jump();
  884. notInt.link(this);
  885. add64(tagTypeNumberRegister, regT1);
  886. move64ToDouble(regT1, fpRegT1);
  887. skipDoubleLoad.link(this);
  888. }
  889. divDouble(fpRegT1, fpRegT0);
  890. #if ENABLE(VALUE_PROFILER)
  891. // Is the result actually an integer? The DFG JIT would really like to know. If it's
  892. // not an integer, we increment a count. If this together with the slow case counter
  893. // are below threshold then the DFG JIT will compile this division with a specualtion
  894. // that the remainder is zero.
  895. // As well, there are cases where a double result here would cause an important field
  896. // in the heap to sometimes have doubles in it, resulting in double predictions getting
  897. // propagated to a use site where it might cause damage (such as the index to an array
  898. // access). So if we are DFG compiling anything in the program, we want this code to
  899. // ensure that it produces integers whenever possible.
  900. JumpList notInteger;
  901. branchConvertDoubleToInt32(fpRegT0, regT0, notInteger, fpRegT1);
  902. // If we've got an integer, we might as well make that the result of the division.
  903. emitFastArithReTagImmediate(regT0, regT0);
  904. Jump isInteger = jump();
  905. notInteger.link(this);
  906. moveDoubleTo64(fpRegT0, regT0);
  907. Jump doubleZero = branchTest64(Zero, regT0);
  908. add32(TrustedImm32(1), AbsoluteAddress(&m_codeBlock->addSpecialFastCaseProfile(m_bytecodeOffset)->m_counter));
  909. sub64(tagTypeNumberRegister, regT0);
  910. Jump trueDouble = jump();
  911. doubleZero.link(this);
  912. move(tagTypeNumberRegister, regT0);
  913. trueDouble.link(this);
  914. isInteger.link(this);
  915. #else
  916. // Double result.
  917. moveDoubleTo64(fpRegT0, regT0);
  918. sub64(tagTypeNumberRegister, regT0);
  919. #endif
  920. emitPutVirtualRegister(dst, regT0);
  921. }
  922. void JIT::emitSlow_op_div(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  923. {
  924. unsigned result = currentInstruction[1].u.operand;
  925. unsigned op1 = currentInstruction[2].u.operand;
  926. unsigned op2 = currentInstruction[3].u.operand;
  927. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  928. if (types.first().definitelyIsNumber() && types.second().definitelyIsNumber()) {
  929. #ifndef NDEBUG
  930. breakpoint();
  931. #endif
  932. return;
  933. }
  934. if (!isOperandConstantImmediateDouble(op1) && !isOperandConstantImmediateInt(op1)) {
  935. if (!types.first().definitelyIsNumber())
  936. linkSlowCase(iter);
  937. }
  938. if (!isOperandConstantImmediateDouble(op2) && !isOperandConstantImmediateInt(op2)) {
  939. if (!types.second().definitelyIsNumber())
  940. linkSlowCase(iter);
  941. }
  942. // There is an extra slow case for (op1 * -N) or (-N * op2), to check for 0 since this should produce a result of -0.
  943. JITStubCall stubCall(this, cti_op_div);
  944. stubCall.addArgument(op1, regT2);
  945. stubCall.addArgument(op2, regT2);
  946. stubCall.call(result);
  947. }
  948. void JIT::emit_op_sub(Instruction* currentInstruction)
  949. {
  950. unsigned result = currentInstruction[1].u.operand;
  951. unsigned op1 = currentInstruction[2].u.operand;
  952. unsigned op2 = currentInstruction[3].u.operand;
  953. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  954. compileBinaryArithOp(op_sub, result, op1, op2, types);
  955. emitPutVirtualRegister(result);
  956. }
  957. void JIT::emitSlow_op_sub(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
  958. {
  959. unsigned result = currentInstruction[1].u.operand;
  960. unsigned op1 = currentInstruction[2].u.operand;
  961. unsigned op2 = currentInstruction[3].u.operand;
  962. OperandTypes types = OperandTypes::fromInt(currentInstruction[4].u.operand);
  963. compileBinaryArithOpSlowCase(op_sub, iter, result, op1, op2, types, false, false);
  964. }
  965. /* ------------------------------ END: OP_ADD, OP_SUB, OP_MUL ------------------------------ */
  966. #endif // USE(JSVALUE64)
  967. } // namespace JSC
  968. #endif // ENABLE(JIT)