1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the parser class for .ll files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ASMPARSER_LLPARSER_H
15 #define LLVM_ASMPARSER_LLPARSER_H
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/ValueHandle.h"
36 /// ValID - Represents a reference of a definition of some sort with no type.
37 /// There are several cases where we have to parse the value but where the
38 /// type can depend on later context. This may either be a numeric reference
39 /// or a symbolic (%var) reference. This is just a discriminated union.
42 t_LocalID, t_GlobalID, // ID in UIntVal.
43 t_LocalName, t_GlobalName, // Name in StrVal.
44 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
45 t_Null, t_Undef, t_Zero, // No value.
46 t_EmptyArray, // No value: []
47 t_Constant, // Value in ConstantVal.
48 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
49 t_MDNode, // Value in MDNodeVal.
50 t_MDString // Value in MDStringVal.
55 std::string StrVal, StrVal2;
58 Constant *ConstantVal;
60 MDString *MDStringVal;
61 ValID() : APFloatVal(0.0) {}
63 bool operator<(const ValID &RHS) const {
64 if (Kind == t_LocalID || Kind == t_GlobalID)
65 return UIntVal < RHS.UIntVal;
66 assert((Kind == t_LocalName || Kind == t_GlobalName) &&
67 "Ordering not defined for this ValID kind yet");
68 return StrVal < RHS.StrVal;
74 typedef LLLexer::LocTy LocTy;
80 // Instruction metadata resolution. Each instruction can have a list of
81 // MDRef info associated with them.
83 // The simpler approach of just creating temporary MDNodes and then calling
84 // RAUW on them when the definition is processed doesn't work because some
85 // instruction metadata kinds, such as dbg, get stored in the IR in an
86 // "optimized" format which doesn't participate in the normal value use
87 // lists. This means that RAUW doesn't work, even on temporary MDNodes
88 // which otherwise support RAUW. Instead, we defer resolving MDNode
89 // references until the definitions have been processed.
92 unsigned MDKind, MDSlot;
94 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
96 // Type resolution handling data structures.
97 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
98 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
99 std::vector<PATypeHolder> NumberedTypes;
100 std::vector<TrackingVH<MDNode> > NumberedMetadata;
101 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
103 /// Loc - This is the location of the upref.
106 /// NestingLevel - The number of nesting levels that need to be popped
107 /// before this type is resolved.
108 unsigned NestingLevel;
110 /// LastContainedTy - This is the type at the current binding level for
111 /// the type. Every time we reduce the nesting level, this gets updated.
112 const Type *LastContainedTy;
114 /// UpRefTy - This is the actual opaque type that the upreference is
115 /// represented with.
118 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
119 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
122 std::vector<UpRefRecord> UpRefs;
124 // Global Value reference information.
125 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
126 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
127 std::vector<GlobalValue*> NumberedVals;
129 // References to blockaddress. The key is the function ValID, the value is
130 // a list of references to blocks in that function.
131 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
132 ForwardRefBlockAddresses;
136 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
137 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
138 M(m), MallocF(NULL) {}
141 LLVMContext& getContext() { return Context; }
145 bool Error(LocTy L, const Twine &Msg) const {
146 return Lex.Error(L, Msg);
148 bool TokError(const Twine &Msg) const {
149 return Error(Lex.getLoc(), Msg);
152 /// GetGlobalVal - Get a value with the specified name or ID, creating a
153 /// forward reference record if needed. This can return null if the value
154 /// exists but does not have the right type.
155 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
156 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
159 bool ParseToken(lltok::Kind T, const char *ErrMsg);
160 bool EatIfPresent(lltok::Kind T) {
161 if (Lex.getKind() != T) return false;
165 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
166 if (Lex.getKind() != T) {
174 bool ParseStringConstant(std::string &Result);
175 bool ParseUInt32(unsigned &Val);
176 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
178 return ParseUInt32(Val);
180 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
181 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
182 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
183 bool ParseOptionalLinkage(unsigned &Linkage) {
184 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
186 bool ParseOptionalVisibility(unsigned &Visibility);
187 bool ParseOptionalCallingConv(CallingConv::ID &CC);
188 bool ParseOptionalAlignment(unsigned &Alignment);
189 bool ParseOptionalStackAlignment(unsigned &Alignment);
190 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
191 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
192 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
194 if (ParseIndexList(Indices, AteExtraComma)) return true;
196 return TokError("expected index");
200 // Top-Level Entities
201 bool ParseTopLevelEntities();
202 bool ValidateEndOfModule();
203 bool ParseTargetDefinition();
205 bool ParseModuleAsm();
206 bool ParseUnnamedType();
207 bool ParseNamedType();
211 bool ParseGlobalType(bool &IsConstant);
212 bool ParseUnnamedGlobal();
213 bool ParseNamedGlobal();
214 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
215 bool HasLinkage, unsigned Visibility);
216 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
217 bool ParseStandaloneMetadata();
218 bool ParseNamedMetadata();
219 bool ParseMDString(MDString *&Result);
220 bool ParseMDNodeID(MDNode *&Result);
221 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
224 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
225 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
227 return ParseType(Result, AllowVoid);
229 bool ParseTypeRec(PATypeHolder &H);
230 bool ParseStructType(PATypeHolder &H, bool Packed);
231 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
232 bool ParseFunctionType(PATypeHolder &Result);
233 PATypeHolder HandleUpRefs(const Type *Ty);
235 // Function Semantic Analysis.
236 class PerFunctionState {
239 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
240 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
241 std::vector<Value*> NumberedVals;
243 /// FunctionNumber - If this is an unnamed function, this is the slot
244 /// number of it, otherwise it is -1.
247 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
250 Function &getFunction() const { return F; }
252 bool FinishFunction();
254 /// GetVal - Get a value with the specified name or ID, creating a
255 /// forward reference record if needed. This can return null if the value
256 /// exists but does not have the right type.
257 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
258 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
260 /// SetInstName - After an instruction is parsed and inserted into its
261 /// basic block, this installs its name.
262 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
265 /// GetBB - Get a basic block with the specified name or ID, creating a
266 /// forward reference record if needed. This can return null if the value
267 /// is not a BasicBlock.
268 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
269 BasicBlock *GetBB(unsigned ID, LocTy Loc);
271 /// DefineBB - Define the specified basic block, which is either named or
272 /// unnamed. If there is an error, this returns null otherwise it returns
273 /// the block being defined.
274 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
277 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
278 PerFunctionState *PFS);
280 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
281 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
282 PerFunctionState &PFS) {
284 return ParseValue(Ty, V, PFS);
287 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
288 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
290 return ParseTypeAndValue(V, PFS);
292 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
293 PerFunctionState &PFS);
294 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
296 return ParseTypeAndBasicBlock(BB, Loc, PFS);
304 ParamInfo(LocTy loc, Value *v, unsigned attrs)
305 : Loc(loc), V(v), Attrs(attrs) {}
307 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
308 PerFunctionState &PFS);
311 bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL);
312 bool ParseGlobalValue(const Type *Ty, Constant *&V);
313 bool ParseGlobalTypeAndValue(Constant *&V);
314 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
315 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
316 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
317 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
318 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
326 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
327 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
329 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
330 bool &isVarArg, bool inType);
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 int 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 BasicBlock *BB = 0, bool isAlloca = true);
362 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
363 int ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
364 int ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
365 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
366 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
367 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
368 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
370 bool ResolveForwardRefBlockAddresses(Function *TheFn,
371 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
372 PerFunctionState *PFS);
374 } // End llvm namespace