DFGGPRInfo.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (C) 2011 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. #ifndef DFGGPRInfo_h
  26. #define DFGGPRInfo_h
  27. #include <wtf/Platform.h>
  28. #if ENABLE(DFG_JIT)
  29. #include "DFGRegisterBank.h"
  30. #include "MacroAssembler.h"
  31. namespace JSC { namespace DFG {
  32. typedef MacroAssembler::RegisterID GPRReg;
  33. #define InvalidGPRReg ((GPRReg)-1)
  34. #if USE(JSVALUE64)
  35. class JSValueRegs {
  36. public:
  37. JSValueRegs()
  38. : m_gpr(InvalidGPRReg)
  39. {
  40. }
  41. explicit JSValueRegs(GPRReg gpr)
  42. : m_gpr(gpr)
  43. {
  44. }
  45. bool operator!() const { return m_gpr == InvalidGPRReg; }
  46. GPRReg gpr() const { return m_gpr; }
  47. private:
  48. GPRReg m_gpr;
  49. };
  50. class JSValueSource {
  51. public:
  52. JSValueSource()
  53. : m_offset(notAddress())
  54. , m_base(InvalidGPRReg)
  55. {
  56. }
  57. JSValueSource(JSValueRegs regs)
  58. : m_offset(notAddress())
  59. , m_base(regs.gpr())
  60. {
  61. }
  62. explicit JSValueSource(GPRReg gpr)
  63. : m_offset(notAddress())
  64. , m_base(gpr)
  65. {
  66. }
  67. JSValueSource(MacroAssembler::Address address)
  68. : m_offset(address.offset)
  69. , m_base(address.base)
  70. {
  71. ASSERT(m_offset != notAddress());
  72. ASSERT(m_base != InvalidGPRReg);
  73. }
  74. static JSValueSource unboxedCell(GPRReg payloadGPR)
  75. {
  76. return JSValueSource(payloadGPR);
  77. }
  78. bool operator!() const { return m_base == InvalidGPRReg; }
  79. bool isAddress() const { return m_offset != notAddress(); }
  80. int32_t offset() const
  81. {
  82. ASSERT(isAddress());
  83. return m_offset;
  84. }
  85. GPRReg base() const
  86. {
  87. ASSERT(isAddress());
  88. return m_base;
  89. }
  90. GPRReg gpr() const
  91. {
  92. ASSERT(!isAddress());
  93. return m_base;
  94. }
  95. MacroAssembler::Address asAddress() const { return MacroAssembler::Address(base(), offset()); }
  96. private:
  97. static inline int32_t notAddress() { return 0x80000000; }
  98. int32_t m_offset;
  99. GPRReg m_base;
  100. };
  101. #endif
  102. #if USE(JSVALUE32_64)
  103. class JSValueRegs {
  104. public:
  105. JSValueRegs()
  106. : m_tagGPR(static_cast<int8_t>(InvalidGPRReg))
  107. , m_payloadGPR(static_cast<int8_t>(InvalidGPRReg))
  108. {
  109. }
  110. JSValueRegs(GPRReg tagGPR, GPRReg payloadGPR)
  111. : m_tagGPR(tagGPR)
  112. , m_payloadGPR(payloadGPR)
  113. {
  114. ASSERT((static_cast<GPRReg>(m_tagGPR) == InvalidGPRReg) == (static_cast<GPRReg>(payloadGPR) == InvalidGPRReg));
  115. }
  116. bool operator!() const { return static_cast<GPRReg>(m_tagGPR) == InvalidGPRReg; }
  117. GPRReg tagGPR() const { return static_cast<GPRReg>(m_tagGPR); }
  118. GPRReg payloadGPR() const { return static_cast<GPRReg>(m_payloadGPR); }
  119. private:
  120. int8_t m_tagGPR;
  121. int8_t m_payloadGPR;
  122. };
  123. class JSValueSource {
  124. public:
  125. JSValueSource()
  126. : m_offset(notAddress())
  127. , m_baseOrTag(static_cast<int8_t>(InvalidGPRReg))
  128. , m_payload(static_cast<int8_t>(InvalidGPRReg))
  129. , m_tagType(0)
  130. {
  131. }
  132. JSValueSource(JSValueRegs regs)
  133. : m_offset(notAddress())
  134. , m_baseOrTag(regs.tagGPR())
  135. , m_payload(regs.payloadGPR())
  136. , m_tagType(0)
  137. {
  138. }
  139. JSValueSource(GPRReg tagGPR, GPRReg payloadGPR)
  140. : m_offset(notAddress())
  141. , m_baseOrTag(static_cast<int8_t>(tagGPR))
  142. , m_payload(static_cast<int8_t>(payloadGPR))
  143. , m_tagType(0)
  144. {
  145. }
  146. JSValueSource(MacroAssembler::Address address)
  147. : m_offset(address.offset)
  148. , m_baseOrTag(static_cast<int8_t>(address.base))
  149. , m_payload(static_cast<int8_t>(InvalidGPRReg))
  150. , m_tagType(0)
  151. {
  152. ASSERT(m_offset != notAddress());
  153. ASSERT(static_cast<GPRReg>(m_baseOrTag) != InvalidGPRReg);
  154. }
  155. static JSValueSource unboxedCell(GPRReg payloadGPR)
  156. {
  157. JSValueSource result;
  158. result.m_offset = notAddress();
  159. result.m_baseOrTag = static_cast<int8_t>(InvalidGPRReg);
  160. result.m_payload = static_cast<int8_t>(payloadGPR);
  161. result.m_tagType = static_cast<int8_t>(JSValue::CellTag);
  162. return result;
  163. }
  164. bool operator!() const { return static_cast<GPRReg>(m_baseOrTag) == InvalidGPRReg && static_cast<GPRReg>(m_payload) == InvalidGPRReg; }
  165. bool isAddress() const
  166. {
  167. ASSERT(!!*this);
  168. return m_offset != notAddress();
  169. }
  170. int32_t offset() const
  171. {
  172. ASSERT(isAddress());
  173. return m_offset;
  174. }
  175. GPRReg base() const
  176. {
  177. ASSERT(isAddress());
  178. return static_cast<GPRReg>(m_baseOrTag);
  179. }
  180. GPRReg tagGPR() const
  181. {
  182. ASSERT(!isAddress() && static_cast<GPRReg>(m_baseOrTag) != InvalidGPRReg);
  183. return static_cast<GPRReg>(m_baseOrTag);
  184. }
  185. GPRReg payloadGPR() const
  186. {
  187. ASSERT(!isAddress());
  188. return static_cast<GPRReg>(m_payload);
  189. }
  190. bool hasKnownTag() const
  191. {
  192. ASSERT(!!*this);
  193. ASSERT(!isAddress());
  194. return static_cast<GPRReg>(m_baseOrTag) == InvalidGPRReg;
  195. }
  196. uint32_t tag() const
  197. {
  198. return static_cast<int32_t>(m_tagType);
  199. }
  200. MacroAssembler::Address asAddress(unsigned additionalOffset = 0) const { return MacroAssembler::Address(base(), offset() + additionalOffset); }
  201. private:
  202. static inline int32_t notAddress() { return 0x80000000; }
  203. int32_t m_offset;
  204. int8_t m_baseOrTag;
  205. int8_t m_payload;
  206. int8_t m_tagType; // Contains the low bits of the tag.
  207. };
  208. #endif
  209. #if CPU(X86)
  210. #define NUMBER_OF_ARGUMENT_REGISTERS 0
  211. class GPRInfo {
  212. public:
  213. typedef GPRReg RegisterType;
  214. static const unsigned numberOfRegisters = 5;
  215. // Temporary registers.
  216. static const GPRReg regT0 = X86Registers::eax;
  217. static const GPRReg regT1 = X86Registers::edx;
  218. static const GPRReg regT2 = X86Registers::ecx;
  219. static const GPRReg regT3 = X86Registers::ebx;
  220. static const GPRReg regT4 = X86Registers::esi;
  221. // These registers match the baseline JIT.
  222. static const GPRReg cachedResultRegister = regT0;
  223. static const GPRReg cachedResultRegister2 = regT1;
  224. static const GPRReg callFrameRegister = X86Registers::edi;
  225. // These constants provide the names for the general purpose argument & return value registers.
  226. static const GPRReg argumentGPR0 = X86Registers::ecx; // regT2
  227. static const GPRReg argumentGPR1 = X86Registers::edx; // regT1
  228. static const GPRReg nonArgGPR0 = X86Registers::eax; // regT0
  229. static const GPRReg nonArgGPR1 = X86Registers::ebx; // regT3
  230. static const GPRReg nonArgGPR2 = X86Registers::esi; // regT4
  231. static const GPRReg returnValueGPR = X86Registers::eax; // regT0
  232. static const GPRReg returnValueGPR2 = X86Registers::edx; // regT1
  233. static const GPRReg nonPreservedNonReturnGPR = X86Registers::ecx;
  234. static GPRReg toRegister(unsigned index)
  235. {
  236. ASSERT(index < numberOfRegisters);
  237. static const GPRReg registerForIndex[numberOfRegisters] = { regT0, regT1, regT2, regT3, regT4 };
  238. return registerForIndex[index];
  239. }
  240. static unsigned toIndex(GPRReg reg)
  241. {
  242. ASSERT(reg != InvalidGPRReg);
  243. ASSERT(static_cast<int>(reg) < 8);
  244. static const unsigned indexForRegister[8] = { 0, 2, 1, 3, InvalidIndex, InvalidIndex, 4, InvalidIndex };
  245. unsigned result = indexForRegister[reg];
  246. ASSERT(result != InvalidIndex);
  247. return result;
  248. }
  249. static const char* debugName(GPRReg reg)
  250. {
  251. ASSERT(reg != InvalidGPRReg);
  252. ASSERT(static_cast<int>(reg) < 8);
  253. static const char* nameForRegister[8] = {
  254. "eax", "ecx", "edx", "ebx",
  255. "esp", "ebp", "esi", "edi",
  256. };
  257. return nameForRegister[reg];
  258. }
  259. private:
  260. static const unsigned InvalidIndex = 0xffffffff;
  261. };
  262. #endif
  263. #if CPU(X86_64)
  264. #define NUMBER_OF_ARGUMENT_REGISTERS 6
  265. class GPRInfo {
  266. public:
  267. typedef GPRReg RegisterType;
  268. static const unsigned numberOfRegisters = 9;
  269. // These registers match the baseline JIT.
  270. static const GPRReg cachedResultRegister = X86Registers::eax;
  271. static const GPRReg callFrameRegister = X86Registers::r13;
  272. static const GPRReg tagTypeNumberRegister = X86Registers::r14;
  273. static const GPRReg tagMaskRegister = X86Registers::r15;
  274. // Temporary registers.
  275. static const GPRReg regT0 = X86Registers::eax;
  276. static const GPRReg regT1 = X86Registers::edx;
  277. static const GPRReg regT2 = X86Registers::ecx;
  278. static const GPRReg regT3 = X86Registers::ebx;
  279. static const GPRReg regT4 = X86Registers::edi;
  280. static const GPRReg regT5 = X86Registers::esi;
  281. static const GPRReg regT6 = X86Registers::r8;
  282. static const GPRReg regT7 = X86Registers::r9;
  283. static const GPRReg regT8 = X86Registers::r10;
  284. // These constants provide the names for the general purpose argument & return value registers.
  285. static const GPRReg argumentGPR0 = X86Registers::edi; // regT4
  286. static const GPRReg argumentGPR1 = X86Registers::esi; // regT5
  287. static const GPRReg argumentGPR2 = X86Registers::edx; // regT1
  288. static const GPRReg argumentGPR3 = X86Registers::ecx; // regT2
  289. static const GPRReg argumentGPR4 = X86Registers::r8; // regT6
  290. static const GPRReg argumentGPR5 = X86Registers::r9; // regT7
  291. static const GPRReg nonArgGPR0 = X86Registers::eax; // regT0
  292. static const GPRReg nonArgGPR1 = X86Registers::ebx; // regT3
  293. static const GPRReg nonArgGPR2 = X86Registers::r10; // regT8
  294. static const GPRReg returnValueGPR = X86Registers::eax; // regT0
  295. static const GPRReg returnValueGPR2 = X86Registers::edx; // regT1
  296. static const GPRReg nonPreservedNonReturnGPR = X86Registers::esi;
  297. static GPRReg toRegister(unsigned index)
  298. {
  299. ASSERT(index < numberOfRegisters);
  300. static const GPRReg registerForIndex[numberOfRegisters] = { regT0, regT1, regT2, regT3, regT4, regT5, regT6, regT7, regT8 };
  301. return registerForIndex[index];
  302. }
  303. static unsigned toIndex(GPRReg reg)
  304. {
  305. ASSERT(reg != InvalidGPRReg);
  306. ASSERT(static_cast<int>(reg) < 16);
  307. static const unsigned indexForRegister[16] = { 0, 2, 1, 3, InvalidIndex, InvalidIndex, 5, 4, 6, 7, 8, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex };
  308. unsigned result = indexForRegister[reg];
  309. ASSERT(result != InvalidIndex);
  310. return result;
  311. }
  312. static const char* debugName(GPRReg reg)
  313. {
  314. ASSERT(reg != InvalidGPRReg);
  315. ASSERT(static_cast<int>(reg) < 16);
  316. static const char* nameForRegister[16] = {
  317. "rax", "rcx", "rdx", "rbx",
  318. "rsp", "rbp", "rsi", "rdi",
  319. "r8", "r9", "r10", "r11",
  320. "r12", "r13", "r14", "r15"
  321. };
  322. return nameForRegister[reg];
  323. }
  324. private:
  325. static const unsigned InvalidIndex = 0xffffffff;
  326. };
  327. #endif
  328. #if CPU(ARM)
  329. #define NUMBER_OF_ARGUMENT_REGISTERS 4
  330. class GPRInfo {
  331. public:
  332. typedef GPRReg RegisterType;
  333. static const unsigned numberOfRegisters = 8;
  334. // Temporary registers.
  335. static const GPRReg regT0 = ARMRegisters::r0;
  336. static const GPRReg regT1 = ARMRegisters::r1;
  337. static const GPRReg regT2 = ARMRegisters::r2;
  338. static const GPRReg regT3 = ARMRegisters::r4;
  339. static const GPRReg regT4 = ARMRegisters::r8;
  340. static const GPRReg regT5 = ARMRegisters::r9;
  341. static const GPRReg regT6 = ARMRegisters::r10;
  342. static const GPRReg regT7 = ARMRegisters::r11;
  343. // These registers match the baseline JIT.
  344. static const GPRReg cachedResultRegister = regT0;
  345. static const GPRReg cachedResultRegister2 = regT1;
  346. static const GPRReg callFrameRegister = ARMRegisters::r5;
  347. // These constants provide the names for the general purpose argument & return value registers.
  348. static const GPRReg argumentGPR0 = ARMRegisters::r0; // regT0
  349. static const GPRReg argumentGPR1 = ARMRegisters::r1; // regT1
  350. static const GPRReg argumentGPR2 = ARMRegisters::r2; // regT2
  351. // FIXME: r3 is currently used be the MacroAssembler as a temporary - it seems
  352. // This could threoretically be a problem if this is used in code generation
  353. // between the arguments being set up, and the call being made. That said,
  354. // any change introducing a problem here is likely to be immediately apparent!
  355. static const GPRReg argumentGPR3 = ARMRegisters::r3; // FIXME!
  356. static const GPRReg nonArgGPR0 = ARMRegisters::r4; // regT3
  357. static const GPRReg nonArgGPR1 = ARMRegisters::r8; // regT4
  358. static const GPRReg nonArgGPR2 = ARMRegisters::r9; // regT5
  359. static const GPRReg returnValueGPR = ARMRegisters::r0; // regT0
  360. static const GPRReg returnValueGPR2 = ARMRegisters::r1; // regT1
  361. static const GPRReg nonPreservedNonReturnGPR = ARMRegisters::r2;
  362. static GPRReg toRegister(unsigned index)
  363. {
  364. ASSERT(index < numberOfRegisters);
  365. static const GPRReg registerForIndex[numberOfRegisters] = { regT0, regT1, regT2, regT3, regT4, regT5, regT6, regT7 };
  366. return registerForIndex[index];
  367. }
  368. static unsigned toIndex(GPRReg reg)
  369. {
  370. ASSERT(static_cast<unsigned>(reg) != InvalidGPRReg);
  371. ASSERT(static_cast<unsigned>(reg) < 16);
  372. static const unsigned indexForRegister[16] = { 0, 1, 2, InvalidIndex, 3, InvalidIndex, InvalidIndex, InvalidIndex, 4, 5, 6, 7, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex };
  373. unsigned result = indexForRegister[reg];
  374. ASSERT(result != InvalidIndex);
  375. return result;
  376. }
  377. static const char* debugName(GPRReg reg)
  378. {
  379. ASSERT(static_cast<unsigned>(reg) != InvalidGPRReg);
  380. ASSERT(static_cast<unsigned>(reg) < 16);
  381. static const char* nameForRegister[16] = {
  382. "r0", "r1", "r2", "r3",
  383. "r4", "r5", "r6", "r7",
  384. "r8", "r9", "r10", "r11",
  385. "r12", "r13", "r14", "r15"
  386. };
  387. return nameForRegister[reg];
  388. }
  389. private:
  390. static const unsigned InvalidIndex = 0xffffffff;
  391. };
  392. #endif
  393. #if CPU(MIPS)
  394. #define NUMBER_OF_ARGUMENT_REGISTERS 4
  395. class GPRInfo {
  396. public:
  397. typedef GPRReg RegisterType;
  398. static const unsigned numberOfRegisters = 6;
  399. // Temporary registers.
  400. static const GPRReg regT0 = MIPSRegisters::v0;
  401. static const GPRReg regT1 = MIPSRegisters::v1;
  402. static const GPRReg regT2 = MIPSRegisters::t4;
  403. static const GPRReg regT3 = MIPSRegisters::t5;
  404. static const GPRReg regT4 = MIPSRegisters::t6;
  405. static const GPRReg regT5 = MIPSRegisters::t7;
  406. // These registers match the baseline JIT.
  407. static const GPRReg cachedResultRegister = regT0;
  408. static const GPRReg cachedResultRegister2 = regT1;
  409. static const GPRReg callFrameRegister = MIPSRegisters::s0;
  410. // These constants provide the names for the general purpose argument & return value registers.
  411. static const GPRReg argumentGPR0 = MIPSRegisters::a0;
  412. static const GPRReg argumentGPR1 = MIPSRegisters::a1;
  413. static const GPRReg argumentGPR2 = MIPSRegisters::a2;
  414. static const GPRReg argumentGPR3 = MIPSRegisters::a3;
  415. static const GPRReg nonArgGPR0 = regT2;
  416. static const GPRReg nonArgGPR1 = regT3;
  417. static const GPRReg nonArgGPR2 = regT4;
  418. static const GPRReg returnValueGPR = regT0;
  419. static const GPRReg returnValueGPR2 = regT1;
  420. static const GPRReg nonPreservedNonReturnGPR = regT5;
  421. static GPRReg toRegister(unsigned index)
  422. {
  423. ASSERT(index < numberOfRegisters);
  424. static const GPRReg registerForIndex[numberOfRegisters] = { regT0, regT1, regT2, regT3, regT4, regT5 };
  425. return registerForIndex[index];
  426. }
  427. static unsigned toIndex(GPRReg reg)
  428. {
  429. ASSERT(reg != InvalidGPRReg);
  430. ASSERT(reg < 16);
  431. static const unsigned indexForRegister[16] = { InvalidIndex, InvalidIndex, 0, 1, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex, InvalidIndex, 2, 3, 4, 5 };
  432. unsigned result = indexForRegister[reg];
  433. ASSERT(result != InvalidIndex);
  434. return result;
  435. }
  436. static const char* debugName(GPRReg reg)
  437. {
  438. ASSERT(reg != InvalidGPRReg);
  439. ASSERT(reg < 16);
  440. static const char* nameForRegister[16] = {
  441. "zero", "at", "v0", "v1",
  442. "a0", "a1", "a2", "a3",
  443. "t0", "t1", "t2", "t3",
  444. "t4", "t5", "t6", "t7"
  445. };
  446. return nameForRegister[reg];
  447. }
  448. private:
  449. static const unsigned InvalidIndex = 0xffffffff;
  450. };
  451. #endif
  452. typedef RegisterBank<GPRInfo>::iterator gpr_iterator;
  453. } } // namespace JSC::DFG
  454. #endif
  455. #endif