1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
//===-- Parser.h - Abstract Interface To Bytecode Parsing -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This header file defines the interface to the Bytecode Parser and the
// Bytecode Handler interface that it calls.
//
//===----------------------------------------------------------------------===//
#ifndef BYTECODE_PARSER_H
#define BYTECODE_PARSER_H
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/GlobalValue.h"
#include "llvm/Module.h"
#include <utility>
#include <vector>
#include <map>
namespace llvm {
class BytecodeHandler; ///< Forward declare the handler interface
/// This class defines the interface for parsing a buffer of bytecode. The
/// parser itself takes no action except to call the various functions of
/// the handler interface. The parser's sole responsibility is the correct
/// interpretation of the bytecode buffer. The handler is responsible for
/// instantiating and keeping track of all values. As a convenience, the parser
/// is responsible for materializing types and will pass them through the
/// handler interface as necessary.
/// @see BytecodeHandler
/// @brief Abstract Bytecode Parser interface
class AbstractBytecodeParser {
/// @name Constructors
/// @{
public:
AbstractBytecodeParser( BytecodeHandler* h ) { handler = h; }
~AbstractBytecodeParser() { }
/// @}
/// @name Types
/// @{
public:
/// @brief A convenience type for the buffer pointer
typedef const unsigned char* BufPtr;
/// @brief The type used for vector of potentially abstract types
typedef std::vector<PATypeHolder> TypeListTy;
/// @brief
/// @}
/// @name Methods
/// @{
public:
/// @brief Main interface to parsing a bytecode buffer.
void ParseBytecode(const unsigned char *Buf, unsigned Length,
const std::string &ModuleID);
/// The ParseBytecode method lazily parses functions. Use this
/// method to cause the parser to actually parse all the function bodies
/// in the bytecode buffer.
/// @see ParseBytecode
/// @brief Parse all function bodies
void ParseAllFunctionBodies ();
/// The Parsebytecode method lazily parses functions. Use this
/// method to casue the parser to parse the next function of a given
/// types. Note that this will remove the function from what is to be
/// included by ParseAllFunctionBodies.
/// @see ParseAllFunctionBodies
/// @see ParseBytecode
/// @brief Parse the next function of specific type
void ParseNextFunction (Type* FType) ;
/// @}
/// @name Parsing Units For Subclasses
/// @{
protected:
/// @brief Parse whole module scope
void ParseModule (BufPtr &Buf, BufPtr End);
/// @brief Parse the version information block
void ParseVersionInfo (BufPtr &Buf, BufPtr End);
/// @brief Parse the ModuleGlobalInfo block
void ParseModuleGlobalInfo (BufPtr &Buf, BufPtr End);
/// @brief Parse a symbol table
void ParseSymbolTable (BufPtr &Buf, BufPtr End);
/// This function parses LLVM functions lazily. It obtains the type of the
/// function and records where the body of the function is in the bytecode
/// buffer. The caller can then use the ParseNextFunction and
/// ParseAllFunctionBodies to get handler events for the functions.
/// @brief Parse functions lazily.
void ParseFunctionLazily (BufPtr &Buf, BufPtr End);
/// @brief Parse a function body
void ParseFunctionBody (const Type* FType, BufPtr &Buf, BufPtr EndBuf);
/// @brief Parse a compaction table
void ParseCompactionTable (BufPtr &Buf, BufPtr End);
/// @brief Parse global types
void ParseGlobalTypes (BufPtr &Buf, BufPtr End);
/// @brief Parse a basic block (for LLVM 1.0 basic block blocks)
void ParseBasicBlock (BufPtr &Buf, BufPtr End, unsigned BlockNo);
/// @brief parse an instruction list (for post LLVM 1.0 instruction lists
/// with blocks differentiated by terminating instructions.
unsigned ParseInstructionList(BufPtr &Buf, BufPtr End);
/// @brief Parse an instruction.
bool ParseInstruction (BufPtr &Buf, BufPtr End,
std::vector<unsigned>& Args);
/// @brief Parse a constant pool
void ParseConstantPool (BufPtr &Buf, BufPtr End, TypeListTy& List);
/// @brief Parse a constant value
void ParseConstantValue (BufPtr &Buf, BufPtr End, unsigned TypeID);
/// @brief Parse a block of types.
void ParseTypeConstants (BufPtr &Buf, BufPtr End, TypeListTy &Tab,
unsigned NumEntries);
/// @brief Parse a single type.
const Type *ParseTypeConstant(BufPtr &Buf, BufPtr End);
/// @brief Parse a string constants block
void ParseStringConstants (BufPtr &Buf, BufPtr End, unsigned NumEntries);
/// @}
/// @name Data
/// @{
private:
// Information about the module, extracted from the bytecode revision number.
unsigned char RevisionNum; // The rev # itself
// Flags to distinguish LLVM 1.0 & 1.1 bytecode formats (revision #0)
// Revision #0 had an explicit alignment of data only for the ModuleGlobalInfo
// block. This was fixed to be like all other blocks in 1.2
bool hasInconsistentModuleGlobalInfo;
// Revision #0 also explicitly encoded zero values for primitive types like
// int/sbyte/etc.
bool hasExplicitPrimitiveZeros;
// Flags to control features specific the LLVM 1.2 and before (revision #1)
// LLVM 1.2 and earlier required that getelementptr structure indices were
// ubyte constants and that sequential type indices were longs.
bool hasRestrictedGEPTypes;
/// CompactionTable - If a compaction table is active in the current function,
/// this is the mapping that it contains.
std::vector<Type*> CompactionTypeTable;
// ConstantFwdRefs - This maintains a mapping between <Type, Slot #>'s and
// forward references to constants. Such values may be referenced before they
// are defined, and if so, the temporary object that they represent is held
// here.
//
typedef std::map<std::pair<const Type*,unsigned>, Constant*> ConstantRefsType;
ConstantRefsType ConstantFwdRefs;
// TypesLoaded - This vector mirrors the Values[TypeTyID] plane. It is used
// to deal with forward references to types.
//
TypeListTy ModuleTypes;
TypeListTy FunctionTypes;
// When the ModuleGlobalInfo section is read, we create a FunctionType object
// for each function in the module. When the function is loaded, this type is
// used to instantiate the actual function object.
std::vector<const Type*> FunctionSignatureList;
// Constant values are read in after global variables. Because of this, we
// must defer setting the initializers on global variables until after module
// level constants have been read. In the mean time, this list keeps track of
// what we must do.
//
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
/// @}
/// @name Implementation Details
/// @{
private:
/// This stores the parser's handler. It makes virtual function calls through
/// the BytecodeHandler to notify the handler of parsing events. What the
/// handler does with the events is completely orthogonal to the business of
/// parsing the bytecode.
/// @brief The handler of bytecode parsing events.
BytecodeHandler* handler;
/// For lazy reading-in of functions, we need to save away several pieces of
/// information about each function: its begin and end pointer in the buffer
/// and its FunctionSlot.
struct LazyFunctionInfo {
const unsigned char *Buf, *EndBuf;
LazyFunctionInfo(const unsigned char *B = 0, const unsigned char *EB = 0)
: Buf(B), EndBuf(EB) {}
};
typedef std::map<const Type*, LazyFunctionInfo> LazyFunctionMap;
LazyFunctionMap LazyFunctionLoadMap;
private:
static inline void readBlock(const unsigned char *&Buf,
const unsigned char *EndBuf,
unsigned &Type, unsigned &Size) ;
const Type *AbstractBytecodeParser::getType(unsigned ID);
/// getGlobalTableType - This is just like getType, but when a compaction
/// table is in use, it is ignored. Also, no forward references or other
/// fancy features are supported.
const Type *getGlobalTableType(unsigned Slot) {
if (Slot < Type::FirstDerivedTyID) {
const Type *Ty = Type::getPrimitiveType((Type::PrimitiveID)Slot);
assert(Ty && "Not a primitive type ID?");
return Ty;
}
Slot -= Type::FirstDerivedTyID;
if (Slot >= ModuleTypes.size())
throw std::string("Illegal compaction table type reference!");
return ModuleTypes[Slot];
}
unsigned getGlobalTableTypeSlot(const Type *Ty) {
if (Ty->isPrimitiveType())
return Ty->getPrimitiveID();
TypeListTy::iterator I = find(ModuleTypes.begin(),
ModuleTypes.end(), Ty);
if (I == ModuleTypes.end())
throw std::string("Didn't find type in ModuleTypes.");
return Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]);
}
AbstractBytecodeParser(const AbstractBytecodeParser &); // DO NOT IMPLEMENT
void operator=(const AbstractBytecodeParser &); // DO NOT IMPLEMENT
/// @}
};
/// This class provides the interface for the handling bytecode events during
/// parsing. The methods on this interface are invoked by the
/// AbstractBytecodeParser as it discovers the content of a bytecode stream.
/// This class provides a a clear separation of concerns between recognizing
/// the semantic units of a bytecode file and deciding what to do with them.
/// The AbstractBytecodeParser recognizes the content of the bytecode file and
/// calls the BytecodeHandler methods to determine what should be done. This
/// arrangement allows Bytecode files to be read and handled for a number of
/// purposes simply by creating a subclass of BytecodeHandler. None of the
/// parsing details need to be understood, only the meaning of the calls
/// made on this interface.
///
/// Another paradigm that uses this design pattern is the XML SAX Parser. The
/// ContentHandler for SAX plays the same role as the BytecodeHandler here.
/// @see AbstractbytecodeParser
/// @brief Handle Bytecode Parsing Events
class BytecodeHandler {
/// @name Constructors And Operators
/// @{
public:
/// @brief Default constructor (empty)
BytecodeHandler() {}
/// @brief Virtual destructor (empty)
virtual ~BytecodeHandler() {}
private:
BytecodeHandler(const BytecodeHandler &); // DO NOT IMPLEMENT
void operator=(const
|