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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 class CompileFunctionTask : public ThreadPool::Task { | |
74 public: | |
75 CompileFunctionTask(Isolate* isolate, | |
76 const Function& func, | |
77 Monitor* done_monitor, | |
78 bool* done) | |
79 : isolate_(isolate), | |
80 func_(func), | |
81 done_monitor_(done_monitor), | |
82 done_(done) { | |
83 } | |
84 | |
85 virtual void Run() { | |
86 Thread::EnterIsolateAsHelper(isolate_); | |
87 { | |
88 Thread* thread = Thread::Current(); | |
89 StackZone stack_zone(thread); | |
90 HANDLESCOPE(thread); | |
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()); | |
97 } | |
98 Thread::ExitIsolateAsHelper(); | |
99 // Tell main thread that we are done. | |
100 { | |
101 MonitorLocker ml(done_monitor_); | |
102 ASSERT(!*done_); | |
103 *done_ = true; | |
104 ml.Notify(); | |
105 } | |
106 } | |
107 | |
108 private: | |
109 Isolate* isolate_; | |
110 const Function& func_; | |
111 Monitor* done_monitor_; | |
112 bool* done_; | |
113 }; | |
114 | |
115 | |
116 TEST_CASE(CompileFunctionOnHelperThread) { | 73 TEST_CASE(CompileFunctionOnHelperThread) { |
117 Monitor done_monitor; | |
118 bool done = false; | |
119 Isolate* isolate = Thread::Current()->isolate(); | |
120 // Flush store buffers, etc. | |
121 // TODO(koda): Currently, the GC only does this for the current thread, (i.e, | |
122 // the helper, in this test), but it should be done for all *threads* | |
123 // after/at safepointing. | |
124 Thread::PrepareForGC(); | |
125 | |
126 // Create a simple function and compile it without optimization. | 74 // Create a simple function and compile it without optimization. |
127 const char* kScriptChars = | 75 const char* kScriptChars = |
128 "class A {\n" | 76 "class A {\n" |
129 " static foo() { return 42; }\n" | 77 " static foo() { return 42; }\n" |
130 "}\n"; | 78 "}\n"; |
131 String& url = | 79 String& url = |
132 String::Handle(String::New("dart-test:CompileFunctionOnHelperThread")); | 80 String::Handle(String::New("dart-test:CompileFunctionOnHelperThread")); |
133 String& source = String::Handle(String::New(kScriptChars)); | 81 String& source = String::Handle(String::New(kScriptChars)); |
134 Script& script = Script::Handle(Script::New(url, | 82 Script& script = Script::Handle(Script::New(url, |
135 source, | 83 source, |
136 RawScript::kScriptTag)); | 84 RawScript::kScriptTag)); |
137 Library& lib = Library::Handle(Library::CoreLibrary()); | 85 Library& lib = Library::Handle(Library::CoreLibrary()); |
138 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 86 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
139 EXPECT(ClassFinalizer::ProcessPendingClasses()); | 87 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
140 Class& cls = Class::Handle( | 88 Class& cls = Class::Handle( |
141 lib.LookupClass(String::Handle(Symbols::New("A")))); | 89 lib.LookupClass(String::Handle(Symbols::New("A")))); |
142 EXPECT(!cls.IsNull()); | 90 EXPECT(!cls.IsNull()); |
143 String& function_foo_name = String::Handle(String::New("foo")); | 91 String& function_foo_name = String::Handle(String::New("foo")); |
144 Function& func = | 92 Function& func = |
145 Function::Handle(cls.LookupStaticFunction(function_foo_name)); | 93 Function::Handle(cls.LookupStaticFunction(function_foo_name)); |
146 EXPECT(!func.HasCode()); | 94 EXPECT(!func.HasCode()); |
147 CompilerTest::TestCompileFunction(func); | 95 CompilerTest::TestCompileFunction(func); |
148 EXPECT(func.HasCode()); | 96 EXPECT(func.HasCode()); |
149 EXPECT(!func.HasOptimizedCode()); | 97 EXPECT(!func.HasOptimizedCode()); |
150 | 98 BackgroundCompiler::EnsureInit(thread); |
151 // Now optimize it on a helper thread. | 99 Isolate* isolate = thread->isolate(); |
152 Dart::thread_pool()->Run( | 100 ASSERT(isolate->background_compiler() != NULL); |
153 new CompileFunctionTask(isolate, func, &done_monitor, &done)); | 101 isolate->background_compiler()->CompileOptimized(func); |
154 { | 102 Monitor* m = new Monitor(); |
155 // Manually wait. | 103 MonitorLocker ml(m); |
156 // TODO(koda): Replace with execution of Dart and/or VM code when GC | 104 while (!func.HasOptimizedCode()) { |
157 // actually safepoints everything. | 105 isolate->background_compiler()->InstallGeneratedCode(); |
158 MonitorLocker ml(&done_monitor); | 106 ml.Wait(1); |
159 while (!done) { | |
160 ml.Wait(); | |
161 } | |
162 } | 107 } |
| 108 BackgroundCompiler::Stop(isolate->background_compiler()); |
163 } | 109 } |
164 | 110 |
165 | 111 |
166 TEST_CASE(RegenerateAllocStubs) { | 112 TEST_CASE(RegenerateAllocStubs) { |
167 const char* kScriptChars = | 113 const char* kScriptChars = |
168 "class A {\n" | 114 "class A {\n" |
169 "}\n" | 115 "}\n" |
170 "unOpt() => new A(); \n" | 116 "unOpt() => new A(); \n" |
171 "optIt() => new A(); \n" | 117 "optIt() => new A(); \n" |
172 "A main() {\n" | 118 "A main() {\n" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 EXPECT(val.IsInteger()); | 217 EXPECT(val.IsInteger()); |
272 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 218 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
273 | 219 |
274 intptr_t final_class_table_size = | 220 intptr_t final_class_table_size = |
275 Isolate::Current()->class_table()->NumCids(); | 221 Isolate::Current()->class_table()->NumCids(); |
276 // Eval should not eat into this non-renewable resource. | 222 // Eval should not eat into this non-renewable resource. |
277 EXPECT_EQ(initial_class_table_size, final_class_table_size); | 223 EXPECT_EQ(initial_class_table_size, final_class_table_size); |
278 } | 224 } |
279 | 225 |
280 } // namespace dart | 226 } // namespace dart |
OLD | NEW |