blob: 77dc85902292a79c76745c2c20ad9f458e413a83 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029#include <iostream> // NOLINT(readability/streams)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031#include "src/base/utils/random-number-generator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include "src/macro-assembler.h"
33#include "src/mips/macro-assembler-mips.h"
34#include "src/mips/simulator-mips.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#include "src/v8.h"
36#include "test/cctest/cctest.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037
38
39using namespace v8::internal;
40
41typedef void* (*F)(int x, int y, int p2, int p3, int p4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042typedef Object* (*F1)(int x, int p1, int p2, int p3, int p4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043
44#define __ masm->
45
46
47static byte to_non_zero(int n) {
48 return static_cast<unsigned>(n) % 255 + 1;
49}
50
51
52static bool all_zeroes(const byte* beg, const byte* end) {
53 CHECK(beg);
54 CHECK(beg <= end);
55 while (beg < end) {
56 if (*beg++ != 0)
57 return false;
58 }
59 return true;
60}
61
62
63TEST(CopyBytes) {
64 CcTest::InitializeVM();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065 Isolate* isolate = CcTest::i_isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 HandleScope handles(isolate);
67
68 const int data_size = 1 * KB;
69 size_t act_size;
70
71 // Allocate two blocks to copy data between.
72 byte* src_buffer =
73 static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0));
74 CHECK(src_buffer);
75 CHECK(act_size >= static_cast<size_t>(data_size));
76 byte* dest_buffer =
77 static_cast<byte*>(v8::base::OS::Allocate(data_size, &act_size, 0));
78 CHECK(dest_buffer);
79 CHECK(act_size >= static_cast<size_t>(data_size));
80
81 // Storage for a0 and a1.
82 byte* a0_;
83 byte* a1_;
84
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 MacroAssembler assembler(isolate, NULL, 0,
86 v8::internal::CodeObjectRequired::kYes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087 MacroAssembler* masm = &assembler;
88
89 // Code to be generated: The stuff in CopyBytes followed by a store of a0 and
90 // a1, respectively.
91 __ CopyBytes(a0, a1, a2, a3);
92 __ li(a2, Operand(reinterpret_cast<int>(&a0_)));
93 __ li(a3, Operand(reinterpret_cast<int>(&a1_)));
94 __ sw(a0, MemOperand(a2));
95 __ jr(ra);
96 __ sw(a1, MemOperand(a3));
97
98 CodeDesc desc;
99 masm->GetCode(&desc);
100 Handle<Code> code = isolate->factory()->NewCode(
101 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
102
103 ::F f = FUNCTION_CAST< ::F>(code->entry());
104
105 // Initialise source data with non-zero bytes.
106 for (int i = 0; i < data_size; i++) {
107 src_buffer[i] = to_non_zero(i);
108 }
109
110 const int fuzz = 11;
111
112 for (int size = 0; size < 600; size++) {
113 for (const byte* src = src_buffer; src < src_buffer + fuzz; src++) {
114 for (byte* dest = dest_buffer; dest < dest_buffer + fuzz; dest++) {
115 memset(dest_buffer, 0, data_size);
116 CHECK(dest + size < dest_buffer + data_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117 (void)CALL_GENERATED_CODE(isolate, f, reinterpret_cast<int>(src),
118 reinterpret_cast<int>(dest), size, 0, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 // a0 and a1 should point at the first byte after the copied data.
120 CHECK_EQ(src + size, a0_);
121 CHECK_EQ(dest + size, a1_);
122 // Check that we haven't written outside the target area.
123 CHECK(all_zeroes(dest_buffer, dest));
124 CHECK(all_zeroes(dest + size, dest_buffer + data_size));
125 // Check the target area.
126 CHECK_EQ(0, memcmp(src, dest, size));
127 }
128 }
129 }
130
131 // Check that the source data hasn't been clobbered.
132 for (int i = 0; i < data_size; i++) {
133 CHECK(src_buffer[i] == to_non_zero(i));
134 }
135}
136
137
138static void TestNaN(const char *code) {
139 // NaN value is different on MIPS and x86 architectures, and TEST(NaNx)
140 // tests checks the case where a x86 NaN value is serialized into the
141 // snapshot on the simulator during cross compilation.
142 v8::HandleScope scope(CcTest::isolate());
143 v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
144 v8::Context::Scope context_scope(context);
145
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146 v8::Local<v8::Script> script =
147 v8::Script::Compile(context, v8_str(code)).ToLocalChecked();
148 v8::Local<v8::Object> result =
149 v8::Local<v8::Object>::Cast(script->Run(context).ToLocalChecked());
150 i::Handle<i::JSReceiver> o = v8::Utils::OpenHandle(*result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 i::Handle<i::JSArray> array1(reinterpret_cast<i::JSArray*>(*o));
152 i::FixedDoubleArray* a = i::FixedDoubleArray::cast(array1->elements());
153 double value = a->get_scalar(0);
154 CHECK(std::isnan(value) &&
155 bit_cast<uint64_t>(value) ==
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000156 bit_cast<uint64_t>(std::numeric_limits<double>::quiet_NaN()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157}
158
159
160TEST(NaN0) {
161 TestNaN(
162 "var result;"
163 "for (var i = 0; i < 2; i++) {"
164 " result = new Array(Number.NaN, Number.POSITIVE_INFINITY);"
165 "}"
166 "result;");
167}
168
169
170TEST(NaN1) {
171 TestNaN(
172 "var result;"
173 "for (var i = 0; i < 2; i++) {"
174 " result = [NaN];"
175 "}"
176 "result;");
177}
178
179
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000180TEST(jump_tables4) {
181 // Similar to test-assembler-mips jump_tables1, with extra test for branch
182 // trampoline required before emission of the dd table (where trampolines are
183 // blocked), and proper transition to long-branch mode.
184 // Regression test for v8:4294.
185 CcTest::InitializeVM();
186 Isolate* isolate = CcTest::i_isolate();
187 HandleScope scope(isolate);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100188 MacroAssembler assembler(isolate, nullptr, 0,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000189 v8::internal::CodeObjectRequired::kYes);
190 MacroAssembler* masm = &assembler;
191
192 const int kNumCases = 512;
193 int values[kNumCases];
194 isolate->random_number_generator()->NextBytes(values, sizeof(values));
195 Label labels[kNumCases];
Ben Murdoch097c5b22016-05-18 11:27:45 +0100196 Label near_start, end, done;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197
Ben Murdoch097c5b22016-05-18 11:27:45 +0100198 __ Push(ra);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199 __ mov(v0, zero_reg);
200
201 __ Branch(&end);
202 __ bind(&near_start);
203
204 // Generate slightly less than 32K instructions, which will soon require
205 // trampoline for branch distance fixup.
206 for (int i = 0; i < 32768 - 256; ++i) {
207 __ addiu(v0, v0, 1);
208 }
209
Ben Murdoch097c5b22016-05-18 11:27:45 +0100210 __ GenerateSwitchTable(a0, kNumCases,
211 [&labels](size_t i) { return labels + i; });
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000212
213 for (int i = 0; i < kNumCases; ++i) {
214 __ bind(&labels[i]);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100215 __ li(v0, values[i]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 __ Branch(&done);
217 }
218
219 __ bind(&done);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100220 __ Pop(ra);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000221 __ jr(ra);
222 __ nop();
223
224 __ bind(&end);
225 __ Branch(&near_start);
226
227 CodeDesc desc;
228 masm->GetCode(&desc);
229 Handle<Code> code = isolate->factory()->NewCode(
230 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
231#ifdef OBJECT_PRINT
232 code->Print(std::cout);
233#endif
234 F1 f = FUNCTION_CAST<F1>(code->entry());
235 for (int i = 0; i < kNumCases; ++i) {
236 int res =
237 reinterpret_cast<int>(CALL_GENERATED_CODE(isolate, f, i, 0, 0, 0, 0));
238 ::printf("f(%d) = %d\n", i, res);
239 CHECK_EQ(values[i], res);
240 }
241}
242
243
244TEST(jump_tables5) {
245 if (!IsMipsArchVariant(kMips32r6)) return;
246
247 // Similar to test-assembler-mips jump_tables1, with extra test for emitting a
248 // compact branch instruction before emission of the dd table.
249 CcTest::InitializeVM();
250 Isolate* isolate = CcTest::i_isolate();
251 HandleScope scope(isolate);
252 MacroAssembler assembler(isolate, nullptr, 0,
253 v8::internal::CodeObjectRequired::kYes);
254 MacroAssembler* masm = &assembler;
255
256 const int kNumCases = 512;
257 int values[kNumCases];
258 isolate->random_number_generator()->NextBytes(values, sizeof(values));
259 Label labels[kNumCases];
260 Label done;
261
Ben Murdoch097c5b22016-05-18 11:27:45 +0100262 __ Push(ra);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263
264 {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100265 __ BlockTrampolinePoolFor(kNumCases + 6 + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000266 PredictableCodeSizeScope predictable(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100267 masm, kNumCases * kPointerSize + ((6 + 1) * Assembler::kInstrSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000268
Ben Murdoch097c5b22016-05-18 11:27:45 +0100269 __ addiupc(at, 6 + 1);
270 __ lsa(at, at, a0, 2);
271 __ lw(at, MemOperand(at));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000272 __ jalr(at);
273 __ nop(); // Branch delay slot nop.
274 __ bc(&done);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100275 // A nop instruction must be generated by the forbidden slot guard
276 // (Assembler::dd(Label*)).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277 for (int i = 0; i < kNumCases; ++i) {
278 __ dd(&labels[i]);
279 }
280 }
281
282 for (int i = 0; i < kNumCases; ++i) {
283 __ bind(&labels[i]);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100284 __ li(v0, values[i]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000285 __ jr(ra);
286 __ nop();
287 }
288
289 __ bind(&done);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100290 __ Pop(ra);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 __ jr(ra);
292 __ nop();
293
294 CodeDesc desc;
295 masm->GetCode(&desc);
296 Handle<Code> code = isolate->factory()->NewCode(
297 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
298#ifdef OBJECT_PRINT
299 code->Print(std::cout);
300#endif
301 F1 f = FUNCTION_CAST<F1>(code->entry());
302 for (int i = 0; i < kNumCases; ++i) {
303 int32_t res = reinterpret_cast<int32_t>(
304 CALL_GENERATED_CODE(isolate, f, i, 0, 0, 0, 0));
305 ::printf("f(%d) = %d\n", i, res);
306 CHECK_EQ(values[i], res);
307 }
308}
309
310
311static uint32_t run_lsa(uint32_t rt, uint32_t rs, int8_t sa) {
312 Isolate* isolate = CcTest::i_isolate();
313 HandleScope scope(isolate);
314 MacroAssembler assembler(isolate, nullptr, 0,
315 v8::internal::CodeObjectRequired::kYes);
316 MacroAssembler* masm = &assembler;
317
318 __ Lsa(v0, a0, a1, sa);
319 __ jr(ra);
320 __ nop();
321
322 CodeDesc desc;
323 assembler.GetCode(&desc);
324 Handle<Code> code = isolate->factory()->NewCode(
325 desc, Code::ComputeFlags(Code::STUB), Handle<Code>());
326
327 F1 f = FUNCTION_CAST<F1>(code->entry());
328
329 uint32_t res = reinterpret_cast<uint32_t>(
330 CALL_GENERATED_CODE(isolate, f, rt, rs, 0, 0, 0));
331
332 return res;
333}
334
335
336TEST(Lsa) {
337 CcTest::InitializeVM();
338 struct TestCaseLsa {
339 int32_t rt;
340 int32_t rs;
341 uint8_t sa;
342 uint32_t expected_res;
343 };
344
345 struct TestCaseLsa tc[] = {// rt, rs, sa, expected_res
346 {0x4, 0x1, 1, 0x6},
347 {0x4, 0x1, 2, 0x8},
348 {0x4, 0x1, 3, 0xc},
349 {0x4, 0x1, 4, 0x14},
350 {0x4, 0x1, 5, 0x24},
351 {0x0, 0x1, 1, 0x2},
352 {0x0, 0x1, 2, 0x4},
353 {0x0, 0x1, 3, 0x8},
354 {0x0, 0x1, 4, 0x10},
355 {0x0, 0x1, 5, 0x20},
356 {0x4, 0x0, 1, 0x4},
357 {0x4, 0x0, 2, 0x4},
358 {0x4, 0x0, 3, 0x4},
359 {0x4, 0x0, 4, 0x4},
360 {0x4, 0x0, 5, 0x4},
361
362 // Shift overflow.
363 {0x4, INT32_MAX, 1, 0x2},
364 {0x4, INT32_MAX >> 1, 2, 0x0},
365 {0x4, INT32_MAX >> 2, 3, 0xfffffffc},
366 {0x4, INT32_MAX >> 3, 4, 0xfffffff4},
367 {0x4, INT32_MAX >> 4, 5, 0xffffffe4},
368
369 // Signed addition overflow.
370 {INT32_MAX - 1, 0x1, 1, 0x80000000},
371 {INT32_MAX - 3, 0x1, 2, 0x80000000},
372 {INT32_MAX - 7, 0x1, 3, 0x80000000},
373 {INT32_MAX - 15, 0x1, 4, 0x80000000},
374 {INT32_MAX - 31, 0x1, 5, 0x80000000},
375
376 // Addition overflow.
377 {-2, 0x1, 1, 0x0},
378 {-4, 0x1, 2, 0x0},
379 {-8, 0x1, 3, 0x0},
380 {-16, 0x1, 4, 0x0},
381 {-32, 0x1, 5, 0x0}};
382
383 size_t nr_test_cases = sizeof(tc) / sizeof(TestCaseLsa);
384 for (size_t i = 0; i < nr_test_cases; ++i) {
385 uint32_t res = run_lsa(tc[i].rt, tc[i].rs, tc[i].sa);
386 PrintF("0x%x =? 0x%x == lsa(v0, %x, %x, %hhu)\n", tc[i].expected_res, res,
387 tc[i].rt, tc[i].rs, tc[i].sa);
388 CHECK_EQ(tc[i].expected_res, res);
389 }
390}
391
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392#undef __