blob: 2e1041d3c956dc1f95aea116f1aa8d204b6606c5 [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:
Ian Rogersd81871c2011-10-03 13:57:23 -070014 void Visit(const uint16_t* code, size_t size_in_bytes) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070015 T* derived = static_cast<T*>(this);
Ian Rogersd81871c2011-10-03 13:57:23 -070016 size_t size_in_code_units = size_in_bytes / sizeof(uint16_t);
17 size_t i = 0;
18 while (i < size_in_code_units) {
19 const Instruction* inst = Instruction::At(&code[i]);
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:
Elliott Hughesf5a7a472011-10-07 14:31:02 -070031 CHECK(false);
Carl Shapiro12eb78e2011-06-24 14:51:06 -070032 }
Ian Rogersd81871c2011-10-03 13:57:23 -070033 i += inst->SizeInCodeUnits();
Carl Shapiro12eb78e2011-06-24 14:51:06 -070034 }
35 }
36
37 private:
38 // Specific handlers for each instruction.
jeffhaoba5ebb92011-08-25 17:24:37 -070039#define INSTRUCTION_VISITOR(o, cname, p, f, r, i, a, v) \
40 void Do_ ## cname(const Instruction* inst) { \
41 T* derived = static_cast<T*>(this); \
42 derived->Do_Default(inst); \
Carl Shapiro744ad052011-08-06 15:53:36 -070043 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070044#include "dex_instruction_list.h"
Carl Shapirod84f49c2011-06-29 00:27:46 -070045 DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR)
46#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070047#undef INSTRUCTION_VISITOR
48
49 // The default instruction handler.
jeffhaoba5ebb92011-08-25 17:24:37 -070050 void Do_Default(const Instruction* inst) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070051 return;
52 }
53};
54
jeffhaoba5ebb92011-08-25 17:24:37 -070055
Carl Shapiro12eb78e2011-06-24 14:51:06 -070056} // namespace art
57
58#endif // ART_SRC_DEX_INSTRUCTION_VISITOR_H_