Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 "vm/code_generator.h" | 5 #include "vm/code_generator.h" |
| 6 | 6 |
| 7 #include "vm/code_index_table.h" | 7 #include "vm/code_index_table.h" |
| 8 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| 11 #include "vm/dart_entry.h" | 11 #include "vm/dart_entry.h" |
| 12 #include "vm/debugger.h" | 12 #include "vm/debugger.h" |
| 13 #include "vm/exceptions.h" | 13 #include "vm/exceptions.h" |
| 14 #include "vm/object_store.h" | 14 #include "vm/object_store.h" |
| 15 #include "vm/resolver.h" | 15 #include "vm/resolver.h" |
| 16 #include "vm/runtime_entry.h" | 16 #include "vm/runtime_entry.h" |
| 17 #include "vm/stack_frame.h" | 17 #include "vm/stack_frame.h" |
| 18 #include "vm/verifier.h" | 18 #include "vm/verifier.h" |
| 19 | 19 |
| 20 namespace dart { | 20 namespace dart { |
| 21 | 21 |
| 22 DEFINE_FLAG(bool, inline_cache, true, "enable inline caches"); | 22 DEFINE_FLAG(bool, inline_cache, true, "enable inline caches"); |
| 23 DEFINE_FLAG(bool, trace_deopt, false, "Trace deoptimization"); | 23 DEFINE_FLAG(bool, trace_deopt, false, "Trace deoptimization"); |
| 24 DEFINE_FLAG(bool, trace_ic, false, "trace IC handling"); | 24 DEFINE_FLAG(bool, trace_ic, false, "trace IC handling"); |
| 25 DEFINE_FLAG(bool, trace_patching, false, "Trace patching of code."); | 25 DEFINE_FLAG(bool, trace_patching, false, "Trace patching of code."); |
| 26 DEFINE_FLAG(bool, trace_runtime_calls, false, "Trace runtime calls."); | 26 DEFINE_FLAG(bool, trace_runtime_calls, false, "Trace runtime calls."); |
| 27 DEFINE_FLAG(int, optimization_counter_threshold, 2000, | 27 DEFINE_FLAG(int, optimization_counter_threshold, 2000, |
| 28 "function's usage-counter value before it is optimized, -1 means never."); | 28 "function's usage-counter value before it is optimized, -1 means never."); |
| 29 DECLARE_FLAG(bool, enable_type_checks); | |
| 29 DECLARE_FLAG(bool, trace_type_checks); | 30 DECLARE_FLAG(bool, trace_type_checks); |
| 30 DECLARE_FLAG(bool, report_usage_count); | 31 DECLARE_FLAG(bool, report_usage_count); |
| 31 DECLARE_FLAG(int, deoptimization_counter_threshold); | 32 DECLARE_FLAG(int, deoptimization_counter_threshold); |
| 32 | 33 |
| 33 | 34 |
| 34 bool CodeGenerator::CanOptimize() { | 35 bool CodeGenerator::CanOptimize() { |
| 35 return | 36 return |
| 36 !FLAG_report_usage_count && | 37 !FLAG_report_usage_count && |
| 37 (FLAG_optimization_counter_threshold >= 0) && | 38 (FLAG_optimization_counter_threshold >= 0) && |
| 38 !Isolate::Current()->debugger()->IsActive(); | 39 !Isolate::Current()->debugger()->IsActive(); |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 cloned_ctx.set_parent(Context::Handle(ctx.parent())); | 348 cloned_ctx.set_parent(Context::Handle(ctx.parent())); |
| 348 for (int i = 0; i < ctx.num_variables(); i++) { | 349 for (int i = 0; i < ctx.num_variables(); i++) { |
| 349 cloned_ctx.SetAt(i, Instance::Handle(ctx.At(i))); | 350 cloned_ctx.SetAt(i, Instance::Handle(ctx.At(i))); |
| 350 } | 351 } |
| 351 arguments.SetReturn(cloned_ctx); | 352 arguments.SetReturn(cloned_ctx); |
| 352 } | 353 } |
| 353 | 354 |
| 354 | 355 |
| 355 // Check that the given instance is an instance of the given type. | 356 // Check that the given instance is an instance of the given type. |
| 356 // Tested instance may not be null, because the null test is inlined. | 357 // Tested instance may not be null, because the null test is inlined. |
| 357 // Arg0: instance being checked. | 358 // Arg0: index of the token of the instanceof test (source location). |
|
srdjan
2012/03/07 00:41:23
You should be able to get the token from the PcDes
regis
2012/03/07 02:22:38
The location is passed to Exceptions::CreateAndThr
| |
| 358 // Arg1: type. | 359 // Arg1: instance being checked. |
| 359 // Arg2: type arguments of the instantiator of the type. | 360 // Arg2: type. |
| 360 // Return value: true or false. | 361 // Arg3: type arguments of the instantiator of the type. |
| 361 DEFINE_RUNTIME_ENTRY(Instanceof, 3) { | 362 // Return value: true or false, or may throw a type error in checked mode. |
| 363 DEFINE_RUNTIME_ENTRY(Instanceof, 4) { | |
| 362 ASSERT(arguments.Count() == kInstanceofRuntimeEntry.argument_count()); | 364 ASSERT(arguments.Count() == kInstanceofRuntimeEntry.argument_count()); |
| 363 const Instance& instance = Instance::CheckedHandle(arguments.At(0)); | 365 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); |
| 364 const AbstractType& type = AbstractType::CheckedHandle(arguments.At(1)); | 366 const Instance& instance = Instance::CheckedHandle(arguments.At(1)); |
| 367 const AbstractType& type = AbstractType::CheckedHandle(arguments.At(2)); | |
| 365 const AbstractTypeArguments& type_instantiator = | 368 const AbstractTypeArguments& type_instantiator = |
| 366 AbstractTypeArguments::CheckedHandle(arguments.At(2)); | 369 AbstractTypeArguments::CheckedHandle(arguments.At(3)); |
| 367 ASSERT(type.IsFinalized()); | 370 ASSERT(type.IsFinalized()); |
| 371 Error& malformed_error = Error::Handle(); | |
| 368 const Bool& result = Bool::Handle( | 372 const Bool& result = Bool::Handle( |
| 369 instance.IsInstanceOf(type, type_instantiator) ? | 373 instance.IsInstanceOf(type, type_instantiator, &malformed_error) ? |
| 370 Bool::True() : Bool::False()); | 374 Bool::True() : Bool::False()); |
| 371 if (FLAG_trace_type_checks) { | 375 if (FLAG_trace_type_checks) { |
| 372 const Type& instance_type = Type::Handle(instance.GetType()); | 376 const Type& instance_type = Type::Handle(instance.GetType()); |
| 373 ASSERT(instance_type.IsInstantiated()); | 377 ASSERT(instance_type.IsInstantiated()); |
| 374 if (type.IsInstantiated()) { | 378 if (type.IsInstantiated()) { |
| 375 OS::Print("InstanceOf: '%s' %s '%s'.\n", | 379 OS::Print("InstanceOf: '%s' %s '%s'.\n", |
| 376 String::Handle(instance_type.Name()).ToCString(), | 380 String::Handle(instance_type.Name()).ToCString(), |
| 377 (result.raw() == Bool::True()) ? "is" : "is !", | 381 (result.raw() == Bool::True()) ? "is" : "is !", |
| 378 String::Handle(type.Name()).ToCString()); | 382 String::Handle(type.Name()).ToCString()); |
| 379 } else { | 383 } else { |
| 380 // Instantiate type before printing. | 384 // Instantiate type before printing. |
| 381 const AbstractType& instantiated_type = | 385 const AbstractType& instantiated_type = |
| 382 AbstractType::Handle(type.InstantiateFrom(type_instantiator)); | 386 AbstractType::Handle(type.InstantiateFrom(type_instantiator)); |
| 383 OS::Print("InstanceOf: '%s' %s '%s' instantiated from '%s'.\n", | 387 OS::Print("InstanceOf: '%s' %s '%s' instantiated from '%s'.\n", |
| 384 String::Handle(instance_type.Name()).ToCString(), | 388 String::Handle(instance_type.Name()).ToCString(), |
| 385 (result.raw() == Bool::True()) ? "is" : "is !", | 389 (result.raw() == Bool::True()) ? "is" : "is !", |
| 386 String::Handle(instantiated_type.Name()).ToCString(), | 390 String::Handle(instantiated_type.Name()).ToCString(), |
| 387 String::Handle(type.Name()).ToCString()); | 391 String::Handle(type.Name()).ToCString()); |
| 388 } | 392 } |
| 389 DartFrameIterator iterator; | 393 DartFrameIterator iterator; |
| 390 DartFrame* caller_frame = iterator.NextFrame(); | 394 DartFrame* caller_frame = iterator.NextFrame(); |
| 391 ASSERT(caller_frame != NULL); | 395 ASSERT(caller_frame != NULL); |
| 392 const Function& function = Function::Handle( | 396 const Function& function = Function::Handle( |
| 393 caller_frame->LookupDartFunction()); | 397 caller_frame->LookupDartFunction()); |
| 394 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString()); | 398 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString()); |
| 395 } | 399 } |
| 400 if (!result.value() && !malformed_error.IsNull()) { | |
| 401 ASSERT(FLAG_enable_type_checks); | |
| 402 // Throw a dynamic type error only if the instanceof test fails. | |
| 403 String& malformed_error_message = String::Handle( | |
| 404 String::New(malformed_error.ToErrorCString())); | |
| 405 const String& no_name = String::Handle(String::NewSymbol("")); | |
| 406 Exceptions::CreateAndThrowTypeError( | |
| 407 location, no_name, no_name, no_name, malformed_error_message); | |
| 408 UNREACHABLE(); | |
| 409 } | |
| 396 arguments.SetReturn(result); | 410 arguments.SetReturn(result); |
| 397 } | 411 } |
| 398 | 412 |
| 399 | 413 |
| 414 // Check that the type of the given instance is assignable to the given type. | |
| 415 // Arg0: index of the token of the assignment (source location). | |
|
srdjan
2012/03/07 00:41:23
ditto here and below.
regis
2012/03/07 02:22:38
Added TODO.
| |
| 416 // Arg1: instance being assigned. | |
| 417 // Arg2: type being assigned to. | |
| 418 // Arg3: type arguments of the instantiator of the type being assigned to. | |
| 419 // Arg4: name of instance being assigned to. | |
| 420 // Return value: instance if assignable, otherwise throw a TypeError. | |
| 421 DEFINE_RUNTIME_ENTRY(TypeCheck, 5) { | |
| 422 ASSERT(arguments.Count() == kTypeCheckRuntimeEntry.argument_count()); | |
| 423 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | |
| 424 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1)); | |
| 425 const AbstractType& dst_type = AbstractType::CheckedHandle(arguments.At(2)); | |
| 426 const AbstractTypeArguments& dst_type_instantiator = | |
| 427 AbstractTypeArguments::CheckedHandle(arguments.At(3)); | |
| 428 const String& dst_name = String::CheckedHandle(arguments.At(4)); | |
| 429 ASSERT(!dst_type.IsDynamicType()); // No need to check assignment. | |
| 430 ASSERT(!src_instance.IsNull()); // Already checked in inlined code. | |
| 431 | |
| 432 Error& malformed_error = Error::Handle(); | |
| 433 const bool is_assignable = src_instance.IsAssignableTo( | |
| 434 dst_type, dst_type_instantiator, &malformed_error); | |
| 435 | |
| 436 if (FLAG_trace_type_checks) { | |
| 437 const Type& src_type = Type::Handle(src_instance.GetType()); | |
| 438 if (dst_type.IsInstantiated()) { | |
| 439 OS::Print("TypeCheck: '%s' %s assignable to '%s' of '%s'.\n", | |
| 440 String::Handle(src_type.Name()).ToCString(), | |
| 441 is_assignable ? "is" : "is not", | |
| 442 String::Handle(dst_type.Name()).ToCString(), | |
| 443 dst_name.ToCString()); | |
| 444 } else { | |
| 445 // Instantiate dst_type before printing. | |
| 446 const AbstractType& instantiated_dst_type = AbstractType::Handle( | |
| 447 dst_type.InstantiateFrom(dst_type_instantiator)); | |
| 448 OS::Print("TypeCheck: '%s' %s assignable to '%s' of '%s' " | |
| 449 "instantiated from '%s'.\n", | |
| 450 String::Handle(src_type.Name()).ToCString(), | |
| 451 is_assignable ? "is" : "is not", | |
| 452 String::Handle(instantiated_dst_type.Name()).ToCString(), | |
| 453 dst_name.ToCString(), | |
| 454 String::Handle(dst_type.Name()).ToCString()); | |
| 455 } | |
| 456 DartFrameIterator iterator; | |
| 457 DartFrame* caller_frame = iterator.NextFrame(); | |
| 458 ASSERT(caller_frame != NULL); | |
| 459 const Function& function = Function::Handle( | |
| 460 caller_frame->LookupDartFunction()); | |
| 461 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString()); | |
| 462 } | |
| 463 if (!is_assignable) { | |
| 464 const Type& src_type = Type::Handle(src_instance.GetType()); | |
| 465 const String& src_type_name = String::Handle(src_type.Name()); | |
| 466 String& dst_type_name = String::Handle(); | |
| 467 if (!dst_type.IsInstantiated()) { | |
| 468 // Instantiate dst_type before reporting the error. | |
| 469 const AbstractType& instantiated_dst_type = AbstractType::Handle( | |
| 470 dst_type.InstantiateFrom(dst_type_instantiator)); | |
| 471 dst_type_name = instantiated_dst_type.Name(); | |
| 472 } else { | |
| 473 dst_type_name = dst_type.Name(); | |
| 474 } | |
| 475 String& malformed_error_message = String::Handle(); | |
| 476 if (!malformed_error.IsNull()) { | |
| 477 ASSERT(FLAG_enable_type_checks); | |
| 478 malformed_error_message = String::New(malformed_error.ToErrorCString()); | |
| 479 } | |
| 480 Exceptions::CreateAndThrowTypeError(location, src_type_name, dst_type_name, | |
| 481 dst_name, malformed_error_message); | |
| 482 UNREACHABLE(); | |
| 483 } | |
| 484 arguments.SetReturn(src_instance); | |
| 485 } | |
| 486 | |
| 487 | |
| 488 // Report that the type of the given object is not bool in conditional context. | |
| 489 // Arg0: index of the token of the assignment (source location). | |
| 490 // Arg1: bad object. | |
| 491 // Return value: none, throws a TypeError. | |
| 492 DEFINE_RUNTIME_ENTRY(ConditionTypeError, 2) { | |
| 493 ASSERT(arguments.Count() == | |
| 494 kConditionTypeErrorRuntimeEntry.argument_count()); | |
| 495 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | |
| 496 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1)); | |
| 497 ASSERT(src_instance.IsNull() || !src_instance.IsBool()); | |
| 498 const Type& bool_interface = Type::Handle(Type::BoolInterface()); | |
| 499 const Type& src_type = Type::Handle(src_instance.GetType()); | |
| 500 const String& src_type_name = String::Handle(src_type.Name()); | |
| 501 const String& bool_type_name = String::Handle(bool_interface.Name()); | |
| 502 const String& expr = String::Handle(String::NewSymbol("boolean expression")); | |
| 503 const String& no_malformed_type_error = String::Handle(); | |
| 504 Exceptions::CreateAndThrowTypeError(location, src_type_name, bool_type_name, | |
| 505 expr, no_malformed_type_error); | |
| 506 UNREACHABLE(); | |
| 507 } | |
| 508 | |
| 509 | |
| 510 // Report that the type of the type check is malformed. | |
| 511 // Arg0: index of the token of the failed type check. | |
| 512 // Arg1: src value. | |
| 513 // Arg2: name of instance being assigned to. | |
| 514 // Arg3: malformed type error message. | |
| 515 // Return value: none, throws an exception. | |
| 516 DEFINE_RUNTIME_ENTRY(MalformedTypeError, 4) { | |
| 517 ASSERT(arguments.Count() == | |
| 518 kMalformedTypeErrorRuntimeEntry.argument_count()); | |
| 519 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | |
| 520 const Instance& src_value = Instance::CheckedHandle(arguments.At(1)); | |
| 521 const String& dst_name = String::CheckedHandle(arguments.At(2)); | |
| 522 const String& malformed_error = String::CheckedHandle(arguments.At(3)); | |
| 523 const String& dst_type_name = String::Handle(String::NewSymbol("malformed")); | |
| 524 const String& src_type_name = | |
| 525 String::Handle(Type::Handle(src_value.GetType()).Name()); | |
| 526 Exceptions::CreateAndThrowTypeError(location, src_type_name, | |
| 527 dst_type_name, dst_name, malformed_error); | |
| 528 UNREACHABLE(); | |
| 529 } | |
| 530 | |
| 531 | |
| 532 // Check that the type of each element of the given array is assignable to the | |
| 533 // given type. | |
| 534 // Arg0: index of the token of the rest argument declaration (source location). | |
| 535 // Arg1: rest argument array. | |
| 536 // Arg2: element declaration type. | |
| 537 // Arg3: type arguments of the instantiator of the element declaration type. | |
| 538 // Arg4: name of object being assigned to, i.e. name of rest argument. | |
| 539 // Return value: null if assignable, otherwise allocate and throw a TypeError. | |
| 540 DEFINE_RUNTIME_ENTRY(RestArgumentTypeCheck, 5) { | |
| 541 ASSERT(arguments.Count() == | |
| 542 kRestArgumentTypeCheckRuntimeEntry.argument_count()); | |
| 543 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | |
| 544 const Array& rest_array = Array::CheckedHandle(arguments.At(1)); | |
| 545 const AbstractType& element_type = | |
| 546 AbstractType::CheckedHandle(arguments.At(2)); | |
| 547 const AbstractTypeArguments& element_type_instantiator = | |
| 548 AbstractTypeArguments::CheckedHandle(arguments.At(3)); | |
| 549 const String& rest_name = String::CheckedHandle(arguments.At(4)); | |
| 550 ASSERT(!element_type.IsDynamicType()); // No need to check assignment. | |
| 551 ASSERT(!rest_array.IsNull()); | |
| 552 | |
| 553 Instance& elem = Instance::Handle(); | |
| 554 Error& malformed_error = Error::Handle(); | |
| 555 for (intptr_t i = 0; i < rest_array.Length(); i++) { | |
| 556 elem ^= rest_array.At(i); | |
| 557 // The previous successful type check may have set malformed_error. | |
| 558 // Note that a returned malformed_error is ignored if a type check succeeds. | |
| 559 malformed_error = Error::null(); | |
| 560 if (!elem.IsNull() && !elem.IsAssignableTo(element_type, | |
| 561 element_type_instantiator, | |
| 562 &malformed_error)) { | |
| 563 // Allocate and throw a new instance of TypeError. | |
| 564 char buf[256]; | |
| 565 OS::SNPrint(buf, sizeof(buf), "%s[%d]", | |
| 566 rest_name.ToCString(), static_cast<int>(i)); | |
| 567 const String& src_type_name = | |
| 568 String::Handle(Type::Handle(elem.GetType()).Name()); | |
| 569 String& dst_type_name = String::Handle(); | |
| 570 if (!element_type.IsInstantiated()) { | |
| 571 // Instantiate element_type before reporting the error. | |
| 572 const AbstractType& instantiated_element_type = AbstractType::Handle( | |
| 573 element_type.InstantiateFrom(element_type_instantiator)); | |
| 574 dst_type_name = instantiated_element_type.Name(); | |
| 575 } else { | |
| 576 dst_type_name = element_type.Name(); | |
| 577 } | |
| 578 const String& dst_name = String::Handle(String::New(buf)); | |
| 579 String& malformed_error_message = String::Handle(); | |
| 580 if (!malformed_error.IsNull()) { | |
| 581 ASSERT(FLAG_enable_type_checks); | |
| 582 malformed_error_message = String::New(malformed_error.ToErrorCString()); | |
| 583 } | |
| 584 Exceptions::CreateAndThrowTypeError(location, src_type_name, | |
| 585 dst_type_name, dst_name, | |
| 586 malformed_error_message); | |
| 587 UNREACHABLE(); | |
| 588 } | |
| 589 } | |
| 590 } | |
| 591 | |
| 592 | |
| 400 DEFINE_RUNTIME_ENTRY(Throw, 1) { | 593 DEFINE_RUNTIME_ENTRY(Throw, 1) { |
| 401 ASSERT(arguments.Count() == kThrowRuntimeEntry.argument_count()); | 594 ASSERT(arguments.Count() == kThrowRuntimeEntry.argument_count()); |
| 402 const Instance& exception = Instance::CheckedHandle(arguments.At(0)); | 595 const Instance& exception = Instance::CheckedHandle(arguments.At(0)); |
| 403 Exceptions::Throw(exception); | 596 Exceptions::Throw(exception); |
| 404 } | 597 } |
| 405 | 598 |
| 406 | 599 |
| 407 DEFINE_RUNTIME_ENTRY(ReThrow, 2) { | 600 DEFINE_RUNTIME_ENTRY(ReThrow, 2) { |
| 408 ASSERT(arguments.Count() == kReThrowRuntimeEntry.argument_count()); | 601 ASSERT(arguments.Count() == kReThrowRuntimeEntry.argument_count()); |
| 409 const Instance& exception = Instance::CheckedHandle(arguments.At(0)); | 602 const Instance& exception = Instance::CheckedHandle(arguments.At(0)); |
| (...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1194 } | 1387 } |
| 1195 } | 1388 } |
| 1196 } | 1389 } |
| 1197 // The cache is null terminated, therefore the loop above should never | 1390 // The cache is null terminated, therefore the loop above should never |
| 1198 // terminate by itself. | 1391 // terminate by itself. |
| 1199 UNREACHABLE(); | 1392 UNREACHABLE(); |
| 1200 return Code::null(); | 1393 return Code::null(); |
| 1201 } | 1394 } |
| 1202 | 1395 |
| 1203 } // namespace dart | 1396 } // namespace dart |
| OLD | NEW |