blob: 6a6bc3eb25ef0e415338f9c6aa84598775857b43 [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "dex_instruction.h"
7#include "macros.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -07008
9namespace art {
10
11template<typename T>
12class DexInstructionVisitor {
13 public:
jeffhaoba5ebb92011-08-25 17:24:37 -070014 void Visit(const uint16_t* code, size_t size) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070015 T* derived = static_cast<T*>(this);
jeffhaoba5ebb92011-08-25 17:24:37 -070016 const byte* ptr = reinterpret_cast<const byte*>(code);
17 const byte* end = ptr + size;
Carl Shapiro12eb78e2011-06-24 14:51:06 -070018 while (ptr != end) {
jeffhaoba5ebb92011-08-25 17:24:37 -070019 const Instruction* inst = Instruction::At(ptr);
Carl Shapiro12eb78e2011-06-24 14:51:06 -070020 switch (inst->Opcode()) {
jeffhaoba5ebb92011-08-25 17:24:37 -070021#define INSTRUCTION_CASE(o, cname, p, f, r, i, a, v) \
22 case Instruction::cname: { \
23 derived->Do_ ## cname(inst); \
24 break; \
Carl Shapiro12eb78e2011-06-24 14:51:06 -070025 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "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 }
jeffhaoba5ebb92011-08-25 17:24:37 -070033 ptr += inst->Size() * sizeof(uint16_t);
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.
jeffhaoba5ebb92011-08-25 17:24:37 -070040#define INSTRUCTION_VISITOR(o, cname, p, f, r, i, a, v) \
41 void Do_ ## cname(const Instruction* inst) { \
42 T* derived = static_cast<T*>(this); \
43 derived->Do_Default(inst); \
Carl Shapiro744ad052011-08-06 15:53:36 -070044 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070045#include "dex_instruction_list.h"
Carl Shapirod84f49c2011-06-29 00:27:46 -070046 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.
jeffhaoba5ebb92011-08-25 17:24:37 -070051 void Do_Default(const Instruction* inst) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070052 return;
53 }
54};
55
jeffhaoba5ebb92011-08-25 17:24:37 -070056
Carl Shapiro12eb78e2011-06-24 14:51:06 -070057} // namespace art
58
59#endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_