blob: ae1d9c7665db501ff0e4f9a8712c863521c70ea5 [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;
Chris Lattnerdf986172009-01-02 07:01:27 +000049 struct UpRefRecord {
50 /// Loc - This is the location of the upref.
51 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000052
Chris Lattnerdf986172009-01-02 07:01:27 +000053 /// NestingLevel - The number of nesting levels that need to be popped
54 /// before this type is resolved.
55 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000056
Chris Lattnerdf986172009-01-02 07:01:27 +000057 /// LastContainedTy - This is the type at the current binding level for
58 /// the type. Every time we reduce the nesting level, this gets updated.
59 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000060
Chris Lattnerdf986172009-01-02 07:01:27 +000061 /// UpRefTy - This is the actual opaque type that the upreference is
62 /// represented with.
63 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000064
Chris Lattnerdf986172009-01-02 07:01:27 +000065 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
66 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
67 UpRefTy(URTy) {}
68 };
69 std::vector<UpRefRecord> UpRefs;
70
71 // Global Value reference information.
72 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
73 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
74 std::vector<GlobalValue*> NumberedVals;
75 public:
Owen Andersonfba933c2009-07-01 23:57:11 +000076 LLParser(MemoryBuffer *F, ParseError &Err, Module *m) :
77 Context(m->getContext()), Lex(F, Err), M(m) {}
Chris Lattnerad7d1e22009-01-04 20:44:11 +000078 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +000079
Chris Lattnerdf986172009-01-02 07:01:27 +000080 private:
81
82 bool Error(LocTy L, const std::string &Msg) const {
83 return Lex.Error(L, Msg);
84 }
85 bool TokError(const std::string &Msg) const {
86 return Error(Lex.getLoc(), Msg);
87 }
Misha Brukman9ea40342009-01-02 22:46:48 +000088
Chris Lattnerdf986172009-01-02 07:01:27 +000089 /// GetGlobalVal - Get a value with the specified name or ID, creating a
90 /// forward reference record if needed. This can return null if the value
91 /// exists but does not have the right type.
92 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
93 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +000094
Chris Lattnerdf986172009-01-02 07:01:27 +000095 // Helper Routines.
96 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +000097 bool EatIfPresent(lltok::Kind T) {
98 if (Lex.getKind() != T) return false;
99 Lex.Lex();
100 return true;
101 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000102 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
103 if (Lex.getKind() != T) {
104 Present = false;
105 } else {
106 Lex.Lex();
107 Present = true;
108 }
109 return false;
110 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000111 bool ParseStringConstant(std::string &Result);
112 bool ParseUInt32(unsigned &Val);
113 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000114 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000115 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000116 }
117 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
118 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
119 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
120 bool ParseOptionalLinkage(unsigned &Linkage) {
121 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
122 }
123 bool ParseOptionalVisibility(unsigned &Visibility);
124 bool ParseOptionalCallingConv(unsigned &CC);
125 bool ParseOptionalAlignment(unsigned &Alignment);
126 bool ParseOptionalCommaAlignment(unsigned &Alignment);
127 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000128
Chris Lattnerdf986172009-01-02 07:01:27 +0000129 // Top-Level Entities
130 bool ParseTopLevelEntities();
131 bool ValidateEndOfModule();
132 bool ParseTargetDefinition();
133 bool ParseDepLibs();
134 bool ParseModuleAsm();
135 bool ParseUnnamedType();
136 bool ParseNamedType();
137 bool ParseDeclare();
138 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000139
Chris Lattnerdf986172009-01-02 07:01:27 +0000140 bool ParseGlobalType(bool &IsConstant);
141 bool ParseNamedGlobal();
142 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
143 bool HasLinkage, unsigned Visibility);
144 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Devang Patel923078c2009-07-01 19:21:12 +0000145 bool ParseStandaloneMetadata();
Misha Brukman9ea40342009-01-02 22:46:48 +0000146
Chris Lattnerdf986172009-01-02 07:01:27 +0000147 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000148 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
149 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000150 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000151 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000152 }
153 bool ParseTypeRec(PATypeHolder &H);
154 bool ParseStructType(PATypeHolder &H, bool Packed);
155 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
156 bool ParseFunctionType(PATypeHolder &Result);
157 PATypeHolder HandleUpRefs(const Type *Ty);
158
159 // Constants.
160 bool ParseValID(ValID &ID);
161 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
162 bool ParseGlobalValue(const Type *Ty, Constant *&V);
163 bool ParseGlobalTypeAndValue(Constant *&V);
164 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewyckycb337992009-05-10 20:57:05 +0000165 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000166
Misha Brukman9ea40342009-01-02 22:46:48 +0000167
Chris Lattnerdf986172009-01-02 07:01:27 +0000168 // Function Semantic Analysis.
169 class PerFunctionState {
170 LLParser &P;
171 Function &F;
172 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
173 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
174 std::vector<Value*> NumberedVals;
175 public:
176 PerFunctionState(LLParser &p, Function &f);
177 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000178
Chris Lattnerdf986172009-01-02 07:01:27 +0000179 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000180
Chris Lattnerdf986172009-01-02 07:01:27 +0000181 bool VerifyFunctionComplete();
Misha Brukman9ea40342009-01-02 22:46:48 +0000182
Chris Lattnerdf986172009-01-02 07:01:27 +0000183 /// GetVal - Get a value with the specified name or ID, creating a
184 /// forward reference record if needed. This can return null if the value
185 /// exists but does not have the right type.
186 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
187 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000188
Chris Lattnerdf986172009-01-02 07:01:27 +0000189 /// SetInstName - After an instruction is parsed and inserted into its
190 /// basic block, this installs its name.
191 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
192 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000193
Chris Lattnerdf986172009-01-02 07:01:27 +0000194 /// GetBB - Get a basic block with the specified name or ID, creating a
195 /// forward reference record if needed. This can return null if the value
196 /// is not a BasicBlock.
197 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
198 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000199
Chris Lattnerdf986172009-01-02 07:01:27 +0000200 /// DefineBB - Define the specified basic block, which is either named or
201 /// unnamed. If there is an error, this returns null otherwise it returns
202 /// the block being defined.
203 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
204 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000205
Chris Lattnerdf986172009-01-02 07:01:27 +0000206 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
207 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000208
Chris Lattnerdf986172009-01-02 07:01:27 +0000209 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
210 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
211 PerFunctionState &PFS) {
212 Loc = Lex.getLoc();
213 return ParseValue(Ty, V, PFS);
214 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000215
Chris Lattnerdf986172009-01-02 07:01:27 +0000216 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
217 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
218 Loc = Lex.getLoc();
219 return ParseTypeAndValue(V, PFS);
220 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000221
Chris Lattnerdf986172009-01-02 07:01:27 +0000222 struct ParamInfo {
223 LocTy Loc;
224 Value *V;
225 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000226 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000227 : Loc(loc), V(v), Attrs(attrs) {}
228 };
229 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
230 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000231
Chris Lattnerdf986172009-01-02 07:01:27 +0000232 // Function Parsing.
233 struct ArgInfo {
234 LocTy Loc;
235 PATypeHolder Type;
236 unsigned Attrs;
237 std::string Name;
238 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
239 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
240 };
241 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000242 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000243 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
244 bool ParseFunctionBody(Function &Fn);
245 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000246
Chris Lattnerdf986172009-01-02 07:01:27 +0000247 // Instruction Parsing.
248 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
249 PerFunctionState &PFS);
250 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000251
Chris Lattnerdf986172009-01-02 07:01:27 +0000252 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
253 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
254 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
255 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000256
Chris Lattnere914b592009-01-05 08:24:46 +0000257 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
258 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000259 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
260 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
261 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
262 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000263 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000264 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
265 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
266 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
267 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
268 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
269 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
270 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
271 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
272 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
273 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
274 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
275 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
276 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
277 };
278} // End llvm namespace
279
280#endif