blob: 2b03e2d3320d7a87909c0f9af020a9a701ba7fd1 [file] [log] [blame]
Reid Spencerdac69c82004-06-07 17:53:43 +00001//===-- BytecodeHandler.h - Parsing Handler ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header file defines the BytecodeHandler class that gets called by the
11// AbstractBytecodeParser when parsing events occur.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef BYTECODE_HANDLER_H
16#define BYTECODE_HANDLER_H
17
18#include "llvm/Module.h"
19#include "llvm/GlobalValue.h"
20#include <vector>
21
22namespace llvm {
23
24class ArrayType;
25class StructType;
26class PointerType;
27class ConstantArray;
28
29/// This class provides the interface for the handling bytecode events during
30/// parsing. The methods on this interface are invoked by the
31/// AbstractBytecodeParser as it discovers the content of a bytecode stream.
32/// This class provides a a clear separation of concerns between recognizing
33/// the semantic units of a bytecode file and deciding what to do with them.
34/// The AbstractBytecodeParser recognizes the content of the bytecode file and
35/// calls the BytecodeHandler methods to determine what should be done. This
36/// arrangement allows Bytecode files to be read and handled for a number of
37/// purposes simply by creating a subclass of BytecodeHandler. None of the
38/// parsing details need to be understood, only the meaning of the calls
39/// made on this interface.
40///
41/// Another paradigm that uses this design pattern is the XML SAX Parser. The
42/// ContentHandler for SAX plays the same role as the BytecodeHandler here.
43/// @brief Handle Bytecode Parsing Events
44class BytecodeHandler {
45
46/// @name Constructors And Operators
47/// @{
48public:
49 /// @brief Default constructor (empty)
50 BytecodeHandler() {}
51 /// @brief Virtual destructor (empty)
52 virtual ~BytecodeHandler() {}
53
54private:
55 BytecodeHandler(const BytecodeHandler &); // DO NOT IMPLEMENT
56 void operator=(const BytecodeHandler &); // DO NOT IMPLEMENT
57
58/// @}
59/// @name Handler Methods
60/// @{
61public:
62
63 /// This method is called whenever the parser detects an error in the
64 /// bytecode formatting. Returning true will cause the parser to keep
65 /// going, however this is inadvisable in most cases. Returning false will
66 /// cause the parser to throw the message as a std::string.
67 /// @brief Handle parsing errors.
68 virtual bool handleError(const std::string& str );
69
70 /// This method is called at the beginning of a parse before anything is
71 /// read in order to give the handler a chance to initialize.
72 /// @brief Handle the start of a bytecode parse
73 virtual void handleStart();
74
75 /// This method is called at the end of a parse after everything has been
76 /// read in order to give the handler a chance to terminate.
77 /// @brief Handle the end of a bytecode parse
78 virtual void handleFinish();
79
80 /// This method is called at the start of a module to indicate that a
81 /// module is being parsed.
82 /// @brief Handle the start of a module.
83 virtual void handleModuleBegin(const std::string& id);
84
85 /// This method is called at the end of a module to indicate that the module
86 /// previously being parsed has concluded.
87 /// @brief Handle the end of a module.
88 virtual void handleModuleEnd(const std::string& id);
89
90 /// This method is called once the version information has been parsed. It
91 /// provides the information about the version of the bytecode file being
92 /// read.
93 /// @brief Handle the bytecode prolog
94 virtual void handleVersionInfo(
95 unsigned char RevisionNum, ///< Byte code revision number
96 Module::Endianness Endianness, ///< Endianness indicator
97 Module::PointerSize PointerSize ///< PointerSize indicator
98 );
99
100 /// This method is called at the start of a module globals block which
101 /// contains the global variables and the function placeholders
102 virtual void handleModuleGlobalsBegin();
103
104 /// This method is called when a non-initialized global variable is
105 /// recognized. Its type, constness, and linkage type are provided.
106 /// @brief Handle a non-initialized global variable
107 virtual void handleGlobalVariable(
108 const Type* ElemType, ///< The type of the global variable
109 bool isConstant, ///< Whether the GV is constant or not
110 GlobalValue::LinkageTypes ///< The linkage type of the GV
111 );
112
113 /// This method is called when an initialized global variable is recognized.
114 /// Its type constness, linkage type, and the slot number of the initializer
115 /// are provided.
116 /// @brief Handle an intialized global variable.
117 virtual void handleInitializedGV(
118 const Type* ElemType, ///< The type of the global variable
119 bool isConstant, ///< Whether the GV is constant or not
120 GlobalValue::LinkageTypes,///< The linkage type of the GV
121 unsigned initSlot ///< Slot number of GV's initializer
122 );
123
124 /// This method is called when a new type is recognized. The type is
125 /// converted from the bytecode and passed to this method.
126 /// @brief Handle a type
127 virtual void handleType( const Type* Ty );
128
129 /// This method is called when the function prototype for a function is
130 /// encountered in the module globals block.
131 virtual void handleFunctionDeclaration(
132 const Type* FuncType ///< The type of the function
133 );
134
135 /// This method is called at the end of the module globals block.
136 /// @brief Handle end of module globals block.
137 virtual void handleModuleGlobalsEnd();
138
139 /// This method is called at the beginning of a compaction table.
140 /// @brief Handle start of compaction table.
141 virtual void handleCompactionTableBegin();
142 virtual void handleCompactionTablePlane(
143 unsigned Ty,
144 unsigned NumEntries
145 );
146
147 virtual void handleCompactionTableType(
148 unsigned i,
149 unsigned TypSlot,
150 const Type*
151 );
152
153 virtual void handleCompactionTableValue(
154 unsigned i,
155 unsigned ValSlot,
156 const Type*
157 );
158
159 virtual void handleCompactionTableEnd();
160
161 virtual void handleSymbolTableBegin();
162
163 virtual void handleSymbolTablePlane(
164 unsigned Ty,
165 unsigned NumEntries,
166 const Type* Ty
167 );
168
169 virtual void handleSymbolTableType(
170 unsigned i,
171 unsigned slot,
172 const std::string& name
173 );
174
175 virtual void handleSymbolTableValue(
176 unsigned i,
177 unsigned slot,
178 const std::string& name
179 );
180
181 virtual void handleSymbolTableEnd();
182
183 virtual void handleFunctionBegin(
184 const Type* FType,
185 GlobalValue::LinkageTypes linkage
186 );
187
188 virtual void handleFunctionEnd(
189 const Type* FType
190 );
191
192 virtual void handleBasicBlockBegin(
193 unsigned blocknum
194 );
195
196 /// This method is called for each instruction that is parsed.
197 /// @returns true if the instruction is a block terminating instruction
198 /// @brief Handle an instruction
199 virtual bool handleInstruction(
200 unsigned Opcode,
201 const Type* iType,
202 std::vector<unsigned>& Operands
203 );
204
205 /// This method is called for each block that is parsed.
206 virtual void handleBasicBlockEnd(unsigned blocknum);
207 /// This method is called at the start of the global constants block.
208 /// @brief Handle start of global constants block.
209 virtual void handleGlobalConstantsBegin();
210
211 virtual void handleConstantExpression(
212 unsigned Opcode,
213 const Type* Typ,
214 std::vector<std::pair<const Type*,unsigned> > ArgVec
215 );
216
217 virtual void handleConstantArray(
218 const ArrayType* AT,
219 std::vector<unsigned>& ElementSlots
220 );
221
222 virtual void handleConstantStruct(
223 const StructType* ST,
224 std::vector<unsigned>& ElementSlots
225 );
226
227 virtual void handleConstantPointer(
228 const PointerType* PT,
229 unsigned Slot
230 );
231
232 virtual void handleConstantString(
233 const ConstantArray* CA
234 );
235
236 virtual void handleConstantValue( Constant * c );
237 virtual void handleGlobalConstantsEnd();
238
239/// @}
240
241};
242
243} // End llvm namespace
244
245#endif
246
247// vim: sw=2