| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/class_finalizer.h" | 6 #include "vm/class_finalizer.h" |
| 7 #include "vm/code_patcher.h" | 7 #include "vm/code_patcher.h" |
| 8 #include "vm/compiler.h" | 8 #include "vm/compiler.h" |
| 9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 String& url = String::Handle(String::New("dart-test:CompileScript")); | 22 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 23 String& source = String::Handle(String::New(kScriptChars)); | 23 String& source = String::Handle(String::New(kScriptChars)); |
| 24 Script& script = Script::Handle(Script::New(url, | 24 Script& script = Script::Handle(Script::New(url, |
| 25 source, | 25 source, |
| 26 RawScript::kScriptTag)); | 26 RawScript::kScriptTag)); |
| 27 Library& lib = Library::Handle(Library::CoreLibrary()); | 27 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 28 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 28 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 | 31 |
| 32 static void CompileFunctionImpl() { | 32 TEST_CASE(CompileFunction) { |
| 33 const char* kScriptChars = | 33 const char* kScriptChars = |
| 34 "class A {\n" | 34 "class A {\n" |
| 35 " static foo() { return 42; }\n" | 35 " static foo() { return 42; }\n" |
| 36 " static moo() {\n" | 36 " static moo() {\n" |
| 37 " // A.foo();\n" | 37 " // A.foo();\n" |
| 38 " }\n" | 38 " }\n" |
| 39 "}\n"; | 39 "}\n"; |
| 40 String& url = String::Handle(String::New("dart-test:CompileFunction")); | 40 String& url = String::Handle(String::New("dart-test:CompileFunction")); |
| 41 String& source = String::Handle(String::New(kScriptChars)); | 41 String& source = String::Handle(String::New(kScriptChars)); |
| 42 Script& script = Script::Handle(Script::New(url, | 42 Script& script = Script::Handle(Script::New(url, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 63 EXPECT(!function_moo.IsNull()); | 63 EXPECT(!function_moo.IsNull()); |
| 64 | 64 |
| 65 EXPECT(CompilerTest::TestCompileFunction(function_moo)); | 65 EXPECT(CompilerTest::TestCompileFunction(function_moo)); |
| 66 EXPECT(function_moo.HasCode()); | 66 EXPECT(function_moo.HasCode()); |
| 67 function_source = function_moo.GetSource(); | 67 function_source = function_moo.GetSource(); |
| 68 EXPECT_STREQ("static moo() {\n // A.foo();\n }", | 68 EXPECT_STREQ("static moo() {\n // A.foo();\n }", |
| 69 function_source.ToCString()); | 69 function_source.ToCString()); |
| 70 } | 70 } |
| 71 | 71 |
| 72 | 72 |
| 73 TEST_CASE(CompileFunction) { | |
| 74 CompileFunctionImpl(); | |
| 75 } | |
| 76 | |
| 77 | |
| 78 // Runs 'CompileFunctionImpl' on a helper thread. | |
| 79 class CompileFunctionTask : public ThreadPool::Task { | 73 class CompileFunctionTask : public ThreadPool::Task { |
| 80 public: | 74 public: |
| 81 CompileFunctionTask(Isolate* isolate, | 75 CompileFunctionTask(Isolate* isolate, |
| 82 Monitor* done_monitor, | 76 const Function& func, |
| 83 bool* done) | 77 Monitor* done_monitor, |
| 78 bool* done) |
| 84 : isolate_(isolate), | 79 : isolate_(isolate), |
| 80 func_(func), |
| 85 done_monitor_(done_monitor), | 81 done_monitor_(done_monitor), |
| 86 done_(done) { | 82 done_(done) { |
| 87 } | 83 } |
| 88 | 84 |
| 89 virtual void Run() { | 85 virtual void Run() { |
| 90 Thread::EnterIsolateAsHelper(isolate_); | 86 Thread::EnterIsolateAsHelper(isolate_); |
| 91 { | 87 { |
| 92 Thread* thread = Thread::Current(); | 88 Thread* thread = Thread::Current(); |
| 93 StackZone stack_zone(thread); | 89 StackZone stack_zone(thread); |
| 94 HANDLESCOPE(thread); | 90 HANDLESCOPE(thread); |
| 95 CompileFunctionImpl(); | 91 EXPECT(func_.HasCode()); |
| 92 EXPECT(!func_.HasOptimizedCode()); |
| 93 const Error& err = |
| 94 Error::Handle(Compiler::CompileOptimizedFunction(thread, func_)); |
| 95 EXPECT(err.IsNull()); |
| 96 EXPECT(func_.HasOptimizedCode()); |
| 96 } | 97 } |
| 97 Thread::ExitIsolateAsHelper(); | 98 Thread::ExitIsolateAsHelper(); |
| 98 // Tell main thread that we are done. | 99 // Tell main thread that we are done. |
| 99 { | 100 { |
| 100 MonitorLocker ml(done_monitor_); | 101 MonitorLocker ml(done_monitor_); |
| 101 ASSERT(!*done_); | 102 ASSERT(!*done_); |
| 102 *done_ = true; | 103 *done_ = true; |
| 103 ml.Notify(); | 104 ml.Notify(); |
| 104 } | 105 } |
| 105 } | 106 } |
| 106 | 107 |
| 107 private: | 108 private: |
| 108 Isolate* isolate_; | 109 Isolate* isolate_; |
| 110 const Function& func_; |
| 109 Monitor* done_monitor_; | 111 Monitor* done_monitor_; |
| 110 bool* done_; | 112 bool* done_; |
| 111 }; | 113 }; |
| 112 | 114 |
| 113 | 115 |
| 114 TEST_CASE(CompileFunctionOnHelperThread) { | 116 TEST_CASE(CompileFunctionOnHelperThread) { |
| 115 Monitor done_monitor; | 117 Monitor done_monitor; |
| 116 bool done = false; | 118 bool done = false; |
| 117 Isolate* isolate = Thread::Current()->isolate(); | 119 Isolate* isolate = Thread::Current()->isolate(); |
| 118 // Flush store buffers, etc. | 120 // Flush store buffers, etc. |
| 119 // TODO(koda): Currently, the GC only does this for the current thread, (i.e, | 121 // TODO(koda): Currently, the GC only does this for the current thread, (i.e, |
| 120 // the helper, in this test), but it should be done for all *threads* | 122 // the helper, in this test), but it should be done for all *threads* |
| 121 // after/at safepointing. | 123 // after/at safepointing. |
| 122 Thread::PrepareForGC(); | 124 Thread::PrepareForGC(); |
| 125 |
| 126 // Create a simple function and compile it without optimization. |
| 127 const char* kScriptChars = |
| 128 "class A {\n" |
| 129 " static foo() { return 42; }\n" |
| 130 "}\n"; |
| 131 String& url = |
| 132 String::Handle(String::New("dart-test:CompileFunctionOnHelperThread")); |
| 133 String& source = String::Handle(String::New(kScriptChars)); |
| 134 Script& script = Script::Handle(Script::New(url, |
| 135 source, |
| 136 RawScript::kScriptTag)); |
| 137 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 138 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 139 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
| 140 Class& cls = Class::Handle( |
| 141 lib.LookupClass(String::Handle(Symbols::New("A")))); |
| 142 EXPECT(!cls.IsNull()); |
| 143 String& function_foo_name = String::Handle(String::New("foo")); |
| 144 Function& func = |
| 145 Function::Handle(cls.LookupStaticFunction(function_foo_name)); |
| 146 EXPECT(!func.HasCode()); |
| 147 CompilerTest::TestCompileFunction(func); |
| 148 EXPECT(func.HasCode()); |
| 149 EXPECT(!func.HasOptimizedCode()); |
| 150 |
| 151 // Now optimize it on a helper thread. |
| 123 Dart::thread_pool()->Run( | 152 Dart::thread_pool()->Run( |
| 124 new CompileFunctionTask(isolate, &done_monitor, &done)); | 153 new CompileFunctionTask(isolate, func, &done_monitor, &done)); |
| 125 { | 154 { |
| 126 // Manually wait. | 155 // Manually wait. |
| 127 // TODO(koda): Replace with execution of Dart and/or VM code when GC | 156 // TODO(koda): Replace with execution of Dart and/or VM code when GC |
| 128 // actually safepoints everything. | 157 // actually safepoints everything. |
| 129 MonitorLocker ml(&done_monitor); | 158 MonitorLocker ml(&done_monitor); |
| 130 while (!done) { | 159 while (!done) { |
| 131 ml.Wait(); | 160 ml.Wait(); |
| 132 } | 161 } |
| 133 } | 162 } |
| 134 } | 163 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 EXPECT(val.IsInteger()); | 271 EXPECT(val.IsInteger()); |
| 243 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 272 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 244 | 273 |
| 245 intptr_t final_class_table_size = | 274 intptr_t final_class_table_size = |
| 246 Isolate::Current()->class_table()->NumCids(); | 275 Isolate::Current()->class_table()->NumCids(); |
| 247 // Eval should not eat into this non-renewable resource. | 276 // Eval should not eat into this non-renewable resource. |
| 248 EXPECT_EQ(initial_class_table_size, final_class_table_size); | 277 EXPECT_EQ(initial_class_table_size, final_class_table_size); |
| 249 } | 278 } |
| 250 | 279 |
| 251 } // namespace dart | 280 } // namespace dart |
| OLD | NEW |