CACAO
Pass.hpp
Go to the documentation of this file.
1 /* src/vm/jit/compiler2/Pass.hpp - Pass
2 
3  Copyright (C) 2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 #ifndef _JIT_COMPILER2_PASS
26 #define _JIT_COMPILER2_PASS
27 
30 #include <cstddef>
31 
32 MM_MAKE_NAME(Pass)
33 
34 namespace cacao {
35 namespace jit {
36 namespace compiler2 {
37 
38 // forward declaration
39 class PassUsage;
40 class JITData;
41 
42 /**
43  * Pass superclass
44  * All compiler passes should inheritate this class.
45  * TODO: more info
46  */
47 class Pass {
48 private:
50 
52  bool allowed_to_use_result(const PassInfo::IDTy &id) const;
53 public:
54  Pass() : pm(NULL) {}
55 
57  pm = pr;
58  }
59 
60  /**
61  * This template will return a unique ID for each type that it is called with.
62  *
63  * This is needed since Pass IDs are accessed statically via a concrete Pass type
64  * and not via a concrete instance.
65  */
66  template<class T>
67  static PassInfo::IDTy ID() {
68  static PassInfo::IDTy ID = id_counter++;
69  return ID;
70  }
71 
72  /**
73  * Get the result of a previous compiler pass
74  *
75  * Can only be used if ResultType is added to required in get_PassUsage().
76  */
77  template<class _PassClass>
78  _PassClass *get_Pass() const {
79  if (!allowed_to_use_result(_PassClass::template ID<_PassClass>())) {
80  assert(0 && "Not allowed to get result (not declared in get_PassUsage())");
81  return NULL;
82  }
83  return pm->get_Pass_result<_PassClass>();
84  }
85 
86  /**
87  * Get the result of a previous compiler pass
88  *
89  * Can only be used if ResultType is added to required in get_PassUsage().
90  */
91  template<class _PassClass>
92  _PassClass *get_Pass_if_available() const {
93  if (!pm->result_ready[_PassClass::template ID<_PassClass>()])
94  return NULL;
95  return pm->get_Pass_result<_PassClass>();
96  }
97  /**
98  * Set the requirements for the pass
99  */
100  virtual PassUsage& get_PassUsage(PassUsage &PU) const {
101  // default: require nothing, destroy nothing
102  return PU;
103  }
104 
105  /**
106  * Allows concrete passes to enable/disable themselves the way they like
107  */
108  virtual bool is_enabled() const {
109  return true;
110  }
111 
112  /**
113  * Initialize the Pass.
114  * This method is called by the PassManager before the pass is started. It should be used to
115  * initialize e.g. data structures. A Pass object might be reused so the construtor can not be
116  * used in some cases.
117  */
118  virtual void initialize() {}
119 
120  /**
121  * Finalize the Pass.
122  * This method is called by the PassManager after the pass is no longer used. It should be used to
123  * clean up stuff done in initialize().
124  */
125  virtual void finalize() {}
126 
127  /**
128  * Run the Pass.
129  * This method implements the compiler pass.
130  *
131  * @return false if a problem occurred, true otherwise
132  */
133  virtual bool run(JITData &JD) = 0;
134 
135  /**
136  * Verify the Result.
137  * This method is used to verify the result of the pass. It has the same motivation
138  * than the assert() statement. It should be only used for debugging purposes
139  * and might not be called in release builds.
140  */
141  virtual bool verify() const { return true; }
142 
143  /**
144  * Destructor
145  */
146  virtual ~Pass() {}
147 };
148 
149 
150 } // end namespace compiler2
151 } // end namespace jit
152 } // end namespace cacao
153 
154 #endif /* _JIT_COMPILER2_PASS */
155 
156 
157 /*
158  * These are local overrides for various environment variables in Emacs.
159  * Please do not remove this and leave it at the end of the file, where
160  * Emacs will automagically detect them.
161  * ---------------------------------------------------------------------
162  * Local variables:
163  * mode: c++
164  * indent-tabs-mode: t
165  * c-basic-offset: 4
166  * tab-width: 4
167  * End:
168  * vim:noexpandtab:sw=4:ts=4:
169  */
Pass superclass All compiler passes should inheritate this class.
Definition: Pass.hpp:47
Each instance of PassRunner represents a single run of the compiler2.
static PassInfo::IDTy ID()
This template will return a unique ID for each type that it is called with.
Definition: Pass.hpp:67
virtual PassUsage & get_PassUsage(PassUsage &PU) const
Set the requirements for the pass.
Definition: Pass.hpp:100
#define MM_MAKE_NAME(x)
Definition: memstats.hpp:128
virtual void finalize()
Finalize the Pass.
Definition: Pass.hpp:125
_PassClass * get_Pass() const
Get the result of a previous compiler pass.
Definition: Pass.hpp:78
static PassInfo::IDTy id_counter
Definition: Pass.hpp:49
Stores the interdependencies of a pass.
Definition: PassUsage.hpp:55
virtual bool is_enabled() const
Allows concrete passes to enable/disable themselves the way they like.
Definition: Pass.hpp:108
virtual bool verify() const
Verify the Result.
Definition: Pass.hpp:141
void set_PassRunner(PassRunner *pr)
Definition: Pass.hpp:56
virtual ~Pass()
Destructor.
Definition: Pass.hpp:146
_PassClass * get_Pass_if_available() const
Get the result of a previous compiler pass.
Definition: Pass.hpp:92
virtual void initialize()
Initialize the Pass.
Definition: Pass.hpp:118