blob: c73b6c06e7ec8aff7b40adcce897d9ff328fdf12 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- ReaderInternals.h - Definitions internal to the reader ---*- C++ -*--=//
2//
3// This header file defines various stuff that is used by the bytecode reader.
4//
5//===----------------------------------------------------------------------===//
6
7#ifndef READER_INTERNALS_H
8#define READER_INTERNALS_H
9
10#include "llvm/Bytecode/Primitives.h"
11#include "llvm/SymTabValue.h"
12#include "llvm/Method.h"
13#include "llvm/Instruction.h"
14#include <map>
15#include <utility>
16
17class BasicBlock;
18class Method;
19class Module;
20class Type;
21
22typedef unsigned char uchar;
23
24struct RawInst { // The raw fields out of the bytecode stream...
25 unsigned NumOperands;
26 unsigned Opcode;
27 const Type *Ty;
28 unsigned Arg1, Arg2;
29 union {
30 unsigned Arg3;
31 vector<unsigned> *VarArgs; // Contains arg #3,4,5... if NumOperands > 3
32 };
33};
34
35class BytecodeParser {
36public:
37 BytecodeParser() {
38 // Define this in case we don't see a ModuleGlobalInfo block.
39 FirstDerivedTyID = Type::FirstDerivedTyID;
40 }
41
42 Module *ParseBytecode(const uchar *Buf, const uchar *EndBuf);
43private: // All of this data is transient across calls to ParseBytecode
44 typedef vector<Value *> ValueList;
45 typedef vector<ValueList> ValueTable;
46 typedef map<const Type *, unsigned> TypeMapType;
47 ValueTable Values, LateResolveValues;
48 ValueTable ModuleValues, LateResolveModuleValues;
49 TypeMapType TypeMap;
50
51 // Information read from the ModuleGlobalInfo section of the file...
52 unsigned FirstDerivedTyID;
53
54 // When the ModuleGlobalInfo section is read, we load the type of each method
55 // and the 'ModuleValues' slot that it lands in. We then load a placeholder
56 // into its slot to reserve it. When the method is loaded, this placeholder
57 // is replaced.
58 //
59 list<pair<const MethodType *, unsigned> > MethodSignatureList;
60
61private:
62 bool ParseModule (const uchar * Buf, const uchar *End, Module *&);
63 bool ParseModuleGlobalInfo (const uchar *&Buf, const uchar *End, Module *);
64 bool ParseSymbolTable (const uchar *&Buf, const uchar *End);
65 bool ParseMethod (const uchar *&Buf, const uchar *End, Module *);
66 bool ParseBasicBlock (const uchar *&Buf, const uchar *End, BasicBlock *&);
67 bool ParseInstruction (const uchar *&Buf, const uchar *End, Instruction *&);
68 bool ParseRawInst (const uchar *&Buf, const uchar *End, RawInst &);
69
70 bool ParseConstantPool(const uchar *&Buf, const uchar *EndBuf,
71 SymTabValue::ConstantPoolType &CP, ValueTable &Tab);
72
73
74 bool parseConstPoolValue(const uchar *&Buf, const uchar *End,
75 const Type *Ty, ConstPoolVal *&V);
76 bool parseTypeConstant (const uchar *&Buf, const uchar *, ConstPoolVal *&);
77
78 Value *getValue(const Type *Ty, unsigned num, bool Create = true);
79 const Type *getType(unsigned ID);
80
81 bool insertValue(Value *D, vector<ValueList> &D);
82 bool postResolveValues(ValueTable &ValTab);
83
84 bool getTypeSlot(const Type *Ty, unsigned &Slot);
85};
86
87template<class SuperType>
88class PlaceholderDef : public SuperType {
89 unsigned ID;
90public:
91 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
92 unsigned getID() { return ID; }
93};
94
95struct InstPlaceHolderHelper : public Instruction {
96 InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
Chris Lattnera41f50d2001-07-07 19:24:15 +000097 virtual const char *getOpcodeName() const { return "placeholder"; }
Chris Lattner00950542001-06-06 20:29:01 +000098
99 virtual Instruction *clone() const { abort(); return 0; }
Chris Lattner00950542001-06-06 20:29:01 +0000100};
101
102struct BBPlaceHolderHelper : public BasicBlock {
103 BBPlaceHolderHelper(const Type *Ty) : BasicBlock() {
104 assert(Ty->isLabelType());
105 }
106};
107
108struct MethPlaceHolderHelper : public Method {
109 MethPlaceHolderHelper(const Type *Ty)
110 : Method((const MethodType*)Ty) {
111 assert(Ty->isMethodType() && "Method placeholders must be method types!");
112 }
113};
114
115typedef PlaceholderDef<InstPlaceHolderHelper> DefPHolder;
116typedef PlaceholderDef<BBPlaceHolderHelper> BBPHolder;
117typedef PlaceholderDef<MethPlaceHolderHelper> MethPHolder;
118
119static inline unsigned getValueIDNumberFromPlaceHolder(Value *Def) {
120 switch (Def->getType()->getPrimitiveID()) {
121 case Type::LabelTyID: return ((BBPHolder*)Def)->getID();
122 case Type::MethodTyID: return ((MethPHolder*)Def)->getID();
123 default: return ((DefPHolder*)Def)->getID();
124 }
125}
126
127static inline bool readBlock(const uchar *&Buf, const uchar *EndBuf,
128 unsigned &Type, unsigned &Size) {
129#if DEBUG_OUTPUT
130 bool Result = read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
131 cerr << "StartLoc = " << ((unsigned)Buf & 4095)
132 << " Type = " << Type << " Size = " << Size << endl;
133 return Result;
134#else
135 return read(Buf, EndBuf, Type) || read(Buf, EndBuf, Size);
136#endif
137}
138
139#endif