CACAO
PassManager.hpp
Go to the documentation of this file.
1 /* src/vm/jit/compiler2/PassManager.hpp - PassManager
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_PASSMANAGER
26 #define _JIT_COMPILER2_PASSMANAGER
27 
31 
32 #include "toolbox/logging.hpp"
33 
34 // NOTE: the register stuff can not use LOG* macros because comman line
35 // arguments are not parsed at registration
36 #if 0
37 #define MYLOG(EXPR) do { dbg() << EXPR; } while(0)
38 #else
39 #define MYLOG(EXPR)
40 #endif
41 
42 namespace cacao {
43 namespace jit {
44 namespace compiler2 {
45 
46 class JITData;
47 class Pass;
48 
49 
50 class PassInfo {
51 public:
52  typedef void * IDTy;
53  typedef Pass* (*ConstructorTy)();
54 private:
55  const char *const name;
56  /// Constructor function pointer
58 public:
60  PassInfo(const char* name, PassInfo::IDTy ID, ConstructorTy ctor) : name(name), ctor(ctor), ID(ID) {}
61  const char* get_name() const {
62  return name;
63  }
64  Pass* create_Pass() const {
65  return ctor();
66  }
67 };
68 
69 /**
70  * Manage the execution of compiler passes
71  *
72  * @todo handle modified (timestamp?)
73  * @todo conditionally reevaluate
74  */
75 class PassManager {
76 public:
82 private:
83  /**
84  * This stores the initialized passes.
85  * Every Pass can only occur once.
86  */
88  /**
89  * This variable contains a schedule of the passes.
90  * A pass may occur more than once.
91  */
93  /**
94  * The list of passed that should be performed
95  */
97  /**
98  * Map of ready results
99  */
101 
104  return registered_passes;
105  }
106 
108 
109  template<class _PassClass>
110  _PassClass* get_Pass_result() {
111  assert_msg(result_ready[&_PassClass::ID], "result for "
112  << get_Pass_name(&_PassClass::ID) << " is not ready!");
113  return (_PassClass*)initialized_passes[&_PassClass::ID];
114  }
115  void schedulePasses();
116 public:
117  const char * get_Pass_name(PassInfo::IDTy ID) {
118  PassInfo *PI = registered_passes()[ID];
119  assert(PI && "Pass not registered");
120  return PI->get_name();
121  }
123  MYLOG("PassManager::PassManager()" << nl);
124  }
125 
126  ~PassManager();
127 
128  /**
129  * DO NOT CALL THIS MANUALLY. ONLY INVOKE VIA RegisterPass.
130  */
131  static void register_Pass(PassInfo *PI) {
132  MYLOG("PassManager::register_Pass: " << PI->get_name() << nl);
133  registered_passes().insert(std::make_pair(PI->ID,PI));
134  }
135 
136  /**
137  * run pass initializers
138  */
139  void initializePasses();
140 
141  /**
142  * run passes
143  */
144  void runPasses(JITData &JD);
145 
146  /**
147  * run pass finalizers
148  */
149  void finalizePasses();
150 
151  /**
152  * add a compiler pass
153  */
154  template<class _PassClass>
155  void add_Pass() {
156  PassInfo::IDTy ID = &_PassClass::ID;
157  assert(registered_passes()[ID] && "Pass not registered");
158  passes.insert(ID);
159  schedule.push_back(ID);
160  }
161 
166 
167  friend class Pass;
168 
169 };
170 
171 template<class _PassClass>
172 Pass *call_ctor() { return new _PassClass(); }
173 
174 template <class _PassClass>
175 struct PassRegistry : public PassInfo {
176  PassRegistry(const char * name) : PassInfo(name, &_PassClass::ID, (PassInfo::ConstructorTy)call_ctor<_PassClass>) {
178  }
179 };
180 
181 #undef MYLOG
182 
183 } // end namespace cacao
184 } // end namespace jit
185 } // end namespace compiler2
186 
187 #endif /* _JIT_COMPILER2_PASSMANAGER */
188 
189 
190 /*
191  * These are local overrides for various environment variables in Emacs.
192  * Please do not remove this and leave it at the end of the file, where
193  * Emacs will automagically detect them.
194  * ---------------------------------------------------------------------
195  * Local variables:
196  * mode: c++
197  * indent-tabs-mode: t
198  * c-basic-offset: 4
199  * tab-width: 4
200  * End:
201  * vim:noexpandtab:sw=4:ts=4:
202  */
void finalizePasses()
run pass finalizers
Pass superclass All compiler passes should inheritate this class.
Definition: Pass.hpp:48
alloc::unordered_map< PassInfo::IDTy, bool >::type ResultReadyMapTy
Definition: PassManager.hpp:80
alloc::unordered_set< PassInfo::IDTy >::type PassListTy
Definition: PassManager.hpp:77
alloc::unordered_map< PassInfo::IDTy, PassInfo * >::type PassInfoMapTy
Definition: PassManager.hpp:81
PassInfo(const char *name, PassInfo::IDTy ID, ConstructorTy ctor)
Definition: PassManager.hpp:60
PassMapTy initialized_passes
This stores the initialized passes.
Definition: PassManager.hpp:87
_Base::const_iterator const_iterator
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
PassInfoMapTy::const_iterator registered_begin() const
Pass * get_initialized_Pass(PassInfo::IDTy ID)
Definition: PassManager.cpp:62
PassMapTy::const_iterator initialized_end() const
void initializePasses()
run pass initializers
Definition: PassManager.cpp:88
std::vector< T, Allocator< T > > type
Definition: vector.hpp:38
static PassInfoMapTy & registered_passes()
PassInfoMapTy::const_iterator registered_end() const
Manage the execution of compiler passes.
Definition: PassManager.hpp:75
alloc::vector< PassInfo::IDTy >::type ScheduleListTy
Definition: PassManager.hpp:78
#define MYLOG(EXPR)
Definition: PassManager.hpp:39
const char * get_Pass_name(PassInfo::IDTy ID)
void add_Pass()
add a compiler pass
static void register_Pass(PassInfo *PI)
DO NOT CALL THIS MANUALLY.
PassMapTy::const_iterator initialized_begin() const
void runPasses(JITData &JD)
run passes
Definition: PassManager.cpp:91
const char * get_name() const
Definition: PassManager.hpp:61
#define assert_msg(COND, EXPR)
Definition: logging.hpp:107
ConstructorTy ctor
Constructor function pointer.
Definition: PassManager.hpp:57
ResultReadyMapTy result_ready
Map of ready results.
PassListTy passes
The list of passed that should be performed.
Definition: PassManager.hpp:96
Nl nl
Definition: OStream.cpp:56
alloc::unordered_map< PassInfo::IDTy, Pass * >::type PassMapTy
Definition: PassManager.hpp:79
ScheduleListTy schedule
This variable contains a schedule of the passes.
Definition: PassManager.hpp:92