2012-01-16 12:28:10 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
2011-10-05 05:20:07 +00:00
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2017-09-04 14:18:37 +00:00
|
|
|
#include "vm/compiler/jit/compiler.h"
|
2012-01-16 12:28:10 +00:00
|
|
|
#include "platform/assert.h"
|
2012-04-11 18:12:28 +00:00
|
|
|
#include "vm/class_finalizer.h"
|
2014-09-11 17:56:42 +00:00
|
|
|
#include "vm/code_patcher.h"
|
2019-07-08 23:12:26 +00:00
|
|
|
#include "vm/compiler/frontend/bytecode_reader.h"
|
2013-08-19 22:17:09 +00:00
|
|
|
#include "vm/dart_api_impl.h"
|
2018-06-20 00:39:49 +00:00
|
|
|
#include "vm/heap/safepoint.h"
|
2018-06-05 17:40:59 +00:00
|
|
|
#include "vm/kernel_isolate.h"
|
2011-10-05 05:20:07 +00:00
|
|
|
#include "vm/object.h"
|
2012-07-24 00:01:50 +00:00
|
|
|
#include "vm/symbols.h"
|
2015-08-17 20:27:57 +00:00
|
|
|
#include "vm/thread_pool.h"
|
2011-10-05 05:20:07 +00:00
|
|
|
#include "vm/unit_test.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
2017-01-31 23:16:38 +00:00
|
|
|
ISOLATE_UNIT_TEST_CASE(CompileFunction) {
|
2011-10-05 05:20:07 +00:00
|
|
|
const char* kScriptChars =
|
2016-11-08 21:54:47 +00:00
|
|
|
"class A {\n"
|
|
|
|
" static foo() { return 42; }\n"
|
|
|
|
" static moo() {\n"
|
|
|
|
" // A.foo();\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2019-04-03 00:58:43 +00:00
|
|
|
Dart_Handle library;
|
|
|
|
{
|
|
|
|
TransitionVMToNative transition(thread);
|
|
|
|
library = TestCase::LoadTestScript(kScriptChars, NULL);
|
|
|
|
}
|
|
|
|
const Library& lib =
|
|
|
|
Library::Handle(Library::RawCast(Api::UnwrapHandle(library)));
|
2013-11-27 00:11:32 +00:00
|
|
|
EXPECT(ClassFinalizer::ProcessPendingClasses());
|
2016-11-08 21:54:47 +00:00
|
|
|
Class& cls =
|
|
|
|
Class::Handle(lib.LookupClass(String::Handle(Symbols::New(thread, "A"))));
|
2013-09-09 21:52:40 +00:00
|
|
|
EXPECT(!cls.IsNull());
|
2011-10-05 05:20:07 +00:00
|
|
|
String& function_foo_name = String::Handle(String::New("foo"));
|
|
|
|
Function& function_foo =
|
|
|
|
Function::Handle(cls.LookupStaticFunction(function_foo_name));
|
|
|
|
EXPECT(!function_foo.IsNull());
|
2013-11-13 18:09:41 +00:00
|
|
|
String& function_source = String::Handle(function_foo.GetSource());
|
2015-04-14 19:41:38 +00:00
|
|
|
EXPECT_STREQ("static foo() { return 42; }", function_source.ToCString());
|
2011-10-05 05:20:07 +00:00
|
|
|
EXPECT(CompilerTest::TestCompileFunction(function_foo));
|
|
|
|
EXPECT(function_foo.HasCode());
|
|
|
|
|
|
|
|
String& function_moo_name = String::Handle(String::New("moo"));
|
|
|
|
Function& function_moo =
|
|
|
|
Function::Handle(cls.LookupStaticFunction(function_moo_name));
|
|
|
|
EXPECT(!function_moo.IsNull());
|
|
|
|
|
|
|
|
EXPECT(CompilerTest::TestCompileFunction(function_moo));
|
|
|
|
EXPECT(function_moo.HasCode());
|
2013-11-13 18:09:41 +00:00
|
|
|
function_source = function_moo.GetSource();
|
2015-04-14 19:41:38 +00:00
|
|
|
EXPECT_STREQ("static moo() {\n // A.foo();\n }",
|
2013-11-13 18:09:41 +00:00
|
|
|
function_source.ToCString());
|
2011-10-05 05:20:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 00:58:43 +00:00
|
|
|
ISOLATE_UNIT_TEST_CASE(OptimizeCompileFunctionOnHelperThread) {
|
2015-08-19 16:42:56 +00:00
|
|
|
// Create a simple function and compile it without optimization.
|
|
|
|
const char* kScriptChars =
|
2016-11-08 21:54:47 +00:00
|
|
|
"class A {\n"
|
|
|
|
" static foo() { return 42; }\n"
|
|
|
|
"}\n";
|
2019-04-03 00:58:43 +00:00
|
|
|
Dart_Handle library;
|
|
|
|
{
|
|
|
|
TransitionVMToNative transition(thread);
|
|
|
|
library = TestCase::LoadTestScript(kScriptChars, NULL);
|
|
|
|
}
|
|
|
|
const Library& lib =
|
|
|
|
Library::Handle(Library::RawCast(Api::UnwrapHandle(library)));
|
2015-08-19 16:42:56 +00:00
|
|
|
EXPECT(ClassFinalizer::ProcessPendingClasses());
|
2016-11-08 21:54:47 +00:00
|
|
|
Class& cls =
|
|
|
|
Class::Handle(lib.LookupClass(String::Handle(Symbols::New(thread, "A"))));
|
2015-08-19 16:42:56 +00:00
|
|
|
EXPECT(!cls.IsNull());
|
|
|
|
String& function_foo_name = String::Handle(String::New("foo"));
|
|
|
|
Function& func =
|
|
|
|
Function::Handle(cls.LookupStaticFunction(function_foo_name));
|
|
|
|
EXPECT(!func.HasCode());
|
|
|
|
CompilerTest::TestCompileFunction(func);
|
|
|
|
EXPECT(func.HasCode());
|
|
|
|
EXPECT(!func.HasOptimizedCode());
|
2016-02-25 15:53:39 +00:00
|
|
|
#if !defined(PRODUCT)
|
|
|
|
// Constant in product mode.
|
2015-10-28 17:19:35 +00:00
|
|
|
FLAG_background_compilation = true;
|
2016-02-25 15:53:39 +00:00
|
|
|
#endif
|
2015-10-26 22:12:29 +00:00
|
|
|
Isolate* isolate = thread->isolate();
|
2017-11-30 22:59:51 +00:00
|
|
|
BackgroundCompiler::Start(isolate);
|
2019-04-03 00:58:43 +00:00
|
|
|
isolate->optimizing_background_compiler()->Compile(func);
|
2015-10-26 22:12:29 +00:00
|
|
|
Monitor* m = new Monitor();
|
2017-01-11 19:12:40 +00:00
|
|
|
{
|
|
|
|
MonitorLocker ml(m);
|
|
|
|
while (!func.HasOptimizedCode()) {
|
|
|
|
ml.WaitWithSafepointCheck(thread, 1);
|
|
|
|
}
|
2015-08-17 20:27:57 +00:00
|
|
|
}
|
2017-01-11 19:12:40 +00:00
|
|
|
delete m;
|
2016-04-12 22:44:50 +00:00
|
|
|
BackgroundCompiler::Stop(isolate);
|
2015-08-17 20:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 00:58:43 +00:00
|
|
|
ISOLATE_UNIT_TEST_CASE(CompileFunctionOnHelperThread) {
|
|
|
|
// Create a simple function and compile it without optimization.
|
|
|
|
const char* kScriptChars =
|
|
|
|
"class A {\n"
|
|
|
|
" static foo() { return 42; }\n"
|
|
|
|
"}\n";
|
|
|
|
Dart_Handle library;
|
|
|
|
{
|
|
|
|
TransitionVMToNative transition(thread);
|
|
|
|
library = TestCase::LoadTestScript(kScriptChars, NULL);
|
|
|
|
}
|
|
|
|
const Library& lib =
|
|
|
|
Library::Handle(Library::RawCast(Api::UnwrapHandle(library)));
|
|
|
|
EXPECT(ClassFinalizer::ProcessPendingClasses());
|
|
|
|
Class& cls =
|
|
|
|
Class::Handle(lib.LookupClass(String::Handle(Symbols::New(thread, "A"))));
|
|
|
|
EXPECT(!cls.IsNull());
|
|
|
|
String& function_foo_name = String::Handle(String::New("foo"));
|
|
|
|
Function& func =
|
|
|
|
Function::Handle(cls.LookupStaticFunction(function_foo_name));
|
|
|
|
EXPECT(!func.HasCode());
|
|
|
|
if (!FLAG_enable_interpreter) {
|
|
|
|
CompilerTest::TestCompileFunction(func);
|
|
|
|
EXPECT(func.HasCode());
|
|
|
|
return;
|
|
|
|
}
|
2019-07-08 23:12:26 +00:00
|
|
|
// Bytecode loading must happen on the main thread. Ensure the bytecode is
|
|
|
|
// loaded before asking for an unoptimized compile on a background thread.
|
|
|
|
kernel::BytecodeReader::ReadFunctionBytecode(thread, func);
|
2019-04-03 00:58:43 +00:00
|
|
|
#if !defined(PRODUCT)
|
|
|
|
// Constant in product mode.
|
|
|
|
FLAG_background_compilation = true;
|
|
|
|
#endif
|
|
|
|
Isolate* isolate = thread->isolate();
|
|
|
|
BackgroundCompiler::Start(isolate);
|
|
|
|
isolate->background_compiler()->Compile(func);
|
|
|
|
Monitor* m = new Monitor();
|
|
|
|
{
|
|
|
|
MonitorLocker ml(m);
|
|
|
|
while (!func.HasCode()) {
|
|
|
|
ml.WaitWithSafepointCheck(thread, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete m;
|
|
|
|
BackgroundCompiler::Stop(isolate);
|
|
|
|
}
|
|
|
|
|
2018-10-31 19:51:52 +00:00
|
|
|
ISOLATE_UNIT_TEST_CASE(RegenerateAllocStubs) {
|
2014-09-11 17:56:42 +00:00
|
|
|
const char* kScriptChars =
|
2016-11-08 21:54:47 +00:00
|
|
|
"class A {\n"
|
|
|
|
"}\n"
|
|
|
|
"unOpt() => new A(); \n"
|
|
|
|
"optIt() => new A(); \n"
|
|
|
|
"A main() {\n"
|
|
|
|
" return unOpt();\n"
|
|
|
|
"}\n";
|
2014-09-11 17:56:42 +00:00
|
|
|
|
2018-10-31 19:51:52 +00:00
|
|
|
Class& cls = Class::Handle();
|
|
|
|
TransitionVMToNative transition(thread);
|
|
|
|
|
2014-09-11 17:56:42 +00:00
|
|
|
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
|
|
|
|
Dart_Handle result = Dart_Invoke(lib, NewString("main"), 0, NULL);
|
|
|
|
EXPECT_VALID(result);
|
|
|
|
|
2018-10-31 19:51:52 +00:00
|
|
|
{
|
|
|
|
TransitionNativeToVM transition(thread);
|
|
|
|
Library& lib_handle =
|
|
|
|
Library::Handle(Library::RawCast(Api::UnwrapHandle(lib)));
|
|
|
|
cls = lib_handle.LookupClass(String::Handle(Symbols::New(thread, "A")));
|
|
|
|
EXPECT(!cls.IsNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
TransitionNativeToVM transition(thread);
|
|
|
|
cls.DisableAllocationStub();
|
|
|
|
}
|
2014-09-11 17:56:42 +00:00
|
|
|
result = Dart_Invoke(lib, NewString("main"), 0, NULL);
|
|
|
|
EXPECT_VALID(result);
|
2014-09-16 23:14:54 +00:00
|
|
|
|
2018-10-31 19:51:52 +00:00
|
|
|
{
|
|
|
|
TransitionNativeToVM transition(thread);
|
|
|
|
cls.DisableAllocationStub();
|
|
|
|
}
|
2014-09-16 23:14:54 +00:00
|
|
|
result = Dart_Invoke(lib, NewString("main"), 0, NULL);
|
|
|
|
EXPECT_VALID(result);
|
|
|
|
|
2018-10-31 19:51:52 +00:00
|
|
|
{
|
|
|
|
TransitionNativeToVM transition(thread);
|
|
|
|
cls.DisableAllocationStub();
|
|
|
|
}
|
2014-09-16 23:14:54 +00:00
|
|
|
result = Dart_Invoke(lib, NewString("main"), 0, NULL);
|
|
|
|
EXPECT_VALID(result);
|
2014-09-11 17:56:42 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 22:17:09 +00:00
|
|
|
TEST_CASE(EvalExpression) {
|
|
|
|
const char* kScriptChars =
|
|
|
|
"int ten = 2 * 5; \n"
|
|
|
|
"get dot => '.'; \n"
|
|
|
|
"class A { \n"
|
|
|
|
" var apa = 'Herr Nilsson'; \n"
|
|
|
|
" calc(x) => '${x*ten}'; \n"
|
|
|
|
"} \n"
|
|
|
|
"makeObj() => new A(); \n";
|
|
|
|
|
|
|
|
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
|
|
|
|
Dart_Handle obj_handle =
|
2016-11-08 21:54:47 +00:00
|
|
|
Dart_Invoke(lib, Dart_NewStringFromCString("makeObj"), 0, NULL);
|
2019-06-25 00:57:26 +00:00
|
|
|
EXPECT_VALID(obj_handle);
|
2016-02-01 18:57:34 +00:00
|
|
|
TransitionNativeToVM transition(thread);
|
2013-08-19 22:17:09 +00:00
|
|
|
const Object& obj = Object::Handle(Api::UnwrapHandle(obj_handle));
|
|
|
|
EXPECT(!obj.IsNull());
|
|
|
|
EXPECT(obj.IsInstance());
|
|
|
|
|
|
|
|
String& expr_text = String::Handle();
|
|
|
|
expr_text = String::New("apa + ' ${calc(10)}' + dot");
|
|
|
|
Object& val = Object::Handle();
|
2016-04-04 22:22:57 +00:00
|
|
|
const Class& receiver_cls = Class::Handle(obj.clazz());
|
2018-06-05 17:40:59 +00:00
|
|
|
|
|
|
|
if (!KernelIsolate::IsRunning()) {
|
2018-10-25 20:33:04 +00:00
|
|
|
UNREACHABLE();
|
2018-06-05 17:40:59 +00:00
|
|
|
} else {
|
|
|
|
RawLibrary* raw_library = Library::RawCast(Api::UnwrapHandle(lib));
|
|
|
|
Library& lib_handle = Library::ZoneHandle(raw_library);
|
|
|
|
|
2018-10-31 19:51:52 +00:00
|
|
|
Dart_KernelCompilationResult compilation_result =
|
|
|
|
KernelIsolate::CompileExpressionToKernel(
|
|
|
|
expr_text.ToCString(), Array::empty_array(), Array::empty_array(),
|
|
|
|
String::Handle(lib_handle.url()).ToCString(), "A",
|
|
|
|
/* is_static= */ false);
|
2018-06-05 17:40:59 +00:00
|
|
|
EXPECT_EQ(Dart_KernelCompilationStatus_Ok, compilation_result.status);
|
|
|
|
|
|
|
|
const uint8_t* kernel_bytes = compilation_result.kernel;
|
|
|
|
intptr_t kernel_length = compilation_result.kernel_size;
|
|
|
|
|
|
|
|
val = Instance::Cast(obj).EvaluateCompiledExpression(
|
2019-05-31 22:15:51 +00:00
|
|
|
receiver_cls, kernel_bytes, kernel_length, Array::empty_array(),
|
|
|
|
Array::empty_array(), TypeArguments::null_type_arguments());
|
|
|
|
free(const_cast<uint8_t*>(kernel_bytes));
|
2018-06-05 17:40:59 +00:00
|
|
|
}
|
2013-08-19 22:17:09 +00:00
|
|
|
EXPECT(!val.IsNull());
|
|
|
|
EXPECT(!val.IsError());
|
|
|
|
EXPECT(val.IsString());
|
|
|
|
EXPECT_STREQ("Herr Nilsson 100.", val.ToCString());
|
|
|
|
}
|
|
|
|
|
2017-01-31 23:16:38 +00:00
|
|
|
ISOLATE_UNIT_TEST_CASE(EvalExpressionWithLazyCompile) {
|
2018-05-15 12:03:22 +00:00
|
|
|
{ // Initialize an incremental compiler in DFE mode.
|
|
|
|
TransitionVMToNative transition(thread);
|
|
|
|
TestCase::LoadTestScript("", NULL);
|
|
|
|
}
|
2014-07-10 23:23:01 +00:00
|
|
|
Library& lib = Library::Handle(Library::CoreLibrary());
|
2016-11-08 21:54:47 +00:00
|
|
|
const String& expression = String::Handle(
|
|
|
|
String::New("(){ return (){ return (){ return 3 + 4; }(); }(); }()"));
|
2014-07-10 23:23:01 +00:00
|
|
|
Object& val = Object::Handle();
|
2018-06-19 02:41:00 +00:00
|
|
|
val = Api::UnwrapHandle(
|
|
|
|
TestCase::EvaluateExpression(lib, expression,
|
|
|
|
/* param_names= */ Array::empty_array(),
|
|
|
|
/* param_values= */ Array::empty_array()));
|
2014-07-10 23:23:01 +00:00
|
|
|
|
|
|
|
EXPECT(!val.IsNull());
|
|
|
|
EXPECT(!val.IsError());
|
|
|
|
EXPECT(val.IsInteger());
|
|
|
|
EXPECT_EQ(7, Integer::Cast(val).AsInt64Value());
|
|
|
|
}
|
|
|
|
|
2017-01-31 23:16:38 +00:00
|
|
|
ISOLATE_UNIT_TEST_CASE(EvalExpressionExhaustCIDs) {
|
2018-05-15 12:03:22 +00:00
|
|
|
{ // Initialize an incremental compiler in DFE mode.
|
|
|
|
TransitionVMToNative transition(thread);
|
|
|
|
TestCase::LoadTestScript("", NULL);
|
|
|
|
}
|
2014-07-10 23:23:01 +00:00
|
|
|
Library& lib = Library::Handle(Library::CoreLibrary());
|
|
|
|
const String& expression = String::Handle(String::New("3 + 4"));
|
|
|
|
Object& val = Object::Handle();
|
2018-06-19 02:41:00 +00:00
|
|
|
val = Api::UnwrapHandle(
|
|
|
|
TestCase::EvaluateExpression(lib, expression,
|
|
|
|
/* param_names= */ Array::empty_array(),
|
|
|
|
/* param_values= */ Array::empty_array()));
|
2014-07-10 23:23:01 +00:00
|
|
|
|
2015-06-12 23:42:40 +00:00
|
|
|
EXPECT(!val.IsNull());
|
|
|
|
EXPECT(!val.IsError());
|
|
|
|
EXPECT(val.IsInteger());
|
|
|
|
EXPECT_EQ(7, Integer::Cast(val).AsInt64Value());
|
|
|
|
|
|
|
|
intptr_t initial_class_table_size =
|
|
|
|
Isolate::Current()->class_table()->NumCids();
|
2014-07-10 23:23:01 +00:00
|
|
|
|
2018-06-19 02:41:00 +00:00
|
|
|
val = Api::UnwrapHandle(
|
|
|
|
TestCase::EvaluateExpression(lib, expression,
|
|
|
|
/* param_names= */ Array::empty_array(),
|
|
|
|
/* param_values= */ Array::empty_array()));
|
2014-07-10 23:23:01 +00:00
|
|
|
EXPECT(!val.IsNull());
|
|
|
|
EXPECT(!val.IsError());
|
|
|
|
EXPECT(val.IsInteger());
|
|
|
|
EXPECT_EQ(7, Integer::Cast(val).AsInt64Value());
|
2015-06-12 23:42:40 +00:00
|
|
|
|
|
|
|
intptr_t final_class_table_size =
|
|
|
|
Isolate::Current()->class_table()->NumCids();
|
|
|
|
// Eval should not eat into this non-renewable resource.
|
|
|
|
EXPECT_EQ(initial_class_table_size, final_class_table_size);
|
2014-07-10 23:23:01 +00:00
|
|
|
}
|
|
|
|
|
2011-10-05 05:20:07 +00:00
|
|
|
} // namespace dart
|