1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the parser class for .ll files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ASMPARSER_LLPARSER_H
15 #define LLVM_ASMPARSER_LLPARSER_H
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
38 typedef LLLexer::LocTy LocTy
;
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
;
53 /// Loc - This is the location of the upref.
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
68 UpRefRecord(LocTy L
, unsigned NL
, OpaqueType
*URTy
)
69 : Loc(L
), NestingLevel(NL
), LastContainedTy((Type
*)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
;
79 LLParser(MemoryBuffer
*F
, SourceMgr
&SM
, SMDiagnostic
&Err
, Module
*m
) :
80 Context(m
->getContext()), Lex(F
, SM
, Err
, m
->getContext()), M(m
) {}
83 LLVMContext
& getContext() { return Context
; }
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
);
101 bool ParseToken(lltok::Kind T
, const char *ErrMsg
);
102 bool EatIfPresent(lltok::Kind T
) {
103 if (Lex
.getKind() != T
) return false;
107 bool ParseOptionalToken(lltok::Kind T
, bool &Present
) {
108 if (Lex
.getKind() != T
) {
116 bool ParseStringConstant(std::string
&Result
);
117 bool ParseUInt32(unsigned &Val
);
118 bool ParseUInt32(unsigned &Val
, LocTy
&Loc
) {
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(CallingConv::ID
&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();
139 bool ParseModuleAsm();
140 bool ParseUnnamedType();
141 bool ParseNamedType();
145 bool ParseGlobalType(bool &IsConstant
);
146 bool ParseUnnamedGlobal();
147 bool ParseNamedGlobal();
148 bool ParseGlobal(const std::string
&Name
, LocTy Loc
, unsigned Linkage
,
149 bool HasLinkage
, unsigned Visibility
);
150 bool ParseAlias(const std::string
&Name
, LocTy Loc
, unsigned Visibility
);
151 bool ParseStandaloneMetadata();
152 bool ParseNamedMetadata();
153 bool ParseMDString(MetadataBase
*&S
);
154 bool ParseMDNode(MetadataBase
*&N
);
157 bool ParseType(PATypeHolder
&Result
, bool AllowVoid
= false);
158 bool ParseType(PATypeHolder
&Result
, LocTy
&Loc
, bool AllowVoid
= false) {
160 return ParseType(Result
, AllowVoid
);
162 bool ParseTypeRec(PATypeHolder
&H
);
163 bool ParseStructType(PATypeHolder
&H
, bool Packed
);
164 bool ParseArrayVectorType(PATypeHolder
&H
, bool isVector
);
165 bool ParseFunctionType(PATypeHolder
&Result
);
166 PATypeHolder
HandleUpRefs(const Type
*Ty
);
169 bool ParseValID(ValID
&ID
);
170 bool ConvertGlobalValIDToValue(const Type
*Ty
, ValID
&ID
, Constant
*&V
);
171 bool ParseGlobalValue(const Type
*Ty
, Constant
*&V
);
172 bool ParseGlobalTypeAndValue(Constant
*&V
);
173 bool ParseGlobalValueVector(SmallVectorImpl
<Constant
*> &Elts
);
174 bool ParseMDNodeVector(SmallVectorImpl
<Value
*> &);
177 // Function Semantic Analysis.
178 class PerFunctionState
{
181 std::map
<std::string
, std::pair
<Value
*, LocTy
> > ForwardRefVals
;
182 std::map
<unsigned, std::pair
<Value
*, LocTy
> > ForwardRefValIDs
;
183 std::vector
<Value
*> NumberedVals
;
185 PerFunctionState(LLParser
&p
, Function
&f
);
188 Function
&getFunction() const { return F
; }
190 bool VerifyFunctionComplete();
192 /// GetVal - Get a value with the specified name or ID, creating a
193 /// forward reference record if needed. This can return null if the value
194 /// exists but does not have the right type.
195 Value
*GetVal(const std::string
&Name
, const Type
*Ty
, LocTy Loc
);
196 Value
*GetVal(unsigned ID
, const Type
*Ty
, LocTy Loc
);
198 /// SetInstName - After an instruction is parsed and inserted into its
199 /// basic block, this installs its name.
200 bool SetInstName(int NameID
, const std::string
&NameStr
, LocTy NameLoc
,
203 /// GetBB - Get a basic block with the specified name or ID, creating a
204 /// forward reference record if needed. This can return null if the value
205 /// is not a BasicBlock.
206 BasicBlock
*GetBB(const std::string
&Name
, LocTy Loc
);
207 BasicBlock
*GetBB(unsigned ID
, LocTy Loc
);
209 /// DefineBB - Define the specified basic block, which is either named or
210 /// unnamed. If there is an error, this returns null otherwise it returns
211 /// the block being defined.
212 BasicBlock
*DefineBB(const std::string
&Name
, LocTy Loc
);
215 bool ConvertValIDToValue(const Type
*Ty
, ValID
&ID
, Value
*&V
,
216 PerFunctionState
&PFS
);
218 bool ParseValue(const Type
*Ty
, Value
*&V
, PerFunctionState
&PFS
);
219 bool ParseValue(const Type
*Ty
, Value
*&V
, LocTy
&Loc
,
220 PerFunctionState
&PFS
) {
222 return ParseValue(Ty
, V
, PFS
);
225 bool ParseTypeAndValue(Value
*&V
, PerFunctionState
&PFS
);
226 bool ParseTypeAndValue(Value
*&V
, LocTy
&Loc
, PerFunctionState
&PFS
) {
228 return ParseTypeAndValue(V
, PFS
);
235 ParamInfo(LocTy loc
, Value
*v
, unsigned attrs
)
236 : Loc(loc
), V(v
), Attrs(attrs
) {}
238 bool ParseParameterList(SmallVectorImpl
<ParamInfo
> &ArgList
,
239 PerFunctionState
&PFS
);
247 ArgInfo(LocTy L
, PATypeHolder Ty
, unsigned Attr
, const std::string
&N
)
248 : Loc(L
), Type(Ty
), Attrs(Attr
), Name(N
) {}
250 bool ParseArgumentList(std::vector
<ArgInfo
> &ArgList
,
251 bool &isVarArg
, bool inType
);
252 bool ParseFunctionHeader(Function
*&Fn
, bool isDefine
);
253 bool ParseFunctionBody(Function
&Fn
);
254 bool ParseBasicBlock(PerFunctionState
&PFS
);
256 // Instruction Parsing.
257 bool ParseInstruction(Instruction
*&Inst
, BasicBlock
*BB
,
258 PerFunctionState
&PFS
);
259 bool ParseCmpPredicate(unsigned &Pred
, unsigned Opc
);
261 bool ParseRet(Instruction
*&Inst
, BasicBlock
*BB
, PerFunctionState
&PFS
);
262 bool ParseBr(Instruction
*&Inst
, PerFunctionState
&PFS
);
263 bool ParseSwitch(Instruction
*&Inst
, PerFunctionState
&PFS
);
264 bool ParseInvoke(Instruction
*&Inst
, PerFunctionState
&PFS
);
266 bool ParseArithmetic(Instruction
*&I
, PerFunctionState
&PFS
, unsigned Opc
,
267 unsigned OperandType
);
268 bool ParseLogical(Instruction
*&I
, PerFunctionState
&PFS
, unsigned Opc
);
269 bool ParseCompare(Instruction
*&I
, PerFunctionState
&PFS
, unsigned Opc
);
270 bool ParseCast(Instruction
*&I
, PerFunctionState
&PFS
, unsigned Opc
);
271 bool ParseSelect(Instruction
*&I
, PerFunctionState
&PFS
);
272 bool ParseVA_Arg(Instruction
*&I
, PerFunctionState
&PFS
);
273 bool ParseExtractElement(Instruction
*&I
, PerFunctionState
&PFS
);
274 bool ParseInsertElement(Instruction
*&I
, PerFunctionState
&PFS
);
275 bool ParseShuffleVector(Instruction
*&I
, PerFunctionState
&PFS
);
276 bool ParsePHI(Instruction
*&I
, PerFunctionState
&PFS
);
277 bool ParseCall(Instruction
*&I
, PerFunctionState
&PFS
, bool isTail
);
278 bool ParseAlloc(Instruction
*&I
, PerFunctionState
&PFS
, unsigned Opc
);
279 bool ParseFree(Instruction
*&I
, PerFunctionState
&PFS
);
280 bool ParseLoad(Instruction
*&I
, PerFunctionState
&PFS
, bool isVolatile
);
281 bool ParseStore(Instruction
*&I
, PerFunctionState
&PFS
, bool isVolatile
);
282 bool ParseGetResult(Instruction
*&I
, PerFunctionState
&PFS
);
283 bool ParseGetElementPtr(Instruction
*&I
, PerFunctionState
&PFS
);
284 bool ParseExtractValue(Instruction
*&I
, PerFunctionState
&PFS
);
285 bool ParseInsertValue(Instruction
*&I
, PerFunctionState
&PFS
);
287 } // End llvm namespace