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/Type.h"
36 typedef LLLexer::LocTy LocTy
;
42 // 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
;
48 /// Loc - This is the location of the upref.
51 /// NestingLevel - The number of nesting levels that need to be popped
52 /// before this type is resolved.
53 unsigned NestingLevel
;
55 /// 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
;
59 /// UpRefTy - This is the actual opaque type that the upreference is
63 UpRefRecord(LocTy L
, unsigned NL
, OpaqueType
*URTy
)
64 : Loc(L
), NestingLevel(NL
), LastContainedTy((Type
*)URTy
),
67 std::vector
<UpRefRecord
> UpRefs
;
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
;
74 LLParser(MemoryBuffer
*F
, ParseError
&Err
, Module
*m
) : Lex(F
, Err
), M(m
) {}
79 bool Error(LocTy L
, const std::string
&Msg
) const {
80 return Lex
.Error(L
, Msg
);
82 bool TokError(const std::string
&Msg
) const {
83 return Error(Lex
.getLoc(), Msg
);
86 /// 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
);
93 bool ParseToken(lltok::Kind T
, const char *ErrMsg
);
94 bool EatIfPresent(lltok::Kind T
) {
95 if (Lex
.getKind() != T
) return false;
99 bool ParseOptionalToken(lltok::Kind T
, bool &Present
) {
100 if (Lex
.getKind() != T
) {
108 bool ParseStringConstant(std::string
&Result
);
109 bool ParseUInt32(unsigned &Val
);
110 bool ParseUInt32(unsigned &Val
, LocTy
&Loc
) {
112 return ParseUInt32(Val
);
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
);
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
);
126 // Top-Level Entities
127 bool ParseTopLevelEntities();
128 bool ValidateEndOfModule();
129 bool ParseTargetDefinition();
131 bool ParseModuleAsm();
132 bool ParseUnnamedType();
133 bool ParseNamedType();
137 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
);
144 bool ParseType(PATypeHolder
&Result
, bool AllowVoid
= false);
145 bool ParseType(PATypeHolder
&Result
, LocTy
&Loc
, bool AllowVoid
= false) {
147 return ParseType(Result
, AllowVoid
);
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
);
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
);
161 bool ParseMDNodeVector(SmallVectorImpl
<Value
*> &);
164 // Function Semantic Analysis.
165 class PerFunctionState
{
168 std::map
<std::string
, std::pair
<Value
*, LocTy
> > ForwardRefVals
;
169 std::map
<unsigned, std::pair
<Value
*, LocTy
> > ForwardRefValIDs
;
170 std::vector
<Value
*> NumberedVals
;
172 PerFunctionState(LLParser
&p
, Function
&f
);
175 Function
&getFunction() const { return F
; }
177 bool VerifyFunctionComplete();
179 /// 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
);
185 /// 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
,
190 /// 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
);
196 /// 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
);
202 bool ConvertValIDToValue(const Type
*Ty
, ValID
&ID
, Value
*&V
,
203 PerFunctionState
&PFS
);
205 bool ParseValue(const Type
*Ty
, Value
*&V
, PerFunctionState
&PFS
);
206 bool ParseValue(const Type
*Ty
, Value
*&V
, LocTy
&Loc
,
207 PerFunctionState
&PFS
) {
209 return ParseValue(Ty
, V
, PFS
);
212 bool ParseTypeAndValue(Value
*&V
, PerFunctionState
&PFS
);
213 bool ParseTypeAndValue(Value
*&V
, LocTy
&Loc
, PerFunctionState
&PFS
) {
215 return ParseTypeAndValue(V
, PFS
);
222 ParamInfo(LocTy loc
, Value
*v
, unsigned attrs
)
223 : Loc(loc
), V(v
), Attrs(attrs
) {}
225 bool ParseParameterList(SmallVectorImpl
<ParamInfo
> &ArgList
,
226 PerFunctionState
&PFS
);
234 ArgInfo(LocTy L
, PATypeHolder Ty
, unsigned Attr
, const std::string
&N
)
235 : Loc(L
), Type(Ty
), Attrs(Attr
), Name(N
) {}
237 bool ParseArgumentList(std::vector
<ArgInfo
> &ArgList
,
238 bool &isVarArg
, bool inType
);
239 bool ParseFunctionHeader(Function
*&Fn
, bool isDefine
);
240 bool ParseFunctionBody(Function
&Fn
);
241 bool ParseBasicBlock(PerFunctionState
&PFS
);
243 // Instruction Parsing.
244 bool ParseInstruction(Instruction
*&Inst
, BasicBlock
*BB
,
245 PerFunctionState
&PFS
);
246 bool ParseCmpPredicate(unsigned &Pred
, unsigned Opc
);
248 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
);
253 bool ParseArithmetic(Instruction
*&I
, PerFunctionState
&PFS
, unsigned Opc
,
254 unsigned OperandType
);
255 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
);
259 bool ParseVA_Arg(Instruction
*&I
, PerFunctionState
&PFS
);
260 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
);
274 } // End llvm namespace