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/ADT/DenseMap.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Attributes.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/Operator.h"
24 #include "llvm/Support/ValueHandle.h"
25 #include "llvm/Type.h"
41 /// ValID - Represents a reference of a definition of some sort with no type.
42 /// There are several cases where we have to parse the value but where the
43 /// type can depend on later context. This may either be a numeric reference
44 /// or a symbolic (%var) reference. This is just a discriminated union.
47 t_LocalID, t_GlobalID, // ID in UIntVal.
48 t_LocalName, t_GlobalName, // Name in StrVal.
49 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
50 t_Null, t_Undef, t_Zero, // No value.
51 t_EmptyArray, // No value: []
52 t_Constant, // Value in ConstantVal.
53 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
54 t_MDNode, // Value in MDNodeVal.
55 t_MDString, // Value in MDStringVal.
56 t_ConstantStruct, // Value in ConstantStructElts.
57 t_PackedConstantStruct // Value in ConstantStructElts.
62 std::string StrVal, StrVal2;
65 Constant *ConstantVal;
67 MDString *MDStringVal;
68 Constant **ConstantStructElts;
70 ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
72 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
73 delete [] ConstantStructElts;
76 bool operator<(const ValID &RHS) const {
77 if (Kind == t_LocalID || Kind == t_GlobalID)
78 return UIntVal < RHS.UIntVal;
79 assert((Kind == t_LocalName || Kind == t_GlobalName ||
80 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
81 "Ordering not defined for this ValID kind yet");
82 return StrVal < RHS.StrVal;
88 typedef LLLexer::LocTy LocTy;
94 // Instruction metadata resolution. Each instruction can have a list of
95 // MDRef info associated with them.
97 // The simpler approach of just creating temporary MDNodes and then calling
98 // RAUW on them when the definition is processed doesn't work because some
99 // instruction metadata kinds, such as dbg, get stored in the IR in an
100 // "optimized" format which doesn't participate in the normal value use
101 // lists. This means that RAUW doesn't work, even on temporary MDNodes
102 // which otherwise support RAUW. Instead, we defer resolving MDNode
103 // references until the definitions have been processed.
106 unsigned MDKind, MDSlot;
108 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
110 // Type resolution handling data structures. The location is set when we
111 // have processed a use of the type but not a definition yet.
112 StringMap<std::pair<Type*, LocTy> > NamedTypes;
113 std::vector<std::pair<Type*, LocTy> > NumberedTypes;
115 std::vector<TrackingVH<MDNode> > NumberedMetadata;
116 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
118 // Global Value reference information.
119 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
120 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
121 std::vector<GlobalValue*> NumberedVals;
123 // References to blockaddress. The key is the function ValID, the value is
124 // a list of references to blocks in that function.
125 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
126 ForwardRefBlockAddresses;
129 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
130 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
134 LLVMContext &getContext() { return Context; }
138 bool Error(LocTy L, const Twine &Msg) const {
139 return Lex.Error(L, Msg);
141 bool TokError(const Twine &Msg) const {
142 return Error(Lex.getLoc(), Msg);
145 /// GetGlobalVal - Get a value with the specified name or ID, creating a
146 /// forward reference record if needed. This can return null if the value
147 /// exists but does not have the right type.
148 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
149 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
152 bool ParseToken(lltok::Kind T, const char *ErrMsg);
153 bool EatIfPresent(lltok::Kind T) {
154 if (Lex.getKind() != T) return false;
159 FastMathFlags EatFastMathFlagsIfPresent() {
162 switch (Lex.getKind()) {
163 case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue;
164 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
165 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
166 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
167 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
173 bool ParseOptionalToken(lltok::Kind T, bool &Present, LocTy *Loc = 0) {
174 if (Lex.getKind() != T) {
184 bool ParseStringConstant(std::string &Result);
185 bool ParseUInt32(unsigned &Val);
186 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
188 return ParseUInt32(Val);
191 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
192 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
193 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
194 bool ParseOptionalFuncAttrs(AttrBuilder &B);
195 bool ParseOptionalParamAttrs(AttrBuilder &B);
196 bool ParseOptionalReturnAttrs(AttrBuilder &B);
197 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
198 bool ParseOptionalLinkage(unsigned &Linkage) {
199 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
201 bool ParseOptionalVisibility(unsigned &Visibility);
202 bool ParseOptionalCallingConv(CallingConv::ID &CC);
203 bool ParseOptionalAlignment(unsigned &Alignment);
204 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
205 AtomicOrdering &Ordering);
206 bool ParseOptionalStackAlignment(unsigned &Alignment);
207 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
208 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
209 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
211 if (ParseIndexList(Indices, AteExtraComma)) return true;
213 return TokError("expected index");
217 // Top-Level Entities
218 bool ParseTopLevelEntities();
219 bool ValidateEndOfModule();
220 bool ParseTargetDefinition();
221 bool ParseModuleAsm();
222 bool ParseDepLibs(); // FIXME: Remove in 4.0.
223 bool ParseUnnamedType();
224 bool ParseNamedType();
228 bool ParseGlobalType(bool &IsConstant);
229 bool ParseUnnamedGlobal();
230 bool ParseNamedGlobal();
231 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
232 bool HasLinkage, unsigned Visibility);
233 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
234 bool ParseStandaloneMetadata();
235 bool ParseNamedMetadata();
236 bool ParseMDString(MDString *&Result);
237 bool ParseMDNodeID(MDNode *&Result);
238 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
241 bool ParseType(Type *&Result, bool AllowVoid = false);
242 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
244 return ParseType(Result, AllowVoid);
246 bool ParseAnonStructType(Type *&Result, bool Packed);
247 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
248 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
249 std::pair<Type*, LocTy> &Entry,
252 bool ParseArrayVectorType(Type *&Result, bool isVector);
253 bool ParseFunctionType(Type *&Result);
255 // Function Semantic Analysis.
256 class PerFunctionState {
259 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
260 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
261 std::vector<Value*> NumberedVals;
263 /// FunctionNumber - If this is an unnamed function, this is the slot
264 /// number of it, otherwise it is -1.
267 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
270 Function &getFunction() const { return F; }
272 bool FinishFunction();
274 /// GetVal - Get a value with the specified name or ID, creating a
275 /// forward reference record if needed. This can return null if the value
276 /// exists but does not have the right type.
277 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
278 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
280 /// SetInstName - After an instruction is parsed and inserted into its
281 /// basic block, this installs its name.
282 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
285 /// GetBB - Get a basic block with the specified name or ID, creating a
286 /// forward reference record if needed. This can return null if the value
287 /// is not a BasicBlock.
288 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
289 BasicBlock *GetBB(unsigned ID, LocTy Loc);
291 /// DefineBB - Define the specified basic block, which is either named or
292 /// unnamed. If there is an error, this returns null otherwise it returns
293 /// the block being defined.
294 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
297 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
298 PerFunctionState *PFS);
300 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
301 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
302 return ParseValue(Ty, V, &PFS);
304 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
305 PerFunctionState &PFS) {
307 return ParseValue(Ty, V, &PFS);
310 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
311 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
312 return ParseTypeAndValue(V, &PFS);
314 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
316 return ParseTypeAndValue(V, PFS);
318 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
319 PerFunctionState &PFS);
320 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
322 return ParseTypeAndBasicBlock(BB, Loc, PFS);
330 ParamInfo(LocTy loc, Value *v, Attribute attrs)
331 : Loc(loc), V(v), Attrs(attrs) {}
333 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
334 PerFunctionState &PFS);
337 bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL);
338 bool ParseGlobalValue(Type *Ty, Constant *&V);
339 bool ParseGlobalTypeAndValue(Constant *&V);
340 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
341 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
342 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
343 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
344 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
352 ArgInfo(LocTy L, Type *ty, Attribute Attr, const std::string &N)
353 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
355 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
356 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
357 bool ParseFunctionBody(Function &Fn);
358 bool ParseBasicBlock(PerFunctionState &PFS);
360 // Instruction Parsing. Each instruction parsing routine can return with a
361 // normal result, an error result, or return having eaten an extra comma.
362 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
363 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
364 PerFunctionState &PFS);
365 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
367 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
368 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
369 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
370 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
371 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
372 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
374 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
375 unsigned OperandType);
376 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
377 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
378 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
379 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
380 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
381 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
382 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
383 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
384 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
385 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
386 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
387 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
388 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
389 int ParseStore(Instruction *&I, PerFunctionState &PFS);
390 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
391 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
392 int ParseFence(Instruction *&I, PerFunctionState &PFS);
393 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
394 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
395 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
397 bool ResolveForwardRefBlockAddresses(Function *TheFn,
398 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
399 PerFunctionState *PFS);
401 } // End llvm namespace