blob: be55ac6481f134e97a0aa8378b37aa2db4e0adba [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) {
Alp Tokere69170a2014-06-26 22:52:05 +000031 std::string Result;
32 raw_string_ostream Tmp(Result);
33 Tmp << *T;
34 return Tmp.str();
Chris Lattner0f214eb2011-06-18 21:18:23 +000035}
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
David Majnemerdad0a642014-06-27 18:19:56 +0000166 if (!ForwardRefComdats.empty())
167 return Error(ForwardRefComdats.begin()->second,
168 "use of undefined comdat '$" +
169 ForwardRefComdats.begin()->first + "'");
170
Chris Lattnerac161bf2009-01-02 07:01:27 +0000171 if (!ForwardRefVals.empty())
172 return Error(ForwardRefVals.begin()->second.second,
173 "use of undefined value '@" + ForwardRefVals.begin()->first +
174 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000175
Chris Lattnerac161bf2009-01-02 07:01:27 +0000176 if (!ForwardRefValIDs.empty())
177 return Error(ForwardRefValIDs.begin()->second.second,
178 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000179 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000180
Devang Pateld2541152009-07-08 19:23:54 +0000181 if (!ForwardRefMDNodes.empty())
182 return Error(ForwardRefMDNodes.begin()->second.second,
183 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000184 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000185
Devang Pateld2541152009-07-08 19:23:54 +0000186
Chris Lattnerac161bf2009-01-02 07:01:27 +0000187 // Look for intrinsic functions and CallInst that need to be upgraded
188 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
189 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000190
Manman Ren8b4306c2013-12-02 21:29:56 +0000191 UpgradeDebugInfo(*M);
192
Chris Lattnerac161bf2009-01-02 07:01:27 +0000193 return false;
194}
195
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000196bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000197 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
198 PerFunctionState *PFS) {
199 // Loop over all the references, resolving them.
200 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
201 BasicBlock *Res;
Chris Lattneraa99c942009-11-01 01:27:45 +0000202 if (PFS) {
Chris Lattner3432c622009-10-28 03:39:23 +0000203 if (Refs[i].first.Kind == ValID::t_LocalName)
204 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattneraa99c942009-11-01 01:27:45 +0000205 else
Chris Lattner3432c622009-10-28 03:39:23 +0000206 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
207 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
208 return Error(Refs[i].first.Loc,
Chris Lattnera38a4df2009-11-02 18:28:45 +0000209 "cannot take address of numeric label after the function is defined");
Chris Lattner3432c622009-10-28 03:39:23 +0000210 } else {
211 Res = dyn_cast_or_null<BasicBlock>(
212 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
213 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000214
Craig Topper2617dcc2014-04-15 06:32:26 +0000215 if (!Res)
Chris Lattner3432c622009-10-28 03:39:23 +0000216 return Error(Refs[i].first.Loc,
217 "referenced value is not a basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000218
Chris Lattner3432c622009-10-28 03:39:23 +0000219 // Get the BlockAddress for this and update references to use it.
220 BlockAddress *BA = BlockAddress::get(TheFn, Res);
221 Refs[i].second->replaceAllUsesWith(BA);
222 Refs[i].second->eraseFromParent();
223 }
224 return false;
225}
226
227
Chris Lattnerac161bf2009-01-02 07:01:27 +0000228//===----------------------------------------------------------------------===//
229// Top-Level Entities
230//===----------------------------------------------------------------------===//
231
232bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000233 while (1) {
234 switch (Lex.getKind()) {
235 default: return TokError("expected top-level entity");
236 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000237 case lltok::kw_declare: if (ParseDeclare()) return true; break;
238 case lltok::kw_define: if (ParseDefine()) return true; break;
239 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
240 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000241 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000242 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000243 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000244 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000245 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
David Majnemerdad0a642014-06-27 18:19:56 +0000246 case lltok::ComdatVar: if (parseComdat()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000247 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000248 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000249
250 // The Global variable production with no name can have many different
251 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000252 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
253 // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000254 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000255 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000256 case lltok::kw_internal: // OptionalLinkage
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +0000257 case lltok::kw_linker_private: // Obsolete OptionalLinkage
258 case lltok::kw_linker_private_weak: // Obsolete OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000259 case lltok::kw_weak: // OptionalLinkage
260 case lltok::kw_weak_odr: // OptionalLinkage
261 case lltok::kw_linkonce: // OptionalLinkage
262 case lltok::kw_linkonce_odr: // OptionalLinkage
263 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000264 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000265 case lltok::kw_extern_weak: // OptionalLinkage
Rafael Espindola52b74422014-06-03 20:00:20 +0000266 case lltok::kw_external: // OptionalLinkage
267 case lltok::kw_default: // OptionalVisibility
268 case lltok::kw_hidden: // OptionalVisibility
269 case lltok::kw_protected: // OptionalVisibility
Rafael Espindola63e92fb2014-06-03 20:07:32 +0000270 case lltok::kw_dllimport: // OptionalDLLStorageClass
271 case lltok::kw_dllexport: // OptionalDLLStorageClass
Rafael Espindola52b74422014-06-03 20:00:20 +0000272 case lltok::kw_thread_local: // OptionalThreadLocal
273 case lltok::kw_addrspace: // OptionalAddrSpace
274 case lltok::kw_constant: // GlobalType
275 case lltok::kw_global: { // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000276 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000277 bool UnnamedAddr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000278 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola52b74422014-06-03 20:00:20 +0000279 bool HasLinkage;
280 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000281 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000282 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000283 ParseOptionalThreadLocal(TLM) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000284 parseOptionalUnnamedAddr(UnnamedAddr) ||
Rafael Espindola52b74422014-06-03 20:00:20 +0000285 ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000286 DLLStorageClass, TLM, UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000287 return true;
288 break;
289 }
Bill Wendlinga7c38772013-02-09 15:48:49 +0000290
291 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000292 }
293 }
294}
295
296
297/// toplevelentity
298/// ::= 'module' 'asm' STRINGCONSTANT
299bool LLParser::ParseModuleAsm() {
300 assert(Lex.getKind() == lltok::kw_module);
301 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000302
303 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000304 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
305 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000306
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000307 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000308 return false;
309}
310
311/// toplevelentity
312/// ::= 'target' 'triple' '=' STRINGCONSTANT
313/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
314bool LLParser::ParseTargetDefinition() {
315 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000316 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317 switch (Lex.Lex()) {
318 default: return TokError("unknown target property");
319 case lltok::kw_triple:
320 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000321 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
322 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000323 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000324 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000325 return false;
326 case lltok::kw_datalayout:
327 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000328 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
329 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000330 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000331 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000332 return false;
333 }
334}
335
Bill Wendling706d3d62012-11-28 08:41:48 +0000336/// toplevelentity
337/// ::= 'deplibs' '=' '[' ']'
338/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
339/// FIXME: Remove in 4.0. Currently parse, but ignore.
340bool LLParser::ParseDepLibs() {
341 assert(Lex.getKind() == lltok::kw_deplibs);
342 Lex.Lex();
343 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
344 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
345 return true;
346
347 if (EatIfPresent(lltok::rsquare))
348 return false;
349
350 do {
351 std::string Str;
352 if (ParseStringConstant(Str)) return true;
353 } while (EatIfPresent(lltok::comma));
354
355 return ParseToken(lltok::rsquare, "expected ']' at end of list");
356}
357
Dan Gohman466876b2009-08-12 23:32:33 +0000358/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000359/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000360bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000361 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000362 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000363 Lex.Lex(); // eat LocalVarID;
364
365 if (ParseToken(lltok::equal, "expected '=' after name") ||
366 ParseToken(lltok::kw_type, "expected 'type' after '='"))
367 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000368
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000369 if (TypeID >= NumberedTypes.size())
370 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000371
Craig Topper2617dcc2014-04-15 06:32:26 +0000372 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000373 if (ParseStructDefinition(TypeLoc, "",
374 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000375
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000376 if (!isa<StructType>(Result)) {
377 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
378 if (Entry.first)
379 return Error(TypeLoc, "non-struct types may not be recursive");
380 Entry.first = Result;
381 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000382 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000383
Chris Lattnerac161bf2009-01-02 07:01:27 +0000384 return false;
385}
386
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000387
Chris Lattnerac161bf2009-01-02 07:01:27 +0000388/// toplevelentity
389/// ::= LocalVar '=' 'type' type
390bool LLParser::ParseNamedType() {
391 std::string Name = Lex.getStrVal();
392 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000393 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000394
Chris Lattner3822f632009-01-02 08:05:26 +0000395 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000396 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000397 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000398
Craig Topper2617dcc2014-04-15 06:32:26 +0000399 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000400 if (ParseStructDefinition(NameLoc, Name,
401 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000402
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000403 if (!isa<StructType>(Result)) {
404 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
405 if (Entry.first)
406 return Error(NameLoc, "non-struct types may not be recursive");
407 Entry.first = Result;
408 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000409 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000410
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000411 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000412}
413
414
415/// toplevelentity
416/// ::= 'declare' FunctionHeader
417bool LLParser::ParseDeclare() {
418 assert(Lex.getKind() == lltok::kw_declare);
419 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000420
Chris Lattnerac161bf2009-01-02 07:01:27 +0000421 Function *F;
422 return ParseFunctionHeader(F, false);
423}
424
425/// toplevelentity
426/// ::= 'define' FunctionHeader '{' ...
427bool LLParser::ParseDefine() {
428 assert(Lex.getKind() == lltok::kw_define);
429 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000430
Chris Lattnerac161bf2009-01-02 07:01:27 +0000431 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000432 return ParseFunctionHeader(F, true) ||
433 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000434}
435
Chris Lattner3822f632009-01-02 08:05:26 +0000436/// ParseGlobalType
437/// ::= 'constant'
438/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000439bool LLParser::ParseGlobalType(bool &IsConstant) {
440 if (Lex.getKind() == lltok::kw_constant)
441 IsConstant = true;
442 else if (Lex.getKind() == lltok::kw_global)
443 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000444 else {
445 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000446 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000447 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000448 Lex.Lex();
449 return false;
450}
451
Dan Gohman466876b2009-08-12 23:32:33 +0000452/// ParseUnnamedGlobal:
453/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000454/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
455/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000456/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000457/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
458/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000459bool LLParser::ParseUnnamedGlobal() {
460 unsigned VarID = NumberedVals.size();
461 std::string Name;
462 LocTy NameLoc = Lex.getLoc();
463
464 // Handle the GlobalID form.
465 if (Lex.getKind() == lltok::GlobalID) {
466 if (Lex.getUIntVal() != VarID)
467 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000468 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000469 Lex.Lex(); // eat GlobalID;
470
471 if (ParseToken(lltok::equal, "expected '=' after name"))
472 return true;
473 }
474
475 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000476 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000477 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000478 bool UnnamedAddr;
Dan Gohman466876b2009-08-12 23:32:33 +0000479 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000480 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000481 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000482 ParseOptionalThreadLocal(TLM) ||
483 parseOptionalUnnamedAddr(UnnamedAddr))
Dan Gohman466876b2009-08-12 23:32:33 +0000484 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000485
Dan Gohman466876b2009-08-12 23:32:33 +0000486 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000487 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000488 DLLStorageClass, TLM, UnnamedAddr);
489 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM,
490 UnnamedAddr);
Dan Gohman466876b2009-08-12 23:32:33 +0000491}
492
Chris Lattnerac161bf2009-01-02 07:01:27 +0000493/// ParseNamedGlobal:
494/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000495/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
496/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000497bool LLParser::ParseNamedGlobal() {
498 assert(Lex.getKind() == lltok::GlobalVar);
499 LocTy NameLoc = Lex.getLoc();
500 std::string Name = Lex.getStrVal();
501 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000502
Chris Lattnerac161bf2009-01-02 07:01:27 +0000503 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000504 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000505 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000506 bool UnnamedAddr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000507 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
508 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000509 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000510 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000511 ParseOptionalThreadLocal(TLM) ||
512 parseOptionalUnnamedAddr(UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000513 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000514
Chris Lattnerac161bf2009-01-02 07:01:27 +0000515 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000516 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000517 DLLStorageClass, TLM, UnnamedAddr);
518 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM,
519 UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000520}
521
David Majnemerdad0a642014-06-27 18:19:56 +0000522bool LLParser::parseComdat() {
523 assert(Lex.getKind() == lltok::ComdatVar);
524 std::string Name = Lex.getStrVal();
525 LocTy NameLoc = Lex.getLoc();
526 Lex.Lex();
527
528 if (ParseToken(lltok::equal, "expected '=' here"))
529 return true;
530
531 if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
532 return TokError("expected comdat type");
533
534 Comdat::SelectionKind SK;
535 switch (Lex.getKind()) {
536 default:
537 return TokError("unknown selection kind");
538 case lltok::kw_any:
539 SK = Comdat::Any;
540 break;
541 case lltok::kw_exactmatch:
542 SK = Comdat::ExactMatch;
543 break;
544 case lltok::kw_largest:
545 SK = Comdat::Largest;
546 break;
547 case lltok::kw_noduplicates:
548 SK = Comdat::NoDuplicates;
549 break;
550 case lltok::kw_samesize:
551 SK = Comdat::SameSize;
552 break;
553 }
554 Lex.Lex();
555
556 // See if the comdat was forward referenced, if so, use the comdat.
557 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
558 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
559 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
560 return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
561
562 Comdat *C;
563 if (I != ComdatSymTab.end())
564 C = &I->second;
565 else
566 C = M->getOrInsertComdat(Name);
567 C->setSelectionKind(SK);
568
569 return false;
570}
571
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000572// MDString:
573// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000574bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000575 std::string Str;
576 if (ParseStringConstant(Str)) return true;
Eli Bendersky5d5e18d2014-06-25 15:41:00 +0000577 llvm::UpgradeMDStringConstant(Str);
Chris Lattner1797fc72009-12-29 21:53:55 +0000578 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000579 return false;
580}
581
582// MDNode:
583// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000584//
585/// This version of ParseMDNodeID returns the slot number and null in the case
586/// of a forward reference.
587bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
588 // !{ ..., !42, ... }
589 if (ParseUInt32(SlotNo)) return true;
590
591 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000592 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000593 Result = NumberedMetadata[SlotNo];
594 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000595 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000596 return false;
597}
598
Chris Lattner6dac02a2009-12-30 04:15:23 +0000599bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000600 // !{ ..., !42, ... }
601 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000602 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000603
Chris Lattner8eff0152010-04-01 05:14:45 +0000604 // If not a forward reference, just return it now.
605 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000606
Chris Lattner8eff0152010-04-01 05:14:45 +0000607 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000608 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000609 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000610
Chris Lattnerfc58af22009-12-30 04:51:58 +0000611 if (NumberedMetadata.size() <= MID)
612 NumberedMetadata.resize(MID+1);
613 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000614 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000615 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000616}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000617
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000618/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000619/// !foo = !{ !1, !2 }
620bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000621 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000622 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000623 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000624
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000625 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000626 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000627 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000628 return true;
629
Dan Gohman2637cc12010-07-21 23:38:33 +0000630 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000631 if (Lex.getKind() != lltok::rbrace)
632 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000633 if (ParseToken(lltok::exclaim, "Expected '!' here"))
634 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000635
Craig Topper2617dcc2014-04-15 06:32:26 +0000636 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000637 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000638 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000639 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000640
641 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
642 return true;
643
Devang Patelbe626972009-07-29 00:34:02 +0000644 return false;
645}
646
Devang Patel39e64d42009-07-01 19:21:12 +0000647/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000648/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000649bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000650 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000651 Lex.Lex();
652 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000653
654 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000655 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000656 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000657 if (ParseUInt32(MetadataID) ||
658 ParseToken(lltok::equal, "expected '=' here") ||
659 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000660 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000661 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000662 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000663 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000664 return true;
665
Jay Foad5514afe2011-04-21 19:59:31 +0000666 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000667
Chris Lattnerfc58af22009-12-30 04:51:58 +0000668 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000669 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000670 FI = ForwardRefMDNodes.find(MetadataID);
671 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000672 MDNode *Temp = FI->second.first;
673 Temp->replaceAllUsesWith(Init);
674 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000675 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000676
Chris Lattnerfc58af22009-12-30 04:51:58 +0000677 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
678 } else {
679 if (MetadataID >= NumberedMetadata.size())
680 NumberedMetadata.resize(MetadataID+1);
681
Craig Topper2617dcc2014-04-15 06:32:26 +0000682 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000683 return TokError("Metadata id is already used");
684 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000685 }
686
Devang Patel39e64d42009-07-01 19:21:12 +0000687 return false;
688}
689
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000690static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
691 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
692 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
693}
694
Chris Lattnerac161bf2009-01-02 07:01:27 +0000695/// ParseAlias:
Rafael Espindola5d92ffb2014-06-03 20:25:26 +0000696/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000697/// OptionalThreadLocal OptionalUnNammedAddr 'alias'
698/// OptionalLinkage Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000699///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000700/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000701/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000702///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000703/// Everything through OptionalUnNammedAddr has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000704///
705bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000706 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000707 GlobalVariable::ThreadLocalMode TLM,
708 bool UnnamedAddr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000709 assert(Lex.getKind() == lltok::kw_alias);
710 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000711 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000712 unsigned L;
713 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000714 return true;
715
Rafael Espindola78527052013-10-06 15:10:43 +0000716 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
717
Rafael Espindolacaa43562013-10-09 16:07:32 +0000718 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000719 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000720
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000721 if (!isValidVisibilityForLinkage(Visibility, L))
722 return Error(LinkageLoc,
723 "symbol with local linkage must have default visibility");
724
Rafael Espindola64c1e182014-06-03 02:41:57 +0000725 Constant *Aliasee;
726 LocTy AliaseeLoc = Lex.getLoc();
727 if (Lex.getKind() != lltok::kw_bitcast &&
728 Lex.getKind() != lltok::kw_getelementptr &&
729 Lex.getKind() != lltok::kw_addrspacecast &&
730 Lex.getKind() != lltok::kw_inttoptr) {
731 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000732 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000733 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000734 // The bitcast dest type is not present, it is implied by the dest type.
735 ValID ID;
736 if (ParseValID(ID))
737 return true;
738 if (ID.Kind != ValID::t_Constant)
739 return Error(AliaseeLoc, "invalid aliasee");
740 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000741 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000742
Rafael Espindola64c1e182014-06-03 02:41:57 +0000743 Type *AliaseeType = Aliasee->getType();
744 auto *PTy = dyn_cast<PointerType>(AliaseeType);
745 if (!PTy)
746 return Error(AliaseeLoc, "An alias must have pointer type");
747 Type *Ty = PTy->getElementType();
748 unsigned AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000749
750 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000751 std::unique_ptr<GlobalAlias> GA(
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000752 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
753 Name, Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000754 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000755 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000756 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000757 GA->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000758
Chris Lattnerac161bf2009-01-02 07:01:27 +0000759 // See if this value already exists in the symbol table. If so, it is either
760 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000761 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000762 // See if this was a redefinition. If so, there is no entry in
763 // ForwardRefVals.
764 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
765 I = ForwardRefVals.find(Name);
766 if (I == ForwardRefVals.end())
767 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
768
769 // Otherwise, this was a definition of forward ref. Verify that types
770 // agree.
771 if (Val->getType() != GA->getType())
772 return Error(NameLoc,
773 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000774
Chris Lattnerac161bf2009-01-02 07:01:27 +0000775 // If they agree, just RAUW the old value with the alias and remove the
776 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000777 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000778 Val->eraseFromParent();
779 ForwardRefVals.erase(I);
780 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000781
Chris Lattnerac161bf2009-01-02 07:01:27 +0000782 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000783 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000784 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000785
Rafael Espindolaaa273822014-05-09 21:49:17 +0000786 // The module owns this now
787 GA.release();
788
Chris Lattnerac161bf2009-01-02 07:01:27 +0000789 return false;
790}
791
792/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000793/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000794/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000795/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000796/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000797/// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000798/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000799///
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000800/// Everything up to and including OptionalUnNammedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000801/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000802///
803bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
804 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000805 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000806 GlobalVariable::ThreadLocalMode TLM,
807 bool UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000808 if (!isValidVisibilityForLinkage(Visibility, Linkage))
809 return Error(NameLoc,
810 "symbol with local linkage must have default visibility");
811
Chris Lattnerac161bf2009-01-02 07:01:27 +0000812 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000813 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000814 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000815 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000816
Craig Topper2617dcc2014-04-15 06:32:26 +0000817 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000818 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000819 ParseOptionalToken(lltok::kw_externally_initialized,
820 IsExternallyInitialized,
821 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000822 ParseGlobalType(IsConstant) ||
823 ParseType(Ty, TyLoc))
824 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000825
Chris Lattnerac161bf2009-01-02 07:01:27 +0000826 // If the linkage is specified and is external, then no initializer is
827 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000828 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000829 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000830 Linkage != GlobalValue::ExternalLinkage)) {
831 if (ParseGlobalValue(Ty, Init))
832 return true;
833 }
834
Duncan Sands19d0b472010-02-16 11:11:14 +0000835 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000836 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000837
Craig Topper2617dcc2014-04-15 06:32:26 +0000838 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000839
840 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000841 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000842 if (GlobalValue *GVal = M->getNamedValue(Name)) {
843 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
844 return Error(NameLoc, "redefinition of global '@" + Name + "'");
845 GV = cast<GlobalVariable>(GVal);
846 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000847 } else {
848 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
849 I = ForwardRefValIDs.find(NumberedVals.size());
850 if (I != ForwardRefValIDs.end()) {
851 GV = cast<GlobalVariable>(I->second.first);
852 ForwardRefValIDs.erase(I);
853 }
854 }
855
Craig Topper2617dcc2014-04-15 06:32:26 +0000856 if (!GV) {
857 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
858 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000859 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000860 } else {
861 if (GV->getType()->getElementType() != Ty)
862 return Error(TyLoc,
863 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000864
Chris Lattnerac161bf2009-01-02 07:01:27 +0000865 // Move the forward-reference to the correct spot in the module.
866 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
867 }
868
869 if (Name.empty())
870 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000871
Chris Lattnerac161bf2009-01-02 07:01:27 +0000872 // Set the parsed properties on the global.
873 if (Init)
874 GV->setInitializer(Init);
875 GV->setConstant(IsConstant);
876 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
877 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000878 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000879 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000880 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000881 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000882
Chris Lattnerac161bf2009-01-02 07:01:27 +0000883 // Parse attributes on the global.
884 while (Lex.getKind() == lltok::comma) {
885 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000886
Chris Lattnerac161bf2009-01-02 07:01:27 +0000887 if (Lex.getKind() == lltok::kw_section) {
888 Lex.Lex();
889 GV->setSection(Lex.getStrVal());
890 if (ParseToken(lltok::StringConstant, "expected global section string"))
891 return true;
892 } else if (Lex.getKind() == lltok::kw_align) {
893 unsigned Alignment;
894 if (ParseOptionalAlignment(Alignment)) return true;
895 GV->setAlignment(Alignment);
896 } else {
David Majnemerdad0a642014-06-27 18:19:56 +0000897 Comdat *C;
898 if (parseOptionalComdat(C))
899 return true;
900 if (C)
901 GV->setComdat(C);
902 else
903 return TokError("unknown global variable property!");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000904 }
905 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000906
Chris Lattnerac161bf2009-01-02 07:01:27 +0000907 return false;
908}
909
Bill Wendling63b88192013-02-06 06:52:58 +0000910/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000911/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000912bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000913 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000914 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000915 Lex.Lex();
916
917 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000918 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000919 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000920 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000921 Lex.Lex();
922
923 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000924 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000925 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000926 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000927 ParseToken(lltok::rbrace, "expected end of attribute group"))
928 return true;
929
Bill Wendlingb32b0412013-02-08 06:32:06 +0000930 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000931 return Error(AttrGrpLoc, "attribute group has no attributes");
932
933 return false;
934}
935
Bill Wendling8b0321d2013-02-08 00:52:31 +0000936/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000937/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000938bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
939 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000940 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000941 bool HaveError = false;
942
943 B.clear();
944
Bill Wendling63b88192013-02-06 06:52:58 +0000945 while (true) {
946 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000947 if (Token == lltok::kw_builtin)
948 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000949 switch (Token) {
950 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000951 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000952 return Error(Lex.getLoc(), "unterminated attribute group");
953 case lltok::rbrace:
954 // Finished.
955 return false;
956
Bill Wendlingb32b0412013-02-08 06:32:06 +0000957 case lltok::AttrGrpID: {
958 // Allow a function to reference an attribute group:
959 //
960 // define void @foo() #1 { ... }
961 if (inAttrGrp)
962 HaveError |=
963 Error(Lex.getLoc(),
964 "cannot have an attribute group reference in an attribute group");
965
966 unsigned AttrGrpNum = Lex.getUIntVal();
967 if (inAttrGrp) break;
968
969 // Save the reference to the attribute group. We'll fill it in later.
970 FwdRefAttrGrps.push_back(AttrGrpNum);
971 break;
972 }
Bill Wendling63b88192013-02-06 06:52:58 +0000973 // Target-dependent attributes:
974 case lltok::StringConstant: {
975 std::string Attr = Lex.getStrVal();
976 Lex.Lex();
977 std::string Val;
978 if (EatIfPresent(lltok::equal) &&
979 ParseStringConstant(Val))
980 return true;
981
982 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000983 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000984 }
985
986 // Target-independent attributes:
987 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000988 // As a hack, we allow function alignment to be initially parsed as an
989 // attribute on a function declaration/definition or added to an attribute
990 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000991 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000992 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000993 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000994 if (ParseToken(lltok::equal, "expected '=' here") ||
995 ParseUInt32(Alignment))
996 return true;
997 } else {
998 if (ParseOptionalAlignment(Alignment))
999 return true;
1000 }
Bill Wendling63b88192013-02-06 06:52:58 +00001001 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +00001002 continue;
Bill Wendling63b88192013-02-06 06:52:58 +00001003 }
1004 case lltok::kw_alignstack: {
1005 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +00001006 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +00001007 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +00001008 if (ParseToken(lltok::equal, "expected '=' here") ||
1009 ParseUInt32(Alignment))
1010 return true;
1011 } else {
1012 if (ParseOptionalStackAlignment(Alignment))
1013 return true;
1014 }
Bill Wendling63b88192013-02-06 06:52:58 +00001015 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +00001016 continue;
Bill Wendling63b88192013-02-06 06:52:58 +00001017 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +00001018 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +00001019 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +00001020 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +00001021 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +00001022 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +00001023 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
1024 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
1025 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
1026 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
1027 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
1028 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
1029 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
1030 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
1031 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
1032 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001033 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +00001034 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
1035 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1036 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
1037 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
1038 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
1039 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
1040 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
1041 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
1042 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
1043 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
1044 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +00001045
1046 // Error handling.
1047 case lltok::kw_inreg:
1048 case lltok::kw_signext:
1049 case lltok::kw_zeroext:
1050 HaveError |=
1051 Error(Lex.getLoc(),
1052 "invalid use of attribute on a function");
1053 break;
1054 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001055 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001056 case lltok::kw_nest:
1057 case lltok::kw_noalias:
1058 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001059 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +00001060 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001061 case lltok::kw_sret:
1062 HaveError |=
1063 Error(Lex.getLoc(),
1064 "invalid use of parameter-only attribute on a function");
1065 break;
Bill Wendling63b88192013-02-06 06:52:58 +00001066 }
1067
1068 Lex.Lex();
1069 }
1070}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001071
1072//===----------------------------------------------------------------------===//
1073// GlobalValue Reference/Resolution Routines.
1074//===----------------------------------------------------------------------===//
1075
1076/// GetGlobalVal - Get a value with the specified name or ID, creating a
1077/// forward reference record if needed. This can return null if the value
1078/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001079GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001080 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001081 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001082 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001083 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001084 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001085 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001086
Chris Lattnerac161bf2009-01-02 07:01:27 +00001087 // Look this name up in the normal function symbol table.
1088 GlobalValue *Val =
1089 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001090
Chris Lattnerac161bf2009-01-02 07:01:27 +00001091 // If this is a forward reference for the value, see if we already created a
1092 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001093 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001094 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1095 I = ForwardRefVals.find(Name);
1096 if (I != ForwardRefVals.end())
1097 Val = I->second.first;
1098 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001099
Chris Lattnerac161bf2009-01-02 07:01:27 +00001100 // If we have the value in the symbol table or fwd-ref table, return it.
1101 if (Val) {
1102 if (Val->getType() == Ty) return Val;
1103 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001104 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001105 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001106 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001107
Chris Lattnerac161bf2009-01-02 07:01:27 +00001108 // Otherwise, create a new forward reference for this value and remember it.
1109 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001110 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001111 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001112 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001113 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001114 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1115 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001116 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001117
Chris Lattnerac161bf2009-01-02 07:01:27 +00001118 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1119 return FwdVal;
1120}
1121
Chris Lattner229907c2011-07-18 04:54:35 +00001122GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1123 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001124 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001125 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001126 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001127 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001128
Craig Topper2617dcc2014-04-15 06:32:26 +00001129 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001130
Chris Lattnerac161bf2009-01-02 07:01:27 +00001131 // If this is a forward reference for the value, see if we already created a
1132 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001133 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001134 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1135 I = ForwardRefValIDs.find(ID);
1136 if (I != ForwardRefValIDs.end())
1137 Val = I->second.first;
1138 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001139
Chris Lattnerac161bf2009-01-02 07:01:27 +00001140 // If we have the value in the symbol table or fwd-ref table, return it.
1141 if (Val) {
1142 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001143 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001144 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001145 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001146 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001147
Chris Lattnerac161bf2009-01-02 07:01:27 +00001148 // Otherwise, create a new forward reference for this value and remember it.
1149 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001150 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001151 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001152 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001153 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001154 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001155
Chris Lattnerac161bf2009-01-02 07:01:27 +00001156 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1157 return FwdVal;
1158}
1159
1160
1161//===----------------------------------------------------------------------===//
David Majnemerdad0a642014-06-27 18:19:56 +00001162// Comdat Reference/Resolution Routines.
1163//===----------------------------------------------------------------------===//
1164
1165Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1166 // Look this name up in the comdat symbol table.
1167 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1168 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1169 if (I != ComdatSymTab.end())
1170 return &I->second;
1171
1172 // Otherwise, create a new forward reference for this value and remember it.
1173 Comdat *C = M->getOrInsertComdat(Name);
1174 ForwardRefComdats[Name] = Loc;
1175 return C;
1176}
1177
1178
1179//===----------------------------------------------------------------------===//
Chris Lattnerac161bf2009-01-02 07:01:27 +00001180// Helper Routines.
1181//===----------------------------------------------------------------------===//
1182
1183/// ParseToken - If the current token has the specified kind, eat it and return
1184/// success. Otherwise, emit the specified error and return failure.
1185bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1186 if (Lex.getKind() != T)
1187 return TokError(ErrMsg);
1188 Lex.Lex();
1189 return false;
1190}
1191
Chris Lattner3822f632009-01-02 08:05:26 +00001192/// ParseStringConstant
1193/// ::= StringConstant
1194bool LLParser::ParseStringConstant(std::string &Result) {
1195 if (Lex.getKind() != lltok::StringConstant)
1196 return TokError("expected string constant");
1197 Result = Lex.getStrVal();
1198 Lex.Lex();
1199 return false;
1200}
1201
1202/// ParseUInt32
1203/// ::= uint32
1204bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001205 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1206 return TokError("expected integer");
1207 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1208 if (Val64 != unsigned(Val64))
1209 return TokError("expected 32-bit integer (too large)");
1210 Val = Val64;
1211 Lex.Lex();
1212 return false;
1213}
1214
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001215/// ParseTLSModel
1216/// := 'localdynamic'
1217/// := 'initialexec'
1218/// := 'localexec'
1219bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1220 switch (Lex.getKind()) {
1221 default:
1222 return TokError("expected localdynamic, initialexec or localexec");
1223 case lltok::kw_localdynamic:
1224 TLM = GlobalVariable::LocalDynamicTLSModel;
1225 break;
1226 case lltok::kw_initialexec:
1227 TLM = GlobalVariable::InitialExecTLSModel;
1228 break;
1229 case lltok::kw_localexec:
1230 TLM = GlobalVariable::LocalExecTLSModel;
1231 break;
1232 }
1233
1234 Lex.Lex();
1235 return false;
1236}
1237
1238/// ParseOptionalThreadLocal
1239/// := /*empty*/
1240/// := 'thread_local'
1241/// := 'thread_local' '(' tlsmodel ')'
1242bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1243 TLM = GlobalVariable::NotThreadLocal;
1244 if (!EatIfPresent(lltok::kw_thread_local))
1245 return false;
1246
1247 TLM = GlobalVariable::GeneralDynamicTLSModel;
1248 if (Lex.getKind() == lltok::lparen) {
1249 Lex.Lex();
1250 return ParseTLSModel(TLM) ||
1251 ParseToken(lltok::rparen, "expected ')' after thread local model");
1252 }
1253 return false;
1254}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001255
1256/// ParseOptionalAddrSpace
1257/// := /*empty*/
1258/// := 'addrspace' '(' uint32 ')'
1259bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1260 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001261 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001262 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001263 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001264 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001265 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001266}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001267
Bill Wendling34c2eb22012-12-04 23:40:58 +00001268/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1269bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1270 bool HaveError = false;
1271
1272 B.clear();
1273
1274 while (1) {
1275 lltok::Kind Token = Lex.getKind();
1276 switch (Token) {
1277 default: // End of attributes.
1278 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001279 case lltok::kw_align: {
1280 unsigned Alignment;
1281 if (ParseOptionalAlignment(Alignment))
1282 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001283 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001284 continue;
1285 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001286 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001287 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001288 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1289 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1290 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1291 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001292 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001293 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1294 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001295 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001296 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1297 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1298 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001299
Stephen Lin7577ed52013-04-20 13:16:13 +00001300 case lltok::kw_alignstack:
1301 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001302 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001303 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001304 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001305 case lltok::kw_minsize:
1306 case lltok::kw_naked:
1307 case lltok::kw_nobuiltin:
1308 case lltok::kw_noduplicate:
1309 case lltok::kw_noimplicitfloat:
1310 case lltok::kw_noinline:
1311 case lltok::kw_nonlazybind:
1312 case lltok::kw_noredzone:
1313 case lltok::kw_noreturn:
1314 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001315 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001316 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001317 case lltok::kw_returns_twice:
1318 case lltok::kw_sanitize_address:
1319 case lltok::kw_sanitize_memory:
1320 case lltok::kw_sanitize_thread:
1321 case lltok::kw_ssp:
1322 case lltok::kw_sspreq:
1323 case lltok::kw_sspstrong:
1324 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001325 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1326 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001327 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001328
Bill Wendling34c2eb22012-12-04 23:40:58 +00001329 Lex.Lex();
1330 }
1331}
1332
1333/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1334bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1335 bool HaveError = false;
1336
1337 B.clear();
1338
1339 while (1) {
1340 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001341 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001342 default: // End of attributes.
1343 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001344 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1345 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001346 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001347 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1348 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001349
Bill Wendling34c2eb22012-12-04 23:40:58 +00001350 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001351 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001352 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001353 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001354 case lltok::kw_nest:
1355 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001356 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001357 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001358 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001359 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001360
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001361 case lltok::kw_alignstack:
1362 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001363 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001364 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001365 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001366 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001367 case lltok::kw_minsize:
1368 case lltok::kw_naked:
1369 case lltok::kw_nobuiltin:
1370 case lltok::kw_noduplicate:
1371 case lltok::kw_noimplicitfloat:
1372 case lltok::kw_noinline:
1373 case lltok::kw_nonlazybind:
1374 case lltok::kw_noredzone:
1375 case lltok::kw_noreturn:
1376 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001377 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001378 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001379 case lltok::kw_returns_twice:
1380 case lltok::kw_sanitize_address:
1381 case lltok::kw_sanitize_memory:
1382 case lltok::kw_sanitize_thread:
1383 case lltok::kw_ssp:
1384 case lltok::kw_sspreq:
1385 case lltok::kw_sspstrong:
1386 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001387 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001388 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001389
1390 case lltok::kw_readnone:
1391 case lltok::kw_readonly:
1392 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001393 }
1394
Chris Lattnerac161bf2009-01-02 07:01:27 +00001395 Lex.Lex();
1396 }
1397}
1398
1399/// ParseOptionalLinkage
1400/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001401/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001402/// ::= 'internal'
1403/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001404/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001405/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001406/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001407/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001408/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001409/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001410/// ::= 'extern_weak'
1411/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001412///
1413/// Deprecated Values:
1414/// ::= 'linker_private'
1415/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001416bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1417 HasLinkage = false;
1418 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001419 default: Res=GlobalValue::ExternalLinkage; return false;
1420 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001421 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1422 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1423 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1424 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1425 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001426 case lltok::kw_available_externally:
1427 Res = GlobalValue::AvailableExternallyLinkage;
1428 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001429 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001430 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001431 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1432 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001433
1434 case lltok::kw_linker_private:
1435 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001436 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1437 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001438 Lex.Lex();
1439 // treat linker_private and linker_private_weak as PrivateLinkage
1440 Res = GlobalValue::PrivateLinkage;
1441 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001442 }
1443 Lex.Lex();
1444 HasLinkage = true;
1445 return false;
1446}
1447
1448/// ParseOptionalVisibility
1449/// ::= /*empty*/
1450/// ::= 'default'
1451/// ::= 'hidden'
1452/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001453///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001454bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1455 switch (Lex.getKind()) {
1456 default: Res = GlobalValue::DefaultVisibility; return false;
1457 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1458 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1459 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1460 }
1461 Lex.Lex();
1462 return false;
1463}
1464
Nico Rieck7157bb72014-01-14 15:22:47 +00001465/// ParseOptionalDLLStorageClass
1466/// ::= /*empty*/
1467/// ::= 'dllimport'
1468/// ::= 'dllexport'
1469///
1470bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1471 switch (Lex.getKind()) {
1472 default: Res = GlobalValue::DefaultStorageClass; return false;
1473 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1474 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1475 }
1476 Lex.Lex();
1477 return false;
1478}
1479
Chris Lattnerac161bf2009-01-02 07:01:27 +00001480/// ParseOptionalCallingConv
1481/// ::= /*empty*/
1482/// ::= 'ccc'
1483/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001484/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001485/// ::= 'coldcc'
1486/// ::= 'x86_stdcallcc'
1487/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001488/// ::= 'x86_thiscallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001489/// ::= 'arm_apcscc'
1490/// ::= 'arm_aapcscc'
1491/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001492/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001493/// ::= 'ptx_kernel'
1494/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001495/// ::= 'spir_func'
1496/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001497/// ::= 'x86_64_sysvcc'
1498/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001499/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001500/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001501/// ::= 'preserve_mostcc'
1502/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001503/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001504///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001505bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001506 switch (Lex.getKind()) {
1507 default: CC = CallingConv::C; return false;
1508 case lltok::kw_ccc: CC = CallingConv::C; break;
1509 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1510 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1511 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1512 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001513 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001514 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1515 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1516 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001517 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001518 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1519 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001520 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1521 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001522 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001523 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1524 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001525 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001526 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001527 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1528 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001529 case lltok::kw_cc: {
1530 unsigned ArbitraryCC;
1531 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001532 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001533 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001534 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1535 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001536 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001537 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001538
Chris Lattnerac161bf2009-01-02 07:01:27 +00001539 Lex.Lex();
1540 return false;
1541}
1542
Chris Lattner5c427632009-12-30 05:31:19 +00001543/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001544/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001545bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1546 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001547 do {
1548 if (Lex.getKind() != lltok::MetadataVar)
1549 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001550
Chris Lattner596760d2009-12-29 21:25:40 +00001551 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001552 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001553 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001554
Chris Lattner1797fc72009-12-29 21:53:55 +00001555 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001556 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001557
1558 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001559 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001560
Dan Gohmanf0715b12010-08-24 14:35:45 +00001561 // This code is similar to that of ParseMetadataValue, however it needs to
1562 // have special-case code for a forward reference; see the comments on
1563 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1564 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001565 if (Lex.getKind() == lltok::lbrace) {
1566 ValID ID;
1567 if (ParseMetadataListValue(ID, PFS))
1568 return true;
1569 assert(ID.Kind == ValID::t_MDNode);
1570 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001571 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001572 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001573 if (ParseMDNodeID(Node, NodeID))
1574 return true;
1575 if (Node) {
1576 // If we got the node, add it to the instruction.
1577 Inst->setMetadata(MDK, Node);
1578 } else {
1579 MDRef R = { Loc, MDK, NodeID };
1580 // Otherwise, remember that this should be resolved later.
1581 ForwardRefInstMetadata[Inst].push_back(R);
1582 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001583 }
Chris Lattner596760d2009-12-29 21:25:40 +00001584
Manman Ren209b17c2013-09-28 00:22:27 +00001585 if (MDK == LLVMContext::MD_tbaa)
1586 InstsWithTBAATag.push_back(Inst);
1587
Chris Lattner596760d2009-12-29 21:25:40 +00001588 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001589 } while (EatIfPresent(lltok::comma));
1590 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001591}
1592
Chris Lattnerac161bf2009-01-02 07:01:27 +00001593/// ParseOptionalAlignment
1594/// ::= /* empty */
1595/// ::= 'align' 4
1596bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1597 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001598 if (!EatIfPresent(lltok::kw_align))
1599 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001600 LocTy AlignLoc = Lex.getLoc();
1601 if (ParseUInt32(Alignment)) return true;
1602 if (!isPowerOf2_32(Alignment))
1603 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001604 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001605 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001606 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001607}
1608
Chris Lattnerb2f39502009-12-30 05:44:30 +00001609/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001610/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001611/// ::= ',' align 4
1612///
1613/// This returns with AteExtraComma set to true if it ate an excess comma at the
1614/// end.
1615bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1616 bool &AteExtraComma) {
1617 AteExtraComma = false;
1618 while (EatIfPresent(lltok::comma)) {
1619 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001620 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001621 AteExtraComma = true;
1622 return false;
1623 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001624
Chris Lattner95b0ff42010-04-23 00:50:50 +00001625 if (Lex.getKind() != lltok::kw_align)
1626 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001627
Chris Lattner95b0ff42010-04-23 00:50:50 +00001628 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001629 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001630
Devang Patelea8a4b92009-09-17 23:04:48 +00001631 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001632}
1633
Eli Friedmanfee02c62011-07-25 23:16:38 +00001634/// ParseScopeAndOrdering
1635/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1636/// else: ::=
1637///
1638/// This sets Scope and Ordering to the parsed values.
1639bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1640 AtomicOrdering &Ordering) {
1641 if (!isAtomic)
1642 return false;
1643
1644 Scope = CrossThread;
1645 if (EatIfPresent(lltok::kw_singlethread))
1646 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001647
1648 return ParseOrdering(Ordering);
1649}
1650
1651/// ParseOrdering
1652/// ::= AtomicOrdering
1653///
1654/// This sets Ordering to the parsed value.
1655bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001656 switch (Lex.getKind()) {
1657 default: return TokError("Expected ordering on atomic instruction");
1658 case lltok::kw_unordered: Ordering = Unordered; break;
1659 case lltok::kw_monotonic: Ordering = Monotonic; break;
1660 case lltok::kw_acquire: Ordering = Acquire; break;
1661 case lltok::kw_release: Ordering = Release; break;
1662 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1663 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1664 }
1665 Lex.Lex();
1666 return false;
1667}
1668
Charles Davisbe5557e2010-02-12 00:31:15 +00001669/// ParseOptionalStackAlignment
1670/// ::= /* empty */
1671/// ::= 'alignstack' '(' 4 ')'
1672bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1673 Alignment = 0;
1674 if (!EatIfPresent(lltok::kw_alignstack))
1675 return false;
1676 LocTy ParenLoc = Lex.getLoc();
1677 if (!EatIfPresent(lltok::lparen))
1678 return Error(ParenLoc, "expected '('");
1679 LocTy AlignLoc = Lex.getLoc();
1680 if (ParseUInt32(Alignment)) return true;
1681 ParenLoc = Lex.getLoc();
1682 if (!EatIfPresent(lltok::rparen))
1683 return Error(ParenLoc, "expected ')'");
1684 if (!isPowerOf2_32(Alignment))
1685 return Error(AlignLoc, "stack alignment is not a power of two");
1686 return false;
1687}
Devang Patelea8a4b92009-09-17 23:04:48 +00001688
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001689/// ParseIndexList - This parses the index list for an insert/extractvalue
1690/// instruction. This sets AteExtraComma in the case where we eat an extra
1691/// comma at the end of the line and find that it is followed by metadata.
1692/// Clients that don't allow metadata can call the version of this function that
1693/// only takes one argument.
1694///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001695/// ParseIndexList
1696/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001697///
1698bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1699 bool &AteExtraComma) {
1700 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001701
Chris Lattnerac161bf2009-01-02 07:01:27 +00001702 if (Lex.getKind() != lltok::comma)
1703 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001704
Chris Lattner3822f632009-01-02 08:05:26 +00001705 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001706 if (Lex.getKind() == lltok::MetadataVar) {
1707 AteExtraComma = true;
1708 return false;
1709 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001710 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001711 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001712 Indices.push_back(Idx);
1713 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001714
Chris Lattnerac161bf2009-01-02 07:01:27 +00001715 return false;
1716}
1717
1718//===----------------------------------------------------------------------===//
1719// Type Parsing.
1720//===----------------------------------------------------------------------===//
1721
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001722/// ParseType - Parse a type.
1723bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1724 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001725 switch (Lex.getKind()) {
1726 default:
1727 return TokError("expected type");
1728 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001729 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001730 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001731 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001732 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001733 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001734 // Type ::= StructType
1735 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001736 return true;
1737 break;
1738 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001739 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001740 Lex.Lex(); // eat the lsquare.
1741 if (ParseArrayVectorType(Result, false))
1742 return true;
1743 break;
1744 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001745 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001746 Lex.Lex();
1747 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001748 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001749 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001750 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001751 } else if (ParseArrayVectorType(Result, true))
1752 return true;
1753 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001754 case lltok::LocalVar: {
1755 // Type ::= %foo
1756 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001757
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001758 // If the type hasn't been defined yet, create a forward definition and
1759 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001760 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001761 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001762 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001763 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001764 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001765 Lex.Lex();
1766 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001767 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001768
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001769 case lltok::LocalVarID: {
1770 // Type ::= %4
1771 if (Lex.getUIntVal() >= NumberedTypes.size())
1772 NumberedTypes.resize(Lex.getUIntVal()+1);
1773 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001774
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001775 // If the type hasn't been defined yet, create a forward definition and
1776 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001777 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001778 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001779 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001780 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001781 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001782 Lex.Lex();
1783 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001784 }
1785 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001786
1787 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001788 while (1) {
1789 switch (Lex.getKind()) {
1790 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001791 default:
1792 if (!AllowVoid && Result->isVoidTy())
1793 return Error(TypeLoc, "void type only allowed for function results");
1794 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001795
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001796 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001797 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001798 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001799 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001800 if (Result->isVoidTy())
1801 return TokError("pointers to void are invalid - use i8* instead");
1802 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001803 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001804 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001805 Lex.Lex();
1806 break;
1807
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001808 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001809 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001810 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001811 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001812 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001813 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001814 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001815 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001816 unsigned AddrSpace;
1817 if (ParseOptionalAddrSpace(AddrSpace) ||
1818 ParseToken(lltok::star, "expected '*' in address space"))
1819 return true;
1820
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001821 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001822 break;
1823 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001824
Chris Lattnerac161bf2009-01-02 07:01:27 +00001825 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1826 case lltok::lparen:
1827 if (ParseFunctionType(Result))
1828 return true;
1829 break;
1830 }
1831 }
1832}
1833
1834/// ParseParameterList
1835/// ::= '(' ')'
1836/// ::= '(' Arg (',' Arg)* ')'
1837/// Arg
1838/// ::= Type OptionalAttributes Value OptionalAttributes
1839bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1840 PerFunctionState &PFS) {
1841 if (ParseToken(lltok::lparen, "expected '(' in call"))
1842 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001843
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001844 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001845 while (Lex.getKind() != lltok::rparen) {
1846 // If this isn't the first argument, we need a comma.
1847 if (!ArgList.empty() &&
1848 ParseToken(lltok::comma, "expected ',' in argument list"))
1849 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001850
Chris Lattnerac161bf2009-01-02 07:01:27 +00001851 // Parse the argument.
1852 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001853 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001854 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001855 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001856 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001857 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001858
Chris Lattner5b4a9622009-12-30 02:11:14 +00001859 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001860 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001861 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001862 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1863 AttrIndex++,
1864 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001865 }
1866
1867 Lex.Lex(); // Lex the ')'.
1868 return false;
1869}
1870
1871
1872
Chris Lattner2ed06b42009-01-05 18:34:07 +00001873/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001874/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001875/// ::= '(' ArgTypeListI ')'
1876/// ArgTypeListI
1877/// ::= /*empty*/
1878/// ::= '...'
1879/// ::= ArgTypeList ',' '...'
1880/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001881///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001882bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1883 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001884 isVarArg = false;
1885 assert(Lex.getKind() == lltok::lparen);
1886 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001887
Chris Lattnerac161bf2009-01-02 07:01:27 +00001888 if (Lex.getKind() == lltok::rparen) {
1889 // empty
1890 } else if (Lex.getKind() == lltok::dotdotdot) {
1891 isVarArg = true;
1892 Lex.Lex();
1893 } else {
1894 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001895 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001896 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001897 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001898
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001899 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001900 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001901
Chris Lattnerfdd87902009-10-05 05:54:46 +00001902 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001903 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001904
Chris Lattnerdef19492011-06-17 06:36:20 +00001905 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001906 Name = Lex.getStrVal();
1907 Lex.Lex();
1908 }
Chris Lattner3822f632009-01-02 08:05:26 +00001909
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001910 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001911 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001912
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001913 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001914 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001915 AttributeSet::get(ArgTy->getContext(),
1916 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001917
Chris Lattner3822f632009-01-02 08:05:26 +00001918 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001919 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001920 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001921 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001922 break;
1923 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001924
Chris Lattnerac161bf2009-01-02 07:01:27 +00001925 // Otherwise must be an argument type.
1926 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001927 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001928
Chris Lattnerfdd87902009-10-05 05:54:46 +00001929 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001930 return Error(TypeLoc, "argument can not have void type");
1931
Chris Lattnerdef19492011-06-17 06:36:20 +00001932 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001933 Name = Lex.getStrVal();
1934 Lex.Lex();
1935 } else {
1936 Name = "";
1937 }
Chris Lattner3822f632009-01-02 08:05:26 +00001938
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001939 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001940 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001941
Bill Wendlingd079a442012-10-15 04:46:55 +00001942 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001943 AttributeSet::get(ArgTy->getContext(),
1944 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001945 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001946 }
1947 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001948
Chris Lattner3822f632009-01-02 08:05:26 +00001949 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001950}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001951
Chris Lattnerac161bf2009-01-02 07:01:27 +00001952/// ParseFunctionType
1953/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001954bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001955 assert(Lex.getKind() == lltok::lparen);
1956
Chris Lattnerce473c72009-01-05 08:04:33 +00001957 if (!FunctionType::isValidReturnType(Result))
1958 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001959
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001960 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001961 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001962 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001963 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001964
Chris Lattnerac161bf2009-01-02 07:01:27 +00001965 // Reject names on the arguments lists.
1966 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1967 if (!ArgList[i].Name.empty())
1968 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001969 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001970 return Error(ArgList[i].Loc,
1971 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001972 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001973
Jay Foadb804a2b2011-07-12 14:06:48 +00001974 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001975 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001976 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001977
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001978 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001979 return false;
1980}
1981
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001982/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1983/// other structs.
1984bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1985 SmallVector<Type*, 8> Elts;
1986 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001987
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001988 Result = StructType::get(Context, Elts, Packed);
1989 return false;
1990}
1991
1992/// ParseStructDefinition - Parse a struct in a 'type' definition.
1993bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1994 std::pair<Type*, LocTy> &Entry,
1995 Type *&ResultTy) {
1996 // If the type was already defined, diagnose the redefinition.
1997 if (Entry.first && !Entry.second.isValid())
1998 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001999
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002000 // If we have opaque, just return without filling in the definition for the
2001 // struct. This counts as a definition as far as the .ll file goes.
2002 if (EatIfPresent(lltok::kw_opaque)) {
2003 // This type is being defined, so clear the location to indicate this.
2004 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002005
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002006 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002007 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002008 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002009 ResultTy = Entry.first;
2010 return false;
2011 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002012
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002013 // If the type starts with '<', then it is either a packed struct or a vector.
2014 bool isPacked = EatIfPresent(lltok::less);
2015
2016 // If we don't have a struct, then we have a random type alias, which we
2017 // accept for compatibility with old files. These types are not allowed to be
2018 // forward referenced and not allowed to be recursive.
2019 if (Lex.getKind() != lltok::lbrace) {
2020 if (Entry.first)
2021 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002022
Craig Topper2617dcc2014-04-15 06:32:26 +00002023 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002024 if (isPacked)
2025 return ParseArrayVectorType(ResultTy, true);
2026 return ParseType(ResultTy);
2027 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002028
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002029 // This type is being defined, so clear the location to indicate this.
2030 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002031
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002032 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002033 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002034 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002035
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002036 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002037
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002038 SmallVector<Type*, 8> Body;
2039 if (ParseStructBody(Body) ||
2040 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
2041 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002042
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002043 STy->setBody(Body, isPacked);
2044 ResultTy = STy;
2045 return false;
2046}
2047
2048
Chris Lattnerac161bf2009-01-02 07:01:27 +00002049/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002050/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00002051/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002052/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002053/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002054/// ::= '<' '{' Type (',' Type)* '}' '>'
2055bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002056 assert(Lex.getKind() == lltok::lbrace);
2057 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002058
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002059 // Handle the empty struct.
2060 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002061 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002062
Chris Lattnerf880ca22009-03-09 04:49:14 +00002063 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002064 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002065 if (ParseType(Ty)) return true;
2066 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002067
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002068 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002069 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002070
Chris Lattner3822f632009-01-02 08:05:26 +00002071 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00002072 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002073 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002074
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002075 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002076 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002077
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002078 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002079 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002080
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002081 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002082}
2083
2084/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2085/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002086/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002087/// ::= '[' APSINTVAL 'x' Types ']'
2088/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002089bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002090 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2091 Lex.getAPSIntVal().getBitWidth() > 64)
2092 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002093
Chris Lattnerac161bf2009-01-02 07:01:27 +00002094 LocTy SizeLoc = Lex.getLoc();
2095 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002096 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002097
Chris Lattner3822f632009-01-02 08:05:26 +00002098 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2099 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002100
2101 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002102 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002103 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002104
Chris Lattner3822f632009-01-02 08:05:26 +00002105 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2106 "expected end of sequential type"))
2107 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002108
Chris Lattnerac161bf2009-01-02 07:01:27 +00002109 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002110 if (Size == 0)
2111 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002112 if ((unsigned)Size != Size)
2113 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002114 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002115 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002116 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002117 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002118 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002119 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002120 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002121 }
2122 return false;
2123}
2124
2125//===----------------------------------------------------------------------===//
2126// Function Semantic Analysis.
2127//===----------------------------------------------------------------------===//
2128
Chris Lattner3432c622009-10-28 03:39:23 +00002129LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2130 int functionNumber)
2131 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002132
2133 // Insert unnamed arguments into the NumberedVals list.
2134 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2135 AI != E; ++AI)
2136 if (!AI->hasName())
2137 NumberedVals.push_back(AI);
2138}
2139
2140LLParser::PerFunctionState::~PerFunctionState() {
2141 // If there were any forward referenced non-basicblock values, delete them.
2142 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2143 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2144 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002145 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002146 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002147 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002148 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002149 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002150
Chris Lattnerac161bf2009-01-02 07:01:27 +00002151 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2152 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2153 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002154 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002155 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002156 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002157 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002158 }
2159}
2160
Chris Lattner3432c622009-10-28 03:39:23 +00002161bool LLParser::PerFunctionState::FinishFunction() {
2162 // Check to see if someone took the address of labels in this block.
2163 if (!P.ForwardRefBlockAddresses.empty()) {
2164 ValID FunctionID;
2165 if (!F.getName().empty()) {
2166 FunctionID.Kind = ValID::t_GlobalName;
2167 FunctionID.StrVal = F.getName();
2168 } else {
2169 FunctionID.Kind = ValID::t_GlobalID;
2170 FunctionID.UIntVal = FunctionNumber;
2171 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002172
Chris Lattner3432c622009-10-28 03:39:23 +00002173 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2174 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2175 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2176 // Resolve all these references.
2177 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2178 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002179
Chris Lattner3432c622009-10-28 03:39:23 +00002180 P.ForwardRefBlockAddresses.erase(FRBAI);
2181 }
2182 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002183
Chris Lattnerac161bf2009-01-02 07:01:27 +00002184 if (!ForwardRefVals.empty())
2185 return P.Error(ForwardRefVals.begin()->second.second,
2186 "use of undefined value '%" + ForwardRefVals.begin()->first +
2187 "'");
2188 if (!ForwardRefValIDs.empty())
2189 return P.Error(ForwardRefValIDs.begin()->second.second,
2190 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002191 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002192 return false;
2193}
2194
2195
2196/// GetVal - Get a value with the specified name or ID, creating a
2197/// forward reference record if needed. This can return null if the value
2198/// exists but does not have the right type.
2199Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002200 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002201 // Look this name up in the normal function symbol table.
2202 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002203
Chris Lattnerac161bf2009-01-02 07:01:27 +00002204 // If this is a forward reference for the value, see if we already created a
2205 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002206 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002207 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2208 I = ForwardRefVals.find(Name);
2209 if (I != ForwardRefVals.end())
2210 Val = I->second.first;
2211 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002212
Chris Lattnerac161bf2009-01-02 07:01:27 +00002213 // If we have the value in the symbol table or fwd-ref table, return it.
2214 if (Val) {
2215 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002216 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002217 P.Error(Loc, "'%" + Name + "' is not a basic block");
2218 else
2219 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002220 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002221 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002222 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002223
Chris Lattnerac161bf2009-01-02 07:01:27 +00002224 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002225 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002226 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002227 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002228 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002229
Chris Lattnerac161bf2009-01-02 07:01:27 +00002230 // Otherwise, create a new forward reference for this value and remember it.
2231 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002232 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002233 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002234 else
2235 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002236
Chris Lattnerac161bf2009-01-02 07:01:27 +00002237 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2238 return FwdVal;
2239}
2240
Chris Lattner229907c2011-07-18 04:54:35 +00002241Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002242 LocTy Loc) {
2243 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002244 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002245
Chris Lattnerac161bf2009-01-02 07:01:27 +00002246 // If this is a forward reference for the value, see if we already created a
2247 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002248 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002249 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2250 I = ForwardRefValIDs.find(ID);
2251 if (I != ForwardRefValIDs.end())
2252 Val = I->second.first;
2253 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002254
Chris Lattnerac161bf2009-01-02 07:01:27 +00002255 // If we have the value in the symbol table or fwd-ref table, return it.
2256 if (Val) {
2257 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002258 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002259 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002260 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002261 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002262 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002263 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002264 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002265
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002266 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002267 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002268 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002269 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002270
Chris Lattnerac161bf2009-01-02 07:01:27 +00002271 // Otherwise, create a new forward reference for this value and remember it.
2272 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002273 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002274 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002275 else
2276 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002277
Chris Lattnerac161bf2009-01-02 07:01:27 +00002278 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2279 return FwdVal;
2280}
2281
2282/// SetInstName - After an instruction is parsed and inserted into its
2283/// basic block, this installs its name.
2284bool LLParser::PerFunctionState::SetInstName(int NameID,
2285 const std::string &NameStr,
2286 LocTy NameLoc, Instruction *Inst) {
2287 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002288 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002289 if (NameID != -1 || !NameStr.empty())
2290 return P.Error(NameLoc, "instructions returning void cannot have a name");
2291 return false;
2292 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002293
Chris Lattnerac161bf2009-01-02 07:01:27 +00002294 // If this was a numbered instruction, verify that the instruction is the
2295 // expected value and resolve any forward references.
2296 if (NameStr.empty()) {
2297 // If neither a name nor an ID was specified, just use the next ID.
2298 if (NameID == -1)
2299 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002300
Chris Lattnerac161bf2009-01-02 07:01:27 +00002301 if (unsigned(NameID) != NumberedVals.size())
2302 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002303 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002304
Chris Lattnerac161bf2009-01-02 07:01:27 +00002305 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2306 ForwardRefValIDs.find(NameID);
2307 if (FI != ForwardRefValIDs.end()) {
2308 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002309 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002310 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002311 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002312 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002313 ForwardRefValIDs.erase(FI);
2314 }
2315
2316 NumberedVals.push_back(Inst);
2317 return false;
2318 }
2319
2320 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2321 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2322 FI = ForwardRefVals.find(NameStr);
2323 if (FI != ForwardRefVals.end()) {
2324 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002325 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002326 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002327 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002328 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002329 ForwardRefVals.erase(FI);
2330 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002331
Chris Lattnerac161bf2009-01-02 07:01:27 +00002332 // Set the name on the instruction.
2333 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002334
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002335 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002336 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002337 NameStr + "'");
2338 return false;
2339}
2340
2341/// GetBB - Get a basic block with the specified name or ID, creating a
2342/// forward reference record if needed.
2343BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2344 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002345 return cast_or_null<BasicBlock>(GetVal(Name,
2346 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002347}
2348
2349BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002350 return cast_or_null<BasicBlock>(GetVal(ID,
2351 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002352}
2353
2354/// DefineBB - Define the specified basic block, which is either named or
2355/// unnamed. If there is an error, this returns null otherwise it returns
2356/// the block being defined.
2357BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2358 LocTy Loc) {
2359 BasicBlock *BB;
2360 if (Name.empty())
2361 BB = GetBB(NumberedVals.size(), Loc);
2362 else
2363 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002364 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002365
Chris Lattnerac161bf2009-01-02 07:01:27 +00002366 // Move the block to the end of the function. Forward ref'd blocks are
2367 // inserted wherever they happen to be referenced.
2368 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002369
Chris Lattnerac161bf2009-01-02 07:01:27 +00002370 // Remove the block from forward ref sets.
2371 if (Name.empty()) {
2372 ForwardRefValIDs.erase(NumberedVals.size());
2373 NumberedVals.push_back(BB);
2374 } else {
2375 // BB forward references are already in the function symbol table.
2376 ForwardRefVals.erase(Name);
2377 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002378
Chris Lattnerac161bf2009-01-02 07:01:27 +00002379 return BB;
2380}
2381
2382//===----------------------------------------------------------------------===//
2383// Constants.
2384//===----------------------------------------------------------------------===//
2385
2386/// ParseValID - Parse an abstract value that doesn't necessarily have a
2387/// type implied. For example, if we parse "4" we don't know what integer type
2388/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002389/// sanity. PFS is used to convert function-local operands of metadata (since
2390/// metadata operands are not just parsed here but also converted to values).
2391/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002392bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002393 ID.Loc = Lex.getLoc();
2394 switch (Lex.getKind()) {
2395 default: return TokError("expected value token");
2396 case lltok::GlobalID: // @42
2397 ID.UIntVal = Lex.getUIntVal();
2398 ID.Kind = ValID::t_GlobalID;
2399 break;
2400 case lltok::GlobalVar: // @foo
2401 ID.StrVal = Lex.getStrVal();
2402 ID.Kind = ValID::t_GlobalName;
2403 break;
2404 case lltok::LocalVarID: // %42
2405 ID.UIntVal = Lex.getUIntVal();
2406 ID.Kind = ValID::t_LocalID;
2407 break;
2408 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002409 ID.StrVal = Lex.getStrVal();
2410 ID.Kind = ValID::t_LocalName;
2411 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002412 case lltok::exclaim: // !42, !{...}, or !"foo"
2413 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002414 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002415 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002416 ID.Kind = ValID::t_APSInt;
2417 break;
2418 case lltok::APFloat:
2419 ID.APFloatVal = Lex.getAPFloatVal();
2420 ID.Kind = ValID::t_APFloat;
2421 break;
2422 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002423 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002424 ID.Kind = ValID::t_Constant;
2425 break;
2426 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002427 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002428 ID.Kind = ValID::t_Constant;
2429 break;
2430 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2431 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2432 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002433
Chris Lattnerac161bf2009-01-02 07:01:27 +00002434 case lltok::lbrace: {
2435 // ValID ::= '{' ConstVector '}'
2436 Lex.Lex();
2437 SmallVector<Constant*, 16> Elts;
2438 if (ParseGlobalValueVector(Elts) ||
2439 ParseToken(lltok::rbrace, "expected end of struct constant"))
2440 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002441
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002442 ID.ConstantStructElts = new Constant*[Elts.size()];
2443 ID.UIntVal = Elts.size();
2444 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2445 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002446 return false;
2447 }
2448 case lltok::less: {
2449 // ValID ::= '<' ConstVector '>' --> Vector.
2450 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2451 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002452 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002453
Chris Lattnerac161bf2009-01-02 07:01:27 +00002454 SmallVector<Constant*, 16> Elts;
2455 LocTy FirstEltLoc = Lex.getLoc();
2456 if (ParseGlobalValueVector(Elts) ||
2457 (isPackedStruct &&
2458 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2459 ParseToken(lltok::greater, "expected end of constant"))
2460 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002461
Chris Lattnerac161bf2009-01-02 07:01:27 +00002462 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002463 ID.ConstantStructElts = new Constant*[Elts.size()];
2464 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2465 ID.UIntVal = Elts.size();
2466 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002467 return false;
2468 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002469
Chris Lattnerac161bf2009-01-02 07:01:27 +00002470 if (Elts.empty())
2471 return Error(ID.Loc, "constant vector must not be empty");
2472
Duncan Sands9dff9be2010-02-15 16:12:20 +00002473 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002474 !Elts[0]->getType()->isFloatingPointTy() &&
2475 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002476 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002477 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002478
Chris Lattnerac161bf2009-01-02 07:01:27 +00002479 // Verify that all the vector elements have the same type.
2480 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2481 if (Elts[i]->getType() != Elts[0]->getType())
2482 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002483 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002484 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002485
Chris Lattner69229312011-02-15 00:14:00 +00002486 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002487 ID.Kind = ValID::t_Constant;
2488 return false;
2489 }
2490 case lltok::lsquare: { // Array Constant
2491 Lex.Lex();
2492 SmallVector<Constant*, 16> Elts;
2493 LocTy FirstEltLoc = Lex.getLoc();
2494 if (ParseGlobalValueVector(Elts) ||
2495 ParseToken(lltok::rsquare, "expected end of array constant"))
2496 return true;
2497
2498 // Handle empty element.
2499 if (Elts.empty()) {
2500 // Use undef instead of an array because it's inconvenient to determine
2501 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002502 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002503 return false;
2504 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002505
Chris Lattnerac161bf2009-01-02 07:01:27 +00002506 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002507 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002508 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002509
Owen Anderson4056ca92009-07-29 22:17:13 +00002510 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002511
Chris Lattnerac161bf2009-01-02 07:01:27 +00002512 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002513 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002514 if (Elts[i]->getType() != Elts[0]->getType())
2515 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002516 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002517 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002518 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002519
Jay Foad83be3612011-06-22 09:24:39 +00002520 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002521 ID.Kind = ValID::t_Constant;
2522 return false;
2523 }
2524 case lltok::kw_c: // c "foo"
2525 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002526 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2527 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002528 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2529 ID.Kind = ValID::t_Constant;
2530 return false;
2531
2532 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002533 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2534 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002535 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002536 Lex.Lex();
2537 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002538 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002539 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002540 ParseStringConstant(ID.StrVal) ||
2541 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002542 ParseToken(lltok::StringConstant, "expected constraint string"))
2543 return true;
2544 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002545 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002546 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002547 ID.Kind = ValID::t_InlineAsm;
2548 return false;
2549 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002550
Chris Lattner3432c622009-10-28 03:39:23 +00002551 case lltok::kw_blockaddress: {
2552 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2553 Lex.Lex();
2554
2555 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002556
Chris Lattner3432c622009-10-28 03:39:23 +00002557 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2558 ParseValID(Fn) ||
2559 ParseToken(lltok::comma, "expected comma in block address expression")||
2560 ParseValID(Label) ||
2561 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2562 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002563
Chris Lattner3432c622009-10-28 03:39:23 +00002564 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2565 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002566 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002567 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002568
Chris Lattner3432c622009-10-28 03:39:23 +00002569 // Make a global variable as a placeholder for this reference.
2570 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2571 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002572 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002573 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2574 ID.ConstantVal = FwdRef;
2575 ID.Kind = ValID::t_Constant;
2576 return false;
2577 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002578
Chris Lattnerac161bf2009-01-02 07:01:27 +00002579 case lltok::kw_trunc:
2580 case lltok::kw_zext:
2581 case lltok::kw_sext:
2582 case lltok::kw_fptrunc:
2583 case lltok::kw_fpext:
2584 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002585 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002586 case lltok::kw_uitofp:
2587 case lltok::kw_sitofp:
2588 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002589 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002590 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002591 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002592 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002593 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002594 Constant *SrcVal;
2595 Lex.Lex();
2596 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2597 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002598 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002599 ParseType(DestTy) ||
2600 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2601 return true;
2602 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2603 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002604 getTypeString(SrcVal->getType()) + "' to '" +
2605 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002606 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002607 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002608 ID.Kind = ValID::t_Constant;
2609 return false;
2610 }
2611 case lltok::kw_extractvalue: {
2612 Lex.Lex();
2613 Constant *Val;
2614 SmallVector<unsigned, 4> Indices;
2615 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2616 ParseGlobalTypeAndValue(Val) ||
2617 ParseIndexList(Indices) ||
2618 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2619 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002620
Chris Lattner392be582010-02-12 20:49:41 +00002621 if (!Val->getType()->isAggregateType())
2622 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002623 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002624 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002625 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002626 ID.Kind = ValID::t_Constant;
2627 return false;
2628 }
2629 case lltok::kw_insertvalue: {
2630 Lex.Lex();
2631 Constant *Val0, *Val1;
2632 SmallVector<unsigned, 4> Indices;
2633 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2634 ParseGlobalTypeAndValue(Val0) ||
2635 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2636 ParseGlobalTypeAndValue(Val1) ||
2637 ParseIndexList(Indices) ||
2638 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2639 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002640 if (!Val0->getType()->isAggregateType())
2641 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002642 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002643 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002644 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002645 ID.Kind = ValID::t_Constant;
2646 return false;
2647 }
2648 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002649 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002650 unsigned PredVal, Opc = Lex.getUIntVal();
2651 Constant *Val0, *Val1;
2652 Lex.Lex();
2653 if (ParseCmpPredicate(PredVal, Opc) ||
2654 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2655 ParseGlobalTypeAndValue(Val0) ||
2656 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2657 ParseGlobalTypeAndValue(Val1) ||
2658 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2659 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002660
Chris Lattnerac161bf2009-01-02 07:01:27 +00002661 if (Val0->getType() != Val1->getType())
2662 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002663
Chris Lattnerac161bf2009-01-02 07:01:27 +00002664 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002665
Chris Lattnerac161bf2009-01-02 07:01:27 +00002666 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002667 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002668 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002669 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002670 } else {
2671 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002672 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002673 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002674 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002675 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002676 }
2677 ID.Kind = ValID::t_Constant;
2678 return false;
2679 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002680
Chris Lattnerac161bf2009-01-02 07:01:27 +00002681 // Binary Operators.
2682 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002683 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002684 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002685 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002686 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002687 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002688 case lltok::kw_udiv:
2689 case lltok::kw_sdiv:
2690 case lltok::kw_fdiv:
2691 case lltok::kw_urem:
2692 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002693 case lltok::kw_frem:
2694 case lltok::kw_shl:
2695 case lltok::kw_lshr:
2696 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002697 bool NUW = false;
2698 bool NSW = false;
2699 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002700 unsigned Opc = Lex.getUIntVal();
2701 Constant *Val0, *Val1;
2702 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002703 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002704 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2705 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002706 if (EatIfPresent(lltok::kw_nuw))
2707 NUW = true;
2708 if (EatIfPresent(lltok::kw_nsw)) {
2709 NSW = true;
2710 if (EatIfPresent(lltok::kw_nuw))
2711 NUW = true;
2712 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002713 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2714 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002715 if (EatIfPresent(lltok::kw_exact))
2716 Exact = true;
2717 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002718 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2719 ParseGlobalTypeAndValue(Val0) ||
2720 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2721 ParseGlobalTypeAndValue(Val1) ||
2722 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2723 return true;
2724 if (Val0->getType() != Val1->getType())
2725 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002726 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002727 if (NUW)
2728 return Error(ModifierLoc, "nuw only applies to integer operations");
2729 if (NSW)
2730 return Error(ModifierLoc, "nsw only applies to integer operations");
2731 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002732 // Check that the type is valid for the operator.
2733 switch (Opc) {
2734 case Instruction::Add:
2735 case Instruction::Sub:
2736 case Instruction::Mul:
2737 case Instruction::UDiv:
2738 case Instruction::SDiv:
2739 case Instruction::URem:
2740 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002741 case Instruction::Shl:
2742 case Instruction::AShr:
2743 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002744 if (!Val0->getType()->isIntOrIntVectorTy())
2745 return Error(ID.Loc, "constexpr requires integer operands");
2746 break;
2747 case Instruction::FAdd:
2748 case Instruction::FSub:
2749 case Instruction::FMul:
2750 case Instruction::FDiv:
2751 case Instruction::FRem:
2752 if (!Val0->getType()->isFPOrFPVectorTy())
2753 return Error(ID.Loc, "constexpr requires fp operands");
2754 break;
2755 default: llvm_unreachable("Unknown binary operator!");
2756 }
Dan Gohman1b849082009-09-07 23:54:19 +00002757 unsigned Flags = 0;
2758 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2759 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002760 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002761 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002762 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002763 ID.Kind = ValID::t_Constant;
2764 return false;
2765 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002766
Chris Lattnerac161bf2009-01-02 07:01:27 +00002767 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002768 case lltok::kw_and:
2769 case lltok::kw_or:
2770 case lltok::kw_xor: {
2771 unsigned Opc = Lex.getUIntVal();
2772 Constant *Val0, *Val1;
2773 Lex.Lex();
2774 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2775 ParseGlobalTypeAndValue(Val0) ||
2776 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2777 ParseGlobalTypeAndValue(Val1) ||
2778 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2779 return true;
2780 if (Val0->getType() != Val1->getType())
2781 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002782 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002783 return Error(ID.Loc,
2784 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002785 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002786 ID.Kind = ValID::t_Constant;
2787 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002788 }
2789
Chris Lattnerac161bf2009-01-02 07:01:27 +00002790 case lltok::kw_getelementptr:
2791 case lltok::kw_shufflevector:
2792 case lltok::kw_insertelement:
2793 case lltok::kw_extractelement:
2794 case lltok::kw_select: {
2795 unsigned Opc = Lex.getUIntVal();
2796 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002797 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002798 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002799 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002800 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002801 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2802 ParseGlobalValueVector(Elts) ||
2803 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2804 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002805
Chris Lattnerac161bf2009-01-02 07:01:27 +00002806 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002807 if (Elts.size() == 0 ||
2808 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002809 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002810
Jay Foaded8db7d2011-07-21 14:31:17 +00002811 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002812 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002813 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002814 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2815 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002816 } else if (Opc == Instruction::Select) {
2817 if (Elts.size() != 3)
2818 return Error(ID.Loc, "expected three operands to select");
2819 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2820 Elts[2]))
2821 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002822 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002823 } else if (Opc == Instruction::ShuffleVector) {
2824 if (Elts.size() != 3)
2825 return Error(ID.Loc, "expected three operands to shufflevector");
2826 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2827 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002828 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002829 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002830 } else if (Opc == Instruction::ExtractElement) {
2831 if (Elts.size() != 2)
2832 return Error(ID.Loc, "expected two operands to extractelement");
2833 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2834 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002835 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002836 } else {
2837 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2838 if (Elts.size() != 3)
2839 return Error(ID.Loc, "expected three operands to insertelement");
2840 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2841 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002842 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002843 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002844 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002845
Chris Lattnerac161bf2009-01-02 07:01:27 +00002846 ID.Kind = ValID::t_Constant;
2847 return false;
2848 }
2849 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002850
Chris Lattnerac161bf2009-01-02 07:01:27 +00002851 Lex.Lex();
2852 return false;
2853}
2854
2855/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002856bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002857 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002858 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002859 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002860 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002861 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002862 if (V && !(C = dyn_cast<Constant>(V)))
2863 return Error(ID.Loc, "global values must be constants");
2864 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002865}
2866
Victor Hernandez9d75c962010-01-11 22:31:58 +00002867bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002868 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002869 return ParseType(Ty) ||
2870 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002871}
2872
David Majnemerdad0a642014-06-27 18:19:56 +00002873bool LLParser::parseOptionalComdat(Comdat *&C) {
2874 C = nullptr;
2875 if (!EatIfPresent(lltok::kw_comdat))
2876 return false;
2877 if (Lex.getKind() != lltok::ComdatVar)
2878 return TokError("expected comdat variable");
2879 LocTy Loc = Lex.getLoc();
2880 StringRef Name = Lex.getStrVal();
2881 C = getComdat(Name, Loc);
2882 Lex.Lex();
2883 return false;
2884}
2885
Victor Hernandez9d75c962010-01-11 22:31:58 +00002886/// ParseGlobalValueVector
2887/// ::= /*empty*/
2888/// ::= TypeAndValue (',' TypeAndValue)*
2889bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2890 // Empty list.
2891 if (Lex.getKind() == lltok::rbrace ||
2892 Lex.getKind() == lltok::rsquare ||
2893 Lex.getKind() == lltok::greater ||
2894 Lex.getKind() == lltok::rparen)
2895 return false;
2896
2897 Constant *C;
2898 if (ParseGlobalTypeAndValue(C)) return true;
2899 Elts.push_back(C);
2900
2901 while (EatIfPresent(lltok::comma)) {
2902 if (ParseGlobalTypeAndValue(C)) return true;
2903 Elts.push_back(C);
2904 }
2905
2906 return false;
2907}
2908
Dan Gohmanc828c542010-08-24 02:24:03 +00002909bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2910 assert(Lex.getKind() == lltok::lbrace);
2911 Lex.Lex();
2912
2913 SmallVector<Value*, 16> Elts;
2914 if (ParseMDNodeVector(Elts, PFS) ||
2915 ParseToken(lltok::rbrace, "expected end of metadata node"))
2916 return true;
2917
Jay Foad5514afe2011-04-21 19:59:31 +00002918 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002919 ID.Kind = ValID::t_MDNode;
2920 return false;
2921}
2922
Dan Gohman8939ba332010-07-14 18:26:50 +00002923/// ParseMetadataValue
2924/// ::= !42
2925/// ::= !{...}
2926/// ::= !"string"
2927bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2928 assert(Lex.getKind() == lltok::exclaim);
2929 Lex.Lex();
2930
2931 // MDNode:
2932 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002933 if (Lex.getKind() == lltok::lbrace)
2934 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002935
2936 // Standalone metadata reference
2937 // !42
2938 if (Lex.getKind() == lltok::APSInt) {
2939 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2940 ID.Kind = ValID::t_MDNode;
2941 return false;
2942 }
2943
2944 // MDString:
2945 // ::= '!' STRINGCONSTANT
2946 if (ParseMDString(ID.MDStringVal)) return true;
2947 ID.Kind = ValID::t_MDString;
2948 return false;
2949}
2950
Victor Hernandez9d75c962010-01-11 22:31:58 +00002951
2952//===----------------------------------------------------------------------===//
2953// Function Parsing.
2954//===----------------------------------------------------------------------===//
2955
Chris Lattner229907c2011-07-18 04:54:35 +00002956bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002957 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002958 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002959 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002960
Chris Lattnerac161bf2009-01-02 07:01:27 +00002961 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002962 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002963 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2964 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002965 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002966 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002967 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2968 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002969 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002970 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002971 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002972 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002973 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002974 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2975 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002976 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002977 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002978 return false;
2979 }
2980 case ValID::t_MDNode:
2981 if (!Ty->isMetadataTy())
2982 return Error(ID.Loc, "metadata value must have metadata type");
2983 V = ID.MDNodeVal;
2984 return false;
2985 case ValID::t_MDString:
2986 if (!Ty->isMetadataTy())
2987 return Error(ID.Loc, "metadata value must have metadata type");
2988 V = ID.MDStringVal;
2989 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002990 case ValID::t_GlobalName:
2991 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002992 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002993 case ValID::t_GlobalID:
2994 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002995 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002996 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002997 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002998 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002999 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00003000 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003001 return false;
3002 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00003003 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003004 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
3005 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003006
Dan Gohman518cda42011-12-17 00:04:22 +00003007 // The lexer has no type info, so builds all half, float, and double FP
3008 // constants as double. Fix this here. Long double does not need this.
3009 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003010 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00003011 if (Ty->isHalfTy())
3012 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
3013 &Ignored);
3014 else if (Ty->isFloatTy())
3015 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
3016 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003017 }
Owen Anderson69c464d2009-07-27 20:59:43 +00003018 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003019
Chris Lattner8f57d29e2009-01-05 18:24:23 +00003020 if (V->getType() != Ty)
3021 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003022 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003023
Chris Lattnerac161bf2009-01-02 07:01:27 +00003024 return false;
3025 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00003026 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003027 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003028 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003029 return false;
3030 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00003031 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003032 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00003033 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003034 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003035 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00003036 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00003037 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00003038 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003039 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00003040 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003041 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00003042 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00003043 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003044 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00003045 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003046 return false;
3047 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00003048 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003049 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00003050
Chris Lattnerac161bf2009-01-02 07:01:27 +00003051 V = ID.ConstantVal;
3052 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003053 case ValID::t_ConstantStruct:
3054 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00003055 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003056 if (ST->getNumElements() != ID.UIntVal)
3057 return Error(ID.Loc,
3058 "initializer with struct type has wrong # elements");
3059 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
3060 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003061
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003062 // Verify that the elements are compatible with the structtype.
3063 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
3064 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
3065 return Error(ID.Loc, "element " + Twine(i) +
3066 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003067
Frits van Bommel717d7ed2011-07-18 12:00:32 +00003068 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
3069 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003070 } else
3071 return Error(ID.Loc, "constant expression type mismatch");
3072 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003073 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00003074 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003075}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003076
Chris Lattner229907c2011-07-18 04:54:35 +00003077bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003078 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003079 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003080 return ParseValID(ID, PFS) ||
3081 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003082}
3083
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003084bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003085 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003086 return ParseType(Ty) ||
3087 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003088}
3089
Chris Lattner3ed871f2009-10-27 19:13:16 +00003090bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
3091 PerFunctionState &PFS) {
3092 Value *V;
3093 Loc = Lex.getLoc();
3094 if (ParseTypeAndValue(V, PFS)) return true;
3095 if (!isa<BasicBlock>(V))
3096 return Error(Loc, "expected a basic block");
3097 BB = cast<BasicBlock>(V);
3098 return false;
3099}
3100
3101
Chris Lattnerac161bf2009-01-02 07:01:27 +00003102/// FunctionHeader
3103/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00003104/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003105/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00003106bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
3107 // Parse the linkage.
3108 LocTy LinkageLoc = Lex.getLoc();
3109 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003110
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00003111 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00003112 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00003113 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003114 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003115 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003116 LocTy RetTypeLoc = Lex.getLoc();
3117 if (ParseOptionalLinkage(Linkage) ||
3118 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003119 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003120 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003121 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003122 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003123 return true;
3124
3125 // Verify that the linkage is ok.
3126 switch ((GlobalValue::LinkageTypes)Linkage) {
3127 case GlobalValue::ExternalLinkage:
3128 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003129 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003130 if (isDefine)
3131 return Error(LinkageLoc, "invalid linkage for function definition");
3132 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003133 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003134 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003135 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003136 case GlobalValue::LinkOnceAnyLinkage:
3137 case GlobalValue::LinkOnceODRLinkage:
3138 case GlobalValue::WeakAnyLinkage:
3139 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003140 if (!isDefine)
3141 return Error(LinkageLoc, "invalid linkage for function declaration");
3142 break;
3143 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003144 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003145 return Error(LinkageLoc, "invalid function linkage type");
3146 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003147
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003148 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3149 return Error(LinkageLoc,
3150 "symbol with local linkage must have default visibility");
3151
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003152 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003153 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003154
Chris Lattnerac161bf2009-01-02 07:01:27 +00003155 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003156
3157 std::string FunctionName;
3158 if (Lex.getKind() == lltok::GlobalVar) {
3159 FunctionName = Lex.getStrVal();
3160 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3161 unsigned NameID = Lex.getUIntVal();
3162
3163 if (NameID != NumberedVals.size())
3164 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003165 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003166 } else {
3167 return TokError("expected function name");
3168 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003169
Chris Lattner3822f632009-01-02 08:05:26 +00003170 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003171
Chris Lattner3822f632009-01-02 08:05:26 +00003172 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003173 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003174
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003175 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003176 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003177 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003178 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003179 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003180 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003181 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003182 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003183 bool UnnamedAddr;
3184 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003185 Constant *Prefix = nullptr;
David Majnemerdad0a642014-06-27 18:19:56 +00003186 Comdat *C;
Chris Lattner3822f632009-01-02 08:05:26 +00003187
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003188 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003189 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3190 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003191 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003192 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003193 (EatIfPresent(lltok::kw_section) &&
3194 ParseStringConstant(Section)) ||
David Majnemerdad0a642014-06-27 18:19:56 +00003195 parseOptionalComdat(C) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003196 ParseOptionalAlignment(Alignment) ||
3197 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003198 ParseStringConstant(GC)) ||
3199 (EatIfPresent(lltok::kw_prefix) &&
3200 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003201 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003202
Michael Gottesman41748d72013-06-27 00:25:01 +00003203 if (FuncAttrs.contains(Attribute::Builtin))
3204 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003205
Chris Lattnerac161bf2009-01-02 07:01:27 +00003206 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003207 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003208 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003209 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003210 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003211
Chris Lattnerac161bf2009-01-02 07:01:27 +00003212 // Okay, if we got here, the function is syntactically valid. Convert types
3213 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003214 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003215 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003216
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003217 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003218 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3219 AttributeSet::ReturnIndex,
3220 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003221
Chris Lattnerac161bf2009-01-02 07:01:27 +00003222 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003223 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003224 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3225 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003226 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3227 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003228 }
3229
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003230 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003231 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3232 AttributeSet::FunctionIndex,
3233 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003234
Bill Wendlinge94d8432012-12-07 23:16:57 +00003235 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003236
Bill Wendling749a43d2012-12-30 13:50:49 +00003237 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003238 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3239
Chris Lattner229907c2011-07-18 04:54:35 +00003240 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003241 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003242 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003243
Craig Topper2617dcc2014-04-15 06:32:26 +00003244 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003245 if (!FunctionName.empty()) {
3246 // If this was a definition of a forward reference, remove the definition
3247 // from the forward reference table and fill in the forward ref.
3248 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3249 ForwardRefVals.find(FunctionName);
3250 if (FRVI != ForwardRefVals.end()) {
3251 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003252 if (!Fn)
3253 return Error(FRVI->second.second, "invalid forward reference to "
3254 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003255 if (Fn->getType() != PFT)
3256 return Error(FRVI->second.second, "invalid forward reference to "
3257 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003258
Chris Lattnerac161bf2009-01-02 07:01:27 +00003259 ForwardRefVals.erase(FRVI);
3260 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003261 // Reject redefinitions.
3262 return Error(NameLoc, "invalid redefinition of function '" +
3263 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003264 } else if (M->getNamedValue(FunctionName)) {
3265 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003266 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003267
Dan Gohman399d6ae2009-08-29 23:37:49 +00003268 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003269 // If this is a definition of a forward referenced function, make sure the
3270 // types agree.
3271 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3272 = ForwardRefValIDs.find(NumberedVals.size());
3273 if (I != ForwardRefValIDs.end()) {
3274 Fn = cast<Function>(I->second.first);
3275 if (Fn->getType() != PFT)
3276 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003277 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003278 ForwardRefValIDs.erase(I);
3279 }
3280 }
3281
Craig Topper2617dcc2014-04-15 06:32:26 +00003282 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003283 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3284 else // Move the forward-reference to the correct spot in the module.
3285 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3286
3287 if (FunctionName.empty())
3288 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003289
Chris Lattnerac161bf2009-01-02 07:01:27 +00003290 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3291 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003292 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003293 Fn->setCallingConv(CC);
3294 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003295 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003296 Fn->setAlignment(Alignment);
3297 Fn->setSection(Section);
David Majnemerdad0a642014-06-27 18:19:56 +00003298 Fn->setComdat(C);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003299 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003300 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003301 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003302
Chris Lattnerac161bf2009-01-02 07:01:27 +00003303 // Add all of the arguments we parsed to the function.
3304 Function::arg_iterator ArgIt = Fn->arg_begin();
3305 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3306 // If the argument has a name, insert it into the argument symbol table.
3307 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003308
Chris Lattnerac161bf2009-01-02 07:01:27 +00003309 // Set the name, if it conflicted, it will be auto-renamed.
3310 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003311
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003312 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003313 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3314 ArgList[i].Name + "'");
3315 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003316
Chris Lattnerac161bf2009-01-02 07:01:27 +00003317 return false;
3318}
3319
3320
3321/// ParseFunctionBody
3322/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003323///
3324bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003325 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003326 return TokError("expected '{' in function body");
3327 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003328
Chris Lattner3432c622009-10-28 03:39:23 +00003329 int FunctionNumber = -1;
3330 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003331
Chris Lattner3432c622009-10-28 03:39:23 +00003332 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003333
Chris Lattnerbbddd962010-01-09 19:20:07 +00003334 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003335 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003336 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003337
Chris Lattner4649a732011-06-17 06:42:57 +00003338 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003339 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003340
Chris Lattnerac161bf2009-01-02 07:01:27 +00003341 // Eat the }.
3342 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003343
Chris Lattnerac161bf2009-01-02 07:01:27 +00003344 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003345 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003346}
3347
3348/// ParseBasicBlock
3349/// ::= LabelStr? Instruction*
3350bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3351 // If this basic block starts out with a name, remember it.
3352 std::string Name;
3353 LocTy NameLoc = Lex.getLoc();
3354 if (Lex.getKind() == lltok::LabelStr) {
3355 Name = Lex.getStrVal();
3356 Lex.Lex();
3357 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003358
Chris Lattnerac161bf2009-01-02 07:01:27 +00003359 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003360 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003361
Chris Lattnerac161bf2009-01-02 07:01:27 +00003362 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003363
Chris Lattnerac161bf2009-01-02 07:01:27 +00003364 // Parse the instructions in this block until we get a terminator.
3365 Instruction *Inst;
3366 do {
3367 // This instruction may have three possibilities for a name: a) none
3368 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3369 LocTy NameLoc = Lex.getLoc();
3370 int NameID = -1;
3371 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003372
Chris Lattnerac161bf2009-01-02 07:01:27 +00003373 if (Lex.getKind() == lltok::LocalVarID) {
3374 NameID = Lex.getUIntVal();
3375 Lex.Lex();
3376 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3377 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003378 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003379 NameStr = Lex.getStrVal();
3380 Lex.Lex();
3381 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3382 return true;
3383 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003384
Chris Lattner77b89dc2009-12-30 05:23:43 +00003385 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003386 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003387 case InstError: return true;
3388 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003389 BB->getInstList().push_back(Inst);
3390
Chris Lattner77b89dc2009-12-30 05:23:43 +00003391 // With a normal result, we check to see if the instruction is followed by
3392 // a comma and metadata.
3393 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003394 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003395 return true;
3396 break;
3397 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003398 BB->getInstList().push_back(Inst);
3399
Chris Lattner77b89dc2009-12-30 05:23:43 +00003400 // If the instruction parser ate an extra comma at the end of it, it
3401 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003402 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003403 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003404 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003405 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003406
Chris Lattnerac161bf2009-01-02 07:01:27 +00003407 // Set the name on the instruction.
3408 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3409 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003410
Chris Lattnerac161bf2009-01-02 07:01:27 +00003411 return false;
3412}
3413
3414//===----------------------------------------------------------------------===//
3415// Instruction Parsing.
3416//===----------------------------------------------------------------------===//
3417
3418/// ParseInstruction - Parse one of the many different instructions.
3419///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003420int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3421 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003422 lltok::Kind Token = Lex.getKind();
3423 if (Token == lltok::Eof)
3424 return TokError("found end of file when expecting more instructions");
3425 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003426 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003427 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003428
Chris Lattnerac161bf2009-01-02 07:01:27 +00003429 switch (Token) {
3430 default: return Error(Loc, "expected instruction opcode");
3431 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003432 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003433 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3434 case lltok::kw_br: return ParseBr(Inst, PFS);
3435 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003436 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003437 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003438 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003439 // Binary Operators.
3440 case lltok::kw_add:
3441 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003442 case lltok::kw_mul:
3443 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003444 bool NUW = EatIfPresent(lltok::kw_nuw);
3445 bool NSW = EatIfPresent(lltok::kw_nsw);
3446 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003447
Chris Lattnera676c0f2011-02-07 16:40:21 +00003448 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003449
Chris Lattnera676c0f2011-02-07 16:40:21 +00003450 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3451 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3452 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003453 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003454 case lltok::kw_fadd:
3455 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003456 case lltok::kw_fmul:
3457 case lltok::kw_fdiv:
3458 case lltok::kw_frem: {
3459 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3460 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3461 if (Res != 0)
3462 return Res;
3463 if (FMF.any())
3464 Inst->setFastMathFlags(FMF);
3465 return 0;
3466 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003467
Chris Lattner35315d02011-02-06 21:44:57 +00003468 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003469 case lltok::kw_udiv:
3470 case lltok::kw_lshr:
3471 case lltok::kw_ashr: {
3472 bool Exact = EatIfPresent(lltok::kw_exact);
3473
3474 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3475 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3476 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003477 }
3478
Chris Lattnerac161bf2009-01-02 07:01:27 +00003479 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003480 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003481 case lltok::kw_and:
3482 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003483 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003484 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003485 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003486 // Casts.
3487 case lltok::kw_trunc:
3488 case lltok::kw_zext:
3489 case lltok::kw_sext:
3490 case lltok::kw_fptrunc:
3491 case lltok::kw_fpext:
3492 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003493 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003494 case lltok::kw_uitofp:
3495 case lltok::kw_sitofp:
3496 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003497 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003498 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003499 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003500 // Other.
3501 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003502 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003503 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3504 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3505 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3506 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003507 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003508 // Call.
3509 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3510 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3511 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003512 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003513 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003514 case lltok::kw_load: return ParseLoad(Inst, PFS);
3515 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003516 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3517 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003518 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003519 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3520 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3521 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3522 }
3523}
3524
3525/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3526bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003527 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003528 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003529 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003530 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3531 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3532 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3533 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3534 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3535 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3536 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3537 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3538 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3539 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3540 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3541 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3542 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3543 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3544 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3545 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3546 }
3547 } else {
3548 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003549 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003550 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3551 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3552 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3553 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3554 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3555 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3556 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3557 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3558 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3559 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3560 }
3561 }
3562 Lex.Lex();
3563 return false;
3564}
3565
3566//===----------------------------------------------------------------------===//
3567// Terminator Instructions.
3568//===----------------------------------------------------------------------===//
3569
3570/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003571/// ::= 'ret' void (',' !dbg, !1)*
3572/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003573bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003574 PerFunctionState &PFS) {
3575 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003576 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003577 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003578
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003579 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003580
Chris Lattnerfdd87902009-10-05 05:54:46 +00003581 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003582 if (!ResType->isVoidTy())
3583 return Error(TypeLoc, "value doesn't match function result type '" +
3584 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003585
Owen Anderson55f1c092009-08-13 21:58:54 +00003586 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003587 return false;
3588 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003589
Chris Lattnerac161bf2009-01-02 07:01:27 +00003590 Value *RV;
3591 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003592
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003593 if (ResType != RV->getType())
3594 return Error(TypeLoc, "value doesn't match function result type '" +
3595 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003596
Owen Anderson55f1c092009-08-13 21:58:54 +00003597 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003598 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003599}
3600
3601
3602/// ParseBr
3603/// ::= 'br' TypeAndValue
3604/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3605bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3606 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003607 Value *Op0;
3608 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003609 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003610
Chris Lattnerac161bf2009-01-02 07:01:27 +00003611 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3612 Inst = BranchInst::Create(BB);
3613 return false;
3614 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003615
Owen Anderson55f1c092009-08-13 21:58:54 +00003616 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003617 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003618
Chris Lattnerac161bf2009-01-02 07:01:27 +00003619 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003620 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003621 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003622 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003623 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003624
Chris Lattner3ed871f2009-10-27 19:13:16 +00003625 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003626 return false;
3627}
3628
3629/// ParseSwitch
3630/// Instruction
3631/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3632/// JumpTable
3633/// ::= (TypeAndValue ',' TypeAndValue)*
3634bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3635 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003636 Value *Cond;
3637 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003638 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3639 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003640 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003641 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3642 return true;
3643
Duncan Sands19d0b472010-02-16 11:11:14 +00003644 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003645 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003646
Chris Lattnerac161bf2009-01-02 07:01:27 +00003647 // Parse the jump table pairs.
3648 SmallPtrSet<Value*, 32> SeenCases;
3649 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3650 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003651 Value *Constant;
3652 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003653
Chris Lattnerac161bf2009-01-02 07:01:27 +00003654 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3655 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003656 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003657 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003658
Chris Lattnerac161bf2009-01-02 07:01:27 +00003659 if (!SeenCases.insert(Constant))
3660 return Error(CondLoc, "duplicate case value in switch");
3661 if (!isa<ConstantInt>(Constant))
3662 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003663
Chris Lattner3ed871f2009-10-27 19:13:16 +00003664 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003665 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003666
Chris Lattnerac161bf2009-01-02 07:01:27 +00003667 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003668
Chris Lattner3ed871f2009-10-27 19:13:16 +00003669 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003670 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3671 SI->addCase(Table[i].first, Table[i].second);
3672 Inst = SI;
3673 return false;
3674}
3675
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003676/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003677/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003678/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3679bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003680 LocTy AddrLoc;
3681 Value *Address;
3682 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003683 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3684 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003685 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003686
Duncan Sands19d0b472010-02-16 11:11:14 +00003687 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003688 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003689
Chris Lattner3ed871f2009-10-27 19:13:16 +00003690 // Parse the destination list.
3691 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003692
Chris Lattner3ed871f2009-10-27 19:13:16 +00003693 if (Lex.getKind() != lltok::rsquare) {
3694 BasicBlock *DestBB;
3695 if (ParseTypeAndBasicBlock(DestBB, PFS))
3696 return true;
3697 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003698
Chris Lattner3ed871f2009-10-27 19:13:16 +00003699 while (EatIfPresent(lltok::comma)) {
3700 if (ParseTypeAndBasicBlock(DestBB, PFS))
3701 return true;
3702 DestList.push_back(DestBB);
3703 }
3704 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003705
Chris Lattner3ed871f2009-10-27 19:13:16 +00003706 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3707 return true;
3708
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003709 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003710 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3711 IBI->addDestination(DestList[i]);
3712 Inst = IBI;
3713 return false;
3714}
3715
3716
Chris Lattnerac161bf2009-01-02 07:01:27 +00003717/// ParseInvoke
3718/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3719/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3720bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3721 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003722 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003723 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003724 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003725 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003726 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003727 LocTy RetTypeLoc;
3728 ValID CalleeID;
3729 SmallVector<ParamInfo, 16> ArgList;
3730
Chris Lattner3ed871f2009-10-27 19:13:16 +00003731 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003732 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003733 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003734 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003735 ParseValID(CalleeID) ||
3736 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003737 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3738 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003739 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003740 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003741 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003742 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003743 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003744
Chris Lattnerac161bf2009-01-02 07:01:27 +00003745 // If RetType is a non-function pointer type, then this is the short syntax
3746 // for the call, which means that RetType is just the return type. Infer the
3747 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003748 PointerType *PFTy = nullptr;
3749 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003750 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3751 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3752 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003753 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003754 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3755 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003756
Chris Lattnerac161bf2009-01-02 07:01:27 +00003757 if (!FunctionType::isValidReturnType(RetType))
3758 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003759
Owen Anderson4056ca92009-07-29 22:17:13 +00003760 Ty = FunctionType::get(RetType, ParamTypes, false);
3761 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003762 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003763
Chris Lattnerac161bf2009-01-02 07:01:27 +00003764 // Look up the callee.
3765 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003766 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003767
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003768 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003769 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003770 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003771 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3772 AttributeSet::ReturnIndex,
3773 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003774
Chris Lattnerac161bf2009-01-02 07:01:27 +00003775 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003776
Chris Lattnerac161bf2009-01-02 07:01:27 +00003777 // Loop through FunctionType's arguments and ensure they are specified
3778 // correctly. Also, gather any parameter attributes.
3779 FunctionType::param_iterator I = Ty->param_begin();
3780 FunctionType::param_iterator E = Ty->param_end();
3781 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003782 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003783 if (I != E) {
3784 ExpectedTy = *I++;
3785 } else if (!Ty->isVarArg()) {
3786 return Error(ArgList[i].Loc, "too many arguments specified");
3787 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003788
Chris Lattnerac161bf2009-01-02 07:01:27 +00003789 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3790 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003791 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003792 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003793 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3794 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003795 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3796 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003797 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003798
Chris Lattnerac161bf2009-01-02 07:01:27 +00003799 if (I != E)
3800 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003801
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003802 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003803 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3804 AttributeSet::FunctionIndex,
3805 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003806
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003807 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003808 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003809
Jay Foad5bd375a2011-07-15 08:37:34 +00003810 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003811 II->setCallingConv(CC);
3812 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003813 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003814 Inst = II;
3815 return false;
3816}
3817
Bill Wendlingf891bf82011-07-31 06:30:59 +00003818/// ParseResume
3819/// ::= 'resume' TypeAndValue
3820bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3821 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003822 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3823 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003824
Bill Wendlingf891bf82011-07-31 06:30:59 +00003825 ResumeInst *RI = ResumeInst::Create(Exn);
3826 Inst = RI;
3827 return false;
3828}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003829
3830//===----------------------------------------------------------------------===//
3831// Binary Operators.
3832//===----------------------------------------------------------------------===//
3833
3834/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003835/// ::= ArithmeticOps TypeAndValue ',' Value
3836///
3837/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3838/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003839bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003840 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003841 LocTy Loc; Value *LHS, *RHS;
3842 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3843 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3844 ParseValue(LHS->getType(), RHS, PFS))
3845 return true;
3846
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003847 bool Valid;
3848 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003849 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003850 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003851 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3852 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003853 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003854 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3855 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003856 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003857
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003858 if (!Valid)
3859 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003860
Chris Lattnerac161bf2009-01-02 07:01:27 +00003861 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3862 return false;
3863}
3864
3865/// ParseLogical
3866/// ::= ArithmeticOps TypeAndValue ',' Value {
3867bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3868 unsigned Opc) {
3869 LocTy Loc; Value *LHS, *RHS;
3870 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3871 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3872 ParseValue(LHS->getType(), RHS, PFS))
3873 return true;
3874
Duncan Sands9dff9be2010-02-15 16:12:20 +00003875 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003876 return Error(Loc,"instruction requires integer or integer vector operands");
3877
3878 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3879 return false;
3880}
3881
3882
3883/// ParseCompare
3884/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3885/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003886bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3887 unsigned Opc) {
3888 // Parse the integer/fp comparison predicate.
3889 LocTy Loc;
3890 unsigned Pred;
3891 Value *LHS, *RHS;
3892 if (ParseCmpPredicate(Pred, Opc) ||
3893 ParseTypeAndValue(LHS, Loc, PFS) ||
3894 ParseToken(lltok::comma, "expected ',' after compare value") ||
3895 ParseValue(LHS->getType(), RHS, PFS))
3896 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003897
Chris Lattnerac161bf2009-01-02 07:01:27 +00003898 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003899 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003900 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003901 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003902 } else {
3903 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003904 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003905 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003906 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003907 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003908 }
3909 return false;
3910}
3911
3912//===----------------------------------------------------------------------===//
3913// Other Instructions.
3914//===----------------------------------------------------------------------===//
3915
3916
3917/// ParseCast
3918/// ::= CastOpc TypeAndValue 'to' Type
3919bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3920 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003921 LocTy Loc;
3922 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003923 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003924 if (ParseTypeAndValue(Op, Loc, PFS) ||
3925 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3926 ParseType(DestTy))
3927 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003928
Chris Lattner89d856e2009-03-01 00:53:13 +00003929 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3930 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003931 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003932 getTypeString(Op->getType()) + "' to '" +
3933 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003934 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003935 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3936 return false;
3937}
3938
3939/// ParseSelect
3940/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3941bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3942 LocTy Loc;
3943 Value *Op0, *Op1, *Op2;
3944 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3945 ParseToken(lltok::comma, "expected ',' after select condition") ||
3946 ParseTypeAndValue(Op1, PFS) ||
3947 ParseToken(lltok::comma, "expected ',' after select value") ||
3948 ParseTypeAndValue(Op2, PFS))
3949 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003950
Chris Lattnerac161bf2009-01-02 07:01:27 +00003951 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3952 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003953
Chris Lattnerac161bf2009-01-02 07:01:27 +00003954 Inst = SelectInst::Create(Op0, Op1, Op2);
3955 return false;
3956}
3957
Chris Lattnerb55ab542009-01-05 08:18:44 +00003958/// ParseVA_Arg
3959/// ::= 'va_arg' TypeAndValue ',' Type
3960bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003961 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003962 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003963 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003964 if (ParseTypeAndValue(Op, PFS) ||
3965 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003966 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003967 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003968
Chris Lattnerb55ab542009-01-05 08:18:44 +00003969 if (!EltTy->isFirstClassType())
3970 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003971
3972 Inst = new VAArgInst(Op, EltTy);
3973 return false;
3974}
3975
3976/// ParseExtractElement
3977/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3978bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3979 LocTy Loc;
3980 Value *Op0, *Op1;
3981 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3982 ParseToken(lltok::comma, "expected ',' after extract value") ||
3983 ParseTypeAndValue(Op1, PFS))
3984 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003985
Chris Lattnerac161bf2009-01-02 07:01:27 +00003986 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3987 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003988
Eric Christopherc9742252009-07-25 02:28:41 +00003989 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003990 return false;
3991}
3992
3993/// ParseInsertElement
3994/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3995bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3996 LocTy Loc;
3997 Value *Op0, *Op1, *Op2;
3998 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3999 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
4000 ParseTypeAndValue(Op1, PFS) ||
4001 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
4002 ParseTypeAndValue(Op2, PFS))
4003 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004004
Chris Lattnerac161bf2009-01-02 07:01:27 +00004005 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00004006 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004007
Chris Lattnerac161bf2009-01-02 07:01:27 +00004008 Inst = InsertElementInst::Create(Op0, Op1, Op2);
4009 return false;
4010}
4011
4012/// ParseShuffleVector
4013/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4014bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
4015 LocTy Loc;
4016 Value *Op0, *Op1, *Op2;
4017 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4018 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
4019 ParseTypeAndValue(Op1, PFS) ||
4020 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
4021 ParseTypeAndValue(Op2, PFS))
4022 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004023
Chris Lattnerac161bf2009-01-02 07:01:27 +00004024 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00004025 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004026
Chris Lattnerac161bf2009-01-02 07:01:27 +00004027 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
4028 return false;
4029}
4030
4031/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00004032/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004033int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004034 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004035 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004036
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004037 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004038 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
4039 ParseValue(Ty, Op0, PFS) ||
4040 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00004041 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004042 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
4043 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004044
Chris Lattnerf4f03422009-12-30 05:27:33 +00004045 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004046 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
4047 while (1) {
4048 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004049
Chris Lattner3822f632009-01-02 08:05:26 +00004050 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004051 break;
4052
Chris Lattnerf4f03422009-12-30 05:27:33 +00004053 if (Lex.getKind() == lltok::MetadataVar) {
4054 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00004055 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004056 }
Devang Patel8f842d32009-10-16 18:45:49 +00004057
Chris Lattner3822f632009-01-02 08:05:26 +00004058 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004059 ParseValue(Ty, Op0, PFS) ||
4060 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00004061 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004062 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
4063 return true;
4064 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004065
Chris Lattnerac161bf2009-01-02 07:01:27 +00004066 if (!Ty->isFirstClassType())
4067 return Error(TypeLoc, "phi node must have first class type");
4068
Jay Foad52131342011-03-30 11:28:46 +00004069 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00004070 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
4071 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
4072 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004073 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004074}
4075
Bill Wendlingfae14752011-08-12 20:24:12 +00004076/// ParseLandingPad
4077/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
4078/// Clause
4079/// ::= 'catch' TypeAndValue
4080/// ::= 'filter'
4081/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
4082bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004083 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004084 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004085
4086 if (ParseType(Ty, TyLoc) ||
4087 ParseToken(lltok::kw_personality, "expected 'personality'") ||
4088 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
4089 return true;
4090
4091 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
4092 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
4093
4094 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
4095 LandingPadInst::ClauseType CT;
4096 if (EatIfPresent(lltok::kw_catch))
4097 CT = LandingPadInst::Catch;
4098 else if (EatIfPresent(lltok::kw_filter))
4099 CT = LandingPadInst::Filter;
4100 else
4101 return TokError("expected 'catch' or 'filter' clause type");
4102
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004103 Value *V;
4104 LocTy VLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004105 if (ParseTypeAndValue(V, VLoc, PFS)) {
4106 delete LP;
4107 return true;
4108 }
4109
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00004110 // A 'catch' type expects a non-array constant. A filter clause expects an
4111 // array constant.
4112 if (CT == LandingPadInst::Catch) {
4113 if (isa<ArrayType>(V->getType()))
4114 Error(VLoc, "'catch' clause has an invalid type");
4115 } else {
4116 if (!isa<ArrayType>(V->getType()))
4117 Error(VLoc, "'filter' clause has an invalid type");
4118 }
4119
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00004120 LP->addClause(cast<Constant>(V));
Bill Wendlingfae14752011-08-12 20:24:12 +00004121 }
4122
4123 Inst = LP;
4124 return false;
4125}
4126
Chris Lattnerac161bf2009-01-02 07:01:27 +00004127/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004128/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4129/// ParameterList OptionalAttrs
4130/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4131/// ParameterList OptionalAttrs
4132/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004133/// ParameterList OptionalAttrs
4134bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004135 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004136 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004137 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004138 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004139 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004140 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004141 LocTy RetTypeLoc;
4142 ValID CalleeID;
4143 SmallVector<ParamInfo, 16> ArgList;
4144 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004145
Reid Kleckner5772b772014-04-24 20:14:34 +00004146 if ((TCK != CallInst::TCK_None &&
4147 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004148 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004149 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004150 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004151 ParseValID(CalleeID) ||
4152 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004153 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004154 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004155 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004156
Chris Lattnerac161bf2009-01-02 07:01:27 +00004157 // If RetType is a non-function pointer type, then this is the short syntax
4158 // for the call, which means that RetType is just the return type. Infer the
4159 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004160 PointerType *PFTy = nullptr;
4161 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004162 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4163 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4164 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004165 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004166 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4167 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004168
Chris Lattnerac161bf2009-01-02 07:01:27 +00004169 if (!FunctionType::isValidReturnType(RetType))
4170 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004171
Owen Anderson4056ca92009-07-29 22:17:13 +00004172 Ty = FunctionType::get(RetType, ParamTypes, false);
4173 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004174 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004175
Chris Lattnerac161bf2009-01-02 07:01:27 +00004176 // Look up the callee.
4177 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004178 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004179
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004180 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004181 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004182 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004183 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4184 AttributeSet::ReturnIndex,
4185 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004186
Chris Lattnerac161bf2009-01-02 07:01:27 +00004187 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004188
Chris Lattnerac161bf2009-01-02 07:01:27 +00004189 // Loop through FunctionType's arguments and ensure they are specified
4190 // correctly. Also, gather any parameter attributes.
4191 FunctionType::param_iterator I = Ty->param_begin();
4192 FunctionType::param_iterator E = Ty->param_end();
4193 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004194 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004195 if (I != E) {
4196 ExpectedTy = *I++;
4197 } else if (!Ty->isVarArg()) {
4198 return Error(ArgList[i].Loc, "too many arguments specified");
4199 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004200
Chris Lattnerac161bf2009-01-02 07:01:27 +00004201 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4202 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004203 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004204 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004205 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4206 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004207 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4208 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004209 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004210
Chris Lattnerac161bf2009-01-02 07:01:27 +00004211 if (I != E)
4212 return Error(CallLoc, "not enough parameters specified for call");
4213
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004214 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004215 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4216 AttributeSet::FunctionIndex,
4217 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004218
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004219 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004220 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004221
Jay Foad5bd375a2011-07-15 08:37:34 +00004222 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004223 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004224 CI->setCallingConv(CC);
4225 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004226 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004227 Inst = CI;
4228 return false;
4229}
4230
4231//===----------------------------------------------------------------------===//
4232// Memory Instructions.
4233//===----------------------------------------------------------------------===//
4234
4235/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004236/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004237int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004238 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004239 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004240 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004241 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004242
4243 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4244
Chris Lattner3822f632009-01-02 08:05:26 +00004245 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004246
Chris Lattnerb2f39502009-12-30 05:44:30 +00004247 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004248 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004249 if (Lex.getKind() == lltok::kw_align) {
4250 if (ParseOptionalAlignment(Alignment)) return true;
4251 } else if (Lex.getKind() == lltok::MetadataVar) {
4252 AteExtraComma = true;
4253 } else {
4254 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4255 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4256 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004257 }
4258 }
4259
Dan Gohman2140a742010-05-28 01:14:11 +00004260 if (Size && !Size->getType()->isIntegerTy())
4261 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004262
Reid Kleckner436c42e2014-01-17 23:58:17 +00004263 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4264 AI->setUsedWithInAlloca(IsInAlloca);
4265 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004266 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004267}
4268
4269/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004270/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004271/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004272/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004273int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004274 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004275 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004276 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004277 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004278 AtomicOrdering Ordering = NotAtomic;
4279 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004280
4281 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004282 isAtomic = true;
4283 Lex.Lex();
4284 }
4285
Chris Lattnerbc639292011-11-27 06:56:53 +00004286 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004287 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004288 isVolatile = true;
4289 Lex.Lex();
4290 }
4291
Chris Lattnerb2f39502009-12-30 05:44:30 +00004292 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004293 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004294 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4295 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004296
Duncan Sands19d0b472010-02-16 11:11:14 +00004297 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004298 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4299 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004300 if (isAtomic && !Alignment)
4301 return Error(Loc, "atomic load must have explicit non-zero alignment");
4302 if (Ordering == Release || Ordering == AcquireRelease)
4303 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004304
Eli Friedman59b66882011-08-09 23:02:53 +00004305 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004306 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004307}
4308
4309/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004310
4311/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4312/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004313/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004314int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004315 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004316 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004317 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004318 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004319 AtomicOrdering Ordering = NotAtomic;
4320 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004321
4322 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004323 isAtomic = true;
4324 Lex.Lex();
4325 }
4326
Chris Lattnerbc639292011-11-27 06:56:53 +00004327 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004328 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004329 isVolatile = true;
4330 Lex.Lex();
4331 }
4332
Chris Lattnerac161bf2009-01-02 07:01:27 +00004333 if (ParseTypeAndValue(Val, Loc, PFS) ||
4334 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004335 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004336 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004337 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004338 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004339
Duncan Sands19d0b472010-02-16 11:11:14 +00004340 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004341 return Error(PtrLoc, "store operand must be a pointer");
4342 if (!Val->getType()->isFirstClassType())
4343 return Error(Loc, "store operand must be a first class value");
4344 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4345 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004346 if (isAtomic && !Alignment)
4347 return Error(Loc, "atomic store must have explicit non-zero alignment");
4348 if (Ordering == Acquire || Ordering == AcquireRelease)
4349 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004350
Eli Friedman59b66882011-08-09 23:02:53 +00004351 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004352 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004353}
4354
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004355/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00004356/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
4357/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004358int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004359 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4360 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004361 AtomicOrdering SuccessOrdering = NotAtomic;
4362 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004363 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004364 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00004365 bool isWeak = false;
4366
4367 if (EatIfPresent(lltok::kw_weak))
4368 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00004369
4370 if (EatIfPresent(lltok::kw_volatile))
4371 isVolatile = true;
4372
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004373 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4374 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4375 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4376 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4377 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004378 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4379 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004380 return true;
4381
Tim Northovere94a5182014-03-11 10:48:52 +00004382 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004383 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004384 if (SuccessOrdering < FailureOrdering)
4385 return TokError("cmpxchg must be at least as ordered on success as failure");
4386 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4387 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004388 if (!Ptr->getType()->isPointerTy())
4389 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4390 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4391 return Error(CmpLoc, "compare value and pointer type do not match");
4392 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4393 return Error(NewLoc, "new value and pointer type do not match");
4394 if (!New->getType()->isIntegerTy())
4395 return Error(NewLoc, "cmpxchg operand must be an integer");
4396 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4397 if (Size < 8 || (Size & (Size - 1)))
4398 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4399 " integer");
4400
Tim Northover420a2162014-06-13 14:24:07 +00004401 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
4402 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004403 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00004404 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004405 Inst = CXI;
4406 return AteExtraComma ? InstExtraComma : InstNormal;
4407}
4408
4409/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004410/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4411/// 'singlethread'? AtomicOrdering
4412int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004413 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4414 bool AteExtraComma = false;
4415 AtomicOrdering Ordering = NotAtomic;
4416 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004417 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004418 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004419
4420 if (EatIfPresent(lltok::kw_volatile))
4421 isVolatile = true;
4422
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004423 switch (Lex.getKind()) {
4424 default: return TokError("expected binary operation in atomicrmw");
4425 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4426 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4427 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4428 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4429 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4430 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4431 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4432 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4433 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4434 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4435 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4436 }
4437 Lex.Lex(); // Eat the operation.
4438
4439 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4440 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4441 ParseTypeAndValue(Val, ValLoc, PFS) ||
4442 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4443 return true;
4444
4445 if (Ordering == Unordered)
4446 return TokError("atomicrmw cannot be unordered");
4447 if (!Ptr->getType()->isPointerTy())
4448 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4449 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4450 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4451 if (!Val->getType()->isIntegerTy())
4452 return Error(ValLoc, "atomicrmw operand must be an integer");
4453 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4454 if (Size < 8 || (Size & (Size - 1)))
4455 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4456 " integer");
4457
4458 AtomicRMWInst *RMWI =
4459 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4460 RMWI->setVolatile(isVolatile);
4461 Inst = RMWI;
4462 return AteExtraComma ? InstExtraComma : InstNormal;
4463}
4464
Eli Friedmanfee02c62011-07-25 23:16:38 +00004465/// ParseFence
4466/// ::= 'fence' 'singlethread'? AtomicOrdering
4467int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4468 AtomicOrdering Ordering = NotAtomic;
4469 SynchronizationScope Scope = CrossThread;
4470 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4471 return true;
4472
4473 if (Ordering == Unordered)
4474 return TokError("fence cannot be unordered");
4475 if (Ordering == Monotonic)
4476 return TokError("fence cannot be monotonic");
4477
4478 Inst = new FenceInst(Context, Ordering, Scope);
4479 return InstNormal;
4480}
4481
Chris Lattnerac161bf2009-01-02 07:01:27 +00004482/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004483/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004484int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004485 Value *Ptr = nullptr;
4486 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004487 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004488
Dan Gohman16cbbe42009-07-29 15:58:36 +00004489 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004490
Chris Lattner3822f632009-01-02 08:05:26 +00004491 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004492
Eli Benderskyd9806682013-04-22 17:03:42 +00004493 Type *BaseType = Ptr->getType();
4494 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4495 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004496 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004497
Chris Lattnerac161bf2009-01-02 07:01:27 +00004498 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004499 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004500 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004501 if (Lex.getKind() == lltok::MetadataVar) {
4502 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004503 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004504 }
Chris Lattner3822f632009-01-02 08:05:26 +00004505 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004506 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004507 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004508 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4509 return Error(EltLoc, "getelementptr index type missmatch");
4510 if (Val->getType()->isVectorTy()) {
4511 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4512 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4513 if (ValNumEl != PtrNumEl)
4514 return Error(EltLoc,
4515 "getelementptr vector index has a wrong number of elements");
4516 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004517 Indices.push_back(Val);
4518 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004519
Eli Benderskyd9806682013-04-22 17:03:42 +00004520 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4521 return Error(Loc, "base element of getelementptr must be sized");
4522
4523 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004524 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004525 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004526 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004527 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004528 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004529}
4530
4531/// ParseExtractValue
4532/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004533int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004534 Value *Val; LocTy Loc;
4535 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004536 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004537 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004538 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004539 return true;
4540
Chris Lattner392be582010-02-12 20:49:41 +00004541 if (!Val->getType()->isAggregateType())
4542 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004543
Jay Foad57aa6362011-07-13 10:26:04 +00004544 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004545 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004546 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004547 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004548}
4549
4550/// ParseInsertValue
4551/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004552int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004553 Value *Val0, *Val1; LocTy Loc0, Loc1;
4554 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004555 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004556 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4557 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4558 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004559 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004560 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004561
Chris Lattner392be582010-02-12 20:49:41 +00004562 if (!Val0->getType()->isAggregateType())
4563 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004564
Jay Foad57aa6362011-07-13 10:26:04 +00004565 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004566 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004567 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004568 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004569}
Nick Lewycky49f89192009-04-04 07:22:01 +00004570
4571//===----------------------------------------------------------------------===//
4572// Embedded metadata.
4573//===----------------------------------------------------------------------===//
4574
4575/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004576/// ::= Element (',' Element)*
4577/// Element
4578/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004579bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004580 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004581 // Check for an empty list.
4582 if (Lex.getKind() == lltok::rbrace)
4583 return false;
4584
Nick Lewycky49f89192009-04-04 07:22:01 +00004585 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004586 // Null is a special case since it is typeless.
4587 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004588 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004589 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004590 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004591
Craig Topper2617dcc2014-04-15 06:32:26 +00004592 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004593 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004594 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004595 } while (EatIfPresent(lltok::comma));
4596
4597 return false;
4598}