blob: 7e9018205279acec544d2f543dcfe030d71bfbe6 [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.
49 t_Metadata // Value in MetadataVal.
50 } Kind;
51
52 LLLexer::LocTy Loc;
53 unsigned UIntVal;
54 std::string StrVal, StrVal2;
55 APSInt APSIntVal;
56 APFloat APFloatVal;
57 Constant *ConstantVal;
58 MetadataBase *MetadataVal;
59 ValID() : APFloatVal(0.0) {}
60
61 bool operator<(const ValID &RHS) const {
62 if (Kind == t_LocalID || Kind == t_GlobalID)
63 return UIntVal < RHS.UIntVal;
64 assert((Kind == t_LocalName || Kind == t_GlobalName) &&
65 "Ordering not defined for this ValID kind yet");
66 return StrVal < RHS.StrVal;
67 }
68 };
69
Chris Lattnerdf986172009-01-02 07:01:27 +000070 class LLParser {
71 public:
72 typedef LLLexer::LocTy LocTy;
73 private:
Owen Andersonfba933c2009-07-01 23:57:11 +000074 LLVMContext& Context;
Chris Lattnerdf986172009-01-02 07:01:27 +000075 LLLexer Lex;
76 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000077
Chris Lattnerdf986172009-01-02 07:01:27 +000078 // Type resolution handling data structures.
79 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
80 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
81 std::vector<PATypeHolder> NumberedTypes;
Devang Patel923078c2009-07-01 19:21:12 +000082 /// MetadataCache - This map keeps track of parsed metadata constants.
Chris Lattnere80250e2009-12-29 21:43:58 +000083 std::map<unsigned, TrackingVH<MDNode> > MetadataCache;
84 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
Devang Patela2148402009-09-28 21:14:55 +000085 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +000086 struct UpRefRecord {
87 /// Loc - This is the location of the upref.
88 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000089
Chris Lattnerdf986172009-01-02 07:01:27 +000090 /// NestingLevel - The number of nesting levels that need to be popped
91 /// before this type is resolved.
92 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000093
Chris Lattnerdf986172009-01-02 07:01:27 +000094 /// LastContainedTy - This is the type at the current binding level for
95 /// the type. Every time we reduce the nesting level, this gets updated.
96 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000097
Chris Lattnerdf986172009-01-02 07:01:27 +000098 /// UpRefTy - This is the actual opaque type that the upreference is
99 /// represented with.
100 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +0000101
Chris Lattnerdf986172009-01-02 07:01:27 +0000102 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
103 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
104 UpRefTy(URTy) {}
105 };
106 std::vector<UpRefRecord> UpRefs;
107
108 // Global Value reference information.
109 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
110 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
111 std::vector<GlobalValue*> NumberedVals;
Chris Lattner09d9ef42009-10-28 03:39:23 +0000112
113 // References to blockaddress. The key is the function ValID, the value is
114 // a list of references to blocks in that function.
115 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
116 ForwardRefBlockAddresses;
117
118 Function *MallocF;
Chris Lattnerdf986172009-01-02 07:01:27 +0000119 public:
Chris Lattnereeb4a842009-07-02 23:08:13 +0000120 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000121 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
122 M(m), MallocF(NULL) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +0000123 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +0000124
Owen Andersonb43eae72009-07-02 17:04:01 +0000125 LLVMContext& getContext() { return Context; }
126
Chris Lattnerdf986172009-01-02 07:01:27 +0000127 private:
128
129 bool Error(LocTy L, const std::string &Msg) const {
130 return Lex.Error(L, Msg);
131 }
132 bool TokError(const std::string &Msg) const {
133 return Error(Lex.getLoc(), Msg);
134 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000135
Chris Lattnerdf986172009-01-02 07:01:27 +0000136 /// GetGlobalVal - Get a value with the specified name or ID, creating a
137 /// forward reference record if needed. This can return null if the value
138 /// exists but does not have the right type.
139 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
140 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000141
Chris Lattnerdf986172009-01-02 07:01:27 +0000142 // Helper Routines.
143 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000144 bool EatIfPresent(lltok::Kind T) {
145 if (Lex.getKind() != T) return false;
146 Lex.Lex();
147 return true;
148 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000149 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
150 if (Lex.getKind() != T) {
151 Present = false;
152 } else {
153 Lex.Lex();
154 Present = true;
155 }
156 return false;
157 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000158 bool ParseStringConstant(std::string &Result);
159 bool ParseUInt32(unsigned &Val);
160 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000161 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000162 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000163 }
164 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
165 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
166 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
167 bool ParseOptionalLinkage(unsigned &Linkage) {
168 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
169 }
170 bool ParseOptionalVisibility(unsigned &Visibility);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000171 bool ParseOptionalCallingConv(CallingConv::ID &CC);
Chris Lattnerdf986172009-01-02 07:01:27 +0000172 bool ParseOptionalAlignment(unsigned &Alignment);
Devang Patel0475c912009-09-29 00:01:14 +0000173 bool ParseOptionalCustomMetadata();
Devang Patelf633a062009-09-17 23:04:48 +0000174 bool ParseOptionalInfo(unsigned &Alignment);
Chris Lattnerdf986172009-01-02 07:01:27 +0000175 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000176
Chris Lattnerdf986172009-01-02 07:01:27 +0000177 // Top-Level Entities
178 bool ParseTopLevelEntities();
179 bool ValidateEndOfModule();
180 bool ParseTargetDefinition();
181 bool ParseDepLibs();
182 bool ParseModuleAsm();
183 bool ParseUnnamedType();
184 bool ParseNamedType();
185 bool ParseDeclare();
186 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000187
Chris Lattnerdf986172009-01-02 07:01:27 +0000188 bool ParseGlobalType(bool &IsConstant);
Dan Gohman3845e502009-08-12 23:32:33 +0000189 bool ParseUnnamedGlobal();
Chris Lattnerdf986172009-01-02 07:01:27 +0000190 bool ParseNamedGlobal();
191 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
192 bool HasLinkage, unsigned Visibility);
193 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Devang Patel923078c2009-07-01 19:21:12 +0000194 bool ParseStandaloneMetadata();
Devang Pateleff2ab62009-07-29 00:34:02 +0000195 bool ParseNamedMetadata();
Devang Patele54abc92009-07-22 17:43:22 +0000196 bool ParseMDString(MetadataBase *&S);
Devang Patel104cf9e2009-07-23 01:07:34 +0000197 bool ParseMDNode(MetadataBase *&N);
Misha Brukman9ea40342009-01-02 22:46:48 +0000198
Chris Lattnerdf986172009-01-02 07:01:27 +0000199 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000200 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
201 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000202 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000203 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000204 }
205 bool ParseTypeRec(PATypeHolder &H);
206 bool ParseStructType(PATypeHolder &H, bool Packed);
207 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
208 bool ParseFunctionType(PATypeHolder &Result);
209 PATypeHolder HandleUpRefs(const Type *Ty);
210
211 // Constants.
212 bool ParseValID(ValID &ID);
213 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
214 bool ParseGlobalValue(const Type *Ty, Constant *&V);
215 bool ParseGlobalTypeAndValue(Constant *&V);
216 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewyckycb337992009-05-10 20:57:05 +0000217 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000218
Misha Brukman9ea40342009-01-02 22:46:48 +0000219
Chris Lattnerdf986172009-01-02 07:01:27 +0000220 // Function Semantic Analysis.
221 class PerFunctionState {
222 LLParser &P;
223 Function &F;
224 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
225 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
226 std::vector<Value*> NumberedVals;
Chris Lattner09d9ef42009-10-28 03:39:23 +0000227
228 /// FunctionNumber - If this is an unnamed function, this is the slot
229 /// number of it, otherwise it is -1.
230 int FunctionNumber;
Chris Lattnerdf986172009-01-02 07:01:27 +0000231 public:
Chris Lattner09d9ef42009-10-28 03:39:23 +0000232 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerdf986172009-01-02 07:01:27 +0000233 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000234
Chris Lattnerdf986172009-01-02 07:01:27 +0000235 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000236
Chris Lattner09d9ef42009-10-28 03:39:23 +0000237 bool FinishFunction();
Misha Brukman9ea40342009-01-02 22:46:48 +0000238
Chris Lattnerdf986172009-01-02 07:01:27 +0000239 /// GetVal - Get a value with the specified name or ID, creating a
240 /// forward reference record if needed. This can return null if the value
241 /// exists but does not have the right type.
242 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
243 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000244
Chris Lattnerdf986172009-01-02 07:01:27 +0000245 /// SetInstName - After an instruction is parsed and inserted into its
246 /// basic block, this installs its name.
247 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
248 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000249
Chris Lattnerdf986172009-01-02 07:01:27 +0000250 /// GetBB - Get a basic block with the specified name or ID, creating a
251 /// forward reference record if needed. This can return null if the value
252 /// is not a BasicBlock.
253 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
254 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000255
Chris Lattnerdf986172009-01-02 07:01:27 +0000256 /// DefineBB - Define the specified basic block, which is either named or
257 /// unnamed. If there is an error, this returns null otherwise it returns
258 /// the block being defined.
259 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
260 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000261
Chris Lattnerdf986172009-01-02 07:01:27 +0000262 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
263 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000264
Chris Lattnerdf986172009-01-02 07:01:27 +0000265 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
266 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
267 PerFunctionState &PFS) {
268 Loc = Lex.getLoc();
269 return ParseValue(Ty, V, PFS);
270 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000271
Chris Lattnerdf986172009-01-02 07:01:27 +0000272 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
273 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
274 Loc = Lex.getLoc();
275 return ParseTypeAndValue(V, PFS);
276 }
Chris Lattnerf9be95f2009-10-27 19:13:16 +0000277 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
278 PerFunctionState &PFS);
279 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
280 LocTy Loc;
281 return ParseTypeAndBasicBlock(BB, Loc, PFS);
282 }
Victor Hernandez19715562009-12-03 23:40:58 +0000283
284 bool ParseInlineMetadata(Value *&V, PerFunctionState &PFS);
285
Chris Lattnerdf986172009-01-02 07:01:27 +0000286 struct ParamInfo {
287 LocTy Loc;
288 Value *V;
289 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000290 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000291 : Loc(loc), V(v), Attrs(attrs) {}
292 };
293 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
294 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000295
Chris Lattnerdf986172009-01-02 07:01:27 +0000296 // Function Parsing.
297 struct ArgInfo {
298 LocTy Loc;
299 PATypeHolder Type;
300 unsigned Attrs;
301 std::string Name;
302 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
303 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
304 };
305 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000306 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000307 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
308 bool ParseFunctionBody(Function &Fn);
309 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000310
Chris Lattnerdf986172009-01-02 07:01:27 +0000311 // Instruction Parsing.
312 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
313 PerFunctionState &PFS);
314 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000315
Chris Lattnerdf986172009-01-02 07:01:27 +0000316 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
317 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
318 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +0000319 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000320 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000321
Chris Lattnere914b592009-01-05 08:24:46 +0000322 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
323 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000324 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
325 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
326 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
327 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000328 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000329 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
330 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
331 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
332 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
333 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000334 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
335 BasicBlock *BB = 0, bool isAlloca = true);
Victor Hernandez66284e02009-10-24 04:23:03 +0000336 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
Chris Lattnerdf986172009-01-02 07:01:27 +0000337 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
338 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
339 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
340 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
341 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
342 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Chris Lattner09d9ef42009-10-28 03:39:23 +0000343
344 bool ResolveForwardRefBlockAddresses(Function *TheFn,
345 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
346 PerFunctionState *PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000347 };
348} // End llvm namespace
349
350#endif