blob: 7d5cfcf95a7f3c29f72b5a72cbfab87ebadc3e8b [file] [log] [blame]
Chris Lattnerdf986172009-01-02 07:01:27 +00001//===-- 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.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ASMPARSER_LLPARSER_H
15#define LLVM_ASMPARSER_LLPARSER_H
16
17#include "LLLexer.h"
Owen Andersonfba933c2009-07-01 23:57:11 +000018#include "llvm/Module.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000019#include "llvm/Type.h"
Chris Lattner56608462009-12-28 08:20:46 +000020#include "llvm/Support/ValueHandle.h"
Chris Lattnere80250e2009-12-29 21:43:58 +000021#include <map>
Chris Lattnerdf986172009-01-02 07:01:27 +000022
23namespace llvm {
24 class Module;
25 class OpaqueType;
26 class Function;
27 class Value;
28 class BasicBlock;
29 class Instruction;
30 class Constant;
31 class GlobalValue;
Devang Patele54abc92009-07-22 17:43:22 +000032 class MetadataBase;
Nick Lewycky21cc4462009-04-04 07:22:01 +000033 class MDString;
34 class MDNode;
Misha Brukman9ea40342009-01-02 22:46:48 +000035
Chris Lattner09d9ef42009-10-28 03:39:23 +000036 /// 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.
40 struct ValID {
41 enum {
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.
Chris Lattner287881d2009-12-30 02:11:14 +000049 t_MDNode, // Value in MDNodeVal.
50 t_MDString // Value in MDStringVal.
Chris Lattner09d9ef42009-10-28 03:39:23 +000051 } Kind;
52
53 LLLexer::LocTy Loc;
54 unsigned UIntVal;
55 std::string StrVal, StrVal2;
56 APSInt APSIntVal;
57 APFloat APFloatVal;
58 Constant *ConstantVal;
Chris Lattner287881d2009-12-30 02:11:14 +000059 MDNode *MDNodeVal;
60 MDString *MDStringVal;
Chris Lattner09d9ef42009-10-28 03:39:23 +000061 ValID() : APFloatVal(0.0) {}
62
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;
69 }
70 };
71
Chris Lattnerdf986172009-01-02 07:01:27 +000072 class LLParser {
73 public:
74 typedef LLLexer::LocTy LocTy;
75 private:
Owen Andersonfba933c2009-07-01 23:57:11 +000076 LLVMContext& Context;
Chris Lattnerdf986172009-01-02 07:01:27 +000077 LLLexer Lex;
78 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000079
Chris Lattnerdf986172009-01-02 07:01:27 +000080 // Type resolution handling data structures.
81 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
82 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
83 std::vector<PATypeHolder> NumberedTypes;
Devang Patel923078c2009-07-01 19:21:12 +000084 /// MetadataCache - This map keeps track of parsed metadata constants.
Chris Lattnere80250e2009-12-29 21:43:58 +000085 std::map<unsigned, TrackingVH<MDNode> > MetadataCache;
86 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
Devang Patela2148402009-09-28 21:14:55 +000087 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +000088 struct UpRefRecord {
89 /// Loc - This is the location of the upref.
90 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000091
Chris Lattnerdf986172009-01-02 07:01:27 +000092 /// NestingLevel - The number of nesting levels that need to be popped
93 /// before this type is resolved.
94 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000095
Chris Lattnerdf986172009-01-02 07:01:27 +000096 /// LastContainedTy - This is the type at the current binding level for
97 /// the type. Every time we reduce the nesting level, this gets updated.
98 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000099
Chris Lattnerdf986172009-01-02 07:01:27 +0000100 /// UpRefTy - This is the actual opaque type that the upreference is
101 /// represented with.
102 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +0000103
Chris Lattnerdf986172009-01-02 07:01:27 +0000104 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
105 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
106 UpRefTy(URTy) {}
107 };
108 std::vector<UpRefRecord> UpRefs;
109
110 // Global Value reference information.
111 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
112 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
113 std::vector<GlobalValue*> NumberedVals;
Chris Lattner09d9ef42009-10-28 03:39:23 +0000114
115 // References to blockaddress. The key is the function ValID, the value is
116 // a list of references to blocks in that function.
117 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
118 ForwardRefBlockAddresses;
119
120 Function *MallocF;
Chris Lattnerdf986172009-01-02 07:01:27 +0000121 public:
Chris Lattnereeb4a842009-07-02 23:08:13 +0000122 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000123 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
124 M(m), MallocF(NULL) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +0000125 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +0000126
Owen Andersonb43eae72009-07-02 17:04:01 +0000127 LLVMContext& getContext() { return Context; }
128
Chris Lattnerdf986172009-01-02 07:01:27 +0000129 private:
130
131 bool Error(LocTy L, const std::string &Msg) const {
132 return Lex.Error(L, Msg);
133 }
134 bool TokError(const std::string &Msg) const {
135 return Error(Lex.getLoc(), Msg);
136 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000137
Chris Lattnerdf986172009-01-02 07:01:27 +0000138 /// GetGlobalVal - Get a value with the specified name or ID, creating a
139 /// forward reference record if needed. This can return null if the value
140 /// exists but does not have the right type.
141 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
142 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000143
Chris Lattnerdf986172009-01-02 07:01:27 +0000144 // Helper Routines.
145 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000146 bool EatIfPresent(lltok::Kind T) {
147 if (Lex.getKind() != T) return false;
148 Lex.Lex();
149 return true;
150 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000151 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
152 if (Lex.getKind() != T) {
153 Present = false;
154 } else {
155 Lex.Lex();
156 Present = true;
157 }
158 return false;
159 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000160 bool ParseStringConstant(std::string &Result);
161 bool ParseUInt32(unsigned &Val);
162 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000163 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000164 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000165 }
166 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
167 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
168 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
169 bool ParseOptionalLinkage(unsigned &Linkage) {
170 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
171 }
172 bool ParseOptionalVisibility(unsigned &Visibility);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000173 bool ParseOptionalCallingConv(CallingConv::ID &CC);
Chris Lattnerdf986172009-01-02 07:01:27 +0000174 bool ParseOptionalAlignment(unsigned &Alignment);
Devang Patel0475c912009-09-29 00:01:14 +0000175 bool ParseOptionalCustomMetadata();
Devang Patelf633a062009-09-17 23:04:48 +0000176 bool ParseOptionalInfo(unsigned &Alignment);
Chris Lattnerdf986172009-01-02 07:01:27 +0000177 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000178
Chris Lattnerdf986172009-01-02 07:01:27 +0000179 // Top-Level Entities
180 bool ParseTopLevelEntities();
181 bool ValidateEndOfModule();
182 bool ParseTargetDefinition();
183 bool ParseDepLibs();
184 bool ParseModuleAsm();
185 bool ParseUnnamedType();
186 bool ParseNamedType();
187 bool ParseDeclare();
188 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000189
Chris Lattnerdf986172009-01-02 07:01:27 +0000190 bool ParseGlobalType(bool &IsConstant);
Dan Gohman3845e502009-08-12 23:32:33 +0000191 bool ParseUnnamedGlobal();
Chris Lattnerdf986172009-01-02 07:01:27 +0000192 bool ParseNamedGlobal();
193 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
194 bool HasLinkage, unsigned Visibility);
195 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Devang Patel923078c2009-07-01 19:21:12 +0000196 bool ParseStandaloneMetadata();
Devang Pateleff2ab62009-07-29 00:34:02 +0000197 bool ParseNamedMetadata();
Chris Lattner442ffa12009-12-29 21:53:55 +0000198 bool ParseMDString(MDString *&Result);
Chris Lattner4a72efc2009-12-30 04:15:23 +0000199 bool ParseMDNodeID(MDNode *&Result);
Misha Brukman9ea40342009-01-02 22:46:48 +0000200
Chris Lattnerdf986172009-01-02 07:01:27 +0000201 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000202 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
203 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000204 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000205 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000206 }
207 bool ParseTypeRec(PATypeHolder &H);
208 bool ParseStructType(PATypeHolder &H, bool Packed);
209 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
210 bool ParseFunctionType(PATypeHolder &Result);
211 PATypeHolder HandleUpRefs(const Type *Ty);
212
213 // Constants.
214 bool ParseValID(ValID &ID);
215 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
Chris Lattnera7352392009-12-30 04:42:57 +0000216 bool ConvertGlobalOrMetadataValIDToValue(const Type *Ty, ValID &ID,
217 Value *&V);
Chris Lattnerdf986172009-01-02 07:01:27 +0000218 bool ParseGlobalValue(const Type *Ty, Constant *&V);
219 bool ParseGlobalTypeAndValue(Constant *&V);
220 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewyckycb337992009-05-10 20:57:05 +0000221 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000222
Misha Brukman9ea40342009-01-02 22:46:48 +0000223
Chris Lattnerdf986172009-01-02 07:01:27 +0000224 // Function Semantic Analysis.
225 class PerFunctionState {
226 LLParser &P;
227 Function &F;
228 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
229 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
230 std::vector<Value*> NumberedVals;
Chris Lattner09d9ef42009-10-28 03:39:23 +0000231
232 /// FunctionNumber - If this is an unnamed function, this is the slot
233 /// number of it, otherwise it is -1.
234 int FunctionNumber;
Chris Lattnerdf986172009-01-02 07:01:27 +0000235 public:
Chris Lattner09d9ef42009-10-28 03:39:23 +0000236 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerdf986172009-01-02 07:01:27 +0000237 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000238
Chris Lattnerdf986172009-01-02 07:01:27 +0000239 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000240
Chris Lattner09d9ef42009-10-28 03:39:23 +0000241 bool FinishFunction();
Misha Brukman9ea40342009-01-02 22:46:48 +0000242
Chris Lattnerdf986172009-01-02 07:01:27 +0000243 /// GetVal - Get a value with the specified name or ID, creating a
244 /// forward reference record if needed. This can return null if the value
245 /// exists but does not have the right type.
246 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
247 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000248
Chris Lattnerdf986172009-01-02 07:01:27 +0000249 /// SetInstName - After an instruction is parsed and inserted into its
250 /// basic block, this installs its name.
251 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
252 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000253
Chris Lattnerdf986172009-01-02 07:01:27 +0000254 /// GetBB - Get a basic block with the specified name or ID, creating a
255 /// forward reference record if needed. This can return null if the value
256 /// is not a BasicBlock.
257 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
258 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000259
Chris Lattnerdf986172009-01-02 07:01:27 +0000260 /// DefineBB - Define the specified basic block, which is either named or
261 /// unnamed. If there is an error, this returns null otherwise it returns
262 /// the block being defined.
263 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
264 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000265
Chris Lattnerdf986172009-01-02 07:01:27 +0000266 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
267 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000268
Chris Lattnerdf986172009-01-02 07:01:27 +0000269 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
270 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
271 PerFunctionState &PFS) {
272 Loc = Lex.getLoc();
273 return ParseValue(Ty, V, PFS);
274 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000275
Chris Lattnerdf986172009-01-02 07:01:27 +0000276 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
277 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
278 Loc = Lex.getLoc();
279 return ParseTypeAndValue(V, PFS);
280 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +0000281 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
282 PerFunctionState &PFS);
283 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
284 LocTy Loc;
285 return ParseTypeAndBasicBlock(BB, Loc, PFS);
286 }
Victor Hernandez19715562009-12-03 23:40:58 +0000287
Chris Lattnerdf986172009-01-02 07:01:27 +0000288 struct ParamInfo {
289 LocTy Loc;
290 Value *V;
291 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000292 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000293 : Loc(loc), V(v), Attrs(attrs) {}
294 };
295 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
296 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000297
Chris Lattnerdf986172009-01-02 07:01:27 +0000298 // Function Parsing.
299 struct ArgInfo {
300 LocTy Loc;
301 PATypeHolder Type;
302 unsigned Attrs;
303 std::string Name;
304 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
305 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
306 };
307 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000308 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000309 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
310 bool ParseFunctionBody(Function &Fn);
311 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000312
Chris Lattnerdf986172009-01-02 07:01:27 +0000313 // Instruction Parsing.
314 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
315 PerFunctionState &PFS);
316 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000317
Chris Lattnerdf986172009-01-02 07:01:27 +0000318 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
319 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
320 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +0000321 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000322 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000323
Chris Lattnere914b592009-01-05 08:24:46 +0000324 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
325 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000326 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
327 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
328 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
329 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000330 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000331 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
332 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
333 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
334 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
335 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000336 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
337 BasicBlock *BB = 0, bool isAlloca = true);
Victor Hernandez66284e02009-10-24 04:23:03 +0000338 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
Chris Lattnerdf986172009-01-02 07:01:27 +0000339 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
340 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
341 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
342 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
343 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
344 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Chris Lattner09d9ef42009-10-28 03:39:23 +0000345
346 bool ResolveForwardRefBlockAddresses(Function *TheFn,
347 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
348 PerFunctionState *PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000349 };
350} // End llvm namespace
351
352#endif