Backed out changeset 8f976ed899d7 (bug 1847231) for causing bc failures on browser_se...
[gecko.git] / js / src / jsapi-tests / testJitRValueAlloc.cpp
blob27242b7652a79a3adfb1c3b1adfbfb1507565f25
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "jit/Snapshots.h"
10 #include "jsapi-tests/tests.h"
12 using namespace js;
13 using namespace js::jit;
15 // These tests are checking that all slots of the current architecture can all
16 // be encoded and decoded correctly. We iterate on all registers and on many
17 // fake stack locations (Fibonacci).
18 static RValueAllocation Read(const RValueAllocation& slot) {
19 CompactBufferWriter writer;
20 slot.write(writer);
22 // Call hash to run its assertions.
23 slot.hash();
25 CompactBufferReader reader(writer);
26 return RValueAllocation::read(reader);
29 class Fibonacci {
30 class Iterator {
31 public:
32 // std::iterator traits.
33 using iterator_category = std::input_iterator_tag;
34 using value_type = int32_t;
35 using difference_type = int32_t;
36 using pointer = value_type*;
37 using reference = value_type&;
39 private:
40 uint32_t value_{};
41 uint32_t last_{};
43 Iterator() = default;
44 Iterator(value_type value, value_type last) : value_(value), last_(last) {}
46 friend class Fibonacci;
48 public:
49 Iterator& operator++() {
50 auto next = value_ + last_;
51 if (next <= static_cast<uint32_t>(INT32_MAX)) {
52 last_ = value_;
53 value_ = next;
54 } else {
55 *this = Iterator{};
57 return *this;
60 bool operator==(const Iterator& other) const {
61 return value_ == other.value_ && last_ == other.last_;
64 bool operator!=(const Iterator& other) const { return !(*this == other); }
66 auto operator*() const { return static_cast<int32_t>(value_); }
69 public:
70 auto begin() { return Iterator{0, 1}; }
72 auto end() { return Iterator{}; }
75 BEGIN_TEST(testJitRValueAlloc_Double) {
76 RValueAllocation s;
77 for (uint32_t i = 0; i < FloatRegisters::Total; i++) {
78 s = RValueAllocation::Double(FloatRegister::FromCode(i));
79 CHECK(s == Read(s));
81 return true;
83 END_TEST(testJitRValueAlloc_Double)
85 BEGIN_TEST(testJitRValueAlloc_FloatReg) {
86 RValueAllocation s;
87 for (uint32_t i = 0; i < FloatRegisters::Total; i++) {
88 s = RValueAllocation::AnyFloat(FloatRegister::FromCode(i));
89 CHECK(s == Read(s));
91 return true;
93 END_TEST(testJitRValueAlloc_FloatReg)
95 BEGIN_TEST(testJitRValueAlloc_FloatStack) {
96 RValueAllocation s;
97 for (auto i : Fibonacci{}) {
98 s = RValueAllocation::AnyFloat(i);
99 CHECK(s == Read(s));
101 return true;
103 END_TEST(testJitRValueAlloc_FloatStack)
105 BEGIN_TEST(testJitRValueAlloc_TypedReg) {
106 RValueAllocation s;
107 for (uint32_t i = 0; i < Registers::Total; i++) {
108 #define FOR_EACH_JSVAL(_) \
109 /* _(JSVAL_TYPE_DOUBLE) */ \
110 _(JSVAL_TYPE_INT32) \
111 /* _(JSVAL_TYPE_UNDEFINED) */ \
112 _(JSVAL_TYPE_BOOLEAN) \
113 /* _(JSVAL_TYPE_MAGIC) */ \
114 _(JSVAL_TYPE_STRING) \
115 _(JSVAL_TYPE_SYMBOL) \
116 _(JSVAL_TYPE_BIGINT) \
117 /* _(JSVAL_TYPE_NULL) */ \
118 _(JSVAL_TYPE_OBJECT)
120 #define CHECK_WITH_JSVAL(jsval) \
121 s = RValueAllocation::Typed(jsval, Register::FromCode(i)); \
122 CHECK(s == Read(s));
124 FOR_EACH_JSVAL(CHECK_WITH_JSVAL)
125 #undef CHECK_WITH_JSVAL
126 #undef FOR_EACH_JSVAL
128 return true;
130 END_TEST(testJitRValueAlloc_TypedReg)
132 BEGIN_TEST(testJitRValueAlloc_TypedStack) {
133 RValueAllocation s;
134 for (auto i : Fibonacci{}) {
135 #define FOR_EACH_JSVAL(_) \
136 _(JSVAL_TYPE_DOUBLE) \
137 _(JSVAL_TYPE_INT32) \
138 /* _(JSVAL_TYPE_UNDEFINED) */ \
139 _(JSVAL_TYPE_BOOLEAN) \
140 /* _(JSVAL_TYPE_MAGIC) */ \
141 _(JSVAL_TYPE_STRING) \
142 _(JSVAL_TYPE_SYMBOL) \
143 _(JSVAL_TYPE_BIGINT) \
144 /* _(JSVAL_TYPE_NULL) */ \
145 _(JSVAL_TYPE_OBJECT)
147 #define CHECK_WITH_JSVAL(jsval) \
148 s = RValueAllocation::Typed(jsval, i); \
149 CHECK(s == Read(s));
151 FOR_EACH_JSVAL(CHECK_WITH_JSVAL)
152 #undef CHECK_WITH_JSVAL
153 #undef FOR_EACH_JSVAL
155 return true;
157 END_TEST(testJitRValueAlloc_TypedStack)
159 #if defined(JS_NUNBOX32)
161 BEGIN_TEST(testJitRValueAlloc_UntypedRegReg) {
162 RValueAllocation s;
163 for (uint32_t i = 0; i < Registers::Total; i++) {
164 for (uint32_t j = 0; j < Registers::Total; j++) {
165 if (i == j) {
166 continue;
168 s = RValueAllocation::Untyped(Register::FromCode(i),
169 Register::FromCode(j));
170 MOZ_ASSERT(s == Read(s));
171 CHECK(s == Read(s));
174 return true;
176 END_TEST(testJitRValueAlloc_UntypedRegReg)
178 BEGIN_TEST(testJitRValueAlloc_UntypedRegStack) {
179 RValueAllocation s;
180 for (uint32_t i = 0; i < Registers::Total; i++) {
181 for (auto j : Fibonacci{}) {
182 s = RValueAllocation::Untyped(Register::FromCode(i), j);
183 CHECK(s == Read(s));
186 return true;
188 END_TEST(testJitRValueAlloc_UntypedRegStack)
190 BEGIN_TEST(testJitRValueAlloc_UntypedStackReg) {
191 RValueAllocation s;
192 for (auto i : Fibonacci{}) {
193 for (uint32_t j = 0; j < Registers::Total; j++) {
194 s = RValueAllocation::Untyped(i, Register::FromCode(j));
195 CHECK(s == Read(s));
198 return true;
200 END_TEST(testJitRValueAlloc_UntypedStackReg)
202 BEGIN_TEST(testJitRValueAlloc_UntypedStackStack) {
203 RValueAllocation s;
204 for (auto i : Fibonacci{}) {
205 for (auto j : Fibonacci{}) {
206 s = RValueAllocation::Untyped(i, j);
207 CHECK(s == Read(s));
210 return true;
212 END_TEST(testJitRValueAlloc_UntypedStackStack)
214 #else
216 BEGIN_TEST(testJitRValueAlloc_UntypedReg) {
217 RValueAllocation s;
218 for (uint32_t i = 0; i < Registers::Total; i++) {
219 s = RValueAllocation::Untyped(Register::FromCode(i));
220 CHECK(s == Read(s));
222 return true;
224 END_TEST(testJitRValueAlloc_UntypedReg)
226 BEGIN_TEST(testJitRValueAlloc_UntypedStack) {
227 RValueAllocation s;
228 for (auto i : Fibonacci{}) {
229 s = RValueAllocation::Untyped(i);
230 CHECK(s == Read(s));
232 return true;
234 END_TEST(testJitRValueAlloc_UntypedStack)
236 #endif
238 BEGIN_TEST(testJitRValueAlloc_UndefinedAndNull) {
239 RValueAllocation s;
240 s = RValueAllocation::Undefined();
241 CHECK(s == Read(s));
242 s = RValueAllocation::Null();
243 CHECK(s == Read(s));
244 return true;
246 END_TEST(testJitRValueAlloc_UndefinedAndNull)
248 BEGIN_TEST(testJitRValueAlloc_ConstantPool) {
249 RValueAllocation s;
250 for (auto i : Fibonacci{}) {
251 s = RValueAllocation::ConstantPool(i);
252 CHECK(s == Read(s));
254 return true;
256 END_TEST(testJitRValueAlloc_ConstantPool)