Land the long talked about "type system rewrite" patch. This
[llvm/stm8.git] / lib / AsmParser / LLParser.h
blob96306578506199555b29d0fbf77a7d687258b968
1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the parser class for .ll files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ASMPARSER_LLPARSER_H
15 #define LLVM_ASMPARSER_LLPARSER_H
17 #include "LLLexer.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Support/ValueHandle.h"
23 #include <map>
25 namespace llvm {
26 class Module;
27 class OpaqueType;
28 class Function;
29 class Value;
30 class BasicBlock;
31 class Instruction;
32 class Constant;
33 class GlobalValue;
34 class MDString;
35 class MDNode;
36 class StructType;
38 /// ValID - Represents a reference of a definition of some sort with no type.
39 /// There are several cases where we have to parse the value but where the
40 /// type can depend on later context. This may either be a numeric reference
41 /// or a symbolic (%var) reference. This is just a discriminated union.
42 struct ValID {
43 enum {
44 t_LocalID, t_GlobalID, // ID in UIntVal.
45 t_LocalName, t_GlobalName, // Name in StrVal.
46 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
47 t_Null, t_Undef, t_Zero, // No value.
48 t_EmptyArray, // No value: []
49 t_Constant, // Value in ConstantVal.
50 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
51 t_MDNode, // Value in MDNodeVal.
52 t_MDString, // Value in MDStringVal.
53 t_ConstantStruct, // Value in ConstantStructElts.
54 t_PackedConstantStruct // Value in ConstantStructElts.
55 } Kind;
57 LLLexer::LocTy Loc;
58 unsigned UIntVal;
59 std::string StrVal, StrVal2;
60 APSInt APSIntVal;
61 APFloat APFloatVal;
62 Constant *ConstantVal;
63 MDNode *MDNodeVal;
64 MDString *MDStringVal;
65 Constant **ConstantStructElts;
67 ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
68 ~ValID() {
69 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
70 delete [] ConstantStructElts;
73 bool operator<(const ValID &RHS) const {
74 if (Kind == t_LocalID || Kind == t_GlobalID)
75 return UIntVal < RHS.UIntVal;
76 assert((Kind == t_LocalName || Kind == t_GlobalName ||
77 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
78 "Ordering not defined for this ValID kind yet");
79 return StrVal < RHS.StrVal;
83 class LLParser {
84 public:
85 typedef LLLexer::LocTy LocTy;
86 private:
87 LLVMContext &Context;
88 LLLexer Lex;
89 Module *M;
91 // Instruction metadata resolution. Each instruction can have a list of
92 // MDRef info associated with them.
94 // The simpler approach of just creating temporary MDNodes and then calling
95 // RAUW on them when the definition is processed doesn't work because some
96 // instruction metadata kinds, such as dbg, get stored in the IR in an
97 // "optimized" format which doesn't participate in the normal value use
98 // lists. This means that RAUW doesn't work, even on temporary MDNodes
99 // which otherwise support RAUW. Instead, we defer resolving MDNode
100 // references until the definitions have been processed.
101 struct MDRef {
102 SMLoc Loc;
103 unsigned MDKind, MDSlot;
105 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
107 // Type resolution handling data structures. The location is set when we
108 // have processed a use of the type but not a definition yet.
109 StringMap<std::pair<Type*, LocTy> > NamedTypes;
110 std::vector<std::pair<Type*, LocTy> > NumberedTypes;
112 std::vector<TrackingVH<MDNode> > NumberedMetadata;
113 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
115 // Global Value reference information.
116 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
117 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
118 std::vector<GlobalValue*> NumberedVals;
120 // References to blockaddress. The key is the function ValID, the value is
121 // a list of references to blocks in that function.
122 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
123 ForwardRefBlockAddresses;
125 public:
126 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
127 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
128 M(m) {}
129 bool Run();
131 LLVMContext &getContext() { return Context; }
133 private:
135 bool Error(LocTy L, const Twine &Msg) const {
136 return Lex.Error(L, Msg);
138 bool TokError(const Twine &Msg) const {
139 return Error(Lex.getLoc(), Msg);
142 /// GetGlobalVal - Get a value with the specified name or ID, creating a
143 /// forward reference record if needed. This can return null if the value
144 /// exists but does not have the right type.
145 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
146 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
148 // Helper Routines.
149 bool ParseToken(lltok::Kind T, const char *ErrMsg);
150 bool EatIfPresent(lltok::Kind T) {
151 if (Lex.getKind() != T) return false;
152 Lex.Lex();
153 return true;
155 bool ParseOptionalToken(lltok::Kind T, bool &Present, LocTy *Loc = 0) {
156 if (Lex.getKind() != T) {
157 Present = false;
158 } else {
159 if (Loc)
160 *Loc = Lex.getLoc();
161 Lex.Lex();
162 Present = true;
164 return false;
166 bool ParseStringConstant(std::string &Result);
167 bool ParseUInt32(unsigned &Val);
168 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
169 Loc = Lex.getLoc();
170 return ParseUInt32(Val);
172 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
173 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
174 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
175 bool ParseOptionalLinkage(unsigned &Linkage) {
176 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
178 bool ParseOptionalVisibility(unsigned &Visibility);
179 bool ParseOptionalCallingConv(CallingConv::ID &CC);
180 bool ParseOptionalAlignment(unsigned &Alignment);
181 bool ParseOptionalStackAlignment(unsigned &Alignment);
182 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
183 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
184 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
185 bool AteExtraComma;
186 if (ParseIndexList(Indices, AteExtraComma)) return true;
187 if (AteExtraComma)
188 return TokError("expected index");
189 return false;
192 // Top-Level Entities
193 bool ParseTopLevelEntities();
194 bool ValidateEndOfModule();
195 bool ParseTargetDefinition();
196 bool ParseDepLibs();
197 bool ParseModuleAsm();
198 bool ParseUnnamedType();
199 bool ParseNamedType();
200 bool ParseDeclare();
201 bool ParseDefine();
203 bool ParseGlobalType(bool &IsConstant);
204 bool ParseUnnamedGlobal();
205 bool ParseNamedGlobal();
206 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
207 bool HasLinkage, unsigned Visibility);
208 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
209 bool ParseStandaloneMetadata();
210 bool ParseNamedMetadata();
211 bool ParseMDString(MDString *&Result);
212 bool ParseMDNodeID(MDNode *&Result);
213 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
215 // Type Parsing.
216 bool ParseType(Type *&Result, bool AllowVoid = false);
217 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
218 Loc = Lex.getLoc();
219 return ParseType(Result, AllowVoid);
221 bool ParseAnonStructType(Type *&Result, bool Packed);
222 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
223 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
224 std::pair<Type*, LocTy> &Entry,
225 Type *&ResultTy);
227 bool ParseArrayVectorType(Type *&Result, bool isVector);
228 bool ParseFunctionType(Type *&Result);
230 // Function Semantic Analysis.
231 class PerFunctionState {
232 LLParser &P;
233 Function &F;
234 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
235 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
236 std::vector<Value*> NumberedVals;
238 /// FunctionNumber - If this is an unnamed function, this is the slot
239 /// number of it, otherwise it is -1.
240 int FunctionNumber;
241 public:
242 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
243 ~PerFunctionState();
245 Function &getFunction() const { return F; }
247 bool FinishFunction();
249 /// GetVal - Get a value with the specified name or ID, creating a
250 /// forward reference record if needed. This can return null if the value
251 /// exists but does not have the right type.
252 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
253 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
255 /// SetInstName - After an instruction is parsed and inserted into its
256 /// basic block, this installs its name.
257 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
258 Instruction *Inst);
260 /// GetBB - Get a basic block with the specified name or ID, creating a
261 /// forward reference record if needed. This can return null if the value
262 /// is not a BasicBlock.
263 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
264 BasicBlock *GetBB(unsigned ID, LocTy Loc);
266 /// DefineBB - Define the specified basic block, which is either named or
267 /// unnamed. If there is an error, this returns null otherwise it returns
268 /// the block being defined.
269 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
272 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
273 PerFunctionState *PFS);
275 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState *PFS);
276 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
277 return ParseValue(Ty, V, &PFS);
279 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
280 PerFunctionState &PFS) {
281 Loc = Lex.getLoc();
282 return ParseValue(Ty, V, &PFS);
285 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
286 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
287 return ParseTypeAndValue(V, &PFS);
289 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
290 Loc = Lex.getLoc();
291 return ParseTypeAndValue(V, PFS);
293 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
294 PerFunctionState &PFS);
295 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
296 LocTy Loc;
297 return ParseTypeAndBasicBlock(BB, Loc, PFS);
301 struct ParamInfo {
302 LocTy Loc;
303 Value *V;
304 unsigned Attrs;
305 ParamInfo(LocTy loc, Value *v, unsigned attrs)
306 : Loc(loc), V(v), Attrs(attrs) {}
308 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
309 PerFunctionState &PFS);
311 // Constant Parsing.
312 bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL);
313 bool ParseGlobalValue(const Type *Ty, Constant *&V);
314 bool ParseGlobalTypeAndValue(Constant *&V);
315 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
316 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
317 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
318 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
319 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
321 // Function Parsing.
322 struct ArgInfo {
323 LocTy Loc;
324 Type *Ty;
325 unsigned Attrs;
326 std::string Name;
327 ArgInfo(LocTy L, Type *ty, unsigned Attr, const std::string &N)
328 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
330 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
331 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
332 bool ParseFunctionBody(Function &Fn);
333 bool ParseBasicBlock(PerFunctionState &PFS);
335 // Instruction Parsing. Each instruction parsing routine can return with a
336 // normal result, an error result, or return having eaten an extra comma.
337 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
338 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
339 PerFunctionState &PFS);
340 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
342 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
343 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
344 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
345 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
346 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
348 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
349 unsigned OperandType);
350 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
351 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
352 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
353 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
354 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
355 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
356 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
357 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
358 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
359 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
360 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
361 int ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
362 int ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
363 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
364 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
365 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
367 bool ResolveForwardRefBlockAddresses(Function *TheFn,
368 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
369 PerFunctionState *PFS);
371 } // End llvm namespace
373 #endif