Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_
     18 #define ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_
     19 
     20 #include "base/macros.h"
     21 #include "dex_instruction.h"
     22 
     23 namespace art {
     24 
     25 template<typename T>
     26 class DexInstructionVisitor {
     27  public:
     28   void Visit(const uint16_t* code, size_t size_in_bytes) {
     29     T* derived = static_cast<T*>(this);
     30     size_t size_in_code_units = size_in_bytes / sizeof(uint16_t);
     31     size_t i = 0;
     32     while (i < size_in_code_units) {
     33       const Instruction* inst = Instruction::At(&code[i]);
     34       switch (inst->Opcode()) {
     35 #define INSTRUCTION_CASE(o, cname, p, f, i, a, v)  \
     36         case Instruction::cname: {                    \
     37           derived->Do_ ## cname(inst);                \
     38           break;                                      \
     39         }
     40 #include "dex_instruction_list.h"
     41         DEX_INSTRUCTION_LIST(INSTRUCTION_CASE)
     42 #undef DEX_INSTRUCTION_LIST
     43 #undef INSTRUCTION_CASE
     44         default:
     45           CHECK(false);
     46       }
     47       i += inst->SizeInCodeUnits();
     48     }
     49   }
     50 
     51  private:
     52   // Specific handlers for each instruction.
     53 #define INSTRUCTION_VISITOR(o, cname, p, f, i, a, v)    \
     54   void Do_ ## cname(const Instruction* inst) {             \
     55     T* derived = static_cast<T*>(this);                    \
     56     derived->Do_Default(inst);                             \
     57   }
     58 #include "dex_instruction_list.h"
     59   DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR)
     60 #undef DEX_INSTRUCTION_LIST
     61 #undef INSTRUCTION_VISITOR
     62 
     63   // The default instruction handler.
     64   void Do_Default(const Instruction*) {
     65     return;
     66   }
     67 };
     68 
     69 
     70 }  // namespace art
     71 
     72 #endif  // ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_
     73