Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: runtime/vm/code_generator.cc

Issue 9615035: Generate dynamic type errors according to spec. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/class_finalizer.cc ('k') | runtime/vm/code_generator_ia32.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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).
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 // TODO(regis): Get the token index from the PcDesc (via DartFrame).
364 const AbstractType& type = AbstractType::CheckedHandle(arguments.At(1)); 366 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
367 const Instance& instance = Instance::CheckedHandle(arguments.At(1));
368 const AbstractType& type = AbstractType::CheckedHandle(arguments.At(2));
365 const AbstractTypeArguments& type_instantiator = 369 const AbstractTypeArguments& type_instantiator =
366 AbstractTypeArguments::CheckedHandle(arguments.At(2)); 370 AbstractTypeArguments::CheckedHandle(arguments.At(3));
367 ASSERT(type.IsFinalized()); 371 ASSERT(type.IsFinalized());
372 Error& malformed_error = Error::Handle();
368 const Bool& result = Bool::Handle( 373 const Bool& result = Bool::Handle(
369 instance.IsInstanceOf(type, type_instantiator) ? 374 instance.IsInstanceOf(type, type_instantiator, &malformed_error) ?
370 Bool::True() : Bool::False()); 375 Bool::True() : Bool::False());
371 if (FLAG_trace_type_checks) { 376 if (FLAG_trace_type_checks) {
372 const Type& instance_type = Type::Handle(instance.GetType()); 377 const Type& instance_type = Type::Handle(instance.GetType());
373 ASSERT(instance_type.IsInstantiated()); 378 ASSERT(instance_type.IsInstantiated());
374 if (type.IsInstantiated()) { 379 if (type.IsInstantiated()) {
375 OS::Print("InstanceOf: '%s' %s '%s'.\n", 380 OS::Print("InstanceOf: '%s' %s '%s'.\n",
376 String::Handle(instance_type.Name()).ToCString(), 381 String::Handle(instance_type.Name()).ToCString(),
377 (result.raw() == Bool::True()) ? "is" : "is !", 382 (result.raw() == Bool::True()) ? "is" : "is !",
378 String::Handle(type.Name()).ToCString()); 383 String::Handle(type.Name()).ToCString());
379 } else { 384 } else {
380 // Instantiate type before printing. 385 // Instantiate type before printing.
381 const AbstractType& instantiated_type = 386 const AbstractType& instantiated_type =
382 AbstractType::Handle(type.InstantiateFrom(type_instantiator)); 387 AbstractType::Handle(type.InstantiateFrom(type_instantiator));
383 OS::Print("InstanceOf: '%s' %s '%s' instantiated from '%s'.\n", 388 OS::Print("InstanceOf: '%s' %s '%s' instantiated from '%s'.\n",
384 String::Handle(instance_type.Name()).ToCString(), 389 String::Handle(instance_type.Name()).ToCString(),
385 (result.raw() == Bool::True()) ? "is" : "is !", 390 (result.raw() == Bool::True()) ? "is" : "is !",
386 String::Handle(instantiated_type.Name()).ToCString(), 391 String::Handle(instantiated_type.Name()).ToCString(),
387 String::Handle(type.Name()).ToCString()); 392 String::Handle(type.Name()).ToCString());
388 } 393 }
389 DartFrameIterator iterator; 394 DartFrameIterator iterator;
390 DartFrame* caller_frame = iterator.NextFrame(); 395 DartFrame* caller_frame = iterator.NextFrame();
391 ASSERT(caller_frame != NULL); 396 ASSERT(caller_frame != NULL);
392 const Function& function = Function::Handle( 397 const Function& function = Function::Handle(
393 caller_frame->LookupDartFunction()); 398 caller_frame->LookupDartFunction());
394 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString()); 399 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString());
395 } 400 }
401 if (!result.value() && !malformed_error.IsNull()) {
402 ASSERT(FLAG_enable_type_checks);
403 // Throw a dynamic type error only if the instanceof test fails.
404 String& malformed_error_message = String::Handle(
405 String::New(malformed_error.ToErrorCString()));
406 const String& no_name = String::Handle(String::NewSymbol(""));
407 Exceptions::CreateAndThrowTypeError(
408 location, no_name, no_name, no_name, malformed_error_message);
409 UNREACHABLE();
410 }
396 arguments.SetReturn(result); 411 arguments.SetReturn(result);
397 } 412 }
398 413
399 414
415 // Check that the type of the given instance is assignable to the given type.
416 // Arg0: index of the token of the assignment (source location).
417 // Arg1: instance being assigned.
418 // Arg2: type being assigned to.
419 // Arg3: type arguments of the instantiator of the type being assigned to.
420 // Arg4: name of instance being assigned to.
421 // Return value: instance if assignable, otherwise throw a TypeError.
422 DEFINE_RUNTIME_ENTRY(TypeCheck, 5) {
423 ASSERT(arguments.Count() == kTypeCheckRuntimeEntry.argument_count());
424 // TODO(regis): Get the token index from the PcDesc (via DartFrame).
425 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
426 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1));
427 const AbstractType& dst_type = AbstractType::CheckedHandle(arguments.At(2));
428 const AbstractTypeArguments& dst_type_instantiator =
429 AbstractTypeArguments::CheckedHandle(arguments.At(3));
430 const String& dst_name = String::CheckedHandle(arguments.At(4));
431 ASSERT(!dst_type.IsDynamicType()); // No need to check assignment.
432 ASSERT(!src_instance.IsNull()); // Already checked in inlined code.
433
434 Error& malformed_error = Error::Handle();
435 const bool is_assignable = src_instance.IsAssignableTo(
436 dst_type, dst_type_instantiator, &malformed_error);
437
438 if (FLAG_trace_type_checks) {
439 const Type& src_type = Type::Handle(src_instance.GetType());
440 if (dst_type.IsInstantiated()) {
441 OS::Print("TypeCheck: '%s' %s assignable to '%s' of '%s'.\n",
442 String::Handle(src_type.Name()).ToCString(),
443 is_assignable ? "is" : "is not",
444 String::Handle(dst_type.Name()).ToCString(),
445 dst_name.ToCString());
446 } else {
447 // Instantiate dst_type before printing.
448 const AbstractType& instantiated_dst_type = AbstractType::Handle(
449 dst_type.InstantiateFrom(dst_type_instantiator));
450 OS::Print("TypeCheck: '%s' %s assignable to '%s' of '%s' "
451 "instantiated from '%s'.\n",
452 String::Handle(src_type.Name()).ToCString(),
453 is_assignable ? "is" : "is not",
454 String::Handle(instantiated_dst_type.Name()).ToCString(),
455 dst_name.ToCString(),
456 String::Handle(dst_type.Name()).ToCString());
457 }
458 DartFrameIterator iterator;
459 DartFrame* caller_frame = iterator.NextFrame();
460 ASSERT(caller_frame != NULL);
461 const Function& function = Function::Handle(
462 caller_frame->LookupDartFunction());
463 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString());
464 }
465 if (!is_assignable) {
466 const Type& src_type = Type::Handle(src_instance.GetType());
467 const String& src_type_name = String::Handle(src_type.Name());
468 String& dst_type_name = String::Handle();
469 if (!dst_type.IsInstantiated()) {
470 // Instantiate dst_type before reporting the error.
471 const AbstractType& instantiated_dst_type = AbstractType::Handle(
472 dst_type.InstantiateFrom(dst_type_instantiator));
473 dst_type_name = instantiated_dst_type.Name();
474 } else {
475 dst_type_name = dst_type.Name();
476 }
477 String& malformed_error_message = String::Handle();
478 if (!malformed_error.IsNull()) {
479 ASSERT(FLAG_enable_type_checks);
480 malformed_error_message = String::New(malformed_error.ToErrorCString());
481 }
482 Exceptions::CreateAndThrowTypeError(location, src_type_name, dst_type_name,
483 dst_name, malformed_error_message);
484 UNREACHABLE();
485 }
486 arguments.SetReturn(src_instance);
487 }
488
489
490 // Report that the type of the given object is not bool in conditional context.
491 // Arg0: index of the token of the assignment (source location).
492 // Arg1: bad object.
493 // Return value: none, throws a TypeError.
494 DEFINE_RUNTIME_ENTRY(ConditionTypeError, 2) {
495 ASSERT(arguments.Count() ==
496 kConditionTypeErrorRuntimeEntry.argument_count());
497 // TODO(regis): Get the token index from the PcDesc (via DartFrame).
498 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
499 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1));
500 ASSERT(src_instance.IsNull() || !src_instance.IsBool());
501 const Type& bool_interface = Type::Handle(Type::BoolInterface());
502 const Type& src_type = Type::Handle(src_instance.GetType());
503 const String& src_type_name = String::Handle(src_type.Name());
504 const String& bool_type_name = String::Handle(bool_interface.Name());
505 const String& expr = String::Handle(String::NewSymbol("boolean expression"));
506 const String& no_malformed_type_error = String::Handle();
507 Exceptions::CreateAndThrowTypeError(location, src_type_name, bool_type_name,
508 expr, no_malformed_type_error);
509 UNREACHABLE();
510 }
511
512
513 // Report that the type of the type check is malformed.
514 // Arg0: index of the token of the failed type check.
515 // Arg1: src value.
516 // Arg2: name of instance being assigned to.
517 // Arg3: malformed type error message.
518 // Return value: none, throws an exception.
519 DEFINE_RUNTIME_ENTRY(MalformedTypeError, 4) {
520 ASSERT(arguments.Count() ==
521 kMalformedTypeErrorRuntimeEntry.argument_count());
522 // TODO(regis): Get the token index from the PcDesc (via DartFrame).
523 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
524 const Instance& src_value = Instance::CheckedHandle(arguments.At(1));
525 const String& dst_name = String::CheckedHandle(arguments.At(2));
526 const String& malformed_error = String::CheckedHandle(arguments.At(3));
527 const String& dst_type_name = String::Handle(String::NewSymbol("malformed"));
528 const String& src_type_name =
529 String::Handle(Type::Handle(src_value.GetType()).Name());
530 Exceptions::CreateAndThrowTypeError(location, src_type_name,
531 dst_type_name, dst_name, malformed_error);
532 UNREACHABLE();
533 }
534
535
536 // Check that the type of each element of the given array is assignable to the
537 // given type.
538 // Arg0: index of the token of the rest argument declaration (source location).
539 // Arg1: rest argument array.
540 // Arg2: element declaration type.
541 // Arg3: type arguments of the instantiator of the element declaration type.
542 // Arg4: name of object being assigned to, i.e. name of rest argument.
543 // Return value: null if assignable, otherwise allocate and throw a TypeError.
544 DEFINE_RUNTIME_ENTRY(RestArgumentTypeCheck, 5) {
545 ASSERT(arguments.Count() ==
546 kRestArgumentTypeCheckRuntimeEntry.argument_count());
547 // TODO(regis): Get the token index from the PcDesc (via DartFrame).
548 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value();
549 const Array& rest_array = Array::CheckedHandle(arguments.At(1));
550 const AbstractType& element_type =
551 AbstractType::CheckedHandle(arguments.At(2));
552 const AbstractTypeArguments& element_type_instantiator =
553 AbstractTypeArguments::CheckedHandle(arguments.At(3));
554 const String& rest_name = String::CheckedHandle(arguments.At(4));
555 ASSERT(!element_type.IsDynamicType()); // No need to check assignment.
556 ASSERT(!rest_array.IsNull());
557
558 Instance& elem = Instance::Handle();
559 Error& malformed_error = Error::Handle();
560 for (intptr_t i = 0; i < rest_array.Length(); i++) {
561 elem ^= rest_array.At(i);
562 // The previous successful type check may have set malformed_error.
563 // Note that a returned malformed_error is ignored if a type check succeeds.
564 malformed_error = Error::null();
565 if (!elem.IsNull() && !elem.IsAssignableTo(element_type,
566 element_type_instantiator,
567 &malformed_error)) {
568 // Allocate and throw a new instance of TypeError.
569 char buf[256];
570 OS::SNPrint(buf, sizeof(buf), "%s[%d]",
571 rest_name.ToCString(), static_cast<int>(i));
572 const String& src_type_name =
573 String::Handle(Type::Handle(elem.GetType()).Name());
574 String& dst_type_name = String::Handle();
575 if (!element_type.IsInstantiated()) {
576 // Instantiate element_type before reporting the error.
577 const AbstractType& instantiated_element_type = AbstractType::Handle(
578 element_type.InstantiateFrom(element_type_instantiator));
579 dst_type_name = instantiated_element_type.Name();
580 } else {
581 dst_type_name = element_type.Name();
582 }
583 const String& dst_name = String::Handle(String::New(buf));
584 String& malformed_error_message = String::Handle();
585 if (!malformed_error.IsNull()) {
586 ASSERT(FLAG_enable_type_checks);
587 malformed_error_message = String::New(malformed_error.ToErrorCString());
588 }
589 Exceptions::CreateAndThrowTypeError(location, src_type_name,
590 dst_type_name, dst_name,
591 malformed_error_message);
592 UNREACHABLE();
593 }
594 }
595 }
596
597
400 DEFINE_RUNTIME_ENTRY(Throw, 1) { 598 DEFINE_RUNTIME_ENTRY(Throw, 1) {
401 ASSERT(arguments.Count() == kThrowRuntimeEntry.argument_count()); 599 ASSERT(arguments.Count() == kThrowRuntimeEntry.argument_count());
402 const Instance& exception = Instance::CheckedHandle(arguments.At(0)); 600 const Instance& exception = Instance::CheckedHandle(arguments.At(0));
403 Exceptions::Throw(exception); 601 Exceptions::Throw(exception);
404 } 602 }
405 603
406 604
407 DEFINE_RUNTIME_ENTRY(ReThrow, 2) { 605 DEFINE_RUNTIME_ENTRY(ReThrow, 2) {
408 ASSERT(arguments.Count() == kReThrowRuntimeEntry.argument_count()); 606 ASSERT(arguments.Count() == kReThrowRuntimeEntry.argument_count());
409 const Instance& exception = Instance::CheckedHandle(arguments.At(0)); 607 const Instance& exception = Instance::CheckedHandle(arguments.At(0));
(...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 } 1392 }
1195 } 1393 }
1196 } 1394 }
1197 // The cache is null terminated, therefore the loop above should never 1395 // The cache is null terminated, therefore the loop above should never
1198 // terminate by itself. 1396 // terminate by itself.
1199 UNREACHABLE(); 1397 UNREACHABLE();
1200 return Code::null(); 1398 return Code::null();
1201 } 1399 }
1202 1400
1203 } // namespace dart 1401 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/class_finalizer.cc ('k') | runtime/vm/code_generator_ia32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698