art虚拟机解释执行

art 可以解释执行dex字节码,

解释执行:读一行dex字节码执行该字节码

art/runtime/interpreter/interpreter_switch_impl.cc

调用ExecuteSwitchImplCpp函数开始解释执行字节码

使用do while循环取dex字节码

接下来使用switch判断需要解释哪一个字节码

我们拿Instruction::NEW_INSTANCE举例

653        case Instruction::NEW_INSTANCE: {
654        PREAMBLE();
655        ObjPtr obj = nullptr;
                                                          //调用该方法通过class linker获取class文件描述.
656        ObjPtr c = ResolveVerifyAndClinit(dex::TypeIndex(inst->VRegB_21c()),
657                                                         shadow_frame.GetMethod(),
658                                                         self,
659                                                         false,
660                                                         do_access_check);
661        if (LIKELY(c != nullptr)) {
                            //String对象需要特殊处理,这里我们只关注普通对象
662          if (UNLIKELY(c->IsStringClass())) {
663            gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
664            obj = mirror::String::AllocEmptyString(self, allocator_type);
665          } else {
                          //调用该方法,为对象分配内存空间
666            obj = AllocObjectFromCode(
667                c.Ptr(),
668                self,
669                Runtime::Current()->GetHeap()->GetCurrentAllocator());
670          }
671        }
672        if (UNLIKELY(obj == nullptr)) {
673          HANDLE_PENDING_EXCEPTION();
674        } else {
675          obj->GetClass()->AssertInitializedOrInitializingInThread(self);
676          // Don't allow finalizable objects to be allocated during a transaction since these can't
677          // be finalized without a started runtime.
678          if (transaction_active && obj->GetClass()->IsFinalizable()) {
679            AbortTransactionF(self, "Allocating finalizable object in transaction: %s",
680                              obj->PrettyTypeOf().c_str());
681            HANDLE_PENDING_EXCEPTION();
682            break;
683          }
              //让变量指向obj的指针,就可以用变量调用对象的方法了/
684          shadow_frame.SetVRegReference(inst->VRegA_21c(inst_data), obj.Ptr());
                //字节码执行成功,执行下一个字节码
685          inst = inst->Next_2xx();
686        }
687        break;
688      }

接下来针对这几行代码进行更深入的分析

你可能感兴趣的:(art虚拟机解释执行)