blob: e064c686b34b34be6056595ec3e790455792a5f4 [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;
30 struct ValID;
Misha Brukman9ea40342009-01-02 22:46:48 +000031
Chris Lattnerdf986172009-01-02 07:01:27 +000032 class LLParser {
33 public:
34 typedef LLLexer::LocTy LocTy;
35 private:
Misha Brukman9ea40342009-01-02 22:46:48 +000036
Chris Lattnerdf986172009-01-02 07:01:27 +000037 LLLexer Lex;
38 Module *M;
Misha Brukman9ea40342009-01-02 22:46:48 +000039
Chris Lattnerdf986172009-01-02 07:01:27 +000040 // Type resolution handling data structures.
41 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
42 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
43 std::vector<PATypeHolder> NumberedTypes;
Misha Brukman9ea40342009-01-02 22:46:48 +000044
Chris Lattnerdf986172009-01-02 07:01:27 +000045 struct UpRefRecord {
46 /// Loc - This is the location of the upref.
47 LocTy Loc;
Misha Brukman9ea40342009-01-02 22:46:48 +000048
Chris Lattnerdf986172009-01-02 07:01:27 +000049 /// NestingLevel - The number of nesting levels that need to be popped
50 /// before this type is resolved.
51 unsigned NestingLevel;
Misha Brukman9ea40342009-01-02 22:46:48 +000052
Chris Lattnerdf986172009-01-02 07:01:27 +000053 /// LastContainedTy - This is the type at the current binding level for
54 /// the type. Every time we reduce the nesting level, this gets updated.
55 const Type *LastContainedTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000056
Chris Lattnerdf986172009-01-02 07:01:27 +000057 /// UpRefTy - This is the actual opaque type that the upreference is
58 /// represented with.
59 OpaqueType *UpRefTy;
Misha Brukman9ea40342009-01-02 22:46:48 +000060
Chris Lattnerdf986172009-01-02 07:01:27 +000061 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
62 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
63 UpRefTy(URTy) {}
64 };
65 std::vector<UpRefRecord> UpRefs;
66
67 // Global Value reference information.
68 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
69 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
70 std::vector<GlobalValue*> NumberedVals;
71 public:
Chris Lattnerad7d1e22009-01-04 20:44:11 +000072 LLParser(MemoryBuffer *F, ParseError &Err, Module *m) : Lex(F, Err), M(m) {}
73 bool Run();
Misha Brukman9ea40342009-01-02 22:46:48 +000074
Chris Lattnerdf986172009-01-02 07:01:27 +000075 private:
76
77 bool Error(LocTy L, const std::string &Msg) const {
78 return Lex.Error(L, Msg);
79 }
80 bool TokError(const std::string &Msg) const {
81 return Error(Lex.getLoc(), Msg);
82 }
Misha Brukman9ea40342009-01-02 22:46:48 +000083
Chris Lattnerdf986172009-01-02 07:01:27 +000084 /// GetGlobalVal - Get a value with the specified name or ID, creating a
85 /// forward reference record if needed. This can return null if the value
86 /// exists but does not have the right type.
87 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
88 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +000089
Chris Lattnerdf986172009-01-02 07:01:27 +000090 // Helper Routines.
91 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3ed88ef2009-01-02 08:05:26 +000092 bool EatIfPresent(lltok::Kind T) {
93 if (Lex.getKind() != T) return false;
94 Lex.Lex();
95 return true;
96 }
Chris Lattnerdf986172009-01-02 07:01:27 +000097 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
98 if (Lex.getKind() != T) {
99 Present = false;
100 } else {
101 Lex.Lex();
102 Present = true;
103 }
104 return false;
105 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000106 bool ParseStringConstant(std::string &Result);
107 bool ParseUInt32(unsigned &Val);
108 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000109 Loc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000110 return ParseUInt32(Val);
Chris Lattnerdf986172009-01-02 07:01:27 +0000111 }
112 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
113 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
114 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
115 bool ParseOptionalLinkage(unsigned &Linkage) {
116 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
117 }
118 bool ParseOptionalVisibility(unsigned &Visibility);
119 bool ParseOptionalCallingConv(unsigned &CC);
120 bool ParseOptionalAlignment(unsigned &Alignment);
121 bool ParseOptionalCommaAlignment(unsigned &Alignment);
122 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
Misha Brukman9ea40342009-01-02 22:46:48 +0000123
Chris Lattnerdf986172009-01-02 07:01:27 +0000124 // Top-Level Entities
125 bool ParseTopLevelEntities();
126 bool ValidateEndOfModule();
127 bool ParseTargetDefinition();
128 bool ParseDepLibs();
129 bool ParseModuleAsm();
130 bool ParseUnnamedType();
131 bool ParseNamedType();
132 bool ParseDeclare();
133 bool ParseDefine();
Misha Brukman9ea40342009-01-02 22:46:48 +0000134
Chris Lattnerdf986172009-01-02 07:01:27 +0000135 bool ParseGlobalType(bool &IsConstant);
136 bool ParseNamedGlobal();
137 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
138 bool HasLinkage, unsigned Visibility);
139 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
Misha Brukman9ea40342009-01-02 22:46:48 +0000140
Chris Lattnerdf986172009-01-02 07:01:27 +0000141 // Type Parsing.
142 bool ParseType(PATypeHolder &Result);
143 bool ParseType(PATypeHolder &Result, LocTy &Loc) {
144 Loc = Lex.getLoc();
145 return ParseType(Result);
146 }
147 bool ParseTypeRec(PATypeHolder &H);
148 bool ParseStructType(PATypeHolder &H, bool Packed);
149 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
150 bool ParseFunctionType(PATypeHolder &Result);
151 PATypeHolder HandleUpRefs(const Type *Ty);
152
153 // Constants.
154 bool ParseValID(ValID &ID);
155 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
156 bool ParseGlobalValue(const Type *Ty, Constant *&V);
157 bool ParseGlobalTypeAndValue(Constant *&V);
158 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
159
Misha Brukman9ea40342009-01-02 22:46:48 +0000160
Chris Lattnerdf986172009-01-02 07:01:27 +0000161 // Function Semantic Analysis.
162 class PerFunctionState {
163 LLParser &P;
164 Function &F;
165 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
166 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
167 std::vector<Value*> NumberedVals;
168 public:
169 PerFunctionState(LLParser &p, Function &f);
170 ~PerFunctionState();
Misha Brukman9ea40342009-01-02 22:46:48 +0000171
Chris Lattnerdf986172009-01-02 07:01:27 +0000172 Function &getFunction() const { return F; }
Misha Brukman9ea40342009-01-02 22:46:48 +0000173
Chris Lattnerdf986172009-01-02 07:01:27 +0000174 bool VerifyFunctionComplete();
Misha Brukman9ea40342009-01-02 22:46:48 +0000175
Chris Lattnerdf986172009-01-02 07:01:27 +0000176 /// GetVal - Get a value with the specified name or ID, creating a
177 /// forward reference record if needed. This can return null if the value
178 /// exists but does not have the right type.
179 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
180 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000181
Chris Lattnerdf986172009-01-02 07:01:27 +0000182 /// SetInstName - After an instruction is parsed and inserted into its
183 /// basic block, this installs its name.
184 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
185 Instruction *Inst);
Misha Brukman9ea40342009-01-02 22:46:48 +0000186
Chris Lattnerdf986172009-01-02 07:01:27 +0000187 /// GetBB - Get a basic block with the specified name or ID, creating a
188 /// forward reference record if needed. This can return null if the value
189 /// is not a BasicBlock.
190 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
191 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000192
Chris Lattnerdf986172009-01-02 07:01:27 +0000193 /// DefineBB - Define the specified basic block, which is either named or
194 /// unnamed. If there is an error, this returns null otherwise it returns
195 /// the block being defined.
196 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
197 };
Misha Brukman9ea40342009-01-02 22:46:48 +0000198
Chris Lattnerdf986172009-01-02 07:01:27 +0000199 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
200 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000201
Chris Lattnerdf986172009-01-02 07:01:27 +0000202 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
203 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
204 PerFunctionState &PFS) {
205 Loc = Lex.getLoc();
206 return ParseValue(Ty, V, PFS);
207 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000208
Chris Lattnerdf986172009-01-02 07:01:27 +0000209 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
210 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
211 Loc = Lex.getLoc();
212 return ParseTypeAndValue(V, PFS);
213 }
Misha Brukman9ea40342009-01-02 22:46:48 +0000214
Chris Lattnerdf986172009-01-02 07:01:27 +0000215 struct ParamInfo {
216 LocTy Loc;
217 Value *V;
218 unsigned Attrs;
Misha Brukman9ea40342009-01-02 22:46:48 +0000219 ParamInfo(LocTy loc, Value *v, unsigned attrs)
Chris Lattnerdf986172009-01-02 07:01:27 +0000220 : Loc(loc), V(v), Attrs(attrs) {}
221 };
222 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
223 PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000224
Chris Lattnerdf986172009-01-02 07:01:27 +0000225 // Function Parsing.
226 struct ArgInfo {
227 LocTy Loc;
228 PATypeHolder Type;
229 unsigned Attrs;
230 std::string Name;
231 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
232 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
233 };
234 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
235 bool &isVarArg);
236 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
237 bool ParseFunctionBody(Function &Fn);
238 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000239
Chris Lattnerdf986172009-01-02 07:01:27 +0000240 // Instruction Parsing.
241 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
242 PerFunctionState &PFS);
243 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman9ea40342009-01-02 22:46:48 +0000244
Chris Lattnerdf986172009-01-02 07:01:27 +0000245 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
246 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
247 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
248 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman9ea40342009-01-02 22:46:48 +0000249
Chris Lattnerdf986172009-01-02 07:01:27 +0000250 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
251 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
252 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
253 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
254 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
255 bool ParseVAArg(Instruction *&I, PerFunctionState &PFS);
256 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
257 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
258 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
259 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
260 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
261 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
262 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
263 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
264 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
265 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
266 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
267 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
268 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
269 };
270} // End llvm namespace
271
272#endif