OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 #include "vm/globals.h" |
| 6 #if defined(TARGET_ARCH_X64) |
| 7 |
| 8 #include "vm/code_generator.h" |
| 9 |
| 10 #include "vm/ast.h" |
| 11 #include "vm/assembler.h" |
| 12 #include "vm/assert.h" |
| 13 #include "vm/class_finalizer.h" |
| 14 #include "vm/compiler.h" |
| 15 #include "vm/dart_entry.h" |
| 16 #include "vm/native_entry.h" |
| 17 #include "vm/native_entry_test.h" |
| 18 #include "vm/unit_test.h" |
| 19 #include "vm/virtual_memory.h" |
| 20 |
| 21 namespace dart { |
| 22 |
| 23 #define __ assembler_-> |
| 24 |
| 25 |
| 26 static const intptr_t kPos = 1; // Dummy token index in non-existing source. |
| 27 |
| 28 |
| 29 // Helper to allocate and return a LocalVariable. |
| 30 static LocalVariable* NewTestLocalVariable(const char* name) { |
| 31 const String& variable_name = String::ZoneHandle(String::New(name)); |
| 32 const Type& variable_type = Type::ZoneHandle(Type::DynamicType()); |
| 33 return new LocalVariable(kPos, variable_name, variable_type); |
| 34 } |
| 35 |
| 36 |
| 37 CODEGEN_TEST_GENERATE(SimpleReturnCodegen, test) { |
| 38 test->node_sequence()->Add(new ReturnNode(kPos)); |
| 39 } |
| 40 CODEGEN_TEST_RUN(SimpleReturnCodegen, Instance::null()) |
| 41 |
| 42 |
| 43 CODEGEN_TEST_GENERATE(SmiReturnCodegen, test) { |
| 44 LiteralNode* l = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); |
| 45 test->node_sequence()->Add(new ReturnNode(kPos, l)); |
| 46 } |
| 47 CODEGEN_TEST_RUN(SmiReturnCodegen, Smi::New(3)) |
| 48 |
| 49 |
| 50 CODEGEN_TEST2_GENERATE(SimpleStaticCallCodegen, function, test) { |
| 51 // Wrap the SmiReturnCodegen test above as a static function and call it. |
| 52 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
| 53 test->node_sequence()->Add( |
| 54 new ReturnNode(kPos, new StaticCallNode(kPos, function, no_arguments))); |
| 55 } |
| 56 CODEGEN_TEST2_RUN(SimpleStaticCallCodegen, SmiReturnCodegen, Smi::New(3)) |
| 57 |
| 58 |
| 59 CODEGEN_TEST_GENERATE(ReturnParameterCodegen, test) { |
| 60 SequenceNode* node_seq = test->node_sequence(); |
| 61 const int num_params = 1; |
| 62 LocalVariable* parameter = NewTestLocalVariable("parameter"); |
| 63 LocalScope* local_scope = node_seq->scope(); |
| 64 local_scope->AddVariable(parameter); |
| 65 ASSERT(local_scope->num_variables() == num_params); |
| 66 const Function& function = test->function(); |
| 67 function.set_num_fixed_parameters(num_params); |
| 68 ASSERT(function.num_optional_parameters() == 0); |
| 69 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, *parameter))); |
| 70 } |
| 71 |
| 72 |
| 73 CODEGEN_TEST2_GENERATE(StaticCallReturnParameterCodegen, function, test) { |
| 74 // Wrap and call the ReturnParameterCodegen test above as a static function. |
| 75 SequenceNode* node_seq = test->node_sequence(); |
| 76 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 77 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
| 78 node_seq->Add(new ReturnNode(kPos, |
| 79 new StaticCallNode(kPos, function, arguments))); |
| 80 } |
| 81 CODEGEN_TEST2_RUN(StaticCallReturnParameterCodegen, |
| 82 ReturnParameterCodegen, |
| 83 Smi::New(3)) |
| 84 |
| 85 |
| 86 CODEGEN_TEST_GENERATE(SmiParamSumCodegen, test) { |
| 87 SequenceNode* node_seq = test->node_sequence(); |
| 88 const int num_params = 2; |
| 89 LocalVariable* param1 = NewTestLocalVariable("param1"); |
| 90 LocalVariable* param2 = NewTestLocalVariable("param2"); |
| 91 const int num_locals = 1; |
| 92 LocalVariable* sum = NewTestLocalVariable("sum"); |
| 93 LocalScope* local_scope = node_seq->scope(); |
| 94 local_scope->AddVariable(param1); |
| 95 local_scope->AddVariable(param2); |
| 96 local_scope->AddVariable(sum); |
| 97 ASSERT(local_scope->num_variables() == num_params + num_locals); |
| 98 const Function& function = test->function(); |
| 99 function.set_num_fixed_parameters(num_params); |
| 100 ASSERT(function.num_optional_parameters() == 0); |
| 101 BinaryOpNode* add = new BinaryOpNode(kPos, |
| 102 Token::kADD, |
| 103 new LoadLocalNode(kPos, *param1), |
| 104 new LoadLocalNode(kPos, *param2)); |
| 105 node_seq->Add(new StoreLocalNode(kPos, *sum, add)); |
| 106 node_seq->Add(new ReturnNode(kPos, new LoadLocalNode(kPos, *sum))); |
| 107 } |
| 108 |
| 109 |
| 110 CODEGEN_TEST2_GENERATE(StaticCallSmiParamSumCodegen, function, test) { |
| 111 // Wrap and call the SmiParamSumCodegen test above as a static function. |
| 112 SequenceNode* node_seq = test->node_sequence(); |
| 113 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 114 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
| 115 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2)))); |
| 116 node_seq->Add(new ReturnNode(kPos, |
| 117 new StaticCallNode(kPos, function, arguments))); |
| 118 } |
| 119 CODEGEN_TEST2_RUN(StaticCallSmiParamSumCodegen, |
| 120 SmiParamSumCodegen, |
| 121 Smi::New(5)) |
| 122 |
| 123 |
| 124 CODEGEN_TEST_GENERATE(SmiAddCodegen, test) { |
| 125 SequenceNode* node_seq = test->node_sequence(); |
| 126 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); |
| 127 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 128 BinaryOpNode* add_node = new BinaryOpNode(kPos, Token::kADD, a, b); |
| 129 node_seq->Add(new ReturnNode(kPos, add_node)); |
| 130 } |
| 131 CODEGEN_TEST_RUN(SmiAddCodegen, Smi::New(5)) |
| 132 |
| 133 |
| 134 CODEGEN_TEST_GENERATE(GenericAddCodegen, test) { |
| 135 SequenceNode* node_seq = test->node_sequence(); |
| 136 LiteralNode* a = new LiteralNode(kPos, Double::ZoneHandle(Double::New(12.2))); |
| 137 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 138 BinaryOpNode* add_node_1 = new BinaryOpNode(kPos, Token::kADD, a, b); |
| 139 LiteralNode* c = new LiteralNode(kPos, Double::ZoneHandle(Double::New(0.8))); |
| 140 BinaryOpNode* add_node_2 = new BinaryOpNode(kPos, Token::kADD, add_node_1, c); |
| 141 node_seq->Add(new ReturnNode(kPos, add_node_2)); |
| 142 } |
| 143 CODEGEN_TEST_RUN(GenericAddCodegen, Double::New(15.0)) |
| 144 |
| 145 |
| 146 CODEGEN_TEST_GENERATE(SmiBinaryOpCodegen, test) { |
| 147 SequenceNode* node_seq = test->node_sequence(); |
| 148 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(4))); |
| 149 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 150 LiteralNode* c = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3))); |
| 151 BinaryOpNode* sub_node = |
| 152 new BinaryOpNode(kPos, Token::kSUB, a, b); // 4 - 2 -> 2. |
| 153 BinaryOpNode* mul_node = |
| 154 new BinaryOpNode(kPos, Token::kMUL, sub_node, c); // 2 * 3 -> 6. |
| 155 BinaryOpNode* div_node = |
| 156 new BinaryOpNode(kPos, Token::kTRUNCDIV, mul_node, b); // 6 ~/ 2 -> 3. |
| 157 node_seq->Add(new ReturnNode(kPos, div_node)); |
| 158 } |
| 159 CODEGEN_TEST_RUN(SmiBinaryOpCodegen, Smi::New(3)) |
| 160 |
| 161 |
| 162 CODEGEN_TEST_GENERATE(BoolNotCodegen, test) { |
| 163 SequenceNode* node_seq = test->node_sequence(); |
| 164 const Bool& bool_false = Bool::ZoneHandle(Bool::False()); |
| 165 LiteralNode* b = new LiteralNode(kPos, bool_false); |
| 166 UnaryOpNode* not_node = new UnaryOpNode(kPos, Token::kNOT, b); |
| 167 node_seq->Add(new ReturnNode(kPos, not_node)); |
| 168 } |
| 169 CODEGEN_TEST_RUN(BoolNotCodegen, Bool::True()) |
| 170 |
| 171 |
| 172 CODEGEN_TEST_GENERATE(BoolAndCodegen, test) { |
| 173 SequenceNode* node_seq = test->node_sequence(); |
| 174 const Bool& bool_true = Bool::ZoneHandle(Bool::True()); |
| 175 const Bool& bool_false = Bool::ZoneHandle(Bool::False()); |
| 176 LiteralNode* a = new LiteralNode(kPos, bool_true); |
| 177 LiteralNode* b = new LiteralNode(kPos, bool_false); |
| 178 BinaryOpNode* and_node = new BinaryOpNode(kPos, Token::kAND, a, b); |
| 179 node_seq->Add(new ReturnNode(kPos, and_node)); |
| 180 } |
| 181 CODEGEN_TEST_RUN(BoolAndCodegen, Bool::False()) |
| 182 |
| 183 |
| 184 CODEGEN_TEST_GENERATE(BinaryOpCodegen, test) { |
| 185 SequenceNode* node_seq = test->node_sequence(); |
| 186 LiteralNode* a = new LiteralNode(kPos, Double::ZoneHandle(Double::New(12))); |
| 187 LiteralNode* b = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2))); |
| 188 LiteralNode* c = new LiteralNode(kPos, Double::ZoneHandle(Double::New(0.5))); |
| 189 BinaryOpNode* sub_node = new BinaryOpNode(kPos, Token::kSUB, a, b); |
| 190 BinaryOpNode* mul_node = new BinaryOpNode(kPos, Token::kMUL, sub_node, c); |
| 191 BinaryOpNode* div_node = new BinaryOpNode(kPos, Token::kDIV, mul_node, b); |
| 192 node_seq->Add(new ReturnNode(kPos, div_node)); |
| 193 } |
| 194 CODEGEN_TEST_RUN(BinaryOpCodegen, Double::New(2.5)); |
| 195 |
| 196 |
| 197 // Tested Dart code: |
| 198 // int dec(int a, int b = 1) native: "TestSmiSub"; |
| 199 // The native entry TestSmiSub implements dec natively. |
| 200 CODEGEN_TEST_GENERATE(NativeDecCodegen, test) { |
| 201 // A NativeBodyNode, preceded by an EnterNode and followed by a ReturnNode, |
| 202 // implements the body of a native Dart function. Let's take this native |
| 203 // function as an example: int dec(int a, int b = 1) native; |
| 204 // Since this function has an optional parameter, its prologue will copy |
| 205 // incoming parameters to locals. |
| 206 SequenceNode* node_seq = test->node_sequence(); |
| 207 const int num_fixed_params = 1; |
| 208 const int num_opt_params = 1; |
| 209 const int num_params = num_fixed_params + num_opt_params; |
| 210 LocalScope* local_scope = node_seq->scope(); |
| 211 local_scope->AddVariable(NewTestLocalVariable("a")); |
| 212 local_scope->AddVariable(NewTestLocalVariable("b")); |
| 213 ASSERT(local_scope->num_variables() == num_params); |
| 214 const Array& default_values = Array::ZoneHandle(Array::New(num_opt_params)); |
| 215 default_values.SetAt(0, Smi::ZoneHandle(Smi::New(1))); // b = 1. |
| 216 test->set_default_parameter_values(default_values); |
| 217 const Function& function = test->function(); |
| 218 function.set_num_fixed_parameters(num_fixed_params); |
| 219 function.set_num_optional_parameters(num_opt_params); |
| 220 const bool has_opt_params = true; |
| 221 const String& native_name = |
| 222 String::ZoneHandle(String::NewSymbol("TestSmiSub")); |
| 223 NativeFunction native_function = |
| 224 reinterpret_cast<NativeFunction>(NATIVE_ENTRY_FUNCTION(TestSmiSub)); |
| 225 node_seq->Add(new ReturnNode(kPos, |
| 226 new NativeBodyNode(kPos, |
| 227 native_name, |
| 228 native_function, |
| 229 num_params, |
| 230 has_opt_params))); |
| 231 } |
| 232 |
| 233 |
| 234 // Tested Dart code: |
| 235 // return dec(5); |
| 236 CODEGEN_TEST2_GENERATE(StaticDecCallCodegen, function, test) { |
| 237 SequenceNode* node_seq = test->node_sequence(); |
| 238 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 239 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(5)))); |
| 240 node_seq->Add(new ReturnNode(kPos, |
| 241 new StaticCallNode(kPos, function, arguments))); |
| 242 } |
| 243 CODEGEN_TEST2_RUN(StaticDecCallCodegen, NativeDecCodegen, Smi::New(4)) |
| 244 |
| 245 |
| 246 CODEGEN_TEST_GENERATE(SmiUnaryOpCodegen, test) { |
| 247 SequenceNode* node_seq = test->node_sequence(); |
| 248 LiteralNode* a = new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(12))); |
| 249 UnaryOpNode* neg_node = new UnaryOpNode(kPos, Token::kSUB, a); |
| 250 node_seq->Add(new ReturnNode(kPos, neg_node)); |
| 251 } |
| 252 CODEGEN_TEST_RUN(SmiUnaryOpCodegen, Smi::New(-12)) |
| 253 |
| 254 |
| 255 CODEGEN_TEST_GENERATE(DoubleUnaryOpCodegen, test) { |
| 256 SequenceNode* node_seq = test->node_sequence(); |
| 257 LiteralNode* a = new LiteralNode(kPos, Double::ZoneHandle(Double::New(12.0))); |
| 258 UnaryOpNode* neg_node = new UnaryOpNode(kPos, Token::kSUB, a); |
| 259 node_seq->Add(new ReturnNode(kPos, neg_node)); |
| 260 } |
| 261 CODEGEN_TEST_RUN(DoubleUnaryOpCodegen, Double::New(-12.0)) |
| 262 |
| 263 |
| 264 static Library& MakeTestLibrary(const char* url) { |
| 265 const String& lib_url = String::ZoneHandle(String::NewSymbol(url)); |
| 266 Library& lib = Library::ZoneHandle(Library::New(lib_url)); |
| 267 lib.Register(); |
| 268 return lib; |
| 269 } |
| 270 |
| 271 |
| 272 static RawClass* LookupClass(const Library& lib, const char* name) { |
| 273 const String& cls_name = String::ZoneHandle(String::NewSymbol(name)); |
| 274 return lib.LookupClass(cls_name); |
| 275 } |
| 276 |
| 277 |
| 278 CODEGEN_TEST_GENERATE(StaticCallCodegen, test) { |
| 279 const char* kScriptChars = |
| 280 "class A {\n" |
| 281 " static bar() { return 42; }\n" |
| 282 " static fly() { return 5; }\n" |
| 283 "}\n"; |
| 284 |
| 285 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 286 String& source = String::Handle(String::New(kScriptChars)); |
| 287 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); |
| 288 Library& lib = MakeTestLibrary("TestLib"); |
| 289 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 290 Class& cls = Class::Handle(LookupClass(lib, "A")); |
| 291 EXPECT(!cls.IsNull()); |
| 292 |
| 293 // 'bar' will not be compiled. |
| 294 String& function_bar_name = String::Handle(String::New("bar")); |
| 295 Function& function_bar = |
| 296 Function::ZoneHandle(cls.LookupStaticFunction(function_bar_name)); |
| 297 EXPECT(!function_bar.IsNull()); |
| 298 EXPECT(!function_bar.HasCode()); |
| 299 |
| 300 // 'fly' will be compiled. |
| 301 String& function_fly_name = String::Handle(String::New("fly")); |
| 302 Function& function_fly = |
| 303 Function::ZoneHandle(cls.LookupStaticFunction(function_fly_name)); |
| 304 EXPECT(!function_fly.IsNull()); |
| 305 EXPECT(CompilerTest::TestCompileFunction(function_fly)); |
| 306 EXPECT(function_fly.HasCode()); |
| 307 |
| 308 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
| 309 StaticCallNode* call_bar = |
| 310 new StaticCallNode(kPos, function_bar, no_arguments); |
| 311 StaticCallNode* call_fly = |
| 312 new StaticCallNode(kPos, function_fly, no_arguments); |
| 313 |
| 314 BinaryOpNode* add_node = |
| 315 new BinaryOpNode(kPos, Token::kADD, call_bar, call_fly); |
| 316 |
| 317 test->node_sequence()->Add(new ReturnNode(kPos, add_node)); |
| 318 } |
| 319 CODEGEN_TEST_RUN(StaticCallCodegen, Smi::New(42 + 5)) |
| 320 |
| 321 |
| 322 CODEGEN_TEST_GENERATE(InstanceCallCodegen, test) { |
| 323 const char* kScriptChars = |
| 324 "class A {\n" |
| 325 " A() {}\n" |
| 326 " int bar() { return 42; }\n" |
| 327 "}\n"; |
| 328 |
| 329 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 330 String& source = String::Handle(String::New(kScriptChars)); |
| 331 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); |
| 332 Library& lib = MakeTestLibrary("TestLib"); |
| 333 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 334 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 335 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); |
| 336 EXPECT(!cls.IsNull()); |
| 337 |
| 338 String& constructor_name = String::Handle(String::New("A.")); |
| 339 Function& constructor = |
| 340 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); |
| 341 EXPECT(!constructor.IsNull()); |
| 342 |
| 343 // The unit test creates an instance of class A and calls function 'bar'. |
| 344 String& function_bar_name = String::ZoneHandle(String::NewSymbol("bar")); |
| 345 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
| 346 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); |
| 347 InstanceCallNode* call_bar = new InstanceCallNode( |
| 348 kPos, |
| 349 new ConstructorCallNode( |
| 350 kPos, no_type_arguments, constructor, no_arguments), |
| 351 function_bar_name, |
| 352 no_arguments); |
| 353 |
| 354 test->node_sequence()->Add(new ReturnNode(kPos, call_bar)); |
| 355 } |
| 356 CODEGEN_TEST_RUN(InstanceCallCodegen, Smi::New(42)) |
| 357 |
| 358 |
| 359 // Tested Dart code: |
| 360 // int sum(int a, int b, |
| 361 // [int c = 10, int d = 21, int e = -32]) native: "TestSmiSum"; |
| 362 // The native entry TestSmiSum implements sum natively. |
| 363 CODEGEN_TEST_GENERATE(NativeSumCodegen, test) { |
| 364 SequenceNode* node_seq = test->node_sequence(); |
| 365 const int num_fixed_params = 2; |
| 366 const int num_opt_params = 3; |
| 367 const int num_params = num_fixed_params + num_opt_params; |
| 368 LocalScope* local_scope = node_seq->scope(); |
| 369 local_scope->AddVariable(NewTestLocalVariable("a")); |
| 370 local_scope->AddVariable(NewTestLocalVariable("b")); |
| 371 local_scope->AddVariable(NewTestLocalVariable("c")); |
| 372 local_scope->AddVariable(NewTestLocalVariable("d")); |
| 373 local_scope->AddVariable(NewTestLocalVariable("e")); |
| 374 ASSERT(local_scope->num_variables() == num_params); |
| 375 const Array& default_values = Array::ZoneHandle(Array::New(num_opt_params)); |
| 376 default_values.SetAt(0, Smi::ZoneHandle(Smi::New(10))); |
| 377 default_values.SetAt(1, Smi::ZoneHandle(Smi::New(21))); |
| 378 default_values.SetAt(2, Smi::ZoneHandle(Smi::New(-32))); |
| 379 test->set_default_parameter_values(default_values); |
| 380 const Function& function = test->function(); |
| 381 function.set_num_fixed_parameters(num_fixed_params); |
| 382 function.set_num_optional_parameters(num_opt_params); |
| 383 function.set_parameter_types(Array::Handle(Array::New(num_params))); |
| 384 function.set_parameter_names(Array::Handle(Array::New(num_params))); |
| 385 const Type& param_type = Type::Handle(Type::DynamicType()); |
| 386 for (int i = 0; i < num_params - 1; i++) { |
| 387 function.SetParameterTypeAt(i, param_type); |
| 388 } |
| 389 const bool has_opt_params = true; |
| 390 const String& native_name = |
| 391 String::ZoneHandle(String::NewSymbol("TestSmiSum")); |
| 392 NativeFunction native_function = |
| 393 reinterpret_cast<NativeFunction>(NATIVE_ENTRY_FUNCTION(TestSmiSum)); |
| 394 node_seq->Add(new ReturnNode(kPos, |
| 395 new NativeBodyNode(kPos, |
| 396 native_name, |
| 397 native_function, |
| 398 num_params, |
| 399 has_opt_params))); |
| 400 } |
| 401 |
| 402 |
| 403 // Tested Dart code, calling function sum declared above: |
| 404 // return sum(1, 3); |
| 405 // Optional arguments are not passed and hence are set to their default values. |
| 406 CODEGEN_TEST2_GENERATE(StaticSumCallNoOptCodegen, function, test) { |
| 407 SequenceNode* node_seq = test->node_sequence(); |
| 408 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 409 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(1)))); |
| 410 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
| 411 node_seq->Add(new ReturnNode(kPos, |
| 412 new StaticCallNode(kPos, function, arguments))); |
| 413 } |
| 414 CODEGEN_TEST2_RUN(StaticSumCallNoOptCodegen, |
| 415 NativeSumCodegen, |
| 416 Smi::New(1 + 3 + 10 + 21 - 32)) |
| 417 |
| 418 |
| 419 // Tested Dart code, calling function sum declared above: |
| 420 // return sum(1, 3, 5); |
| 421 // Only one out of three optional arguments is passed in; the second and third |
| 422 // arguments are hence set to their default values. |
| 423 CODEGEN_TEST2_GENERATE(StaticSumCallOneOptCodegen, function, test) { |
| 424 SequenceNode* node_seq = test->node_sequence(); |
| 425 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 426 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(1)))); |
| 427 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
| 428 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(5)))); |
| 429 node_seq->Add(new ReturnNode(kPos, |
| 430 new StaticCallNode(kPos, function, arguments))); |
| 431 } |
| 432 CODEGEN_TEST2_RUN(StaticSumCallOneOptCodegen, |
| 433 NativeSumCodegen, |
| 434 Smi::New(1 + 3 + 5 + 21 - 32)) |
| 435 |
| 436 |
| 437 // Tested Dart code, calling function sum declared above: |
| 438 // return sum(0, 1, 1, 2, 3); |
| 439 // Optional arguments are passed in. |
| 440 CODEGEN_TEST2_GENERATE(StaticSumCallTenFiboCodegen, function, test) { |
| 441 SequenceNode* node_seq = test->node_sequence(); |
| 442 ArgumentListNode* arguments = new ArgumentListNode(kPos); |
| 443 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(0)))); |
| 444 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(1)))); |
| 445 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(1)))); |
| 446 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(2)))); |
| 447 arguments->Add(new LiteralNode(kPos, Smi::ZoneHandle(Smi::New(3)))); |
| 448 node_seq->Add(new ReturnNode(kPos, |
| 449 new StaticCallNode(kPos, function, arguments))); |
| 450 } |
| 451 CODEGEN_TEST2_RUN( |
| 452 StaticSumCallTenFiboCodegen, |
| 453 NativeSumCodegen, |
| 454 Smi::New(0 + 1 + 1 + 2 + 3)) |
| 455 |
| 456 |
| 457 // Test allocation of dart objects. |
| 458 CODEGEN_TEST_GENERATE(AllocateNewObjectCodegen, test) { |
| 459 const char* kScriptChars = |
| 460 "class A {\n" |
| 461 " A() {}\n" |
| 462 " static bar() { return 42; }\n" |
| 463 "}\n"; |
| 464 |
| 465 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 466 String& source = String::Handle(String::New(kScriptChars)); |
| 467 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); |
| 468 Library& lib = MakeTestLibrary("TestLib"); |
| 469 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 470 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 471 Class& cls = Class::ZoneHandle(LookupClass(lib, "A")); |
| 472 EXPECT(!cls.IsNull()); |
| 473 |
| 474 String& constructor_name = String::Handle(String::New("A.")); |
| 475 Function& constructor = |
| 476 Function::ZoneHandle(cls.LookupConstructor(constructor_name)); |
| 477 EXPECT(!constructor.IsNull()); |
| 478 |
| 479 const TypeArguments& no_type_arguments = TypeArguments::ZoneHandle(); |
| 480 ArgumentListNode* no_arguments = new ArgumentListNode(kPos); |
| 481 test->node_sequence()->Add(new ReturnNode(kPos, new ConstructorCallNode( |
| 482 kPos, no_type_arguments, constructor, no_arguments))); |
| 483 } |
| 484 |
| 485 |
| 486 CODEGEN_TEST_RAW_RUN(AllocateNewObjectCodegen, function) { |
| 487 GrowableArray<const Object*> arguments; |
| 488 const Array& kNoArgumentNames = Array::Handle(); |
| 489 Instance& result = Instance::Handle(); |
| 490 result = DartEntry::InvokeStatic(function, arguments, kNoArgumentNames); |
| 491 const Library& app_lib = Library::Handle( |
| 492 Isolate::Current()->object_store()->registered_libraries()); |
| 493 const Class& cls = Class::Handle( |
| 494 app_lib.LookupClass(String::Handle(String::NewSymbol("A")))); |
| 495 EXPECT_EQ(cls.raw(), result.clazz()); |
| 496 } |
| 497 |
| 498 } // namespace dart |
| 499 |
| 500 #endif // defined TARGET_ARCH_X64 |
OLD | NEW |