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