Fix part 1 of pr4682. PICADD is a 16-bit instruction even in thumb2 mode.
[llvm/avr.git] / lib / AsmParser / LLParser.h
blob40848cfe77c5118c036cac1ef83d73b44e2b003d
1 //===-- 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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ASMPARSER_LLPARSER_H
15 #define LLVM_ASMPARSER_LLPARSER_H
17 #include "LLLexer.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include <map>
22 namespace 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;
31 class MetadataBase;
32 class MDString;
33 class MDNode;
34 struct ValID;
36 class LLParser {
37 public:
38 typedef LLLexer::LocTy LocTy;
39 private:
40 LLVMContext& Context;
41 LLLexer Lex;
42 Module *M;
44 // 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;
48 /// MetadataCache - This map keeps track of parsed metadata constants.
49 std::map<unsigned, MetadataBase *> MetadataCache;
50 std::map<unsigned, std::pair<MetadataBase *, LocTy> > ForwardRefMDNodes;
52 struct UpRefRecord {
53 /// Loc - This is the location of the upref.
54 LocTy Loc;
56 /// NestingLevel - The number of nesting levels that need to be popped
57 /// before this type is resolved.
58 unsigned NestingLevel;
60 /// 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;
64 /// UpRefTy - This is the actual opaque type that the upreference is
65 /// represented with.
66 OpaqueType *UpRefTy;
68 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
69 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
70 UpRefTy(URTy) {}
72 std::vector<UpRefRecord> UpRefs;
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:
79 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
80 Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m) {}
81 bool Run();
83 LLVMContext& getContext() { return Context; }
85 private:
87 bool Error(LocTy L, const std::string &Msg) const {
88 return Lex.Error(L, Msg);
90 bool TokError(const std::string &Msg) const {
91 return Error(Lex.getLoc(), Msg);
94 /// 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);
100 // Helper Routines.
101 bool ParseToken(lltok::Kind T, const char *ErrMsg);
102 bool EatIfPresent(lltok::Kind T) {
103 if (Lex.getKind() != T) return false;
104 Lex.Lex();
105 return true;
107 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
108 if (Lex.getKind() != T) {
109 Present = false;
110 } else {
111 Lex.Lex();
112 Present = true;
114 return false;
116 bool ParseStringConstant(std::string &Result);
117 bool ParseUInt32(unsigned &Val);
118 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
119 Loc = Lex.getLoc();
120 return ParseUInt32(Val);
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);
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);
134 // 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();
145 bool ParseGlobalType(bool &IsConstant);
146 bool ParseNamedGlobal();
147 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
148 bool HasLinkage, unsigned Visibility);
149 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
150 bool ParseStandaloneMetadata();
151 bool ParseNamedMetadata();
152 bool ParseMDString(MetadataBase *&S);
153 bool ParseMDNode(MetadataBase *&N);
155 // Type Parsing.
156 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
157 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
158 Loc = Lex.getLoc();
159 return ParseType(Result, AllowVoid);
161 bool ParseTypeRec(PATypeHolder &H);
162 bool ParseStructType(PATypeHolder &H, bool Packed);
163 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
164 bool ParseFunctionType(PATypeHolder &Result);
165 PATypeHolder HandleUpRefs(const Type *Ty);
167 // Constants.
168 bool ParseValID(ValID &ID);
169 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
170 bool ParseGlobalValue(const Type *Ty, Constant *&V);
171 bool ParseGlobalTypeAndValue(Constant *&V);
172 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
173 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
176 // Function Semantic Analysis.
177 class PerFunctionState {
178 LLParser &P;
179 Function &F;
180 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
181 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
182 std::vector<Value*> NumberedVals;
183 public:
184 PerFunctionState(LLParser &p, Function &f);
185 ~PerFunctionState();
187 Function &getFunction() const { return F; }
189 bool VerifyFunctionComplete();
191 /// GetVal - Get a value with the specified name or ID, creating a
192 /// forward reference record if needed. This can return null if the value
193 /// exists but does not have the right type.
194 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
195 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
197 /// SetInstName - After an instruction is parsed and inserted into its
198 /// basic block, this installs its name.
199 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
200 Instruction *Inst);
202 /// GetBB - Get a basic block with the specified name or ID, creating a
203 /// forward reference record if needed. This can return null if the value
204 /// is not a BasicBlock.
205 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
206 BasicBlock *GetBB(unsigned ID, LocTy Loc);
208 /// DefineBB - Define the specified basic block, which is either named or
209 /// unnamed. If there is an error, this returns null otherwise it returns
210 /// the block being defined.
211 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
214 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
215 PerFunctionState &PFS);
217 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
218 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
219 PerFunctionState &PFS) {
220 Loc = Lex.getLoc();
221 return ParseValue(Ty, V, PFS);
224 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
225 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
226 Loc = Lex.getLoc();
227 return ParseTypeAndValue(V, PFS);
230 struct ParamInfo {
231 LocTy Loc;
232 Value *V;
233 unsigned Attrs;
234 ParamInfo(LocTy loc, Value *v, unsigned attrs)
235 : Loc(loc), V(v), Attrs(attrs) {}
237 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
238 PerFunctionState &PFS);
240 // Function Parsing.
241 struct ArgInfo {
242 LocTy Loc;
243 PATypeHolder Type;
244 unsigned Attrs;
245 std::string Name;
246 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
247 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
249 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
250 bool &isVarArg, bool inType);
251 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
252 bool ParseFunctionBody(Function &Fn);
253 bool ParseBasicBlock(PerFunctionState &PFS);
255 // Instruction Parsing.
256 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
257 PerFunctionState &PFS);
258 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
260 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
261 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
262 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
263 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
265 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
266 unsigned OperandType);
267 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
268 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
269 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
270 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
271 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
272 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
273 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
274 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
275 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
276 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
277 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
278 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
279 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
280 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
281 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
282 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
283 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
284 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
286 } // End llvm namespace
288 #endif