blob: 44f4c2a6524d16ac25307252880aced08c7921fa [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"
18#include "llvm/Type.h"
19#include <map>
20
21namespace llvm {
22 class Module;
23 class OpaqueType;
24 class Function;
25 class Value;
26 class BasicBlock;
27 class Instruction;
28 class Constant;
29 class GlobalValue;
Nick Lewycky21cc4462009-04-04 07:22:01 +000030 class MDString;
31 class MDNode;
Chris Lattnerdf986172009-01-02 07:01:27 +000032 struct ValID;
Misha Brukman9ea40342009-01-02 22:46:48 +000033
Chris Lattnerdf986172009-01-02 07:01:27 +000034 class LLParser {
35 public:
36 typedef LLLexer::LocTy LocTy;
37 private:
Misha Brukman9ea40342009-01-02 22:46:48 +000038
Chris Lattnerdf986172009-01-02 07:01:27 +000039 LLLexer Lex;
40 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000041
Chris Lattnerdf986172009-01-02 07:01:27 +000042 // Type resolution handling data structures.
43 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
44 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
45 std::vector<PATypeHolder> NumberedTypes;
Misha Brukman9ea40342009-01-02 22:46:48 +000046
Chris Lattnerdf986172009-01-02 07:01:27 +000047 struct UpRefRecord {
48 /// Loc - This is the location of the upref.
49 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000050
Chris Lattnerdf986172009-01-02 07:01:27 +000051 /// NestingLevel - The number of nesting levels that need to be popped
52 /// before this type is resolved.
53 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000054
Chris Lattnerdf986172009-01-02 07:01:27 +000055 /// LastContainedTy - This is the type at the current binding level for
56 /// the type. Every time we reduce the nesting level, this gets updated.
57 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000058
Chris Lattnerdf986172009-01-02 07:01:27 +000059 /// UpRefTy - This is the actual opaque type that the upreference is
60 /// represented with.
61 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000062
Chris Lattnerdf986172009-01-02 07:01:27 +000063 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
64 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
65 UpRefTy(URTy) {}
66 };
67 std::vector<UpRefRecord> UpRefs;
68
69 // Global Value reference information.
70 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
71 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
72 std::vector<GlobalValue*> NumberedVals;
73 public:
Chris Lattnerad7d1e22009-01-04 20:44:11 +000074 LLParser(MemoryBuffer *F, ParseError &Err, Module *m) : Lex(F, Err), M(m) {}
75 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +000076
Chris Lattnerdf986172009-01-02 07:01:27 +000077 private:
78
79 bool Error(LocTy L, const std::string &Msg) const {
80 return Lex.Error(L, Msg);
81 }
82 bool TokError(const std::string &Msg) const {
83 return Error(Lex.getLoc(), Msg);
84 }
Misha Brukman9ea40342009-01-02 22:46:48 +000085
Chris Lattnerdf986172009-01-02 07:01:27 +000086 /// GetGlobalVal - Get a value with the specified name or ID, creating a
87 /// forward reference record if needed. This can return null if the value
88 /// exists but does not have the right type.
89 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
90 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +000091
Chris Lattnerdf986172009-01-02 07:01:27 +000092 // Helper Routines.
93 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +000094 bool EatIfPresent(lltok::Kind T) {
95 if (Lex.getKind() != T) return false;
96 Lex.Lex();
97 return true;
98 }
Chris Lattnerdf986172009-01-02 07:01:27 +000099 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
100 if (Lex.getKind() != T) {
101 Present = false;
102 } else {
103 Lex.Lex();
104 Present = true;
105 }
106 return false;
107 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000108 bool ParseStringConstant(std::string &Result);
109 bool ParseUInt32(unsigned &Val);
110 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000111 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000112 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000113 }
114 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
115 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
116 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
117 bool ParseOptionalLinkage(unsigned &Linkage) {
118 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
119 }
120 bool ParseOptionalVisibility(unsigned &Visibility);
121 bool ParseOptionalCallingConv(unsigned &CC);
122 bool ParseOptionalAlignment(unsigned &Alignment);
123 bool ParseOptionalCommaAlignment(unsigned &Alignment);
124 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000125
Chris Lattnerdf986172009-01-02 07:01:27 +0000126 // Top-Level Entities
127 bool ParseTopLevelEntities();
128 bool ValidateEndOfModule();
129 bool ParseTargetDefinition();
130 bool ParseDepLibs();
131 bool ParseModuleAsm();
132 bool ParseUnnamedType();
133 bool ParseNamedType();
134 bool ParseDeclare();
135 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000136
Chris Lattnerdf986172009-01-02 07:01:27 +0000137 bool ParseGlobalType(bool &IsConstant);
138 bool ParseNamedGlobal();
139 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
140 bool HasLinkage, unsigned Visibility);
141 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Misha Brukman9ea40342009-01-02 22:46:48 +0000142
Chris Lattnerdf986172009-01-02 07:01:27 +0000143 // Type Parsing.
Chris Lattnera9a9e072009-03-09 04:49:14 +0000144 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
145 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000146 Loc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +0000147 return ParseType(Result, AllowVoid);
Chris Lattnerdf986172009-01-02 07:01:27 +0000148 }
149 bool ParseTypeRec(PATypeHolder &H);
150 bool ParseStructType(PATypeHolder &H, bool Packed);
151 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
152 bool ParseFunctionType(PATypeHolder &Result);
153 PATypeHolder HandleUpRefs(const Type *Ty);
154
155 // Constants.
156 bool ParseValID(ValID &ID);
157 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
158 bool ParseGlobalValue(const Type *Ty, Constant *&V);
159 bool ParseGlobalTypeAndValue(Constant *&V);
160 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
Nick Lewycky21cc4462009-04-04 07:22:01 +0000161 bool ParseMDNodeVector(SmallVectorImpl<Constant*> &);
Chris Lattnerdf986172009-01-02 07:01:27 +0000162
Misha Brukman9ea40342009-01-02 22:46:48 +0000163
Chris Lattnerdf986172009-01-02 07:01:27 +0000164 // Function Semantic Analysis.
165 class PerFunctionState {
166 LLParser &P;
167 Function &F;
168 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
169 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
170 std::vector<Value*> NumberedVals;
171 public:
172 PerFunctionState(LLParser &p, Function &f);
173 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000174
Chris Lattnerdf986172009-01-02 07:01:27 +0000175 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000176
Chris Lattnerdf986172009-01-02 07:01:27 +0000177 bool VerifyFunctionComplete();
Misha Brukman9ea40342009-01-02 22:46:48 +0000178
Chris Lattnerdf986172009-01-02 07:01:27 +0000179 /// GetVal - Get a value with the specified name or ID, creating a
180 /// forward reference record if needed. This can return null if the value
181 /// exists but does not have the right type.
182 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
183 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000184
Chris Lattnerdf986172009-01-02 07:01:27 +0000185 /// SetInstName - After an instruction is parsed and inserted into its
186 /// basic block, this installs its name.
187 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
188 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000189
Chris Lattnerdf986172009-01-02 07:01:27 +0000190 /// GetBB - Get a basic block with the specified name or ID, creating a
191 /// forward reference record if needed. This can return null if the value
192 /// is not a BasicBlock.
193 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
194 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000195
Chris Lattnerdf986172009-01-02 07:01:27 +0000196 /// DefineBB - Define the specified basic block, which is either named or
197 /// unnamed. If there is an error, this returns null otherwise it returns
198 /// the block being defined.
199 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
200 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000201
Chris Lattnerdf986172009-01-02 07:01:27 +0000202 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
203 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000204
Chris Lattnerdf986172009-01-02 07:01:27 +0000205 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
206 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
207 PerFunctionState &PFS) {
208 Loc = Lex.getLoc();
209 return ParseValue(Ty, V, PFS);
210 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000211
Chris Lattnerdf986172009-01-02 07:01:27 +0000212 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
213 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
214 Loc = Lex.getLoc();
215 return ParseTypeAndValue(V, PFS);
216 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000217
Chris Lattnerdf986172009-01-02 07:01:27 +0000218 struct ParamInfo {
219 LocTy Loc;
220 Value *V;
221 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000222 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000223 : Loc(loc), V(v), Attrs(attrs) {}
224 };
225 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
226 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000227
Chris Lattnerdf986172009-01-02 07:01:27 +0000228 // Function Parsing.
229 struct ArgInfo {
230 LocTy Loc;
231 PATypeHolder Type;
232 unsigned Attrs;
233 std::string Name;
234 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
235 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
236 };
237 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +0000238 bool &isVarArg, bool inType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000239 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
240 bool ParseFunctionBody(Function &Fn);
241 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000242
Chris Lattnerdf986172009-01-02 07:01:27 +0000243 // Instruction Parsing.
244 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
245 PerFunctionState &PFS);
246 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000247
Chris Lattnerdf986172009-01-02 07:01:27 +0000248 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
249 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
250 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
251 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000252
Chris Lattnere914b592009-01-05 08:24:46 +0000253 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
254 unsigned OperandType);
Chris Lattnerdf986172009-01-02 07:01:27 +0000255 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
256 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
257 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
258 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +0000259 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +0000260 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
261 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
262 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
263 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
264 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
265 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
266 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
267 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
268 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
269 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
270 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
271 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
272 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
273 };
274} // End llvm namespace
275
276#endif