Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_DEX_INSTRUCTION_VISITOR_H_ |
| 4 | #define ART_SRC_DEX_INSTRUCTION_VISITOR_H_ |
| 5 | |
| 6 | #include "src/dex_instruction.h" |
| 7 | #include "src/macros.h" |
| 8 | |
| 9 | namespace art { |
| 10 | |
| 11 | template<typename T> |
| 12 | class DexInstructionVisitor { |
| 13 | public: |
| 14 | void Visit(uint16_t* code, size_t size) { |
| 15 | T* derived = static_cast<T*>(this); |
| 16 | byte* ptr = reinterpret_cast<byte*>(code); |
| 17 | byte* end = ptr + size; |
| 18 | while (ptr != end) { |
| 19 | Instruction* inst = Instruction::At(ptr); |
| 20 | switch (inst->Opcode()) { |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 21 | #define INSTRUCTION_CASE(o, cname, p, f, r, i, a) \ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 22 | case Instruction::cname: { \ |
| 23 | derived->Do_ ## cname(inst); \ |
| 24 | break; \ |
| 25 | } |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 26 | #include "src/dex_instruction_list.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 27 | DEX_INSTRUCTION_LIST(INSTRUCTION_CASE) |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 28 | #undef DEX_INSTRUCTION_LIST |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 29 | #undef INSTRUCTION_CASE |
| 30 | default: |
| 31 | CHECK(true); |
| 32 | } |
| 33 | ptr += inst->Size(); |
buzbee | a7ab79d | 2011-06-28 16:07:35 -0700 | [diff] [blame] | 34 | CHECK_LE(ptr, end); |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | // Specific handlers for each instruction. |
Carl Shapiro | e4c1ce4 | 2011-07-09 02:31:57 -0700 | [diff] [blame] | 40 | #define INSTRUCTION_VISITOR(o, cname, p, f, r, i, a) \ |
| 41 | void Do_ ## cname(Instruction* inst) { \ |
| 42 | T* derived = static_cast<T*>(this); \ |
| 43 | derived->Do_Default(inst); \ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 44 | }; |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 45 | #include "src/dex_instruction_list.h" |
| 46 | DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR) |
| 47 | #undef DEX_INSTRUCTION_LIST |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 48 | #undef INSTRUCTION_VISITOR |
| 49 | |
| 50 | // The default instruction handler. |
| 51 | void Do_Default(Instruction* inst) { |
| 52 | return; |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | } // namespace art |
| 57 | |
| 58 | #endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_ |