2016-08-26 08:41:20 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "test/cctest/compiler/function-tester.h"
|
|
|
|
|
2018-07-23 11:42:37 +00:00
|
|
|
#include "src/api-inl.h"
|
[assembler] Factor out Registers from assembler.h
This refactoring reduces the LoC after preprocessor expansion by 370,322
gen ( 21 files): 71,503 to 1,631,168 ( 23x)
src ( 624 files): 367,639 to 53,231,764 ( 145x)
test ( 392 files): 490,770 to 37,450,839 ( 76x)
third_party ( 432 files): 239,085 to 9,547,902 ( 40x)
total ( 1521 files): 1,183,681 to 102,836,194 ( 87x)
gen ( 21 files): 71,503 to 1,613,222 ( 23x)
src ( 624 files): 367,634 to 52,964,046 ( 144x)
test ( 392 files): 490,771 to 37,366,181 ( 76x)
third_party ( 432 files): 239,085 to 9,547,902 ( 40x)
total ( 1521 files): 1,183,677 to 102,465,872 ( 87x)
Bug: v8:8562
Change-Id: Ib4e771c37471a2ff19c5538e62c038943cc74eaf
Reviewed-on: https://chromium-review.googlesource.com/c/1382469
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Georg Neis <neis@chromium.org>
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58349}
2018-12-18 15:02:38 +00:00
|
|
|
#include "src/assembler.h"
|
2019-01-29 08:41:54 +00:00
|
|
|
#include "src/compiler.h"
|
2016-08-26 08:41:20 +00:00
|
|
|
#include "src/compiler/linkage.h"
|
|
|
|
#include "src/compiler/pipeline.h"
|
|
|
|
#include "src/execution.h"
|
|
|
|
#include "src/handles.h"
|
|
|
|
#include "src/objects-inl.h"
|
2018-04-04 20:30:34 +00:00
|
|
|
#include "src/optimized-compilation-info.h"
|
2016-08-26 08:41:20 +00:00
|
|
|
#include "src/parsing/parse-info.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
FunctionTester::FunctionTester(const char* source, uint32_t flags)
|
|
|
|
: isolate(main_isolate()),
|
2018-07-31 11:12:53 +00:00
|
|
|
canonical(isolate),
|
2016-08-26 08:41:20 +00:00
|
|
|
function((FLAG_allow_natives_syntax = true, NewFunction(source))),
|
|
|
|
flags_(flags) {
|
|
|
|
Compile(function);
|
2018-04-04 20:30:34 +00:00
|
|
|
const uint32_t supported_flags = OptimizedCompilationInfo::kInliningEnabled;
|
2016-08-26 08:41:20 +00:00
|
|
|
CHECK_EQ(0u, flags_ & ~supported_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionTester::FunctionTester(Graph* graph, int param_count)
|
|
|
|
: isolate(main_isolate()),
|
2018-07-31 11:12:53 +00:00
|
|
|
canonical(isolate),
|
2016-08-26 08:41:20 +00:00
|
|
|
function(NewFunction(BuildFunction(param_count).c_str())),
|
|
|
|
flags_(0) {
|
|
|
|
CompileGraph(graph);
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionTester::FunctionTester(Handle<Code> code, int param_count)
|
|
|
|
: isolate(main_isolate()),
|
2018-07-31 11:12:53 +00:00
|
|
|
canonical(isolate),
|
2016-08-26 08:41:20 +00:00
|
|
|
function((FLAG_allow_natives_syntax = true,
|
|
|
|
NewFunction(BuildFunction(param_count).c_str()))),
|
|
|
|
flags_(0) {
|
2017-07-18 13:45:08 +00:00
|
|
|
CHECK(!code.is_null());
|
2016-08-26 08:41:20 +00:00
|
|
|
Compile(function);
|
2017-09-28 11:59:42 +00:00
|
|
|
function->set_code(*code);
|
2016-08-26 08:41:20 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 11:48:07 +00:00
|
|
|
FunctionTester::FunctionTester(Handle<Code> code) : FunctionTester(code, 0) {}
|
2016-08-26 08:41:20 +00:00
|
|
|
|
2017-04-07 12:50:15 +00:00
|
|
|
void FunctionTester::CheckThrows(Handle<Object> a) {
|
|
|
|
TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
|
|
|
MaybeHandle<Object> no_result = Call(a);
|
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(no_result.is_null());
|
|
|
|
isolate->OptionalRescheduleException(true);
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:41:20 +00:00
|
|
|
void FunctionTester::CheckThrows(Handle<Object> a, Handle<Object> b) {
|
|
|
|
TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
|
|
|
MaybeHandle<Object> no_result = Call(a, b);
|
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(no_result.is_null());
|
|
|
|
isolate->OptionalRescheduleException(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Local<v8::Message> FunctionTester::CheckThrowsReturnMessage(
|
|
|
|
Handle<Object> a, Handle<Object> b) {
|
|
|
|
TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
|
|
|
MaybeHandle<Object> no_result = Call(a, b);
|
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(no_result.is_null());
|
|
|
|
isolate->OptionalRescheduleException(true);
|
|
|
|
CHECK(!try_catch.Message().IsEmpty());
|
|
|
|
return try_catch.Message();
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
void FunctionTester::CheckCall(Handle<Object> expected, Handle<Object> a,
|
|
|
|
Handle<Object> b, Handle<Object> c,
|
|
|
|
Handle<Object> d) {
|
|
|
|
Handle<Object> result = Call(a, b, c, d).ToHandleChecked();
|
|
|
|
CHECK(expected->SameValue(*result));
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSFunction> FunctionTester::NewFunction(const char* source) {
|
|
|
|
return Handle<JSFunction>::cast(v8::Utils::OpenHandle(
|
|
|
|
*v8::Local<v8::Function>::Cast(CompileRun(source))));
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSObject> FunctionTester::NewObject(const char* source) {
|
|
|
|
return Handle<JSObject>::cast(
|
|
|
|
v8::Utils::OpenHandle(*v8::Local<v8::Object>::Cast(CompileRun(source))));
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:41:20 +00:00
|
|
|
Handle<String> FunctionTester::Val(const char* string) {
|
|
|
|
return isolate->factory()->InternalizeUtf8String(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::Val(double value) {
|
|
|
|
return isolate->factory()->NewNumber(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::infinity() {
|
|
|
|
return isolate->factory()->infinity_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::minus_infinity() { return Val(-V8_INFINITY); }
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::nan() { return isolate->factory()->nan_value(); }
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::undefined() {
|
|
|
|
return isolate->factory()->undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::null() {
|
|
|
|
return isolate->factory()->null_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::true_value() {
|
|
|
|
return isolate->factory()->true_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> FunctionTester::false_value() {
|
|
|
|
return isolate->factory()->false_value();
|
|
|
|
}
|
|
|
|
|
2016-09-01 12:01:33 +00:00
|
|
|
Handle<JSFunction> FunctionTester::ForMachineGraph(Graph* graph,
|
|
|
|
int param_count) {
|
2018-12-08 02:59:17 +00:00
|
|
|
JSFunction p;
|
2016-09-01 12:01:33 +00:00
|
|
|
{ // because of the implicit handle scope of FunctionTester.
|
|
|
|
FunctionTester f(graph, param_count);
|
|
|
|
p = *f.function;
|
|
|
|
}
|
2018-06-23 09:05:50 +00:00
|
|
|
return Handle<JSFunction>(
|
|
|
|
p, p->GetIsolate()); // allocated in outer handle scope.
|
2016-09-01 12:01:33 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:41:20 +00:00
|
|
|
Handle<JSFunction> FunctionTester::Compile(Handle<JSFunction> function) {
|
2019-01-29 08:41:54 +00:00
|
|
|
Handle<SharedFunctionInfo> shared(function->shared(), isolate);
|
|
|
|
IsCompiledScope is_compiled_scope(shared->is_compiled_scope());
|
|
|
|
CHECK(is_compiled_scope.is_compiled() ||
|
|
|
|
Compiler::Compile(function, Compiler::CLEAR_EXCEPTION,
|
|
|
|
&is_compiled_scope));
|
|
|
|
|
2018-06-11 09:53:20 +00:00
|
|
|
Zone zone(isolate->allocator(), ZONE_NAME);
|
2019-01-29 08:41:54 +00:00
|
|
|
OptimizedCompilationInfo info(&zone, isolate, shared, function);
|
|
|
|
|
|
|
|
if (flags_ & OptimizedCompilationInfo::kInliningEnabled) {
|
|
|
|
info.MarkAsInliningEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(info.shared_info()->HasBytecodeArray());
|
|
|
|
JSFunction::EnsureFeedbackVector(function);
|
|
|
|
|
|
|
|
Handle<Code> code =
|
|
|
|
Pipeline::GenerateCodeForTesting(&info, isolate).ToHandleChecked();
|
|
|
|
info.native_context()->AddOptimizedCode(*code);
|
|
|
|
function->set_code(*code);
|
|
|
|
return function;
|
2016-08-26 08:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the given machine graph instead of the source of the function
|
|
|
|
// and replace the JSFunction's code with the result.
|
|
|
|
Handle<JSFunction> FunctionTester::CompileGraph(Graph* graph) {
|
2018-06-23 09:05:50 +00:00
|
|
|
Handle<SharedFunctionInfo> shared(function->shared(), isolate);
|
2018-06-11 09:53:20 +00:00
|
|
|
Zone zone(isolate->allocator(), ZONE_NAME);
|
|
|
|
OptimizedCompilationInfo info(&zone, isolate, shared, function);
|
2016-08-26 08:41:20 +00:00
|
|
|
|
2018-07-03 09:13:55 +00:00
|
|
|
auto call_descriptor = Linkage::ComputeIncoming(&zone, &info);
|
2018-06-19 08:09:09 +00:00
|
|
|
Handle<Code> code =
|
2018-07-06 08:58:43 +00:00
|
|
|
Pipeline::GenerateCodeForTesting(&info, isolate, call_descriptor, graph,
|
|
|
|
AssemblerOptions::Default(isolate))
|
2018-07-03 09:13:55 +00:00
|
|
|
.ToHandleChecked();
|
2017-09-28 11:59:42 +00:00
|
|
|
function->set_code(*code);
|
2016-08-26 08:41:20 +00:00
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|