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/Instructions.h"
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/Support/ValueHandle.h"
39 /// ValID - Represents a reference of a definition of some sort with no type.
40 /// There are several cases where we have to parse the value but where the
41 /// type can depend on later context. This may either be a numeric reference
42 /// or a symbolic (%var) reference. This is just a discriminated union.
45 t_LocalID, t_GlobalID, // ID in UIntVal.
46 t_LocalName, t_GlobalName, // Name in StrVal.
47 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
48 t_Null, t_Undef, t_Zero, // No value.
49 t_EmptyArray, // No value: []
50 t_Constant, // Value in ConstantVal.
51 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
52 t_MDNode, // Value in MDNodeVal.
53 t_MDString, // Value in MDStringVal.
54 t_ConstantStruct, // Value in ConstantStructElts.
55 t_PackedConstantStruct // Value in ConstantStructElts.
60 std::string StrVal, StrVal2;
63 Constant *ConstantVal;
65 MDString *MDStringVal;
66 Constant **ConstantStructElts;
68 ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
70 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
71 delete [] ConstantStructElts;
74 bool operator<(const ValID &RHS) const {
75 if (Kind == t_LocalID || Kind == t_GlobalID)
76 return UIntVal < RHS.UIntVal;
77 assert((Kind == t_LocalName || Kind == t_GlobalName ||
78 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
79 "Ordering not defined for this ValID kind yet");
80 return StrVal < RHS.StrVal;
86 typedef LLLexer::LocTy LocTy;
92 // Instruction metadata resolution. Each instruction can have a list of
93 // MDRef info associated with them.
95 // The simpler approach of just creating temporary MDNodes and then calling
96 // RAUW on them when the definition is processed doesn't work because some
97 // instruction metadata kinds, such as dbg, get stored in the IR in an
98 // "optimized" format which doesn't participate in the normal value use
99 // lists. This means that RAUW doesn't work, even on temporary MDNodes
100 // which otherwise support RAUW. Instead, we defer resolving MDNode
101 // references until the definitions have been processed.
104 unsigned MDKind, MDSlot;
106 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
108 // Type resolution handling data structures. The location is set when we
109 // have processed a use of the type but not a definition yet.
110 StringMap<std::pair<Type*, LocTy> > NamedTypes;
111 std::vector<std::pair<Type*, LocTy> > NumberedTypes;
113 std::vector<TrackingVH<MDNode> > NumberedMetadata;
114 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
116 // Global Value reference information.
117 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
118 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
119 std::vector<GlobalValue*> NumberedVals;
121 // References to blockaddress. The key is the function ValID, the value is
122 // a list of references to blocks in that function.
123 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
124 ForwardRefBlockAddresses;
127 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
128 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
132 LLVMContext &getContext() { return Context; }
136 bool Error(LocTy L, const Twine &Msg) const {
137 return Lex.Error(L, Msg);
139 bool TokError(const Twine &Msg) const {
140 return Error(Lex.getLoc(), Msg);
143 /// GetGlobalVal - Get a value with the specified name or ID, creating a
144 /// forward reference record if needed. This can return null if the value
145 /// exists but does not have the right type.
146 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
147 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
150 bool ParseToken(lltok::Kind T, const char *ErrMsg);
151 bool EatIfPresent(lltok::Kind T) {
152 if (Lex.getKind() != T) return false;
156 bool ParseOptionalToken(lltok::Kind T, bool &Present, LocTy *Loc = 0) {
157 if (Lex.getKind() != T) {
167 bool ParseStringConstant(std::string &Result);
168 bool ParseUInt32(unsigned &Val);
169 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
171 return ParseUInt32(Val);
173 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
174 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
175 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
176 bool ParseOptionalLinkage(unsigned &Linkage) {
177 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
179 bool ParseOptionalVisibility(unsigned &Visibility);
180 bool ParseOptionalCallingConv(CallingConv::ID &CC);
181 bool ParseOptionalAlignment(unsigned &Alignment);
182 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
183 AtomicOrdering &Ordering);
184 bool ParseOptionalStackAlignment(unsigned &Alignment);
185 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
186 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
187 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
189 if (ParseIndexList(Indices, AteExtraComma)) return true;
191 return TokError("expected index");
195 // Top-Level Entities
196 bool ParseTopLevelEntities();
197 bool ValidateEndOfModule();
198 bool ParseTargetDefinition();
200 bool ParseModuleAsm();
201 bool ParseUnnamedType();
202 bool ParseNamedType();
206 bool ParseGlobalType(bool &IsConstant);
207 bool ParseUnnamedGlobal();
208 bool ParseNamedGlobal();
209 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
210 bool HasLinkage, unsigned Visibility);
211 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
212 bool ParseStandaloneMetadata();
213 bool ParseNamedMetadata();
214 bool ParseMDString(MDString *&Result);
215 bool ParseMDNodeID(MDNode *&Result);
216 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
219 bool ParseType(Type *&Result, bool AllowVoid = false);
220 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
222 return ParseType(Result, AllowVoid);
224 bool ParseAnonStructType(Type *&Result, bool Packed);
225 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
226 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
227 std::pair<Type*, LocTy> &Entry,
230 bool ParseArrayVectorType(Type *&Result, bool isVector);
231 bool ParseFunctionType(Type *&Result);
233 // Function Semantic Analysis.
234 class PerFunctionState {
237 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
238 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
239 std::vector<Value*> NumberedVals;
241 /// FunctionNumber - If this is an unnamed function, this is the slot
242 /// number of it, otherwise it is -1.
245 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
248 Function &getFunction() const { return F; }
250 bool FinishFunction();
252 /// GetVal - Get a value with the specified name or ID, creating a
253 /// forward reference record if needed. This can return null if the value
254 /// exists but does not have the right type.
255 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
256 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
258 /// SetInstName - After an instruction is parsed and inserted into its
259 /// basic block, this installs its name.
260 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
263 /// GetBB - Get a basic block with the specified name or ID, creating a
264 /// forward reference record if needed. This can return null if the value
265 /// is not a BasicBlock.
266 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
267 BasicBlock *GetBB(unsigned ID, LocTy Loc);
269 /// DefineBB - Define the specified basic block, which is either named or
270 /// unnamed. If there is an error, this returns null otherwise it returns
271 /// the block being defined.
272 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
275 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
276 PerFunctionState *PFS);
278 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
279 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
280 return ParseValue(Ty, V, &PFS);
282 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
283 PerFunctionState &PFS) {
285 return ParseValue(Ty, V, &PFS);
288 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
289 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
290 return ParseTypeAndValue(V, &PFS);
292 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
294 return ParseTypeAndValue(V, PFS);
296 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
297 PerFunctionState &PFS);
298 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
300 return ParseTypeAndBasicBlock(BB, Loc, PFS);
308 ParamInfo(LocTy loc, Value *v, unsigned attrs)
309 : Loc(loc), V(v), Attrs(attrs) {}
311 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
312 PerFunctionState &PFS);
315 bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL);
316 bool ParseGlobalValue(Type *Ty, Constant *&V);
317 bool ParseGlobalTypeAndValue(Constant *&V);
318 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
319 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
320 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
321 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
322 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
330 ArgInfo(LocTy L, Type *ty, unsigned Attr, const std::string &N)
331 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
333 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
334 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
335 bool ParseFunctionBody(Function &Fn);
336 bool ParseBasicBlock(PerFunctionState &PFS);
338 // Instruction Parsing. Each instruction parsing routine can return with a
339 // normal result, an error result, or return having eaten an extra comma.
340 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
341 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
342 PerFunctionState &PFS);
343 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
345 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
346 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
347 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
348 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
349 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
350 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
352 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
353 unsigned OperandType);
354 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
355 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
356 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
357 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
358 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
359 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
360 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
361 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
362 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
363 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
364 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
365 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
366 int ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
367 int ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
368 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
369 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
370 int ParseFence(Instruction *&I, PerFunctionState &PFS);
371 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
372 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
373 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
375 bool ResolveForwardRefBlockAddresses(Function *TheFn,
376 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
377 PerFunctionState *PFS);
379 } // End llvm namespace