blob: a05231e534833f911e3d8d468fd72750a45230d7 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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 Murdoch589d6972011-11-30 16:04:58 +000029#include <wchar.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include "src/compiler.h"
34#include "src/disasm.h"
35#include "src/parser.h"
36#include "test/cctest/cctest.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037
38using namespace v8::internal;
39
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040static Handle<Object> GetGlobalProperty(const char* name) {
41 Isolate* isolate = CcTest::i_isolate();
42 return Object::GetProperty(
43 isolate, isolate->global_object(), name).ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +000044}
45
46
47static void SetGlobalProperty(const char* name, Object* value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 Isolate* isolate = CcTest::i_isolate();
49 Handle<Object> object(value, isolate);
50 Handle<String> internalized_name =
51 isolate->factory()->InternalizeUtf8String(name);
52 Handle<JSObject> global(isolate->context()->global_object());
53 Runtime::SetObjectProperty(isolate, global, internalized_name, object,
54 SLOPPY).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +000055}
56
57
58static Handle<JSFunction> Compile(const char* source) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 Isolate* isolate = CcTest::i_isolate();
60 Handle<String> source_code = isolate->factory()->NewStringFromUtf8(
61 CStrVector(source)).ToHandleChecked();
62 Handle<SharedFunctionInfo> shared_function = Compiler::CompileScript(
63 source_code, Handle<String>(), 0, 0, false,
64 Handle<Context>(isolate->native_context()), NULL, NULL,
65 v8::ScriptCompiler::kNoCompileOptions, NOT_NATIVES_CODE);
66 return isolate->factory()->NewFunctionFromSharedFunctionInfo(
67 shared_function, isolate->native_context());
Steve Blocka7e24c12009-10-30 11:49:00 +000068}
69
70
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071static double Inc(Isolate* isolate, int x) {
Steve Blocka7e24c12009-10-30 11:49:00 +000072 const char* source = "result = %d + 1;";
73 EmbeddedVector<char, 512> buffer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 SNPrintF(buffer, source, x);
Steve Blocka7e24c12009-10-30 11:49:00 +000075
76 Handle<JSFunction> fun = Compile(buffer.start());
77 if (fun.is_null()) return -1;
78
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 Handle<JSObject> global(isolate->context()->global_object());
80 Execution::Call(isolate, fun, global, 0, NULL).Check();
81 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +000082}
83
84
85TEST(Inc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 CcTest::InitializeVM();
87 v8::HandleScope scope(CcTest::isolate());
88 CHECK_EQ(4.0, Inc(CcTest::i_isolate(), 3));
Steve Blocka7e24c12009-10-30 11:49:00 +000089}
90
91
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092static double Add(Isolate* isolate, int x, int y) {
Steve Blocka7e24c12009-10-30 11:49:00 +000093 Handle<JSFunction> fun = Compile("result = x + y;");
94 if (fun.is_null()) return -1;
95
96 SetGlobalProperty("x", Smi::FromInt(x));
97 SetGlobalProperty("y", Smi::FromInt(y));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 Handle<JSObject> global(isolate->context()->global_object());
99 Execution::Call(isolate, fun, global, 0, NULL).Check();
100 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000101}
102
103
104TEST(Add) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 CcTest::InitializeVM();
106 v8::HandleScope scope(CcTest::isolate());
107 CHECK_EQ(5.0, Add(CcTest::i_isolate(), 2, 3));
Steve Blocka7e24c12009-10-30 11:49:00 +0000108}
109
110
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111static double Abs(Isolate* isolate, int x) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 Handle<JSFunction> fun = Compile("if (x < 0) result = -x; else result = x;");
113 if (fun.is_null()) return -1;
114
115 SetGlobalProperty("x", Smi::FromInt(x));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 Handle<JSObject> global(isolate->context()->global_object());
117 Execution::Call(isolate, fun, global, 0, NULL).Check();
118 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000119}
120
121
122TEST(Abs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 CcTest::InitializeVM();
124 v8::HandleScope scope(CcTest::isolate());
125 CHECK_EQ(3.0, Abs(CcTest::i_isolate(), -3));
Steve Blocka7e24c12009-10-30 11:49:00 +0000126}
127
128
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129static double Sum(Isolate* isolate, int n) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 Handle<JSFunction> fun =
131 Compile("s = 0; while (n > 0) { s += n; n -= 1; }; result = s;");
132 if (fun.is_null()) return -1;
133
134 SetGlobalProperty("n", Smi::FromInt(n));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 Handle<JSObject> global(isolate->context()->global_object());
136 Execution::Call(isolate, fun, global, 0, NULL).Check();
137 return GetGlobalProperty("result")->Number();
Steve Blocka7e24c12009-10-30 11:49:00 +0000138}
139
140
141TEST(Sum) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 CcTest::InitializeVM();
143 v8::HandleScope scope(CcTest::isolate());
144 CHECK_EQ(5050.0, Sum(CcTest::i_isolate(), 100));
Steve Blocka7e24c12009-10-30 11:49:00 +0000145}
146
147
148TEST(Print) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 v8::HandleScope scope(CcTest::isolate());
150 v8::Local<v8::Context> context = CcTest::NewContext(PRINT_EXTENSION);
151 v8::Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 const char* source = "for (n = 0; n < 100; ++n) print(n, 1, 2);";
153 Handle<JSFunction> fun = Compile(source);
154 if (fun.is_null()) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
156 Execution::Call(CcTest::i_isolate(), fun, global, 0, NULL).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +0000157}
158
159
160// The following test method stems from my coding efforts today. It
161// tests all the functionality I have added to the compiler today
162TEST(Stuff) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 CcTest::InitializeVM();
164 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 const char* source =
166 "r = 0;\n"
167 "a = new Object;\n"
168 "if (a == a) r+=1;\n" // 1
169 "if (a != new Object()) r+=2;\n" // 2
170 "a.x = 42;\n"
171 "if (a.x == 42) r+=4;\n" // 4
172 "function foo() { var x = 87; return x; }\n"
173 "if (foo() == 87) r+=8;\n" // 8
174 "function bar() { var x; x = 99; return x; }\n"
175 "if (bar() == 99) r+=16;\n" // 16
176 "function baz() { var x = 1, y, z = 2; y = 3; return x + y + z; }\n"
177 "if (baz() == 6) r+=32;\n" // 32
178 "function Cons0() { this.x = 42; this.y = 87; }\n"
179 "if (new Cons0().x == 42) r+=64;\n" // 64
180 "if (new Cons0().y == 87) r+=128;\n" // 128
181 "function Cons2(x, y) { this.sum = x + y; }\n"
182 "if (new Cons2(3,4).sum == 7) r+=256;"; // 256
183
184 Handle<JSFunction> fun = Compile(source);
185 CHECK(!fun.is_null());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 Handle<JSObject> global(CcTest::i_isolate()->context()->global_object());
187 Execution::Call(
188 CcTest::i_isolate(), fun, global, 0, NULL).Check();
189 CHECK_EQ(511.0, GetGlobalProperty("r")->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +0000190}
191
192
193TEST(UncaughtThrow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 CcTest::InitializeVM();
195 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000196
197 const char* source = "throw 42;";
198 Handle<JSFunction> fun = Compile(source);
199 CHECK(!fun.is_null());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 Isolate* isolate = fun->GetIsolate();
201 Handle<JSObject> global(isolate->context()->global_object());
202 CHECK(Execution::Call(isolate, fun, global, 0, NULL).is_null());
203 CHECK_EQ(42.0, isolate->pending_exception()->Number());
Steve Blocka7e24c12009-10-30 11:49:00 +0000204}
205
206
207// Tests calling a builtin function from C/C++ code, and the builtin function
208// performs GC. It creates a stack frame looks like following:
209// | C (PerformGC) |
210// | JS-to-C |
211// | JS |
212// | C-to-JS |
213TEST(C2JSFrames) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 FLAG_expose_gc = true;
215 v8::HandleScope scope(CcTest::isolate());
216 v8::Local<v8::Context> context =
217 CcTest::NewContext(PRINT_EXTENSION | GC_EXTENSION);
218 v8::Context::Scope context_scope(context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000219
220 const char* source = "function foo(a) { gc(), print(a); }";
221
222 Handle<JSFunction> fun0 = Compile(source);
223 CHECK(!fun0.is_null());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 Isolate* isolate = fun0->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000225
226 // Run the generated code to populate the global object with 'foo'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 Handle<JSObject> global(isolate->context()->global_object());
228 Execution::Call(isolate, fun0, global, 0, NULL).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +0000229
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 Handle<String> foo_string =
231 isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("foo"));
232 Handle<Object> fun1 = Object::GetProperty(
233 isolate->global_object(), foo_string).ToHandleChecked();
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 CHECK(fun1->IsJSFunction());
235
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000236 Handle<Object> argv[] = {isolate->factory()->InternalizeOneByteString(
237 STATIC_CHAR_VECTOR("hello"))};
238 Execution::Call(isolate,
239 Handle<JSFunction>::cast(fun1),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240 global,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 arraysize(argv),
242 argv).Check();
Steve Blocka7e24c12009-10-30 11:49:00 +0000243}
244
245
246// Regression 236. Calling InitLineEnds on a Script with undefined
247// source resulted in crash.
248TEST(Regression236) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 CcTest::InitializeVM();
250 Isolate* isolate = CcTest::i_isolate();
251 Factory* factory = isolate->factory();
252 v8::HandleScope scope(CcTest::isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000253
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 Handle<Script> script = factory->NewScript(factory->empty_string());
255 script->set_source(CcTest::heap()->undefined_value());
256 CHECK_EQ(-1, Script::GetLineNumber(script, 0));
257 CHECK_EQ(-1, Script::GetLineNumber(script, 100));
258 CHECK_EQ(-1, Script::GetLineNumber(script, -1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000259}
Andrei Popescu402d9372010-02-26 13:31:12 +0000260
261
262TEST(GetScriptLineNumber) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263 LocalContext context;
264 v8::HandleScope scope(CcTest::isolate());
265 v8::ScriptOrigin origin =
266 v8::ScriptOrigin(v8::String::NewFromUtf8(CcTest::isolate(), "test"));
Andrei Popescu402d9372010-02-26 13:31:12 +0000267 const char function_f[] = "function f() {}";
268 const int max_rows = 1000;
269 const int buffer_size = max_rows + sizeof(function_f);
270 ScopedVector<char> buffer(buffer_size);
271 memset(buffer.start(), '\n', buffer_size - 1);
272 buffer[buffer_size - 1] = '\0';
273
274 for (int i = 0; i < max_rows; ++i) {
275 if (i > 0)
276 buffer[i - 1] = '\n';
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 MemCopy(&buffer[i], function_f, sizeof(function_f) - 1);
278 v8::Handle<v8::String> script_body =
279 v8::String::NewFromUtf8(CcTest::isolate(), buffer.start());
Andrei Popescu402d9372010-02-26 13:31:12 +0000280 v8::Script::Compile(script_body, &origin)->Run();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 v8::Local<v8::Function> f =
282 v8::Local<v8::Function>::Cast(context->Global()->Get(
283 v8::String::NewFromUtf8(CcTest::isolate(), "f")));
Andrei Popescu402d9372010-02-26 13:31:12 +0000284 CHECK_EQ(i, f->GetScriptLineNumber());
285 }
286}
Steve Block053d10c2011-06-13 19:13:29 +0100287
288
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289TEST(FeedbackVectorPreservedAcrossRecompiles) {
290 if (i::FLAG_always_opt || !i::FLAG_crankshaft) return;
291 i::FLAG_allow_natives_syntax = true;
292 CcTest::InitializeVM();
293 if (!CcTest::i_isolate()->use_crankshaft()) return;
294 v8::HandleScope scope(CcTest::isolate());
295
296 // Make sure function f has a call that uses a type feedback slot.
297 CompileRun("function fun() {};"
298 "fun1 = fun;"
299 "function f(a) { a(); } f(fun1);");
300
301 Handle<JSFunction> f =
302 v8::Utils::OpenHandle(
303 *v8::Handle<v8::Function>::Cast(
304 CcTest::global()->Get(v8_str("f"))));
305
306 // We shouldn't have deoptimization support. We want to recompile and
307 // verify that our feedback vector preserves information.
308 CHECK(!f->shared()->has_deoptimization_support());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400309 Handle<TypeFeedbackVector> feedback_vector(f->shared()->feedback_vector());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310
311 // Verify that we gathered feedback.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400312 int expected_slots = 0;
313 int expected_ic_slots = 1;
314 CHECK_EQ(expected_slots, feedback_vector->Slots());
315 CHECK_EQ(expected_ic_slots, feedback_vector->ICSlots());
316 FeedbackVectorICSlot slot_for_a(0);
317 CHECK(feedback_vector->Get(slot_for_a)->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318
319 CompileRun("%OptimizeFunctionOnNextCall(f); f(fun1);");
320
321 // Verify that the feedback is still "gathered" despite a recompilation
322 // of the full code.
323 CHECK(f->IsOptimized());
324 CHECK(f->shared()->has_deoptimization_support());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400325 CHECK(f->shared()->feedback_vector()->Get(slot_for_a)->IsJSFunction());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326}
327
328
329TEST(FeedbackVectorUnaffectedByScopeChanges) {
330 if (i::FLAG_always_opt || !i::FLAG_lazy) return;
331 CcTest::InitializeVM();
332 v8::HandleScope scope(CcTest::isolate());
333
334 CompileRun("function builder() {"
335 " call_target = function() { return 3; };"
336 " return (function() {"
337 " eval('');"
338 " return function() {"
339 " 'use strict';"
340 " call_target();"
341 " }"
342 " })();"
343 "}"
344 "morphing_call = builder();");
345
346 Handle<JSFunction> f =
347 v8::Utils::OpenHandle(
348 *v8::Handle<v8::Function>::Cast(
349 CcTest::global()->Get(v8_str("morphing_call"))));
350
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400351 // Not compiled, and so no feedback vector allocated yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000352 CHECK(!f->shared()->is_compiled());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400353 CHECK_EQ(0, f->shared()->feedback_vector()->Slots());
354 CHECK_EQ(0, f->shared()->feedback_vector()->ICSlots());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355
356 CompileRun("morphing_call();");
357
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400358 // Now a feedback vector is allocated.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359 CHECK(f->shared()->is_compiled());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400360 int expected_slots = 0;
361 int expected_ic_slots = FLAG_vector_ics ? 2 : 1;
362 CHECK_EQ(expected_slots, f->shared()->feedback_vector()->Slots());
363 CHECK_EQ(expected_ic_slots, f->shared()->feedback_vector()->ICSlots());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000364}
365
366
367// Test that optimized code for different closures is actually shared
368// immediately by the FastNewClosureStub when run in the same context.
369TEST(OptimizedCodeSharing) {
370 // Skip test if --cache-optimized-code is not activated by default because
371 // FastNewClosureStub that is baked into the snapshot is incorrect.
372 if (!FLAG_cache_optimized_code) return;
373 FLAG_stress_compaction = false;
374 FLAG_allow_natives_syntax = true;
375 CcTest::InitializeVM();
376 v8::HandleScope scope(CcTest::isolate());
377 for (int i = 0; i < 10; i++) {
378 LocalContext env;
379 env->Global()->Set(v8::String::NewFromUtf8(CcTest::isolate(), "x"),
380 v8::Integer::New(CcTest::isolate(), i));
381 CompileRun("function MakeClosure() {"
382 " return function() { return x; };"
383 "}"
384 "var closure0 = MakeClosure();"
385 "%DebugPrint(closure0());"
386 "%OptimizeFunctionOnNextCall(closure0);"
387 "%DebugPrint(closure0());"
388 "var closure1 = MakeClosure();"
389 "var closure2 = MakeClosure();");
390 Handle<JSFunction> fun1 = v8::Utils::OpenHandle(
391 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure1"))));
392 Handle<JSFunction> fun2 = v8::Utils::OpenHandle(
393 *v8::Local<v8::Function>::Cast(env->Global()->Get(v8_str("closure2"))));
394 CHECK(fun1->IsOptimized()
395 || !CcTest::i_isolate()->use_crankshaft() || !fun1->IsOptimizable());
396 CHECK(fun2->IsOptimized()
397 || !CcTest::i_isolate()->use_crankshaft() || !fun2->IsOptimizable());
398 CHECK_EQ(fun1->code(), fun2->code());
399 }
400}
401
402
Steve Block053d10c2011-06-13 19:13:29 +0100403#ifdef ENABLE_DISASSEMBLER
404static Handle<JSFunction> GetJSFunction(v8::Handle<v8::Object> obj,
405 const char* property_name) {
406 v8::Local<v8::Function> fun =
407 v8::Local<v8::Function>::Cast(obj->Get(v8_str(property_name)));
408 return v8::Utils::OpenHandle(*fun);
409}
410
411
412static void CheckCodeForUnsafeLiteral(Handle<JSFunction> f) {
413 // Create a disassembler with default name lookup.
414 disasm::NameConverter name_converter;
415 disasm::Disassembler d(name_converter);
416
417 if (f->code()->kind() == Code::FUNCTION) {
418 Address pc = f->code()->instruction_start();
419 int decode_size =
420 Min(f->code()->instruction_size(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 static_cast<int>(f->code()->back_edge_table_offset()));
Steve Block053d10c2011-06-13 19:13:29 +0100422 Address end = pc + decode_size;
423
424 v8::internal::EmbeddedVector<char, 128> decode_buffer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 v8::internal::EmbeddedVector<char, 128> smi_hex_buffer;
426 Smi* smi = Smi::FromInt(12345678);
427 SNPrintF(smi_hex_buffer, "0x%" V8PRIxPTR, reinterpret_cast<intptr_t>(smi));
Steve Block053d10c2011-06-13 19:13:29 +0100428 while (pc < end) {
Ben Murdoch42effa52011-08-19 16:40:31 +0100429 int num_const = d.ConstantPoolSizeAt(pc);
430 if (num_const >= 0) {
431 pc += (num_const + 1) * kPointerSize;
432 } else {
433 pc += d.InstructionDecode(decode_buffer, pc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 CHECK(strstr(decode_buffer.start(), smi_hex_buffer.start()) == NULL);
Ben Murdoch42effa52011-08-19 16:40:31 +0100435 }
Steve Block053d10c2011-06-13 19:13:29 +0100436 }
437 }
438}
439
440
441TEST(SplitConstantsInFullCompiler) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 LocalContext context;
443 v8::HandleScope scope(CcTest::isolate());
Steve Block053d10c2011-06-13 19:13:29 +0100444
445 CompileRun("function f() { a = 12345678 }; f();");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100447 CompileRun("function f(x) { a = 12345678 + x}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000448 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100449 CompileRun("function f(x) { var arguments = 1; x += 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100451 CompileRun("function f(x) { var arguments = 1; x = 12345678}; f(1);");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 CheckCodeForUnsafeLiteral(GetJSFunction(context->Global(), "f"));
Steve Block053d10c2011-06-13 19:13:29 +0100453}
454#endif