blob: 172fc4f3f39f401fdda7134ec0c19f8dc22b3639 [file] [log] [blame]
Carl Shapiro12eb78e2011-06-24 14:51:06 -07001// 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
9namespace art {
10
11template<typename T>
12class 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()) {
21#define INSTRUCTION_CASE(cname, value) \
22 case Instruction::cname: { \
23 derived->Do_ ## cname(inst); \
24 break; \
25 }
26 DEX_INSTRUCTION_LIST(INSTRUCTION_CASE)
27#undef INSTRUCTION_CASE
28 default:
29 CHECK(true);
30 }
31 ptr += inst->Size();
buzbeea7ab79d2011-06-28 16:07:35 -070032 CHECK_LE(ptr, end);
Carl Shapiro12eb78e2011-06-24 14:51:06 -070033 }
34 }
35
36 private:
37 // Specific handlers for each instruction.
38#define INSTRUCTION_VISITOR(cname, value) \
39 void Do_ ## cname(Instruction* inst) { \
40 T* derived = static_cast<T*>(this); \
41 derived->Do_Default(inst); \
42 };
43 DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR);
44#undef INSTRUCTION_VISITOR
45
46 // The default instruction handler.
47 void Do_Default(Instruction* inst) {
48 return;
49 }
50};
51
52} // namespace art
53
54#endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_