2014-07-30 13:54:45 +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.
|
|
|
|
|
|
|
|
#ifndef V8_CCTEST_COMPILER_FUNCTION_TESTER_H_
|
|
|
|
#define V8_CCTEST_COMPILER_FUNCTION_TESTER_H_
|
|
|
|
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/ast/ast-numbering.h"
|
|
|
|
#include "src/ast/scopes.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/compiler.h"
|
2014-10-08 09:23:33 +00:00
|
|
|
#include "src/compiler/linkage.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/compiler/pipeline.h"
|
|
|
|
#include "src/execution.h"
|
2015-07-24 10:11:46 +00:00
|
|
|
#include "src/full-codegen/full-codegen.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/handles.h"
|
|
|
|
#include "src/objects-inl.h"
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/parsing/parser.h"
|
|
|
|
#include "src/parsing/rewriter.h"
|
2015-10-28 13:32:17 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
class FunctionTester : public InitializedHandleScope {
|
|
|
|
public:
|
2014-08-28 08:39:24 +00:00
|
|
|
explicit FunctionTester(const char* source, uint32_t flags = 0)
|
2014-07-30 13:54:45 +00:00
|
|
|
: isolate(main_isolate()),
|
2014-08-21 12:40:10 +00:00
|
|
|
function((FLAG_allow_natives_syntax = true, NewFunction(source))),
|
2014-08-28 08:39:24 +00:00
|
|
|
flags_(flags) {
|
2014-07-30 13:54:45 +00:00
|
|
|
Compile(function);
|
2015-09-25 11:33:28 +00:00
|
|
|
const uint32_t supported_flags =
|
|
|
|
CompilationInfo::kFunctionContextSpecializing |
|
|
|
|
CompilationInfo::kInliningEnabled | CompilationInfo::kTypingEnabled;
|
2015-01-30 09:29:25 +00:00
|
|
|
CHECK_EQ(0u, flags_ & ~supported_flags);
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 12:35:12 +00:00
|
|
|
FunctionTester(Graph* graph, int param_count)
|
2014-10-08 09:23:33 +00:00
|
|
|
: isolate(main_isolate()),
|
2015-12-02 12:35:12 +00:00
|
|
|
function(NewFunction(BuildFunction(param_count).c_str())),
|
2014-10-08 09:23:33 +00:00
|
|
|
flags_(0) {
|
|
|
|
CompileGraph(graph);
|
|
|
|
}
|
|
|
|
|
2015-12-02 12:35:12 +00:00
|
|
|
FunctionTester(const CallInterfaceDescriptor& descriptor, Handle<Code> code)
|
|
|
|
: isolate(main_isolate()),
|
|
|
|
function(
|
|
|
|
(FLAG_allow_natives_syntax = true,
|
|
|
|
NewFunction(BuildFunctionFromDescriptor(descriptor).c_str()))),
|
|
|
|
flags_(0) {
|
|
|
|
Compile(function);
|
|
|
|
function->ReplaceCode(*code);
|
|
|
|
}
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
Isolate* isolate;
|
|
|
|
Handle<JSFunction> function;
|
|
|
|
|
2015-12-02 12:35:12 +00:00
|
|
|
MaybeHandle<Object> Call() {
|
|
|
|
return Execution::Call(isolate, function, undefined(), 0, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
MaybeHandle<Object> Call(Handle<Object> a, Handle<Object> b) {
|
|
|
|
Handle<Object> args[] = {a, b};
|
2015-09-17 17:11:38 +00:00
|
|
|
return Execution::Call(isolate, function, undefined(), 2, args);
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 13:19:11 +00:00
|
|
|
MaybeHandle<Object> Call(Handle<Object> a, Handle<Object> b, Handle<Object> c,
|
|
|
|
Handle<Object> d) {
|
|
|
|
Handle<Object> args[] = {a, b, c, d};
|
2015-09-17 17:11:38 +00:00
|
|
|
return Execution::Call(isolate, function, undefined(), 4, args);
|
2015-05-20 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
void CheckThrows(Handle<Object> a, Handle<Object> b) {
|
2015-05-28 12:49:31 +00:00
|
|
|
TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
2014-07-30 13:54:45 +00:00
|
|
|
MaybeHandle<Object> no_result = Call(a, b);
|
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(no_result.is_null());
|
|
|
|
isolate->OptionalRescheduleException(true);
|
|
|
|
}
|
|
|
|
|
2015-09-21 10:34:44 +00:00
|
|
|
v8::Local<v8::Message> CheckThrowsReturnMessage(Handle<Object> a,
|
|
|
|
Handle<Object> b) {
|
2015-05-28 12:49:31 +00:00
|
|
|
TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
|
2014-07-30 13:54:45 +00:00
|
|
|
MaybeHandle<Object> no_result = Call(a, b);
|
|
|
|
CHECK(isolate->has_pending_exception());
|
|
|
|
CHECK(try_catch.HasCaught());
|
|
|
|
CHECK(no_result.is_null());
|
|
|
|
isolate->OptionalRescheduleException(true);
|
2015-03-19 12:40:51 +00:00
|
|
|
CHECK(!try_catch.Message().IsEmpty());
|
2014-07-30 13:54:45 +00:00
|
|
|
return try_catch.Message();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckCall(Handle<Object> expected, Handle<Object> a, Handle<Object> b) {
|
|
|
|
Handle<Object> result = Call(a, b).ToHandleChecked();
|
|
|
|
CHECK(expected->SameValue(*result));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckCall(Handle<Object> expected, Handle<Object> a) {
|
|
|
|
CheckCall(expected, a, undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckCall(Handle<Object> expected) {
|
|
|
|
CheckCall(expected, undefined(), undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckCall(double expected, double a, double b) {
|
|
|
|
CheckCall(Val(expected), Val(a), Val(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckTrue(Handle<Object> a, Handle<Object> b) {
|
|
|
|
CheckCall(true_value(), a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckTrue(Handle<Object> a) { CheckCall(true_value(), a, undefined()); }
|
|
|
|
|
|
|
|
void CheckTrue(double a, double b) {
|
|
|
|
CheckCall(true_value(), Val(a), Val(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckFalse(Handle<Object> a, Handle<Object> b) {
|
|
|
|
CheckCall(false_value(), a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckFalse(Handle<Object> a) {
|
|
|
|
CheckCall(false_value(), a, undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckFalse(double a, double b) {
|
|
|
|
CheckCall(false_value(), Val(a), Val(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSFunction> NewFunction(const char* source) {
|
2015-10-23 12:26:49 +00:00
|
|
|
return Handle<JSFunction>::cast(v8::Utils::OpenHandle(
|
|
|
|
*v8::Local<v8::Function>::Cast(CompileRun(source))));
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<JSObject> NewObject(const char* source) {
|
2015-11-16 16:48:43 +00:00
|
|
|
return Handle<JSObject>::cast(v8::Utils::OpenHandle(
|
|
|
|
*v8::Local<v8::Object>::Cast(CompileRun(source))));
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> Val(const char* string) {
|
|
|
|
return isolate->factory()->InternalizeUtf8String(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> Val(double value) {
|
|
|
|
return isolate->factory()->NewNumber(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> infinity() { return isolate->factory()->infinity_value(); }
|
|
|
|
|
2014-12-20 13:17:20 +00:00
|
|
|
Handle<Object> minus_infinity() { return Val(-V8_INFINITY); }
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
Handle<Object> nan() { return isolate->factory()->nan_value(); }
|
|
|
|
|
|
|
|
Handle<Object> undefined() { return isolate->factory()->undefined_value(); }
|
|
|
|
|
|
|
|
Handle<Object> null() { return isolate->factory()->null_value(); }
|
|
|
|
|
|
|
|
Handle<Object> true_value() { return isolate->factory()->true_value(); }
|
|
|
|
|
|
|
|
Handle<Object> false_value() { return isolate->factory()->false_value(); }
|
2014-08-21 12:40:10 +00:00
|
|
|
|
2014-10-08 09:23:33 +00:00
|
|
|
Handle<JSFunction> Compile(Handle<JSFunction> function) {
|
|
|
|
// TODO(titzer): make this method private.
|
2015-03-24 14:17:05 +00:00
|
|
|
Zone zone;
|
|
|
|
ParseInfo parse_info(&zone, function);
|
|
|
|
CompilationInfo info(&parse_info);
|
2015-05-21 11:33:42 +00:00
|
|
|
info.MarkAsDeoptimizationEnabled();
|
2014-10-08 09:23:33 +00:00
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
CHECK(Parser::ParseStatic(info.parse_info()));
|
2014-10-08 09:23:33 +00:00
|
|
|
info.SetOptimizing(BailoutId::None(), Handle<Code>(function->code()));
|
2015-09-25 11:33:28 +00:00
|
|
|
if (flags_ & CompilationInfo::kFunctionContextSpecializing) {
|
|
|
|
info.MarkAsFunctionContextSpecializing();
|
2014-10-08 09:23:33 +00:00
|
|
|
}
|
|
|
|
if (flags_ & CompilationInfo::kInliningEnabled) {
|
|
|
|
info.MarkAsInliningEnabled();
|
|
|
|
}
|
|
|
|
if (flags_ & CompilationInfo::kTypingEnabled) {
|
|
|
|
info.MarkAsTypingEnabled();
|
|
|
|
}
|
2015-03-09 14:51:13 +00:00
|
|
|
CHECK(Compiler::Analyze(info.parse_info()));
|
2014-10-08 09:23:33 +00:00
|
|
|
CHECK(Compiler::EnsureDeoptimizationSupport(&info));
|
|
|
|
|
|
|
|
Pipeline pipeline(&info);
|
|
|
|
Handle<Code> code = pipeline.GenerateCode();
|
|
|
|
CHECK(!code.is_null());
|
2015-10-26 14:04:00 +00:00
|
|
|
info.dependencies()->Commit(code);
|
2015-05-21 11:33:42 +00:00
|
|
|
info.context()->native_context()->AddOptimizedCode(*code);
|
2014-10-08 09:23:33 +00:00
|
|
|
function->ReplaceCode(*code);
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
2015-12-02 12:35:12 +00:00
|
|
|
static Handle<JSFunction> ForMachineGraph(Graph* graph, int param_count) {
|
2014-10-08 09:23:33 +00:00
|
|
|
JSFunction* p = NULL;
|
|
|
|
{ // because of the implicit handle scope of FunctionTester.
|
2015-12-02 12:35:12 +00:00
|
|
|
FunctionTester f(graph, param_count);
|
2014-10-08 09:23:33 +00:00
|
|
|
p = *f.function;
|
|
|
|
}
|
|
|
|
return Handle<JSFunction>(p); // allocated in outer handle scope.
|
|
|
|
}
|
|
|
|
|
2014-08-21 12:40:10 +00:00
|
|
|
private:
|
2014-08-28 08:39:24 +00:00
|
|
|
uint32_t flags_;
|
2014-10-08 09:23:33 +00:00
|
|
|
|
2015-12-02 12:35:12 +00:00
|
|
|
std::string BuildFunction(int param_count) {
|
|
|
|
std::string function_string = "(function(";
|
|
|
|
if (param_count > 0) {
|
|
|
|
char next = 'a';
|
|
|
|
function_string += next;
|
|
|
|
while (param_count-- > 0) {
|
|
|
|
function_string += ',';
|
|
|
|
function_string += ++next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function_string += "){})";
|
|
|
|
return function_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BuildFunctionFromDescriptor(
|
|
|
|
const CallInterfaceDescriptor& descriptor) {
|
|
|
|
return BuildFunction(descriptor.GetParameterCount());
|
|
|
|
}
|
|
|
|
|
2014-10-08 09:23:33 +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> CompileGraph(Graph* graph) {
|
2015-03-24 14:17:05 +00:00
|
|
|
Zone zone;
|
|
|
|
ParseInfo parse_info(&zone, function);
|
|
|
|
CompilationInfo info(&parse_info);
|
2014-10-08 09:23:33 +00:00
|
|
|
|
2015-03-09 14:51:13 +00:00
|
|
|
CHECK(Parser::ParseStatic(info.parse_info()));
|
2014-10-08 09:23:33 +00:00
|
|
|
info.SetOptimizing(BailoutId::None(),
|
|
|
|
Handle<Code>(function->shared()->code()));
|
2015-03-09 14:51:13 +00:00
|
|
|
CHECK(Compiler::Analyze(info.parse_info()));
|
2014-10-08 09:23:33 +00:00
|
|
|
CHECK(Compiler::EnsureDeoptimizationSupport(&info));
|
|
|
|
|
2014-11-17 14:46:41 +00:00
|
|
|
Handle<Code> code = Pipeline::GenerateCodeForTesting(&info, graph);
|
2014-10-08 09:23:33 +00:00
|
|
|
CHECK(!code.is_null());
|
|
|
|
function->ReplaceCode(*code);
|
|
|
|
return function;
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
};
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
#endif // V8_CCTEST_COMPILER_FUNCTION_TESTER_H_
|