1/*
2 * Copyright (C) 2014 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 */
16
17#ifndef ART_COMPILER_DEX_PASS_H_
18#define ART_COMPILER_DEX_PASS_H_
19
20#include <string>
21
22#include "base/macros.h"
23namespace art {
24
25// Forward declarations.
26struct BasicBlock;
27struct CompilationUnit;
28class Pass;
29
30// Empty Pass Data Class, can be extended by any pass extending the base Pass class.
31class PassDataHolder {
32};
33
34/**
35 * @class Pass
36 * @brief Base Pass class, can be extended to perform a more defined way of doing the work call.
37 */
38class Pass {
39 public:
40  explicit Pass(const char* name)
41    : pass_name_(name) {
42  }
43
44  virtual ~Pass() {
45  }
46
47  virtual const char* GetName() const {
48    return pass_name_;
49  }
50
51  /**
52   * @brief Gate for the pass: determines whether to execute the pass or not considering a CompilationUnit
53   * @param data the PassDataHolder.
54   * @return whether or not to execute the pass.
55   */
56  virtual bool Gate(const PassDataHolder* data) const {
57    // Unused parameter.
58    UNUSED(data);
59
60    // Base class says yes.
61    return true;
62  }
63
64  /**
65   * @brief Start of the pass: called before the Worker function.
66   */
67  virtual void Start(PassDataHolder* data) const {
68    // Unused parameter.
69    UNUSED(data);
70  }
71
72  /**
73   * @brief End of the pass: called after the WalkBasicBlocks function.
74   */
75  virtual void End(PassDataHolder* data) const {
76    // Unused parameter.
77    UNUSED(data);
78  }
79
80  /**
81   * @param data the object containing data necessary for the pass.
82   * @return whether or not there is a change when walking the BasicBlock
83   */
84  virtual bool Worker(const PassDataHolder* data) const {
85    // Unused parameter.
86    UNUSED(data);
87
88    // BasicBlock did not change.
89    return false;
90  }
91
92  static void BasePrintMessage(CompilationUnit* c_unit, const char* pass_name, const char* message, ...) {
93    // Check if we want to log something or not.
94    if (c_unit->print_pass) {
95      // Stringify the message.
96      va_list args;
97      va_start(args, message);
98      std::string stringified_message;
99      StringAppendV(&stringified_message, message, args);
100      va_end(args);
101
102      // Log the message and ensure to include pass name.
103      LOG(INFO) << pass_name << ": " << stringified_message;
104    }
105  }
106
107 protected:
108  /** @brief The pass name: used for searching for a pass when running a particular pass or debugging. */
109  const char* const pass_name_;
110
111 private:
112  // In order to make the all passes not copy-friendly.
113  DISALLOW_COPY_AND_ASSIGN(Pass);
114};
115}  // namespace art
116#endif  // ART_COMPILER_DEX_PASS_H_
117