1 //===-- BytecodeHandler.h - Handle Bytecode Parsing Events ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header file defines the interface to the Bytecode Handler. The handler
11 // is called by the Bytecode Reader to obtain out-of-band parsing events for
12 // tasks other then LLVM IR construction.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_BYTECODE_BYTECODEHANDLER_H
17 #define LLVM_BYTECODE_BYTECODEHANDLER_H
19 #include "llvm/Module.h"
30 /// This class provides the interface for handling bytecode events during
31 /// reading of bytecode. The methods on this interface are invoked by the
32 /// BytecodeReader as it discovers the content of a bytecode stream.
33 /// This class provides a a clear separation of concerns between recognizing
34 /// the semantic units of a bytecode file (the Reader) and deciding what to do
35 /// with them (the Handler).
37 /// The BytecodeReader recognizes the content of the bytecode file and
38 /// calls the BytecodeHandler methods to let it perform additional tasks. This
39 /// arrangement allows Bytecode files to be read and handled for a number of
40 /// purposes simply by creating a subclass of BytecodeHandler. None of the
41 /// parsing details need to be understood, only the meaning of the calls
42 /// made on this interface.
44 /// @see BytecodeHandler
45 /// @brief Handle Bytecode Parsing Events
46 class BytecodeHandler {
48 /// @name Constructors And Operators
51 /// @brief Default constructor (empty)
53 /// @brief Virtual destructor (empty)
54 virtual ~BytecodeHandler();
57 BytecodeHandler(const BytecodeHandler &); // DO NOT IMPLEMENT
58 void operator=(const BytecodeHandler &); // DO NOT IMPLEMENT
61 /// @name Handler Methods
65 /// This method is called whenever the parser detects an error in the
66 /// bytecode formatting. It gives the handler a chance to do something
67 /// with the error message before the parser throws an exception to
68 /// terminate the parsing.
69 /// @brief Handle parsing errors.
70 virtual void handleError(const std::string& str ) {}
72 /// This method is called at the beginning of a parse before anything is
73 /// read in order to give the handler a chance to initialize.
74 /// @brief Handle the start of a bytecode parse
75 virtual void handleStart( Module* Mod, unsigned byteSize ) {}
77 /// This method is called at the end of a parse after everything has been
78 /// read in order to give the handler a chance to terminate.
79 /// @brief Handle the end of a bytecode parse
80 virtual void handleFinish() {}
82 /// This method is called at the start of a module to indicate that a
83 /// module is being parsed.
84 /// @brief Handle the start of a module.
85 virtual void handleModuleBegin(const std::string& moduleId) {}
87 /// This method is called at the end of a module to indicate that the module
88 /// previously being parsed has concluded.
89 /// @brief Handle the end of a module.
90 virtual void handleModuleEnd(
91 const std::string& moduleId ///< An identifier for the module
94 /// This method is called once the version information has been parsed. It
95 /// provides the information about the version of the bytecode file being
97 /// @brief Handle the bytecode prolog
98 virtual void handleVersionInfo(
99 unsigned char RevisionNum, ///< Byte code revision number
100 Module::Endianness Endianness, ///< Endianness indicator
101 Module::PointerSize PointerSize ///< PointerSize indicator
104 /// This method is called at the start of a module globals block which
105 /// contains the global variables and the function placeholders
106 virtual void handleModuleGlobalsBegin() {}
108 /// This method is called when a non-initialized global variable is
109 /// recognized. Its type, constness, and linkage type are provided.
110 /// @brief Handle a non-initialized global variable
111 virtual void handleGlobalVariable(
112 const Type* ElemType, ///< The type of the global variable
113 bool isConstant, ///< Whether the GV is constant or not
114 GlobalValue::LinkageTypes,///< The linkage type of the GV
115 unsigned SlotNum, ///< Slot number of GV
116 unsigned initSlot ///< Slot number of GV's initializer (0 if none)
119 /// This method is called when a type list is recognized. It simply
120 /// provides the number of types that the list contains. The handler
121 /// should expect that number of calls to handleType.
122 /// @brief Handle a type
123 virtual void handleTypeList(
124 unsigned numEntries ///< The number of entries in the type list
127 /// This method is called when a new type is recognized. The type is
128 /// converted from the bytecode and passed to this method.
129 /// @brief Handle a type
130 virtual void handleType(
131 const Type* Ty ///< The type that was just recognized
134 /// This method is called when the function prototype for a function is
135 /// encountered in the module globals block.
136 virtual void handleFunctionDeclaration(
137 Function* Func ///< The function being declared
140 /// This method is called when a global variable is initialized with
141 /// its constant value. Because of forward referencing, etc. this is
142 /// done towards the end of the module globals block
143 virtual void handleGlobalInitializer(GlobalVariable*, Constant* ) {}
145 /// This method is called for each dependent library name found
146 /// in the module globals block.
147 virtual void handleDependentLibrary(const std::string& libName) {}
149 /// This method is called if the module globals has a non-empty target
151 virtual void handleTargetTriple(const std::string& triple) {}
153 /// This method is called at the end of the module globals block.
154 /// @brief Handle end of module globals block.
155 virtual void handleModuleGlobalsEnd() {}
157 /// This method is called at the beginning of a compaction table.
158 /// @brief Handle start of compaction table.
159 virtual void handleCompactionTableBegin() {}
161 /// @brief Handle start of a compaction table plane
162 virtual void handleCompactionTablePlane(
163 unsigned Ty, ///< The type of the plane (slot number)
164 unsigned NumEntries ///< The number of entries in the plane
167 /// @brief Handle a type entry in the compaction table
168 virtual void handleCompactionTableType(
169 unsigned i, ///< Index in the plane of this type
170 unsigned TypSlot, ///< Slot number for this type
171 const Type* ///< The type referenced by this slot
174 /// @brief Handle a value entry in the compaction table
175 virtual void handleCompactionTableValue(
176 unsigned i, ///< Index in the compaction table's type plane
177 unsigned TypSlot, ///< The slot (plane) of the type of this value
178 unsigned ValSlot ///< The global value slot of the value
181 /// @brief Handle end of a compaction table
182 virtual void handleCompactionTableEnd() {}
184 /// @brief Handle start of a symbol table
185 virtual void handleSymbolTableBegin(
186 Function* Func, ///< The function to which the ST belongs
187 SymbolTable* ST ///< The symbol table being filled
190 /// @brief Handle start of a symbol table plane
191 virtual void handleSymbolTablePlane(
192 unsigned TySlot, ///< The slotnum of the type plane
193 unsigned NumEntries, ///< Number of entries in the plane
194 const Type* Typ ///< The type of this type plane
197 /// @brief Handle a named type in the symbol table
198 virtual void handleSymbolTableType(
199 unsigned i, ///< The index of the type in this plane
200 unsigned slot, ///< Slot number of the named type
201 const std::string& name ///< Name of the type
204 /// @brief Handle a named value in the symbol table
205 virtual void handleSymbolTableValue(
206 unsigned i, ///< The index of the value in this plane
207 unsigned slot, ///< Slot number of the named value
208 const std::string& name ///< Name of the value.
211 /// @brief Handle the end of a symbol table
212 virtual void handleSymbolTableEnd() {}
214 /// @brief Handle the beginning of a function body
215 virtual void handleFunctionBegin(
216 Function* Func, ///< The function being defined
217 unsigned Size ///< The size (in bytes) of the function's bytecode
220 /// @brief Handle the end of a function body
221 virtual void handleFunctionEnd(
222 Function* Func ///< The function whose definition has just finished.
225 /// @brief Handle the beginning of a basic block
226 virtual void handleBasicBlockBegin(
227 unsigned blocknum ///< The block number of the block
230 /// This method is called for each instruction that is parsed.
231 /// @returns true if the instruction is a block terminating instruction
232 /// @brief Handle an instruction
233 virtual bool handleInstruction(
234 unsigned Opcode, ///< Opcode of the instruction
235 const Type* iType, ///< Instruction type
236 std::vector<unsigned>& Operands, ///< Vector of slot # operands
237 unsigned Length ///< Length of instruction in bc bytes
240 /// @brief Handle the end of a basic block
241 virtual void handleBasicBlockEnd(
242 unsigned blocknum ///< The block number of the block just finished
245 /// @brief Handle start of global constants block.
246 virtual void handleGlobalConstantsBegin() {}
248 /// @brief Handle a constant expression
249 virtual void handleConstantExpression(
250 unsigned Opcode, ///< Opcode of primary expression operator
251 std::vector<Constant*> ArgVec, ///< expression args
252 Constant* C ///< The constant value
255 /// @brief Handle a constant array
256 virtual void handleConstantArray(
257 const ArrayType* AT, ///< Type of the array
258 std::vector<Constant*>& ElementSlots,///< Slot nums for array values
259 unsigned TypeSlot, ///< Slot # of type
260 Constant* Val ///< The constant value
263 /// @brief Handle a constant structure
264 virtual void handleConstantStruct(
265 const StructType* ST, ///< Type of the struct
266 std::vector<Constant*>& ElementSlots,///< Slot nums for struct values
267 Constant* Val ///< The constant value
270 /// @brief Handle a constant packed
271 virtual void handleConstantPacked(
272 const PackedType* PT, ///< Type of the array
273 std::vector<Constant*>& ElementSlots,///< Slot nums for packed values
274 unsigned TypeSlot, ///< Slot # of type
275 Constant* Val ///< The constant value
278 /// @brief Handle a constant pointer
279 virtual void handleConstantPointer(
280 const PointerType* PT, ///< Type of the pointer
281 unsigned Slot, ///< Slot num of initializer value
282 GlobalValue* GV ///< Referenced global value
285 /// @brief Handle a constant strings (array special case)
286 virtual void handleConstantString(
287 const ConstantArray* CA ///< Type of the string array
290 /// @brief Handle a primitive constant value
291 virtual void handleConstantValue(
292 Constant * c ///< The constant just defined
295 /// @brief Handle the end of the global constants
296 virtual void handleGlobalConstantsEnd() {}
298 /// @brief Handle an alignment event
299 virtual void handleAlignment(
300 unsigned numBytes ///< The number of bytes added for alignment
303 /// @brief Handle a bytecode block
304 virtual void handleBlock(
305 unsigned BType, ///< The type of block
306 const unsigned char* StartPtr, ///< The start of the block
307 unsigned Size ///< The size of the block
310 /// @brief Handle a variable bit rate 32 bit unsigned
311 virtual void handleVBR32(
312 unsigned Size ///< Number of bytes the vbr_uint took up
315 /// @brief Handle a variable bit rate 64 bit unsigned
316 virtual void handleVBR64(
317 unsigned Size ///< Number of byte sthe vbr_uint64 took up