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 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "dex_instruction.h" |
| 7 | #include "macros.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 8 | |
| 9 | namespace art { |
| 10 | |
| 11 | template<typename T> |
| 12 | class DexInstructionVisitor { |
| 13 | public: |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 14 | void Visit(const uint16_t* code, size_t size) { |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 15 | T* derived = static_cast<T*>(this); |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 16 | const byte* ptr = reinterpret_cast<const byte*>(code); |
| 17 | const byte* end = ptr + size; |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 18 | while (ptr != end) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 19 | const Instruction* inst = Instruction::At(ptr); |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 20 | switch (inst->Opcode()) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 21 | #define INSTRUCTION_CASE(o, cname, p, f, r, i, a, v) \ |
| 22 | case Instruction::cname: { \ |
| 23 | derived->Do_ ## cname(inst); \ |
| 24 | break; \ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 25 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 26 | #include "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 | } |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 33 | ptr += inst->Size() * sizeof(uint16_t); |
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. |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 40 | #define INSTRUCTION_VISITOR(o, cname, p, f, r, i, a, v) \ |
| 41 | void Do_ ## cname(const Instruction* inst) { \ |
| 42 | T* derived = static_cast<T*>(this); \ |
| 43 | derived->Do_Default(inst); \ |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 44 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 45 | #include "dex_instruction_list.h" |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 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. |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 51 | void Do_Default(const Instruction* inst) { |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | }; |
| 55 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 56 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 57 | } // namespace art |
| 58 | |
| 59 | #endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_ |