blob: 795b95bf76f7e435e8b6e7ae1cd2bc314f679b94 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro12eb78e2011-06-24 14:51:06 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_
18#define ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_
Carl Shapiro12eb78e2011-06-24 14:51:06 -070019
Elliott Hughes76160052012-12-12 16:31:20 -080020#include "base/macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021#include "dex_instruction.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070022
23namespace art {
24
25template<typename T>
26class DexInstructionVisitor {
27 public:
Ian Rogersd81871c2011-10-03 13:57:23 -070028 void Visit(const uint16_t* code, size_t size_in_bytes) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070029 T* derived = static_cast<T*>(this);
Ian Rogersd81871c2011-10-03 13:57:23 -070030 size_t size_in_code_units = size_in_bytes / sizeof(uint16_t);
31 size_t i = 0;
32 while (i < size_in_code_units) {
33 const Instruction* inst = Instruction::At(&code[i]);
Carl Shapiro12eb78e2011-06-24 14:51:06 -070034 switch (inst->Opcode()) {
jeffhaoba5ebb92011-08-25 17:24:37 -070035#define INSTRUCTION_CASE(o, cname, p, f, r, i, a, v) \
36 case Instruction::cname: { \
37 derived->Do_ ## cname(inst); \
38 break; \
Carl Shapiro12eb78e2011-06-24 14:51:06 -070039 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070040#include "dex_instruction_list.h"
Carl Shapiro12eb78e2011-06-24 14:51:06 -070041 DEX_INSTRUCTION_LIST(INSTRUCTION_CASE)
Carl Shapirod84f49c2011-06-29 00:27:46 -070042#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070043#undef INSTRUCTION_CASE
44 default:
Elliott Hughesf5a7a472011-10-07 14:31:02 -070045 CHECK(false);
Carl Shapiro12eb78e2011-06-24 14:51:06 -070046 }
Ian Rogersd81871c2011-10-03 13:57:23 -070047 i += inst->SizeInCodeUnits();
Carl Shapiro12eb78e2011-06-24 14:51:06 -070048 }
49 }
50
51 private:
52 // Specific handlers for each instruction.
jeffhaoba5ebb92011-08-25 17:24:37 -070053#define INSTRUCTION_VISITOR(o, cname, p, f, r, i, a, v) \
54 void Do_ ## cname(const Instruction* inst) { \
55 T* derived = static_cast<T*>(this); \
56 derived->Do_Default(inst); \
Carl Shapiro744ad052011-08-06 15:53:36 -070057 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070058#include "dex_instruction_list.h"
Carl Shapirod84f49c2011-06-29 00:27:46 -070059 DEX_INSTRUCTION_LIST(INSTRUCTION_VISITOR)
60#undef DEX_INSTRUCTION_LIST
Carl Shapiro12eb78e2011-06-24 14:51:06 -070061#undef INSTRUCTION_VISITOR
62
63 // The default instruction handler.
Ian Rogers1b09b092012-08-20 15:35:52 -070064 void Do_Default(const Instruction*) {
Carl Shapiro12eb78e2011-06-24 14:51:06 -070065 return;
66 }
67};
68
jeffhaoba5ebb92011-08-25 17:24:37 -070069
Carl Shapiro12eb78e2011-06-24 14:51:06 -070070} // namespace art
71
Brian Carlstromfc0e3212013-07-17 14:40:12 -070072#endif // ART_RUNTIME_DEX_INSTRUCTION_VISITOR_H_