blob: 31707287913f82e2acd77a25870aa9d4bf134267 [file] [log] [blame]
Chris Lattner103ff152009-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 Andersonc3644562009-07-01 23:57:11 +000018#include "llvm/Module.h"
Chris Lattner103ff152009-01-02 07:01:27 +000019#include "llvm/Type.h"
Chris Lattner20eddd72009-12-28 08:20:46 +000020#include "llvm/Support/ValueHandle.h"
Chris Lattner9e9fa652009-12-29 21:43:58 +000021#include <map>
Chris Lattner103ff152009-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 Patel0c0a6ca2009-07-22 17:43:22 +000032 class MetadataBase;
Nick Lewycky4dcf8102009-04-04 07:22:01 +000033 class MDString;
34 class MDNode;
Misha Brukman7d987212009-01-02 22:46:48 +000035
Chris Lattner5bb63f12009-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 Lattner4a413b52009-12-30 02:11:14 +000049 t_MDNode, // Value in MDNodeVal.
50 t_MDString // Value in MDStringVal.
Chris Lattner5bb63f12009-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 Lattner4a413b52009-12-30 02:11:14 +000059 MDNode *MDNodeVal;
60 MDString *MDStringVal;
Chris Lattner5bb63f12009-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 Lattner103ff152009-01-02 07:01:27 +000072 class LLParser {
73 public:
74 typedef LLLexer::LocTy LocTy;
75 private:
Owen Andersonc3644562009-07-01 23:57:11 +000076 LLVMContext& Context;
Chris Lattner103ff152009-01-02 07:01:27 +000077 LLLexer Lex;
78 Module *M;
Misha Brukman7d987212009-01-02 22:46:48 +000079
Chris Lattner103ff152009-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 Patela8261c62009-07-01 19:21:12 +000084 /// MetadataCache - This map keeps track of parsed metadata constants.
Chris Lattner9e9fa652009-12-29 21:43:58 +000085 std::map<unsigned, TrackingVH<MDNode> > MetadataCache;
86 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
Devang Patelb5896162009-09-28 21:14:55 +000087 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
Chris Lattner103ff152009-01-02 07:01:27 +000088 struct UpRefRecord {
89 /// Loc - This is the location of the upref.
90 LocTy Loc;
Misha Brukman7d987212009-01-02 22:46:48 +000091
Chris Lattner103ff152009-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 Brukman7d987212009-01-02 22:46:48 +000095
Chris Lattner103ff152009-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 Brukman7d987212009-01-02 22:46:48 +000099
Chris Lattner103ff152009-01-02 07:01:27 +0000100 /// UpRefTy - This is the actual opaque type that the upreference is
101 /// represented with.
102 OpaqueType *UpRefTy;
Misha Brukman7d987212009-01-02 22:46:48 +0000103
Chris Lattner103ff152009-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 Lattner5bb63f12009-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 Lattner103ff152009-01-02 07:01:27 +0000121 public:
Chris Lattner0ffa08f2009-07-02 23:08:13 +0000122 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Victor Hernandez6203d9d2009-10-17 00:00:19 +0000123 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
124 M(m), MallocF(NULL) {}
Chris Lattner9df8a4a2009-01-04 20:44:11 +0000125 bool Run();
Misha Brukman7d987212009-01-02 22:46:48 +0000126
Owen Anderson04cbeec2009-07-02 17:04:01 +0000127 LLVMContext& getContext() { return Context; }
128
Chris Lattner103ff152009-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 Brukman7d987212009-01-02 22:46:48 +0000137
Chris Lattner103ff152009-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 Brukman7d987212009-01-02 22:46:48 +0000143
Chris Lattner103ff152009-01-02 07:01:27 +0000144 // Helper Routines.
145 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner6226fd22009-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 Lattner103ff152009-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 Lattner6226fd22009-01-02 08:05:26 +0000160 bool ParseStringConstant(std::string &Result);
161 bool ParseUInt32(unsigned &Val);
162 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattner103ff152009-01-02 07:01:27 +0000163 Loc = Lex.getLoc();
Chris Lattner6226fd22009-01-02 08:05:26 +0000164 return ParseUInt32(Val);
Chris Lattner103ff152009-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 Patel5838baa2009-09-02 08:44:58 +0000173 bool ParseOptionalCallingConv(CallingConv::ID &CC);
Chris Lattner103ff152009-01-02 07:01:27 +0000174 bool ParseOptionalAlignment(unsigned &Alignment);
Devang Patel17c60df2009-09-29 00:01:14 +0000175 bool ParseOptionalCustomMetadata();
Devang Patel91c79232009-09-17 23:04:48 +0000176 bool ParseOptionalInfo(unsigned &Alignment);
Chris Lattner103ff152009-01-02 07:01:27 +0000177 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman7d987212009-01-02 22:46:48 +0000178
Chris Lattner103ff152009-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 Brukman7d987212009-01-02 22:46:48 +0000189
Chris Lattner103ff152009-01-02 07:01:27 +0000190 bool ParseGlobalType(bool &IsConstant);
Dan Gohman39c2d2f2009-08-12 23:32:33 +0000191 bool ParseUnnamedGlobal();
Chris Lattner103ff152009-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 Patela8261c62009-07-01 19:21:12 +0000196 bool ParseStandaloneMetadata();
Devang Patelf6e6aa02009-07-29 00:34:02 +0000197 bool ParseNamedMetadata();
Chris Lattnerb3d6fad2009-12-29 21:53:55 +0000198 bool ParseMDString(MDString *&Result);
199 bool ParseMDNode(MDNode *&Result);
Misha Brukman7d987212009-01-02 22:46:48 +0000200
Chris Lattner103ff152009-01-02 07:01:27 +0000201 // Type Parsing.
Chris Lattnerc3a8e192009-03-09 04:49:14 +0000202 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
203 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattner103ff152009-01-02 07:01:27 +0000204 Loc = Lex.getLoc();
Chris Lattnerc3a8e192009-03-09 04:49:14 +0000205 return ParseType(Result, AllowVoid);
Chris Lattner103ff152009-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);
216 bool ParseGlobalValue(const Type *Ty, Constant *&V);
217 bool ParseGlobalTypeAndValue(Constant *&V);
218 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewycky117f4382009-05-10 20:57:05 +0000219 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattner103ff152009-01-02 07:01:27 +0000220
Misha Brukman7d987212009-01-02 22:46:48 +0000221
Chris Lattner103ff152009-01-02 07:01:27 +0000222 // Function Semantic Analysis.
223 class PerFunctionState {
224 LLParser &P;
225 Function &F;
226 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
227 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
228 std::vector<Value*> NumberedVals;
Chris Lattner5bb63f12009-10-28 03:39:23 +0000229
230 /// FunctionNumber - If this is an unnamed function, this is the slot
231 /// number of it, otherwise it is -1.
232 int FunctionNumber;
Chris Lattner103ff152009-01-02 07:01:27 +0000233 public:
Chris Lattner5bb63f12009-10-28 03:39:23 +0000234 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattner103ff152009-01-02 07:01:27 +0000235 ~PerFunctionState();
Misha Brukman7d987212009-01-02 22:46:48 +0000236
Chris Lattner103ff152009-01-02 07:01:27 +0000237 Function &getFunction() const { return F; }
Misha Brukman7d987212009-01-02 22:46:48 +0000238
Chris Lattner5bb63f12009-10-28 03:39:23 +0000239 bool FinishFunction();
Misha Brukman7d987212009-01-02 22:46:48 +0000240
Chris Lattner103ff152009-01-02 07:01:27 +0000241 /// GetVal - Get a value with the specified name or ID, creating a
242 /// forward reference record if needed. This can return null if the value
243 /// exists but does not have the right type.
244 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
245 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman7d987212009-01-02 22:46:48 +0000246
Chris Lattner103ff152009-01-02 07:01:27 +0000247 /// SetInstName - After an instruction is parsed and inserted into its
248 /// basic block, this installs its name.
249 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
250 Instruction *Inst);
Misha Brukman7d987212009-01-02 22:46:48 +0000251
Chris Lattner103ff152009-01-02 07:01:27 +0000252 /// GetBB - Get a basic block with the specified name or ID, creating a
253 /// forward reference record if needed. This can return null if the value
254 /// is not a BasicBlock.
255 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
256 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman7d987212009-01-02 22:46:48 +0000257
Chris Lattner103ff152009-01-02 07:01:27 +0000258 /// DefineBB - Define the specified basic block, which is either named or
259 /// unnamed. If there is an error, this returns null otherwise it returns
260 /// the block being defined.
261 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
262 };
Misha Brukman7d987212009-01-02 22:46:48 +0000263
Chris Lattner103ff152009-01-02 07:01:27 +0000264 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
265 PerFunctionState &PFS);
Misha Brukman7d987212009-01-02 22:46:48 +0000266
Chris Lattner103ff152009-01-02 07:01:27 +0000267 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
268 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
269 PerFunctionState &PFS) {
270 Loc = Lex.getLoc();
271 return ParseValue(Ty, V, PFS);
272 }
Misha Brukman7d987212009-01-02 22:46:48 +0000273
Chris Lattner103ff152009-01-02 07:01:27 +0000274 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
275 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
276 Loc = Lex.getLoc();
277 return ParseTypeAndValue(V, PFS);
278 }
Chris Lattnere0787282009-10-27 19:13:16 +0000279 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
280 PerFunctionState &PFS);
281 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
282 LocTy Loc;
283 return ParseTypeAndBasicBlock(BB, Loc, PFS);
284 }
Victor Hernandez223c7142009-12-03 23:40:58 +0000285
Chris Lattner103ff152009-01-02 07:01:27 +0000286 struct ParamInfo {
287 LocTy Loc;
288 Value *V;
289 unsigned Attrs;
Misha Brukman7d987212009-01-02 22:46:48 +0000290 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattner103ff152009-01-02 07:01:27 +0000291 : Loc(loc), V(v), Attrs(attrs) {}
292 };
293 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
294 PerFunctionState &PFS);
Misha Brukman7d987212009-01-02 22:46:48 +0000295
Chris Lattner103ff152009-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 Lattnerd3dcc922009-01-05 18:34:07 +0000306 bool &isVarArg, bool inType);
Chris Lattner103ff152009-01-02 07:01:27 +0000307 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
308 bool ParseFunctionBody(Function &Fn);
309 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman7d987212009-01-02 22:46:48 +0000310
Chris Lattner103ff152009-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 Brukman7d987212009-01-02 22:46:48 +0000315
Chris Lattner103ff152009-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 Lattner4c3800f2009-10-28 00:19:10 +0000319 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattner103ff152009-01-02 07:01:27 +0000320 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman7d987212009-01-02 22:46:48 +0000321
Chris Lattner0d5bffc2009-01-05 08:24:46 +0000322 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
323 unsigned OperandType);
Chris Lattner103ff152009-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 Lattner0f928312009-01-05 08:18:44 +0000328 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattner103ff152009-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 Hernandez6203d9d2009-10-17 00:00:19 +0000334 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
335 BasicBlock *BB = 0, bool isAlloca = true);
Victor Hernandez93946082009-10-24 04:23:03 +0000336 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
Chris Lattner103ff152009-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 Lattner5bb63f12009-10-28 03:39:23 +0000343
344 bool ResolveForwardRefBlockAddresses(Function *TheFn,
345 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
346 PerFunctionState *PFS);
Chris Lattner103ff152009-01-02 07:01:27 +0000347 };
348} // End llvm namespace
349
350#endif