Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_ |
| 18 | #define ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 20 | #include "base/macros.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 21 | #include "dex_instruction.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | template<typename T> |
| 26 | class DexInstructionVisitor { |
| 27 | public: |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 28 | void Visit(const uint16_t* code, size_t size_in_bytes) { |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 29 | T* derived = static_cast<T*>(this); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 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]); |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 34 | switch (inst->Opcode()) { |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 35 | #define INSTRUCTION_CASE(o, cname, p, f, r, i, a, v) \ |
| 36 | case Instruction::cname: { \ |
| 37 | derived->Do_ ## cname(inst); \ |
| 38 | break; \ |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 39 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 40 | #include "dex_instruction_list.h" |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 41 | DEX_INSTRUCTION_LIST(INSTRUCTION_CASE) |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 42 | #undef DEX_INSTRUCTION_LIST |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 43 | #undef INSTRUCTION_CASE |
| 44 | default: |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 45 | CHECK(false); |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 46 | } |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 47 | i += inst->SizeInCodeUnits(); |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | // Specific handlers for each instruction. |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 53 | #define INSTRUCTION_VISITOR(o, cname, p, f, r, i, a, v) \ |
| 54 | void Do_ ## cname(const Instruction* inst) { \ |
| 55 | T* derived = static_cast<T*>(this); \ |
| 56 | derived->Do_Default(inst); \ |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 57 | } |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 58 | #include "dex_instruction_list.h" |
Carl Shapiro | d84f49c | 2011-06-29 00:27:46 -0700 | [diff] [blame] | 59 | DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR) |
| 60 | #undef DEX_INSTRUCTION_LIST |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 61 | #undef INSTRUCTION_VISITOR |
| 62 | |
| 63 | // The default instruction handler. |
Ian Rogers | 1b09b09 | 2012-08-20 15:35:52 -0700 | [diff] [blame] | 64 | void Do_Default(const Instruction*) { |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | }; |
| 68 | |
jeffhao | ba5ebb9 | 2011-08-25 17:24:37 -0700 | [diff] [blame] | 69 | |
Carl Shapiro | 12eb78e | 2011-06-24 14:51:06 -0700 | [diff] [blame] | 70 | } // namespace art |
| 71 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 72 | #endif // ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_ |