blob: 1a8325c1301568949ce82f1695755ee1dcde4ee2 [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();
32 }
33 }
34
35 private:
36 // Specific handlers for each instruction.
37#define INSTRUCTION_VISITOR(cname, value) \
38 void Do_ ## cname(Instruction* inst) { \
39 T* derived = static_cast<T*>(this); \
40 derived->Do_Default(inst); \
41 };
42 DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR);
43#undef INSTRUCTION_VISITOR
44
45 // The default instruction handler.
46 void Do_Default(Instruction* inst) {
47 return;
48 }
49};
50
51} // namespace art
52
53#endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_