e45266bb2c3ca03511124cc45fec0602ff69eb66
[oota-llvm.git] / BytecodeHandler.h
1 //===-- BytecodeHandler.h - Handle Bytecode Parsing Events ------*- 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 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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_BYTECODE_BYTECODEHANDLER_H
17 #define LLVM_BYTECODE_BYTECODEHANDLER_H
18
19 #include "llvm/Module.h"
20
21 namespace llvm {
22
23 class ArrayType;
24 class StructType;
25 class PointerType;
26 class VectorType;
27 class ConstantArray;
28 class Module;
29
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).
36 ///
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.
43 ///
44 /// @see BytecodeHandler
45 /// @brief Handle Bytecode Parsing Events
46 class BytecodeHandler {
47
48 /// @name Constructors And Operators
49 /// @{
50 public:
51   /// @brief Default constructor (empty)
52   BytecodeHandler() {}
53   /// @brief Virtual destructor (empty)
54   virtual ~BytecodeHandler();
55
56 private:
57   BytecodeHandler(const BytecodeHandler &);  // DO NOT IMPLEMENT
58   void operator=(const BytecodeHandler &);  // DO NOT IMPLEMENT
59
60 /// @}
61 /// @name Handler Methods
62 /// @{
63 public:
64
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 ) {}
71
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 ) {}
76
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() {}
81
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) {}
86
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
92   ) {}
93
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
96   /// read.
97   /// @brief Handle the bytecode prolog
98   virtual void handleVersionInfo(
99     unsigned char RevisionNum        ///< Byte code revision number
100   ) {}
101
102   /// This method is called at the start of a module globals block which
103   /// contains the global variables and the function placeholders
104   virtual void handleModuleGlobalsBegin() {}
105
106   /// This method is called when a non-initialized global variable is
107   /// recognized. Its type, constness, and linkage type are provided.
108   /// @brief Handle a non-initialized global variable
109   virtual void handleGlobalVariable(
110     const Type* ElemType,     ///< The type of the global variable
111     bool isConstant,          ///< Whether the GV is constant or not
112     GlobalValue::LinkageTypes,///< The linkage type of the GV
113     GlobalValue::VisibilityTypes,///< The visibility style of the GV
114     unsigned SlotNum,         ///< Slot number of GV
115     unsigned initSlot,         ///< Slot number of GV's initializer (0 if none)
116     bool isThreadLocal        ///< Whether the GV is thread local or not
117   ) {}
118
119   virtual void handleGlobalAlias(
120     const Type* ElemType,
121     GlobalValue::LinkageTypes Linkage,
122     unsigned TypeSlotNum,
123     unsigned AliaseeSlot) { }
124   
125   /// This method is called when a type list is recognized. It simply
126   /// provides the number of types that the list contains. The handler
127   /// should expect that number of calls to handleType.
128   /// @brief Handle a type
129   virtual void handleTypeList(
130     unsigned numEntries ///< The number of entries in the type list
131   ) {}
132
133   /// This method is called when a new type is recognized. The type is
134   /// converted from the bytecode and passed to this method.
135   /// @brief Handle a type
136   virtual void handleType(
137     const Type* Ty ///< The type that was just recognized
138   ) {}
139
140   /// This method is called when the function prototype for a function is
141   /// encountered in the module globals block.
142   virtual void handleFunctionDeclaration(
143     Function* Func ///< The function being declared
144   ) {}
145
146   /// This method is called when a global variable is initialized with
147   /// its constant value. Because of forward referencing, etc. this is
148   /// done towards the end of the module globals block
149   virtual void handleGlobalInitializer(GlobalVariable*, Constant* ) {}
150
151   /// This method is called for each dependent library name found
152   /// in the module globals block.
153   virtual void handleDependentLibrary(const std::string& libName) {}
154
155   /// This method is called if the module globals has a non-empty target
156   /// triple
157   virtual void handleTargetTriple(const std::string& triple) {}
158
159   /// This method is called at the end of the module globals block.
160   /// @brief Handle end of module globals block.
161   virtual void handleModuleGlobalsEnd() {}
162
163   /// @brief Handle start of a symbol table
164   virtual void handleTypeSymbolTableBegin(
165     TypeSymbolTable* ST  ///< The symbol table being filled
166   ) {}
167
168   /// @brief Handle start of a symbol table
169   virtual void handleValueSymbolTableBegin(
170     Function* Func,       ///< The function to which the ST belongs or 0 for Mod
171     ValueSymbolTable* ST  ///< The symbol table being filled
172   ) {}
173
174   /// @brief Handle a named type in the symbol table
175   virtual void handleSymbolTableType(
176     unsigned i,              ///< The index of the type in this plane
177     unsigned slot,           ///< Slot number of the named type
178     const std::string& name  ///< Name of the type
179   ) {}
180
181   /// @brief Handle a named value in the symbol table
182   virtual void handleSymbolTableValue(
183     unsigned i,              ///< The index of the value in this plane
184     unsigned slot,           ///< Slot number of the named value
185     const char *name, unsigned NameLen  ///< Name of the value.
186   ) {}
187
188   /// @brief Handle the end of a value symbol table
189   virtual void handleTypeSymbolTableEnd() {}
190
191   /// @brief Handle the end of a type symbol table
192   virtual void handleValueSymbolTableEnd() {}
193
194   /// @brief Handle the beginning of a function body
195   virtual void handleFunctionBegin(
196     Function* Func, ///< The function being defined
197     unsigned Size   ///< The size (in bytes) of the function's bytecode
198   ) {}
199
200   /// @brief Handle the end of a function body
201   virtual void handleFunctionEnd(
202     Function* Func  ///< The function whose definition has just finished.
203   ) {}
204
205   /// @brief Handle the beginning of a basic block
206   virtual void handleBasicBlockBegin(
207     unsigned blocknum ///< The block number of the block
208   ) {}
209
210   /// This method is called for each instruction that is parsed.
211   /// @returns true if the instruction is a block terminating instruction
212   /// @brief Handle an instruction
213   virtual bool handleInstruction(
214     unsigned Opcode,                 ///< Opcode of the instruction
215     const Type* iType,               ///< Instruction type
216     unsigned *Operands, unsigned NumOps, ///< Vector of slot # operands
217     Instruction *Inst,               ///< The resulting instruction
218     unsigned Length                  ///< Length of instruction in bc bytes
219   ) { return false; }
220
221   /// @brief Handle the end of a basic block
222   virtual void handleBasicBlockEnd(
223     unsigned blocknum  ///< The block number of the block just finished
224   ) {}
225
226   /// @brief Handle start of global constants block.
227   virtual void handleGlobalConstantsBegin() {}
228
229   /// @brief Handle a constant expression
230   virtual void handleConstantExpression(
231     unsigned Opcode,  ///< Opcode of primary expression operator
232     Constant**Args, unsigned NumArgs, ///< expression args
233     Constant* C ///< The constant value
234   ) {}
235
236   /// @brief Handle a constant array
237   virtual void handleConstantArray(
238     const ArrayType* AT,                ///< Type of the array
239     Constant**ElementSlots, unsigned NumElts,///< Slot nums for array values
240     unsigned TypeSlot,                  ///< Slot # of type
241     Constant* Val                       ///< The constant value
242   ) {}
243
244   /// @brief Handle a constant structure
245   virtual void handleConstantStruct(
246     const StructType* ST,               ///< Type of the struct
247     Constant**ElementSlots, unsigned NumElts,///< Slot nums for struct values
248     Constant* Val                       ///< The constant value
249   ) {}
250
251   /// @brief Handle a constant packed
252   virtual void handleConstantVector(
253     const VectorType* PT,                ///< Type of the array
254     Constant**ElementSlots, unsigned NumElts,///< Slot nums for packed values
255     unsigned TypeSlot,                  ///< Slot # of type
256     Constant* Val                       ///< The constant value
257   ) {}
258
259   /// @brief Handle a constant pointer
260   virtual void handleConstantPointer(
261     const PointerType* PT, ///< Type of the pointer
262     unsigned Slot,         ///< Slot num of initializer value
263     GlobalValue* GV        ///< Referenced global value
264   ) {}
265
266   /// @brief Handle a constant strings (array special case)
267   virtual void handleConstantString(
268     const ConstantArray* CA ///< Type of the string array
269   ) {}
270
271   /// @brief Handle a primitive constant value
272   virtual void handleConstantValue(
273     Constant * c ///< The constant just defined
274   ) {}
275
276   /// @brief Handle the end of the global constants
277   virtual void handleGlobalConstantsEnd() {}
278
279   /// @brief Handle an alignment event
280   virtual void handleAlignment(
281     unsigned numBytes ///< The number of bytes added for alignment
282   ) {}
283
284   /// @brief Handle a bytecode block
285   virtual void handleBlock(
286     unsigned BType,                ///< The type of block
287     const unsigned char* StartPtr, ///< The start of the block
288     unsigned Size                  ///< The size of the block
289   ) {}
290
291 /// @}
292
293 };
294
295 }
296 #endif