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: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 14 | void Visit(const uint16_t* code, size_t size_in_bytes) { |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 15 | T* derived = static_cast<T*>(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 16 | size_t size_in_code_units = size_in_bytes / sizeof(uint16_t); |
| 17 | size_t i = 0; |
| 18 | while (i < size_in_code_units) { |
| 19 | const Instruction* inst = Instruction::At(&code[i]); |
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: |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 31 | CHECK(false); |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 32 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 33 | i += inst->SizeInCodeUnits(); |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | private: |
| 38 | // Specific handlers for each instruction. |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 39 | #define INSTRUCTION_VISITOR(o, cname, p, f, r, i, a, v) \ |
| 40 | void Do_ ## cname(const Instruction* inst) { \ |
| 41 | T* derived = static_cast<T*>(this); \ |
| 42 | derived->Do_Default(inst); \ |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 43 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 44 | #include "dex_instruction_list.h" |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 45 | DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR) |
| 46 | #undef DEX_INSTRUCTION_LIST |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 47 | #undef INSTRUCTION_VISITOR |
| 48 | |
| 49 | // The default instruction handler. |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 50 | void Do_Default(const Instruction* inst) { |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | }; |
| 54 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 55 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 56 | } // namespace art |
| 57 | |
| 58 | #endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_ |