blob: ff45a4c8b72d18587f5b39454083448362a99e3b [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 }
Carl Shapirod84f49c2011-06-29 00:27:46 -070026#include "src/dex_instruction_list.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070027 DEX_INSTRUCTION_LIST(INSTRUCTION_CASE)
Carl Shapirod84f49c2011-06-29 00:27:46 -070028#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070029#undef INSTRUCTION_CASE
30 default:
31 CHECK(true);
32 }
33 ptr += inst->Size();
buzbeea7ab79d2011-06-28 16:07:35 -070034 CHECK_LE(ptr, end);
Carl Shapiro12eb78e2011-06-24 14:51:06 -070035 }
36 }
37
38 private:
39 // Specific handlers for each instruction.
40#define INSTRUCTION_VISITOR(cname, value) \
41 void Do_ ## cname(Instruction* inst) { \
42 T* derived = static_cast<T*>(this); \
43 derived->Do_Default(inst); \
44 };
Carl Shapirod84f49c2011-06-29 00:27:46 -070045#include "src/dex_instruction_list.h"
46 DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR)
47#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070048#undef INSTRUCTION_VISITOR
49
50 // The default instruction handler.
51 void Do_Default(Instruction* inst) {
52 return;
53 }
54};
55
56} // namespace art
57
58#endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_