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/globals.h" | 5 #include "platform/globals.h" |
6 | 6 |
7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
8 #include "vm/globals.h" | 8 #include "vm/globals.h" |
9 #include "vm/ast.h" | 9 #include "vm/ast.h" |
10 #include "vm/assembler.h" | 10 #include "vm/assembler.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 // Wrap the SmiReturnCodegen test above as a static function and call it. | 40 // Wrap the SmiReturnCodegen test above as a static function and call it. |
41 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); | 41 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
42 test->node_sequence()->Add( | 42 test->node_sequence()->Add( |
43 new ReturnNode(kPos, new StaticCallNode(kPos, function, no_arguments))); | 43 new ReturnNode(kPos, new StaticCallNode(kPos, function, no_arguments))); |
44 } | 44 } |
45 CODEGEN_TEST2_RUN(SimpleStaticCallCodegen, SmiReturnCodegen, Smi::New(3)) | 45 CODEGEN_TEST2_RUN(SimpleStaticCallCodegen, SmiReturnCodegen, Smi::New(3)) |
46 | 46 |
47 | 47 |
48 // Helper to allocate and return a LocalVariable. | 48 // Helper to allocate and return a LocalVariable. |
49 static LocalVariable* NewTestLocalVariable(const char* name) { | 49 static LocalVariable* NewTestLocalVariable(const char* name) { |
50 const String& variable_name = String::ZoneHandle( | 50 const String& variable_name = |
51 Symbols::New(Thread::Current(), name)); | 51 String::ZoneHandle(Symbols::New(Thread::Current(), name)); |
52 const Type& variable_type = Type::ZoneHandle(Type::DynamicType()); | 52 const Type& variable_type = Type::ZoneHandle(Type::DynamicType()); |
53 return new LocalVariable(kPos, kPos, variable_name, variable_type); | 53 return new LocalVariable(kPos, kPos, variable_name, variable_type); |
54 } | 54 } |
55 | 55 |
56 | 56 |
57 CODEGEN_TEST_GENERATE(ReturnParameterCodegen, test) { | 57 CODEGEN_TEST_GENERATE(ReturnParameterCodegen, test) { |
58 SequenceNode* node_seq = test->node_sequence(); | 58 SequenceNode* node_seq = test->node_sequence(); |
59 const int num_params = 1; | 59 const int num_params = 1; |
60 LocalVariable* parameter = NewTestLocalVariable("parameter"); | 60 LocalVariable* parameter = NewTestLocalVariable("parameter"); |
61 LocalScope* local_scope = node_seq->scope(); | 61 LocalScope* local_scope = node_seq->scope(); |
62 local_scope->InsertParameterAt(0, parameter); | 62 local_scope->InsertParameterAt(0, parameter); |
63 ASSERT(local_scope->num_variables() == num_params); | 63 ASSERT(local_scope->num_variables() == num_params); |
64 const Function& function = test->function(); | 64 const Function& function = test->function(); |
65 function.set_num_fixed_parameters(num_params); | 65 function.set_num_fixed_parameters(num_params); |
66 ASSERT(!function.HasOptionalParameters()); | 66 ASSERT(!function.HasOptionalParameters()); |
67 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, parameter))); | 67 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, parameter))); |
68 } | 68 } |
69 | 69 |
70 | 70 |
71 CODEGEN_TEST2_GENERATE(StaticCallReturnParameterCodegen, function, test) { | 71 CODEGEN_TEST2_GENERATE(StaticCallReturnParameterCodegen, function, test) { |
72 // Wrap and call the ReturnParameterCodegen test above as a static function. | 72 // Wrap and call the ReturnParameterCodegen test above as a static function. |
73 SequenceNode* node_seq = test->node_sequence(); | 73 SequenceNode* node_seq = test->node_sequence(); |
74 ArgumentListNode* arguments = new ArgumentListNode(kPos); | 74 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
75 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); | 75 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
76 node_seq->Add(new ReturnNode(kPos, | 76 node_seq->Add( |
77 new StaticCallNode(kPos, function, arguments))); | 77 new ReturnNode(kPos, new StaticCallNode(kPos, function, arguments))); |
78 } | 78 } |
79 CODEGEN_TEST2_RUN(StaticCallReturnParameterCodegen, | 79 CODEGEN_TEST2_RUN(StaticCallReturnParameterCodegen, |
80 ReturnParameterCodegen, | 80 ReturnParameterCodegen, |
81 Smi::New(3)) | 81 Smi::New(3)) |
82 | 82 |
83 | 83 |
84 CODEGEN_TEST_GENERATE(SmiParamSumCodegen, test) { | 84 CODEGEN_TEST_GENERATE(SmiParamSumCodegen, test) { |
85 SequenceNode* node_seq = test->node_sequence(); | 85 SequenceNode* node_seq = test->node_sequence(); |
86 const int num_params = 2; | 86 const int num_params = 2; |
87 LocalVariable* param1 = NewTestLocalVariable("param1"); | 87 LocalVariable* param1 = NewTestLocalVariable("param1"); |
88 LocalVariable* param2 = NewTestLocalVariable("param2"); | 88 LocalVariable* param2 = NewTestLocalVariable("param2"); |
89 const int num_locals = 1; | 89 const int num_locals = 1; |
90 LocalVariable* sum = NewTestLocalVariable("sum"); | 90 LocalVariable* sum = NewTestLocalVariable("sum"); |
91 LocalScope* local_scope = node_seq->scope(); | 91 LocalScope* local_scope = node_seq->scope(); |
92 local_scope->InsertParameterAt(0, param1); | 92 local_scope->InsertParameterAt(0, param1); |
93 local_scope->InsertParameterAt(1, param2); | 93 local_scope->InsertParameterAt(1, param2); |
94 local_scope->AddVariable(sum); | 94 local_scope->AddVariable(sum); |
95 ASSERT(local_scope->num_variables() == num_params + num_locals); | 95 ASSERT(local_scope->num_variables() == num_params + num_locals); |
96 const Function& function = test->function(); | 96 const Function& function = test->function(); |
97 function.set_num_fixed_parameters(num_params); | 97 function.set_num_fixed_parameters(num_params); |
98 ASSERT(!function.HasOptionalParameters()); | 98 ASSERT(!function.HasOptionalParameters()); |
99 BinaryOpNode* add = new BinaryOpNode(kPos, | 99 BinaryOpNode* add = |
100 Token::kADD, | 100 new BinaryOpNode(kPos, Token::kADD, new LoadLocalNode(kPos, param1), |
101 new LoadLocalNode(kPos, param1), | 101 new LoadLocalNode(kPos, param2)); |
102 new LoadLocalNode(kPos, param2)); | |
103 node_seq->Add(new StoreLocalNode(kPos, sum, add)); | 102 node_seq->Add(new StoreLocalNode(kPos, sum, add)); |
104 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, sum))); | 103 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, sum))); |
105 } | 104 } |
106 | 105 |
107 | 106 |
108 CODEGEN_TEST2_GENERATE(StaticCallSmiParamSumCodegen, function, test) { | 107 CODEGEN_TEST2_GENERATE(StaticCallSmiParamSumCodegen, function, test) { |
109 // Wrap and call the SmiParamSumCodegen test above as a static function. | 108 // Wrap and call the SmiParamSumCodegen test above as a static function. |
110 SequenceNode* node_seq = test->node_sequence(); | 109 SequenceNode* node_seq = test->node_sequence(); |
111 ArgumentListNode* arguments = new ArgumentListNode(kPos); | 110 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
112 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); | 111 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
113 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2)))); | 112 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2)))); |
114 node_seq->Add(new ReturnNode(kPos, | 113 node_seq->Add( |
115 new StaticCallNode(kPos, function, arguments))); | 114 new ReturnNode(kPos, new StaticCallNode(kPos, function, arguments))); |
116 } | 115 } |
117 CODEGEN_TEST2_RUN(StaticCallSmiParamSumCodegen, | 116 CODEGEN_TEST2_RUN(StaticCallSmiParamSumCodegen, SmiParamSumCodegen, Smi::New(5)) |
118 SmiParamSumCodegen, | |
119 Smi::New(5)) | |
120 | 117 |
121 | 118 |
122 CODEGEN_TEST_GENERATE(SmiAddCodegen, test) { | 119 CODEGEN_TEST_GENERATE(SmiAddCodegen, test) { |
123 SequenceNode* node_seq = test->node_sequence(); | 120 SequenceNode* node_seq = test->node_sequence(); |
124 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); | 121 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); |
125 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); | 122 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
126 BinaryOpNode* add_node = new BinaryOpNode(kPos, Token::kADD, a, b); | 123 BinaryOpNode* add_node = new BinaryOpNode(kPos, Token::kADD, a, b); |
127 node_seq->Add(new ReturnNode(kPos, add_node)); | 124 node_seq->Add(new ReturnNode(kPos, add_node)); |
128 } | 125 } |
129 CODEGEN_TEST_RUN(SmiAddCodegen, Smi::New(5)) | 126 CODEGEN_TEST_RUN(SmiAddCodegen, Smi::New(5)) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 211 |
215 static Library& MakeTestLibrary(const char* url) { | 212 static Library& MakeTestLibrary(const char* url) { |
216 Thread* thread = Thread::Current(); | 213 Thread* thread = Thread::Current(); |
217 Zone* zone = thread->zone(); | 214 Zone* zone = thread->zone(); |
218 | 215 |
219 const String& lib_url = String::ZoneHandle(zone, Symbols::New(thread, url)); | 216 const String& lib_url = String::ZoneHandle(zone, Symbols::New(thread, url)); |
220 Library& lib = Library::ZoneHandle(zone, Library::New(lib_url)); | 217 Library& lib = Library::ZoneHandle(zone, Library::New(lib_url)); |
221 lib.Register(thread); | 218 lib.Register(thread); |
222 Library& core_lib = Library::Handle(zone, Library::CoreLibrary()); | 219 Library& core_lib = Library::Handle(zone, Library::CoreLibrary()); |
223 ASSERT(!core_lib.IsNull()); | 220 ASSERT(!core_lib.IsNull()); |
224 const Namespace& core_ns = Namespace::Handle(zone, | 221 const Namespace& core_ns = Namespace::Handle( |
225 Namespace::New(core_lib, Array::Handle(zone), Array::Handle(zone))); | 222 zone, Namespace::New(core_lib, Array::Handle(zone), Array::Handle(zone))); |
226 lib.AddImport(core_ns); | 223 lib.AddImport(core_ns); |
227 return lib; | 224 return lib; |
228 } | 225 } |
229 | 226 |
230 | 227 |
231 static RawClass* LookupClass(const Library& lib, const char* name) { | 228 static RawClass* LookupClass(const Library& lib, const char* name) { |
232 const String& cls_name = String::ZoneHandle(Symbols::New(Thread::Current(), | 229 const String& cls_name = |
233 name)); | 230 String::ZoneHandle(Symbols::New(Thread::Current(), name)); |
234 return lib.LookupClass(cls_name); | 231 return lib.LookupClass(cls_name); |
235 } | 232 } |
236 | 233 |
237 | 234 |
238 CODEGEN_TEST_GENERATE(StaticCallCodegen, test) { | 235 CODEGEN_TEST_GENERATE(StaticCallCodegen, test) { |
239 const char* kScriptChars = | 236 const char* kScriptChars = |
240 "class A {\n" | 237 "class A {\n" |
241 " static bar() { return 42; }\n" | 238 " static bar() { return 42; }\n" |
242 " static fly() { return 5; }\n" | 239 " static fly() { return 5; }\n" |
243 "}\n"; | 240 "}\n"; |
244 | 241 |
245 String& url = String::Handle(String::New("dart-test:CompileScript")); | 242 String& url = String::Handle(String::New("dart-test:CompileScript")); |
246 String& source = String::Handle(String::New(kScriptChars)); | 243 String& source = String::Handle(String::New(kScriptChars)); |
247 Script& script = Script::Handle(Script::New(url, | 244 Script& script = |
248 source, | 245 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); |
249 RawScript::kScriptTag)); | |
250 Library& lib = MakeTestLibrary("TestLib"); | 246 Library& lib = MakeTestLibrary("TestLib"); |
251 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 247 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
252 EXPECT(ClassFinalizer::ProcessPendingClasses()); | 248 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
253 Class& cls = Class::Handle(LookupClass(lib, "A")); | 249 Class& cls = Class::Handle(LookupClass(lib, "A")); |
254 EXPECT(!cls.IsNull()); | 250 EXPECT(!cls.IsNull()); |
255 | 251 |
256 // 'bar' will not be compiled. | 252 // 'bar' will not be compiled. |
257 String& function_bar_name = String::Handle(String::New("bar")); | 253 String& function_bar_name = String::Handle(String::New("bar")); |
258 Function& function_bar = | 254 Function& function_bar = |
259 Function::ZoneHandle(cls.LookupStaticFunction(function_bar_name)); | 255 Function::ZoneHandle(cls.LookupStaticFunction(function_bar_name)); |
(...skipping 24 matching lines...) Expand all Loading... |
284 | 280 |
285 CODEGEN_TEST_GENERATE(InstanceCallCodegen, test) { | 281 CODEGEN_TEST_GENERATE(InstanceCallCodegen, test) { |
286 const char* kScriptChars = | 282 const char* kScriptChars = |
287 "class A {\n" | 283 "class A {\n" |
288 " A() {}\n" | 284 " A() {}\n" |
289 " int bar() { return 42; }\n" | 285 " int bar() { return 42; }\n" |
290 "}\n"; | 286 "}\n"; |
291 | 287 |
292 String& url = String::Handle(String::New("dart-test:CompileScript")); | 288 String& url = String::Handle(String::New("dart-test:CompileScript")); |
293 String& source = String::Handle(String::New(kScriptChars)); | 289 String& source = String::Handle(String::New(kScriptChars)); |
294 Script& script = Script::Handle(Script::New(url, | 290 Script& script = |
295 source, | 291 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); |
296 RawScript::kScriptTag)); | |
297 Library& lib = MakeTestLibrary("TestLib"); | 292 Library& lib = MakeTestLibrary("TestLib"); |
298 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 293 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
299 EXPECT(ClassFinalizer::ProcessPendingClasses()); | 294 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
300 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); | 295 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); |
301 EXPECT(!cls.IsNull()); | 296 EXPECT(!cls.IsNull()); |
302 | 297 |
303 String& constructor_name = String::Handle(String::New("A.")); | 298 String& constructor_name = String::Handle(String::New("A.")); |
304 Function& constructor = | 299 Function& constructor = |
305 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); | 300 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); |
306 EXPECT(!constructor.IsNull()); | 301 EXPECT(!constructor.IsNull()); |
307 | 302 |
308 // The unit test creates an instance of class A and calls function 'bar'. | 303 // The unit test creates an instance of class A and calls function 'bar'. |
309 String& function_bar_name = String::ZoneHandle(Symbols::New(Thread::Current(), | 304 String& function_bar_name = |
310 "bar")); | 305 String::ZoneHandle(Symbols::New(Thread::Current(), "bar")); |
311 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); | 306 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
312 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); | 307 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); |
313 InstanceCallNode* call_bar = new InstanceCallNode( | 308 InstanceCallNode* call_bar = new InstanceCallNode( |
314 kPos, | 309 kPos, new ConstructorCallNode(kPos, no_type_arguments, constructor, |
315 new ConstructorCallNode( | 310 no_arguments), |
316 kPos, no_type_arguments, constructor, no_arguments), | 311 function_bar_name, no_arguments); |
317 function_bar_name, | |
318 no_arguments); | |
319 | 312 |
320 test->node_sequence()->Add(new ReturnNode(kPos, call_bar)); | 313 test->node_sequence()->Add(new ReturnNode(kPos, call_bar)); |
321 } | 314 } |
322 CODEGEN_TEST_RUN(InstanceCallCodegen, Smi::New(42)) | 315 CODEGEN_TEST_RUN(InstanceCallCodegen, Smi::New(42)) |
323 | 316 |
324 | 317 |
325 // Test allocation of dart objects. | 318 // Test allocation of dart objects. |
326 CODEGEN_TEST_GENERATE(AllocateNewObjectCodegen, test) { | 319 CODEGEN_TEST_GENERATE(AllocateNewObjectCodegen, test) { |
327 const char* kScriptChars = | 320 const char* kScriptChars = |
328 "class A {\n" | 321 "class A {\n" |
329 " A() {}\n" | 322 " A() {}\n" |
330 " static bar() { return 42; }\n" | 323 " static bar() { return 42; }\n" |
331 "}\n"; | 324 "}\n"; |
332 | 325 |
333 String& url = String::Handle(String::New("dart-test:CompileScript")); | 326 String& url = String::Handle(String::New("dart-test:CompileScript")); |
334 String& source = String::Handle(String::New(kScriptChars)); | 327 String& source = String::Handle(String::New(kScriptChars)); |
335 Script& script = Script::Handle(Script::New(url, | 328 Script& script = |
336 source, | 329 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); |
337 RawScript::kScriptTag)); | |
338 Library& lib = MakeTestLibrary("TestLib"); | 330 Library& lib = MakeTestLibrary("TestLib"); |
339 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 331 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
340 EXPECT(ClassFinalizer::ProcessPendingClasses()); | 332 EXPECT(ClassFinalizer::ProcessPendingClasses()); |
341 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); | 333 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); |
342 EXPECT(!cls.IsNull()); | 334 EXPECT(!cls.IsNull()); |
343 | 335 |
344 String& constructor_name = String::Handle(String::New("A.")); | 336 String& constructor_name = String::Handle(String::New("A.")); |
345 Function& constructor = | 337 Function& constructor = |
346 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); | 338 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); |
347 EXPECT(!constructor.IsNull()); | 339 EXPECT(!constructor.IsNull()); |
348 | 340 |
349 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); | 341 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); |
350 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); | 342 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
351 test->node_sequence()->Add(new ReturnNode(kPos, new ConstructorCallNode( | 343 test->node_sequence()->Add( |
352 kPos, no_type_arguments, constructor, no_arguments))); | 344 new ReturnNode(kPos, new ConstructorCallNode(kPos, no_type_arguments, |
| 345 constructor, no_arguments))); |
353 } | 346 } |
354 | 347 |
355 | 348 |
356 CODEGEN_TEST_RAW_RUN(AllocateNewObjectCodegen, function) { | 349 CODEGEN_TEST_RAW_RUN(AllocateNewObjectCodegen, function) { |
357 const Object& result = Object::Handle( | 350 const Object& result = Object::Handle( |
358 DartEntry::InvokeFunction(function, Object::empty_array())); | 351 DartEntry::InvokeFunction(function, Object::empty_array())); |
359 EXPECT(!result.IsError()); | 352 EXPECT(!result.IsError()); |
360 const GrowableObjectArray& libs = GrowableObjectArray::Handle( | 353 const GrowableObjectArray& libs = GrowableObjectArray::Handle( |
361 Isolate::Current()->object_store()->libraries()); | 354 Isolate::Current()->object_store()->libraries()); |
362 ASSERT(!libs.IsNull()); | 355 ASSERT(!libs.IsNull()); |
363 // App lib is the last one that was loaded. | 356 // App lib is the last one that was loaded. |
364 intptr_t num_libs = libs.Length(); | 357 intptr_t num_libs = libs.Length(); |
365 Library& app_lib = Library::Handle(); | 358 Library& app_lib = Library::Handle(); |
366 app_lib ^= libs.At(num_libs - 1); | 359 app_lib ^= libs.At(num_libs - 1); |
367 ASSERT(!app_lib.IsNull()); | 360 ASSERT(!app_lib.IsNull()); |
368 const Class& cls = Class::Handle( | 361 const Class& cls = Class::Handle(app_lib.LookupClass( |
369 app_lib.LookupClass(String::Handle(Symbols::New(Thread::Current(), | 362 String::Handle(Symbols::New(Thread::Current(), "A")))); |
370 "A")))); | |
371 EXPECT_EQ(cls.raw(), result.clazz()); | 363 EXPECT_EQ(cls.raw(), result.clazz()); |
372 } | 364 } |
373 | 365 |
374 } // namespace dart | 366 } // namespace dart |
OLD | NEW |