blob: f77abaca17f946a5626724da4588baf8aa9db876 [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===-- LLParser.cpp - Parser Class ---------------------------------------===//
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#include "LLParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth91065212014-03-05 10:34:14 +000016#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/CallingConv.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/InlineAsm.h"
21#include "llvm/IR/Instructions.h"
Manman Ren209b17c2013-09-28 00:22:27 +000022#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000027#include "llvm/Support/raw_ostream.h"
28using namespace llvm;
29
Chris Lattner229907c2011-07-18 04:54:35 +000030static std::string getTypeString(Type *T) {
Chris Lattner0f214eb2011-06-18 21:18:23 +000031 std::string Result;
32 raw_string_ostream Tmp(Result);
33 Tmp << *T;
34 return Tmp.str();
35}
36
Chris Lattner3822f632009-01-02 08:05:26 +000037/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000038bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000039 // Prime the lexer.
40 Lex.Lex();
41
Chris Lattnerad6f3352009-01-04 20:44:11 +000042 return ParseTopLevelEntities() ||
43 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000044}
45
46/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
47/// module.
48bool LLParser::ValidateEndOfModule() {
Chris Lattner8eff0152010-04-01 05:14:45 +000049 // Handle any instruction metadata forward references.
50 if (!ForwardRefInstMetadata.empty()) {
51 for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
52 I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
53 I != E; ++I) {
54 Instruction *Inst = I->first;
55 const std::vector<MDRef> &MDList = I->second;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000056
Chris Lattner8eff0152010-04-01 05:14:45 +000057 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
58 unsigned SlotNo = MDList[i].MDSlot;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Craig Topper2617dcc2014-04-15 06:32:26 +000060 if (SlotNo >= NumberedMetadata.size() ||
61 NumberedMetadata[SlotNo] == nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +000062 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +000063 Twine(SlotNo) + "'");
Chris Lattner8eff0152010-04-01 05:14:45 +000064 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
65 }
66 }
67 ForwardRefInstMetadata.clear();
68 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000069
Manman Ren209b17c2013-09-28 00:22:27 +000070 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
71 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
72
Bill Wendlingb32b0412013-02-08 06:32:06 +000073 // Handle any function attribute group forward references.
74 for (std::map<Value*, std::vector<unsigned> >::iterator
75 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
76 I != E; ++I) {
77 Value *V = I->first;
78 std::vector<unsigned> &Vec = I->second;
79 AttrBuilder B;
80
81 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
82 VI != VE; ++VI)
83 B.merge(NumberedAttrBuilders[*VI]);
84
85 if (Function *Fn = dyn_cast<Function>(V)) {
86 AttributeSet AS = Fn->getAttributes();
87 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
88 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
89 AS.getFnAttributes());
90
91 FnAttrs.merge(B);
92
93 // If the alignment was parsed as an attribute, move to the alignment
94 // field.
95 if (FnAttrs.hasAlignmentAttr()) {
96 Fn->setAlignment(FnAttrs.getAlignment());
97 FnAttrs.removeAttribute(Attribute::Alignment);
98 }
99
100 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
101 AttributeSet::get(Context,
102 AttributeSet::FunctionIndex,
103 FnAttrs));
104 Fn->setAttributes(AS);
105 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
106 AttributeSet AS = CI->getAttributes();
107 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
108 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
109 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000110 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000111 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
112 AttributeSet::get(Context,
113 AttributeSet::FunctionIndex,
114 FnAttrs));
115 CI->setAttributes(AS);
116 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
117 AttributeSet AS = II->getAttributes();
118 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
119 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
120 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000121 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000122 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
123 AttributeSet::get(Context,
124 AttributeSet::FunctionIndex,
125 FnAttrs));
126 II->setAttributes(AS);
127 } else {
128 llvm_unreachable("invalid object with forward attribute group reference");
129 }
130 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000131
Chris Lattner3432c622009-10-28 03:39:23 +0000132 // If there are entries in ForwardRefBlockAddresses at this point, they are
133 // references after the function was defined. Resolve those now.
134 while (!ForwardRefBlockAddresses.empty()) {
135 // Okay, we are referencing an already-parsed function, resolve them now.
Craig Topper2617dcc2014-04-15 06:32:26 +0000136 Function *TheFn = nullptr;
Chris Lattner3432c622009-10-28 03:39:23 +0000137 const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
138 if (Fn.Kind == ValID::t_GlobalName)
139 TheFn = M->getFunction(Fn.StrVal);
140 else if (Fn.UIntVal < NumberedVals.size())
141 TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000142
Craig Topper2617dcc2014-04-15 06:32:26 +0000143 if (!TheFn)
Chris Lattner3432c622009-10-28 03:39:23 +0000144 return Error(Fn.Loc, "unknown function referenced by blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000145
Chris Lattner3432c622009-10-28 03:39:23 +0000146 // Resolve all these references.
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000147 if (ResolveForwardRefBlockAddresses(TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000148 ForwardRefBlockAddresses.begin()->second,
Craig Topper2617dcc2014-04-15 06:32:26 +0000149 nullptr))
Chris Lattner3432c622009-10-28 03:39:23 +0000150 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000151
Chris Lattner3432c622009-10-28 03:39:23 +0000152 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
153 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000154
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000155 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)
156 if (NumberedTypes[i].second.isValid())
157 return Error(NumberedTypes[i].second,
158 "use of undefined type '%" + Twine(i) + "'");
159
160 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
161 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
162 if (I->second.second.isValid())
163 return Error(I->second.second,
164 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000165
Chris Lattnerac161bf2009-01-02 07:01:27 +0000166 if (!ForwardRefVals.empty())
167 return Error(ForwardRefVals.begin()->second.second,
168 "use of undefined value '@" + ForwardRefVals.begin()->first +
169 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000170
Chris Lattnerac161bf2009-01-02 07:01:27 +0000171 if (!ForwardRefValIDs.empty())
172 return Error(ForwardRefValIDs.begin()->second.second,
173 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000174 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000175
Devang Pateld2541152009-07-08 19:23:54 +0000176 if (!ForwardRefMDNodes.empty())
177 return Error(ForwardRefMDNodes.begin()->second.second,
178 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000179 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000180
Devang Pateld2541152009-07-08 19:23:54 +0000181
Chris Lattnerac161bf2009-01-02 07:01:27 +0000182 // Look for intrinsic functions and CallInst that need to be upgraded
183 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
184 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000185
Manman Ren8b4306c2013-12-02 21:29:56 +0000186 UpgradeDebugInfo(*M);
187
Chris Lattnerac161bf2009-01-02 07:01:27 +0000188 return false;
189}
190
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000191bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000192 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
193 PerFunctionState *PFS) {
194 // Loop over all the references, resolving them.
195 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
196 BasicBlock *Res;
Chris Lattneraa99c942009-11-01 01:27:45 +0000197 if (PFS) {
Chris Lattner3432c622009-10-28 03:39:23 +0000198 if (Refs[i].first.Kind == ValID::t_LocalName)
199 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattneraa99c942009-11-01 01:27:45 +0000200 else
Chris Lattner3432c622009-10-28 03:39:23 +0000201 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
202 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
203 return Error(Refs[i].first.Loc,
Chris Lattnera38a4df2009-11-02 18:28:45 +0000204 "cannot take address of numeric label after the function is defined");
Chris Lattner3432c622009-10-28 03:39:23 +0000205 } else {
206 Res = dyn_cast_or_null<BasicBlock>(
207 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
208 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000209
Craig Topper2617dcc2014-04-15 06:32:26 +0000210 if (!Res)
Chris Lattner3432c622009-10-28 03:39:23 +0000211 return Error(Refs[i].first.Loc,
212 "referenced value is not a basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000213
Chris Lattner3432c622009-10-28 03:39:23 +0000214 // Get the BlockAddress for this and update references to use it.
215 BlockAddress *BA = BlockAddress::get(TheFn, Res);
216 Refs[i].second->replaceAllUsesWith(BA);
217 Refs[i].second->eraseFromParent();
218 }
219 return false;
220}
221
222
Chris Lattnerac161bf2009-01-02 07:01:27 +0000223//===----------------------------------------------------------------------===//
224// Top-Level Entities
225//===----------------------------------------------------------------------===//
226
227bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000228 while (1) {
229 switch (Lex.getKind()) {
230 default: return TokError("expected top-level entity");
231 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000232 case lltok::kw_declare: if (ParseDeclare()) return true; break;
233 case lltok::kw_define: if (ParseDefine()) return true; break;
234 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
235 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000236 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000237 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000238 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000239 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000240 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000241 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000242 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000243
244 // The Global variable production with no name can have many different
245 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000246 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
247 // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000248 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000249 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000250 case lltok::kw_internal: // OptionalLinkage
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +0000251 case lltok::kw_linker_private: // Obsolete OptionalLinkage
252 case lltok::kw_linker_private_weak: // Obsolete OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000253 case lltok::kw_weak: // OptionalLinkage
254 case lltok::kw_weak_odr: // OptionalLinkage
255 case lltok::kw_linkonce: // OptionalLinkage
256 case lltok::kw_linkonce_odr: // OptionalLinkage
257 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000258 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000259 case lltok::kw_extern_weak: // OptionalLinkage
Rafael Espindola52b74422014-06-03 20:00:20 +0000260 case lltok::kw_external: // OptionalLinkage
261 case lltok::kw_default: // OptionalVisibility
262 case lltok::kw_hidden: // OptionalVisibility
263 case lltok::kw_protected: // OptionalVisibility
Rafael Espindola63e92fb2014-06-03 20:07:32 +0000264 case lltok::kw_dllimport: // OptionalDLLStorageClass
265 case lltok::kw_dllexport: // OptionalDLLStorageClass
Rafael Espindola52b74422014-06-03 20:00:20 +0000266 case lltok::kw_thread_local: // OptionalThreadLocal
267 case lltok::kw_addrspace: // OptionalAddrSpace
268 case lltok::kw_constant: // GlobalType
269 case lltok::kw_global: { // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000270 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000271 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola52b74422014-06-03 20:00:20 +0000272 bool HasLinkage;
273 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000274 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000275 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000276 ParseOptionalThreadLocal(TLM) ||
Rafael Espindola52b74422014-06-03 20:00:20 +0000277 ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility,
278 DLLStorageClass, TLM))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000279 return true;
280 break;
281 }
Bill Wendlinga7c38772013-02-09 15:48:49 +0000282
283 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000284 }
285 }
286}
287
288
289/// toplevelentity
290/// ::= 'module' 'asm' STRINGCONSTANT
291bool LLParser::ParseModuleAsm() {
292 assert(Lex.getKind() == lltok::kw_module);
293 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000294
295 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000296 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
297 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000298
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000299 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000300 return false;
301}
302
303/// toplevelentity
304/// ::= 'target' 'triple' '=' STRINGCONSTANT
305/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
306bool LLParser::ParseTargetDefinition() {
307 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000308 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000309 switch (Lex.Lex()) {
310 default: return TokError("unknown target property");
311 case lltok::kw_triple:
312 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000313 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
314 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000315 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000316 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317 return false;
318 case lltok::kw_datalayout:
319 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000320 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
321 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000322 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000323 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000324 return false;
325 }
326}
327
Bill Wendling706d3d62012-11-28 08:41:48 +0000328/// toplevelentity
329/// ::= 'deplibs' '=' '[' ']'
330/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
331/// FIXME: Remove in 4.0. Currently parse, but ignore.
332bool LLParser::ParseDepLibs() {
333 assert(Lex.getKind() == lltok::kw_deplibs);
334 Lex.Lex();
335 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
336 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
337 return true;
338
339 if (EatIfPresent(lltok::rsquare))
340 return false;
341
342 do {
343 std::string Str;
344 if (ParseStringConstant(Str)) return true;
345 } while (EatIfPresent(lltok::comma));
346
347 return ParseToken(lltok::rsquare, "expected ']' at end of list");
348}
349
Dan Gohman466876b2009-08-12 23:32:33 +0000350/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000351/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000352bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000353 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000354 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000355 Lex.Lex(); // eat LocalVarID;
356
357 if (ParseToken(lltok::equal, "expected '=' after name") ||
358 ParseToken(lltok::kw_type, "expected 'type' after '='"))
359 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000360
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000361 if (TypeID >= NumberedTypes.size())
362 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000363
Craig Topper2617dcc2014-04-15 06:32:26 +0000364 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000365 if (ParseStructDefinition(TypeLoc, "",
366 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000367
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000368 if (!isa<StructType>(Result)) {
369 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
370 if (Entry.first)
371 return Error(TypeLoc, "non-struct types may not be recursive");
372 Entry.first = Result;
373 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000374 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000375
Chris Lattnerac161bf2009-01-02 07:01:27 +0000376 return false;
377}
378
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000379
Chris Lattnerac161bf2009-01-02 07:01:27 +0000380/// toplevelentity
381/// ::= LocalVar '=' 'type' type
382bool LLParser::ParseNamedType() {
383 std::string Name = Lex.getStrVal();
384 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000385 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000386
Chris Lattner3822f632009-01-02 08:05:26 +0000387 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000388 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000389 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000390
Craig Topper2617dcc2014-04-15 06:32:26 +0000391 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000392 if (ParseStructDefinition(NameLoc, Name,
393 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000394
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000395 if (!isa<StructType>(Result)) {
396 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
397 if (Entry.first)
398 return Error(NameLoc, "non-struct types may not be recursive");
399 Entry.first = Result;
400 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000401 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000402
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000403 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000404}
405
406
407/// toplevelentity
408/// ::= 'declare' FunctionHeader
409bool LLParser::ParseDeclare() {
410 assert(Lex.getKind() == lltok::kw_declare);
411 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000412
Chris Lattnerac161bf2009-01-02 07:01:27 +0000413 Function *F;
414 return ParseFunctionHeader(F, false);
415}
416
417/// toplevelentity
418/// ::= 'define' FunctionHeader '{' ...
419bool LLParser::ParseDefine() {
420 assert(Lex.getKind() == lltok::kw_define);
421 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000422
Chris Lattnerac161bf2009-01-02 07:01:27 +0000423 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000424 return ParseFunctionHeader(F, true) ||
425 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000426}
427
Chris Lattner3822f632009-01-02 08:05:26 +0000428/// ParseGlobalType
429/// ::= 'constant'
430/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000431bool LLParser::ParseGlobalType(bool &IsConstant) {
432 if (Lex.getKind() == lltok::kw_constant)
433 IsConstant = true;
434 else if (Lex.getKind() == lltok::kw_global)
435 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000436 else {
437 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000438 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000439 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000440 Lex.Lex();
441 return false;
442}
443
Dan Gohman466876b2009-08-12 23:32:33 +0000444/// ParseUnnamedGlobal:
445/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000446/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
447/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000448/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000449/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
450/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000451bool LLParser::ParseUnnamedGlobal() {
452 unsigned VarID = NumberedVals.size();
453 std::string Name;
454 LocTy NameLoc = Lex.getLoc();
455
456 // Handle the GlobalID form.
457 if (Lex.getKind() == lltok::GlobalID) {
458 if (Lex.getUIntVal() != VarID)
459 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000460 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000461 Lex.Lex(); // eat GlobalID;
462
463 if (ParseToken(lltok::equal, "expected '=' after name"))
464 return true;
465 }
466
467 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000468 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000469 GlobalVariable::ThreadLocalMode TLM;
Dan Gohman466876b2009-08-12 23:32:33 +0000470 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000471 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000472 ParseOptionalDLLStorageClass(DLLStorageClass) ||
473 ParseOptionalThreadLocal(TLM))
Dan Gohman466876b2009-08-12 23:32:33 +0000474 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000475
Dan Gohman466876b2009-08-12 23:32:33 +0000476 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000477 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000478 DLLStorageClass, TLM);
479 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
Dan Gohman466876b2009-08-12 23:32:33 +0000480}
481
Chris Lattnerac161bf2009-01-02 07:01:27 +0000482/// ParseNamedGlobal:
483/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000484/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
485/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000486bool LLParser::ParseNamedGlobal() {
487 assert(Lex.getKind() == lltok::GlobalVar);
488 LocTy NameLoc = Lex.getLoc();
489 std::string Name = Lex.getStrVal();
490 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000491
Chris Lattnerac161bf2009-01-02 07:01:27 +0000492 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000493 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000494 GlobalVariable::ThreadLocalMode TLM;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000495 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
496 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000497 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000498 ParseOptionalDLLStorageClass(DLLStorageClass) ||
499 ParseOptionalThreadLocal(TLM))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000500 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000501
Chris Lattnerac161bf2009-01-02 07:01:27 +0000502 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000503 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000504 DLLStorageClass, TLM);
505 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000506}
507
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000508// MDString:
509// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000510bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000511 std::string Str;
512 if (ParseStringConstant(Str)) return true;
Chris Lattner1797fc72009-12-29 21:53:55 +0000513 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000514 return false;
515}
516
517// MDNode:
518// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000519//
520/// This version of ParseMDNodeID returns the slot number and null in the case
521/// of a forward reference.
522bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
523 // !{ ..., !42, ... }
524 if (ParseUInt32(SlotNo)) return true;
525
526 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000527 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000528 Result = NumberedMetadata[SlotNo];
529 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000530 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000531 return false;
532}
533
Chris Lattner6dac02a2009-12-30 04:15:23 +0000534bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000535 // !{ ..., !42, ... }
536 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000537 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000538
Chris Lattner8eff0152010-04-01 05:14:45 +0000539 // If not a forward reference, just return it now.
540 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000541
Chris Lattner8eff0152010-04-01 05:14:45 +0000542 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000543 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000544 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000545
Chris Lattnerfc58af22009-12-30 04:51:58 +0000546 if (NumberedMetadata.size() <= MID)
547 NumberedMetadata.resize(MID+1);
548 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000549 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000550 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000551}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000552
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000553/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000554/// !foo = !{ !1, !2 }
555bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000556 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000557 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000558 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000559
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000560 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000561 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000562 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000563 return true;
564
Dan Gohman2637cc12010-07-21 23:38:33 +0000565 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000566 if (Lex.getKind() != lltok::rbrace)
567 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000568 if (ParseToken(lltok::exclaim, "Expected '!' here"))
569 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000570
Craig Topper2617dcc2014-04-15 06:32:26 +0000571 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000572 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000573 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000574 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000575
576 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
577 return true;
578
Devang Patelbe626972009-07-29 00:34:02 +0000579 return false;
580}
581
Devang Patel39e64d42009-07-01 19:21:12 +0000582/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000583/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000584bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000585 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000586 Lex.Lex();
587 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000588
589 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000590 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000591 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000592 if (ParseUInt32(MetadataID) ||
593 ParseToken(lltok::equal, "expected '=' here") ||
594 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000595 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000596 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000597 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000598 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000599 return true;
600
Jay Foad5514afe2011-04-21 19:59:31 +0000601 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000602
Chris Lattnerfc58af22009-12-30 04:51:58 +0000603 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000604 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000605 FI = ForwardRefMDNodes.find(MetadataID);
606 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000607 MDNode *Temp = FI->second.first;
608 Temp->replaceAllUsesWith(Init);
609 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000610 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000611
Chris Lattnerfc58af22009-12-30 04:51:58 +0000612 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
613 } else {
614 if (MetadataID >= NumberedMetadata.size())
615 NumberedMetadata.resize(MetadataID+1);
616
Craig Topper2617dcc2014-04-15 06:32:26 +0000617 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000618 return TokError("Metadata id is already used");
619 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000620 }
621
Devang Patel39e64d42009-07-01 19:21:12 +0000622 return false;
623}
624
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000625static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
626 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
627 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
628}
629
Chris Lattnerac161bf2009-01-02 07:01:27 +0000630/// ParseAlias:
Rafael Espindola5d92ffb2014-06-03 20:25:26 +0000631/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass
632/// OptionalThreadLocal 'alias' OptionalLinkage Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000633///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000634/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000635/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000636///
Rafael Espindola5d92ffb2014-06-03 20:25:26 +0000637/// Everything through OptionalThreadLocal has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000638///
639bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000640 unsigned Visibility, unsigned DLLStorageClass,
641 GlobalVariable::ThreadLocalMode TLM) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000642 assert(Lex.getKind() == lltok::kw_alias);
643 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000644 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000645 unsigned L;
646 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000647 return true;
648
Rafael Espindola78527052013-10-06 15:10:43 +0000649 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
650
Rafael Espindolacaa43562013-10-09 16:07:32 +0000651 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000652 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000653
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000654 if (!isValidVisibilityForLinkage(Visibility, L))
655 return Error(LinkageLoc,
656 "symbol with local linkage must have default visibility");
657
Rafael Espindola64c1e182014-06-03 02:41:57 +0000658 Constant *Aliasee;
659 LocTy AliaseeLoc = Lex.getLoc();
660 if (Lex.getKind() != lltok::kw_bitcast &&
661 Lex.getKind() != lltok::kw_getelementptr &&
662 Lex.getKind() != lltok::kw_addrspacecast &&
663 Lex.getKind() != lltok::kw_inttoptr) {
664 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000665 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000666 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000667 // The bitcast dest type is not present, it is implied by the dest type.
668 ValID ID;
669 if (ParseValID(ID))
670 return true;
671 if (ID.Kind != ValID::t_Constant)
672 return Error(AliaseeLoc, "invalid aliasee");
673 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000674 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000675
Rafael Espindola64c1e182014-06-03 02:41:57 +0000676 Type *AliaseeType = Aliasee->getType();
677 auto *PTy = dyn_cast<PointerType>(AliaseeType);
678 if (!PTy)
679 return Error(AliaseeLoc, "An alias must have pointer type");
680 Type *Ty = PTy->getElementType();
681 unsigned AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000682
683 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000684 std::unique_ptr<GlobalAlias> GA(
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000685 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
686 Name, Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000687 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000688 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000689 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000690
Chris Lattnerac161bf2009-01-02 07:01:27 +0000691 // See if this value already exists in the symbol table. If so, it is either
692 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000693 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000694 // See if this was a redefinition. If so, there is no entry in
695 // ForwardRefVals.
696 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
697 I = ForwardRefVals.find(Name);
698 if (I == ForwardRefVals.end())
699 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
700
701 // Otherwise, this was a definition of forward ref. Verify that types
702 // agree.
703 if (Val->getType() != GA->getType())
704 return Error(NameLoc,
705 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000706
Chris Lattnerac161bf2009-01-02 07:01:27 +0000707 // If they agree, just RAUW the old value with the alias and remove the
708 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000709 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000710 Val->eraseFromParent();
711 ForwardRefVals.erase(I);
712 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000713
Chris Lattnerac161bf2009-01-02 07:01:27 +0000714 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000715 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000716 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000717
Rafael Espindolaaa273822014-05-09 21:49:17 +0000718 // The module owns this now
719 GA.release();
720
Chris Lattnerac161bf2009-01-02 07:01:27 +0000721 return false;
722}
723
724/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000725/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
726/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000727/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000728/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
729/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000730/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000731///
Rafael Espindola5d92ffb2014-06-03 20:25:26 +0000732/// Everything up to and including OptionalThreadLocal has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000733/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000734///
735bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
736 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000737 unsigned Visibility, unsigned DLLStorageClass,
738 GlobalVariable::ThreadLocalMode TLM) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000739 if (!isValidVisibilityForLinkage(Visibility, Linkage))
740 return Error(NameLoc,
741 "symbol with local linkage must have default visibility");
742
Chris Lattnerac161bf2009-01-02 07:01:27 +0000743 unsigned AddrSpace;
Shuxin Yang2e1890e2013-10-27 03:08:44 +0000744 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Rafael Espindola026d1522011-01-13 01:30:30 +0000745 LocTy UnnamedAddrLoc;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000746 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000747 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000748
Craig Topper2617dcc2014-04-15 06:32:26 +0000749 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000750 if (ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindola026d1522011-01-13 01:30:30 +0000751 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
752 &UnnamedAddrLoc) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000753 ParseOptionalToken(lltok::kw_externally_initialized,
754 IsExternallyInitialized,
755 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000756 ParseGlobalType(IsConstant) ||
757 ParseType(Ty, TyLoc))
758 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000759
Chris Lattnerac161bf2009-01-02 07:01:27 +0000760 // If the linkage is specified and is external, then no initializer is
761 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000762 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000763 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000764 Linkage != GlobalValue::ExternalLinkage)) {
765 if (ParseGlobalValue(Ty, Init))
766 return true;
767 }
768
Duncan Sands19d0b472010-02-16 11:11:14 +0000769 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000770 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000771
Craig Topper2617dcc2014-04-15 06:32:26 +0000772 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000773
774 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000775 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000776 if (GlobalValue *GVal = M->getNamedValue(Name)) {
777 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
778 return Error(NameLoc, "redefinition of global '@" + Name + "'");
779 GV = cast<GlobalVariable>(GVal);
780 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000781 } else {
782 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
783 I = ForwardRefValIDs.find(NumberedVals.size());
784 if (I != ForwardRefValIDs.end()) {
785 GV = cast<GlobalVariable>(I->second.first);
786 ForwardRefValIDs.erase(I);
787 }
788 }
789
Craig Topper2617dcc2014-04-15 06:32:26 +0000790 if (!GV) {
791 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
792 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000793 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000794 } else {
795 if (GV->getType()->getElementType() != Ty)
796 return Error(TyLoc,
797 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000798
Chris Lattnerac161bf2009-01-02 07:01:27 +0000799 // Move the forward-reference to the correct spot in the module.
800 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
801 }
802
803 if (Name.empty())
804 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000805
Chris Lattnerac161bf2009-01-02 07:01:27 +0000806 // Set the parsed properties on the global.
807 if (Init)
808 GV->setInitializer(Init);
809 GV->setConstant(IsConstant);
810 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
811 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000812 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000813 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000814 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000815 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000816
Chris Lattnerac161bf2009-01-02 07:01:27 +0000817 // Parse attributes on the global.
818 while (Lex.getKind() == lltok::comma) {
819 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000820
Chris Lattnerac161bf2009-01-02 07:01:27 +0000821 if (Lex.getKind() == lltok::kw_section) {
822 Lex.Lex();
823 GV->setSection(Lex.getStrVal());
824 if (ParseToken(lltok::StringConstant, "expected global section string"))
825 return true;
826 } else if (Lex.getKind() == lltok::kw_align) {
827 unsigned Alignment;
828 if (ParseOptionalAlignment(Alignment)) return true;
829 GV->setAlignment(Alignment);
830 } else {
831 TokError("unknown global variable property!");
832 }
833 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000834
Chris Lattnerac161bf2009-01-02 07:01:27 +0000835 return false;
836}
837
Bill Wendling63b88192013-02-06 06:52:58 +0000838/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000839/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000840bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000841 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000842 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000843 Lex.Lex();
844
845 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000846 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000847 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000848 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000849 Lex.Lex();
850
851 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000852 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000853 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000854 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000855 ParseToken(lltok::rbrace, "expected end of attribute group"))
856 return true;
857
Bill Wendlingb32b0412013-02-08 06:32:06 +0000858 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000859 return Error(AttrGrpLoc, "attribute group has no attributes");
860
861 return false;
862}
863
Bill Wendling8b0321d2013-02-08 00:52:31 +0000864/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000865/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000866bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
867 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000868 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000869 bool HaveError = false;
870
871 B.clear();
872
Bill Wendling63b88192013-02-06 06:52:58 +0000873 while (true) {
874 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000875 if (Token == lltok::kw_builtin)
876 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000877 switch (Token) {
878 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000879 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000880 return Error(Lex.getLoc(), "unterminated attribute group");
881 case lltok::rbrace:
882 // Finished.
883 return false;
884
Bill Wendlingb32b0412013-02-08 06:32:06 +0000885 case lltok::AttrGrpID: {
886 // Allow a function to reference an attribute group:
887 //
888 // define void @foo() #1 { ... }
889 if (inAttrGrp)
890 HaveError |=
891 Error(Lex.getLoc(),
892 "cannot have an attribute group reference in an attribute group");
893
894 unsigned AttrGrpNum = Lex.getUIntVal();
895 if (inAttrGrp) break;
896
897 // Save the reference to the attribute group. We'll fill it in later.
898 FwdRefAttrGrps.push_back(AttrGrpNum);
899 break;
900 }
Bill Wendling63b88192013-02-06 06:52:58 +0000901 // Target-dependent attributes:
902 case lltok::StringConstant: {
903 std::string Attr = Lex.getStrVal();
904 Lex.Lex();
905 std::string Val;
906 if (EatIfPresent(lltok::equal) &&
907 ParseStringConstant(Val))
908 return true;
909
910 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000911 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000912 }
913
914 // Target-independent attributes:
915 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000916 // As a hack, we allow function alignment to be initially parsed as an
917 // attribute on a function declaration/definition or added to an attribute
918 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000919 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000920 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000921 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000922 if (ParseToken(lltok::equal, "expected '=' here") ||
923 ParseUInt32(Alignment))
924 return true;
925 } else {
926 if (ParseOptionalAlignment(Alignment))
927 return true;
928 }
Bill Wendling63b88192013-02-06 06:52:58 +0000929 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000930 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000931 }
932 case lltok::kw_alignstack: {
933 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000934 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000935 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000936 if (ParseToken(lltok::equal, "expected '=' here") ||
937 ParseUInt32(Alignment))
938 return true;
939 } else {
940 if (ParseOptionalStackAlignment(Alignment))
941 return true;
942 }
Bill Wendling63b88192013-02-06 06:52:58 +0000943 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000944 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000945 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000946 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000947 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000948 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000949 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000950 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000951 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
952 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
953 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
954 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
955 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
956 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
957 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
958 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
959 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
960 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000961 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000962 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
963 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
964 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
965 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
966 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
967 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
968 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
969 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
970 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
971 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
972 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000973
974 // Error handling.
975 case lltok::kw_inreg:
976 case lltok::kw_signext:
977 case lltok::kw_zeroext:
978 HaveError |=
979 Error(Lex.getLoc(),
980 "invalid use of attribute on a function");
981 break;
982 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +0000983 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000984 case lltok::kw_nest:
985 case lltok::kw_noalias:
986 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000987 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +0000988 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000989 case lltok::kw_sret:
990 HaveError |=
991 Error(Lex.getLoc(),
992 "invalid use of parameter-only attribute on a function");
993 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000994 }
995
996 Lex.Lex();
997 }
998}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000999
1000//===----------------------------------------------------------------------===//
1001// GlobalValue Reference/Resolution Routines.
1002//===----------------------------------------------------------------------===//
1003
1004/// GetGlobalVal - Get a value with the specified name or ID, creating a
1005/// forward reference record if needed. This can return null if the value
1006/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001007GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001008 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001009 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001010 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001011 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001012 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001013 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001014
Chris Lattnerac161bf2009-01-02 07:01:27 +00001015 // Look this name up in the normal function symbol table.
1016 GlobalValue *Val =
1017 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001018
Chris Lattnerac161bf2009-01-02 07:01:27 +00001019 // If this is a forward reference for the value, see if we already created a
1020 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001021 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001022 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1023 I = ForwardRefVals.find(Name);
1024 if (I != ForwardRefVals.end())
1025 Val = I->second.first;
1026 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001027
Chris Lattnerac161bf2009-01-02 07:01:27 +00001028 // If we have the value in the symbol table or fwd-ref table, return it.
1029 if (Val) {
1030 if (Val->getType() == Ty) return Val;
1031 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001032 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001033 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001034 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001035
Chris Lattnerac161bf2009-01-02 07:01:27 +00001036 // Otherwise, create a new forward reference for this value and remember it.
1037 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001038 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001039 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001040 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001041 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001042 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1043 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001044 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001045
Chris Lattnerac161bf2009-01-02 07:01:27 +00001046 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1047 return FwdVal;
1048}
1049
Chris Lattner229907c2011-07-18 04:54:35 +00001050GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1051 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001052 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001053 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001054 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001055 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001056
Craig Topper2617dcc2014-04-15 06:32:26 +00001057 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001058
Chris Lattnerac161bf2009-01-02 07:01:27 +00001059 // If this is a forward reference for the value, see if we already created a
1060 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001061 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001062 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1063 I = ForwardRefValIDs.find(ID);
1064 if (I != ForwardRefValIDs.end())
1065 Val = I->second.first;
1066 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001067
Chris Lattnerac161bf2009-01-02 07:01:27 +00001068 // If we have the value in the symbol table or fwd-ref table, return it.
1069 if (Val) {
1070 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001071 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001072 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001073 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001074 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001075
Chris Lattnerac161bf2009-01-02 07:01:27 +00001076 // Otherwise, create a new forward reference for this value and remember it.
1077 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001078 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001079 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001080 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001081 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001082 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001083
Chris Lattnerac161bf2009-01-02 07:01:27 +00001084 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1085 return FwdVal;
1086}
1087
1088
1089//===----------------------------------------------------------------------===//
1090// Helper Routines.
1091//===----------------------------------------------------------------------===//
1092
1093/// ParseToken - If the current token has the specified kind, eat it and return
1094/// success. Otherwise, emit the specified error and return failure.
1095bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1096 if (Lex.getKind() != T)
1097 return TokError(ErrMsg);
1098 Lex.Lex();
1099 return false;
1100}
1101
Chris Lattner3822f632009-01-02 08:05:26 +00001102/// ParseStringConstant
1103/// ::= StringConstant
1104bool LLParser::ParseStringConstant(std::string &Result) {
1105 if (Lex.getKind() != lltok::StringConstant)
1106 return TokError("expected string constant");
1107 Result = Lex.getStrVal();
1108 Lex.Lex();
1109 return false;
1110}
1111
1112/// ParseUInt32
1113/// ::= uint32
1114bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001115 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1116 return TokError("expected integer");
1117 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1118 if (Val64 != unsigned(Val64))
1119 return TokError("expected 32-bit integer (too large)");
1120 Val = Val64;
1121 Lex.Lex();
1122 return false;
1123}
1124
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001125/// ParseTLSModel
1126/// := 'localdynamic'
1127/// := 'initialexec'
1128/// := 'localexec'
1129bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1130 switch (Lex.getKind()) {
1131 default:
1132 return TokError("expected localdynamic, initialexec or localexec");
1133 case lltok::kw_localdynamic:
1134 TLM = GlobalVariable::LocalDynamicTLSModel;
1135 break;
1136 case lltok::kw_initialexec:
1137 TLM = GlobalVariable::InitialExecTLSModel;
1138 break;
1139 case lltok::kw_localexec:
1140 TLM = GlobalVariable::LocalExecTLSModel;
1141 break;
1142 }
1143
1144 Lex.Lex();
1145 return false;
1146}
1147
1148/// ParseOptionalThreadLocal
1149/// := /*empty*/
1150/// := 'thread_local'
1151/// := 'thread_local' '(' tlsmodel ')'
1152bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1153 TLM = GlobalVariable::NotThreadLocal;
1154 if (!EatIfPresent(lltok::kw_thread_local))
1155 return false;
1156
1157 TLM = GlobalVariable::GeneralDynamicTLSModel;
1158 if (Lex.getKind() == lltok::lparen) {
1159 Lex.Lex();
1160 return ParseTLSModel(TLM) ||
1161 ParseToken(lltok::rparen, "expected ')' after thread local model");
1162 }
1163 return false;
1164}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001165
1166/// ParseOptionalAddrSpace
1167/// := /*empty*/
1168/// := 'addrspace' '(' uint32 ')'
1169bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1170 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001171 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001172 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001173 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001174 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001175 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001176}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001177
Bill Wendling34c2eb22012-12-04 23:40:58 +00001178/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1179bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1180 bool HaveError = false;
1181
1182 B.clear();
1183
1184 while (1) {
1185 lltok::Kind Token = Lex.getKind();
1186 switch (Token) {
1187 default: // End of attributes.
1188 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001189 case lltok::kw_align: {
1190 unsigned Alignment;
1191 if (ParseOptionalAlignment(Alignment))
1192 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001193 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001194 continue;
1195 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001196 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001197 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001198 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1199 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1200 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1201 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001202 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001203 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1204 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001205 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001206 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1207 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1208 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001209
Stephen Lin7577ed52013-04-20 13:16:13 +00001210 case lltok::kw_alignstack:
1211 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001212 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001213 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001214 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001215 case lltok::kw_minsize:
1216 case lltok::kw_naked:
1217 case lltok::kw_nobuiltin:
1218 case lltok::kw_noduplicate:
1219 case lltok::kw_noimplicitfloat:
1220 case lltok::kw_noinline:
1221 case lltok::kw_nonlazybind:
1222 case lltok::kw_noredzone:
1223 case lltok::kw_noreturn:
1224 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001225 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001226 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001227 case lltok::kw_returns_twice:
1228 case lltok::kw_sanitize_address:
1229 case lltok::kw_sanitize_memory:
1230 case lltok::kw_sanitize_thread:
1231 case lltok::kw_ssp:
1232 case lltok::kw_sspreq:
1233 case lltok::kw_sspstrong:
1234 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001235 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1236 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001237 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001238
Bill Wendling34c2eb22012-12-04 23:40:58 +00001239 Lex.Lex();
1240 }
1241}
1242
1243/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1244bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1245 bool HaveError = false;
1246
1247 B.clear();
1248
1249 while (1) {
1250 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001251 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001252 default: // End of attributes.
1253 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001254 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1255 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001256 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001257 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1258 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001259
Bill Wendling34c2eb22012-12-04 23:40:58 +00001260 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001261 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001262 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001263 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001264 case lltok::kw_nest:
1265 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001266 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001267 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001268 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001269 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001270
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001271 case lltok::kw_alignstack:
1272 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001273 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001274 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001275 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001276 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001277 case lltok::kw_minsize:
1278 case lltok::kw_naked:
1279 case lltok::kw_nobuiltin:
1280 case lltok::kw_noduplicate:
1281 case lltok::kw_noimplicitfloat:
1282 case lltok::kw_noinline:
1283 case lltok::kw_nonlazybind:
1284 case lltok::kw_noredzone:
1285 case lltok::kw_noreturn:
1286 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001287 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001288 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001289 case lltok::kw_returns_twice:
1290 case lltok::kw_sanitize_address:
1291 case lltok::kw_sanitize_memory:
1292 case lltok::kw_sanitize_thread:
1293 case lltok::kw_ssp:
1294 case lltok::kw_sspreq:
1295 case lltok::kw_sspstrong:
1296 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001297 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001298 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001299
1300 case lltok::kw_readnone:
1301 case lltok::kw_readonly:
1302 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001303 }
1304
Chris Lattnerac161bf2009-01-02 07:01:27 +00001305 Lex.Lex();
1306 }
1307}
1308
1309/// ParseOptionalLinkage
1310/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001311/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001312/// ::= 'internal'
1313/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001314/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001315/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001316/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001317/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001318/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001319/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001320/// ::= 'extern_weak'
1321/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001322///
1323/// Deprecated Values:
1324/// ::= 'linker_private'
1325/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001326bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1327 HasLinkage = false;
1328 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001329 default: Res=GlobalValue::ExternalLinkage; return false;
1330 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001331 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1332 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1333 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1334 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1335 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001336 case lltok::kw_available_externally:
1337 Res = GlobalValue::AvailableExternallyLinkage;
1338 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001339 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001340 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001341 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1342 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001343
1344 case lltok::kw_linker_private:
1345 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001346 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1347 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001348 Lex.Lex();
1349 // treat linker_private and linker_private_weak as PrivateLinkage
1350 Res = GlobalValue::PrivateLinkage;
1351 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001352 }
1353 Lex.Lex();
1354 HasLinkage = true;
1355 return false;
1356}
1357
1358/// ParseOptionalVisibility
1359/// ::= /*empty*/
1360/// ::= 'default'
1361/// ::= 'hidden'
1362/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001363///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001364bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1365 switch (Lex.getKind()) {
1366 default: Res = GlobalValue::DefaultVisibility; return false;
1367 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1368 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1369 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1370 }
1371 Lex.Lex();
1372 return false;
1373}
1374
Nico Rieck7157bb72014-01-14 15:22:47 +00001375/// ParseOptionalDLLStorageClass
1376/// ::= /*empty*/
1377/// ::= 'dllimport'
1378/// ::= 'dllexport'
1379///
1380bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1381 switch (Lex.getKind()) {
1382 default: Res = GlobalValue::DefaultStorageClass; return false;
1383 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1384 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1385 }
1386 Lex.Lex();
1387 return false;
1388}
1389
Chris Lattnerac161bf2009-01-02 07:01:27 +00001390/// ParseOptionalCallingConv
1391/// ::= /*empty*/
1392/// ::= 'ccc'
1393/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001394/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001395/// ::= 'coldcc'
1396/// ::= 'x86_stdcallcc'
1397/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001398/// ::= 'x86_thiscallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001399/// ::= 'arm_apcscc'
1400/// ::= 'arm_aapcscc'
1401/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001402/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001403/// ::= 'ptx_kernel'
1404/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001405/// ::= 'spir_func'
1406/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001407/// ::= 'x86_64_sysvcc'
1408/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001409/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001410/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001411/// ::= 'preserve_mostcc'
1412/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001413/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001414///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001415bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001416 switch (Lex.getKind()) {
1417 default: CC = CallingConv::C; return false;
1418 case lltok::kw_ccc: CC = CallingConv::C; break;
1419 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1420 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1421 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1422 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001423 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001424 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1425 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1426 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001427 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001428 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1429 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001430 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1431 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001432 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001433 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1434 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001435 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001436 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001437 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1438 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001439 case lltok::kw_cc: {
1440 unsigned ArbitraryCC;
1441 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001442 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001443 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001444 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1445 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001446 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001447 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001448
Chris Lattnerac161bf2009-01-02 07:01:27 +00001449 Lex.Lex();
1450 return false;
1451}
1452
Chris Lattner5c427632009-12-30 05:31:19 +00001453/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001454/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001455bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1456 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001457 do {
1458 if (Lex.getKind() != lltok::MetadataVar)
1459 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001460
Chris Lattner596760d2009-12-29 21:25:40 +00001461 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001462 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001463 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001464
Chris Lattner1797fc72009-12-29 21:53:55 +00001465 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001466 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001467
1468 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001469 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001470
Dan Gohmanf0715b12010-08-24 14:35:45 +00001471 // This code is similar to that of ParseMetadataValue, however it needs to
1472 // have special-case code for a forward reference; see the comments on
1473 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1474 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001475 if (Lex.getKind() == lltok::lbrace) {
1476 ValID ID;
1477 if (ParseMetadataListValue(ID, PFS))
1478 return true;
1479 assert(ID.Kind == ValID::t_MDNode);
1480 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001481 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001482 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001483 if (ParseMDNodeID(Node, NodeID))
1484 return true;
1485 if (Node) {
1486 // If we got the node, add it to the instruction.
1487 Inst->setMetadata(MDK, Node);
1488 } else {
1489 MDRef R = { Loc, MDK, NodeID };
1490 // Otherwise, remember that this should be resolved later.
1491 ForwardRefInstMetadata[Inst].push_back(R);
1492 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001493 }
Chris Lattner596760d2009-12-29 21:25:40 +00001494
Manman Ren209b17c2013-09-28 00:22:27 +00001495 if (MDK == LLVMContext::MD_tbaa)
1496 InstsWithTBAATag.push_back(Inst);
1497
Chris Lattner596760d2009-12-29 21:25:40 +00001498 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001499 } while (EatIfPresent(lltok::comma));
1500 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001501}
1502
Chris Lattnerac161bf2009-01-02 07:01:27 +00001503/// ParseOptionalAlignment
1504/// ::= /* empty */
1505/// ::= 'align' 4
1506bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1507 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001508 if (!EatIfPresent(lltok::kw_align))
1509 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001510 LocTy AlignLoc = Lex.getLoc();
1511 if (ParseUInt32(Alignment)) return true;
1512 if (!isPowerOf2_32(Alignment))
1513 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001514 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001515 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001516 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001517}
1518
Chris Lattnerb2f39502009-12-30 05:44:30 +00001519/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001520/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001521/// ::= ',' align 4
1522///
1523/// This returns with AteExtraComma set to true if it ate an excess comma at the
1524/// end.
1525bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1526 bool &AteExtraComma) {
1527 AteExtraComma = false;
1528 while (EatIfPresent(lltok::comma)) {
1529 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001530 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001531 AteExtraComma = true;
1532 return false;
1533 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001534
Chris Lattner95b0ff42010-04-23 00:50:50 +00001535 if (Lex.getKind() != lltok::kw_align)
1536 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001537
Chris Lattner95b0ff42010-04-23 00:50:50 +00001538 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001539 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001540
Devang Patelea8a4b92009-09-17 23:04:48 +00001541 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001542}
1543
Eli Friedmanfee02c62011-07-25 23:16:38 +00001544/// ParseScopeAndOrdering
1545/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1546/// else: ::=
1547///
1548/// This sets Scope and Ordering to the parsed values.
1549bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1550 AtomicOrdering &Ordering) {
1551 if (!isAtomic)
1552 return false;
1553
1554 Scope = CrossThread;
1555 if (EatIfPresent(lltok::kw_singlethread))
1556 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001557
1558 return ParseOrdering(Ordering);
1559}
1560
1561/// ParseOrdering
1562/// ::= AtomicOrdering
1563///
1564/// This sets Ordering to the parsed value.
1565bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001566 switch (Lex.getKind()) {
1567 default: return TokError("Expected ordering on atomic instruction");
1568 case lltok::kw_unordered: Ordering = Unordered; break;
1569 case lltok::kw_monotonic: Ordering = Monotonic; break;
1570 case lltok::kw_acquire: Ordering = Acquire; break;
1571 case lltok::kw_release: Ordering = Release; break;
1572 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1573 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1574 }
1575 Lex.Lex();
1576 return false;
1577}
1578
Charles Davisbe5557e2010-02-12 00:31:15 +00001579/// ParseOptionalStackAlignment
1580/// ::= /* empty */
1581/// ::= 'alignstack' '(' 4 ')'
1582bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1583 Alignment = 0;
1584 if (!EatIfPresent(lltok::kw_alignstack))
1585 return false;
1586 LocTy ParenLoc = Lex.getLoc();
1587 if (!EatIfPresent(lltok::lparen))
1588 return Error(ParenLoc, "expected '('");
1589 LocTy AlignLoc = Lex.getLoc();
1590 if (ParseUInt32(Alignment)) return true;
1591 ParenLoc = Lex.getLoc();
1592 if (!EatIfPresent(lltok::rparen))
1593 return Error(ParenLoc, "expected ')'");
1594 if (!isPowerOf2_32(Alignment))
1595 return Error(AlignLoc, "stack alignment is not a power of two");
1596 return false;
1597}
Devang Patelea8a4b92009-09-17 23:04:48 +00001598
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001599/// ParseIndexList - This parses the index list for an insert/extractvalue
1600/// instruction. This sets AteExtraComma in the case where we eat an extra
1601/// comma at the end of the line and find that it is followed by metadata.
1602/// Clients that don't allow metadata can call the version of this function that
1603/// only takes one argument.
1604///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001605/// ParseIndexList
1606/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001607///
1608bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1609 bool &AteExtraComma) {
1610 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001611
Chris Lattnerac161bf2009-01-02 07:01:27 +00001612 if (Lex.getKind() != lltok::comma)
1613 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001614
Chris Lattner3822f632009-01-02 08:05:26 +00001615 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001616 if (Lex.getKind() == lltok::MetadataVar) {
1617 AteExtraComma = true;
1618 return false;
1619 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001620 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001621 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001622 Indices.push_back(Idx);
1623 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001624
Chris Lattnerac161bf2009-01-02 07:01:27 +00001625 return false;
1626}
1627
1628//===----------------------------------------------------------------------===//
1629// Type Parsing.
1630//===----------------------------------------------------------------------===//
1631
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001632/// ParseType - Parse a type.
1633bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1634 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001635 switch (Lex.getKind()) {
1636 default:
1637 return TokError("expected type");
1638 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001639 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001640 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001641 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001642 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001643 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001644 // Type ::= StructType
1645 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001646 return true;
1647 break;
1648 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001649 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001650 Lex.Lex(); // eat the lsquare.
1651 if (ParseArrayVectorType(Result, false))
1652 return true;
1653 break;
1654 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001655 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001656 Lex.Lex();
1657 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001658 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001659 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001660 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001661 } else if (ParseArrayVectorType(Result, true))
1662 return true;
1663 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001664 case lltok::LocalVar: {
1665 // Type ::= %foo
1666 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001667
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001668 // If the type hasn't been defined yet, create a forward definition and
1669 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001670 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001671 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001672 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001673 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001674 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001675 Lex.Lex();
1676 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001677 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001678
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001679 case lltok::LocalVarID: {
1680 // Type ::= %4
1681 if (Lex.getUIntVal() >= NumberedTypes.size())
1682 NumberedTypes.resize(Lex.getUIntVal()+1);
1683 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001684
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001685 // If the type hasn't been defined yet, create a forward definition and
1686 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001687 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001688 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001689 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001690 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001691 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001692 Lex.Lex();
1693 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001694 }
1695 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001696
1697 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001698 while (1) {
1699 switch (Lex.getKind()) {
1700 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001701 default:
1702 if (!AllowVoid && Result->isVoidTy())
1703 return Error(TypeLoc, "void type only allowed for function results");
1704 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001705
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001706 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001707 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001708 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001709 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001710 if (Result->isVoidTy())
1711 return TokError("pointers to void are invalid - use i8* instead");
1712 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001713 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001714 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001715 Lex.Lex();
1716 break;
1717
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001718 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001719 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001720 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001721 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001722 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001723 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001724 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001725 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001726 unsigned AddrSpace;
1727 if (ParseOptionalAddrSpace(AddrSpace) ||
1728 ParseToken(lltok::star, "expected '*' in address space"))
1729 return true;
1730
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001731 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001732 break;
1733 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001734
Chris Lattnerac161bf2009-01-02 07:01:27 +00001735 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1736 case lltok::lparen:
1737 if (ParseFunctionType(Result))
1738 return true;
1739 break;
1740 }
1741 }
1742}
1743
1744/// ParseParameterList
1745/// ::= '(' ')'
1746/// ::= '(' Arg (',' Arg)* ')'
1747/// Arg
1748/// ::= Type OptionalAttributes Value OptionalAttributes
1749bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1750 PerFunctionState &PFS) {
1751 if (ParseToken(lltok::lparen, "expected '(' in call"))
1752 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001753
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001754 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001755 while (Lex.getKind() != lltok::rparen) {
1756 // If this isn't the first argument, we need a comma.
1757 if (!ArgList.empty() &&
1758 ParseToken(lltok::comma, "expected ',' in argument list"))
1759 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001760
Chris Lattnerac161bf2009-01-02 07:01:27 +00001761 // Parse the argument.
1762 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001763 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001764 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001765 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001766 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001767 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001768
Chris Lattner5b4a9622009-12-30 02:11:14 +00001769 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001770 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001771 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001772 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1773 AttrIndex++,
1774 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001775 }
1776
1777 Lex.Lex(); // Lex the ')'.
1778 return false;
1779}
1780
1781
1782
Chris Lattner2ed06b42009-01-05 18:34:07 +00001783/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001784/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001785/// ::= '(' ArgTypeListI ')'
1786/// ArgTypeListI
1787/// ::= /*empty*/
1788/// ::= '...'
1789/// ::= ArgTypeList ',' '...'
1790/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001791///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001792bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1793 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001794 isVarArg = false;
1795 assert(Lex.getKind() == lltok::lparen);
1796 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001797
Chris Lattnerac161bf2009-01-02 07:01:27 +00001798 if (Lex.getKind() == lltok::rparen) {
1799 // empty
1800 } else if (Lex.getKind() == lltok::dotdotdot) {
1801 isVarArg = true;
1802 Lex.Lex();
1803 } else {
1804 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001805 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001806 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001807 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001808
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001809 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001810 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001811
Chris Lattnerfdd87902009-10-05 05:54:46 +00001812 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001813 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001814
Chris Lattnerdef19492011-06-17 06:36:20 +00001815 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001816 Name = Lex.getStrVal();
1817 Lex.Lex();
1818 }
Chris Lattner3822f632009-01-02 08:05:26 +00001819
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001820 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001821 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001822
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001823 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001824 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001825 AttributeSet::get(ArgTy->getContext(),
1826 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001827
Chris Lattner3822f632009-01-02 08:05:26 +00001828 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001829 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001830 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001831 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001832 break;
1833 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001834
Chris Lattnerac161bf2009-01-02 07:01:27 +00001835 // Otherwise must be an argument type.
1836 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001837 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001838
Chris Lattnerfdd87902009-10-05 05:54:46 +00001839 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001840 return Error(TypeLoc, "argument can not have void type");
1841
Chris Lattnerdef19492011-06-17 06:36:20 +00001842 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001843 Name = Lex.getStrVal();
1844 Lex.Lex();
1845 } else {
1846 Name = "";
1847 }
Chris Lattner3822f632009-01-02 08:05:26 +00001848
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001849 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001850 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001851
Bill Wendlingd079a442012-10-15 04:46:55 +00001852 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001853 AttributeSet::get(ArgTy->getContext(),
1854 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001855 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001856 }
1857 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001858
Chris Lattner3822f632009-01-02 08:05:26 +00001859 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001860}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001861
Chris Lattnerac161bf2009-01-02 07:01:27 +00001862/// ParseFunctionType
1863/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001864bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001865 assert(Lex.getKind() == lltok::lparen);
1866
Chris Lattnerce473c72009-01-05 08:04:33 +00001867 if (!FunctionType::isValidReturnType(Result))
1868 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001869
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001870 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001871 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001872 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001873 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001874
Chris Lattnerac161bf2009-01-02 07:01:27 +00001875 // Reject names on the arguments lists.
1876 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1877 if (!ArgList[i].Name.empty())
1878 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001879 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001880 return Error(ArgList[i].Loc,
1881 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001882 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001883
Jay Foadb804a2b2011-07-12 14:06:48 +00001884 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001885 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001886 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001887
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001888 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001889 return false;
1890}
1891
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001892/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1893/// other structs.
1894bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1895 SmallVector<Type*, 8> Elts;
1896 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001897
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001898 Result = StructType::get(Context, Elts, Packed);
1899 return false;
1900}
1901
1902/// ParseStructDefinition - Parse a struct in a 'type' definition.
1903bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1904 std::pair<Type*, LocTy> &Entry,
1905 Type *&ResultTy) {
1906 // If the type was already defined, diagnose the redefinition.
1907 if (Entry.first && !Entry.second.isValid())
1908 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001909
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001910 // If we have opaque, just return without filling in the definition for the
1911 // struct. This counts as a definition as far as the .ll file goes.
1912 if (EatIfPresent(lltok::kw_opaque)) {
1913 // This type is being defined, so clear the location to indicate this.
1914 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001915
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001916 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001917 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001918 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001919 ResultTy = Entry.first;
1920 return false;
1921 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001922
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001923 // If the type starts with '<', then it is either a packed struct or a vector.
1924 bool isPacked = EatIfPresent(lltok::less);
1925
1926 // If we don't have a struct, then we have a random type alias, which we
1927 // accept for compatibility with old files. These types are not allowed to be
1928 // forward referenced and not allowed to be recursive.
1929 if (Lex.getKind() != lltok::lbrace) {
1930 if (Entry.first)
1931 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001932
Craig Topper2617dcc2014-04-15 06:32:26 +00001933 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001934 if (isPacked)
1935 return ParseArrayVectorType(ResultTy, true);
1936 return ParseType(ResultTy);
1937 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001938
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001939 // This type is being defined, so clear the location to indicate this.
1940 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001941
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001942 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001943 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001944 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001945
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001946 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001947
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001948 SmallVector<Type*, 8> Body;
1949 if (ParseStructBody(Body) ||
1950 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1951 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001952
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001953 STy->setBody(Body, isPacked);
1954 ResultTy = STy;
1955 return false;
1956}
1957
1958
Chris Lattnerac161bf2009-01-02 07:01:27 +00001959/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001960/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001961/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001962/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001963/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001964/// ::= '<' '{' Type (',' Type)* '}' '>'
1965bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001966 assert(Lex.getKind() == lltok::lbrace);
1967 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001968
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001969 // Handle the empty struct.
1970 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001971 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001972
Chris Lattnerf880ca22009-03-09 04:49:14 +00001973 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001974 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001975 if (ParseType(Ty)) return true;
1976 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001977
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001978 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001979 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001980
Chris Lattner3822f632009-01-02 08:05:26 +00001981 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001982 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001983 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001984
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001985 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001986 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001987
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001988 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001989 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001990
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001991 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001992}
1993
1994/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1995/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001996/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00001997/// ::= '[' APSINTVAL 'x' Types ']'
1998/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001999bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002000 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2001 Lex.getAPSIntVal().getBitWidth() > 64)
2002 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002003
Chris Lattnerac161bf2009-01-02 07:01:27 +00002004 LocTy SizeLoc = Lex.getLoc();
2005 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002006 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002007
Chris Lattner3822f632009-01-02 08:05:26 +00002008 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2009 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002010
2011 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002012 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002013 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002014
Chris Lattner3822f632009-01-02 08:05:26 +00002015 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2016 "expected end of sequential type"))
2017 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002018
Chris Lattnerac161bf2009-01-02 07:01:27 +00002019 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002020 if (Size == 0)
2021 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002022 if ((unsigned)Size != Size)
2023 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002024 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002025 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002026 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002027 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002028 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002029 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002030 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002031 }
2032 return false;
2033}
2034
2035//===----------------------------------------------------------------------===//
2036// Function Semantic Analysis.
2037//===----------------------------------------------------------------------===//
2038
Chris Lattner3432c622009-10-28 03:39:23 +00002039LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2040 int functionNumber)
2041 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002042
2043 // Insert unnamed arguments into the NumberedVals list.
2044 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2045 AI != E; ++AI)
2046 if (!AI->hasName())
2047 NumberedVals.push_back(AI);
2048}
2049
2050LLParser::PerFunctionState::~PerFunctionState() {
2051 // If there were any forward referenced non-basicblock values, delete them.
2052 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2053 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2054 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002055 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002056 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002057 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002058 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002059 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002060
Chris Lattnerac161bf2009-01-02 07:01:27 +00002061 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2062 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2063 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002064 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002065 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002066 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002067 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002068 }
2069}
2070
Chris Lattner3432c622009-10-28 03:39:23 +00002071bool LLParser::PerFunctionState::FinishFunction() {
2072 // Check to see if someone took the address of labels in this block.
2073 if (!P.ForwardRefBlockAddresses.empty()) {
2074 ValID FunctionID;
2075 if (!F.getName().empty()) {
2076 FunctionID.Kind = ValID::t_GlobalName;
2077 FunctionID.StrVal = F.getName();
2078 } else {
2079 FunctionID.Kind = ValID::t_GlobalID;
2080 FunctionID.UIntVal = FunctionNumber;
2081 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002082
Chris Lattner3432c622009-10-28 03:39:23 +00002083 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2084 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2085 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2086 // Resolve all these references.
2087 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2088 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002089
Chris Lattner3432c622009-10-28 03:39:23 +00002090 P.ForwardRefBlockAddresses.erase(FRBAI);
2091 }
2092 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002093
Chris Lattnerac161bf2009-01-02 07:01:27 +00002094 if (!ForwardRefVals.empty())
2095 return P.Error(ForwardRefVals.begin()->second.second,
2096 "use of undefined value '%" + ForwardRefVals.begin()->first +
2097 "'");
2098 if (!ForwardRefValIDs.empty())
2099 return P.Error(ForwardRefValIDs.begin()->second.second,
2100 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002101 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002102 return false;
2103}
2104
2105
2106/// GetVal - Get a value with the specified name or ID, creating a
2107/// forward reference record if needed. This can return null if the value
2108/// exists but does not have the right type.
2109Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002110 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002111 // Look this name up in the normal function symbol table.
2112 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002113
Chris Lattnerac161bf2009-01-02 07:01:27 +00002114 // If this is a forward reference for the value, see if we already created a
2115 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002116 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002117 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2118 I = ForwardRefVals.find(Name);
2119 if (I != ForwardRefVals.end())
2120 Val = I->second.first;
2121 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002122
Chris Lattnerac161bf2009-01-02 07:01:27 +00002123 // If we have the value in the symbol table or fwd-ref table, return it.
2124 if (Val) {
2125 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002126 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002127 P.Error(Loc, "'%" + Name + "' is not a basic block");
2128 else
2129 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002130 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002131 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002132 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002133
Chris Lattnerac161bf2009-01-02 07:01:27 +00002134 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002135 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002136 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002137 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002138 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002139
Chris Lattnerac161bf2009-01-02 07:01:27 +00002140 // Otherwise, create a new forward reference for this value and remember it.
2141 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002142 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002143 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002144 else
2145 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002146
Chris Lattnerac161bf2009-01-02 07:01:27 +00002147 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2148 return FwdVal;
2149}
2150
Chris Lattner229907c2011-07-18 04:54:35 +00002151Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002152 LocTy Loc) {
2153 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002154 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002155
Chris Lattnerac161bf2009-01-02 07:01:27 +00002156 // If this is a forward reference for the value, see if we already created a
2157 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002158 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002159 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2160 I = ForwardRefValIDs.find(ID);
2161 if (I != ForwardRefValIDs.end())
2162 Val = I->second.first;
2163 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002164
Chris Lattnerac161bf2009-01-02 07:01:27 +00002165 // If we have the value in the symbol table or fwd-ref table, return it.
2166 if (Val) {
2167 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002168 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002169 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002170 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002171 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002172 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002173 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002174 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002175
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002176 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002177 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002178 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002179 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002180
Chris Lattnerac161bf2009-01-02 07:01:27 +00002181 // Otherwise, create a new forward reference for this value and remember it.
2182 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002183 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002184 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002185 else
2186 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002187
Chris Lattnerac161bf2009-01-02 07:01:27 +00002188 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2189 return FwdVal;
2190}
2191
2192/// SetInstName - After an instruction is parsed and inserted into its
2193/// basic block, this installs its name.
2194bool LLParser::PerFunctionState::SetInstName(int NameID,
2195 const std::string &NameStr,
2196 LocTy NameLoc, Instruction *Inst) {
2197 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002198 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002199 if (NameID != -1 || !NameStr.empty())
2200 return P.Error(NameLoc, "instructions returning void cannot have a name");
2201 return false;
2202 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002203
Chris Lattnerac161bf2009-01-02 07:01:27 +00002204 // If this was a numbered instruction, verify that the instruction is the
2205 // expected value and resolve any forward references.
2206 if (NameStr.empty()) {
2207 // If neither a name nor an ID was specified, just use the next ID.
2208 if (NameID == -1)
2209 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002210
Chris Lattnerac161bf2009-01-02 07:01:27 +00002211 if (unsigned(NameID) != NumberedVals.size())
2212 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002213 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002214
Chris Lattnerac161bf2009-01-02 07:01:27 +00002215 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2216 ForwardRefValIDs.find(NameID);
2217 if (FI != ForwardRefValIDs.end()) {
2218 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002219 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002220 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002221 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002222 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002223 ForwardRefValIDs.erase(FI);
2224 }
2225
2226 NumberedVals.push_back(Inst);
2227 return false;
2228 }
2229
2230 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2231 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2232 FI = ForwardRefVals.find(NameStr);
2233 if (FI != ForwardRefVals.end()) {
2234 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002235 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002236 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002237 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002238 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002239 ForwardRefVals.erase(FI);
2240 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002241
Chris Lattnerac161bf2009-01-02 07:01:27 +00002242 // Set the name on the instruction.
2243 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002244
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002245 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002246 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002247 NameStr + "'");
2248 return false;
2249}
2250
2251/// GetBB - Get a basic block with the specified name or ID, creating a
2252/// forward reference record if needed.
2253BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2254 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002255 return cast_or_null<BasicBlock>(GetVal(Name,
2256 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002257}
2258
2259BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002260 return cast_or_null<BasicBlock>(GetVal(ID,
2261 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002262}
2263
2264/// DefineBB - Define the specified basic block, which is either named or
2265/// unnamed. If there is an error, this returns null otherwise it returns
2266/// the block being defined.
2267BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2268 LocTy Loc) {
2269 BasicBlock *BB;
2270 if (Name.empty())
2271 BB = GetBB(NumberedVals.size(), Loc);
2272 else
2273 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002274 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002275
Chris Lattnerac161bf2009-01-02 07:01:27 +00002276 // Move the block to the end of the function. Forward ref'd blocks are
2277 // inserted wherever they happen to be referenced.
2278 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002279
Chris Lattnerac161bf2009-01-02 07:01:27 +00002280 // Remove the block from forward ref sets.
2281 if (Name.empty()) {
2282 ForwardRefValIDs.erase(NumberedVals.size());
2283 NumberedVals.push_back(BB);
2284 } else {
2285 // BB forward references are already in the function symbol table.
2286 ForwardRefVals.erase(Name);
2287 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002288
Chris Lattnerac161bf2009-01-02 07:01:27 +00002289 return BB;
2290}
2291
2292//===----------------------------------------------------------------------===//
2293// Constants.
2294//===----------------------------------------------------------------------===//
2295
2296/// ParseValID - Parse an abstract value that doesn't necessarily have a
2297/// type implied. For example, if we parse "4" we don't know what integer type
2298/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002299/// sanity. PFS is used to convert function-local operands of metadata (since
2300/// metadata operands are not just parsed here but also converted to values).
2301/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002302bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002303 ID.Loc = Lex.getLoc();
2304 switch (Lex.getKind()) {
2305 default: return TokError("expected value token");
2306 case lltok::GlobalID: // @42
2307 ID.UIntVal = Lex.getUIntVal();
2308 ID.Kind = ValID::t_GlobalID;
2309 break;
2310 case lltok::GlobalVar: // @foo
2311 ID.StrVal = Lex.getStrVal();
2312 ID.Kind = ValID::t_GlobalName;
2313 break;
2314 case lltok::LocalVarID: // %42
2315 ID.UIntVal = Lex.getUIntVal();
2316 ID.Kind = ValID::t_LocalID;
2317 break;
2318 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002319 ID.StrVal = Lex.getStrVal();
2320 ID.Kind = ValID::t_LocalName;
2321 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002322 case lltok::exclaim: // !42, !{...}, or !"foo"
2323 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002324 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002325 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002326 ID.Kind = ValID::t_APSInt;
2327 break;
2328 case lltok::APFloat:
2329 ID.APFloatVal = Lex.getAPFloatVal();
2330 ID.Kind = ValID::t_APFloat;
2331 break;
2332 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002333 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002334 ID.Kind = ValID::t_Constant;
2335 break;
2336 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002337 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002338 ID.Kind = ValID::t_Constant;
2339 break;
2340 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2341 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2342 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002343
Chris Lattnerac161bf2009-01-02 07:01:27 +00002344 case lltok::lbrace: {
2345 // ValID ::= '{' ConstVector '}'
2346 Lex.Lex();
2347 SmallVector<Constant*, 16> Elts;
2348 if (ParseGlobalValueVector(Elts) ||
2349 ParseToken(lltok::rbrace, "expected end of struct constant"))
2350 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002351
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002352 ID.ConstantStructElts = new Constant*[Elts.size()];
2353 ID.UIntVal = Elts.size();
2354 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2355 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002356 return false;
2357 }
2358 case lltok::less: {
2359 // ValID ::= '<' ConstVector '>' --> Vector.
2360 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2361 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002362 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002363
Chris Lattnerac161bf2009-01-02 07:01:27 +00002364 SmallVector<Constant*, 16> Elts;
2365 LocTy FirstEltLoc = Lex.getLoc();
2366 if (ParseGlobalValueVector(Elts) ||
2367 (isPackedStruct &&
2368 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2369 ParseToken(lltok::greater, "expected end of constant"))
2370 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002371
Chris Lattnerac161bf2009-01-02 07:01:27 +00002372 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002373 ID.ConstantStructElts = new Constant*[Elts.size()];
2374 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2375 ID.UIntVal = Elts.size();
2376 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002377 return false;
2378 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002379
Chris Lattnerac161bf2009-01-02 07:01:27 +00002380 if (Elts.empty())
2381 return Error(ID.Loc, "constant vector must not be empty");
2382
Duncan Sands9dff9be2010-02-15 16:12:20 +00002383 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002384 !Elts[0]->getType()->isFloatingPointTy() &&
2385 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002386 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002387 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002388
Chris Lattnerac161bf2009-01-02 07:01:27 +00002389 // Verify that all the vector elements have the same type.
2390 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2391 if (Elts[i]->getType() != Elts[0]->getType())
2392 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002393 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002394 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002395
Chris Lattner69229312011-02-15 00:14:00 +00002396 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002397 ID.Kind = ValID::t_Constant;
2398 return false;
2399 }
2400 case lltok::lsquare: { // Array Constant
2401 Lex.Lex();
2402 SmallVector<Constant*, 16> Elts;
2403 LocTy FirstEltLoc = Lex.getLoc();
2404 if (ParseGlobalValueVector(Elts) ||
2405 ParseToken(lltok::rsquare, "expected end of array constant"))
2406 return true;
2407
2408 // Handle empty element.
2409 if (Elts.empty()) {
2410 // Use undef instead of an array because it's inconvenient to determine
2411 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002412 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002413 return false;
2414 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002415
Chris Lattnerac161bf2009-01-02 07:01:27 +00002416 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002417 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002418 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002419
Owen Anderson4056ca92009-07-29 22:17:13 +00002420 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002421
Chris Lattnerac161bf2009-01-02 07:01:27 +00002422 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002423 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002424 if (Elts[i]->getType() != Elts[0]->getType())
2425 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002426 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002427 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002428 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002429
Jay Foad83be3612011-06-22 09:24:39 +00002430 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002431 ID.Kind = ValID::t_Constant;
2432 return false;
2433 }
2434 case lltok::kw_c: // c "foo"
2435 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002436 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2437 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002438 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2439 ID.Kind = ValID::t_Constant;
2440 return false;
2441
2442 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002443 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2444 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002445 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002446 Lex.Lex();
2447 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002448 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002449 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002450 ParseStringConstant(ID.StrVal) ||
2451 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002452 ParseToken(lltok::StringConstant, "expected constraint string"))
2453 return true;
2454 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002455 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002456 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002457 ID.Kind = ValID::t_InlineAsm;
2458 return false;
2459 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002460
Chris Lattner3432c622009-10-28 03:39:23 +00002461 case lltok::kw_blockaddress: {
2462 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2463 Lex.Lex();
2464
2465 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002466
Chris Lattner3432c622009-10-28 03:39:23 +00002467 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2468 ParseValID(Fn) ||
2469 ParseToken(lltok::comma, "expected comma in block address expression")||
2470 ParseValID(Label) ||
2471 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2472 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002473
Chris Lattner3432c622009-10-28 03:39:23 +00002474 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2475 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002476 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002477 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002478
Chris Lattner3432c622009-10-28 03:39:23 +00002479 // Make a global variable as a placeholder for this reference.
2480 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2481 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002482 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002483 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2484 ID.ConstantVal = FwdRef;
2485 ID.Kind = ValID::t_Constant;
2486 return false;
2487 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002488
Chris Lattnerac161bf2009-01-02 07:01:27 +00002489 case lltok::kw_trunc:
2490 case lltok::kw_zext:
2491 case lltok::kw_sext:
2492 case lltok::kw_fptrunc:
2493 case lltok::kw_fpext:
2494 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002495 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002496 case lltok::kw_uitofp:
2497 case lltok::kw_sitofp:
2498 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002499 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002500 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002501 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002502 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002503 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002504 Constant *SrcVal;
2505 Lex.Lex();
2506 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2507 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002508 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002509 ParseType(DestTy) ||
2510 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2511 return true;
2512 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2513 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002514 getTypeString(SrcVal->getType()) + "' to '" +
2515 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002516 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002517 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002518 ID.Kind = ValID::t_Constant;
2519 return false;
2520 }
2521 case lltok::kw_extractvalue: {
2522 Lex.Lex();
2523 Constant *Val;
2524 SmallVector<unsigned, 4> Indices;
2525 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2526 ParseGlobalTypeAndValue(Val) ||
2527 ParseIndexList(Indices) ||
2528 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2529 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002530
Chris Lattner392be582010-02-12 20:49:41 +00002531 if (!Val->getType()->isAggregateType())
2532 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002533 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002534 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002535 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002536 ID.Kind = ValID::t_Constant;
2537 return false;
2538 }
2539 case lltok::kw_insertvalue: {
2540 Lex.Lex();
2541 Constant *Val0, *Val1;
2542 SmallVector<unsigned, 4> Indices;
2543 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2544 ParseGlobalTypeAndValue(Val0) ||
2545 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2546 ParseGlobalTypeAndValue(Val1) ||
2547 ParseIndexList(Indices) ||
2548 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2549 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002550 if (!Val0->getType()->isAggregateType())
2551 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002552 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002553 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002554 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002555 ID.Kind = ValID::t_Constant;
2556 return false;
2557 }
2558 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002559 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002560 unsigned PredVal, Opc = Lex.getUIntVal();
2561 Constant *Val0, *Val1;
2562 Lex.Lex();
2563 if (ParseCmpPredicate(PredVal, Opc) ||
2564 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2565 ParseGlobalTypeAndValue(Val0) ||
2566 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2567 ParseGlobalTypeAndValue(Val1) ||
2568 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2569 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002570
Chris Lattnerac161bf2009-01-02 07:01:27 +00002571 if (Val0->getType() != Val1->getType())
2572 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002573
Chris Lattnerac161bf2009-01-02 07:01:27 +00002574 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002575
Chris Lattnerac161bf2009-01-02 07:01:27 +00002576 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002577 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002578 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002579 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002580 } else {
2581 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002582 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002583 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002584 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002585 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002586 }
2587 ID.Kind = ValID::t_Constant;
2588 return false;
2589 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002590
Chris Lattnerac161bf2009-01-02 07:01:27 +00002591 // Binary Operators.
2592 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002593 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002594 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002595 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002596 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002597 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002598 case lltok::kw_udiv:
2599 case lltok::kw_sdiv:
2600 case lltok::kw_fdiv:
2601 case lltok::kw_urem:
2602 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002603 case lltok::kw_frem:
2604 case lltok::kw_shl:
2605 case lltok::kw_lshr:
2606 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002607 bool NUW = false;
2608 bool NSW = false;
2609 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002610 unsigned Opc = Lex.getUIntVal();
2611 Constant *Val0, *Val1;
2612 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002613 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002614 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2615 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002616 if (EatIfPresent(lltok::kw_nuw))
2617 NUW = true;
2618 if (EatIfPresent(lltok::kw_nsw)) {
2619 NSW = true;
2620 if (EatIfPresent(lltok::kw_nuw))
2621 NUW = true;
2622 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002623 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2624 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002625 if (EatIfPresent(lltok::kw_exact))
2626 Exact = true;
2627 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002628 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2629 ParseGlobalTypeAndValue(Val0) ||
2630 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2631 ParseGlobalTypeAndValue(Val1) ||
2632 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2633 return true;
2634 if (Val0->getType() != Val1->getType())
2635 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002636 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002637 if (NUW)
2638 return Error(ModifierLoc, "nuw only applies to integer operations");
2639 if (NSW)
2640 return Error(ModifierLoc, "nsw only applies to integer operations");
2641 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002642 // Check that the type is valid for the operator.
2643 switch (Opc) {
2644 case Instruction::Add:
2645 case Instruction::Sub:
2646 case Instruction::Mul:
2647 case Instruction::UDiv:
2648 case Instruction::SDiv:
2649 case Instruction::URem:
2650 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002651 case Instruction::Shl:
2652 case Instruction::AShr:
2653 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002654 if (!Val0->getType()->isIntOrIntVectorTy())
2655 return Error(ID.Loc, "constexpr requires integer operands");
2656 break;
2657 case Instruction::FAdd:
2658 case Instruction::FSub:
2659 case Instruction::FMul:
2660 case Instruction::FDiv:
2661 case Instruction::FRem:
2662 if (!Val0->getType()->isFPOrFPVectorTy())
2663 return Error(ID.Loc, "constexpr requires fp operands");
2664 break;
2665 default: llvm_unreachable("Unknown binary operator!");
2666 }
Dan Gohman1b849082009-09-07 23:54:19 +00002667 unsigned Flags = 0;
2668 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2669 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002670 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002671 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002672 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002673 ID.Kind = ValID::t_Constant;
2674 return false;
2675 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002676
Chris Lattnerac161bf2009-01-02 07:01:27 +00002677 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002678 case lltok::kw_and:
2679 case lltok::kw_or:
2680 case lltok::kw_xor: {
2681 unsigned Opc = Lex.getUIntVal();
2682 Constant *Val0, *Val1;
2683 Lex.Lex();
2684 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2685 ParseGlobalTypeAndValue(Val0) ||
2686 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2687 ParseGlobalTypeAndValue(Val1) ||
2688 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2689 return true;
2690 if (Val0->getType() != Val1->getType())
2691 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002692 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002693 return Error(ID.Loc,
2694 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002695 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002696 ID.Kind = ValID::t_Constant;
2697 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002698 }
2699
Chris Lattnerac161bf2009-01-02 07:01:27 +00002700 case lltok::kw_getelementptr:
2701 case lltok::kw_shufflevector:
2702 case lltok::kw_insertelement:
2703 case lltok::kw_extractelement:
2704 case lltok::kw_select: {
2705 unsigned Opc = Lex.getUIntVal();
2706 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002707 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002708 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002709 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002710 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002711 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2712 ParseGlobalValueVector(Elts) ||
2713 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2714 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002715
Chris Lattnerac161bf2009-01-02 07:01:27 +00002716 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002717 if (Elts.size() == 0 ||
2718 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002719 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002720
Jay Foaded8db7d2011-07-21 14:31:17 +00002721 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002722 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002723 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002724 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2725 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002726 } else if (Opc == Instruction::Select) {
2727 if (Elts.size() != 3)
2728 return Error(ID.Loc, "expected three operands to select");
2729 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2730 Elts[2]))
2731 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002732 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002733 } else if (Opc == Instruction::ShuffleVector) {
2734 if (Elts.size() != 3)
2735 return Error(ID.Loc, "expected three operands to shufflevector");
2736 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2737 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002738 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002739 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002740 } else if (Opc == Instruction::ExtractElement) {
2741 if (Elts.size() != 2)
2742 return Error(ID.Loc, "expected two operands to extractelement");
2743 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2744 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002745 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002746 } else {
2747 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2748 if (Elts.size() != 3)
2749 return Error(ID.Loc, "expected three operands to insertelement");
2750 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2751 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002752 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002753 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002754 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002755
Chris Lattnerac161bf2009-01-02 07:01:27 +00002756 ID.Kind = ValID::t_Constant;
2757 return false;
2758 }
2759 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002760
Chris Lattnerac161bf2009-01-02 07:01:27 +00002761 Lex.Lex();
2762 return false;
2763}
2764
2765/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002766bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002767 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002768 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002769 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002770 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002771 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002772 if (V && !(C = dyn_cast<Constant>(V)))
2773 return Error(ID.Loc, "global values must be constants");
2774 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002775}
2776
Victor Hernandez9d75c962010-01-11 22:31:58 +00002777bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002778 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002779 return ParseType(Ty) ||
2780 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002781}
2782
2783/// ParseGlobalValueVector
2784/// ::= /*empty*/
2785/// ::= TypeAndValue (',' TypeAndValue)*
2786bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2787 // Empty list.
2788 if (Lex.getKind() == lltok::rbrace ||
2789 Lex.getKind() == lltok::rsquare ||
2790 Lex.getKind() == lltok::greater ||
2791 Lex.getKind() == lltok::rparen)
2792 return false;
2793
2794 Constant *C;
2795 if (ParseGlobalTypeAndValue(C)) return true;
2796 Elts.push_back(C);
2797
2798 while (EatIfPresent(lltok::comma)) {
2799 if (ParseGlobalTypeAndValue(C)) return true;
2800 Elts.push_back(C);
2801 }
2802
2803 return false;
2804}
2805
Dan Gohmanc828c542010-08-24 02:24:03 +00002806bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2807 assert(Lex.getKind() == lltok::lbrace);
2808 Lex.Lex();
2809
2810 SmallVector<Value*, 16> Elts;
2811 if (ParseMDNodeVector(Elts, PFS) ||
2812 ParseToken(lltok::rbrace, "expected end of metadata node"))
2813 return true;
2814
Jay Foad5514afe2011-04-21 19:59:31 +00002815 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002816 ID.Kind = ValID::t_MDNode;
2817 return false;
2818}
2819
Dan Gohman8939ba332010-07-14 18:26:50 +00002820/// ParseMetadataValue
2821/// ::= !42
2822/// ::= !{...}
2823/// ::= !"string"
2824bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2825 assert(Lex.getKind() == lltok::exclaim);
2826 Lex.Lex();
2827
2828 // MDNode:
2829 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002830 if (Lex.getKind() == lltok::lbrace)
2831 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002832
2833 // Standalone metadata reference
2834 // !42
2835 if (Lex.getKind() == lltok::APSInt) {
2836 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2837 ID.Kind = ValID::t_MDNode;
2838 return false;
2839 }
2840
2841 // MDString:
2842 // ::= '!' STRINGCONSTANT
2843 if (ParseMDString(ID.MDStringVal)) return true;
2844 ID.Kind = ValID::t_MDString;
2845 return false;
2846}
2847
Victor Hernandez9d75c962010-01-11 22:31:58 +00002848
2849//===----------------------------------------------------------------------===//
2850// Function Parsing.
2851//===----------------------------------------------------------------------===//
2852
Chris Lattner229907c2011-07-18 04:54:35 +00002853bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002854 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002855 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002856 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002857
Chris Lattnerac161bf2009-01-02 07:01:27 +00002858 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002859 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002860 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2861 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002862 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002863 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002864 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2865 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002866 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002867 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002868 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002869 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002870 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002871 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2872 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002873 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002874 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002875 return false;
2876 }
2877 case ValID::t_MDNode:
2878 if (!Ty->isMetadataTy())
2879 return Error(ID.Loc, "metadata value must have metadata type");
2880 V = ID.MDNodeVal;
2881 return false;
2882 case ValID::t_MDString:
2883 if (!Ty->isMetadataTy())
2884 return Error(ID.Loc, "metadata value must have metadata type");
2885 V = ID.MDStringVal;
2886 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002887 case ValID::t_GlobalName:
2888 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002889 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002890 case ValID::t_GlobalID:
2891 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002892 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002893 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002894 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002895 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002896 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002897 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002898 return false;
2899 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002900 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002901 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2902 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002903
Dan Gohman518cda42011-12-17 00:04:22 +00002904 // The lexer has no type info, so builds all half, float, and double FP
2905 // constants as double. Fix this here. Long double does not need this.
2906 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002907 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002908 if (Ty->isHalfTy())
2909 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2910 &Ignored);
2911 else if (Ty->isFloatTy())
2912 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2913 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002914 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002915 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002916
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002917 if (V->getType() != Ty)
2918 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002919 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002920
Chris Lattnerac161bf2009-01-02 07:01:27 +00002921 return false;
2922 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002923 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002924 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002925 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002926 return false;
2927 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002928 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002929 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002930 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002931 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002932 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002933 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002934 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002935 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002936 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002937 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002938 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002939 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002940 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002941 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002942 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002943 return false;
2944 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002945 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002946 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002947
Chris Lattnerac161bf2009-01-02 07:01:27 +00002948 V = ID.ConstantVal;
2949 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002950 case ValID::t_ConstantStruct:
2951 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002952 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002953 if (ST->getNumElements() != ID.UIntVal)
2954 return Error(ID.Loc,
2955 "initializer with struct type has wrong # elements");
2956 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2957 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002958
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002959 // Verify that the elements are compatible with the structtype.
2960 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2961 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2962 return Error(ID.Loc, "element " + Twine(i) +
2963 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002964
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002965 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2966 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002967 } else
2968 return Error(ID.Loc, "constant expression type mismatch");
2969 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002970 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002971 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002972}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002973
Chris Lattner229907c2011-07-18 04:54:35 +00002974bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002975 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002976 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002977 return ParseValID(ID, PFS) ||
2978 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002979}
2980
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002981bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002982 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002983 return ParseType(Ty) ||
2984 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002985}
2986
Chris Lattner3ed871f2009-10-27 19:13:16 +00002987bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2988 PerFunctionState &PFS) {
2989 Value *V;
2990 Loc = Lex.getLoc();
2991 if (ParseTypeAndValue(V, PFS)) return true;
2992 if (!isa<BasicBlock>(V))
2993 return Error(Loc, "expected a basic block");
2994 BB = cast<BasicBlock>(V);
2995 return false;
2996}
2997
2998
Chris Lattnerac161bf2009-01-02 07:01:27 +00002999/// FunctionHeader
3000/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00003001/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003002/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00003003bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
3004 // Parse the linkage.
3005 LocTy LinkageLoc = Lex.getLoc();
3006 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003007
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00003008 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00003009 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00003010 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003011 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003012 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003013 LocTy RetTypeLoc = Lex.getLoc();
3014 if (ParseOptionalLinkage(Linkage) ||
3015 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003016 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003017 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003018 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003019 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003020 return true;
3021
3022 // Verify that the linkage is ok.
3023 switch ((GlobalValue::LinkageTypes)Linkage) {
3024 case GlobalValue::ExternalLinkage:
3025 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003026 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003027 if (isDefine)
3028 return Error(LinkageLoc, "invalid linkage for function definition");
3029 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003030 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003031 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003032 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003033 case GlobalValue::LinkOnceAnyLinkage:
3034 case GlobalValue::LinkOnceODRLinkage:
3035 case GlobalValue::WeakAnyLinkage:
3036 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003037 if (!isDefine)
3038 return Error(LinkageLoc, "invalid linkage for function declaration");
3039 break;
3040 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003041 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003042 return Error(LinkageLoc, "invalid function linkage type");
3043 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003044
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003045 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3046 return Error(LinkageLoc,
3047 "symbol with local linkage must have default visibility");
3048
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003049 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003050 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003051
Chris Lattnerac161bf2009-01-02 07:01:27 +00003052 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003053
3054 std::string FunctionName;
3055 if (Lex.getKind() == lltok::GlobalVar) {
3056 FunctionName = Lex.getStrVal();
3057 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3058 unsigned NameID = Lex.getUIntVal();
3059
3060 if (NameID != NumberedVals.size())
3061 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003062 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003063 } else {
3064 return TokError("expected function name");
3065 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003066
Chris Lattner3822f632009-01-02 08:05:26 +00003067 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003068
Chris Lattner3822f632009-01-02 08:05:26 +00003069 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003070 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003071
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003072 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003073 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003074 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003075 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003076 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003077 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003078 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003079 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003080 bool UnnamedAddr;
3081 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003082 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003083
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003084 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003085 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3086 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003087 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003088 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003089 (EatIfPresent(lltok::kw_section) &&
3090 ParseStringConstant(Section)) ||
3091 ParseOptionalAlignment(Alignment) ||
3092 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003093 ParseStringConstant(GC)) ||
3094 (EatIfPresent(lltok::kw_prefix) &&
3095 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003096 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003097
Michael Gottesman41748d72013-06-27 00:25:01 +00003098 if (FuncAttrs.contains(Attribute::Builtin))
3099 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003100
Chris Lattnerac161bf2009-01-02 07:01:27 +00003101 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003102 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003103 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003104 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003105 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003106
Chris Lattnerac161bf2009-01-02 07:01:27 +00003107 // Okay, if we got here, the function is syntactically valid. Convert types
3108 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003109 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003110 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003111
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003112 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003113 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3114 AttributeSet::ReturnIndex,
3115 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003116
Chris Lattnerac161bf2009-01-02 07:01:27 +00003117 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003118 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003119 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3120 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003121 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3122 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003123 }
3124
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003125 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003126 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3127 AttributeSet::FunctionIndex,
3128 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003129
Bill Wendlinge94d8432012-12-07 23:16:57 +00003130 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003131
Bill Wendling749a43d2012-12-30 13:50:49 +00003132 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003133 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3134
Chris Lattner229907c2011-07-18 04:54:35 +00003135 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003136 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003137 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003138
Craig Topper2617dcc2014-04-15 06:32:26 +00003139 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003140 if (!FunctionName.empty()) {
3141 // If this was a definition of a forward reference, remove the definition
3142 // from the forward reference table and fill in the forward ref.
3143 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3144 ForwardRefVals.find(FunctionName);
3145 if (FRVI != ForwardRefVals.end()) {
3146 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003147 if (!Fn)
3148 return Error(FRVI->second.second, "invalid forward reference to "
3149 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003150 if (Fn->getType() != PFT)
3151 return Error(FRVI->second.second, "invalid forward reference to "
3152 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003153
Chris Lattnerac161bf2009-01-02 07:01:27 +00003154 ForwardRefVals.erase(FRVI);
3155 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003156 // Reject redefinitions.
3157 return Error(NameLoc, "invalid redefinition of function '" +
3158 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003159 } else if (M->getNamedValue(FunctionName)) {
3160 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003161 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003162
Dan Gohman399d6ae2009-08-29 23:37:49 +00003163 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003164 // If this is a definition of a forward referenced function, make sure the
3165 // types agree.
3166 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3167 = ForwardRefValIDs.find(NumberedVals.size());
3168 if (I != ForwardRefValIDs.end()) {
3169 Fn = cast<Function>(I->second.first);
3170 if (Fn->getType() != PFT)
3171 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003172 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003173 ForwardRefValIDs.erase(I);
3174 }
3175 }
3176
Craig Topper2617dcc2014-04-15 06:32:26 +00003177 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003178 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3179 else // Move the forward-reference to the correct spot in the module.
3180 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3181
3182 if (FunctionName.empty())
3183 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003184
Chris Lattnerac161bf2009-01-02 07:01:27 +00003185 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3186 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003187 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003188 Fn->setCallingConv(CC);
3189 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003190 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003191 Fn->setAlignment(Alignment);
3192 Fn->setSection(Section);
3193 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003194 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003195 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003196
Chris Lattnerac161bf2009-01-02 07:01:27 +00003197 // Add all of the arguments we parsed to the function.
3198 Function::arg_iterator ArgIt = Fn->arg_begin();
3199 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3200 // If the argument has a name, insert it into the argument symbol table.
3201 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003202
Chris Lattnerac161bf2009-01-02 07:01:27 +00003203 // Set the name, if it conflicted, it will be auto-renamed.
3204 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003205
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003206 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003207 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3208 ArgList[i].Name + "'");
3209 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003210
Chris Lattnerac161bf2009-01-02 07:01:27 +00003211 return false;
3212}
3213
3214
3215/// ParseFunctionBody
3216/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003217///
3218bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003219 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003220 return TokError("expected '{' in function body");
3221 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003222
Chris Lattner3432c622009-10-28 03:39:23 +00003223 int FunctionNumber = -1;
3224 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003225
Chris Lattner3432c622009-10-28 03:39:23 +00003226 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003227
Chris Lattnerbbddd962010-01-09 19:20:07 +00003228 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003229 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003230 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003231
Chris Lattner4649a732011-06-17 06:42:57 +00003232 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003233 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003234
Chris Lattnerac161bf2009-01-02 07:01:27 +00003235 // Eat the }.
3236 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003237
Chris Lattnerac161bf2009-01-02 07:01:27 +00003238 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003239 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003240}
3241
3242/// ParseBasicBlock
3243/// ::= LabelStr? Instruction*
3244bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3245 // If this basic block starts out with a name, remember it.
3246 std::string Name;
3247 LocTy NameLoc = Lex.getLoc();
3248 if (Lex.getKind() == lltok::LabelStr) {
3249 Name = Lex.getStrVal();
3250 Lex.Lex();
3251 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003252
Chris Lattnerac161bf2009-01-02 07:01:27 +00003253 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003254 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003255
Chris Lattnerac161bf2009-01-02 07:01:27 +00003256 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003257
Chris Lattnerac161bf2009-01-02 07:01:27 +00003258 // Parse the instructions in this block until we get a terminator.
3259 Instruction *Inst;
3260 do {
3261 // This instruction may have three possibilities for a name: a) none
3262 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3263 LocTy NameLoc = Lex.getLoc();
3264 int NameID = -1;
3265 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003266
Chris Lattnerac161bf2009-01-02 07:01:27 +00003267 if (Lex.getKind() == lltok::LocalVarID) {
3268 NameID = Lex.getUIntVal();
3269 Lex.Lex();
3270 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3271 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003272 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003273 NameStr = Lex.getStrVal();
3274 Lex.Lex();
3275 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3276 return true;
3277 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003278
Chris Lattner77b89dc2009-12-30 05:23:43 +00003279 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003280 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003281 case InstError: return true;
3282 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003283 BB->getInstList().push_back(Inst);
3284
Chris Lattner77b89dc2009-12-30 05:23:43 +00003285 // With a normal result, we check to see if the instruction is followed by
3286 // a comma and metadata.
3287 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003288 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003289 return true;
3290 break;
3291 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003292 BB->getInstList().push_back(Inst);
3293
Chris Lattner77b89dc2009-12-30 05:23:43 +00003294 // If the instruction parser ate an extra comma at the end of it, it
3295 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003296 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003297 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003298 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003299 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003300
Chris Lattnerac161bf2009-01-02 07:01:27 +00003301 // Set the name on the instruction.
3302 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3303 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003304
Chris Lattnerac161bf2009-01-02 07:01:27 +00003305 return false;
3306}
3307
3308//===----------------------------------------------------------------------===//
3309// Instruction Parsing.
3310//===----------------------------------------------------------------------===//
3311
3312/// ParseInstruction - Parse one of the many different instructions.
3313///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003314int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3315 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003316 lltok::Kind Token = Lex.getKind();
3317 if (Token == lltok::Eof)
3318 return TokError("found end of file when expecting more instructions");
3319 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003320 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003321 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003322
Chris Lattnerac161bf2009-01-02 07:01:27 +00003323 switch (Token) {
3324 default: return Error(Loc, "expected instruction opcode");
3325 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003326 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003327 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3328 case lltok::kw_br: return ParseBr(Inst, PFS);
3329 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003330 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003331 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003332 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003333 // Binary Operators.
3334 case lltok::kw_add:
3335 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003336 case lltok::kw_mul:
3337 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003338 bool NUW = EatIfPresent(lltok::kw_nuw);
3339 bool NSW = EatIfPresent(lltok::kw_nsw);
3340 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003341
Chris Lattnera676c0f2011-02-07 16:40:21 +00003342 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003343
Chris Lattnera676c0f2011-02-07 16:40:21 +00003344 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3345 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3346 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003347 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003348 case lltok::kw_fadd:
3349 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003350 case lltok::kw_fmul:
3351 case lltok::kw_fdiv:
3352 case lltok::kw_frem: {
3353 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3354 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3355 if (Res != 0)
3356 return Res;
3357 if (FMF.any())
3358 Inst->setFastMathFlags(FMF);
3359 return 0;
3360 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003361
Chris Lattner35315d02011-02-06 21:44:57 +00003362 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003363 case lltok::kw_udiv:
3364 case lltok::kw_lshr:
3365 case lltok::kw_ashr: {
3366 bool Exact = EatIfPresent(lltok::kw_exact);
3367
3368 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3369 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3370 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003371 }
3372
Chris Lattnerac161bf2009-01-02 07:01:27 +00003373 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003374 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003375 case lltok::kw_and:
3376 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003377 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003378 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003379 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003380 // Casts.
3381 case lltok::kw_trunc:
3382 case lltok::kw_zext:
3383 case lltok::kw_sext:
3384 case lltok::kw_fptrunc:
3385 case lltok::kw_fpext:
3386 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003387 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003388 case lltok::kw_uitofp:
3389 case lltok::kw_sitofp:
3390 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003391 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003392 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003393 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003394 // Other.
3395 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003396 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003397 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3398 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3399 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3400 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003401 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003402 // Call.
3403 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3404 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3405 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003406 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003407 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003408 case lltok::kw_load: return ParseLoad(Inst, PFS);
3409 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003410 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3411 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003412 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003413 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3414 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3415 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3416 }
3417}
3418
3419/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3420bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003421 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003422 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003423 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003424 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3425 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3426 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3427 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3428 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3429 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3430 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3431 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3432 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3433 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3434 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3435 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3436 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3437 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3438 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3439 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3440 }
3441 } else {
3442 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003443 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003444 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3445 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3446 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3447 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3448 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3449 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3450 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3451 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3452 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3453 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3454 }
3455 }
3456 Lex.Lex();
3457 return false;
3458}
3459
3460//===----------------------------------------------------------------------===//
3461// Terminator Instructions.
3462//===----------------------------------------------------------------------===//
3463
3464/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003465/// ::= 'ret' void (',' !dbg, !1)*
3466/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003467bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003468 PerFunctionState &PFS) {
3469 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003470 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003471 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003472
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003473 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003474
Chris Lattnerfdd87902009-10-05 05:54:46 +00003475 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003476 if (!ResType->isVoidTy())
3477 return Error(TypeLoc, "value doesn't match function result type '" +
3478 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003479
Owen Anderson55f1c092009-08-13 21:58:54 +00003480 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003481 return false;
3482 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003483
Chris Lattnerac161bf2009-01-02 07:01:27 +00003484 Value *RV;
3485 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003486
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003487 if (ResType != RV->getType())
3488 return Error(TypeLoc, "value doesn't match function result type '" +
3489 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003490
Owen Anderson55f1c092009-08-13 21:58:54 +00003491 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003492 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003493}
3494
3495
3496/// ParseBr
3497/// ::= 'br' TypeAndValue
3498/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3499bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3500 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003501 Value *Op0;
3502 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003503 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003504
Chris Lattnerac161bf2009-01-02 07:01:27 +00003505 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3506 Inst = BranchInst::Create(BB);
3507 return false;
3508 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003509
Owen Anderson55f1c092009-08-13 21:58:54 +00003510 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003511 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003512
Chris Lattnerac161bf2009-01-02 07:01:27 +00003513 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003514 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003515 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003516 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003517 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003518
Chris Lattner3ed871f2009-10-27 19:13:16 +00003519 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003520 return false;
3521}
3522
3523/// ParseSwitch
3524/// Instruction
3525/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3526/// JumpTable
3527/// ::= (TypeAndValue ',' TypeAndValue)*
3528bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3529 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003530 Value *Cond;
3531 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003532 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3533 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003534 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003535 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3536 return true;
3537
Duncan Sands19d0b472010-02-16 11:11:14 +00003538 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003539 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003540
Chris Lattnerac161bf2009-01-02 07:01:27 +00003541 // Parse the jump table pairs.
3542 SmallPtrSet<Value*, 32> SeenCases;
3543 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3544 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003545 Value *Constant;
3546 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003547
Chris Lattnerac161bf2009-01-02 07:01:27 +00003548 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3549 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003550 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003551 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003552
Chris Lattnerac161bf2009-01-02 07:01:27 +00003553 if (!SeenCases.insert(Constant))
3554 return Error(CondLoc, "duplicate case value in switch");
3555 if (!isa<ConstantInt>(Constant))
3556 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003557
Chris Lattner3ed871f2009-10-27 19:13:16 +00003558 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003559 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003560
Chris Lattnerac161bf2009-01-02 07:01:27 +00003561 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003562
Chris Lattner3ed871f2009-10-27 19:13:16 +00003563 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003564 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3565 SI->addCase(Table[i].first, Table[i].second);
3566 Inst = SI;
3567 return false;
3568}
3569
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003570/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003571/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003572/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3573bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003574 LocTy AddrLoc;
3575 Value *Address;
3576 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003577 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3578 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003579 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003580
Duncan Sands19d0b472010-02-16 11:11:14 +00003581 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003582 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003583
Chris Lattner3ed871f2009-10-27 19:13:16 +00003584 // Parse the destination list.
3585 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003586
Chris Lattner3ed871f2009-10-27 19:13:16 +00003587 if (Lex.getKind() != lltok::rsquare) {
3588 BasicBlock *DestBB;
3589 if (ParseTypeAndBasicBlock(DestBB, PFS))
3590 return true;
3591 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003592
Chris Lattner3ed871f2009-10-27 19:13:16 +00003593 while (EatIfPresent(lltok::comma)) {
3594 if (ParseTypeAndBasicBlock(DestBB, PFS))
3595 return true;
3596 DestList.push_back(DestBB);
3597 }
3598 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003599
Chris Lattner3ed871f2009-10-27 19:13:16 +00003600 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3601 return true;
3602
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003603 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003604 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3605 IBI->addDestination(DestList[i]);
3606 Inst = IBI;
3607 return false;
3608}
3609
3610
Chris Lattnerac161bf2009-01-02 07:01:27 +00003611/// ParseInvoke
3612/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3613/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3614bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3615 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003616 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003617 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003618 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003619 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003620 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003621 LocTy RetTypeLoc;
3622 ValID CalleeID;
3623 SmallVector<ParamInfo, 16> ArgList;
3624
Chris Lattner3ed871f2009-10-27 19:13:16 +00003625 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003626 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003627 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003628 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003629 ParseValID(CalleeID) ||
3630 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003631 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3632 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003633 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003634 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003635 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003636 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003637 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003638
Chris Lattnerac161bf2009-01-02 07:01:27 +00003639 // If RetType is a non-function pointer type, then this is the short syntax
3640 // for the call, which means that RetType is just the return type. Infer the
3641 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003642 PointerType *PFTy = nullptr;
3643 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003644 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3645 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3646 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003647 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003648 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3649 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003650
Chris Lattnerac161bf2009-01-02 07:01:27 +00003651 if (!FunctionType::isValidReturnType(RetType))
3652 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003653
Owen Anderson4056ca92009-07-29 22:17:13 +00003654 Ty = FunctionType::get(RetType, ParamTypes, false);
3655 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003656 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003657
Chris Lattnerac161bf2009-01-02 07:01:27 +00003658 // Look up the callee.
3659 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003660 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003661
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003662 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003663 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003664 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003665 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3666 AttributeSet::ReturnIndex,
3667 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003668
Chris Lattnerac161bf2009-01-02 07:01:27 +00003669 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003670
Chris Lattnerac161bf2009-01-02 07:01:27 +00003671 // Loop through FunctionType's arguments and ensure they are specified
3672 // correctly. Also, gather any parameter attributes.
3673 FunctionType::param_iterator I = Ty->param_begin();
3674 FunctionType::param_iterator E = Ty->param_end();
3675 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003676 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003677 if (I != E) {
3678 ExpectedTy = *I++;
3679 } else if (!Ty->isVarArg()) {
3680 return Error(ArgList[i].Loc, "too many arguments specified");
3681 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003682
Chris Lattnerac161bf2009-01-02 07:01:27 +00003683 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3684 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003685 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003686 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003687 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3688 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003689 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3690 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003691 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003692
Chris Lattnerac161bf2009-01-02 07:01:27 +00003693 if (I != E)
3694 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003695
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003696 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003697 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3698 AttributeSet::FunctionIndex,
3699 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003700
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003701 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003702 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003703
Jay Foad5bd375a2011-07-15 08:37:34 +00003704 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003705 II->setCallingConv(CC);
3706 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003707 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003708 Inst = II;
3709 return false;
3710}
3711
Bill Wendlingf891bf82011-07-31 06:30:59 +00003712/// ParseResume
3713/// ::= 'resume' TypeAndValue
3714bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3715 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003716 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3717 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003718
Bill Wendlingf891bf82011-07-31 06:30:59 +00003719 ResumeInst *RI = ResumeInst::Create(Exn);
3720 Inst = RI;
3721 return false;
3722}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003723
3724//===----------------------------------------------------------------------===//
3725// Binary Operators.
3726//===----------------------------------------------------------------------===//
3727
3728/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003729/// ::= ArithmeticOps TypeAndValue ',' Value
3730///
3731/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3732/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003733bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003734 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003735 LocTy Loc; Value *LHS, *RHS;
3736 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3737 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3738 ParseValue(LHS->getType(), RHS, PFS))
3739 return true;
3740
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003741 bool Valid;
3742 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003743 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003744 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003745 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3746 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003747 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003748 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3749 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003750 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003751
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003752 if (!Valid)
3753 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003754
Chris Lattnerac161bf2009-01-02 07:01:27 +00003755 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3756 return false;
3757}
3758
3759/// ParseLogical
3760/// ::= ArithmeticOps TypeAndValue ',' Value {
3761bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3762 unsigned Opc) {
3763 LocTy Loc; Value *LHS, *RHS;
3764 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3765 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3766 ParseValue(LHS->getType(), RHS, PFS))
3767 return true;
3768
Duncan Sands9dff9be2010-02-15 16:12:20 +00003769 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003770 return Error(Loc,"instruction requires integer or integer vector operands");
3771
3772 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3773 return false;
3774}
3775
3776
3777/// ParseCompare
3778/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3779/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003780bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3781 unsigned Opc) {
3782 // Parse the integer/fp comparison predicate.
3783 LocTy Loc;
3784 unsigned Pred;
3785 Value *LHS, *RHS;
3786 if (ParseCmpPredicate(Pred, Opc) ||
3787 ParseTypeAndValue(LHS, Loc, PFS) ||
3788 ParseToken(lltok::comma, "expected ',' after compare value") ||
3789 ParseValue(LHS->getType(), RHS, PFS))
3790 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003791
Chris Lattnerac161bf2009-01-02 07:01:27 +00003792 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003793 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003794 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003795 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003796 } else {
3797 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003798 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003799 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003800 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003801 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003802 }
3803 return false;
3804}
3805
3806//===----------------------------------------------------------------------===//
3807// Other Instructions.
3808//===----------------------------------------------------------------------===//
3809
3810
3811/// ParseCast
3812/// ::= CastOpc TypeAndValue 'to' Type
3813bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3814 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003815 LocTy Loc;
3816 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003817 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003818 if (ParseTypeAndValue(Op, Loc, PFS) ||
3819 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3820 ParseType(DestTy))
3821 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003822
Chris Lattner89d856e2009-03-01 00:53:13 +00003823 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3824 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003825 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003826 getTypeString(Op->getType()) + "' to '" +
3827 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003828 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003829 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3830 return false;
3831}
3832
3833/// ParseSelect
3834/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3835bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3836 LocTy Loc;
3837 Value *Op0, *Op1, *Op2;
3838 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3839 ParseToken(lltok::comma, "expected ',' after select condition") ||
3840 ParseTypeAndValue(Op1, PFS) ||
3841 ParseToken(lltok::comma, "expected ',' after select value") ||
3842 ParseTypeAndValue(Op2, PFS))
3843 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003844
Chris Lattnerac161bf2009-01-02 07:01:27 +00003845 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3846 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003847
Chris Lattnerac161bf2009-01-02 07:01:27 +00003848 Inst = SelectInst::Create(Op0, Op1, Op2);
3849 return false;
3850}
3851
Chris Lattnerb55ab542009-01-05 08:18:44 +00003852/// ParseVA_Arg
3853/// ::= 'va_arg' TypeAndValue ',' Type
3854bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003855 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003856 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003857 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003858 if (ParseTypeAndValue(Op, PFS) ||
3859 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003860 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003861 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003862
Chris Lattnerb55ab542009-01-05 08:18:44 +00003863 if (!EltTy->isFirstClassType())
3864 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003865
3866 Inst = new VAArgInst(Op, EltTy);
3867 return false;
3868}
3869
3870/// ParseExtractElement
3871/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3872bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3873 LocTy Loc;
3874 Value *Op0, *Op1;
3875 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3876 ParseToken(lltok::comma, "expected ',' after extract value") ||
3877 ParseTypeAndValue(Op1, PFS))
3878 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003879
Chris Lattnerac161bf2009-01-02 07:01:27 +00003880 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3881 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003882
Eric Christopherc9742252009-07-25 02:28:41 +00003883 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003884 return false;
3885}
3886
3887/// ParseInsertElement
3888/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3889bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3890 LocTy Loc;
3891 Value *Op0, *Op1, *Op2;
3892 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3893 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3894 ParseTypeAndValue(Op1, PFS) ||
3895 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3896 ParseTypeAndValue(Op2, PFS))
3897 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003898
Chris Lattnerac161bf2009-01-02 07:01:27 +00003899 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003900 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003901
Chris Lattnerac161bf2009-01-02 07:01:27 +00003902 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3903 return false;
3904}
3905
3906/// ParseShuffleVector
3907/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3908bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3909 LocTy Loc;
3910 Value *Op0, *Op1, *Op2;
3911 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3912 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3913 ParseTypeAndValue(Op1, PFS) ||
3914 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3915 ParseTypeAndValue(Op2, PFS))
3916 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003917
Chris Lattnerac161bf2009-01-02 07:01:27 +00003918 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003919 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003920
Chris Lattnerac161bf2009-01-02 07:01:27 +00003921 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3922 return false;
3923}
3924
3925/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003926/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003927int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003928 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003929 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003930
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003931 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003932 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3933 ParseValue(Ty, Op0, PFS) ||
3934 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003935 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003936 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3937 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003938
Chris Lattnerf4f03422009-12-30 05:27:33 +00003939 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003940 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3941 while (1) {
3942 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003943
Chris Lattner3822f632009-01-02 08:05:26 +00003944 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003945 break;
3946
Chris Lattnerf4f03422009-12-30 05:27:33 +00003947 if (Lex.getKind() == lltok::MetadataVar) {
3948 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003949 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003950 }
Devang Patel8f842d32009-10-16 18:45:49 +00003951
Chris Lattner3822f632009-01-02 08:05:26 +00003952 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003953 ParseValue(Ty, Op0, PFS) ||
3954 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003955 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003956 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3957 return true;
3958 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003959
Chris Lattnerac161bf2009-01-02 07:01:27 +00003960 if (!Ty->isFirstClassType())
3961 return Error(TypeLoc, "phi node must have first class type");
3962
Jay Foad52131342011-03-30 11:28:46 +00003963 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003964 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3965 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3966 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003967 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003968}
3969
Bill Wendlingfae14752011-08-12 20:24:12 +00003970/// ParseLandingPad
3971/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3972/// Clause
3973/// ::= 'catch' TypeAndValue
3974/// ::= 'filter'
3975/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3976bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003977 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003978 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003979
3980 if (ParseType(Ty, TyLoc) ||
3981 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3982 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3983 return true;
3984
3985 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3986 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3987
3988 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3989 LandingPadInst::ClauseType CT;
3990 if (EatIfPresent(lltok::kw_catch))
3991 CT = LandingPadInst::Catch;
3992 else if (EatIfPresent(lltok::kw_filter))
3993 CT = LandingPadInst::Filter;
3994 else
3995 return TokError("expected 'catch' or 'filter' clause type");
3996
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00003997 Value *V;
3998 LocTy VLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003999 if (ParseTypeAndValue(V, VLoc, PFS)) {
4000 delete LP;
4001 return true;
4002 }
4003
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00004004 // A 'catch' type expects a non-array constant. A filter clause expects an
4005 // array constant.
4006 if (CT == LandingPadInst::Catch) {
4007 if (isa<ArrayType>(V->getType()))
4008 Error(VLoc, "'catch' clause has an invalid type");
4009 } else {
4010 if (!isa<ArrayType>(V->getType()))
4011 Error(VLoc, "'filter' clause has an invalid type");
4012 }
4013
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004014 LP->addClause(cast<Constant>(V));
Bill Wendlingfae14752011-08-12 20:24:12 +00004015 }
4016
4017 Inst = LP;
4018 return false;
4019}
4020
Chris Lattnerac161bf2009-01-02 07:01:27 +00004021/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004022/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4023/// ParameterList OptionalAttrs
4024/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4025/// ParameterList OptionalAttrs
4026/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004027/// ParameterList OptionalAttrs
4028bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004029 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004030 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004031 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004032 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004033 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004034 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004035 LocTy RetTypeLoc;
4036 ValID CalleeID;
4037 SmallVector<ParamInfo, 16> ArgList;
4038 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004039
Reid Kleckner5772b772014-04-24 20:14:34 +00004040 if ((TCK != CallInst::TCK_None &&
4041 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004042 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004043 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004044 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004045 ParseValID(CalleeID) ||
4046 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004047 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004048 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004049 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004050
Chris Lattnerac161bf2009-01-02 07:01:27 +00004051 // If RetType is a non-function pointer type, then this is the short syntax
4052 // for the call, which means that RetType is just the return type. Infer the
4053 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004054 PointerType *PFTy = nullptr;
4055 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004056 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4057 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4058 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004059 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004060 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4061 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004062
Chris Lattnerac161bf2009-01-02 07:01:27 +00004063 if (!FunctionType::isValidReturnType(RetType))
4064 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004065
Owen Anderson4056ca92009-07-29 22:17:13 +00004066 Ty = FunctionType::get(RetType, ParamTypes, false);
4067 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004068 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004069
Chris Lattnerac161bf2009-01-02 07:01:27 +00004070 // Look up the callee.
4071 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004072 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004073
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004074 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004075 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004076 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004077 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4078 AttributeSet::ReturnIndex,
4079 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004080
Chris Lattnerac161bf2009-01-02 07:01:27 +00004081 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004082
Chris Lattnerac161bf2009-01-02 07:01:27 +00004083 // Loop through FunctionType's arguments and ensure they are specified
4084 // correctly. Also, gather any parameter attributes.
4085 FunctionType::param_iterator I = Ty->param_begin();
4086 FunctionType::param_iterator E = Ty->param_end();
4087 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004088 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004089 if (I != E) {
4090 ExpectedTy = *I++;
4091 } else if (!Ty->isVarArg()) {
4092 return Error(ArgList[i].Loc, "too many arguments specified");
4093 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004094
Chris Lattnerac161bf2009-01-02 07:01:27 +00004095 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4096 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004097 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004098 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004099 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4100 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004101 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4102 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004103 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004104
Chris Lattnerac161bf2009-01-02 07:01:27 +00004105 if (I != E)
4106 return Error(CallLoc, "not enough parameters specified for call");
4107
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004108 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004109 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4110 AttributeSet::FunctionIndex,
4111 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004112
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004113 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004114 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004115
Jay Foad5bd375a2011-07-15 08:37:34 +00004116 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004117 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004118 CI->setCallingConv(CC);
4119 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004120 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004121 Inst = CI;
4122 return false;
4123}
4124
4125//===----------------------------------------------------------------------===//
4126// Memory Instructions.
4127//===----------------------------------------------------------------------===//
4128
4129/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004130/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004131int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004132 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004133 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004134 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004135 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004136
4137 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4138
Chris Lattner3822f632009-01-02 08:05:26 +00004139 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004140
Chris Lattnerb2f39502009-12-30 05:44:30 +00004141 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004142 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004143 if (Lex.getKind() == lltok::kw_align) {
4144 if (ParseOptionalAlignment(Alignment)) return true;
4145 } else if (Lex.getKind() == lltok::MetadataVar) {
4146 AteExtraComma = true;
4147 } else {
4148 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4149 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4150 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004151 }
4152 }
4153
Dan Gohman2140a742010-05-28 01:14:11 +00004154 if (Size && !Size->getType()->isIntegerTy())
4155 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004156
Reid Kleckner436c42e2014-01-17 23:58:17 +00004157 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4158 AI->setUsedWithInAlloca(IsInAlloca);
4159 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004160 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004161}
4162
4163/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004164/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004165/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004166/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004167int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004168 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004169 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004170 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004171 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004172 AtomicOrdering Ordering = NotAtomic;
4173 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004174
4175 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004176 isAtomic = true;
4177 Lex.Lex();
4178 }
4179
Chris Lattnerbc639292011-11-27 06:56:53 +00004180 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004181 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004182 isVolatile = true;
4183 Lex.Lex();
4184 }
4185
Chris Lattnerb2f39502009-12-30 05:44:30 +00004186 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004187 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004188 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4189 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004190
Duncan Sands19d0b472010-02-16 11:11:14 +00004191 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004192 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4193 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004194 if (isAtomic && !Alignment)
4195 return Error(Loc, "atomic load must have explicit non-zero alignment");
4196 if (Ordering == Release || Ordering == AcquireRelease)
4197 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004198
Eli Friedman59b66882011-08-09 23:02:53 +00004199 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004200 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004201}
4202
4203/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004204
4205/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4206/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004207/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004208int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004209 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004210 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004211 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004212 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004213 AtomicOrdering Ordering = NotAtomic;
4214 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004215
4216 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004217 isAtomic = true;
4218 Lex.Lex();
4219 }
4220
Chris Lattnerbc639292011-11-27 06:56:53 +00004221 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004222 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004223 isVolatile = true;
4224 Lex.Lex();
4225 }
4226
Chris Lattnerac161bf2009-01-02 07:01:27 +00004227 if (ParseTypeAndValue(Val, Loc, PFS) ||
4228 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004229 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004230 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004231 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004232 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004233
Duncan Sands19d0b472010-02-16 11:11:14 +00004234 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004235 return Error(PtrLoc, "store operand must be a pointer");
4236 if (!Val->getType()->isFirstClassType())
4237 return Error(Loc, "store operand must be a first class value");
4238 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4239 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004240 if (isAtomic && !Alignment)
4241 return Error(Loc, "atomic store must have explicit non-zero alignment");
4242 if (Ordering == Acquire || Ordering == AcquireRelease)
4243 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004244
Eli Friedman59b66882011-08-09 23:02:53 +00004245 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004246 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004247}
4248
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004249/// ParseCmpXchg
Eli Friedman02e737b2011-08-12 22:50:01 +00004250/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
Tim Northovere94a5182014-03-11 10:48:52 +00004251/// 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004252int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004253 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4254 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004255 AtomicOrdering SuccessOrdering = NotAtomic;
4256 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004257 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004258 bool isVolatile = false;
4259
4260 if (EatIfPresent(lltok::kw_volatile))
4261 isVolatile = true;
4262
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004263 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4264 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4265 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4266 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4267 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004268 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4269 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004270 return true;
4271
Tim Northovere94a5182014-03-11 10:48:52 +00004272 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004273 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004274 if (SuccessOrdering < FailureOrdering)
4275 return TokError("cmpxchg must be at least as ordered on success as failure");
4276 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4277 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004278 if (!Ptr->getType()->isPointerTy())
4279 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4280 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4281 return Error(CmpLoc, "compare value and pointer type do not match");
4282 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4283 return Error(NewLoc, "new value and pointer type do not match");
4284 if (!New->getType()->isIntegerTy())
4285 return Error(NewLoc, "cmpxchg operand must be an integer");
4286 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4287 if (Size < 8 || (Size & (Size - 1)))
4288 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4289 " integer");
4290
Tim Northovere94a5182014-03-11 10:48:52 +00004291 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
4292 FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004293 CXI->setVolatile(isVolatile);
4294 Inst = CXI;
4295 return AteExtraComma ? InstExtraComma : InstNormal;
4296}
4297
4298/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004299/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4300/// 'singlethread'? AtomicOrdering
4301int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004302 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4303 bool AteExtraComma = false;
4304 AtomicOrdering Ordering = NotAtomic;
4305 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004306 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004307 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004308
4309 if (EatIfPresent(lltok::kw_volatile))
4310 isVolatile = true;
4311
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004312 switch (Lex.getKind()) {
4313 default: return TokError("expected binary operation in atomicrmw");
4314 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4315 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4316 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4317 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4318 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4319 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4320 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4321 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4322 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4323 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4324 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4325 }
4326 Lex.Lex(); // Eat the operation.
4327
4328 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4329 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4330 ParseTypeAndValue(Val, ValLoc, PFS) ||
4331 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4332 return true;
4333
4334 if (Ordering == Unordered)
4335 return TokError("atomicrmw cannot be unordered");
4336 if (!Ptr->getType()->isPointerTy())
4337 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4338 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4339 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4340 if (!Val->getType()->isIntegerTy())
4341 return Error(ValLoc, "atomicrmw operand must be an integer");
4342 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4343 if (Size < 8 || (Size & (Size - 1)))
4344 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4345 " integer");
4346
4347 AtomicRMWInst *RMWI =
4348 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4349 RMWI->setVolatile(isVolatile);
4350 Inst = RMWI;
4351 return AteExtraComma ? InstExtraComma : InstNormal;
4352}
4353
Eli Friedmanfee02c62011-07-25 23:16:38 +00004354/// ParseFence
4355/// ::= 'fence' 'singlethread'? AtomicOrdering
4356int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4357 AtomicOrdering Ordering = NotAtomic;
4358 SynchronizationScope Scope = CrossThread;
4359 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4360 return true;
4361
4362 if (Ordering == Unordered)
4363 return TokError("fence cannot be unordered");
4364 if (Ordering == Monotonic)
4365 return TokError("fence cannot be monotonic");
4366
4367 Inst = new FenceInst(Context, Ordering, Scope);
4368 return InstNormal;
4369}
4370
Chris Lattnerac161bf2009-01-02 07:01:27 +00004371/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004372/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004373int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004374 Value *Ptr = nullptr;
4375 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004376 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004377
Dan Gohman16cbbe42009-07-29 15:58:36 +00004378 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004379
Chris Lattner3822f632009-01-02 08:05:26 +00004380 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004381
Eli Benderskyd9806682013-04-22 17:03:42 +00004382 Type *BaseType = Ptr->getType();
4383 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4384 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004385 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004386
Chris Lattnerac161bf2009-01-02 07:01:27 +00004387 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004388 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004389 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004390 if (Lex.getKind() == lltok::MetadataVar) {
4391 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004392 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004393 }
Chris Lattner3822f632009-01-02 08:05:26 +00004394 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004395 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004396 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004397 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4398 return Error(EltLoc, "getelementptr index type missmatch");
4399 if (Val->getType()->isVectorTy()) {
4400 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4401 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4402 if (ValNumEl != PtrNumEl)
4403 return Error(EltLoc,
4404 "getelementptr vector index has a wrong number of elements");
4405 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004406 Indices.push_back(Val);
4407 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004408
Eli Benderskyd9806682013-04-22 17:03:42 +00004409 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4410 return Error(Loc, "base element of getelementptr must be sized");
4411
4412 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004413 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004414 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004415 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004416 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004417 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004418}
4419
4420/// ParseExtractValue
4421/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004422int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004423 Value *Val; LocTy Loc;
4424 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004425 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004426 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004427 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004428 return true;
4429
Chris Lattner392be582010-02-12 20:49:41 +00004430 if (!Val->getType()->isAggregateType())
4431 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004432
Jay Foad57aa6362011-07-13 10:26:04 +00004433 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004434 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004435 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004436 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004437}
4438
4439/// ParseInsertValue
4440/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004441int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004442 Value *Val0, *Val1; LocTy Loc0, Loc1;
4443 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004444 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004445 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4446 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4447 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004448 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004449 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004450
Chris Lattner392be582010-02-12 20:49:41 +00004451 if (!Val0->getType()->isAggregateType())
4452 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004453
Jay Foad57aa6362011-07-13 10:26:04 +00004454 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004455 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004456 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004457 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004458}
Nick Lewycky49f89192009-04-04 07:22:01 +00004459
4460//===----------------------------------------------------------------------===//
4461// Embedded metadata.
4462//===----------------------------------------------------------------------===//
4463
4464/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004465/// ::= Element (',' Element)*
4466/// Element
4467/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004468bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004469 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004470 // Check for an empty list.
4471 if (Lex.getKind() == lltok::rbrace)
4472 return false;
4473
Nick Lewycky49f89192009-04-04 07:22:01 +00004474 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004475 // Null is a special case since it is typeless.
4476 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004477 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004478 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004479 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004480
Craig Topper2617dcc2014-04-15 06:32:26 +00004481 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004482 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004483 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004484 } while (EatIfPresent(lltok::comma));
4485
4486 return false;
4487}