blob: 4bdbbfeab2193ec8692a0ff139316550d43b0f57 [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"
20#include <map>
21
22namespace llvm {
23 class Module;
24 class OpaqueType;
25 class Function;
26 class Value;
27 class BasicBlock;
28 class Instruction;
29 class Constant;
30 class GlobalValue;
Devang Patele54abc92009-07-22 17:43:22 +000031 class MetadataBase;
Nick Lewycky21cc4462009-04-04 07:22:01 +000032 class MDString;
33 class MDNode;
Chris Lattnerdf986172009-01-02 07:01:27 +000034 struct ValID;
Misha Brukman9ea40342009-01-02 22:46:48 +000035
Chris Lattnerdf986172009-01-02 07:01:27 +000036 class LLParser {
37 public:
38 typedef LLLexer::LocTy LocTy;
39 private:
Owen Andersonfba933c2009-07-01 23:57:11 +000040 LLVMContext& Context;
Chris Lattnerdf986172009-01-02 07:01:27 +000041 LLLexer Lex;
42 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000043
Chris Lattnerdf986172009-01-02 07:01:27 +000044 // Type resolution handling data structures.
45 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
46 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
47 std::vector<PATypeHolder> NumberedTypes;
Devang Patel923078c2009-07-01 19:21:12 +000048 /// MetadataCache - This map keeps track of parsed metadata constants.
Devang Patel104cf9e2009-07-23 01:07:34 +000049 std::map<unsigned, MetadataBase *> MetadataCache;
50 std::map<unsigned, std::pair<MetadataBase *, LocTy> > ForwardRefMDNodes;
Devang Patel1c7eea62009-07-08 19:23:54 +000051
Chris Lattnerdf986172009-01-02 07:01:27 +000052 struct UpRefRecord {
53 /// Loc - This is the location of the upref.
54 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000055
Chris Lattnerdf986172009-01-02 07:01:27 +000056 /// NestingLevel - The number of nesting levels that need to be popped
57 /// before this type is resolved.
58 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000059
Chris Lattnerdf986172009-01-02 07:01:27 +000060 /// LastContainedTy - This is the type at the current binding level for
61 /// the type. Every time we reduce the nesting level, this gets updated.
62 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000063
Chris Lattnerdf986172009-01-02 07:01:27 +000064 /// UpRefTy - This is the actual opaque type that the upreference is
65 /// represented with.
66 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000067
Chris Lattnerdf986172009-01-02 07:01:27 +000068 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
69 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
70 UpRefTy(URTy) {}
71 };
72 std::vector<UpRefRecord> UpRefs;
73
74 // Global Value reference information.
75 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
76 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
77 std::vector<GlobalValue*> NumberedVals;
78 public:
Chris Lattnereeb4a842009-07-02 23:08:13 +000079 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Owen Andersonff6c91e2009-07-07 18:44:11 +000080 Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +000081 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +000082
Owen Andersonb43eae72009-07-02 17:04:01 +000083 LLVMContext& getContext() { return Context; }
84
Chris Lattnerdf986172009-01-02 07:01:27 +000085 private:
86
87 bool Error(LocTy L, const std::string &Msg) const {
88 return Lex.Error(L, Msg);
89 }
90 bool TokError(const std::string &Msg) const {
91 return Error(Lex.getLoc(), Msg);
92 }
Misha Brukman9ea40342009-01-02 22:46:48 +000093
Chris Lattnerdf986172009-01-02 07:01:27 +000094 /// GetGlobalVal - Get a value with the specified name or ID, creating a
95 /// forward reference record if needed. This can return null if the value
96 /// exists but does not have the right type.
97 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
98 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +000099
Chris Lattnerdf986172009-01-02 07:01:27 +0000100 // Helper Routines.
101 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000102 bool EatIfPresent(lltok::Kind T) {
103 if (Lex.getKind() != T) return false;
104 Lex.Lex();
105 return true;
106 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000107 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
108 if (Lex.getKind() != T) {
109 Present = false;
110 } else {
111 Lex.Lex();
112 Present = true;
113 }
114 return false;
115 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000116 bool ParseStringConstant(std::string &Result);
117 bool ParseUInt32(unsigned &Val);
118 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000119 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000120 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000121 }
122 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
123 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
124 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
125 bool ParseOptionalLinkage(unsigned &Linkage) {
126 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
127 }
128 bool ParseOptionalVisibility(unsigned &Visibility);
129 bool ParseOptionalCallingConv(unsigned &CC);
130 bool ParseOptionalAlignment(unsigned &Alignment);
131 bool ParseOptionalCommaAlignment(unsigned &Alignment);
132 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000133
Chris Lattnerdf986172009-01-02 07:01:27 +0000134 // Top-Level Entities
135 bool ParseTopLevelEntities();
136 bool ValidateEndOfModule();
137 bool ParseTargetDefinition();
138 bool ParseDepLibs();
139 bool ParseModuleAsm();
140 bool ParseUnnamedType();
141 bool ParseNamedType();
142 bool ParseDeclare();
143 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000144
Chris Lattnerdf986172009-01-02 07:01:27 +0000145 bool ParseGlobalType(bool &IsConstant);
Dan Gohman3845e502009-08-12 23:32:33 +0000146 bool ParseUnnamedGlobal();
Chris Lattnerdf986172009-01-02 07:01:27 +0000147 bool ParseNamedGlobal();
148 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
149 bool HasLinkage, unsigned Visibility);
150 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Devang Patel923078c2009-07-01 19:21:12 +0000151 bool ParseStandaloneMetadata();
Devang Pateleff2ab62009-07-29 00:34:02 +0000152 bool ParseNamedMetadata();
Devang Patele54abc92009-07-22 17:43:22 +0000153 bool ParseMDString(MetadataBase *&S);
Devang Patel104cf9e2009-07-23 01:07:34 +0000154 bool ParseMDNode(MetadataBase *&N);
Misha Brukman9ea40342009-01-02 22:46:48 +0000155
Chris Lattnerdf986172009-01-02 07:01:27 +0000156 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000157 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
158 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000159 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000160 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000161 }
162 bool ParseTypeRec(PATypeHolder &H);
163 bool ParseStructType(PATypeHolder &H, bool Packed);
164 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
165 bool ParseFunctionType(PATypeHolder &Result);
166 PATypeHolder HandleUpRefs(const Type *Ty);
167
168 // Constants.
169 bool ParseValID(ValID &ID);
170 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
171 bool ParseGlobalValue(const Type *Ty, Constant *&V);
172 bool ParseGlobalTypeAndValue(Constant *&V);
173 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewyckycb337992009-05-10 20:57:05 +0000174 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000175
Misha Brukman9ea40342009-01-02 22:46:48 +0000176
Chris Lattnerdf986172009-01-02 07:01:27 +0000177 // Function Semantic Analysis.
178 class PerFunctionState {
179 LLParser &P;
180 Function &F;
181 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
182 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
183 std::vector<Value*> NumberedVals;
184 public:
185 PerFunctionState(LLParser &p, Function &f);
186 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000187
Chris Lattnerdf986172009-01-02 07:01:27 +0000188 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000189
Chris Lattnerdf986172009-01-02 07:01:27 +0000190 bool VerifyFunctionComplete();
Misha Brukman9ea40342009-01-02 22:46:48 +0000191
Chris Lattnerdf986172009-01-02 07:01:27 +0000192 /// GetVal - Get a value with the specified name or ID, creating a
193 /// forward reference record if needed. This can return null if the value
194 /// exists but does not have the right type.
195 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
196 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000197
Chris Lattnerdf986172009-01-02 07:01:27 +0000198 /// SetInstName - After an instruction is parsed and inserted into its
199 /// basic block, this installs its name.
200 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
201 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000202
Chris Lattnerdf986172009-01-02 07:01:27 +0000203 /// GetBB - Get a basic block with the specified name or ID, creating a
204 /// forward reference record if needed. This can return null if the value
205 /// is not a BasicBlock.
206 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
207 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000208
Chris Lattnerdf986172009-01-02 07:01:27 +0000209 /// DefineBB - Define the specified basic block, which is either named or
210 /// unnamed. If there is an error, this returns null otherwise it returns
211 /// the block being defined.
212 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
213 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000214
Chris Lattnerdf986172009-01-02 07:01:27 +0000215 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
216 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000217
Chris Lattnerdf986172009-01-02 07:01:27 +0000218 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
219 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
220 PerFunctionState &PFS) {
221 Loc = Lex.getLoc();
222 return ParseValue(Ty, V, PFS);
223 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000224
Chris Lattnerdf986172009-01-02 07:01:27 +0000225 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
226 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
227 Loc = Lex.getLoc();
228 return ParseTypeAndValue(V, PFS);
229 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000230
Chris Lattnerdf986172009-01-02 07:01:27 +0000231 struct ParamInfo {
232 LocTy Loc;
233 Value *V;
234 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000235 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000236 : Loc(loc), V(v), Attrs(attrs) {}
237 };
238 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
239 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000240
Chris Lattnerdf986172009-01-02 07:01:27 +0000241 // Function Parsing.
242 struct ArgInfo {
243 LocTy Loc;
244 PATypeHolder Type;
245 unsigned Attrs;
246 std::string Name;
247 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
248 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
249 };
250 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000251 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000252 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
253 bool ParseFunctionBody(Function &Fn);
254 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000255
Chris Lattnerdf986172009-01-02 07:01:27 +0000256 // Instruction Parsing.
257 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
258 PerFunctionState &PFS);
259 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000260
Chris Lattnerdf986172009-01-02 07:01:27 +0000261 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
262 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
263 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
264 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000265
Chris Lattnere914b592009-01-05 08:24:46 +0000266 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
267 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000268 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
269 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
270 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
271 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000272 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000273 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
274 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
275 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
276 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
277 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
278 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
279 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
280 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
281 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
282 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
283 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
284 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
285 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
286 };
287} // End llvm namespace
288
289#endif