blob: b03fe52d5072d84f7fff405b797ce9b9f1ff8900 [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;
Chris Lattner0834e6a2009-12-30 04:51:58 +000084 std::vector<TrackingVH<MDNode> > NumberedMetadata;
Chris Lattnere80250e2009-12-29 21:43:58 +000085 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
Devang Patela2148402009-09-28 21:14:55 +000086 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +000087 struct UpRefRecord {
88 /// Loc - This is the location of the upref.
89 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000090
Chris Lattnerdf986172009-01-02 07:01:27 +000091 /// NestingLevel - The number of nesting levels that need to be popped
92 /// before this type is resolved.
93 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000094
Chris Lattnerdf986172009-01-02 07:01:27 +000095 /// LastContainedTy - This is the type at the current binding level for
96 /// the type. Every time we reduce the nesting level, this gets updated.
97 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000098
Chris Lattnerdf986172009-01-02 07:01:27 +000099 /// UpRefTy - This is the actual opaque type that the upreference is
100 /// represented with.
101 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +0000102
Chris Lattnerdf986172009-01-02 07:01:27 +0000103 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
104 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
105 UpRefTy(URTy) {}
106 };
107 std::vector<UpRefRecord> UpRefs;
108
109 // Global Value reference information.
110 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
111 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
112 std::vector<GlobalValue*> NumberedVals;
Chris Lattner09d9ef42009-10-28 03:39:23 +0000113
114 // References to blockaddress. The key is the function ValID, the value is
115 // a list of references to blocks in that function.
116 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
117 ForwardRefBlockAddresses;
118
119 Function *MallocF;
Chris Lattnerdf986172009-01-02 07:01:27 +0000120 public:
Chris Lattnereeb4a842009-07-02 23:08:13 +0000121 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000122 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
123 M(m), MallocF(NULL) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +0000124 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +0000125
Owen Andersonb43eae72009-07-02 17:04:01 +0000126 LLVMContext& getContext() { return Context; }
127
Chris Lattnerdf986172009-01-02 07:01:27 +0000128 private:
129
130 bool Error(LocTy L, const std::string &Msg) const {
131 return Lex.Error(L, Msg);
132 }
133 bool TokError(const std::string &Msg) const {
134 return Error(Lex.getLoc(), Msg);
135 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000136
Chris Lattnerdf986172009-01-02 07:01:27 +0000137 /// GetGlobalVal - Get a value with the specified name or ID, creating a
138 /// forward reference record if needed. This can return null if the value
139 /// exists but does not have the right type.
140 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
141 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000142
Chris Lattnerdf986172009-01-02 07:01:27 +0000143 // Helper Routines.
144 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000145 bool EatIfPresent(lltok::Kind T) {
146 if (Lex.getKind() != T) return false;
147 Lex.Lex();
148 return true;
149 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000150 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
151 if (Lex.getKind() != T) {
152 Present = false;
153 } else {
154 Lex.Lex();
155 Present = true;
156 }
157 return false;
158 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000159 bool ParseStringConstant(std::string &Result);
160 bool ParseUInt32(unsigned &Val);
161 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000162 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000163 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000164 }
165 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
166 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
167 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
168 bool ParseOptionalLinkage(unsigned &Linkage) {
169 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
170 }
171 bool ParseOptionalVisibility(unsigned &Visibility);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000172 bool ParseOptionalCallingConv(CallingConv::ID &CC);
Chris Lattnerdf986172009-01-02 07:01:27 +0000173 bool ParseOptionalAlignment(unsigned &Alignment);
Devang Patel0475c912009-09-29 00:01:14 +0000174 bool ParseOptionalCustomMetadata();
Devang Patelf633a062009-09-17 23:04:48 +0000175 bool ParseOptionalInfo(unsigned &Alignment);
Chris Lattner628c13a2009-12-30 05:14:00 +0000176 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
177 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
178 bool AteExtraComma;
179 if (ParseIndexList(Indices, AteExtraComma)) return true;
180 if (AteExtraComma)
181 return TokError("expected index");
182 return false;
183 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000184
Chris Lattnerdf986172009-01-02 07:01:27 +0000185 // Top-Level Entities
186 bool ParseTopLevelEntities();
187 bool ValidateEndOfModule();
188 bool ParseTargetDefinition();
189 bool ParseDepLibs();
190 bool ParseModuleAsm();
191 bool ParseUnnamedType();
192 bool ParseNamedType();
193 bool ParseDeclare();
194 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000195
Chris Lattnerdf986172009-01-02 07:01:27 +0000196 bool ParseGlobalType(bool &IsConstant);
Dan Gohman3845e502009-08-12 23:32:33 +0000197 bool ParseUnnamedGlobal();
Chris Lattnerdf986172009-01-02 07:01:27 +0000198 bool ParseNamedGlobal();
199 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
200 bool HasLinkage, unsigned Visibility);
201 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Devang Patel923078c2009-07-01 19:21:12 +0000202 bool ParseStandaloneMetadata();
Devang Pateleff2ab62009-07-29 00:34:02 +0000203 bool ParseNamedMetadata();
Chris Lattner442ffa12009-12-29 21:53:55 +0000204 bool ParseMDString(MDString *&Result);
Chris Lattner4a72efc2009-12-30 04:15:23 +0000205 bool ParseMDNodeID(MDNode *&Result);
Misha Brukman9ea40342009-01-02 22:46:48 +0000206
Chris Lattnerdf986172009-01-02 07:01:27 +0000207 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000208 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
209 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000210 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000211 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000212 }
213 bool ParseTypeRec(PATypeHolder &H);
214 bool ParseStructType(PATypeHolder &H, bool Packed);
215 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
216 bool ParseFunctionType(PATypeHolder &Result);
217 PATypeHolder HandleUpRefs(const Type *Ty);
218
219 // Constants.
220 bool ParseValID(ValID &ID);
221 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
Chris Lattnera7352392009-12-30 04:42:57 +0000222 bool ConvertGlobalOrMetadataValIDToValue(const Type *Ty, ValID &ID,
223 Value *&V);
Chris Lattnerdf986172009-01-02 07:01:27 +0000224 bool ParseGlobalValue(const Type *Ty, Constant *&V);
225 bool ParseGlobalTypeAndValue(Constant *&V);
226 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewyckycb337992009-05-10 20:57:05 +0000227 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000228
Misha Brukman9ea40342009-01-02 22:46:48 +0000229
Chris Lattnerdf986172009-01-02 07:01:27 +0000230 // 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;
Chris Lattner09d9ef42009-10-28 03:39:23 +0000237
238 /// FunctionNumber - If this is an unnamed function, this is the slot
239 /// number of it, otherwise it is -1.
240 int FunctionNumber;
Chris Lattnerdf986172009-01-02 07:01:27 +0000241 public:
Chris Lattner09d9ef42009-10-28 03:39:23 +0000242 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerdf986172009-01-02 07:01:27 +0000243 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000244
Chris Lattnerdf986172009-01-02 07:01:27 +0000245 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000246
Chris Lattner09d9ef42009-10-28 03:39:23 +0000247 bool FinishFunction();
Misha Brukman9ea40342009-01-02 22:46:48 +0000248
Chris Lattnerdf986172009-01-02 07:01:27 +0000249 /// 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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000254
Chris Lattnerdf986172009-01-02 07:01:27 +0000255 /// 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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000259
Chris Lattnerdf986172009-01-02 07:01:27 +0000260 /// 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);
Misha Brukman9ea40342009-01-02 22:46:48 +0000265
Chris Lattnerdf986172009-01-02 07:01:27 +0000266 /// 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);
270 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000271
Chris Lattnerdf986172009-01-02 07:01:27 +0000272 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
273 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000274
Chris Lattnerdf986172009-01-02 07:01:27 +0000275 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
276 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
277 PerFunctionState &PFS) {
278 Loc = Lex.getLoc();
279 return ParseValue(Ty, V, PFS);
280 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000281
Chris Lattnerdf986172009-01-02 07:01:27 +0000282 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
283 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
284 Loc = Lex.getLoc();
285 return ParseTypeAndValue(V, PFS);
286 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +0000287 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
288 PerFunctionState &PFS);
289 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
290 LocTy Loc;
291 return ParseTypeAndBasicBlock(BB, Loc, PFS);
292 }
Victor Hernandez19715562009-12-03 23:40:58 +0000293
Chris Lattnerdf986172009-01-02 07:01:27 +0000294 struct ParamInfo {
295 LocTy Loc;
296 Value *V;
297 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000298 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000299 : Loc(loc), V(v), Attrs(attrs) {}
300 };
301 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
302 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000303
Chris Lattnerdf986172009-01-02 07:01:27 +0000304 // Function Parsing.
305 struct ArgInfo {
306 LocTy Loc;
307 PATypeHolder Type;
308 unsigned Attrs;
309 std::string Name;
310 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
311 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
312 };
313 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000314 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000315 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
316 bool ParseFunctionBody(Function &Fn);
317 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000318
Chris Lattnerdf986172009-01-02 07:01:27 +0000319 // Instruction Parsing.
320 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
321 PerFunctionState &PFS);
322 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000323
Chris Lattnerdf986172009-01-02 07:01:27 +0000324 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
325 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
326 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +0000327 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000328 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000329
Chris Lattnere914b592009-01-05 08:24:46 +0000330 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
331 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000332 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
333 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
334 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
335 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000336 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000337 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
338 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
339 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
340 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
341 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000342 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
343 BasicBlock *BB = 0, bool isAlloca = true);
Victor Hernandez66284e02009-10-24 04:23:03 +0000344 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
Chris Lattnerdf986172009-01-02 07:01:27 +0000345 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
346 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
347 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
348 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
349 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
350 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Chris Lattner09d9ef42009-10-28 03:39:23 +0000351
352 bool ResolveForwardRefBlockAddresses(Function *TheFn,
353 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
354 PerFunctionState *PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000355 };
356} // End llvm namespace
357
358#endif