在~/android-6.0.1_r62/art/compiler/optimizing/builder.cc中为类HGraphBuilder的具体信息:
1/首要分析的方法为:
bool AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc);
该方法的主要功能是分析dex instruction,将HInstruction添加到graph中以可以执行该dex instruction。返回值意为,该dex instruction是否能被处理。
具体实现:
A.
1317 if (current_block_ == nullptr) {
1318 return true; // Dead code
1319 }
先检查current_block_ == nullptr,若相等,则直接返回真。
B. switch (instruction.Opcode())
根据Opcode的不同进行不同的处理,类instruction中定义了dex instruction,Opcode()方法返回的是instruction的opcode field,也就是instruction的前16位。
下面以CONST_4为例,总共只有三行代码。
1322 case Instruction::CONST_4: {
1323 int32_t register_index = instruction.VRegA();
1324 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n());//HIntConstant类包含的是int类型的常量。
1325 UpdateLocal(register_index, constant);
1326 break;
1327 }
其中CONST_4为dalvik指令const/4:
const/4 vA,#+B 将数值符号扩展为32位后赋值给寄存器vA。
关于dalvik指令更详细的介绍在~/android-6.0.1_r62/art/runtime/dex_instruction_list.h中,例如对于CONST_4:
#define DEX_INSTRUCTION_LIST(V) \
V(0x12, CONST_4, "const/4", k11n, true, kNone, kContinue | kRegBFieldOrConstant, kVerifyRegA)
①int32_t register_index = instruction.VRegA();
57 inline int32_t Instruction::VRegA() const {
58 switch (FormatOf(Opcode())) {
59 case k10t: return VRegA_10t();
60 case k10x: return VRegA_10x();
61 case k11n: return VRegA_11n();
62 case k11x: return VRegA_11x();
63 case<