PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/hphp/vixl/test/test-utils-a64.cc

http://github.com/facebook/hiphop-php
C++ | 427 lines | 291 code | 75 blank | 61 comment | 69 complexity | fb61b3bb23fad744fb90ab4685a73c3d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. // Copyright 2013, ARM Limited
  2. // 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 are met:
  6. //
  7. // * Redistributions of source code must retain the above copyright notice,
  8. // this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright notice,
  10. // this list of conditions and the following disclaimer in the documentation
  11. // and/or other materials provided with the distribution.
  12. // * Neither the name of ARM Limited nor the names of its contributors may be
  13. // used to endorse or promote products derived from this software without
  14. // specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
  17. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. #include "test-utils-a64.h"
  27. #include <cmath> // Needed for isnan().
  28. #include "hphp/vixl/a64/macro-assembler-a64.h"
  29. #include "hphp/vixl/a64/simulator-a64.h"
  30. #include "hphp/vixl/a64/disasm-a64.h"
  31. #include "hphp/vixl/a64/cpu-a64.h"
  32. #define __ masm->
  33. namespace vixl {
  34. bool Equal32(uint32_t expected, const RegisterDump*, uint32_t result) {
  35. if (result != expected) {
  36. printf("Expected 0x%08" PRIx32 "\t Found 0x%08" PRIx32 "\n",
  37. expected, result);
  38. }
  39. return expected == result;
  40. }
  41. bool Equal64(uint64_t expected, const RegisterDump*, uint64_t result) {
  42. if (result != expected) {
  43. printf("Expected 0x%016" PRIx64 "\t Found 0x%016" PRIx64 "\n",
  44. expected, result);
  45. }
  46. return expected == result;
  47. }
  48. bool EqualFP32(float expected, const RegisterDump*, float result) {
  49. if (float_to_rawbits(expected) == float_to_rawbits(result)) {
  50. return true;
  51. } else {
  52. if (std::isnan(expected) || (expected == 0.0)) {
  53. printf("Expected 0x%08" PRIx32 "\t Found 0x%08" PRIx32 "\n",
  54. float_to_rawbits(expected), float_to_rawbits(result));
  55. } else {
  56. printf("Expected %.9f (0x%08" PRIx32 ")\t "
  57. "Found %.9f (0x%08" PRIx32 ")\n",
  58. expected, float_to_rawbits(expected),
  59. result, float_to_rawbits(result));
  60. }
  61. return false;
  62. }
  63. }
  64. bool EqualFP64(double expected, const RegisterDump*, double result) {
  65. if (double_to_rawbits(expected) == double_to_rawbits(result)) {
  66. return true;
  67. }
  68. if (std::isnan(expected) || (expected == 0.0)) {
  69. printf("Expected 0x%016" PRIx64 "\t Found 0x%016" PRIx64 "\n",
  70. double_to_rawbits(expected), double_to_rawbits(result));
  71. } else {
  72. printf("Expected %.17f (0x%016" PRIx64 ")\t "
  73. "Found %.17f (0x%016" PRIx64 ")\n",
  74. expected, double_to_rawbits(expected),
  75. result, double_to_rawbits(result));
  76. }
  77. return false;
  78. }
  79. bool Equal32(uint32_t expected, const RegisterDump* core, const Register& reg) {
  80. assert(reg.Is32Bits());
  81. // Retrieve the corresponding X register so we can check that the upper part
  82. // was properly cleared.
  83. int64_t result_x = core->xreg(reg.code());
  84. if ((result_x & 0xffffffff00000000L) != 0) {
  85. printf("Expected 0x%08" PRIx32 "\t Found 0x%016" PRIx64 "\n",
  86. expected, result_x);
  87. return false;
  88. }
  89. uint32_t result_w = core->wreg(reg.code());
  90. return Equal32(expected, core, result_w);
  91. }
  92. bool Equal64(uint64_t expected,
  93. const RegisterDump* core,
  94. const Register& reg) {
  95. assert(reg.Is64Bits());
  96. uint64_t result = core->xreg(reg.code());
  97. return Equal64(expected, core, result);
  98. }
  99. bool EqualFP32(float expected,
  100. const RegisterDump* core,
  101. const FPRegister& fpreg) {
  102. assert(fpreg.Is32Bits());
  103. // Retrieve the corresponding D register so we can check that the upper part
  104. // was properly cleared.
  105. uint64_t result_64 = core->dreg_bits(fpreg.code());
  106. if ((result_64 & 0xffffffff00000000L) != 0) {
  107. printf("Expected 0x%08" PRIx32 " (%f)\t Found 0x%016" PRIx64 "\n",
  108. float_to_rawbits(expected), expected, result_64);
  109. return false;
  110. }
  111. return EqualFP32(expected, core, core->sreg(fpreg.code()));
  112. }
  113. bool EqualFP64(double expected,
  114. const RegisterDump* core,
  115. const FPRegister& fpreg) {
  116. assert(fpreg.Is64Bits());
  117. return EqualFP64(expected, core, core->dreg(fpreg.code()));
  118. }
  119. bool Equal64(const Register& reg0,
  120. const RegisterDump* core,
  121. const Register& reg1) {
  122. assert(reg0.Is64Bits() && reg1.Is64Bits());
  123. int64_t expected = core->xreg(reg0.code());
  124. int64_t result = core->xreg(reg1.code());
  125. return Equal64(expected, core, result);
  126. }
  127. static char FlagN(uint32_t flags) {
  128. return (flags & NFlag) ? 'N' : 'n';
  129. }
  130. static char FlagZ(uint32_t flags) {
  131. return (flags & ZFlag) ? 'Z' : 'z';
  132. }
  133. static char FlagC(uint32_t flags) {
  134. return (flags & CFlag) ? 'C' : 'c';
  135. }
  136. static char FlagV(uint32_t flags) {
  137. return (flags & VFlag) ? 'V' : 'v';
  138. }
  139. bool EqualNzcv(uint32_t expected, uint32_t result) {
  140. assert((expected & ~NZCVFlag) == 0);
  141. assert((result & ~NZCVFlag) == 0);
  142. if (result != expected) {
  143. printf("Expected: %c%c%c%c\t Found: %c%c%c%c\n",
  144. FlagN(expected), FlagZ(expected), FlagC(expected), FlagV(expected),
  145. FlagN(result), FlagZ(result), FlagC(result), FlagV(result));
  146. return false;
  147. }
  148. return true;
  149. }
  150. bool EqualRegisters(const RegisterDump* a, const RegisterDump* b) {
  151. for (unsigned i = 0; i < kNumberOfRegisters; i++) {
  152. if (a->xreg(i) != b->xreg(i)) {
  153. printf("x%d\t Expected 0x%016" PRIx64 "\t Found 0x%016" PRIx64 "\n",
  154. i, a->xreg(i), b->xreg(i));
  155. return false;
  156. }
  157. }
  158. for (unsigned i = 0; i < kNumberOfFPRegisters; i++) {
  159. uint64_t a_bits = a->dreg_bits(i);
  160. uint64_t b_bits = b->dreg_bits(i);
  161. if (a_bits != b_bits) {
  162. printf("d%d\t Expected 0x%016" PRIx64 "\t Found 0x%016" PRIx64 "\n",
  163. i, a_bits, b_bits);
  164. return false;
  165. }
  166. }
  167. return true;
  168. }
  169. RegList PopulateRegisterArray(Register* w, Register* x, Register* r,
  170. int reg_size, int reg_count, RegList allowed) {
  171. RegList list = 0;
  172. int i = 0;
  173. for (unsigned n = 0; (n < kNumberOfRegisters) && (i < reg_count); n++) {
  174. if (((1UL << n) & allowed) != 0) {
  175. // Only assign allowed registers.
  176. if (r) {
  177. r[i] = Register(n, reg_size);
  178. }
  179. if (x) {
  180. x[i] = Register(n, kXRegSize);
  181. }
  182. if (w) {
  183. w[i] = Register(n, kWRegSize);
  184. }
  185. list |= (1UL << n);
  186. i++;
  187. }
  188. }
  189. // Check that we got enough registers.
  190. assert(CountSetBits(list, kNumberOfRegisters) == reg_count);
  191. return list;
  192. }
  193. RegList PopulateFPRegisterArray(FPRegister* s, FPRegister* d, FPRegister* v,
  194. int reg_size, int reg_count, RegList allowed) {
  195. RegList list = 0;
  196. int i = 0;
  197. for (unsigned n = 0; (n < kNumberOfFPRegisters) && (i < reg_count); n++) {
  198. if (((1UL << n) & allowed) != 0) {
  199. // Only assigned allowed registers.
  200. if (v) {
  201. v[i] = FPRegister(n, reg_size);
  202. }
  203. if (d) {
  204. d[i] = FPRegister(n, kDRegSize);
  205. }
  206. if (s) {
  207. s[i] = FPRegister(n, kSRegSize);
  208. }
  209. list |= (1UL << n);
  210. i++;
  211. }
  212. }
  213. // Check that we got enough registers.
  214. assert(CountSetBits(list, kNumberOfFPRegisters) == reg_count);
  215. return list;
  216. }
  217. void Clobber(MacroAssembler* masm, RegList reg_list, uint64_t const value) {
  218. Register first = NoReg;
  219. for (unsigned i = 0; i < kNumberOfRegisters; i++) {
  220. if (reg_list & (1UL << i)) {
  221. Register xn(i, kXRegSize);
  222. // We should never write into sp here.
  223. assert(!xn.Is(sp));
  224. if (!xn.IsZero()) {
  225. if (!first.IsValid()) {
  226. // This is the first register we've hit, so construct the literal.
  227. __ Mov(xn, value);
  228. first = xn;
  229. } else {
  230. // We've already loaded the literal, so re-use the value already
  231. // loaded into the first register we hit.
  232. __ Mov(xn, first);
  233. }
  234. }
  235. }
  236. }
  237. }
  238. void ClobberFP(MacroAssembler* masm, RegList reg_list, double const value) {
  239. FPRegister first = NoFPReg;
  240. for (unsigned i = 0; i < kNumberOfFPRegisters; i++) {
  241. if (reg_list & (1UL << i)) {
  242. FPRegister dn(i, kDRegSize);
  243. if (!first.IsValid()) {
  244. // This is the first register we've hit, so construct the literal.
  245. __ Fmov(dn, value);
  246. first = dn;
  247. } else {
  248. // We've already loaded the literal, so re-use the value already loaded
  249. // into the first register we hit.
  250. __ Fmov(dn, first);
  251. }
  252. }
  253. }
  254. }
  255. void Clobber(MacroAssembler* masm, CPURegList reg_list) {
  256. if (reg_list.type() == CPURegister::kRegister) {
  257. // This will always clobber X registers.
  258. Clobber(masm, reg_list.list());
  259. } else if (reg_list.type() == CPURegister::kFPRegister) {
  260. // This will always clobber D registers.
  261. ClobberFP(masm, reg_list.list());
  262. } else {
  263. not_reached();
  264. }
  265. }
  266. void RegisterDump::Dump(MacroAssembler* masm) {
  267. assert(__ StackPointer().Is(sp));
  268. // Ensure that we don't unintentionally clobber any registers.
  269. Register old_tmp0 = __ Tmp0();
  270. Register old_tmp1 = __ Tmp1();
  271. FPRegister old_fptmp0 = __ FPTmp0();
  272. __ SetScratchRegisters(NoReg, NoReg);
  273. __ SetFPScratchRegister(NoFPReg);
  274. // Preserve some temporary registers.
  275. Register dump_base = x0;
  276. Register dump = x1;
  277. Register tmp = x2;
  278. Register dump_base_w = dump_base.W();
  279. Register dump_w = dump.W();
  280. Register tmp_w = tmp.W();
  281. // Offsets into the dump_ structure.
  282. const int x_offset = offsetof(dump_t, x_);
  283. const int w_offset = offsetof(dump_t, w_);
  284. const int d_offset = offsetof(dump_t, d_);
  285. const int s_offset = offsetof(dump_t, s_);
  286. const int sp_offset = offsetof(dump_t, sp_);
  287. const int wsp_offset = offsetof(dump_t, wsp_);
  288. const int flags_offset = offsetof(dump_t, flags_);
  289. __ Push(xzr, dump_base, dump, tmp);
  290. // Load the address where we will dump the state.
  291. __ Mov(dump_base, reinterpret_cast<uint64_t>(&dump_));
  292. // Dump the stack pointer (sp and wsp).
  293. // The stack pointer cannot be stored directly; it needs to be moved into
  294. // another register first. Also, we pushed four X registers, so we need to
  295. // compensate here.
  296. __ Add(tmp, sp, 4 * kXRegSizeInBytes);
  297. __ Str(tmp, MemOperand(dump_base, sp_offset));
  298. __ Add(tmp_w, wsp, 4 * kXRegSizeInBytes);
  299. __ Str(tmp_w, MemOperand(dump_base, wsp_offset));
  300. // Dump X registers.
  301. __ Add(dump, dump_base, x_offset);
  302. for (unsigned i = 0; i < kNumberOfRegisters; i += 2) {
  303. __ Stp(Register::XRegFromCode(i), Register::XRegFromCode(i + 1),
  304. MemOperand(dump, i * kXRegSizeInBytes));
  305. }
  306. // Dump W registers.
  307. __ Add(dump, dump_base, w_offset);
  308. for (unsigned i = 0; i < kNumberOfRegisters; i += 2) {
  309. __ Stp(Register::WRegFromCode(i), Register::WRegFromCode(i + 1),
  310. MemOperand(dump, i * kWRegSizeInBytes));
  311. }
  312. // Dump D registers.
  313. __ Add(dump, dump_base, d_offset);
  314. for (unsigned i = 0; i < kNumberOfFPRegisters; i += 2) {
  315. __ Stp(FPRegister::DRegFromCode(i), FPRegister::DRegFromCode(i + 1),
  316. MemOperand(dump, i * kDRegSizeInBytes));
  317. }
  318. // Dump S registers.
  319. __ Add(dump, dump_base, s_offset);
  320. for (unsigned i = 0; i < kNumberOfFPRegisters; i += 2) {
  321. __ Stp(FPRegister::SRegFromCode(i), FPRegister::SRegFromCode(i + 1),
  322. MemOperand(dump, i * kSRegSizeInBytes));
  323. }
  324. // Dump the flags.
  325. __ Mrs(tmp, NZCV);
  326. __ Str(tmp, MemOperand(dump_base, flags_offset));
  327. // To dump the values that were in tmp amd dump, we need a new scratch
  328. // register. We can use any of the already dumped registers since we can
  329. // easily restore them.
  330. Register dump2_base = x10;
  331. Register dump2 = x11;
  332. assert(!AreAliased(dump_base, dump, tmp, dump2_base, dump2));
  333. // Don't lose the dump_ address.
  334. __ Mov(dump2_base, dump_base);
  335. __ Pop(tmp, dump, dump_base, xzr);
  336. __ Add(dump2, dump2_base, w_offset);
  337. __ Str(dump_base_w, MemOperand(dump2, dump_base.code() * kWRegSizeInBytes));
  338. __ Str(dump_w, MemOperand(dump2, dump.code() * kWRegSizeInBytes));
  339. __ Str(tmp_w, MemOperand(dump2, tmp.code() * kWRegSizeInBytes));
  340. __ Add(dump2, dump2_base, x_offset);
  341. __ Str(dump_base, MemOperand(dump2, dump_base.code() * kXRegSizeInBytes));
  342. __ Str(dump, MemOperand(dump2, dump.code() * kXRegSizeInBytes));
  343. __ Str(tmp, MemOperand(dump2, tmp.code() * kXRegSizeInBytes));
  344. // Finally, restore dump2_base and dump2.
  345. __ Ldr(dump2_base, MemOperand(dump2, dump2_base.code() * kXRegSizeInBytes));
  346. __ Ldr(dump2, MemOperand(dump2, dump2.code() * kXRegSizeInBytes));
  347. // Restore the MacroAssembler's scratch registers.
  348. __ SetScratchRegisters(old_tmp0, old_tmp1);
  349. __ SetFPScratchRegister(old_fptmp0);
  350. completed_ = true;
  351. }
  352. } // namespace vixl