blob: 30a49cd8848049a18b8be43e458ee5a2c8d7faa8 [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;
Nick Lewycky21cc4462009-04-04 07:22:01 +000031 class MDString;
32 class MDNode;
Chris Lattnerdf986172009-01-02 07:01:27 +000033 struct ValID;
Misha Brukman9ea40342009-01-02 22:46:48 +000034
Chris Lattnerdf986172009-01-02 07:01:27 +000035 class LLParser {
36 public:
37 typedef LLLexer::LocTy LocTy;
38 private:
Owen Andersonfba933c2009-07-01 23:57:11 +000039 LLVMContext& Context;
Chris Lattnerdf986172009-01-02 07:01:27 +000040 LLLexer Lex;
41 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000042
Chris Lattnerdf986172009-01-02 07:01:27 +000043 // Type resolution handling data structures.
44 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
45 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
46 std::vector<PATypeHolder> NumberedTypes;
Devang Patel923078c2009-07-01 19:21:12 +000047 /// MetadataCache - This map keeps track of parsed metadata constants.
48 std::map<unsigned, Constant *> MetadataCache;
Devang Patel1c7eea62009-07-08 19:23:54 +000049 std::map<unsigned, std::pair<Constant *, LocTy> > ForwardRefMDNodes;
50
Chris Lattnerdf986172009-01-02 07:01:27 +000051 struct UpRefRecord {
52 /// Loc - This is the location of the upref.
53 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000054
Chris Lattnerdf986172009-01-02 07:01:27 +000055 /// NestingLevel - The number of nesting levels that need to be popped
56 /// before this type is resolved.
57 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000058
Chris Lattnerdf986172009-01-02 07:01:27 +000059 /// LastContainedTy - This is the type at the current binding level for
60 /// the type. Every time we reduce the nesting level, this gets updated.
61 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000062
Chris Lattnerdf986172009-01-02 07:01:27 +000063 /// UpRefTy - This is the actual opaque type that the upreference is
64 /// represented with.
65 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000066
Chris Lattnerdf986172009-01-02 07:01:27 +000067 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
68 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
69 UpRefTy(URTy) {}
70 };
71 std::vector<UpRefRecord> UpRefs;
72
73 // Global Value reference information.
74 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
75 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
76 std::vector<GlobalValue*> NumberedVals;
77 public:
Chris Lattnereeb4a842009-07-02 23:08:13 +000078 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
Owen Andersonff6c91e2009-07-07 18:44:11 +000079 Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +000080 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +000081
Owen Andersonb43eae72009-07-02 17:04:01 +000082 LLVMContext& getContext() { return Context; }
83
Chris Lattnerdf986172009-01-02 07:01:27 +000084 private:
85
86 bool Error(LocTy L, const std::string &Msg) const {
87 return Lex.Error(L, Msg);
88 }
89 bool TokError(const std::string &Msg) const {
90 return Error(Lex.getLoc(), Msg);
91 }
Misha Brukman9ea40342009-01-02 22:46:48 +000092
Chris Lattnerdf986172009-01-02 07:01:27 +000093 /// GetGlobalVal - Get a value with the specified name or ID, creating a
94 /// forward reference record if needed. This can return null if the value
95 /// exists but does not have the right type.
96 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
97 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +000098
Chris Lattnerdf986172009-01-02 07:01:27 +000099 // Helper Routines.
100 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000101 bool EatIfPresent(lltok::Kind T) {
102 if (Lex.getKind() != T) return false;
103 Lex.Lex();
104 return true;
105 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000106 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
107 if (Lex.getKind() != T) {
108 Present = false;
109 } else {
110 Lex.Lex();
111 Present = true;
112 }
113 return false;
114 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000115 bool ParseStringConstant(std::string &Result);
116 bool ParseUInt32(unsigned &Val);
117 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000118 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000119 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000120 }
121 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
122 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
123 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
124 bool ParseOptionalLinkage(unsigned &Linkage) {
125 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
126 }
127 bool ParseOptionalVisibility(unsigned &Visibility);
128 bool ParseOptionalCallingConv(unsigned &CC);
129 bool ParseOptionalAlignment(unsigned &Alignment);
130 bool ParseOptionalCommaAlignment(unsigned &Alignment);
131 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000132
Chris Lattnerdf986172009-01-02 07:01:27 +0000133 // Top-Level Entities
134 bool ParseTopLevelEntities();
135 bool ValidateEndOfModule();
136 bool ParseTargetDefinition();
137 bool ParseDepLibs();
138 bool ParseModuleAsm();
139 bool ParseUnnamedType();
140 bool ParseNamedType();
141 bool ParseDeclare();
142 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000143
Chris Lattnerdf986172009-01-02 07:01:27 +0000144 bool ParseGlobalType(bool &IsConstant);
145 bool ParseNamedGlobal();
146 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
147 bool HasLinkage, unsigned Visibility);
148 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Devang Patel923078c2009-07-01 19:21:12 +0000149 bool ParseStandaloneMetadata();
Misha Brukman9ea40342009-01-02 22:46:48 +0000150
Chris Lattnerdf986172009-01-02 07:01:27 +0000151 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000152 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
153 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000154 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000155 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000156 }
157 bool ParseTypeRec(PATypeHolder &H);
158 bool ParseStructType(PATypeHolder &H, bool Packed);
159 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
160 bool ParseFunctionType(PATypeHolder &Result);
161 PATypeHolder HandleUpRefs(const Type *Ty);
162
163 // Constants.
164 bool ParseValID(ValID &ID);
165 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
166 bool ParseGlobalValue(const Type *Ty, Constant *&V);
167 bool ParseGlobalTypeAndValue(Constant *&V);
168 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewyckycb337992009-05-10 20:57:05 +0000169 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000170
Misha Brukman9ea40342009-01-02 22:46:48 +0000171
Chris Lattnerdf986172009-01-02 07:01:27 +0000172 // Function Semantic Analysis.
173 class PerFunctionState {
174 LLParser &P;
175 Function &F;
176 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
177 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
178 std::vector<Value*> NumberedVals;
179 public:
180 PerFunctionState(LLParser &p, Function &f);
181 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000182
Chris Lattnerdf986172009-01-02 07:01:27 +0000183 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000184
Chris Lattnerdf986172009-01-02 07:01:27 +0000185 bool VerifyFunctionComplete();
Misha Brukman9ea40342009-01-02 22:46:48 +0000186
Chris Lattnerdf986172009-01-02 07:01:27 +0000187 /// GetVal - Get a value with the specified name or ID, creating a
188 /// forward reference record if needed. This can return null if the value
189 /// exists but does not have the right type.
190 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
191 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000192
Chris Lattnerdf986172009-01-02 07:01:27 +0000193 /// SetInstName - After an instruction is parsed and inserted into its
194 /// basic block, this installs its name.
195 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
196 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000197
Chris Lattnerdf986172009-01-02 07:01:27 +0000198 /// GetBB - Get a basic block with the specified name or ID, creating a
199 /// forward reference record if needed. This can return null if the value
200 /// is not a BasicBlock.
201 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
202 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000203
Chris Lattnerdf986172009-01-02 07:01:27 +0000204 /// DefineBB - Define the specified basic block, which is either named or
205 /// unnamed. If there is an error, this returns null otherwise it returns
206 /// the block being defined.
207 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
208 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000209
Chris Lattnerdf986172009-01-02 07:01:27 +0000210 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
211 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000212
Chris Lattnerdf986172009-01-02 07:01:27 +0000213 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
214 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
215 PerFunctionState &PFS) {
216 Loc = Lex.getLoc();
217 return ParseValue(Ty, V, PFS);
218 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000219
Chris Lattnerdf986172009-01-02 07:01:27 +0000220 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
221 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
222 Loc = Lex.getLoc();
223 return ParseTypeAndValue(V, PFS);
224 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000225
Chris Lattnerdf986172009-01-02 07:01:27 +0000226 struct ParamInfo {
227 LocTy Loc;
228 Value *V;
229 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000230 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000231 : Loc(loc), V(v), Attrs(attrs) {}
232 };
233 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
234 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000235
Chris Lattnerdf986172009-01-02 07:01:27 +0000236 // Function Parsing.
237 struct ArgInfo {
238 LocTy Loc;
239 PATypeHolder Type;
240 unsigned Attrs;
241 std::string Name;
242 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
243 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
244 };
245 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000246 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000247 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
248 bool ParseFunctionBody(Function &Fn);
249 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000250
Chris Lattnerdf986172009-01-02 07:01:27 +0000251 // Instruction Parsing.
252 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
253 PerFunctionState &PFS);
254 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000255
Chris Lattnerdf986172009-01-02 07:01:27 +0000256 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
257 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
258 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
259 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000260
Chris Lattnere914b592009-01-05 08:24:46 +0000261 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
262 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000263 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
264 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
265 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
266 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000267 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000268 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
269 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
270 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
271 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
272 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
273 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
274 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
275 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
276 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
277 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
278 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
279 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
280 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
281 };
282} // End llvm namespace
283
284#endif