blob: e5813f0d210a73db75d012c31e95d16e0e8a2422 [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the parser class for .ll files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth91065212014-03-05 10:34:14 +000016#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/CallingConv.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/InlineAsm.h"
21#include "llvm/IR/Instructions.h"
Manman Ren209b17c2013-09-28 00:22:27 +000022#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000027#include "llvm/Support/raw_ostream.h"
28using namespace llvm;
29
Chris Lattner229907c2011-07-18 04:54:35 +000030static std::string getTypeString(Type *T) {
Chris Lattner0f214eb2011-06-18 21:18:23 +000031 std::string Result;
32 raw_string_ostream Tmp(Result);
33 Tmp << *T;
34 return Tmp.str();
35}
36
Chris Lattner3822f632009-01-02 08:05:26 +000037/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000038bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000039 // Prime the lexer.
40 Lex.Lex();
41
Chris Lattnerad6f3352009-01-04 20:44:11 +000042 return ParseTopLevelEntities() ||
43 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000044}
45
46/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
47/// module.
48bool LLParser::ValidateEndOfModule() {
Chris Lattner8eff0152010-04-01 05:14:45 +000049 // Handle any instruction metadata forward references.
50 if (!ForwardRefInstMetadata.empty()) {
51 for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
52 I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
53 I != E; ++I) {
54 Instruction *Inst = I->first;
55 const std::vector<MDRef> &MDList = I->second;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000056
Chris Lattner8eff0152010-04-01 05:14:45 +000057 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
58 unsigned SlotNo = MDList[i].MDSlot;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Craig Topper2617dcc2014-04-15 06:32:26 +000060 if (SlotNo >= NumberedMetadata.size() ||
61 NumberedMetadata[SlotNo] == nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +000062 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +000063 Twine(SlotNo) + "'");
Chris Lattner8eff0152010-04-01 05:14:45 +000064 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
65 }
66 }
67 ForwardRefInstMetadata.clear();
68 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000069
Manman Ren209b17c2013-09-28 00:22:27 +000070 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
71 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
72
Bill Wendlingb32b0412013-02-08 06:32:06 +000073 // Handle any function attribute group forward references.
74 for (std::map<Value*, std::vector<unsigned> >::iterator
75 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
76 I != E; ++I) {
77 Value *V = I->first;
78 std::vector<unsigned> &Vec = I->second;
79 AttrBuilder B;
80
81 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
82 VI != VE; ++VI)
83 B.merge(NumberedAttrBuilders[*VI]);
84
85 if (Function *Fn = dyn_cast<Function>(V)) {
86 AttributeSet AS = Fn->getAttributes();
87 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
88 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
89 AS.getFnAttributes());
90
91 FnAttrs.merge(B);
92
93 // If the alignment was parsed as an attribute, move to the alignment
94 // field.
95 if (FnAttrs.hasAlignmentAttr()) {
96 Fn->setAlignment(FnAttrs.getAlignment());
97 FnAttrs.removeAttribute(Attribute::Alignment);
98 }
99
100 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
101 AttributeSet::get(Context,
102 AttributeSet::FunctionIndex,
103 FnAttrs));
104 Fn->setAttributes(AS);
105 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
106 AttributeSet AS = CI->getAttributes();
107 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
108 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
109 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000110 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000111 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
112 AttributeSet::get(Context,
113 AttributeSet::FunctionIndex,
114 FnAttrs));
115 CI->setAttributes(AS);
116 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
117 AttributeSet AS = II->getAttributes();
118 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
119 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
120 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000121 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000122 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
123 AttributeSet::get(Context,
124 AttributeSet::FunctionIndex,
125 FnAttrs));
126 II->setAttributes(AS);
127 } else {
128 llvm_unreachable("invalid object with forward attribute group reference");
129 }
130 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000131
Chris Lattner3432c622009-10-28 03:39:23 +0000132 // If there are entries in ForwardRefBlockAddresses at this point, they are
133 // references after the function was defined. Resolve those now.
134 while (!ForwardRefBlockAddresses.empty()) {
135 // Okay, we are referencing an already-parsed function, resolve them now.
Craig Topper2617dcc2014-04-15 06:32:26 +0000136 Function *TheFn = nullptr;
Chris Lattner3432c622009-10-28 03:39:23 +0000137 const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
138 if (Fn.Kind == ValID::t_GlobalName)
139 TheFn = M->getFunction(Fn.StrVal);
140 else if (Fn.UIntVal < NumberedVals.size())
141 TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000142
Craig Topper2617dcc2014-04-15 06:32:26 +0000143 if (!TheFn)
Chris Lattner3432c622009-10-28 03:39:23 +0000144 return Error(Fn.Loc, "unknown function referenced by blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000145
Chris Lattner3432c622009-10-28 03:39:23 +0000146 // Resolve all these references.
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000147 if (ResolveForwardRefBlockAddresses(TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000148 ForwardRefBlockAddresses.begin()->second,
Craig Topper2617dcc2014-04-15 06:32:26 +0000149 nullptr))
Chris Lattner3432c622009-10-28 03:39:23 +0000150 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000151
Chris Lattner3432c622009-10-28 03:39:23 +0000152 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
153 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000154
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000155 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)
156 if (NumberedTypes[i].second.isValid())
157 return Error(NumberedTypes[i].second,
158 "use of undefined type '%" + Twine(i) + "'");
159
160 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
161 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
162 if (I->second.second.isValid())
163 return Error(I->second.second,
164 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000165
Chris Lattnerac161bf2009-01-02 07:01:27 +0000166 if (!ForwardRefVals.empty())
167 return Error(ForwardRefVals.begin()->second.second,
168 "use of undefined value '@" + ForwardRefVals.begin()->first +
169 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000170
Chris Lattnerac161bf2009-01-02 07:01:27 +0000171 if (!ForwardRefValIDs.empty())
172 return Error(ForwardRefValIDs.begin()->second.second,
173 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000174 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000175
Devang Pateld2541152009-07-08 19:23:54 +0000176 if (!ForwardRefMDNodes.empty())
177 return Error(ForwardRefMDNodes.begin()->second.second,
178 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000179 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000180
Devang Pateld2541152009-07-08 19:23:54 +0000181
Chris Lattnerac161bf2009-01-02 07:01:27 +0000182 // Look for intrinsic functions and CallInst that need to be upgraded
183 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
184 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000185
Manman Ren8b4306c2013-12-02 21:29:56 +0000186 UpgradeDebugInfo(*M);
187
Chris Lattnerac161bf2009-01-02 07:01:27 +0000188 return false;
189}
190
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000191bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000192 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
193 PerFunctionState *PFS) {
194 // Loop over all the references, resolving them.
195 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
196 BasicBlock *Res;
Chris Lattneraa99c942009-11-01 01:27:45 +0000197 if (PFS) {
Chris Lattner3432c622009-10-28 03:39:23 +0000198 if (Refs[i].first.Kind == ValID::t_LocalName)
199 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattneraa99c942009-11-01 01:27:45 +0000200 else
Chris Lattner3432c622009-10-28 03:39:23 +0000201 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
202 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
203 return Error(Refs[i].first.Loc,
Chris Lattnera38a4df2009-11-02 18:28:45 +0000204 "cannot take address of numeric label after the function is defined");
Chris Lattner3432c622009-10-28 03:39:23 +0000205 } else {
206 Res = dyn_cast_or_null<BasicBlock>(
207 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
208 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000209
Craig Topper2617dcc2014-04-15 06:32:26 +0000210 if (!Res)
Chris Lattner3432c622009-10-28 03:39:23 +0000211 return Error(Refs[i].first.Loc,
212 "referenced value is not a basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000213
Chris Lattner3432c622009-10-28 03:39:23 +0000214 // Get the BlockAddress for this and update references to use it.
215 BlockAddress *BA = BlockAddress::get(TheFn, Res);
216 Refs[i].second->replaceAllUsesWith(BA);
217 Refs[i].second->eraseFromParent();
218 }
219 return false;
220}
221
222
Chris Lattnerac161bf2009-01-02 07:01:27 +0000223//===----------------------------------------------------------------------===//
224// Top-Level Entities
225//===----------------------------------------------------------------------===//
226
227bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000228 while (1) {
229 switch (Lex.getKind()) {
230 default: return TokError("expected top-level entity");
231 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000232 case lltok::kw_declare: if (ParseDeclare()) return true; break;
233 case lltok::kw_define: if (ParseDefine()) return true; break;
234 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
235 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000236 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000237 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000238 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000239 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000240 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000241 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000242 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000243
244 // The Global variable production with no name can have many different
245 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000246 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
247 // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000248 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000249 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000250 case lltok::kw_internal: // OptionalLinkage
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +0000251 case lltok::kw_linker_private: // Obsolete OptionalLinkage
252 case lltok::kw_linker_private_weak: // Obsolete OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000253 case lltok::kw_weak: // OptionalLinkage
254 case lltok::kw_weak_odr: // OptionalLinkage
255 case lltok::kw_linkonce: // OptionalLinkage
256 case lltok::kw_linkonce_odr: // OptionalLinkage
257 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000258 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000259 case lltok::kw_extern_weak: // OptionalLinkage
260 case lltok::kw_external: { // OptionalLinkage
Nico Rieck7157bb72014-01-14 15:22:47 +0000261 unsigned Linkage, Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000262 if (ParseOptionalLinkage(Linkage) ||
263 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000264 ParseOptionalDLLStorageClass(DLLStorageClass) ||
265 ParseGlobal("", SMLoc(), Linkage, true, Visibility, DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000266 return true;
267 break;
268 }
269 case lltok::kw_default: // OptionalVisibility
270 case lltok::kw_hidden: // OptionalVisibility
271 case lltok::kw_protected: { // OptionalVisibility
Nico Rieck7157bb72014-01-14 15:22:47 +0000272 unsigned Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000273 if (ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000274 ParseOptionalDLLStorageClass(DLLStorageClass) ||
275 ParseGlobal("", SMLoc(), 0, false, Visibility, DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000276 return true;
277 break;
278 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000279
Chris Lattnerac161bf2009-01-02 07:01:27 +0000280 case lltok::kw_thread_local: // OptionalThreadLocal
281 case lltok::kw_addrspace: // OptionalAddrSpace
282 case lltok::kw_constant: // GlobalType
283 case lltok::kw_global: // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000284 if (ParseGlobal("", SMLoc(), 0, false, 0, 0)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000285 break;
Bill Wendlinga7c38772013-02-09 15:48:49 +0000286
287 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000288 }
289 }
290}
291
292
293/// toplevelentity
294/// ::= 'module' 'asm' STRINGCONSTANT
295bool LLParser::ParseModuleAsm() {
296 assert(Lex.getKind() == lltok::kw_module);
297 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000298
299 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000300 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
301 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000302
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000303 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000304 return false;
305}
306
307/// toplevelentity
308/// ::= 'target' 'triple' '=' STRINGCONSTANT
309/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
310bool LLParser::ParseTargetDefinition() {
311 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000312 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000313 switch (Lex.Lex()) {
314 default: return TokError("unknown target property");
315 case lltok::kw_triple:
316 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000317 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
318 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000319 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000320 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000321 return false;
322 case lltok::kw_datalayout:
323 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000324 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
325 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000326 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000327 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000328 return false;
329 }
330}
331
Bill Wendling706d3d62012-11-28 08:41:48 +0000332/// toplevelentity
333/// ::= 'deplibs' '=' '[' ']'
334/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
335/// FIXME: Remove in 4.0. Currently parse, but ignore.
336bool LLParser::ParseDepLibs() {
337 assert(Lex.getKind() == lltok::kw_deplibs);
338 Lex.Lex();
339 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
340 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
341 return true;
342
343 if (EatIfPresent(lltok::rsquare))
344 return false;
345
346 do {
347 std::string Str;
348 if (ParseStringConstant(Str)) return true;
349 } while (EatIfPresent(lltok::comma));
350
351 return ParseToken(lltok::rsquare, "expected ']' at end of list");
352}
353
Dan Gohman466876b2009-08-12 23:32:33 +0000354/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000355/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000356bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000357 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000358 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000359 Lex.Lex(); // eat LocalVarID;
360
361 if (ParseToken(lltok::equal, "expected '=' after name") ||
362 ParseToken(lltok::kw_type, "expected 'type' after '='"))
363 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000364
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000365 if (TypeID >= NumberedTypes.size())
366 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000367
Craig Topper2617dcc2014-04-15 06:32:26 +0000368 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000369 if (ParseStructDefinition(TypeLoc, "",
370 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000371
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000372 if (!isa<StructType>(Result)) {
373 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
374 if (Entry.first)
375 return Error(TypeLoc, "non-struct types may not be recursive");
376 Entry.first = Result;
377 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000378 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000379
Chris Lattnerac161bf2009-01-02 07:01:27 +0000380 return false;
381}
382
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000383
Chris Lattnerac161bf2009-01-02 07:01:27 +0000384/// toplevelentity
385/// ::= LocalVar '=' 'type' type
386bool LLParser::ParseNamedType() {
387 std::string Name = Lex.getStrVal();
388 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000389 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000390
Chris Lattner3822f632009-01-02 08:05:26 +0000391 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000392 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000393 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000394
Craig Topper2617dcc2014-04-15 06:32:26 +0000395 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000396 if (ParseStructDefinition(NameLoc, Name,
397 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000398
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000399 if (!isa<StructType>(Result)) {
400 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
401 if (Entry.first)
402 return Error(NameLoc, "non-struct types may not be recursive");
403 Entry.first = Result;
404 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000405 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000406
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000407 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000408}
409
410
411/// toplevelentity
412/// ::= 'declare' FunctionHeader
413bool LLParser::ParseDeclare() {
414 assert(Lex.getKind() == lltok::kw_declare);
415 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000416
Chris Lattnerac161bf2009-01-02 07:01:27 +0000417 Function *F;
418 return ParseFunctionHeader(F, false);
419}
420
421/// toplevelentity
422/// ::= 'define' FunctionHeader '{' ...
423bool LLParser::ParseDefine() {
424 assert(Lex.getKind() == lltok::kw_define);
425 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000426
Chris Lattnerac161bf2009-01-02 07:01:27 +0000427 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000428 return ParseFunctionHeader(F, true) ||
429 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000430}
431
Chris Lattner3822f632009-01-02 08:05:26 +0000432/// ParseGlobalType
433/// ::= 'constant'
434/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000435bool LLParser::ParseGlobalType(bool &IsConstant) {
436 if (Lex.getKind() == lltok::kw_constant)
437 IsConstant = true;
438 else if (Lex.getKind() == lltok::kw_global)
439 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000440 else {
441 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000442 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000443 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000444 Lex.Lex();
445 return false;
446}
447
Dan Gohman466876b2009-08-12 23:32:33 +0000448/// ParseUnnamedGlobal:
449/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000450/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
451/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000452/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000453/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
454/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000455bool LLParser::ParseUnnamedGlobal() {
456 unsigned VarID = NumberedVals.size();
457 std::string Name;
458 LocTy NameLoc = Lex.getLoc();
459
460 // Handle the GlobalID form.
461 if (Lex.getKind() == lltok::GlobalID) {
462 if (Lex.getUIntVal() != VarID)
463 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000464 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000465 Lex.Lex(); // eat GlobalID;
466
467 if (ParseToken(lltok::equal, "expected '=' after name"))
468 return true;
469 }
470
471 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000472 unsigned Linkage, Visibility, DLLStorageClass;
Dan Gohman466876b2009-08-12 23:32:33 +0000473 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000474 ParseOptionalVisibility(Visibility) ||
475 ParseOptionalDLLStorageClass(DLLStorageClass))
Dan Gohman466876b2009-08-12 23:32:33 +0000476 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000477
Dan Gohman466876b2009-08-12 23:32:33 +0000478 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000479 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
480 DLLStorageClass);
481 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
Dan Gohman466876b2009-08-12 23:32:33 +0000482}
483
Chris Lattnerac161bf2009-01-02 07:01:27 +0000484/// ParseNamedGlobal:
485/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000486/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
487/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000488bool LLParser::ParseNamedGlobal() {
489 assert(Lex.getKind() == lltok::GlobalVar);
490 LocTy NameLoc = Lex.getLoc();
491 std::string Name = Lex.getStrVal();
492 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000493
Chris Lattnerac161bf2009-01-02 07:01:27 +0000494 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000495 unsigned Linkage, Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000496 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
497 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000498 ParseOptionalVisibility(Visibility) ||
499 ParseOptionalDLLStorageClass(DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000500 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000501
Chris Lattnerac161bf2009-01-02 07:01:27 +0000502 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000503 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
504 DLLStorageClass);
505 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000506}
507
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000508// MDString:
509// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000510bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000511 std::string Str;
512 if (ParseStringConstant(Str)) return true;
Chris Lattner1797fc72009-12-29 21:53:55 +0000513 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000514 return false;
515}
516
517// MDNode:
518// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000519//
520/// This version of ParseMDNodeID returns the slot number and null in the case
521/// of a forward reference.
522bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
523 // !{ ..., !42, ... }
524 if (ParseUInt32(SlotNo)) return true;
525
526 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000527 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000528 Result = NumberedMetadata[SlotNo];
529 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000530 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000531 return false;
532}
533
Chris Lattner6dac02a2009-12-30 04:15:23 +0000534bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000535 // !{ ..., !42, ... }
536 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000537 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000538
Chris Lattner8eff0152010-04-01 05:14:45 +0000539 // If not a forward reference, just return it now.
540 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000541
Chris Lattner8eff0152010-04-01 05:14:45 +0000542 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000543 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000544 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000545
Chris Lattnerfc58af22009-12-30 04:51:58 +0000546 if (NumberedMetadata.size() <= MID)
547 NumberedMetadata.resize(MID+1);
548 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000549 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000550 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000551}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000552
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000553/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000554/// !foo = !{ !1, !2 }
555bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000556 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000557 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000558 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000559
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000560 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000561 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000562 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000563 return true;
564
Dan Gohman2637cc12010-07-21 23:38:33 +0000565 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000566 if (Lex.getKind() != lltok::rbrace)
567 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000568 if (ParseToken(lltok::exclaim, "Expected '!' here"))
569 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000570
Craig Topper2617dcc2014-04-15 06:32:26 +0000571 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000572 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000573 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000574 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000575
576 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
577 return true;
578
Devang Patelbe626972009-07-29 00:34:02 +0000579 return false;
580}
581
Devang Patel39e64d42009-07-01 19:21:12 +0000582/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000583/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000584bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000585 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000586 Lex.Lex();
587 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000588
589 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000590 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000591 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000592 if (ParseUInt32(MetadataID) ||
593 ParseToken(lltok::equal, "expected '=' here") ||
594 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000595 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000596 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000597 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000598 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000599 return true;
600
Jay Foad5514afe2011-04-21 19:59:31 +0000601 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000602
Chris Lattnerfc58af22009-12-30 04:51:58 +0000603 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000604 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000605 FI = ForwardRefMDNodes.find(MetadataID);
606 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000607 MDNode *Temp = FI->second.first;
608 Temp->replaceAllUsesWith(Init);
609 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000610 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000611
Chris Lattnerfc58af22009-12-30 04:51:58 +0000612 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
613 } else {
614 if (MetadataID >= NumberedMetadata.size())
615 NumberedMetadata.resize(MetadataID+1);
616
Craig Topper2617dcc2014-04-15 06:32:26 +0000617 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000618 return TokError("Metadata id is already used");
619 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000620 }
621
Devang Patel39e64d42009-07-01 19:21:12 +0000622 return false;
623}
624
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000625static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
626 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
627 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
628}
629
Chris Lattnerac161bf2009-01-02 07:01:27 +0000630/// ParseAlias:
Nico Rieck7157bb72014-01-14 15:22:47 +0000631/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
632/// OptionalLinkage Aliasee
Chris Lattnerac161bf2009-01-02 07:01:27 +0000633/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000634/// ::= TypeAndValue
635/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
Dan Gohman1639c392009-07-27 21:53:46 +0000636/// ::= 'getelementptr' 'inbounds'? '(' ... ')'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000637///
Nico Rieck7157bb72014-01-14 15:22:47 +0000638/// Everything through DLL storage class has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000639///
640bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Nico Rieck7157bb72014-01-14 15:22:47 +0000641 unsigned Visibility, unsigned DLLStorageClass) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000642 assert(Lex.getKind() == lltok::kw_alias);
643 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000644 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000645 unsigned L;
646 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000647 return true;
648
Rafael Espindola78527052013-10-06 15:10:43 +0000649 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
650
Rafael Espindolacaa43562013-10-09 16:07:32 +0000651 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000652 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000653
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000654 if (!isValidVisibilityForLinkage(Visibility, L))
655 return Error(LinkageLoc,
656 "symbol with local linkage must have default visibility");
657
Chris Lattnerac161bf2009-01-02 07:01:27 +0000658 Constant *Aliasee;
659 LocTy AliaseeLoc = Lex.getLoc();
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000660 if (Lex.getKind() != lltok::kw_bitcast &&
661 Lex.getKind() != lltok::kw_getelementptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000662 if (ParseGlobalTypeAndValue(Aliasee)) return true;
663 } else {
664 // The bitcast dest type is not present, it is implied by the dest type.
665 ValID ID;
666 if (ParseValID(ID)) return true;
667 if (ID.Kind != ValID::t_Constant)
668 return Error(AliaseeLoc, "invalid aliasee");
669 Aliasee = ID.ConstantVal;
670 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000671
Duncan Sands19d0b472010-02-16 11:11:14 +0000672 if (!Aliasee->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +0000673 return Error(AliaseeLoc, "alias must have pointer type");
674
675 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000676 PointerType *PTy = cast<PointerType>(Aliasee->getType());
677 std::unique_ptr<GlobalAlias> GA(
678 new GlobalAlias(PTy->getElementType(), (GlobalValue::LinkageTypes)Linkage,
679 Name, Aliasee, nullptr, PTy->getAddressSpace()));
Chris Lattnerac161bf2009-01-02 07:01:27 +0000680 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000681 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000682
Chris Lattnerac161bf2009-01-02 07:01:27 +0000683 // See if this value already exists in the symbol table. If so, it is either
684 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000685 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000686 // See if this was a redefinition. If so, there is no entry in
687 // ForwardRefVals.
688 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
689 I = ForwardRefVals.find(Name);
690 if (I == ForwardRefVals.end())
691 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
692
693 // Otherwise, this was a definition of forward ref. Verify that types
694 // agree.
695 if (Val->getType() != GA->getType())
696 return Error(NameLoc,
697 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000698
Chris Lattnerac161bf2009-01-02 07:01:27 +0000699 // If they agree, just RAUW the old value with the alias and remove the
700 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000701 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000702 Val->eraseFromParent();
703 ForwardRefVals.erase(I);
704 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000705
Chris Lattnerac161bf2009-01-02 07:01:27 +0000706 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000707 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000708 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000709
Rafael Espindolaaa273822014-05-09 21:49:17 +0000710 // The module owns this now
711 GA.release();
712
Chris Lattnerac161bf2009-01-02 07:01:27 +0000713 return false;
714}
715
716/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000717/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
718/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000719/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000720/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
721/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000722/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000723///
David Majnemerc4ab61c2014-03-09 06:41:58 +0000724/// Everything up to and including OptionalDLLStorageClass has been parsed
725/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000726///
727bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
728 unsigned Linkage, bool HasLinkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000729 unsigned Visibility, unsigned DLLStorageClass) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000730 if (!isValidVisibilityForLinkage(Visibility, Linkage))
731 return Error(NameLoc,
732 "symbol with local linkage must have default visibility");
733
Chris Lattnerac161bf2009-01-02 07:01:27 +0000734 unsigned AddrSpace;
Shuxin Yang2e1890e2013-10-27 03:08:44 +0000735 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000736 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola026d1522011-01-13 01:30:30 +0000737 LocTy UnnamedAddrLoc;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000738 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000739 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000740
Craig Topper2617dcc2014-04-15 06:32:26 +0000741 Type *Ty = nullptr;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000742 if (ParseOptionalThreadLocal(TLM) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000743 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindola026d1522011-01-13 01:30:30 +0000744 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
745 &UnnamedAddrLoc) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000746 ParseOptionalToken(lltok::kw_externally_initialized,
747 IsExternallyInitialized,
748 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000749 ParseGlobalType(IsConstant) ||
750 ParseType(Ty, TyLoc))
751 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000752
Chris Lattnerac161bf2009-01-02 07:01:27 +0000753 // If the linkage is specified and is external, then no initializer is
754 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000755 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000756 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000757 Linkage != GlobalValue::ExternalLinkage)) {
758 if (ParseGlobalValue(Ty, Init))
759 return true;
760 }
761
Duncan Sands19d0b472010-02-16 11:11:14 +0000762 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000763 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000764
Craig Topper2617dcc2014-04-15 06:32:26 +0000765 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000766
767 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000768 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000769 if (GlobalValue *GVal = M->getNamedValue(Name)) {
770 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
771 return Error(NameLoc, "redefinition of global '@" + Name + "'");
772 GV = cast<GlobalVariable>(GVal);
773 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000774 } else {
775 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
776 I = ForwardRefValIDs.find(NumberedVals.size());
777 if (I != ForwardRefValIDs.end()) {
778 GV = cast<GlobalVariable>(I->second.first);
779 ForwardRefValIDs.erase(I);
780 }
781 }
782
Craig Topper2617dcc2014-04-15 06:32:26 +0000783 if (!GV) {
784 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
785 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000786 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000787 } else {
788 if (GV->getType()->getElementType() != Ty)
789 return Error(TyLoc,
790 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000791
Chris Lattnerac161bf2009-01-02 07:01:27 +0000792 // Move the forward-reference to the correct spot in the module.
793 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
794 }
795
796 if (Name.empty())
797 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000798
Chris Lattnerac161bf2009-01-02 07:01:27 +0000799 // Set the parsed properties on the global.
800 if (Init)
801 GV->setInitializer(Init);
802 GV->setConstant(IsConstant);
803 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
804 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000805 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000806 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000807 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000808 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000809
Chris Lattnerac161bf2009-01-02 07:01:27 +0000810 // Parse attributes on the global.
811 while (Lex.getKind() == lltok::comma) {
812 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000813
Chris Lattnerac161bf2009-01-02 07:01:27 +0000814 if (Lex.getKind() == lltok::kw_section) {
815 Lex.Lex();
816 GV->setSection(Lex.getStrVal());
817 if (ParseToken(lltok::StringConstant, "expected global section string"))
818 return true;
819 } else if (Lex.getKind() == lltok::kw_align) {
820 unsigned Alignment;
821 if (ParseOptionalAlignment(Alignment)) return true;
822 GV->setAlignment(Alignment);
823 } else {
824 TokError("unknown global variable property!");
825 }
826 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000827
Chris Lattnerac161bf2009-01-02 07:01:27 +0000828 return false;
829}
830
Bill Wendling63b88192013-02-06 06:52:58 +0000831/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000832/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000833bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000834 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000835 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000836 Lex.Lex();
837
838 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000839 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000840 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000841 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000842 Lex.Lex();
843
844 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000845 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000846 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000847 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000848 ParseToken(lltok::rbrace, "expected end of attribute group"))
849 return true;
850
Bill Wendlingb32b0412013-02-08 06:32:06 +0000851 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000852 return Error(AttrGrpLoc, "attribute group has no attributes");
853
854 return false;
855}
856
Bill Wendling8b0321d2013-02-08 00:52:31 +0000857/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000858/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000859bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
860 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000861 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000862 bool HaveError = false;
863
864 B.clear();
865
Bill Wendling63b88192013-02-06 06:52:58 +0000866 while (true) {
867 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000868 if (Token == lltok::kw_builtin)
869 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000870 switch (Token) {
871 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000872 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000873 return Error(Lex.getLoc(), "unterminated attribute group");
874 case lltok::rbrace:
875 // Finished.
876 return false;
877
Bill Wendlingb32b0412013-02-08 06:32:06 +0000878 case lltok::AttrGrpID: {
879 // Allow a function to reference an attribute group:
880 //
881 // define void @foo() #1 { ... }
882 if (inAttrGrp)
883 HaveError |=
884 Error(Lex.getLoc(),
885 "cannot have an attribute group reference in an attribute group");
886
887 unsigned AttrGrpNum = Lex.getUIntVal();
888 if (inAttrGrp) break;
889
890 // Save the reference to the attribute group. We'll fill it in later.
891 FwdRefAttrGrps.push_back(AttrGrpNum);
892 break;
893 }
Bill Wendling63b88192013-02-06 06:52:58 +0000894 // Target-dependent attributes:
895 case lltok::StringConstant: {
896 std::string Attr = Lex.getStrVal();
897 Lex.Lex();
898 std::string Val;
899 if (EatIfPresent(lltok::equal) &&
900 ParseStringConstant(Val))
901 return true;
902
903 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000904 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000905 }
906
907 // Target-independent attributes:
908 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000909 // As a hack, we allow function alignment to be initially parsed as an
910 // attribute on a function declaration/definition or added to an attribute
911 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000912 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000913 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000914 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000915 if (ParseToken(lltok::equal, "expected '=' here") ||
916 ParseUInt32(Alignment))
917 return true;
918 } else {
919 if (ParseOptionalAlignment(Alignment))
920 return true;
921 }
Bill Wendling63b88192013-02-06 06:52:58 +0000922 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000923 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000924 }
925 case lltok::kw_alignstack: {
926 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000927 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000928 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000929 if (ParseToken(lltok::equal, "expected '=' here") ||
930 ParseUInt32(Alignment))
931 return true;
932 } else {
933 if (ParseOptionalStackAlignment(Alignment))
934 return true;
935 }
Bill Wendling63b88192013-02-06 06:52:58 +0000936 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000937 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000938 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000939 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000940 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000941 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000942 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
943 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
944 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
945 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
946 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
947 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
948 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
949 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
950 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
951 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
952 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000953 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000954 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
955 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
956 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
957 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
958 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
959 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
960 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
961 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
962 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
963 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
964 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000965
966 // Error handling.
967 case lltok::kw_inreg:
968 case lltok::kw_signext:
969 case lltok::kw_zeroext:
970 HaveError |=
971 Error(Lex.getLoc(),
972 "invalid use of attribute on a function");
973 break;
974 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +0000975 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000976 case lltok::kw_nest:
977 case lltok::kw_noalias:
978 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +0000979 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000980 case lltok::kw_sret:
981 HaveError |=
982 Error(Lex.getLoc(),
983 "invalid use of parameter-only attribute on a function");
984 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000985 }
986
987 Lex.Lex();
988 }
989}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000990
991//===----------------------------------------------------------------------===//
992// GlobalValue Reference/Resolution Routines.
993//===----------------------------------------------------------------------===//
994
995/// GetGlobalVal - Get a value with the specified name or ID, creating a
996/// forward reference record if needed. This can return null if the value
997/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000998GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000999 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001000 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001001 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001002 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001003 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001004 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001005
Chris Lattnerac161bf2009-01-02 07:01:27 +00001006 // Look this name up in the normal function symbol table.
1007 GlobalValue *Val =
1008 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001009
Chris Lattnerac161bf2009-01-02 07:01:27 +00001010 // If this is a forward reference for the value, see if we already created a
1011 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001012 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001013 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1014 I = ForwardRefVals.find(Name);
1015 if (I != ForwardRefVals.end())
1016 Val = I->second.first;
1017 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001018
Chris Lattnerac161bf2009-01-02 07:01:27 +00001019 // If we have the value in the symbol table or fwd-ref table, return it.
1020 if (Val) {
1021 if (Val->getType() == Ty) return Val;
1022 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001023 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001024 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001025 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001026
Chris Lattnerac161bf2009-01-02 07:01:27 +00001027 // Otherwise, create a new forward reference for this value and remember it.
1028 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001029 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001030 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001031 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001032 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001033 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1034 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001035 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001036
Chris Lattnerac161bf2009-01-02 07:01:27 +00001037 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1038 return FwdVal;
1039}
1040
Chris Lattner229907c2011-07-18 04:54:35 +00001041GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1042 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001043 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001044 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001045 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001046 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001047
Craig Topper2617dcc2014-04-15 06:32:26 +00001048 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001049
Chris Lattnerac161bf2009-01-02 07:01:27 +00001050 // If this is a forward reference for the value, see if we already created a
1051 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001052 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001053 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1054 I = ForwardRefValIDs.find(ID);
1055 if (I != ForwardRefValIDs.end())
1056 Val = I->second.first;
1057 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001058
Chris Lattnerac161bf2009-01-02 07:01:27 +00001059 // If we have the value in the symbol table or fwd-ref table, return it.
1060 if (Val) {
1061 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001062 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001063 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001064 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001065 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001066
Chris Lattnerac161bf2009-01-02 07:01:27 +00001067 // Otherwise, create a new forward reference for this value and remember it.
1068 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001069 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001070 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001071 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001072 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001073 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001074
Chris Lattnerac161bf2009-01-02 07:01:27 +00001075 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1076 return FwdVal;
1077}
1078
1079
1080//===----------------------------------------------------------------------===//
1081// Helper Routines.
1082//===----------------------------------------------------------------------===//
1083
1084/// ParseToken - If the current token has the specified kind, eat it and return
1085/// success. Otherwise, emit the specified error and return failure.
1086bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1087 if (Lex.getKind() != T)
1088 return TokError(ErrMsg);
1089 Lex.Lex();
1090 return false;
1091}
1092
Chris Lattner3822f632009-01-02 08:05:26 +00001093/// ParseStringConstant
1094/// ::= StringConstant
1095bool LLParser::ParseStringConstant(std::string &Result) {
1096 if (Lex.getKind() != lltok::StringConstant)
1097 return TokError("expected string constant");
1098 Result = Lex.getStrVal();
1099 Lex.Lex();
1100 return false;
1101}
1102
1103/// ParseUInt32
1104/// ::= uint32
1105bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001106 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1107 return TokError("expected integer");
1108 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1109 if (Val64 != unsigned(Val64))
1110 return TokError("expected 32-bit integer (too large)");
1111 Val = Val64;
1112 Lex.Lex();
1113 return false;
1114}
1115
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001116/// ParseTLSModel
1117/// := 'localdynamic'
1118/// := 'initialexec'
1119/// := 'localexec'
1120bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1121 switch (Lex.getKind()) {
1122 default:
1123 return TokError("expected localdynamic, initialexec or localexec");
1124 case lltok::kw_localdynamic:
1125 TLM = GlobalVariable::LocalDynamicTLSModel;
1126 break;
1127 case lltok::kw_initialexec:
1128 TLM = GlobalVariable::InitialExecTLSModel;
1129 break;
1130 case lltok::kw_localexec:
1131 TLM = GlobalVariable::LocalExecTLSModel;
1132 break;
1133 }
1134
1135 Lex.Lex();
1136 return false;
1137}
1138
1139/// ParseOptionalThreadLocal
1140/// := /*empty*/
1141/// := 'thread_local'
1142/// := 'thread_local' '(' tlsmodel ')'
1143bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1144 TLM = GlobalVariable::NotThreadLocal;
1145 if (!EatIfPresent(lltok::kw_thread_local))
1146 return false;
1147
1148 TLM = GlobalVariable::GeneralDynamicTLSModel;
1149 if (Lex.getKind() == lltok::lparen) {
1150 Lex.Lex();
1151 return ParseTLSModel(TLM) ||
1152 ParseToken(lltok::rparen, "expected ')' after thread local model");
1153 }
1154 return false;
1155}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001156
1157/// ParseOptionalAddrSpace
1158/// := /*empty*/
1159/// := 'addrspace' '(' uint32 ')'
1160bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1161 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001162 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001163 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001164 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001165 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001166 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001167}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001168
Bill Wendling34c2eb22012-12-04 23:40:58 +00001169/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1170bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1171 bool HaveError = false;
1172
1173 B.clear();
1174
1175 while (1) {
1176 lltok::Kind Token = Lex.getKind();
1177 switch (Token) {
1178 default: // End of attributes.
1179 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001180 case lltok::kw_align: {
1181 unsigned Alignment;
1182 if (ParseOptionalAlignment(Alignment))
1183 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001184 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001185 continue;
1186 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001187 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001188 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001189 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1190 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1191 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1192 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001193 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1194 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001195 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001196 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1197 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1198 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001199
Stephen Lin7577ed52013-04-20 13:16:13 +00001200 case lltok::kw_alignstack:
1201 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001202 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001203 case lltok::kw_inlinehint:
1204 case lltok::kw_minsize:
1205 case lltok::kw_naked:
1206 case lltok::kw_nobuiltin:
1207 case lltok::kw_noduplicate:
1208 case lltok::kw_noimplicitfloat:
1209 case lltok::kw_noinline:
1210 case lltok::kw_nonlazybind:
1211 case lltok::kw_noredzone:
1212 case lltok::kw_noreturn:
1213 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001214 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001215 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001216 case lltok::kw_returns_twice:
1217 case lltok::kw_sanitize_address:
1218 case lltok::kw_sanitize_memory:
1219 case lltok::kw_sanitize_thread:
1220 case lltok::kw_ssp:
1221 case lltok::kw_sspreq:
1222 case lltok::kw_sspstrong:
1223 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001224 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1225 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001226 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001227
Bill Wendling34c2eb22012-12-04 23:40:58 +00001228 Lex.Lex();
1229 }
1230}
1231
1232/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1233bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1234 bool HaveError = false;
1235
1236 B.clear();
1237
1238 while (1) {
1239 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001240 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001241 default: // End of attributes.
1242 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001243 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1244 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1245 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1246 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001247
Bill Wendling34c2eb22012-12-04 23:40:58 +00001248 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001249 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001250 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001251 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001252 case lltok::kw_nest:
1253 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001254 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001255 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001256 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001257 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001258
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001259 case lltok::kw_alignstack:
1260 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001261 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001262 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001263 case lltok::kw_inlinehint:
1264 case lltok::kw_minsize:
1265 case lltok::kw_naked:
1266 case lltok::kw_nobuiltin:
1267 case lltok::kw_noduplicate:
1268 case lltok::kw_noimplicitfloat:
1269 case lltok::kw_noinline:
1270 case lltok::kw_nonlazybind:
1271 case lltok::kw_noredzone:
1272 case lltok::kw_noreturn:
1273 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001274 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001275 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001276 case lltok::kw_returns_twice:
1277 case lltok::kw_sanitize_address:
1278 case lltok::kw_sanitize_memory:
1279 case lltok::kw_sanitize_thread:
1280 case lltok::kw_ssp:
1281 case lltok::kw_sspreq:
1282 case lltok::kw_sspstrong:
1283 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001284 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001285 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001286
1287 case lltok::kw_readnone:
1288 case lltok::kw_readonly:
1289 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001290 }
1291
Chris Lattnerac161bf2009-01-02 07:01:27 +00001292 Lex.Lex();
1293 }
1294}
1295
1296/// ParseOptionalLinkage
1297/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001298/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001299/// ::= 'internal'
1300/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001301/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001302/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001303/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001304/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001305/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001306/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001307/// ::= 'extern_weak'
1308/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001309///
1310/// Deprecated Values:
1311/// ::= 'linker_private'
1312/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001313bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1314 HasLinkage = false;
1315 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001316 default: Res=GlobalValue::ExternalLinkage; return false;
1317 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001318 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1319 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1320 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1321 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1322 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001323 case lltok::kw_available_externally:
1324 Res = GlobalValue::AvailableExternallyLinkage;
1325 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001326 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001327 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001328 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1329 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001330
1331 case lltok::kw_linker_private:
1332 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001333 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1334 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001335 Lex.Lex();
1336 // treat linker_private and linker_private_weak as PrivateLinkage
1337 Res = GlobalValue::PrivateLinkage;
1338 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001339 }
1340 Lex.Lex();
1341 HasLinkage = true;
1342 return false;
1343}
1344
1345/// ParseOptionalVisibility
1346/// ::= /*empty*/
1347/// ::= 'default'
1348/// ::= 'hidden'
1349/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001350///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001351bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1352 switch (Lex.getKind()) {
1353 default: Res = GlobalValue::DefaultVisibility; return false;
1354 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1355 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1356 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1357 }
1358 Lex.Lex();
1359 return false;
1360}
1361
Nico Rieck7157bb72014-01-14 15:22:47 +00001362/// ParseOptionalDLLStorageClass
1363/// ::= /*empty*/
1364/// ::= 'dllimport'
1365/// ::= 'dllexport'
1366///
1367bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1368 switch (Lex.getKind()) {
1369 default: Res = GlobalValue::DefaultStorageClass; return false;
1370 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1371 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1372 }
1373 Lex.Lex();
1374 return false;
1375}
1376
Chris Lattnerac161bf2009-01-02 07:01:27 +00001377/// ParseOptionalCallingConv
1378/// ::= /*empty*/
1379/// ::= 'ccc'
1380/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001381/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001382/// ::= 'coldcc'
1383/// ::= 'x86_stdcallcc'
1384/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001385/// ::= 'x86_thiscallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001386/// ::= 'arm_apcscc'
1387/// ::= 'arm_aapcscc'
1388/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001389/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001390/// ::= 'ptx_kernel'
1391/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001392/// ::= 'spir_func'
1393/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001394/// ::= 'x86_64_sysvcc'
1395/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001396/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001397/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001398/// ::= 'preserve_mostcc'
1399/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001400/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001401///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001402bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001403 switch (Lex.getKind()) {
1404 default: CC = CallingConv::C; return false;
1405 case lltok::kw_ccc: CC = CallingConv::C; break;
1406 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1407 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1408 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1409 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001410 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001411 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1412 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1413 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001414 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001415 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1416 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001417 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1418 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001419 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001420 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1421 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001422 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001423 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001424 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1425 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001426 case lltok::kw_cc: {
1427 unsigned ArbitraryCC;
1428 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001429 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001430 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001431 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1432 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001433 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001434 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001435
Chris Lattnerac161bf2009-01-02 07:01:27 +00001436 Lex.Lex();
1437 return false;
1438}
1439
Chris Lattner5c427632009-12-30 05:31:19 +00001440/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001441/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001442bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1443 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001444 do {
1445 if (Lex.getKind() != lltok::MetadataVar)
1446 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001447
Chris Lattner596760d2009-12-29 21:25:40 +00001448 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001449 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001450 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001451
Chris Lattner1797fc72009-12-29 21:53:55 +00001452 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001453 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001454
1455 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001456 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001457
Dan Gohmanf0715b12010-08-24 14:35:45 +00001458 // This code is similar to that of ParseMetadataValue, however it needs to
1459 // have special-case code for a forward reference; see the comments on
1460 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1461 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001462 if (Lex.getKind() == lltok::lbrace) {
1463 ValID ID;
1464 if (ParseMetadataListValue(ID, PFS))
1465 return true;
1466 assert(ID.Kind == ValID::t_MDNode);
1467 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001468 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001469 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001470 if (ParseMDNodeID(Node, NodeID))
1471 return true;
1472 if (Node) {
1473 // If we got the node, add it to the instruction.
1474 Inst->setMetadata(MDK, Node);
1475 } else {
1476 MDRef R = { Loc, MDK, NodeID };
1477 // Otherwise, remember that this should be resolved later.
1478 ForwardRefInstMetadata[Inst].push_back(R);
1479 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001480 }
Chris Lattner596760d2009-12-29 21:25:40 +00001481
Manman Ren209b17c2013-09-28 00:22:27 +00001482 if (MDK == LLVMContext::MD_tbaa)
1483 InstsWithTBAATag.push_back(Inst);
1484
Chris Lattner596760d2009-12-29 21:25:40 +00001485 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001486 } while (EatIfPresent(lltok::comma));
1487 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001488}
1489
Chris Lattnerac161bf2009-01-02 07:01:27 +00001490/// ParseOptionalAlignment
1491/// ::= /* empty */
1492/// ::= 'align' 4
1493bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1494 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001495 if (!EatIfPresent(lltok::kw_align))
1496 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001497 LocTy AlignLoc = Lex.getLoc();
1498 if (ParseUInt32(Alignment)) return true;
1499 if (!isPowerOf2_32(Alignment))
1500 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001501 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001502 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001503 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001504}
1505
Chris Lattnerb2f39502009-12-30 05:44:30 +00001506/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001507/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001508/// ::= ',' align 4
1509///
1510/// This returns with AteExtraComma set to true if it ate an excess comma at the
1511/// end.
1512bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1513 bool &AteExtraComma) {
1514 AteExtraComma = false;
1515 while (EatIfPresent(lltok::comma)) {
1516 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001517 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001518 AteExtraComma = true;
1519 return false;
1520 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001521
Chris Lattner95b0ff42010-04-23 00:50:50 +00001522 if (Lex.getKind() != lltok::kw_align)
1523 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001524
Chris Lattner95b0ff42010-04-23 00:50:50 +00001525 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001526 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001527
Devang Patelea8a4b92009-09-17 23:04:48 +00001528 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001529}
1530
Eli Friedmanfee02c62011-07-25 23:16:38 +00001531/// ParseScopeAndOrdering
1532/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1533/// else: ::=
1534///
1535/// This sets Scope and Ordering to the parsed values.
1536bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1537 AtomicOrdering &Ordering) {
1538 if (!isAtomic)
1539 return false;
1540
1541 Scope = CrossThread;
1542 if (EatIfPresent(lltok::kw_singlethread))
1543 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001544
1545 return ParseOrdering(Ordering);
1546}
1547
1548/// ParseOrdering
1549/// ::= AtomicOrdering
1550///
1551/// This sets Ordering to the parsed value.
1552bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001553 switch (Lex.getKind()) {
1554 default: return TokError("Expected ordering on atomic instruction");
1555 case lltok::kw_unordered: Ordering = Unordered; break;
1556 case lltok::kw_monotonic: Ordering = Monotonic; break;
1557 case lltok::kw_acquire: Ordering = Acquire; break;
1558 case lltok::kw_release: Ordering = Release; break;
1559 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1560 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1561 }
1562 Lex.Lex();
1563 return false;
1564}
1565
Charles Davisbe5557e2010-02-12 00:31:15 +00001566/// ParseOptionalStackAlignment
1567/// ::= /* empty */
1568/// ::= 'alignstack' '(' 4 ')'
1569bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1570 Alignment = 0;
1571 if (!EatIfPresent(lltok::kw_alignstack))
1572 return false;
1573 LocTy ParenLoc = Lex.getLoc();
1574 if (!EatIfPresent(lltok::lparen))
1575 return Error(ParenLoc, "expected '('");
1576 LocTy AlignLoc = Lex.getLoc();
1577 if (ParseUInt32(Alignment)) return true;
1578 ParenLoc = Lex.getLoc();
1579 if (!EatIfPresent(lltok::rparen))
1580 return Error(ParenLoc, "expected ')'");
1581 if (!isPowerOf2_32(Alignment))
1582 return Error(AlignLoc, "stack alignment is not a power of two");
1583 return false;
1584}
Devang Patelea8a4b92009-09-17 23:04:48 +00001585
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001586/// ParseIndexList - This parses the index list for an insert/extractvalue
1587/// instruction. This sets AteExtraComma in the case where we eat an extra
1588/// comma at the end of the line and find that it is followed by metadata.
1589/// Clients that don't allow metadata can call the version of this function that
1590/// only takes one argument.
1591///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001592/// ParseIndexList
1593/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001594///
1595bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1596 bool &AteExtraComma) {
1597 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001598
Chris Lattnerac161bf2009-01-02 07:01:27 +00001599 if (Lex.getKind() != lltok::comma)
1600 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001601
Chris Lattner3822f632009-01-02 08:05:26 +00001602 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001603 if (Lex.getKind() == lltok::MetadataVar) {
1604 AteExtraComma = true;
1605 return false;
1606 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001607 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001608 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001609 Indices.push_back(Idx);
1610 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001611
Chris Lattnerac161bf2009-01-02 07:01:27 +00001612 return false;
1613}
1614
1615//===----------------------------------------------------------------------===//
1616// Type Parsing.
1617//===----------------------------------------------------------------------===//
1618
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001619/// ParseType - Parse a type.
1620bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1621 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001622 switch (Lex.getKind()) {
1623 default:
1624 return TokError("expected type");
1625 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001626 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001627 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001628 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001629 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001630 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001631 // Type ::= StructType
1632 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001633 return true;
1634 break;
1635 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001636 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001637 Lex.Lex(); // eat the lsquare.
1638 if (ParseArrayVectorType(Result, false))
1639 return true;
1640 break;
1641 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001642 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001643 Lex.Lex();
1644 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001645 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001646 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001647 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001648 } else if (ParseArrayVectorType(Result, true))
1649 return true;
1650 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001651 case lltok::LocalVar: {
1652 // Type ::= %foo
1653 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001654
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001655 // If the type hasn't been defined yet, create a forward definition and
1656 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001657 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001658 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001659 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001660 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001661 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001662 Lex.Lex();
1663 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001664 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001665
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001666 case lltok::LocalVarID: {
1667 // Type ::= %4
1668 if (Lex.getUIntVal() >= NumberedTypes.size())
1669 NumberedTypes.resize(Lex.getUIntVal()+1);
1670 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001671
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001672 // If the type hasn't been defined yet, create a forward definition and
1673 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001674 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001675 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001676 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001677 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001678 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001679 Lex.Lex();
1680 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001681 }
1682 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001683
1684 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001685 while (1) {
1686 switch (Lex.getKind()) {
1687 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001688 default:
1689 if (!AllowVoid && Result->isVoidTy())
1690 return Error(TypeLoc, "void type only allowed for function results");
1691 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001692
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001693 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001694 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001695 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001696 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001697 if (Result->isVoidTy())
1698 return TokError("pointers to void are invalid - use i8* instead");
1699 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001700 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001701 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001702 Lex.Lex();
1703 break;
1704
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001705 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001706 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001707 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001708 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001709 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001710 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001711 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001712 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001713 unsigned AddrSpace;
1714 if (ParseOptionalAddrSpace(AddrSpace) ||
1715 ParseToken(lltok::star, "expected '*' in address space"))
1716 return true;
1717
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001718 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001719 break;
1720 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001721
Chris Lattnerac161bf2009-01-02 07:01:27 +00001722 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1723 case lltok::lparen:
1724 if (ParseFunctionType(Result))
1725 return true;
1726 break;
1727 }
1728 }
1729}
1730
1731/// ParseParameterList
1732/// ::= '(' ')'
1733/// ::= '(' Arg (',' Arg)* ')'
1734/// Arg
1735/// ::= Type OptionalAttributes Value OptionalAttributes
1736bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1737 PerFunctionState &PFS) {
1738 if (ParseToken(lltok::lparen, "expected '(' in call"))
1739 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001740
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001741 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001742 while (Lex.getKind() != lltok::rparen) {
1743 // If this isn't the first argument, we need a comma.
1744 if (!ArgList.empty() &&
1745 ParseToken(lltok::comma, "expected ',' in argument list"))
1746 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001747
Chris Lattnerac161bf2009-01-02 07:01:27 +00001748 // Parse the argument.
1749 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001750 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001751 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001752 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001753 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001754 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001755
Chris Lattner5b4a9622009-12-30 02:11:14 +00001756 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001757 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001758 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001759 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1760 AttrIndex++,
1761 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001762 }
1763
1764 Lex.Lex(); // Lex the ')'.
1765 return false;
1766}
1767
1768
1769
Chris Lattner2ed06b42009-01-05 18:34:07 +00001770/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001771/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001772/// ::= '(' ArgTypeListI ')'
1773/// ArgTypeListI
1774/// ::= /*empty*/
1775/// ::= '...'
1776/// ::= ArgTypeList ',' '...'
1777/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001778///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001779bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1780 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001781 isVarArg = false;
1782 assert(Lex.getKind() == lltok::lparen);
1783 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001784
Chris Lattnerac161bf2009-01-02 07:01:27 +00001785 if (Lex.getKind() == lltok::rparen) {
1786 // empty
1787 } else if (Lex.getKind() == lltok::dotdotdot) {
1788 isVarArg = true;
1789 Lex.Lex();
1790 } else {
1791 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001792 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001793 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001794 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001795
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001796 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001797 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001798
Chris Lattnerfdd87902009-10-05 05:54:46 +00001799 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001800 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001801
Chris Lattnerdef19492011-06-17 06:36:20 +00001802 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001803 Name = Lex.getStrVal();
1804 Lex.Lex();
1805 }
Chris Lattner3822f632009-01-02 08:05:26 +00001806
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001807 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001808 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001809
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001810 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001811 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001812 AttributeSet::get(ArgTy->getContext(),
1813 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001814
Chris Lattner3822f632009-01-02 08:05:26 +00001815 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001816 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001817 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001818 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001819 break;
1820 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001821
Chris Lattnerac161bf2009-01-02 07:01:27 +00001822 // Otherwise must be an argument type.
1823 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001824 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001825
Chris Lattnerfdd87902009-10-05 05:54:46 +00001826 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001827 return Error(TypeLoc, "argument can not have void type");
1828
Chris Lattnerdef19492011-06-17 06:36:20 +00001829 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001830 Name = Lex.getStrVal();
1831 Lex.Lex();
1832 } else {
1833 Name = "";
1834 }
Chris Lattner3822f632009-01-02 08:05:26 +00001835
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001836 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001837 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001838
Bill Wendlingd079a442012-10-15 04:46:55 +00001839 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001840 AttributeSet::get(ArgTy->getContext(),
1841 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001842 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001843 }
1844 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001845
Chris Lattner3822f632009-01-02 08:05:26 +00001846 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001847}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001848
Chris Lattnerac161bf2009-01-02 07:01:27 +00001849/// ParseFunctionType
1850/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001851bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001852 assert(Lex.getKind() == lltok::lparen);
1853
Chris Lattnerce473c72009-01-05 08:04:33 +00001854 if (!FunctionType::isValidReturnType(Result))
1855 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001856
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001857 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001858 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001859 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001860 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001861
Chris Lattnerac161bf2009-01-02 07:01:27 +00001862 // Reject names on the arguments lists.
1863 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1864 if (!ArgList[i].Name.empty())
1865 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001866 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001867 return Error(ArgList[i].Loc,
1868 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001869 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001870
Jay Foadb804a2b2011-07-12 14:06:48 +00001871 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001872 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001873 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001874
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001875 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001876 return false;
1877}
1878
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001879/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1880/// other structs.
1881bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1882 SmallVector<Type*, 8> Elts;
1883 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001884
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001885 Result = StructType::get(Context, Elts, Packed);
1886 return false;
1887}
1888
1889/// ParseStructDefinition - Parse a struct in a 'type' definition.
1890bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1891 std::pair<Type*, LocTy> &Entry,
1892 Type *&ResultTy) {
1893 // If the type was already defined, diagnose the redefinition.
1894 if (Entry.first && !Entry.second.isValid())
1895 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001896
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001897 // If we have opaque, just return without filling in the definition for the
1898 // struct. This counts as a definition as far as the .ll file goes.
1899 if (EatIfPresent(lltok::kw_opaque)) {
1900 // This type is being defined, so clear the location to indicate this.
1901 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001902
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001903 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001904 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001905 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001906 ResultTy = Entry.first;
1907 return false;
1908 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001909
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001910 // If the type starts with '<', then it is either a packed struct or a vector.
1911 bool isPacked = EatIfPresent(lltok::less);
1912
1913 // If we don't have a struct, then we have a random type alias, which we
1914 // accept for compatibility with old files. These types are not allowed to be
1915 // forward referenced and not allowed to be recursive.
1916 if (Lex.getKind() != lltok::lbrace) {
1917 if (Entry.first)
1918 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001919
Craig Topper2617dcc2014-04-15 06:32:26 +00001920 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001921 if (isPacked)
1922 return ParseArrayVectorType(ResultTy, true);
1923 return ParseType(ResultTy);
1924 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001925
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001926 // This type is being defined, so clear the location to indicate this.
1927 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001928
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001929 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001930 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001931 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001932
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001933 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001934
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001935 SmallVector<Type*, 8> Body;
1936 if (ParseStructBody(Body) ||
1937 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1938 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001939
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001940 STy->setBody(Body, isPacked);
1941 ResultTy = STy;
1942 return false;
1943}
1944
1945
Chris Lattnerac161bf2009-01-02 07:01:27 +00001946/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001947/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001948/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001949/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001950/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001951/// ::= '<' '{' Type (',' Type)* '}' '>'
1952bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001953 assert(Lex.getKind() == lltok::lbrace);
1954 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001955
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001956 // Handle the empty struct.
1957 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001958 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001959
Chris Lattnerf880ca22009-03-09 04:49:14 +00001960 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001961 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001962 if (ParseType(Ty)) return true;
1963 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001964
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001965 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001966 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001967
Chris Lattner3822f632009-01-02 08:05:26 +00001968 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001969 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001970 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001971
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001972 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001973 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001974
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001975 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001976 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001977
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001978 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001979}
1980
1981/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1982/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001983/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00001984/// ::= '[' APSINTVAL 'x' Types ']'
1985/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001986bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001987 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1988 Lex.getAPSIntVal().getBitWidth() > 64)
1989 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001990
Chris Lattnerac161bf2009-01-02 07:01:27 +00001991 LocTy SizeLoc = Lex.getLoc();
1992 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00001993 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001994
Chris Lattner3822f632009-01-02 08:05:26 +00001995 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1996 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001997
1998 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001999 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002000 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002001
Chris Lattner3822f632009-01-02 08:05:26 +00002002 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2003 "expected end of sequential type"))
2004 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002005
Chris Lattnerac161bf2009-01-02 07:01:27 +00002006 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002007 if (Size == 0)
2008 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002009 if ((unsigned)Size != Size)
2010 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002011 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002012 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002013 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002014 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002015 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002016 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002017 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002018 }
2019 return false;
2020}
2021
2022//===----------------------------------------------------------------------===//
2023// Function Semantic Analysis.
2024//===----------------------------------------------------------------------===//
2025
Chris Lattner3432c622009-10-28 03:39:23 +00002026LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2027 int functionNumber)
2028 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002029
2030 // Insert unnamed arguments into the NumberedVals list.
2031 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2032 AI != E; ++AI)
2033 if (!AI->hasName())
2034 NumberedVals.push_back(AI);
2035}
2036
2037LLParser::PerFunctionState::~PerFunctionState() {
2038 // If there were any forward referenced non-basicblock values, delete them.
2039 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2040 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2041 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002042 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002043 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002044 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002045 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002046 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002047
Chris Lattnerac161bf2009-01-02 07:01:27 +00002048 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2049 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2050 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002051 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002052 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002053 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002054 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002055 }
2056}
2057
Chris Lattner3432c622009-10-28 03:39:23 +00002058bool LLParser::PerFunctionState::FinishFunction() {
2059 // Check to see if someone took the address of labels in this block.
2060 if (!P.ForwardRefBlockAddresses.empty()) {
2061 ValID FunctionID;
2062 if (!F.getName().empty()) {
2063 FunctionID.Kind = ValID::t_GlobalName;
2064 FunctionID.StrVal = F.getName();
2065 } else {
2066 FunctionID.Kind = ValID::t_GlobalID;
2067 FunctionID.UIntVal = FunctionNumber;
2068 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002069
Chris Lattner3432c622009-10-28 03:39:23 +00002070 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2071 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2072 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2073 // Resolve all these references.
2074 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2075 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002076
Chris Lattner3432c622009-10-28 03:39:23 +00002077 P.ForwardRefBlockAddresses.erase(FRBAI);
2078 }
2079 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002080
Chris Lattnerac161bf2009-01-02 07:01:27 +00002081 if (!ForwardRefVals.empty())
2082 return P.Error(ForwardRefVals.begin()->second.second,
2083 "use of undefined value '%" + ForwardRefVals.begin()->first +
2084 "'");
2085 if (!ForwardRefValIDs.empty())
2086 return P.Error(ForwardRefValIDs.begin()->second.second,
2087 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002088 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002089 return false;
2090}
2091
2092
2093/// GetVal - Get a value with the specified name or ID, creating a
2094/// forward reference record if needed. This can return null if the value
2095/// exists but does not have the right type.
2096Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002097 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002098 // Look this name up in the normal function symbol table.
2099 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002100
Chris Lattnerac161bf2009-01-02 07:01:27 +00002101 // If this is a forward reference for the value, see if we already created a
2102 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002103 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002104 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2105 I = ForwardRefVals.find(Name);
2106 if (I != ForwardRefVals.end())
2107 Val = I->second.first;
2108 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002109
Chris Lattnerac161bf2009-01-02 07:01:27 +00002110 // If we have the value in the symbol table or fwd-ref table, return it.
2111 if (Val) {
2112 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002113 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002114 P.Error(Loc, "'%" + Name + "' is not a basic block");
2115 else
2116 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002117 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002118 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002119 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002120
Chris Lattnerac161bf2009-01-02 07:01:27 +00002121 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002122 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002123 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002124 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002125 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002126
Chris Lattnerac161bf2009-01-02 07:01:27 +00002127 // Otherwise, create a new forward reference for this value and remember it.
2128 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002129 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002130 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002131 else
2132 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002133
Chris Lattnerac161bf2009-01-02 07:01:27 +00002134 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2135 return FwdVal;
2136}
2137
Chris Lattner229907c2011-07-18 04:54:35 +00002138Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002139 LocTy Loc) {
2140 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002141 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002142
Chris Lattnerac161bf2009-01-02 07:01:27 +00002143 // If this is a forward reference for the value, see if we already created a
2144 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002145 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002146 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2147 I = ForwardRefValIDs.find(ID);
2148 if (I != ForwardRefValIDs.end())
2149 Val = I->second.first;
2150 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002151
Chris Lattnerac161bf2009-01-02 07:01:27 +00002152 // If we have the value in the symbol table or fwd-ref table, return it.
2153 if (Val) {
2154 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002155 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002156 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002157 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002158 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002159 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002160 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002161 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002162
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002163 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002164 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002165 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002166 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002167
Chris Lattnerac161bf2009-01-02 07:01:27 +00002168 // Otherwise, create a new forward reference for this value and remember it.
2169 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002170 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002171 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002172 else
2173 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002174
Chris Lattnerac161bf2009-01-02 07:01:27 +00002175 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2176 return FwdVal;
2177}
2178
2179/// SetInstName - After an instruction is parsed and inserted into its
2180/// basic block, this installs its name.
2181bool LLParser::PerFunctionState::SetInstName(int NameID,
2182 const std::string &NameStr,
2183 LocTy NameLoc, Instruction *Inst) {
2184 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002185 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002186 if (NameID != -1 || !NameStr.empty())
2187 return P.Error(NameLoc, "instructions returning void cannot have a name");
2188 return false;
2189 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002190
Chris Lattnerac161bf2009-01-02 07:01:27 +00002191 // If this was a numbered instruction, verify that the instruction is the
2192 // expected value and resolve any forward references.
2193 if (NameStr.empty()) {
2194 // If neither a name nor an ID was specified, just use the next ID.
2195 if (NameID == -1)
2196 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002197
Chris Lattnerac161bf2009-01-02 07:01:27 +00002198 if (unsigned(NameID) != NumberedVals.size())
2199 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002200 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002201
Chris Lattnerac161bf2009-01-02 07:01:27 +00002202 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2203 ForwardRefValIDs.find(NameID);
2204 if (FI != ForwardRefValIDs.end()) {
2205 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002206 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002207 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002208 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002209 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002210 ForwardRefValIDs.erase(FI);
2211 }
2212
2213 NumberedVals.push_back(Inst);
2214 return false;
2215 }
2216
2217 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2218 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2219 FI = ForwardRefVals.find(NameStr);
2220 if (FI != ForwardRefVals.end()) {
2221 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002222 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002223 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002224 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002225 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002226 ForwardRefVals.erase(FI);
2227 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002228
Chris Lattnerac161bf2009-01-02 07:01:27 +00002229 // Set the name on the instruction.
2230 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002231
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002232 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002233 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002234 NameStr + "'");
2235 return false;
2236}
2237
2238/// GetBB - Get a basic block with the specified name or ID, creating a
2239/// forward reference record if needed.
2240BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2241 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002242 return cast_or_null<BasicBlock>(GetVal(Name,
2243 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002244}
2245
2246BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002247 return cast_or_null<BasicBlock>(GetVal(ID,
2248 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002249}
2250
2251/// DefineBB - Define the specified basic block, which is either named or
2252/// unnamed. If there is an error, this returns null otherwise it returns
2253/// the block being defined.
2254BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2255 LocTy Loc) {
2256 BasicBlock *BB;
2257 if (Name.empty())
2258 BB = GetBB(NumberedVals.size(), Loc);
2259 else
2260 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002261 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002262
Chris Lattnerac161bf2009-01-02 07:01:27 +00002263 // Move the block to the end of the function. Forward ref'd blocks are
2264 // inserted wherever they happen to be referenced.
2265 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002266
Chris Lattnerac161bf2009-01-02 07:01:27 +00002267 // Remove the block from forward ref sets.
2268 if (Name.empty()) {
2269 ForwardRefValIDs.erase(NumberedVals.size());
2270 NumberedVals.push_back(BB);
2271 } else {
2272 // BB forward references are already in the function symbol table.
2273 ForwardRefVals.erase(Name);
2274 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002275
Chris Lattnerac161bf2009-01-02 07:01:27 +00002276 return BB;
2277}
2278
2279//===----------------------------------------------------------------------===//
2280// Constants.
2281//===----------------------------------------------------------------------===//
2282
2283/// ParseValID - Parse an abstract value that doesn't necessarily have a
2284/// type implied. For example, if we parse "4" we don't know what integer type
2285/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002286/// sanity. PFS is used to convert function-local operands of metadata (since
2287/// metadata operands are not just parsed here but also converted to values).
2288/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002289bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002290 ID.Loc = Lex.getLoc();
2291 switch (Lex.getKind()) {
2292 default: return TokError("expected value token");
2293 case lltok::GlobalID: // @42
2294 ID.UIntVal = Lex.getUIntVal();
2295 ID.Kind = ValID::t_GlobalID;
2296 break;
2297 case lltok::GlobalVar: // @foo
2298 ID.StrVal = Lex.getStrVal();
2299 ID.Kind = ValID::t_GlobalName;
2300 break;
2301 case lltok::LocalVarID: // %42
2302 ID.UIntVal = Lex.getUIntVal();
2303 ID.Kind = ValID::t_LocalID;
2304 break;
2305 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002306 ID.StrVal = Lex.getStrVal();
2307 ID.Kind = ValID::t_LocalName;
2308 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002309 case lltok::exclaim: // !42, !{...}, or !"foo"
2310 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002311 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002312 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002313 ID.Kind = ValID::t_APSInt;
2314 break;
2315 case lltok::APFloat:
2316 ID.APFloatVal = Lex.getAPFloatVal();
2317 ID.Kind = ValID::t_APFloat;
2318 break;
2319 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002320 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002321 ID.Kind = ValID::t_Constant;
2322 break;
2323 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002324 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002325 ID.Kind = ValID::t_Constant;
2326 break;
2327 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2328 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2329 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002330
Chris Lattnerac161bf2009-01-02 07:01:27 +00002331 case lltok::lbrace: {
2332 // ValID ::= '{' ConstVector '}'
2333 Lex.Lex();
2334 SmallVector<Constant*, 16> Elts;
2335 if (ParseGlobalValueVector(Elts) ||
2336 ParseToken(lltok::rbrace, "expected end of struct constant"))
2337 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002338
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002339 ID.ConstantStructElts = new Constant*[Elts.size()];
2340 ID.UIntVal = Elts.size();
2341 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2342 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002343 return false;
2344 }
2345 case lltok::less: {
2346 // ValID ::= '<' ConstVector '>' --> Vector.
2347 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2348 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002349 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002350
Chris Lattnerac161bf2009-01-02 07:01:27 +00002351 SmallVector<Constant*, 16> Elts;
2352 LocTy FirstEltLoc = Lex.getLoc();
2353 if (ParseGlobalValueVector(Elts) ||
2354 (isPackedStruct &&
2355 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2356 ParseToken(lltok::greater, "expected end of constant"))
2357 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002358
Chris Lattnerac161bf2009-01-02 07:01:27 +00002359 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002360 ID.ConstantStructElts = new Constant*[Elts.size()];
2361 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2362 ID.UIntVal = Elts.size();
2363 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002364 return false;
2365 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002366
Chris Lattnerac161bf2009-01-02 07:01:27 +00002367 if (Elts.empty())
2368 return Error(ID.Loc, "constant vector must not be empty");
2369
Duncan Sands9dff9be2010-02-15 16:12:20 +00002370 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002371 !Elts[0]->getType()->isFloatingPointTy() &&
2372 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002373 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002374 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002375
Chris Lattnerac161bf2009-01-02 07:01:27 +00002376 // Verify that all the vector elements have the same type.
2377 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2378 if (Elts[i]->getType() != Elts[0]->getType())
2379 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002380 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002381 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002382
Chris Lattner69229312011-02-15 00:14:00 +00002383 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002384 ID.Kind = ValID::t_Constant;
2385 return false;
2386 }
2387 case lltok::lsquare: { // Array Constant
2388 Lex.Lex();
2389 SmallVector<Constant*, 16> Elts;
2390 LocTy FirstEltLoc = Lex.getLoc();
2391 if (ParseGlobalValueVector(Elts) ||
2392 ParseToken(lltok::rsquare, "expected end of array constant"))
2393 return true;
2394
2395 // Handle empty element.
2396 if (Elts.empty()) {
2397 // Use undef instead of an array because it's inconvenient to determine
2398 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002399 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002400 return false;
2401 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002402
Chris Lattnerac161bf2009-01-02 07:01:27 +00002403 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002404 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002405 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002406
Owen Anderson4056ca92009-07-29 22:17:13 +00002407 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002408
Chris Lattnerac161bf2009-01-02 07:01:27 +00002409 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002410 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002411 if (Elts[i]->getType() != Elts[0]->getType())
2412 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002413 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002414 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002415 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002416
Jay Foad83be3612011-06-22 09:24:39 +00002417 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002418 ID.Kind = ValID::t_Constant;
2419 return false;
2420 }
2421 case lltok::kw_c: // c "foo"
2422 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002423 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2424 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002425 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2426 ID.Kind = ValID::t_Constant;
2427 return false;
2428
2429 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002430 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2431 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002432 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002433 Lex.Lex();
2434 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002435 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002436 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002437 ParseStringConstant(ID.StrVal) ||
2438 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002439 ParseToken(lltok::StringConstant, "expected constraint string"))
2440 return true;
2441 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002442 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002443 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002444 ID.Kind = ValID::t_InlineAsm;
2445 return false;
2446 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002447
Chris Lattner3432c622009-10-28 03:39:23 +00002448 case lltok::kw_blockaddress: {
2449 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2450 Lex.Lex();
2451
2452 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002453
Chris Lattner3432c622009-10-28 03:39:23 +00002454 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2455 ParseValID(Fn) ||
2456 ParseToken(lltok::comma, "expected comma in block address expression")||
2457 ParseValID(Label) ||
2458 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2459 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002460
Chris Lattner3432c622009-10-28 03:39:23 +00002461 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2462 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002463 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002464 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002465
Chris Lattner3432c622009-10-28 03:39:23 +00002466 // Make a global variable as a placeholder for this reference.
2467 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2468 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002469 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002470 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2471 ID.ConstantVal = FwdRef;
2472 ID.Kind = ValID::t_Constant;
2473 return false;
2474 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002475
Chris Lattnerac161bf2009-01-02 07:01:27 +00002476 case lltok::kw_trunc:
2477 case lltok::kw_zext:
2478 case lltok::kw_sext:
2479 case lltok::kw_fptrunc:
2480 case lltok::kw_fpext:
2481 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002482 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002483 case lltok::kw_uitofp:
2484 case lltok::kw_sitofp:
2485 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002486 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002487 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002488 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002489 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002490 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002491 Constant *SrcVal;
2492 Lex.Lex();
2493 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2494 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002495 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002496 ParseType(DestTy) ||
2497 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2498 return true;
2499 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2500 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002501 getTypeString(SrcVal->getType()) + "' to '" +
2502 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002503 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002504 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002505 ID.Kind = ValID::t_Constant;
2506 return false;
2507 }
2508 case lltok::kw_extractvalue: {
2509 Lex.Lex();
2510 Constant *Val;
2511 SmallVector<unsigned, 4> Indices;
2512 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2513 ParseGlobalTypeAndValue(Val) ||
2514 ParseIndexList(Indices) ||
2515 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2516 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002517
Chris Lattner392be582010-02-12 20:49:41 +00002518 if (!Val->getType()->isAggregateType())
2519 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002520 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002521 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002522 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002523 ID.Kind = ValID::t_Constant;
2524 return false;
2525 }
2526 case lltok::kw_insertvalue: {
2527 Lex.Lex();
2528 Constant *Val0, *Val1;
2529 SmallVector<unsigned, 4> Indices;
2530 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2531 ParseGlobalTypeAndValue(Val0) ||
2532 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2533 ParseGlobalTypeAndValue(Val1) ||
2534 ParseIndexList(Indices) ||
2535 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2536 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002537 if (!Val0->getType()->isAggregateType())
2538 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002539 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002540 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002541 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002542 ID.Kind = ValID::t_Constant;
2543 return false;
2544 }
2545 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002546 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002547 unsigned PredVal, Opc = Lex.getUIntVal();
2548 Constant *Val0, *Val1;
2549 Lex.Lex();
2550 if (ParseCmpPredicate(PredVal, Opc) ||
2551 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2552 ParseGlobalTypeAndValue(Val0) ||
2553 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2554 ParseGlobalTypeAndValue(Val1) ||
2555 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2556 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002557
Chris Lattnerac161bf2009-01-02 07:01:27 +00002558 if (Val0->getType() != Val1->getType())
2559 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002560
Chris Lattnerac161bf2009-01-02 07:01:27 +00002561 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002562
Chris Lattnerac161bf2009-01-02 07:01:27 +00002563 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002564 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002565 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002566 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002567 } else {
2568 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002569 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002570 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002571 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002572 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002573 }
2574 ID.Kind = ValID::t_Constant;
2575 return false;
2576 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002577
Chris Lattnerac161bf2009-01-02 07:01:27 +00002578 // Binary Operators.
2579 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002580 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002581 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002582 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002583 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002584 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002585 case lltok::kw_udiv:
2586 case lltok::kw_sdiv:
2587 case lltok::kw_fdiv:
2588 case lltok::kw_urem:
2589 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002590 case lltok::kw_frem:
2591 case lltok::kw_shl:
2592 case lltok::kw_lshr:
2593 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002594 bool NUW = false;
2595 bool NSW = false;
2596 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002597 unsigned Opc = Lex.getUIntVal();
2598 Constant *Val0, *Val1;
2599 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002600 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002601 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2602 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002603 if (EatIfPresent(lltok::kw_nuw))
2604 NUW = true;
2605 if (EatIfPresent(lltok::kw_nsw)) {
2606 NSW = true;
2607 if (EatIfPresent(lltok::kw_nuw))
2608 NUW = true;
2609 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002610 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2611 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002612 if (EatIfPresent(lltok::kw_exact))
2613 Exact = true;
2614 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002615 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2616 ParseGlobalTypeAndValue(Val0) ||
2617 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2618 ParseGlobalTypeAndValue(Val1) ||
2619 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2620 return true;
2621 if (Val0->getType() != Val1->getType())
2622 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002623 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002624 if (NUW)
2625 return Error(ModifierLoc, "nuw only applies to integer operations");
2626 if (NSW)
2627 return Error(ModifierLoc, "nsw only applies to integer operations");
2628 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002629 // Check that the type is valid for the operator.
2630 switch (Opc) {
2631 case Instruction::Add:
2632 case Instruction::Sub:
2633 case Instruction::Mul:
2634 case Instruction::UDiv:
2635 case Instruction::SDiv:
2636 case Instruction::URem:
2637 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002638 case Instruction::Shl:
2639 case Instruction::AShr:
2640 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002641 if (!Val0->getType()->isIntOrIntVectorTy())
2642 return Error(ID.Loc, "constexpr requires integer operands");
2643 break;
2644 case Instruction::FAdd:
2645 case Instruction::FSub:
2646 case Instruction::FMul:
2647 case Instruction::FDiv:
2648 case Instruction::FRem:
2649 if (!Val0->getType()->isFPOrFPVectorTy())
2650 return Error(ID.Loc, "constexpr requires fp operands");
2651 break;
2652 default: llvm_unreachable("Unknown binary operator!");
2653 }
Dan Gohman1b849082009-09-07 23:54:19 +00002654 unsigned Flags = 0;
2655 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2656 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002657 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002658 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002659 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002660 ID.Kind = ValID::t_Constant;
2661 return false;
2662 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002663
Chris Lattnerac161bf2009-01-02 07:01:27 +00002664 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002665 case lltok::kw_and:
2666 case lltok::kw_or:
2667 case lltok::kw_xor: {
2668 unsigned Opc = Lex.getUIntVal();
2669 Constant *Val0, *Val1;
2670 Lex.Lex();
2671 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2672 ParseGlobalTypeAndValue(Val0) ||
2673 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2674 ParseGlobalTypeAndValue(Val1) ||
2675 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2676 return true;
2677 if (Val0->getType() != Val1->getType())
2678 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002679 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002680 return Error(ID.Loc,
2681 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002682 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002683 ID.Kind = ValID::t_Constant;
2684 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002685 }
2686
Chris Lattnerac161bf2009-01-02 07:01:27 +00002687 case lltok::kw_getelementptr:
2688 case lltok::kw_shufflevector:
2689 case lltok::kw_insertelement:
2690 case lltok::kw_extractelement:
2691 case lltok::kw_select: {
2692 unsigned Opc = Lex.getUIntVal();
2693 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002694 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002695 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002696 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002697 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002698 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2699 ParseGlobalValueVector(Elts) ||
2700 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2701 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002702
Chris Lattnerac161bf2009-01-02 07:01:27 +00002703 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002704 if (Elts.size() == 0 ||
2705 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002706 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002707
Jay Foaded8db7d2011-07-21 14:31:17 +00002708 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002709 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002710 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002711 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2712 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002713 } else if (Opc == Instruction::Select) {
2714 if (Elts.size() != 3)
2715 return Error(ID.Loc, "expected three operands to select");
2716 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2717 Elts[2]))
2718 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002719 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002720 } else if (Opc == Instruction::ShuffleVector) {
2721 if (Elts.size() != 3)
2722 return Error(ID.Loc, "expected three operands to shufflevector");
2723 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2724 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002725 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002726 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002727 } else if (Opc == Instruction::ExtractElement) {
2728 if (Elts.size() != 2)
2729 return Error(ID.Loc, "expected two operands to extractelement");
2730 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2731 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002732 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002733 } else {
2734 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2735 if (Elts.size() != 3)
2736 return Error(ID.Loc, "expected three operands to insertelement");
2737 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2738 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002739 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002740 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002741 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002742
Chris Lattnerac161bf2009-01-02 07:01:27 +00002743 ID.Kind = ValID::t_Constant;
2744 return false;
2745 }
2746 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002747
Chris Lattnerac161bf2009-01-02 07:01:27 +00002748 Lex.Lex();
2749 return false;
2750}
2751
2752/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002753bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002754 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002755 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002756 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002757 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002758 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002759 if (V && !(C = dyn_cast<Constant>(V)))
2760 return Error(ID.Loc, "global values must be constants");
2761 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002762}
2763
Victor Hernandez9d75c962010-01-11 22:31:58 +00002764bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002765 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002766 return ParseType(Ty) ||
2767 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002768}
2769
2770/// ParseGlobalValueVector
2771/// ::= /*empty*/
2772/// ::= TypeAndValue (',' TypeAndValue)*
2773bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2774 // Empty list.
2775 if (Lex.getKind() == lltok::rbrace ||
2776 Lex.getKind() == lltok::rsquare ||
2777 Lex.getKind() == lltok::greater ||
2778 Lex.getKind() == lltok::rparen)
2779 return false;
2780
2781 Constant *C;
2782 if (ParseGlobalTypeAndValue(C)) return true;
2783 Elts.push_back(C);
2784
2785 while (EatIfPresent(lltok::comma)) {
2786 if (ParseGlobalTypeAndValue(C)) return true;
2787 Elts.push_back(C);
2788 }
2789
2790 return false;
2791}
2792
Dan Gohmanc828c542010-08-24 02:24:03 +00002793bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2794 assert(Lex.getKind() == lltok::lbrace);
2795 Lex.Lex();
2796
2797 SmallVector<Value*, 16> Elts;
2798 if (ParseMDNodeVector(Elts, PFS) ||
2799 ParseToken(lltok::rbrace, "expected end of metadata node"))
2800 return true;
2801
Jay Foad5514afe2011-04-21 19:59:31 +00002802 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002803 ID.Kind = ValID::t_MDNode;
2804 return false;
2805}
2806
Dan Gohman8939ba332010-07-14 18:26:50 +00002807/// ParseMetadataValue
2808/// ::= !42
2809/// ::= !{...}
2810/// ::= !"string"
2811bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2812 assert(Lex.getKind() == lltok::exclaim);
2813 Lex.Lex();
2814
2815 // MDNode:
2816 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002817 if (Lex.getKind() == lltok::lbrace)
2818 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002819
2820 // Standalone metadata reference
2821 // !42
2822 if (Lex.getKind() == lltok::APSInt) {
2823 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2824 ID.Kind = ValID::t_MDNode;
2825 return false;
2826 }
2827
2828 // MDString:
2829 // ::= '!' STRINGCONSTANT
2830 if (ParseMDString(ID.MDStringVal)) return true;
2831 ID.Kind = ValID::t_MDString;
2832 return false;
2833}
2834
Victor Hernandez9d75c962010-01-11 22:31:58 +00002835
2836//===----------------------------------------------------------------------===//
2837// Function Parsing.
2838//===----------------------------------------------------------------------===//
2839
Chris Lattner229907c2011-07-18 04:54:35 +00002840bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002841 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002842 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002843 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002844
Chris Lattnerac161bf2009-01-02 07:01:27 +00002845 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002846 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002847 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2848 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002849 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002850 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002851 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2852 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002853 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002854 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002855 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002856 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002857 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002858 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2859 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002860 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002861 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002862 return false;
2863 }
2864 case ValID::t_MDNode:
2865 if (!Ty->isMetadataTy())
2866 return Error(ID.Loc, "metadata value must have metadata type");
2867 V = ID.MDNodeVal;
2868 return false;
2869 case ValID::t_MDString:
2870 if (!Ty->isMetadataTy())
2871 return Error(ID.Loc, "metadata value must have metadata type");
2872 V = ID.MDStringVal;
2873 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002874 case ValID::t_GlobalName:
2875 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002876 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002877 case ValID::t_GlobalID:
2878 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002879 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002880 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002881 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002882 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002883 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002884 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002885 return false;
2886 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002887 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002888 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2889 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002890
Dan Gohman518cda42011-12-17 00:04:22 +00002891 // The lexer has no type info, so builds all half, float, and double FP
2892 // constants as double. Fix this here. Long double does not need this.
2893 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002894 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002895 if (Ty->isHalfTy())
2896 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2897 &Ignored);
2898 else if (Ty->isFloatTy())
2899 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2900 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002901 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002902 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002903
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002904 if (V->getType() != Ty)
2905 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002906 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002907
Chris Lattnerac161bf2009-01-02 07:01:27 +00002908 return false;
2909 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002910 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002911 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002912 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002913 return false;
2914 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002915 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002916 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002917 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002918 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002919 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002920 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002921 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002922 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002923 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002924 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002925 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002926 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002927 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002928 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002929 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002930 return false;
2931 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002932 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002933 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002934
Chris Lattnerac161bf2009-01-02 07:01:27 +00002935 V = ID.ConstantVal;
2936 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002937 case ValID::t_ConstantStruct:
2938 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002939 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002940 if (ST->getNumElements() != ID.UIntVal)
2941 return Error(ID.Loc,
2942 "initializer with struct type has wrong # elements");
2943 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2944 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002945
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002946 // Verify that the elements are compatible with the structtype.
2947 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2948 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2949 return Error(ID.Loc, "element " + Twine(i) +
2950 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002951
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002952 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2953 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002954 } else
2955 return Error(ID.Loc, "constant expression type mismatch");
2956 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002957 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002958 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002959}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002960
Chris Lattner229907c2011-07-18 04:54:35 +00002961bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002962 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002963 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002964 return ParseValID(ID, PFS) ||
2965 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002966}
2967
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002968bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002969 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002970 return ParseType(Ty) ||
2971 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002972}
2973
Chris Lattner3ed871f2009-10-27 19:13:16 +00002974bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2975 PerFunctionState &PFS) {
2976 Value *V;
2977 Loc = Lex.getLoc();
2978 if (ParseTypeAndValue(V, PFS)) return true;
2979 if (!isa<BasicBlock>(V))
2980 return Error(Loc, "expected a basic block");
2981 BB = cast<BasicBlock>(V);
2982 return false;
2983}
2984
2985
Chris Lattnerac161bf2009-01-02 07:01:27 +00002986/// FunctionHeader
2987/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00002988/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002989/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00002990bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2991 // Parse the linkage.
2992 LocTy LinkageLoc = Lex.getLoc();
2993 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002994
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00002995 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00002996 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00002997 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00002998 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00002999 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003000 LocTy RetTypeLoc = Lex.getLoc();
3001 if (ParseOptionalLinkage(Linkage) ||
3002 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003003 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003004 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003005 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003006 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003007 return true;
3008
3009 // Verify that the linkage is ok.
3010 switch ((GlobalValue::LinkageTypes)Linkage) {
3011 case GlobalValue::ExternalLinkage:
3012 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003013 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003014 if (isDefine)
3015 return Error(LinkageLoc, "invalid linkage for function definition");
3016 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003017 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003018 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003019 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003020 case GlobalValue::LinkOnceAnyLinkage:
3021 case GlobalValue::LinkOnceODRLinkage:
3022 case GlobalValue::WeakAnyLinkage:
3023 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003024 if (!isDefine)
3025 return Error(LinkageLoc, "invalid linkage for function declaration");
3026 break;
3027 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003028 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003029 return Error(LinkageLoc, "invalid function linkage type");
3030 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003031
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003032 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3033 return Error(LinkageLoc,
3034 "symbol with local linkage must have default visibility");
3035
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003036 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003037 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003038
Chris Lattnerac161bf2009-01-02 07:01:27 +00003039 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003040
3041 std::string FunctionName;
3042 if (Lex.getKind() == lltok::GlobalVar) {
3043 FunctionName = Lex.getStrVal();
3044 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3045 unsigned NameID = Lex.getUIntVal();
3046
3047 if (NameID != NumberedVals.size())
3048 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003049 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003050 } else {
3051 return TokError("expected function name");
3052 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003053
Chris Lattner3822f632009-01-02 08:05:26 +00003054 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003055
Chris Lattner3822f632009-01-02 08:05:26 +00003056 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003057 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003058
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003059 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003060 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003061 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003062 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003063 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003064 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003065 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003066 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003067 bool UnnamedAddr;
3068 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003069 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003070
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003071 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003072 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3073 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003074 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003075 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003076 (EatIfPresent(lltok::kw_section) &&
3077 ParseStringConstant(Section)) ||
3078 ParseOptionalAlignment(Alignment) ||
3079 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003080 ParseStringConstant(GC)) ||
3081 (EatIfPresent(lltok::kw_prefix) &&
3082 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003083 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003084
Michael Gottesman41748d72013-06-27 00:25:01 +00003085 if (FuncAttrs.contains(Attribute::Builtin))
3086 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003087
Chris Lattnerac161bf2009-01-02 07:01:27 +00003088 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003089 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003090 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003091 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003092 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003093
Chris Lattnerac161bf2009-01-02 07:01:27 +00003094 // Okay, if we got here, the function is syntactically valid. Convert types
3095 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003096 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003097 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003098
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003099 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003100 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3101 AttributeSet::ReturnIndex,
3102 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003103
Chris Lattnerac161bf2009-01-02 07:01:27 +00003104 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003105 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003106 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3107 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003108 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3109 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003110 }
3111
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003112 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003113 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3114 AttributeSet::FunctionIndex,
3115 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003116
Bill Wendlinge94d8432012-12-07 23:16:57 +00003117 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003118
Bill Wendling749a43d2012-12-30 13:50:49 +00003119 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003120 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3121
Chris Lattner229907c2011-07-18 04:54:35 +00003122 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003123 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003124 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003125
Craig Topper2617dcc2014-04-15 06:32:26 +00003126 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003127 if (!FunctionName.empty()) {
3128 // If this was a definition of a forward reference, remove the definition
3129 // from the forward reference table and fill in the forward ref.
3130 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3131 ForwardRefVals.find(FunctionName);
3132 if (FRVI != ForwardRefVals.end()) {
3133 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003134 if (!Fn)
3135 return Error(FRVI->second.second, "invalid forward reference to "
3136 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003137 if (Fn->getType() != PFT)
3138 return Error(FRVI->second.second, "invalid forward reference to "
3139 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003140
Chris Lattnerac161bf2009-01-02 07:01:27 +00003141 ForwardRefVals.erase(FRVI);
3142 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003143 // Reject redefinitions.
3144 return Error(NameLoc, "invalid redefinition of function '" +
3145 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003146 } else if (M->getNamedValue(FunctionName)) {
3147 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003148 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003149
Dan Gohman399d6ae2009-08-29 23:37:49 +00003150 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003151 // If this is a definition of a forward referenced function, make sure the
3152 // types agree.
3153 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3154 = ForwardRefValIDs.find(NumberedVals.size());
3155 if (I != ForwardRefValIDs.end()) {
3156 Fn = cast<Function>(I->second.first);
3157 if (Fn->getType() != PFT)
3158 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003159 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003160 ForwardRefValIDs.erase(I);
3161 }
3162 }
3163
Craig Topper2617dcc2014-04-15 06:32:26 +00003164 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003165 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3166 else // Move the forward-reference to the correct spot in the module.
3167 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3168
3169 if (FunctionName.empty())
3170 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003171
Chris Lattnerac161bf2009-01-02 07:01:27 +00003172 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3173 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003174 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003175 Fn->setCallingConv(CC);
3176 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003177 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003178 Fn->setAlignment(Alignment);
3179 Fn->setSection(Section);
3180 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003181 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003182 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003183
Chris Lattnerac161bf2009-01-02 07:01:27 +00003184 // Add all of the arguments we parsed to the function.
3185 Function::arg_iterator ArgIt = Fn->arg_begin();
3186 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3187 // If the argument has a name, insert it into the argument symbol table.
3188 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003189
Chris Lattnerac161bf2009-01-02 07:01:27 +00003190 // Set the name, if it conflicted, it will be auto-renamed.
3191 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003192
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003193 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003194 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3195 ArgList[i].Name + "'");
3196 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003197
Chris Lattnerac161bf2009-01-02 07:01:27 +00003198 return false;
3199}
3200
3201
3202/// ParseFunctionBody
3203/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003204///
3205bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003206 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003207 return TokError("expected '{' in function body");
3208 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003209
Chris Lattner3432c622009-10-28 03:39:23 +00003210 int FunctionNumber = -1;
3211 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003212
Chris Lattner3432c622009-10-28 03:39:23 +00003213 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003214
Chris Lattnerbbddd962010-01-09 19:20:07 +00003215 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003216 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003217 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003218
Chris Lattner4649a732011-06-17 06:42:57 +00003219 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003220 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003221
Chris Lattnerac161bf2009-01-02 07:01:27 +00003222 // Eat the }.
3223 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003224
Chris Lattnerac161bf2009-01-02 07:01:27 +00003225 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003226 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003227}
3228
3229/// ParseBasicBlock
3230/// ::= LabelStr? Instruction*
3231bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3232 // If this basic block starts out with a name, remember it.
3233 std::string Name;
3234 LocTy NameLoc = Lex.getLoc();
3235 if (Lex.getKind() == lltok::LabelStr) {
3236 Name = Lex.getStrVal();
3237 Lex.Lex();
3238 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003239
Chris Lattnerac161bf2009-01-02 07:01:27 +00003240 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003241 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003242
Chris Lattnerac161bf2009-01-02 07:01:27 +00003243 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003244
Chris Lattnerac161bf2009-01-02 07:01:27 +00003245 // Parse the instructions in this block until we get a terminator.
3246 Instruction *Inst;
3247 do {
3248 // This instruction may have three possibilities for a name: a) none
3249 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3250 LocTy NameLoc = Lex.getLoc();
3251 int NameID = -1;
3252 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003253
Chris Lattnerac161bf2009-01-02 07:01:27 +00003254 if (Lex.getKind() == lltok::LocalVarID) {
3255 NameID = Lex.getUIntVal();
3256 Lex.Lex();
3257 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3258 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003259 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003260 NameStr = Lex.getStrVal();
3261 Lex.Lex();
3262 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3263 return true;
3264 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003265
Chris Lattner77b89dc2009-12-30 05:23:43 +00003266 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003267 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003268 case InstError: return true;
3269 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003270 BB->getInstList().push_back(Inst);
3271
Chris Lattner77b89dc2009-12-30 05:23:43 +00003272 // With a normal result, we check to see if the instruction is followed by
3273 // a comma and metadata.
3274 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003275 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003276 return true;
3277 break;
3278 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003279 BB->getInstList().push_back(Inst);
3280
Chris Lattner77b89dc2009-12-30 05:23:43 +00003281 // If the instruction parser ate an extra comma at the end of it, it
3282 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003283 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003284 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003285 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003286 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003287
Chris Lattnerac161bf2009-01-02 07:01:27 +00003288 // Set the name on the instruction.
3289 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3290 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003291
Chris Lattnerac161bf2009-01-02 07:01:27 +00003292 return false;
3293}
3294
3295//===----------------------------------------------------------------------===//
3296// Instruction Parsing.
3297//===----------------------------------------------------------------------===//
3298
3299/// ParseInstruction - Parse one of the many different instructions.
3300///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003301int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3302 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003303 lltok::Kind Token = Lex.getKind();
3304 if (Token == lltok::Eof)
3305 return TokError("found end of file when expecting more instructions");
3306 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003307 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003308 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003309
Chris Lattnerac161bf2009-01-02 07:01:27 +00003310 switch (Token) {
3311 default: return Error(Loc, "expected instruction opcode");
3312 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003313 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003314 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3315 case lltok::kw_br: return ParseBr(Inst, PFS);
3316 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003317 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003318 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003319 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003320 // Binary Operators.
3321 case lltok::kw_add:
3322 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003323 case lltok::kw_mul:
3324 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003325 bool NUW = EatIfPresent(lltok::kw_nuw);
3326 bool NSW = EatIfPresent(lltok::kw_nsw);
3327 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003328
Chris Lattnera676c0f2011-02-07 16:40:21 +00003329 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003330
Chris Lattnera676c0f2011-02-07 16:40:21 +00003331 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3332 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3333 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003334 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003335 case lltok::kw_fadd:
3336 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003337 case lltok::kw_fmul:
3338 case lltok::kw_fdiv:
3339 case lltok::kw_frem: {
3340 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3341 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3342 if (Res != 0)
3343 return Res;
3344 if (FMF.any())
3345 Inst->setFastMathFlags(FMF);
3346 return 0;
3347 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003348
Chris Lattner35315d02011-02-06 21:44:57 +00003349 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003350 case lltok::kw_udiv:
3351 case lltok::kw_lshr:
3352 case lltok::kw_ashr: {
3353 bool Exact = EatIfPresent(lltok::kw_exact);
3354
3355 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3356 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3357 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003358 }
3359
Chris Lattnerac161bf2009-01-02 07:01:27 +00003360 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003361 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003362 case lltok::kw_and:
3363 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003364 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003365 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003366 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003367 // Casts.
3368 case lltok::kw_trunc:
3369 case lltok::kw_zext:
3370 case lltok::kw_sext:
3371 case lltok::kw_fptrunc:
3372 case lltok::kw_fpext:
3373 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003374 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003375 case lltok::kw_uitofp:
3376 case lltok::kw_sitofp:
3377 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003378 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003379 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003380 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003381 // Other.
3382 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003383 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003384 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3385 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3386 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3387 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003388 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003389 // Call.
3390 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3391 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3392 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003393 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003394 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003395 case lltok::kw_load: return ParseLoad(Inst, PFS);
3396 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003397 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3398 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003399 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003400 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3401 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3402 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3403 }
3404}
3405
3406/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3407bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003408 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003409 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003410 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003411 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3412 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3413 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3414 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3415 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3416 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3417 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3418 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3419 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3420 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3421 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3422 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3423 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3424 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3425 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3426 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3427 }
3428 } else {
3429 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003430 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003431 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3432 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3433 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3434 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3435 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3436 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3437 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3438 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3439 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3440 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3441 }
3442 }
3443 Lex.Lex();
3444 return false;
3445}
3446
3447//===----------------------------------------------------------------------===//
3448// Terminator Instructions.
3449//===----------------------------------------------------------------------===//
3450
3451/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003452/// ::= 'ret' void (',' !dbg, !1)*
3453/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003454bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003455 PerFunctionState &PFS) {
3456 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003457 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003458 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003459
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003460 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003461
Chris Lattnerfdd87902009-10-05 05:54:46 +00003462 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003463 if (!ResType->isVoidTy())
3464 return Error(TypeLoc, "value doesn't match function result type '" +
3465 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003466
Owen Anderson55f1c092009-08-13 21:58:54 +00003467 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003468 return false;
3469 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003470
Chris Lattnerac161bf2009-01-02 07:01:27 +00003471 Value *RV;
3472 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003473
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003474 if (ResType != RV->getType())
3475 return Error(TypeLoc, "value doesn't match function result type '" +
3476 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003477
Owen Anderson55f1c092009-08-13 21:58:54 +00003478 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003479 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003480}
3481
3482
3483/// ParseBr
3484/// ::= 'br' TypeAndValue
3485/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3486bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3487 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003488 Value *Op0;
3489 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003490 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003491
Chris Lattnerac161bf2009-01-02 07:01:27 +00003492 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3493 Inst = BranchInst::Create(BB);
3494 return false;
3495 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003496
Owen Anderson55f1c092009-08-13 21:58:54 +00003497 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003498 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003499
Chris Lattnerac161bf2009-01-02 07:01:27 +00003500 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003501 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003502 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003503 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003504 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003505
Chris Lattner3ed871f2009-10-27 19:13:16 +00003506 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003507 return false;
3508}
3509
3510/// ParseSwitch
3511/// Instruction
3512/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3513/// JumpTable
3514/// ::= (TypeAndValue ',' TypeAndValue)*
3515bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3516 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003517 Value *Cond;
3518 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003519 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3520 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003521 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003522 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3523 return true;
3524
Duncan Sands19d0b472010-02-16 11:11:14 +00003525 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003526 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003527
Chris Lattnerac161bf2009-01-02 07:01:27 +00003528 // Parse the jump table pairs.
3529 SmallPtrSet<Value*, 32> SeenCases;
3530 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3531 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003532 Value *Constant;
3533 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003534
Chris Lattnerac161bf2009-01-02 07:01:27 +00003535 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3536 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003537 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003538 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003539
Chris Lattnerac161bf2009-01-02 07:01:27 +00003540 if (!SeenCases.insert(Constant))
3541 return Error(CondLoc, "duplicate case value in switch");
3542 if (!isa<ConstantInt>(Constant))
3543 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003544
Chris Lattner3ed871f2009-10-27 19:13:16 +00003545 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003546 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003547
Chris Lattnerac161bf2009-01-02 07:01:27 +00003548 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003549
Chris Lattner3ed871f2009-10-27 19:13:16 +00003550 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003551 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3552 SI->addCase(Table[i].first, Table[i].second);
3553 Inst = SI;
3554 return false;
3555}
3556
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003557/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003558/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003559/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3560bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003561 LocTy AddrLoc;
3562 Value *Address;
3563 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003564 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3565 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003566 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003567
Duncan Sands19d0b472010-02-16 11:11:14 +00003568 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003569 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003570
Chris Lattner3ed871f2009-10-27 19:13:16 +00003571 // Parse the destination list.
3572 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003573
Chris Lattner3ed871f2009-10-27 19:13:16 +00003574 if (Lex.getKind() != lltok::rsquare) {
3575 BasicBlock *DestBB;
3576 if (ParseTypeAndBasicBlock(DestBB, PFS))
3577 return true;
3578 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003579
Chris Lattner3ed871f2009-10-27 19:13:16 +00003580 while (EatIfPresent(lltok::comma)) {
3581 if (ParseTypeAndBasicBlock(DestBB, PFS))
3582 return true;
3583 DestList.push_back(DestBB);
3584 }
3585 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003586
Chris Lattner3ed871f2009-10-27 19:13:16 +00003587 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3588 return true;
3589
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003590 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003591 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3592 IBI->addDestination(DestList[i]);
3593 Inst = IBI;
3594 return false;
3595}
3596
3597
Chris Lattnerac161bf2009-01-02 07:01:27 +00003598/// ParseInvoke
3599/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3600/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3601bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3602 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003603 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003604 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003605 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003606 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003607 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003608 LocTy RetTypeLoc;
3609 ValID CalleeID;
3610 SmallVector<ParamInfo, 16> ArgList;
3611
Chris Lattner3ed871f2009-10-27 19:13:16 +00003612 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003613 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003614 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003615 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003616 ParseValID(CalleeID) ||
3617 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003618 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3619 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003620 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003621 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003622 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003623 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003624 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003625
Chris Lattnerac161bf2009-01-02 07:01:27 +00003626 // If RetType is a non-function pointer type, then this is the short syntax
3627 // for the call, which means that RetType is just the return type. Infer the
3628 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003629 PointerType *PFTy = nullptr;
3630 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003631 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3632 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3633 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003634 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003635 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3636 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003637
Chris Lattnerac161bf2009-01-02 07:01:27 +00003638 if (!FunctionType::isValidReturnType(RetType))
3639 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003640
Owen Anderson4056ca92009-07-29 22:17:13 +00003641 Ty = FunctionType::get(RetType, ParamTypes, false);
3642 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003643 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003644
Chris Lattnerac161bf2009-01-02 07:01:27 +00003645 // Look up the callee.
3646 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003647 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003648
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003649 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003650 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003651 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003652 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3653 AttributeSet::ReturnIndex,
3654 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003655
Chris Lattnerac161bf2009-01-02 07:01:27 +00003656 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003657
Chris Lattnerac161bf2009-01-02 07:01:27 +00003658 // Loop through FunctionType's arguments and ensure they are specified
3659 // correctly. Also, gather any parameter attributes.
3660 FunctionType::param_iterator I = Ty->param_begin();
3661 FunctionType::param_iterator E = Ty->param_end();
3662 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003663 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003664 if (I != E) {
3665 ExpectedTy = *I++;
3666 } else if (!Ty->isVarArg()) {
3667 return Error(ArgList[i].Loc, "too many arguments specified");
3668 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003669
Chris Lattnerac161bf2009-01-02 07:01:27 +00003670 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3671 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003672 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003673 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003674 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3675 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003676 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3677 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003678 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003679
Chris Lattnerac161bf2009-01-02 07:01:27 +00003680 if (I != E)
3681 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003682
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003683 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003684 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3685 AttributeSet::FunctionIndex,
3686 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003687
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003688 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003689 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003690
Jay Foad5bd375a2011-07-15 08:37:34 +00003691 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003692 II->setCallingConv(CC);
3693 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003694 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003695 Inst = II;
3696 return false;
3697}
3698
Bill Wendlingf891bf82011-07-31 06:30:59 +00003699/// ParseResume
3700/// ::= 'resume' TypeAndValue
3701bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3702 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003703 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3704 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003705
Bill Wendlingf891bf82011-07-31 06:30:59 +00003706 ResumeInst *RI = ResumeInst::Create(Exn);
3707 Inst = RI;
3708 return false;
3709}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003710
3711//===----------------------------------------------------------------------===//
3712// Binary Operators.
3713//===----------------------------------------------------------------------===//
3714
3715/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003716/// ::= ArithmeticOps TypeAndValue ',' Value
3717///
3718/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3719/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003720bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003721 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003722 LocTy Loc; Value *LHS, *RHS;
3723 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3724 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3725 ParseValue(LHS->getType(), RHS, PFS))
3726 return true;
3727
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003728 bool Valid;
3729 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003730 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003731 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003732 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3733 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003734 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003735 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3736 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003737 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003738
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003739 if (!Valid)
3740 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003741
Chris Lattnerac161bf2009-01-02 07:01:27 +00003742 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3743 return false;
3744}
3745
3746/// ParseLogical
3747/// ::= ArithmeticOps TypeAndValue ',' Value {
3748bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3749 unsigned Opc) {
3750 LocTy Loc; Value *LHS, *RHS;
3751 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3752 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3753 ParseValue(LHS->getType(), RHS, PFS))
3754 return true;
3755
Duncan Sands9dff9be2010-02-15 16:12:20 +00003756 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003757 return Error(Loc,"instruction requires integer or integer vector operands");
3758
3759 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3760 return false;
3761}
3762
3763
3764/// ParseCompare
3765/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3766/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003767bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3768 unsigned Opc) {
3769 // Parse the integer/fp comparison predicate.
3770 LocTy Loc;
3771 unsigned Pred;
3772 Value *LHS, *RHS;
3773 if (ParseCmpPredicate(Pred, Opc) ||
3774 ParseTypeAndValue(LHS, Loc, PFS) ||
3775 ParseToken(lltok::comma, "expected ',' after compare value") ||
3776 ParseValue(LHS->getType(), RHS, PFS))
3777 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003778
Chris Lattnerac161bf2009-01-02 07:01:27 +00003779 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003780 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003781 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003782 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003783 } else {
3784 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003785 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003786 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003787 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003788 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003789 }
3790 return false;
3791}
3792
3793//===----------------------------------------------------------------------===//
3794// Other Instructions.
3795//===----------------------------------------------------------------------===//
3796
3797
3798/// ParseCast
3799/// ::= CastOpc TypeAndValue 'to' Type
3800bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3801 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003802 LocTy Loc;
3803 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003804 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003805 if (ParseTypeAndValue(Op, Loc, PFS) ||
3806 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3807 ParseType(DestTy))
3808 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003809
Chris Lattner89d856e2009-03-01 00:53:13 +00003810 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3811 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003812 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003813 getTypeString(Op->getType()) + "' to '" +
3814 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003815 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003816 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3817 return false;
3818}
3819
3820/// ParseSelect
3821/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3822bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3823 LocTy Loc;
3824 Value *Op0, *Op1, *Op2;
3825 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3826 ParseToken(lltok::comma, "expected ',' after select condition") ||
3827 ParseTypeAndValue(Op1, PFS) ||
3828 ParseToken(lltok::comma, "expected ',' after select value") ||
3829 ParseTypeAndValue(Op2, PFS))
3830 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003831
Chris Lattnerac161bf2009-01-02 07:01:27 +00003832 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3833 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003834
Chris Lattnerac161bf2009-01-02 07:01:27 +00003835 Inst = SelectInst::Create(Op0, Op1, Op2);
3836 return false;
3837}
3838
Chris Lattnerb55ab542009-01-05 08:18:44 +00003839/// ParseVA_Arg
3840/// ::= 'va_arg' TypeAndValue ',' Type
3841bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003842 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003843 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003844 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003845 if (ParseTypeAndValue(Op, PFS) ||
3846 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003847 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003848 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003849
Chris Lattnerb55ab542009-01-05 08:18:44 +00003850 if (!EltTy->isFirstClassType())
3851 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003852
3853 Inst = new VAArgInst(Op, EltTy);
3854 return false;
3855}
3856
3857/// ParseExtractElement
3858/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3859bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3860 LocTy Loc;
3861 Value *Op0, *Op1;
3862 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3863 ParseToken(lltok::comma, "expected ',' after extract value") ||
3864 ParseTypeAndValue(Op1, PFS))
3865 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003866
Chris Lattnerac161bf2009-01-02 07:01:27 +00003867 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3868 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003869
Eric Christopherc9742252009-07-25 02:28:41 +00003870 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003871 return false;
3872}
3873
3874/// ParseInsertElement
3875/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3876bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3877 LocTy Loc;
3878 Value *Op0, *Op1, *Op2;
3879 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3880 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3881 ParseTypeAndValue(Op1, PFS) ||
3882 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3883 ParseTypeAndValue(Op2, PFS))
3884 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003885
Chris Lattnerac161bf2009-01-02 07:01:27 +00003886 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003887 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003888
Chris Lattnerac161bf2009-01-02 07:01:27 +00003889 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3890 return false;
3891}
3892
3893/// ParseShuffleVector
3894/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3895bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3896 LocTy Loc;
3897 Value *Op0, *Op1, *Op2;
3898 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3899 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3900 ParseTypeAndValue(Op1, PFS) ||
3901 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3902 ParseTypeAndValue(Op2, PFS))
3903 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003904
Chris Lattnerac161bf2009-01-02 07:01:27 +00003905 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003906 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003907
Chris Lattnerac161bf2009-01-02 07:01:27 +00003908 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3909 return false;
3910}
3911
3912/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003913/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003914int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003915 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003916 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003917
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003918 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003919 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3920 ParseValue(Ty, Op0, PFS) ||
3921 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003922 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003923 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3924 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003925
Chris Lattnerf4f03422009-12-30 05:27:33 +00003926 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003927 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3928 while (1) {
3929 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003930
Chris Lattner3822f632009-01-02 08:05:26 +00003931 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003932 break;
3933
Chris Lattnerf4f03422009-12-30 05:27:33 +00003934 if (Lex.getKind() == lltok::MetadataVar) {
3935 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003936 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003937 }
Devang Patel8f842d32009-10-16 18:45:49 +00003938
Chris Lattner3822f632009-01-02 08:05:26 +00003939 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003940 ParseValue(Ty, Op0, PFS) ||
3941 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003942 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003943 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3944 return true;
3945 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003946
Chris Lattnerac161bf2009-01-02 07:01:27 +00003947 if (!Ty->isFirstClassType())
3948 return Error(TypeLoc, "phi node must have first class type");
3949
Jay Foad52131342011-03-30 11:28:46 +00003950 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003951 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3952 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3953 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003954 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003955}
3956
Bill Wendlingfae14752011-08-12 20:24:12 +00003957/// ParseLandingPad
3958/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3959/// Clause
3960/// ::= 'catch' TypeAndValue
3961/// ::= 'filter'
3962/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3963bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003964 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003965 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003966
3967 if (ParseType(Ty, TyLoc) ||
3968 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3969 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3970 return true;
3971
3972 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3973 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3974
3975 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3976 LandingPadInst::ClauseType CT;
3977 if (EatIfPresent(lltok::kw_catch))
3978 CT = LandingPadInst::Catch;
3979 else if (EatIfPresent(lltok::kw_filter))
3980 CT = LandingPadInst::Filter;
3981 else
3982 return TokError("expected 'catch' or 'filter' clause type");
3983
3984 Value *V; LocTy VLoc;
3985 if (ParseTypeAndValue(V, VLoc, PFS)) {
3986 delete LP;
3987 return true;
3988 }
3989
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00003990 // A 'catch' type expects a non-array constant. A filter clause expects an
3991 // array constant.
3992 if (CT == LandingPadInst::Catch) {
3993 if (isa<ArrayType>(V->getType()))
3994 Error(VLoc, "'catch' clause has an invalid type");
3995 } else {
3996 if (!isa<ArrayType>(V->getType()))
3997 Error(VLoc, "'filter' clause has an invalid type");
3998 }
3999
Bill Wendlingfae14752011-08-12 20:24:12 +00004000 LP->addClause(V);
4001 }
4002
4003 Inst = LP;
4004 return false;
4005}
4006
Chris Lattnerac161bf2009-01-02 07:01:27 +00004007/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004008/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4009/// ParameterList OptionalAttrs
4010/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4011/// ParameterList OptionalAttrs
4012/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004013/// ParameterList OptionalAttrs
4014bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004015 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004016 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004017 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004018 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004019 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004020 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004021 LocTy RetTypeLoc;
4022 ValID CalleeID;
4023 SmallVector<ParamInfo, 16> ArgList;
4024 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004025
Reid Kleckner5772b772014-04-24 20:14:34 +00004026 if ((TCK != CallInst::TCK_None &&
4027 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004028 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004029 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004030 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004031 ParseValID(CalleeID) ||
4032 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004033 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004034 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004035 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004036
Chris Lattnerac161bf2009-01-02 07:01:27 +00004037 // If RetType is a non-function pointer type, then this is the short syntax
4038 // for the call, which means that RetType is just the return type. Infer the
4039 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004040 PointerType *PFTy = nullptr;
4041 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004042 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4043 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4044 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004045 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004046 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4047 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004048
Chris Lattnerac161bf2009-01-02 07:01:27 +00004049 if (!FunctionType::isValidReturnType(RetType))
4050 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004051
Owen Anderson4056ca92009-07-29 22:17:13 +00004052 Ty = FunctionType::get(RetType, ParamTypes, false);
4053 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004054 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004055
Chris Lattnerac161bf2009-01-02 07:01:27 +00004056 // Look up the callee.
4057 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004058 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004059
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004060 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004061 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004062 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004063 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4064 AttributeSet::ReturnIndex,
4065 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004066
Chris Lattnerac161bf2009-01-02 07:01:27 +00004067 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004068
Chris Lattnerac161bf2009-01-02 07:01:27 +00004069 // Loop through FunctionType's arguments and ensure they are specified
4070 // correctly. Also, gather any parameter attributes.
4071 FunctionType::param_iterator I = Ty->param_begin();
4072 FunctionType::param_iterator E = Ty->param_end();
4073 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004074 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004075 if (I != E) {
4076 ExpectedTy = *I++;
4077 } else if (!Ty->isVarArg()) {
4078 return Error(ArgList[i].Loc, "too many arguments specified");
4079 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004080
Chris Lattnerac161bf2009-01-02 07:01:27 +00004081 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4082 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004083 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004084 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004085 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4086 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004087 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4088 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004089 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004090
Chris Lattnerac161bf2009-01-02 07:01:27 +00004091 if (I != E)
4092 return Error(CallLoc, "not enough parameters specified for call");
4093
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004094 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004095 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4096 AttributeSet::FunctionIndex,
4097 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004098
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004099 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004100 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004101
Jay Foad5bd375a2011-07-15 08:37:34 +00004102 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004103 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004104 CI->setCallingConv(CC);
4105 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004106 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004107 Inst = CI;
4108 return false;
4109}
4110
4111//===----------------------------------------------------------------------===//
4112// Memory Instructions.
4113//===----------------------------------------------------------------------===//
4114
4115/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004116/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004117int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004118 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004119 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004120 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004121 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004122
4123 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4124
Chris Lattner3822f632009-01-02 08:05:26 +00004125 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004126
Chris Lattnerb2f39502009-12-30 05:44:30 +00004127 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004128 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004129 if (Lex.getKind() == lltok::kw_align) {
4130 if (ParseOptionalAlignment(Alignment)) return true;
4131 } else if (Lex.getKind() == lltok::MetadataVar) {
4132 AteExtraComma = true;
4133 } else {
4134 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4135 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4136 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004137 }
4138 }
4139
Dan Gohman2140a742010-05-28 01:14:11 +00004140 if (Size && !Size->getType()->isIntegerTy())
4141 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004142
Reid Kleckner436c42e2014-01-17 23:58:17 +00004143 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4144 AI->setUsedWithInAlloca(IsInAlloca);
4145 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004146 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004147}
4148
4149/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004150/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004151/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004152/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004153int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004154 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004155 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004156 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004157 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004158 AtomicOrdering Ordering = NotAtomic;
4159 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004160
4161 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004162 isAtomic = true;
4163 Lex.Lex();
4164 }
4165
Chris Lattnerbc639292011-11-27 06:56:53 +00004166 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004167 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004168 isVolatile = true;
4169 Lex.Lex();
4170 }
4171
Chris Lattnerb2f39502009-12-30 05:44:30 +00004172 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004173 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004174 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4175 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004176
Duncan Sands19d0b472010-02-16 11:11:14 +00004177 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004178 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4179 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004180 if (isAtomic && !Alignment)
4181 return Error(Loc, "atomic load must have explicit non-zero alignment");
4182 if (Ordering == Release || Ordering == AcquireRelease)
4183 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004184
Eli Friedman59b66882011-08-09 23:02:53 +00004185 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004186 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004187}
4188
4189/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004190
4191/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4192/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004193/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004194int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004195 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004196 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004197 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004198 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004199 AtomicOrdering Ordering = NotAtomic;
4200 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004201
4202 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004203 isAtomic = true;
4204 Lex.Lex();
4205 }
4206
Chris Lattnerbc639292011-11-27 06:56:53 +00004207 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004208 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004209 isVolatile = true;
4210 Lex.Lex();
4211 }
4212
Chris Lattnerac161bf2009-01-02 07:01:27 +00004213 if (ParseTypeAndValue(Val, Loc, PFS) ||
4214 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004215 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004216 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004217 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004218 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004219
Duncan Sands19d0b472010-02-16 11:11:14 +00004220 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004221 return Error(PtrLoc, "store operand must be a pointer");
4222 if (!Val->getType()->isFirstClassType())
4223 return Error(Loc, "store operand must be a first class value");
4224 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4225 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004226 if (isAtomic && !Alignment)
4227 return Error(Loc, "atomic store must have explicit non-zero alignment");
4228 if (Ordering == Acquire || Ordering == AcquireRelease)
4229 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004230
Eli Friedman59b66882011-08-09 23:02:53 +00004231 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004232 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004233}
4234
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004235/// ParseCmpXchg
Eli Friedman02e737b2011-08-12 22:50:01 +00004236/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
Tim Northovere94a5182014-03-11 10:48:52 +00004237/// 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004238int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004239 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4240 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004241 AtomicOrdering SuccessOrdering = NotAtomic;
4242 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004243 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004244 bool isVolatile = false;
4245
4246 if (EatIfPresent(lltok::kw_volatile))
4247 isVolatile = true;
4248
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004249 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4250 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4251 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4252 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4253 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004254 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4255 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004256 return true;
4257
Tim Northovere94a5182014-03-11 10:48:52 +00004258 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004259 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004260 if (SuccessOrdering < FailureOrdering)
4261 return TokError("cmpxchg must be at least as ordered on success as failure");
4262 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4263 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004264 if (!Ptr->getType()->isPointerTy())
4265 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4266 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4267 return Error(CmpLoc, "compare value and pointer type do not match");
4268 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4269 return Error(NewLoc, "new value and pointer type do not match");
4270 if (!New->getType()->isIntegerTy())
4271 return Error(NewLoc, "cmpxchg operand must be an integer");
4272 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4273 if (Size < 8 || (Size & (Size - 1)))
4274 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4275 " integer");
4276
Tim Northovere94a5182014-03-11 10:48:52 +00004277 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
4278 FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004279 CXI->setVolatile(isVolatile);
4280 Inst = CXI;
4281 return AteExtraComma ? InstExtraComma : InstNormal;
4282}
4283
4284/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004285/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4286/// 'singlethread'? AtomicOrdering
4287int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004288 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4289 bool AteExtraComma = false;
4290 AtomicOrdering Ordering = NotAtomic;
4291 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004292 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004293 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004294
4295 if (EatIfPresent(lltok::kw_volatile))
4296 isVolatile = true;
4297
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004298 switch (Lex.getKind()) {
4299 default: return TokError("expected binary operation in atomicrmw");
4300 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4301 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4302 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4303 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4304 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4305 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4306 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4307 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4308 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4309 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4310 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4311 }
4312 Lex.Lex(); // Eat the operation.
4313
4314 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4315 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4316 ParseTypeAndValue(Val, ValLoc, PFS) ||
4317 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4318 return true;
4319
4320 if (Ordering == Unordered)
4321 return TokError("atomicrmw cannot be unordered");
4322 if (!Ptr->getType()->isPointerTy())
4323 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4324 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4325 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4326 if (!Val->getType()->isIntegerTy())
4327 return Error(ValLoc, "atomicrmw operand must be an integer");
4328 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4329 if (Size < 8 || (Size & (Size - 1)))
4330 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4331 " integer");
4332
4333 AtomicRMWInst *RMWI =
4334 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4335 RMWI->setVolatile(isVolatile);
4336 Inst = RMWI;
4337 return AteExtraComma ? InstExtraComma : InstNormal;
4338}
4339
Eli Friedmanfee02c62011-07-25 23:16:38 +00004340/// ParseFence
4341/// ::= 'fence' 'singlethread'? AtomicOrdering
4342int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4343 AtomicOrdering Ordering = NotAtomic;
4344 SynchronizationScope Scope = CrossThread;
4345 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4346 return true;
4347
4348 if (Ordering == Unordered)
4349 return TokError("fence cannot be unordered");
4350 if (Ordering == Monotonic)
4351 return TokError("fence cannot be monotonic");
4352
4353 Inst = new FenceInst(Context, Ordering, Scope);
4354 return InstNormal;
4355}
4356
Chris Lattnerac161bf2009-01-02 07:01:27 +00004357/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004358/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004359int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004360 Value *Ptr = nullptr;
4361 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004362 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004363
Dan Gohman16cbbe42009-07-29 15:58:36 +00004364 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004365
Chris Lattner3822f632009-01-02 08:05:26 +00004366 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004367
Eli Benderskyd9806682013-04-22 17:03:42 +00004368 Type *BaseType = Ptr->getType();
4369 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4370 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004371 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004372
Chris Lattnerac161bf2009-01-02 07:01:27 +00004373 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004374 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004375 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004376 if (Lex.getKind() == lltok::MetadataVar) {
4377 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004378 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004379 }
Chris Lattner3822f632009-01-02 08:05:26 +00004380 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004381 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004382 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004383 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4384 return Error(EltLoc, "getelementptr index type missmatch");
4385 if (Val->getType()->isVectorTy()) {
4386 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4387 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4388 if (ValNumEl != PtrNumEl)
4389 return Error(EltLoc,
4390 "getelementptr vector index has a wrong number of elements");
4391 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004392 Indices.push_back(Val);
4393 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004394
Eli Benderskyd9806682013-04-22 17:03:42 +00004395 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4396 return Error(Loc, "base element of getelementptr must be sized");
4397
4398 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004399 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004400 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004401 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004402 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004403 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004404}
4405
4406/// ParseExtractValue
4407/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004408int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004409 Value *Val; LocTy Loc;
4410 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004411 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004412 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004413 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004414 return true;
4415
Chris Lattner392be582010-02-12 20:49:41 +00004416 if (!Val->getType()->isAggregateType())
4417 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004418
Jay Foad57aa6362011-07-13 10:26:04 +00004419 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004420 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004421 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004422 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004423}
4424
4425/// ParseInsertValue
4426/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004427int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004428 Value *Val0, *Val1; LocTy Loc0, Loc1;
4429 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004430 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004431 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4432 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4433 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004434 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004435 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004436
Chris Lattner392be582010-02-12 20:49:41 +00004437 if (!Val0->getType()->isAggregateType())
4438 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004439
Jay Foad57aa6362011-07-13 10:26:04 +00004440 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004441 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004442 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004443 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004444}
Nick Lewycky49f89192009-04-04 07:22:01 +00004445
4446//===----------------------------------------------------------------------===//
4447// Embedded metadata.
4448//===----------------------------------------------------------------------===//
4449
4450/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004451/// ::= Element (',' Element)*
4452/// Element
4453/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004454bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004455 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004456 // Check for an empty list.
4457 if (Lex.getKind() == lltok::rbrace)
4458 return false;
4459
Nick Lewycky49f89192009-04-04 07:22:01 +00004460 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004461 // Null is a special case since it is typeless.
4462 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004463 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004464 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004465 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004466
Craig Topper2617dcc2014-04-15 06:32:26 +00004467 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004468 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004469 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004470 } while (EatIfPresent(lltok::comma));
4471
4472 return false;
4473}