blob: 32037df958bd1769e4a9208396178a59e8251f34 [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.
676 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
677 (GlobalValue::LinkageTypes)Linkage, Name,
678 Aliasee);
679 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000680 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000681
Chris Lattnerac161bf2009-01-02 07:01:27 +0000682 // See if this value already exists in the symbol table. If so, it is either
683 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000684 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000685 // See if this was a redefinition. If so, there is no entry in
686 // ForwardRefVals.
687 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
688 I = ForwardRefVals.find(Name);
689 if (I == ForwardRefVals.end())
690 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
691
692 // Otherwise, this was a definition of forward ref. Verify that types
693 // agree.
694 if (Val->getType() != GA->getType())
695 return Error(NameLoc,
696 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000697
Chris Lattnerac161bf2009-01-02 07:01:27 +0000698 // If they agree, just RAUW the old value with the alias and remove the
699 // forward ref info.
700 Val->replaceAllUsesWith(GA);
701 Val->eraseFromParent();
702 ForwardRefVals.erase(I);
703 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000704
Chris Lattnerac161bf2009-01-02 07:01:27 +0000705 // Insert into the module, we know its name won't collide now.
706 M->getAliasList().push_back(GA);
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000707 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000708
Chris Lattnerac161bf2009-01-02 07:01:27 +0000709 return false;
710}
711
712/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000713/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
714/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000715/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000716/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
717/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000718/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000719///
David Majnemerc4ab61c2014-03-09 06:41:58 +0000720/// Everything up to and including OptionalDLLStorageClass has been parsed
721/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000722///
723bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
724 unsigned Linkage, bool HasLinkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000725 unsigned Visibility, unsigned DLLStorageClass) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000726 if (!isValidVisibilityForLinkage(Visibility, Linkage))
727 return Error(NameLoc,
728 "symbol with local linkage must have default visibility");
729
Chris Lattnerac161bf2009-01-02 07:01:27 +0000730 unsigned AddrSpace;
Shuxin Yang2e1890e2013-10-27 03:08:44 +0000731 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000732 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola026d1522011-01-13 01:30:30 +0000733 LocTy UnnamedAddrLoc;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000734 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000735 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000736
Craig Topper2617dcc2014-04-15 06:32:26 +0000737 Type *Ty = nullptr;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000738 if (ParseOptionalThreadLocal(TLM) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000739 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindola026d1522011-01-13 01:30:30 +0000740 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
741 &UnnamedAddrLoc) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000742 ParseOptionalToken(lltok::kw_externally_initialized,
743 IsExternallyInitialized,
744 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000745 ParseGlobalType(IsConstant) ||
746 ParseType(Ty, TyLoc))
747 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000748
Chris Lattnerac161bf2009-01-02 07:01:27 +0000749 // If the linkage is specified and is external, then no initializer is
750 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000751 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000752 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000753 Linkage != GlobalValue::ExternalLinkage)) {
754 if (ParseGlobalValue(Ty, Init))
755 return true;
756 }
757
Duncan Sands19d0b472010-02-16 11:11:14 +0000758 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000759 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000760
Craig Topper2617dcc2014-04-15 06:32:26 +0000761 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000762
763 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000764 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000765 if (GlobalValue *GVal = M->getNamedValue(Name)) {
766 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
767 return Error(NameLoc, "redefinition of global '@" + Name + "'");
768 GV = cast<GlobalVariable>(GVal);
769 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000770 } else {
771 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
772 I = ForwardRefValIDs.find(NumberedVals.size());
773 if (I != ForwardRefValIDs.end()) {
774 GV = cast<GlobalVariable>(I->second.first);
775 ForwardRefValIDs.erase(I);
776 }
777 }
778
Craig Topper2617dcc2014-04-15 06:32:26 +0000779 if (!GV) {
780 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
781 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000782 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000783 } else {
784 if (GV->getType()->getElementType() != Ty)
785 return Error(TyLoc,
786 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000787
Chris Lattnerac161bf2009-01-02 07:01:27 +0000788 // Move the forward-reference to the correct spot in the module.
789 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
790 }
791
792 if (Name.empty())
793 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000794
Chris Lattnerac161bf2009-01-02 07:01:27 +0000795 // Set the parsed properties on the global.
796 if (Init)
797 GV->setInitializer(Init);
798 GV->setConstant(IsConstant);
799 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
800 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000801 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000802 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000803 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000804 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000805
Chris Lattnerac161bf2009-01-02 07:01:27 +0000806 // Parse attributes on the global.
807 while (Lex.getKind() == lltok::comma) {
808 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000809
Chris Lattnerac161bf2009-01-02 07:01:27 +0000810 if (Lex.getKind() == lltok::kw_section) {
811 Lex.Lex();
812 GV->setSection(Lex.getStrVal());
813 if (ParseToken(lltok::StringConstant, "expected global section string"))
814 return true;
815 } else if (Lex.getKind() == lltok::kw_align) {
816 unsigned Alignment;
817 if (ParseOptionalAlignment(Alignment)) return true;
818 GV->setAlignment(Alignment);
819 } else {
820 TokError("unknown global variable property!");
821 }
822 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000823
Chris Lattnerac161bf2009-01-02 07:01:27 +0000824 return false;
825}
826
Bill Wendling63b88192013-02-06 06:52:58 +0000827/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000828/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000829bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000830 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000831 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000832 Lex.Lex();
833
834 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000835 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000836 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000837 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000838 Lex.Lex();
839
840 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000841 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000842 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000843 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000844 ParseToken(lltok::rbrace, "expected end of attribute group"))
845 return true;
846
Bill Wendlingb32b0412013-02-08 06:32:06 +0000847 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000848 return Error(AttrGrpLoc, "attribute group has no attributes");
849
850 return false;
851}
852
Bill Wendling8b0321d2013-02-08 00:52:31 +0000853/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000854/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000855bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
856 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000857 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000858 bool HaveError = false;
859
860 B.clear();
861
Bill Wendling63b88192013-02-06 06:52:58 +0000862 while (true) {
863 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000864 if (Token == lltok::kw_builtin)
865 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000866 switch (Token) {
867 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000868 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000869 return Error(Lex.getLoc(), "unterminated attribute group");
870 case lltok::rbrace:
871 // Finished.
872 return false;
873
Bill Wendlingb32b0412013-02-08 06:32:06 +0000874 case lltok::AttrGrpID: {
875 // Allow a function to reference an attribute group:
876 //
877 // define void @foo() #1 { ... }
878 if (inAttrGrp)
879 HaveError |=
880 Error(Lex.getLoc(),
881 "cannot have an attribute group reference in an attribute group");
882
883 unsigned AttrGrpNum = Lex.getUIntVal();
884 if (inAttrGrp) break;
885
886 // Save the reference to the attribute group. We'll fill it in later.
887 FwdRefAttrGrps.push_back(AttrGrpNum);
888 break;
889 }
Bill Wendling63b88192013-02-06 06:52:58 +0000890 // Target-dependent attributes:
891 case lltok::StringConstant: {
892 std::string Attr = Lex.getStrVal();
893 Lex.Lex();
894 std::string Val;
895 if (EatIfPresent(lltok::equal) &&
896 ParseStringConstant(Val))
897 return true;
898
899 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000900 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000901 }
902
903 // Target-independent attributes:
904 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000905 // As a hack, we allow function alignment to be initially parsed as an
906 // attribute on a function declaration/definition or added to an attribute
907 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000908 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000909 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000910 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000911 if (ParseToken(lltok::equal, "expected '=' here") ||
912 ParseUInt32(Alignment))
913 return true;
914 } else {
915 if (ParseOptionalAlignment(Alignment))
916 return true;
917 }
Bill Wendling63b88192013-02-06 06:52:58 +0000918 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000919 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000920 }
921 case lltok::kw_alignstack: {
922 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000923 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000924 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000925 if (ParseToken(lltok::equal, "expected '=' here") ||
926 ParseUInt32(Alignment))
927 return true;
928 } else {
929 if (ParseOptionalStackAlignment(Alignment))
930 return true;
931 }
Bill Wendling63b88192013-02-06 06:52:58 +0000932 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000933 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000934 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000935 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000936 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000937 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000938 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
939 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
940 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
941 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
942 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
943 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
944 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
945 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
946 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
947 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
948 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000949 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000950 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
951 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
952 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
953 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
954 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
955 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
956 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
957 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
958 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
959 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
960 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000961
962 // Error handling.
963 case lltok::kw_inreg:
964 case lltok::kw_signext:
965 case lltok::kw_zeroext:
966 HaveError |=
967 Error(Lex.getLoc(),
968 "invalid use of attribute on a function");
969 break;
970 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +0000971 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000972 case lltok::kw_nest:
973 case lltok::kw_noalias:
974 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +0000975 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000976 case lltok::kw_sret:
977 HaveError |=
978 Error(Lex.getLoc(),
979 "invalid use of parameter-only attribute on a function");
980 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000981 }
982
983 Lex.Lex();
984 }
985}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000986
987//===----------------------------------------------------------------------===//
988// GlobalValue Reference/Resolution Routines.
989//===----------------------------------------------------------------------===//
990
991/// GetGlobalVal - Get a value with the specified name or ID, creating a
992/// forward reference record if needed. This can return null if the value
993/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000994GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000995 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +0000996 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +0000997 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000998 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +0000999 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001000 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001001
Chris Lattnerac161bf2009-01-02 07:01:27 +00001002 // Look this name up in the normal function symbol table.
1003 GlobalValue *Val =
1004 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001005
Chris Lattnerac161bf2009-01-02 07:01:27 +00001006 // If this is a forward reference for the value, see if we already created a
1007 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001008 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001009 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1010 I = ForwardRefVals.find(Name);
1011 if (I != ForwardRefVals.end())
1012 Val = I->second.first;
1013 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001014
Chris Lattnerac161bf2009-01-02 07:01:27 +00001015 // If we have the value in the symbol table or fwd-ref table, return it.
1016 if (Val) {
1017 if (Val->getType() == Ty) return Val;
1018 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001019 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001020 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001021 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001022
Chris Lattnerac161bf2009-01-02 07:01:27 +00001023 // Otherwise, create a new forward reference for this value and remember it.
1024 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001025 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001026 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001027 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001028 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001029 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1030 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001031 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001032
Chris Lattnerac161bf2009-01-02 07:01:27 +00001033 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1034 return FwdVal;
1035}
1036
Chris Lattner229907c2011-07-18 04:54:35 +00001037GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1038 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001039 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001040 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001041 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001042 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001043
Craig Topper2617dcc2014-04-15 06:32:26 +00001044 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001045
Chris Lattnerac161bf2009-01-02 07:01:27 +00001046 // If this is a forward reference for the value, see if we already created a
1047 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001048 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001049 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1050 I = ForwardRefValIDs.find(ID);
1051 if (I != ForwardRefValIDs.end())
1052 Val = I->second.first;
1053 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001054
Chris Lattnerac161bf2009-01-02 07:01:27 +00001055 // If we have the value in the symbol table or fwd-ref table, return it.
1056 if (Val) {
1057 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001058 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001059 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001060 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001061 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001062
Chris Lattnerac161bf2009-01-02 07:01:27 +00001063 // Otherwise, create a new forward reference for this value and remember it.
1064 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001065 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001066 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001067 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001068 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001069 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001070
Chris Lattnerac161bf2009-01-02 07:01:27 +00001071 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1072 return FwdVal;
1073}
1074
1075
1076//===----------------------------------------------------------------------===//
1077// Helper Routines.
1078//===----------------------------------------------------------------------===//
1079
1080/// ParseToken - If the current token has the specified kind, eat it and return
1081/// success. Otherwise, emit the specified error and return failure.
1082bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1083 if (Lex.getKind() != T)
1084 return TokError(ErrMsg);
1085 Lex.Lex();
1086 return false;
1087}
1088
Chris Lattner3822f632009-01-02 08:05:26 +00001089/// ParseStringConstant
1090/// ::= StringConstant
1091bool LLParser::ParseStringConstant(std::string &Result) {
1092 if (Lex.getKind() != lltok::StringConstant)
1093 return TokError("expected string constant");
1094 Result = Lex.getStrVal();
1095 Lex.Lex();
1096 return false;
1097}
1098
1099/// ParseUInt32
1100/// ::= uint32
1101bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001102 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1103 return TokError("expected integer");
1104 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1105 if (Val64 != unsigned(Val64))
1106 return TokError("expected 32-bit integer (too large)");
1107 Val = Val64;
1108 Lex.Lex();
1109 return false;
1110}
1111
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001112/// ParseTLSModel
1113/// := 'localdynamic'
1114/// := 'initialexec'
1115/// := 'localexec'
1116bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1117 switch (Lex.getKind()) {
1118 default:
1119 return TokError("expected localdynamic, initialexec or localexec");
1120 case lltok::kw_localdynamic:
1121 TLM = GlobalVariable::LocalDynamicTLSModel;
1122 break;
1123 case lltok::kw_initialexec:
1124 TLM = GlobalVariable::InitialExecTLSModel;
1125 break;
1126 case lltok::kw_localexec:
1127 TLM = GlobalVariable::LocalExecTLSModel;
1128 break;
1129 }
1130
1131 Lex.Lex();
1132 return false;
1133}
1134
1135/// ParseOptionalThreadLocal
1136/// := /*empty*/
1137/// := 'thread_local'
1138/// := 'thread_local' '(' tlsmodel ')'
1139bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1140 TLM = GlobalVariable::NotThreadLocal;
1141 if (!EatIfPresent(lltok::kw_thread_local))
1142 return false;
1143
1144 TLM = GlobalVariable::GeneralDynamicTLSModel;
1145 if (Lex.getKind() == lltok::lparen) {
1146 Lex.Lex();
1147 return ParseTLSModel(TLM) ||
1148 ParseToken(lltok::rparen, "expected ')' after thread local model");
1149 }
1150 return false;
1151}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001152
1153/// ParseOptionalAddrSpace
1154/// := /*empty*/
1155/// := 'addrspace' '(' uint32 ')'
1156bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1157 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001158 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001159 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001160 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001161 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001162 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001163}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001164
Bill Wendling34c2eb22012-12-04 23:40:58 +00001165/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1166bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1167 bool HaveError = false;
1168
1169 B.clear();
1170
1171 while (1) {
1172 lltok::Kind Token = Lex.getKind();
1173 switch (Token) {
1174 default: // End of attributes.
1175 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001176 case lltok::kw_align: {
1177 unsigned Alignment;
1178 if (ParseOptionalAlignment(Alignment))
1179 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001180 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001181 continue;
1182 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001183 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001184 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001185 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1186 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1187 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1188 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001189 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1190 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001191 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001192 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1193 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1194 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001195
Stephen Lin7577ed52013-04-20 13:16:13 +00001196 case lltok::kw_alignstack:
1197 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001198 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001199 case lltok::kw_inlinehint:
1200 case lltok::kw_minsize:
1201 case lltok::kw_naked:
1202 case lltok::kw_nobuiltin:
1203 case lltok::kw_noduplicate:
1204 case lltok::kw_noimplicitfloat:
1205 case lltok::kw_noinline:
1206 case lltok::kw_nonlazybind:
1207 case lltok::kw_noredzone:
1208 case lltok::kw_noreturn:
1209 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001210 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001211 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001212 case lltok::kw_returns_twice:
1213 case lltok::kw_sanitize_address:
1214 case lltok::kw_sanitize_memory:
1215 case lltok::kw_sanitize_thread:
1216 case lltok::kw_ssp:
1217 case lltok::kw_sspreq:
1218 case lltok::kw_sspstrong:
1219 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001220 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1221 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001222 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001223
Bill Wendling34c2eb22012-12-04 23:40:58 +00001224 Lex.Lex();
1225 }
1226}
1227
1228/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1229bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1230 bool HaveError = false;
1231
1232 B.clear();
1233
1234 while (1) {
1235 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001236 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001237 default: // End of attributes.
1238 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001239 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1240 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1241 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1242 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001243
Bill Wendling34c2eb22012-12-04 23:40:58 +00001244 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001245 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001246 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001247 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001248 case lltok::kw_nest:
1249 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001250 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001251 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001252 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001253 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001254
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001255 case lltok::kw_alignstack:
1256 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001257 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001258 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001259 case lltok::kw_inlinehint:
1260 case lltok::kw_minsize:
1261 case lltok::kw_naked:
1262 case lltok::kw_nobuiltin:
1263 case lltok::kw_noduplicate:
1264 case lltok::kw_noimplicitfloat:
1265 case lltok::kw_noinline:
1266 case lltok::kw_nonlazybind:
1267 case lltok::kw_noredzone:
1268 case lltok::kw_noreturn:
1269 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001270 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001271 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001272 case lltok::kw_returns_twice:
1273 case lltok::kw_sanitize_address:
1274 case lltok::kw_sanitize_memory:
1275 case lltok::kw_sanitize_thread:
1276 case lltok::kw_ssp:
1277 case lltok::kw_sspreq:
1278 case lltok::kw_sspstrong:
1279 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001280 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001281 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001282
1283 case lltok::kw_readnone:
1284 case lltok::kw_readonly:
1285 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001286 }
1287
Chris Lattnerac161bf2009-01-02 07:01:27 +00001288 Lex.Lex();
1289 }
1290}
1291
1292/// ParseOptionalLinkage
1293/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001294/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001295/// ::= 'internal'
1296/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001297/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001298/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001299/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001300/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001301/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001302/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001303/// ::= 'extern_weak'
1304/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001305///
1306/// Deprecated Values:
1307/// ::= 'linker_private'
1308/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001309bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1310 HasLinkage = false;
1311 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001312 default: Res=GlobalValue::ExternalLinkage; return false;
1313 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001314 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1315 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1316 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1317 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1318 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001319 case lltok::kw_available_externally:
1320 Res = GlobalValue::AvailableExternallyLinkage;
1321 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001322 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001323 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001324 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1325 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001326
1327 case lltok::kw_linker_private:
1328 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001329 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1330 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001331 Lex.Lex();
1332 // treat linker_private and linker_private_weak as PrivateLinkage
1333 Res = GlobalValue::PrivateLinkage;
1334 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001335 }
1336 Lex.Lex();
1337 HasLinkage = true;
1338 return false;
1339}
1340
1341/// ParseOptionalVisibility
1342/// ::= /*empty*/
1343/// ::= 'default'
1344/// ::= 'hidden'
1345/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001346///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001347bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1348 switch (Lex.getKind()) {
1349 default: Res = GlobalValue::DefaultVisibility; return false;
1350 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1351 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1352 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1353 }
1354 Lex.Lex();
1355 return false;
1356}
1357
Nico Rieck7157bb72014-01-14 15:22:47 +00001358/// ParseOptionalDLLStorageClass
1359/// ::= /*empty*/
1360/// ::= 'dllimport'
1361/// ::= 'dllexport'
1362///
1363bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1364 switch (Lex.getKind()) {
1365 default: Res = GlobalValue::DefaultStorageClass; return false;
1366 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1367 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1368 }
1369 Lex.Lex();
1370 return false;
1371}
1372
Chris Lattnerac161bf2009-01-02 07:01:27 +00001373/// ParseOptionalCallingConv
1374/// ::= /*empty*/
1375/// ::= 'ccc'
1376/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001377/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001378/// ::= 'coldcc'
1379/// ::= 'x86_stdcallcc'
1380/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001381/// ::= 'x86_thiscallcc'
Reid Kleckner1c843222014-01-31 17:41:22 +00001382/// ::= 'x86_cdeclmethodcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001383/// ::= 'arm_apcscc'
1384/// ::= 'arm_aapcscc'
1385/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001386/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001387/// ::= 'ptx_kernel'
1388/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001389/// ::= 'spir_func'
1390/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001391/// ::= 'x86_64_sysvcc'
1392/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001393/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001394/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001395/// ::= 'preserve_mostcc'
1396/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001397/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001398///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001399bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001400 switch (Lex.getKind()) {
1401 default: CC = CallingConv::C; return false;
1402 case lltok::kw_ccc: CC = CallingConv::C; break;
1403 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1404 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1405 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1406 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001407 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner1c843222014-01-31 17:41:22 +00001408 case lltok::kw_x86_cdeclmethodcc:CC = CallingConv::X86_CDeclMethod; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001409 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1410 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1411 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001412 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001413 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1414 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001415 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1416 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001417 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001418 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1419 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001420 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001421 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001422 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1423 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001424 case lltok::kw_cc: {
1425 unsigned ArbitraryCC;
1426 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001427 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001428 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001429 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1430 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001431 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001432 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001433
Chris Lattnerac161bf2009-01-02 07:01:27 +00001434 Lex.Lex();
1435 return false;
1436}
1437
Chris Lattner5c427632009-12-30 05:31:19 +00001438/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001439/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001440bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1441 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001442 do {
1443 if (Lex.getKind() != lltok::MetadataVar)
1444 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001445
Chris Lattner596760d2009-12-29 21:25:40 +00001446 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001447 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001448 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001449
Chris Lattner1797fc72009-12-29 21:53:55 +00001450 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001451 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001452
1453 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001454 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001455
Dan Gohmanf0715b12010-08-24 14:35:45 +00001456 // This code is similar to that of ParseMetadataValue, however it needs to
1457 // have special-case code for a forward reference; see the comments on
1458 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1459 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001460 if (Lex.getKind() == lltok::lbrace) {
1461 ValID ID;
1462 if (ParseMetadataListValue(ID, PFS))
1463 return true;
1464 assert(ID.Kind == ValID::t_MDNode);
1465 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001466 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001467 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001468 if (ParseMDNodeID(Node, NodeID))
1469 return true;
1470 if (Node) {
1471 // If we got the node, add it to the instruction.
1472 Inst->setMetadata(MDK, Node);
1473 } else {
1474 MDRef R = { Loc, MDK, NodeID };
1475 // Otherwise, remember that this should be resolved later.
1476 ForwardRefInstMetadata[Inst].push_back(R);
1477 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001478 }
Chris Lattner596760d2009-12-29 21:25:40 +00001479
Manman Ren209b17c2013-09-28 00:22:27 +00001480 if (MDK == LLVMContext::MD_tbaa)
1481 InstsWithTBAATag.push_back(Inst);
1482
Chris Lattner596760d2009-12-29 21:25:40 +00001483 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001484 } while (EatIfPresent(lltok::comma));
1485 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001486}
1487
Chris Lattnerac161bf2009-01-02 07:01:27 +00001488/// ParseOptionalAlignment
1489/// ::= /* empty */
1490/// ::= 'align' 4
1491bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1492 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001493 if (!EatIfPresent(lltok::kw_align))
1494 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001495 LocTy AlignLoc = Lex.getLoc();
1496 if (ParseUInt32(Alignment)) return true;
1497 if (!isPowerOf2_32(Alignment))
1498 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001499 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001500 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001501 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001502}
1503
Chris Lattnerb2f39502009-12-30 05:44:30 +00001504/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001505/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001506/// ::= ',' align 4
1507///
1508/// This returns with AteExtraComma set to true if it ate an excess comma at the
1509/// end.
1510bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1511 bool &AteExtraComma) {
1512 AteExtraComma = false;
1513 while (EatIfPresent(lltok::comma)) {
1514 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001515 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001516 AteExtraComma = true;
1517 return false;
1518 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001519
Chris Lattner95b0ff42010-04-23 00:50:50 +00001520 if (Lex.getKind() != lltok::kw_align)
1521 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001522
Chris Lattner95b0ff42010-04-23 00:50:50 +00001523 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001524 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001525
Devang Patelea8a4b92009-09-17 23:04:48 +00001526 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001527}
1528
Eli Friedmanfee02c62011-07-25 23:16:38 +00001529/// ParseScopeAndOrdering
1530/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1531/// else: ::=
1532///
1533/// This sets Scope and Ordering to the parsed values.
1534bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1535 AtomicOrdering &Ordering) {
1536 if (!isAtomic)
1537 return false;
1538
1539 Scope = CrossThread;
1540 if (EatIfPresent(lltok::kw_singlethread))
1541 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001542
1543 return ParseOrdering(Ordering);
1544}
1545
1546/// ParseOrdering
1547/// ::= AtomicOrdering
1548///
1549/// This sets Ordering to the parsed value.
1550bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001551 switch (Lex.getKind()) {
1552 default: return TokError("Expected ordering on atomic instruction");
1553 case lltok::kw_unordered: Ordering = Unordered; break;
1554 case lltok::kw_monotonic: Ordering = Monotonic; break;
1555 case lltok::kw_acquire: Ordering = Acquire; break;
1556 case lltok::kw_release: Ordering = Release; break;
1557 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1558 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1559 }
1560 Lex.Lex();
1561 return false;
1562}
1563
Charles Davisbe5557e2010-02-12 00:31:15 +00001564/// ParseOptionalStackAlignment
1565/// ::= /* empty */
1566/// ::= 'alignstack' '(' 4 ')'
1567bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1568 Alignment = 0;
1569 if (!EatIfPresent(lltok::kw_alignstack))
1570 return false;
1571 LocTy ParenLoc = Lex.getLoc();
1572 if (!EatIfPresent(lltok::lparen))
1573 return Error(ParenLoc, "expected '('");
1574 LocTy AlignLoc = Lex.getLoc();
1575 if (ParseUInt32(Alignment)) return true;
1576 ParenLoc = Lex.getLoc();
1577 if (!EatIfPresent(lltok::rparen))
1578 return Error(ParenLoc, "expected ')'");
1579 if (!isPowerOf2_32(Alignment))
1580 return Error(AlignLoc, "stack alignment is not a power of two");
1581 return false;
1582}
Devang Patelea8a4b92009-09-17 23:04:48 +00001583
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001584/// ParseIndexList - This parses the index list for an insert/extractvalue
1585/// instruction. This sets AteExtraComma in the case where we eat an extra
1586/// comma at the end of the line and find that it is followed by metadata.
1587/// Clients that don't allow metadata can call the version of this function that
1588/// only takes one argument.
1589///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001590/// ParseIndexList
1591/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001592///
1593bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1594 bool &AteExtraComma) {
1595 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001596
Chris Lattnerac161bf2009-01-02 07:01:27 +00001597 if (Lex.getKind() != lltok::comma)
1598 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001599
Chris Lattner3822f632009-01-02 08:05:26 +00001600 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001601 if (Lex.getKind() == lltok::MetadataVar) {
1602 AteExtraComma = true;
1603 return false;
1604 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001605 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001606 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001607 Indices.push_back(Idx);
1608 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001609
Chris Lattnerac161bf2009-01-02 07:01:27 +00001610 return false;
1611}
1612
1613//===----------------------------------------------------------------------===//
1614// Type Parsing.
1615//===----------------------------------------------------------------------===//
1616
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001617/// ParseType - Parse a type.
1618bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1619 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001620 switch (Lex.getKind()) {
1621 default:
1622 return TokError("expected type");
1623 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001624 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001625 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001626 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001627 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001628 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001629 // Type ::= StructType
1630 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001631 return true;
1632 break;
1633 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001634 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001635 Lex.Lex(); // eat the lsquare.
1636 if (ParseArrayVectorType(Result, false))
1637 return true;
1638 break;
1639 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001640 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001641 Lex.Lex();
1642 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001643 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001644 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001645 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001646 } else if (ParseArrayVectorType(Result, true))
1647 return true;
1648 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001649 case lltok::LocalVar: {
1650 // Type ::= %foo
1651 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001652
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001653 // If the type hasn't been defined yet, create a forward definition and
1654 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001655 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001656 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001657 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001658 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001659 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001660 Lex.Lex();
1661 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001662 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001663
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001664 case lltok::LocalVarID: {
1665 // Type ::= %4
1666 if (Lex.getUIntVal() >= NumberedTypes.size())
1667 NumberedTypes.resize(Lex.getUIntVal()+1);
1668 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001669
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001670 // If the type hasn't been defined yet, create a forward definition and
1671 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001672 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001673 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001674 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001675 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001676 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001677 Lex.Lex();
1678 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001679 }
1680 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001681
1682 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001683 while (1) {
1684 switch (Lex.getKind()) {
1685 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001686 default:
1687 if (!AllowVoid && Result->isVoidTy())
1688 return Error(TypeLoc, "void type only allowed for function results");
1689 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001690
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001691 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001692 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001693 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001694 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001695 if (Result->isVoidTy())
1696 return TokError("pointers to void are invalid - use i8* instead");
1697 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001698 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001699 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001700 Lex.Lex();
1701 break;
1702
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001703 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001704 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001705 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001706 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001707 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001708 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001709 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001710 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001711 unsigned AddrSpace;
1712 if (ParseOptionalAddrSpace(AddrSpace) ||
1713 ParseToken(lltok::star, "expected '*' in address space"))
1714 return true;
1715
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001716 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001717 break;
1718 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001719
Chris Lattnerac161bf2009-01-02 07:01:27 +00001720 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1721 case lltok::lparen:
1722 if (ParseFunctionType(Result))
1723 return true;
1724 break;
1725 }
1726 }
1727}
1728
1729/// ParseParameterList
1730/// ::= '(' ')'
1731/// ::= '(' Arg (',' Arg)* ')'
1732/// Arg
1733/// ::= Type OptionalAttributes Value OptionalAttributes
1734bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1735 PerFunctionState &PFS) {
1736 if (ParseToken(lltok::lparen, "expected '(' in call"))
1737 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001738
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001739 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001740 while (Lex.getKind() != lltok::rparen) {
1741 // If this isn't the first argument, we need a comma.
1742 if (!ArgList.empty() &&
1743 ParseToken(lltok::comma, "expected ',' in argument list"))
1744 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001745
Chris Lattnerac161bf2009-01-02 07:01:27 +00001746 // Parse the argument.
1747 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001748 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001749 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001750 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001751 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001752 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001753
Chris Lattner5b4a9622009-12-30 02:11:14 +00001754 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001755 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001756 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001757 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1758 AttrIndex++,
1759 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001760 }
1761
1762 Lex.Lex(); // Lex the ')'.
1763 return false;
1764}
1765
1766
1767
Chris Lattner2ed06b42009-01-05 18:34:07 +00001768/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001769/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001770/// ::= '(' ArgTypeListI ')'
1771/// ArgTypeListI
1772/// ::= /*empty*/
1773/// ::= '...'
1774/// ::= ArgTypeList ',' '...'
1775/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001776///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001777bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1778 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001779 isVarArg = false;
1780 assert(Lex.getKind() == lltok::lparen);
1781 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001782
Chris Lattnerac161bf2009-01-02 07:01:27 +00001783 if (Lex.getKind() == lltok::rparen) {
1784 // empty
1785 } else if (Lex.getKind() == lltok::dotdotdot) {
1786 isVarArg = true;
1787 Lex.Lex();
1788 } else {
1789 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001790 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001791 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001792 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001793
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001794 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001795 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001796
Chris Lattnerfdd87902009-10-05 05:54:46 +00001797 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001798 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001799
Chris Lattnerdef19492011-06-17 06:36:20 +00001800 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001801 Name = Lex.getStrVal();
1802 Lex.Lex();
1803 }
Chris Lattner3822f632009-01-02 08:05:26 +00001804
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001805 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001806 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001807
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001808 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001809 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001810 AttributeSet::get(ArgTy->getContext(),
1811 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001812
Chris Lattner3822f632009-01-02 08:05:26 +00001813 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001814 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001815 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001816 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001817 break;
1818 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001819
Chris Lattnerac161bf2009-01-02 07:01:27 +00001820 // Otherwise must be an argument type.
1821 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001822 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001823
Chris Lattnerfdd87902009-10-05 05:54:46 +00001824 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001825 return Error(TypeLoc, "argument can not have void type");
1826
Chris Lattnerdef19492011-06-17 06:36:20 +00001827 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001828 Name = Lex.getStrVal();
1829 Lex.Lex();
1830 } else {
1831 Name = "";
1832 }
Chris Lattner3822f632009-01-02 08:05:26 +00001833
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001834 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001835 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001836
Bill Wendlingd079a442012-10-15 04:46:55 +00001837 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001838 AttributeSet::get(ArgTy->getContext(),
1839 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001840 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001841 }
1842 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001843
Chris Lattner3822f632009-01-02 08:05:26 +00001844 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001845}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001846
Chris Lattnerac161bf2009-01-02 07:01:27 +00001847/// ParseFunctionType
1848/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001849bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001850 assert(Lex.getKind() == lltok::lparen);
1851
Chris Lattnerce473c72009-01-05 08:04:33 +00001852 if (!FunctionType::isValidReturnType(Result))
1853 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001854
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001855 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001856 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001857 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001858 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001859
Chris Lattnerac161bf2009-01-02 07:01:27 +00001860 // Reject names on the arguments lists.
1861 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1862 if (!ArgList[i].Name.empty())
1863 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001864 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001865 return Error(ArgList[i].Loc,
1866 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001867 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001868
Jay Foadb804a2b2011-07-12 14:06:48 +00001869 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001870 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001871 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001872
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001873 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001874 return false;
1875}
1876
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001877/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1878/// other structs.
1879bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1880 SmallVector<Type*, 8> Elts;
1881 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001882
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001883 Result = StructType::get(Context, Elts, Packed);
1884 return false;
1885}
1886
1887/// ParseStructDefinition - Parse a struct in a 'type' definition.
1888bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1889 std::pair<Type*, LocTy> &Entry,
1890 Type *&ResultTy) {
1891 // If the type was already defined, diagnose the redefinition.
1892 if (Entry.first && !Entry.second.isValid())
1893 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001894
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001895 // If we have opaque, just return without filling in the definition for the
1896 // struct. This counts as a definition as far as the .ll file goes.
1897 if (EatIfPresent(lltok::kw_opaque)) {
1898 // This type is being defined, so clear the location to indicate this.
1899 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001900
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001901 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001902 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001903 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001904 ResultTy = Entry.first;
1905 return false;
1906 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001907
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001908 // If the type starts with '<', then it is either a packed struct or a vector.
1909 bool isPacked = EatIfPresent(lltok::less);
1910
1911 // If we don't have a struct, then we have a random type alias, which we
1912 // accept for compatibility with old files. These types are not allowed to be
1913 // forward referenced and not allowed to be recursive.
1914 if (Lex.getKind() != lltok::lbrace) {
1915 if (Entry.first)
1916 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001917
Craig Topper2617dcc2014-04-15 06:32:26 +00001918 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001919 if (isPacked)
1920 return ParseArrayVectorType(ResultTy, true);
1921 return ParseType(ResultTy);
1922 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001923
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001924 // This type is being defined, so clear the location to indicate this.
1925 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001926
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001927 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001928 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001929 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001930
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001931 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001932
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001933 SmallVector<Type*, 8> Body;
1934 if (ParseStructBody(Body) ||
1935 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1936 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001937
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001938 STy->setBody(Body, isPacked);
1939 ResultTy = STy;
1940 return false;
1941}
1942
1943
Chris Lattnerac161bf2009-01-02 07:01:27 +00001944/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001945/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001946/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001947/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001948/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001949/// ::= '<' '{' Type (',' Type)* '}' '>'
1950bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001951 assert(Lex.getKind() == lltok::lbrace);
1952 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001953
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001954 // Handle the empty struct.
1955 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001956 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001957
Chris Lattnerf880ca22009-03-09 04:49:14 +00001958 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001959 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001960 if (ParseType(Ty)) return true;
1961 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001962
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001963 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001964 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001965
Chris Lattner3822f632009-01-02 08:05:26 +00001966 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001967 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001968 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001969
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001970 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001971 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001972
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001973 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001974 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001975
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001976 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001977}
1978
1979/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1980/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001981/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00001982/// ::= '[' APSINTVAL 'x' Types ']'
1983/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001984bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001985 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1986 Lex.getAPSIntVal().getBitWidth() > 64)
1987 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001988
Chris Lattnerac161bf2009-01-02 07:01:27 +00001989 LocTy SizeLoc = Lex.getLoc();
1990 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00001991 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001992
Chris Lattner3822f632009-01-02 08:05:26 +00001993 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1994 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001995
1996 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001997 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001998 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00001999
Chris Lattner3822f632009-01-02 08:05:26 +00002000 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2001 "expected end of sequential type"))
2002 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002003
Chris Lattnerac161bf2009-01-02 07:01:27 +00002004 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002005 if (Size == 0)
2006 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002007 if ((unsigned)Size != Size)
2008 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002009 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002010 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002011 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002012 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002013 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002014 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002015 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002016 }
2017 return false;
2018}
2019
2020//===----------------------------------------------------------------------===//
2021// Function Semantic Analysis.
2022//===----------------------------------------------------------------------===//
2023
Chris Lattner3432c622009-10-28 03:39:23 +00002024LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2025 int functionNumber)
2026 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002027
2028 // Insert unnamed arguments into the NumberedVals list.
2029 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2030 AI != E; ++AI)
2031 if (!AI->hasName())
2032 NumberedVals.push_back(AI);
2033}
2034
2035LLParser::PerFunctionState::~PerFunctionState() {
2036 // If there were any forward referenced non-basicblock values, delete them.
2037 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2038 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2039 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002040 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002041 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002042 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002043 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002044 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002045
Chris Lattnerac161bf2009-01-02 07:01:27 +00002046 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2047 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2048 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002049 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002050 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002051 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002052 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002053 }
2054}
2055
Chris Lattner3432c622009-10-28 03:39:23 +00002056bool LLParser::PerFunctionState::FinishFunction() {
2057 // Check to see if someone took the address of labels in this block.
2058 if (!P.ForwardRefBlockAddresses.empty()) {
2059 ValID FunctionID;
2060 if (!F.getName().empty()) {
2061 FunctionID.Kind = ValID::t_GlobalName;
2062 FunctionID.StrVal = F.getName();
2063 } else {
2064 FunctionID.Kind = ValID::t_GlobalID;
2065 FunctionID.UIntVal = FunctionNumber;
2066 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002067
Chris Lattner3432c622009-10-28 03:39:23 +00002068 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2069 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2070 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2071 // Resolve all these references.
2072 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2073 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002074
Chris Lattner3432c622009-10-28 03:39:23 +00002075 P.ForwardRefBlockAddresses.erase(FRBAI);
2076 }
2077 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002078
Chris Lattnerac161bf2009-01-02 07:01:27 +00002079 if (!ForwardRefVals.empty())
2080 return P.Error(ForwardRefVals.begin()->second.second,
2081 "use of undefined value '%" + ForwardRefVals.begin()->first +
2082 "'");
2083 if (!ForwardRefValIDs.empty())
2084 return P.Error(ForwardRefValIDs.begin()->second.second,
2085 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002086 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002087 return false;
2088}
2089
2090
2091/// GetVal - Get a value with the specified name or ID, creating a
2092/// forward reference record if needed. This can return null if the value
2093/// exists but does not have the right type.
2094Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002095 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002096 // Look this name up in the normal function symbol table.
2097 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002098
Chris Lattnerac161bf2009-01-02 07:01:27 +00002099 // If this is a forward reference for the value, see if we already created a
2100 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002101 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002102 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2103 I = ForwardRefVals.find(Name);
2104 if (I != ForwardRefVals.end())
2105 Val = I->second.first;
2106 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002107
Chris Lattnerac161bf2009-01-02 07:01:27 +00002108 // If we have the value in the symbol table or fwd-ref table, return it.
2109 if (Val) {
2110 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002111 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002112 P.Error(Loc, "'%" + Name + "' is not a basic block");
2113 else
2114 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002115 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002116 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002117 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002118
Chris Lattnerac161bf2009-01-02 07:01:27 +00002119 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002120 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002121 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002122 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002123 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002124
Chris Lattnerac161bf2009-01-02 07:01:27 +00002125 // Otherwise, create a new forward reference for this value and remember it.
2126 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002127 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002128 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002129 else
2130 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002131
Chris Lattnerac161bf2009-01-02 07:01:27 +00002132 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2133 return FwdVal;
2134}
2135
Chris Lattner229907c2011-07-18 04:54:35 +00002136Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002137 LocTy Loc) {
2138 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002139 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002140
Chris Lattnerac161bf2009-01-02 07:01:27 +00002141 // If this is a forward reference for the value, see if we already created a
2142 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002143 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002144 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2145 I = ForwardRefValIDs.find(ID);
2146 if (I != ForwardRefValIDs.end())
2147 Val = I->second.first;
2148 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002149
Chris Lattnerac161bf2009-01-02 07:01:27 +00002150 // If we have the value in the symbol table or fwd-ref table, return it.
2151 if (Val) {
2152 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002153 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002154 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002155 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002156 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002157 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002158 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002159 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002160
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002161 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002162 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002163 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002164 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002165
Chris Lattnerac161bf2009-01-02 07:01:27 +00002166 // Otherwise, create a new forward reference for this value and remember it.
2167 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002168 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002169 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002170 else
2171 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002172
Chris Lattnerac161bf2009-01-02 07:01:27 +00002173 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2174 return FwdVal;
2175}
2176
2177/// SetInstName - After an instruction is parsed and inserted into its
2178/// basic block, this installs its name.
2179bool LLParser::PerFunctionState::SetInstName(int NameID,
2180 const std::string &NameStr,
2181 LocTy NameLoc, Instruction *Inst) {
2182 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002183 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002184 if (NameID != -1 || !NameStr.empty())
2185 return P.Error(NameLoc, "instructions returning void cannot have a name");
2186 return false;
2187 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002188
Chris Lattnerac161bf2009-01-02 07:01:27 +00002189 // If this was a numbered instruction, verify that the instruction is the
2190 // expected value and resolve any forward references.
2191 if (NameStr.empty()) {
2192 // If neither a name nor an ID was specified, just use the next ID.
2193 if (NameID == -1)
2194 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002195
Chris Lattnerac161bf2009-01-02 07:01:27 +00002196 if (unsigned(NameID) != NumberedVals.size())
2197 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002198 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002199
Chris Lattnerac161bf2009-01-02 07:01:27 +00002200 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2201 ForwardRefValIDs.find(NameID);
2202 if (FI != ForwardRefValIDs.end()) {
2203 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002204 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002205 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002206 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002207 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002208 ForwardRefValIDs.erase(FI);
2209 }
2210
2211 NumberedVals.push_back(Inst);
2212 return false;
2213 }
2214
2215 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2216 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2217 FI = ForwardRefVals.find(NameStr);
2218 if (FI != ForwardRefVals.end()) {
2219 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002220 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002221 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002222 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002223 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002224 ForwardRefVals.erase(FI);
2225 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002226
Chris Lattnerac161bf2009-01-02 07:01:27 +00002227 // Set the name on the instruction.
2228 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002229
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002230 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002231 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002232 NameStr + "'");
2233 return false;
2234}
2235
2236/// GetBB - Get a basic block with the specified name or ID, creating a
2237/// forward reference record if needed.
2238BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2239 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002240 return cast_or_null<BasicBlock>(GetVal(Name,
2241 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002242}
2243
2244BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002245 return cast_or_null<BasicBlock>(GetVal(ID,
2246 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002247}
2248
2249/// DefineBB - Define the specified basic block, which is either named or
2250/// unnamed. If there is an error, this returns null otherwise it returns
2251/// the block being defined.
2252BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2253 LocTy Loc) {
2254 BasicBlock *BB;
2255 if (Name.empty())
2256 BB = GetBB(NumberedVals.size(), Loc);
2257 else
2258 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002259 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002260
Chris Lattnerac161bf2009-01-02 07:01:27 +00002261 // Move the block to the end of the function. Forward ref'd blocks are
2262 // inserted wherever they happen to be referenced.
2263 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002264
Chris Lattnerac161bf2009-01-02 07:01:27 +00002265 // Remove the block from forward ref sets.
2266 if (Name.empty()) {
2267 ForwardRefValIDs.erase(NumberedVals.size());
2268 NumberedVals.push_back(BB);
2269 } else {
2270 // BB forward references are already in the function symbol table.
2271 ForwardRefVals.erase(Name);
2272 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002273
Chris Lattnerac161bf2009-01-02 07:01:27 +00002274 return BB;
2275}
2276
2277//===----------------------------------------------------------------------===//
2278// Constants.
2279//===----------------------------------------------------------------------===//
2280
2281/// ParseValID - Parse an abstract value that doesn't necessarily have a
2282/// type implied. For example, if we parse "4" we don't know what integer type
2283/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002284/// sanity. PFS is used to convert function-local operands of metadata (since
2285/// metadata operands are not just parsed here but also converted to values).
2286/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002287bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002288 ID.Loc = Lex.getLoc();
2289 switch (Lex.getKind()) {
2290 default: return TokError("expected value token");
2291 case lltok::GlobalID: // @42
2292 ID.UIntVal = Lex.getUIntVal();
2293 ID.Kind = ValID::t_GlobalID;
2294 break;
2295 case lltok::GlobalVar: // @foo
2296 ID.StrVal = Lex.getStrVal();
2297 ID.Kind = ValID::t_GlobalName;
2298 break;
2299 case lltok::LocalVarID: // %42
2300 ID.UIntVal = Lex.getUIntVal();
2301 ID.Kind = ValID::t_LocalID;
2302 break;
2303 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002304 ID.StrVal = Lex.getStrVal();
2305 ID.Kind = ValID::t_LocalName;
2306 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002307 case lltok::exclaim: // !42, !{...}, or !"foo"
2308 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002309 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002310 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002311 ID.Kind = ValID::t_APSInt;
2312 break;
2313 case lltok::APFloat:
2314 ID.APFloatVal = Lex.getAPFloatVal();
2315 ID.Kind = ValID::t_APFloat;
2316 break;
2317 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002318 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002319 ID.Kind = ValID::t_Constant;
2320 break;
2321 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002322 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002323 ID.Kind = ValID::t_Constant;
2324 break;
2325 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2326 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2327 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002328
Chris Lattnerac161bf2009-01-02 07:01:27 +00002329 case lltok::lbrace: {
2330 // ValID ::= '{' ConstVector '}'
2331 Lex.Lex();
2332 SmallVector<Constant*, 16> Elts;
2333 if (ParseGlobalValueVector(Elts) ||
2334 ParseToken(lltok::rbrace, "expected end of struct constant"))
2335 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002336
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002337 ID.ConstantStructElts = new Constant*[Elts.size()];
2338 ID.UIntVal = Elts.size();
2339 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2340 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002341 return false;
2342 }
2343 case lltok::less: {
2344 // ValID ::= '<' ConstVector '>' --> Vector.
2345 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2346 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002347 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002348
Chris Lattnerac161bf2009-01-02 07:01:27 +00002349 SmallVector<Constant*, 16> Elts;
2350 LocTy FirstEltLoc = Lex.getLoc();
2351 if (ParseGlobalValueVector(Elts) ||
2352 (isPackedStruct &&
2353 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2354 ParseToken(lltok::greater, "expected end of constant"))
2355 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002356
Chris Lattnerac161bf2009-01-02 07:01:27 +00002357 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002358 ID.ConstantStructElts = new Constant*[Elts.size()];
2359 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2360 ID.UIntVal = Elts.size();
2361 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002362 return false;
2363 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002364
Chris Lattnerac161bf2009-01-02 07:01:27 +00002365 if (Elts.empty())
2366 return Error(ID.Loc, "constant vector must not be empty");
2367
Duncan Sands9dff9be2010-02-15 16:12:20 +00002368 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002369 !Elts[0]->getType()->isFloatingPointTy() &&
2370 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002371 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002372 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002373
Chris Lattnerac161bf2009-01-02 07:01:27 +00002374 // Verify that all the vector elements have the same type.
2375 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2376 if (Elts[i]->getType() != Elts[0]->getType())
2377 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002378 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002379 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002380
Chris Lattner69229312011-02-15 00:14:00 +00002381 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002382 ID.Kind = ValID::t_Constant;
2383 return false;
2384 }
2385 case lltok::lsquare: { // Array Constant
2386 Lex.Lex();
2387 SmallVector<Constant*, 16> Elts;
2388 LocTy FirstEltLoc = Lex.getLoc();
2389 if (ParseGlobalValueVector(Elts) ||
2390 ParseToken(lltok::rsquare, "expected end of array constant"))
2391 return true;
2392
2393 // Handle empty element.
2394 if (Elts.empty()) {
2395 // Use undef instead of an array because it's inconvenient to determine
2396 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002397 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002398 return false;
2399 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002400
Chris Lattnerac161bf2009-01-02 07:01:27 +00002401 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002402 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002403 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002404
Owen Anderson4056ca92009-07-29 22:17:13 +00002405 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002406
Chris Lattnerac161bf2009-01-02 07:01:27 +00002407 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002408 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002409 if (Elts[i]->getType() != Elts[0]->getType())
2410 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002411 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002412 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002413 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002414
Jay Foad83be3612011-06-22 09:24:39 +00002415 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002416 ID.Kind = ValID::t_Constant;
2417 return false;
2418 }
2419 case lltok::kw_c: // c "foo"
2420 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002421 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2422 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002423 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2424 ID.Kind = ValID::t_Constant;
2425 return false;
2426
2427 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002428 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2429 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002430 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002431 Lex.Lex();
2432 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002433 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002434 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002435 ParseStringConstant(ID.StrVal) ||
2436 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002437 ParseToken(lltok::StringConstant, "expected constraint string"))
2438 return true;
2439 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002440 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002441 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002442 ID.Kind = ValID::t_InlineAsm;
2443 return false;
2444 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002445
Chris Lattner3432c622009-10-28 03:39:23 +00002446 case lltok::kw_blockaddress: {
2447 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2448 Lex.Lex();
2449
2450 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002451
Chris Lattner3432c622009-10-28 03:39:23 +00002452 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2453 ParseValID(Fn) ||
2454 ParseToken(lltok::comma, "expected comma in block address expression")||
2455 ParseValID(Label) ||
2456 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2457 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002458
Chris Lattner3432c622009-10-28 03:39:23 +00002459 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2460 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002461 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002462 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002463
Chris Lattner3432c622009-10-28 03:39:23 +00002464 // Make a global variable as a placeholder for this reference.
2465 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2466 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002467 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002468 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2469 ID.ConstantVal = FwdRef;
2470 ID.Kind = ValID::t_Constant;
2471 return false;
2472 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002473
Chris Lattnerac161bf2009-01-02 07:01:27 +00002474 case lltok::kw_trunc:
2475 case lltok::kw_zext:
2476 case lltok::kw_sext:
2477 case lltok::kw_fptrunc:
2478 case lltok::kw_fpext:
2479 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002480 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002481 case lltok::kw_uitofp:
2482 case lltok::kw_sitofp:
2483 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002484 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002485 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002486 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002487 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002488 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002489 Constant *SrcVal;
2490 Lex.Lex();
2491 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2492 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002493 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002494 ParseType(DestTy) ||
2495 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2496 return true;
2497 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2498 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002499 getTypeString(SrcVal->getType()) + "' to '" +
2500 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002501 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002502 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002503 ID.Kind = ValID::t_Constant;
2504 return false;
2505 }
2506 case lltok::kw_extractvalue: {
2507 Lex.Lex();
2508 Constant *Val;
2509 SmallVector<unsigned, 4> Indices;
2510 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2511 ParseGlobalTypeAndValue(Val) ||
2512 ParseIndexList(Indices) ||
2513 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2514 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002515
Chris Lattner392be582010-02-12 20:49:41 +00002516 if (!Val->getType()->isAggregateType())
2517 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002518 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002519 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002520 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002521 ID.Kind = ValID::t_Constant;
2522 return false;
2523 }
2524 case lltok::kw_insertvalue: {
2525 Lex.Lex();
2526 Constant *Val0, *Val1;
2527 SmallVector<unsigned, 4> Indices;
2528 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2529 ParseGlobalTypeAndValue(Val0) ||
2530 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2531 ParseGlobalTypeAndValue(Val1) ||
2532 ParseIndexList(Indices) ||
2533 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2534 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002535 if (!Val0->getType()->isAggregateType())
2536 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002537 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002538 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002539 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002540 ID.Kind = ValID::t_Constant;
2541 return false;
2542 }
2543 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002544 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002545 unsigned PredVal, Opc = Lex.getUIntVal();
2546 Constant *Val0, *Val1;
2547 Lex.Lex();
2548 if (ParseCmpPredicate(PredVal, Opc) ||
2549 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2550 ParseGlobalTypeAndValue(Val0) ||
2551 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2552 ParseGlobalTypeAndValue(Val1) ||
2553 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2554 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002555
Chris Lattnerac161bf2009-01-02 07:01:27 +00002556 if (Val0->getType() != Val1->getType())
2557 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002558
Chris Lattnerac161bf2009-01-02 07:01:27 +00002559 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002560
Chris Lattnerac161bf2009-01-02 07:01:27 +00002561 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002562 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002563 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002564 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002565 } else {
2566 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002567 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002568 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002569 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002570 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002571 }
2572 ID.Kind = ValID::t_Constant;
2573 return false;
2574 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002575
Chris Lattnerac161bf2009-01-02 07:01:27 +00002576 // Binary Operators.
2577 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002578 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002579 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002580 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002581 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002582 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002583 case lltok::kw_udiv:
2584 case lltok::kw_sdiv:
2585 case lltok::kw_fdiv:
2586 case lltok::kw_urem:
2587 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002588 case lltok::kw_frem:
2589 case lltok::kw_shl:
2590 case lltok::kw_lshr:
2591 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002592 bool NUW = false;
2593 bool NSW = false;
2594 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002595 unsigned Opc = Lex.getUIntVal();
2596 Constant *Val0, *Val1;
2597 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002598 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002599 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2600 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002601 if (EatIfPresent(lltok::kw_nuw))
2602 NUW = true;
2603 if (EatIfPresent(lltok::kw_nsw)) {
2604 NSW = true;
2605 if (EatIfPresent(lltok::kw_nuw))
2606 NUW = true;
2607 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002608 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2609 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002610 if (EatIfPresent(lltok::kw_exact))
2611 Exact = true;
2612 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002613 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2614 ParseGlobalTypeAndValue(Val0) ||
2615 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2616 ParseGlobalTypeAndValue(Val1) ||
2617 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2618 return true;
2619 if (Val0->getType() != Val1->getType())
2620 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002621 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002622 if (NUW)
2623 return Error(ModifierLoc, "nuw only applies to integer operations");
2624 if (NSW)
2625 return Error(ModifierLoc, "nsw only applies to integer operations");
2626 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002627 // Check that the type is valid for the operator.
2628 switch (Opc) {
2629 case Instruction::Add:
2630 case Instruction::Sub:
2631 case Instruction::Mul:
2632 case Instruction::UDiv:
2633 case Instruction::SDiv:
2634 case Instruction::URem:
2635 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002636 case Instruction::Shl:
2637 case Instruction::AShr:
2638 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002639 if (!Val0->getType()->isIntOrIntVectorTy())
2640 return Error(ID.Loc, "constexpr requires integer operands");
2641 break;
2642 case Instruction::FAdd:
2643 case Instruction::FSub:
2644 case Instruction::FMul:
2645 case Instruction::FDiv:
2646 case Instruction::FRem:
2647 if (!Val0->getType()->isFPOrFPVectorTy())
2648 return Error(ID.Loc, "constexpr requires fp operands");
2649 break;
2650 default: llvm_unreachable("Unknown binary operator!");
2651 }
Dan Gohman1b849082009-09-07 23:54:19 +00002652 unsigned Flags = 0;
2653 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2654 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002655 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002656 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002657 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002658 ID.Kind = ValID::t_Constant;
2659 return false;
2660 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002661
Chris Lattnerac161bf2009-01-02 07:01:27 +00002662 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002663 case lltok::kw_and:
2664 case lltok::kw_or:
2665 case lltok::kw_xor: {
2666 unsigned Opc = Lex.getUIntVal();
2667 Constant *Val0, *Val1;
2668 Lex.Lex();
2669 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2670 ParseGlobalTypeAndValue(Val0) ||
2671 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2672 ParseGlobalTypeAndValue(Val1) ||
2673 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2674 return true;
2675 if (Val0->getType() != Val1->getType())
2676 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002677 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002678 return Error(ID.Loc,
2679 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002680 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002681 ID.Kind = ValID::t_Constant;
2682 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002683 }
2684
Chris Lattnerac161bf2009-01-02 07:01:27 +00002685 case lltok::kw_getelementptr:
2686 case lltok::kw_shufflevector:
2687 case lltok::kw_insertelement:
2688 case lltok::kw_extractelement:
2689 case lltok::kw_select: {
2690 unsigned Opc = Lex.getUIntVal();
2691 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002692 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002693 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002694 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002695 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002696 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2697 ParseGlobalValueVector(Elts) ||
2698 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2699 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002700
Chris Lattnerac161bf2009-01-02 07:01:27 +00002701 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002702 if (Elts.size() == 0 ||
2703 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002704 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002705
Jay Foaded8db7d2011-07-21 14:31:17 +00002706 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002707 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002708 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002709 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2710 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002711 } else if (Opc == Instruction::Select) {
2712 if (Elts.size() != 3)
2713 return Error(ID.Loc, "expected three operands to select");
2714 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2715 Elts[2]))
2716 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002717 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002718 } else if (Opc == Instruction::ShuffleVector) {
2719 if (Elts.size() != 3)
2720 return Error(ID.Loc, "expected three operands to shufflevector");
2721 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2722 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002723 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002724 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002725 } else if (Opc == Instruction::ExtractElement) {
2726 if (Elts.size() != 2)
2727 return Error(ID.Loc, "expected two operands to extractelement");
2728 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2729 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002730 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002731 } else {
2732 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2733 if (Elts.size() != 3)
2734 return Error(ID.Loc, "expected three operands to insertelement");
2735 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2736 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002737 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002738 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002739 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002740
Chris Lattnerac161bf2009-01-02 07:01:27 +00002741 ID.Kind = ValID::t_Constant;
2742 return false;
2743 }
2744 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002745
Chris Lattnerac161bf2009-01-02 07:01:27 +00002746 Lex.Lex();
2747 return false;
2748}
2749
2750/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002751bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002752 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002753 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002754 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002755 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002756 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002757 if (V && !(C = dyn_cast<Constant>(V)))
2758 return Error(ID.Loc, "global values must be constants");
2759 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002760}
2761
Victor Hernandez9d75c962010-01-11 22:31:58 +00002762bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002763 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002764 return ParseType(Ty) ||
2765 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002766}
2767
2768/// ParseGlobalValueVector
2769/// ::= /*empty*/
2770/// ::= TypeAndValue (',' TypeAndValue)*
2771bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2772 // Empty list.
2773 if (Lex.getKind() == lltok::rbrace ||
2774 Lex.getKind() == lltok::rsquare ||
2775 Lex.getKind() == lltok::greater ||
2776 Lex.getKind() == lltok::rparen)
2777 return false;
2778
2779 Constant *C;
2780 if (ParseGlobalTypeAndValue(C)) return true;
2781 Elts.push_back(C);
2782
2783 while (EatIfPresent(lltok::comma)) {
2784 if (ParseGlobalTypeAndValue(C)) return true;
2785 Elts.push_back(C);
2786 }
2787
2788 return false;
2789}
2790
Dan Gohmanc828c542010-08-24 02:24:03 +00002791bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2792 assert(Lex.getKind() == lltok::lbrace);
2793 Lex.Lex();
2794
2795 SmallVector<Value*, 16> Elts;
2796 if (ParseMDNodeVector(Elts, PFS) ||
2797 ParseToken(lltok::rbrace, "expected end of metadata node"))
2798 return true;
2799
Jay Foad5514afe2011-04-21 19:59:31 +00002800 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002801 ID.Kind = ValID::t_MDNode;
2802 return false;
2803}
2804
Dan Gohman8939ba332010-07-14 18:26:50 +00002805/// ParseMetadataValue
2806/// ::= !42
2807/// ::= !{...}
2808/// ::= !"string"
2809bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2810 assert(Lex.getKind() == lltok::exclaim);
2811 Lex.Lex();
2812
2813 // MDNode:
2814 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002815 if (Lex.getKind() == lltok::lbrace)
2816 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002817
2818 // Standalone metadata reference
2819 // !42
2820 if (Lex.getKind() == lltok::APSInt) {
2821 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2822 ID.Kind = ValID::t_MDNode;
2823 return false;
2824 }
2825
2826 // MDString:
2827 // ::= '!' STRINGCONSTANT
2828 if (ParseMDString(ID.MDStringVal)) return true;
2829 ID.Kind = ValID::t_MDString;
2830 return false;
2831}
2832
Victor Hernandez9d75c962010-01-11 22:31:58 +00002833
2834//===----------------------------------------------------------------------===//
2835// Function Parsing.
2836//===----------------------------------------------------------------------===//
2837
Chris Lattner229907c2011-07-18 04:54:35 +00002838bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002839 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002840 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002841 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002842
Chris Lattnerac161bf2009-01-02 07:01:27 +00002843 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002844 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002845 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2846 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002847 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002848 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002849 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2850 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002851 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002852 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002853 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002854 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002855 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002856 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2857 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002858 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002859 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002860 return false;
2861 }
2862 case ValID::t_MDNode:
2863 if (!Ty->isMetadataTy())
2864 return Error(ID.Loc, "metadata value must have metadata type");
2865 V = ID.MDNodeVal;
2866 return false;
2867 case ValID::t_MDString:
2868 if (!Ty->isMetadataTy())
2869 return Error(ID.Loc, "metadata value must have metadata type");
2870 V = ID.MDStringVal;
2871 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002872 case ValID::t_GlobalName:
2873 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002874 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002875 case ValID::t_GlobalID:
2876 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002877 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002878 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002879 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002880 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002881 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002882 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002883 return false;
2884 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002885 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002886 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2887 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002888
Dan Gohman518cda42011-12-17 00:04:22 +00002889 // The lexer has no type info, so builds all half, float, and double FP
2890 // constants as double. Fix this here. Long double does not need this.
2891 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002892 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002893 if (Ty->isHalfTy())
2894 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2895 &Ignored);
2896 else if (Ty->isFloatTy())
2897 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2898 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002899 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002900 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002901
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002902 if (V->getType() != Ty)
2903 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002904 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002905
Chris Lattnerac161bf2009-01-02 07:01:27 +00002906 return false;
2907 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002908 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002909 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002910 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002911 return false;
2912 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002913 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002914 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002915 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002916 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002917 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002918 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002919 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002920 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002921 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002922 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002923 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002924 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002925 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002926 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002927 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002928 return false;
2929 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002930 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002931 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002932
Chris Lattnerac161bf2009-01-02 07:01:27 +00002933 V = ID.ConstantVal;
2934 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002935 case ValID::t_ConstantStruct:
2936 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002937 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002938 if (ST->getNumElements() != ID.UIntVal)
2939 return Error(ID.Loc,
2940 "initializer with struct type has wrong # elements");
2941 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2942 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002943
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002944 // Verify that the elements are compatible with the structtype.
2945 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2946 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2947 return Error(ID.Loc, "element " + Twine(i) +
2948 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002949
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002950 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2951 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002952 } else
2953 return Error(ID.Loc, "constant expression type mismatch");
2954 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002955 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002956 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002957}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002958
Chris Lattner229907c2011-07-18 04:54:35 +00002959bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002960 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002961 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002962 return ParseValID(ID, PFS) ||
2963 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002964}
2965
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002966bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002967 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002968 return ParseType(Ty) ||
2969 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002970}
2971
Chris Lattner3ed871f2009-10-27 19:13:16 +00002972bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2973 PerFunctionState &PFS) {
2974 Value *V;
2975 Loc = Lex.getLoc();
2976 if (ParseTypeAndValue(V, PFS)) return true;
2977 if (!isa<BasicBlock>(V))
2978 return Error(Loc, "expected a basic block");
2979 BB = cast<BasicBlock>(V);
2980 return false;
2981}
2982
2983
Chris Lattnerac161bf2009-01-02 07:01:27 +00002984/// FunctionHeader
2985/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00002986/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002987/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00002988bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2989 // Parse the linkage.
2990 LocTy LinkageLoc = Lex.getLoc();
2991 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002992
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00002993 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00002994 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00002995 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00002996 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00002997 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002998 LocTy RetTypeLoc = Lex.getLoc();
2999 if (ParseOptionalLinkage(Linkage) ||
3000 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003001 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003002 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003003 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003004 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003005 return true;
3006
3007 // Verify that the linkage is ok.
3008 switch ((GlobalValue::LinkageTypes)Linkage) {
3009 case GlobalValue::ExternalLinkage:
3010 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003011 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003012 if (isDefine)
3013 return Error(LinkageLoc, "invalid linkage for function definition");
3014 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003015 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003016 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003017 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003018 case GlobalValue::LinkOnceAnyLinkage:
3019 case GlobalValue::LinkOnceODRLinkage:
3020 case GlobalValue::WeakAnyLinkage:
3021 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003022 if (!isDefine)
3023 return Error(LinkageLoc, "invalid linkage for function declaration");
3024 break;
3025 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003026 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003027 return Error(LinkageLoc, "invalid function linkage type");
3028 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003029
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003030 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3031 return Error(LinkageLoc,
3032 "symbol with local linkage must have default visibility");
3033
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003034 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003035 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003036
Chris Lattnerac161bf2009-01-02 07:01:27 +00003037 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003038
3039 std::string FunctionName;
3040 if (Lex.getKind() == lltok::GlobalVar) {
3041 FunctionName = Lex.getStrVal();
3042 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3043 unsigned NameID = Lex.getUIntVal();
3044
3045 if (NameID != NumberedVals.size())
3046 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003047 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003048 } else {
3049 return TokError("expected function name");
3050 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003051
Chris Lattner3822f632009-01-02 08:05:26 +00003052 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003053
Chris Lattner3822f632009-01-02 08:05:26 +00003054 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003055 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003056
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003057 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003058 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003059 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003060 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003061 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003062 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003063 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003064 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003065 bool UnnamedAddr;
3066 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003067 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003068
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003069 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003070 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3071 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003072 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003073 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003074 (EatIfPresent(lltok::kw_section) &&
3075 ParseStringConstant(Section)) ||
3076 ParseOptionalAlignment(Alignment) ||
3077 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003078 ParseStringConstant(GC)) ||
3079 (EatIfPresent(lltok::kw_prefix) &&
3080 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003081 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003082
Michael Gottesman41748d72013-06-27 00:25:01 +00003083 if (FuncAttrs.contains(Attribute::Builtin))
3084 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003085
Chris Lattnerac161bf2009-01-02 07:01:27 +00003086 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003087 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003088 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003089 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003090 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003091
Chris Lattnerac161bf2009-01-02 07:01:27 +00003092 // Okay, if we got here, the function is syntactically valid. Convert types
3093 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003094 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003095 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003096
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003097 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003098 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3099 AttributeSet::ReturnIndex,
3100 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003101
Chris Lattnerac161bf2009-01-02 07:01:27 +00003102 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003103 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003104 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3105 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003106 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3107 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003108 }
3109
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003110 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003111 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3112 AttributeSet::FunctionIndex,
3113 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003114
Bill Wendlinge94d8432012-12-07 23:16:57 +00003115 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003116
Bill Wendling749a43d2012-12-30 13:50:49 +00003117 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003118 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3119
Chris Lattner229907c2011-07-18 04:54:35 +00003120 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003121 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003122 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003123
Craig Topper2617dcc2014-04-15 06:32:26 +00003124 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003125 if (!FunctionName.empty()) {
3126 // If this was a definition of a forward reference, remove the definition
3127 // from the forward reference table and fill in the forward ref.
3128 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3129 ForwardRefVals.find(FunctionName);
3130 if (FRVI != ForwardRefVals.end()) {
3131 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003132 if (!Fn)
3133 return Error(FRVI->second.second, "invalid forward reference to "
3134 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003135 if (Fn->getType() != PFT)
3136 return Error(FRVI->second.second, "invalid forward reference to "
3137 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003138
Chris Lattnerac161bf2009-01-02 07:01:27 +00003139 ForwardRefVals.erase(FRVI);
3140 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003141 // Reject redefinitions.
3142 return Error(NameLoc, "invalid redefinition of function '" +
3143 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003144 } else if (M->getNamedValue(FunctionName)) {
3145 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003146 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003147
Dan Gohman399d6ae2009-08-29 23:37:49 +00003148 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003149 // If this is a definition of a forward referenced function, make sure the
3150 // types agree.
3151 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3152 = ForwardRefValIDs.find(NumberedVals.size());
3153 if (I != ForwardRefValIDs.end()) {
3154 Fn = cast<Function>(I->second.first);
3155 if (Fn->getType() != PFT)
3156 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003157 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003158 ForwardRefValIDs.erase(I);
3159 }
3160 }
3161
Craig Topper2617dcc2014-04-15 06:32:26 +00003162 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003163 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3164 else // Move the forward-reference to the correct spot in the module.
3165 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3166
3167 if (FunctionName.empty())
3168 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003169
Chris Lattnerac161bf2009-01-02 07:01:27 +00003170 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3171 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003172 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003173 Fn->setCallingConv(CC);
3174 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003175 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003176 Fn->setAlignment(Alignment);
3177 Fn->setSection(Section);
3178 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003179 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003180 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003181
Chris Lattnerac161bf2009-01-02 07:01:27 +00003182 // Add all of the arguments we parsed to the function.
3183 Function::arg_iterator ArgIt = Fn->arg_begin();
3184 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3185 // If the argument has a name, insert it into the argument symbol table.
3186 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003187
Chris Lattnerac161bf2009-01-02 07:01:27 +00003188 // Set the name, if it conflicted, it will be auto-renamed.
3189 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003190
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003191 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003192 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3193 ArgList[i].Name + "'");
3194 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003195
Chris Lattnerac161bf2009-01-02 07:01:27 +00003196 return false;
3197}
3198
3199
3200/// ParseFunctionBody
3201/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003202///
3203bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003204 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003205 return TokError("expected '{' in function body");
3206 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003207
Chris Lattner3432c622009-10-28 03:39:23 +00003208 int FunctionNumber = -1;
3209 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003210
Chris Lattner3432c622009-10-28 03:39:23 +00003211 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003212
Chris Lattnerbbddd962010-01-09 19:20:07 +00003213 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003214 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003215 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003216
Chris Lattner4649a732011-06-17 06:42:57 +00003217 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003218 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003219
Chris Lattnerac161bf2009-01-02 07:01:27 +00003220 // Eat the }.
3221 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003222
Chris Lattnerac161bf2009-01-02 07:01:27 +00003223 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003224 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003225}
3226
3227/// ParseBasicBlock
3228/// ::= LabelStr? Instruction*
3229bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3230 // If this basic block starts out with a name, remember it.
3231 std::string Name;
3232 LocTy NameLoc = Lex.getLoc();
3233 if (Lex.getKind() == lltok::LabelStr) {
3234 Name = Lex.getStrVal();
3235 Lex.Lex();
3236 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003237
Chris Lattnerac161bf2009-01-02 07:01:27 +00003238 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003239 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003240
Chris Lattnerac161bf2009-01-02 07:01:27 +00003241 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003242
Chris Lattnerac161bf2009-01-02 07:01:27 +00003243 // Parse the instructions in this block until we get a terminator.
3244 Instruction *Inst;
3245 do {
3246 // This instruction may have three possibilities for a name: a) none
3247 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3248 LocTy NameLoc = Lex.getLoc();
3249 int NameID = -1;
3250 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003251
Chris Lattnerac161bf2009-01-02 07:01:27 +00003252 if (Lex.getKind() == lltok::LocalVarID) {
3253 NameID = Lex.getUIntVal();
3254 Lex.Lex();
3255 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3256 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003257 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003258 NameStr = Lex.getStrVal();
3259 Lex.Lex();
3260 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3261 return true;
3262 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003263
Chris Lattner77b89dc2009-12-30 05:23:43 +00003264 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003265 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003266 case InstError: return true;
3267 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003268 BB->getInstList().push_back(Inst);
3269
Chris Lattner77b89dc2009-12-30 05:23:43 +00003270 // With a normal result, we check to see if the instruction is followed by
3271 // a comma and metadata.
3272 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003273 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003274 return true;
3275 break;
3276 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003277 BB->getInstList().push_back(Inst);
3278
Chris Lattner77b89dc2009-12-30 05:23:43 +00003279 // If the instruction parser ate an extra comma at the end of it, it
3280 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003281 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003282 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003283 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003284 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003285
Chris Lattnerac161bf2009-01-02 07:01:27 +00003286 // Set the name on the instruction.
3287 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3288 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003289
Chris Lattnerac161bf2009-01-02 07:01:27 +00003290 return false;
3291}
3292
3293//===----------------------------------------------------------------------===//
3294// Instruction Parsing.
3295//===----------------------------------------------------------------------===//
3296
3297/// ParseInstruction - Parse one of the many different instructions.
3298///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003299int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3300 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003301 lltok::Kind Token = Lex.getKind();
3302 if (Token == lltok::Eof)
3303 return TokError("found end of file when expecting more instructions");
3304 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003305 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003306 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003307
Chris Lattnerac161bf2009-01-02 07:01:27 +00003308 switch (Token) {
3309 default: return Error(Loc, "expected instruction opcode");
3310 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003311 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003312 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3313 case lltok::kw_br: return ParseBr(Inst, PFS);
3314 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003315 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003316 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003317 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003318 // Binary Operators.
3319 case lltok::kw_add:
3320 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003321 case lltok::kw_mul:
3322 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003323 bool NUW = EatIfPresent(lltok::kw_nuw);
3324 bool NSW = EatIfPresent(lltok::kw_nsw);
3325 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003326
Chris Lattnera676c0f2011-02-07 16:40:21 +00003327 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003328
Chris Lattnera676c0f2011-02-07 16:40:21 +00003329 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3330 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3331 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003332 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003333 case lltok::kw_fadd:
3334 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003335 case lltok::kw_fmul:
3336 case lltok::kw_fdiv:
3337 case lltok::kw_frem: {
3338 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3339 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3340 if (Res != 0)
3341 return Res;
3342 if (FMF.any())
3343 Inst->setFastMathFlags(FMF);
3344 return 0;
3345 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003346
Chris Lattner35315d02011-02-06 21:44:57 +00003347 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003348 case lltok::kw_udiv:
3349 case lltok::kw_lshr:
3350 case lltok::kw_ashr: {
3351 bool Exact = EatIfPresent(lltok::kw_exact);
3352
3353 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3354 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3355 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003356 }
3357
Chris Lattnerac161bf2009-01-02 07:01:27 +00003358 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003359 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003360 case lltok::kw_and:
3361 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003362 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003363 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003364 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003365 // Casts.
3366 case lltok::kw_trunc:
3367 case lltok::kw_zext:
3368 case lltok::kw_sext:
3369 case lltok::kw_fptrunc:
3370 case lltok::kw_fpext:
3371 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003372 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003373 case lltok::kw_uitofp:
3374 case lltok::kw_sitofp:
3375 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003376 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003377 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003378 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003379 // Other.
3380 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003381 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003382 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3383 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3384 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3385 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003386 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003387 // Call.
3388 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3389 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3390 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003391 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003392 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003393 case lltok::kw_load: return ParseLoad(Inst, PFS);
3394 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003395 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3396 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003397 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003398 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3399 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3400 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3401 }
3402}
3403
3404/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3405bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003406 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003407 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003408 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003409 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3410 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3411 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3412 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3413 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3414 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3415 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3416 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3417 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3418 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3419 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3420 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3421 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3422 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3423 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3424 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3425 }
3426 } else {
3427 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003428 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003429 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3430 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3431 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3432 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3433 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3434 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3435 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3436 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3437 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3438 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3439 }
3440 }
3441 Lex.Lex();
3442 return false;
3443}
3444
3445//===----------------------------------------------------------------------===//
3446// Terminator Instructions.
3447//===----------------------------------------------------------------------===//
3448
3449/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003450/// ::= 'ret' void (',' !dbg, !1)*
3451/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003452bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003453 PerFunctionState &PFS) {
3454 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003455 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003456 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003457
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003458 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003459
Chris Lattnerfdd87902009-10-05 05:54:46 +00003460 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003461 if (!ResType->isVoidTy())
3462 return Error(TypeLoc, "value doesn't match function result type '" +
3463 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003464
Owen Anderson55f1c092009-08-13 21:58:54 +00003465 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003466 return false;
3467 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003468
Chris Lattnerac161bf2009-01-02 07:01:27 +00003469 Value *RV;
3470 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003471
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003472 if (ResType != RV->getType())
3473 return Error(TypeLoc, "value doesn't match function result type '" +
3474 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003475
Owen Anderson55f1c092009-08-13 21:58:54 +00003476 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003477 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003478}
3479
3480
3481/// ParseBr
3482/// ::= 'br' TypeAndValue
3483/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3484bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3485 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003486 Value *Op0;
3487 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003488 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003489
Chris Lattnerac161bf2009-01-02 07:01:27 +00003490 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3491 Inst = BranchInst::Create(BB);
3492 return false;
3493 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003494
Owen Anderson55f1c092009-08-13 21:58:54 +00003495 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003496 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003497
Chris Lattnerac161bf2009-01-02 07:01:27 +00003498 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003499 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003500 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003501 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003502 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003503
Chris Lattner3ed871f2009-10-27 19:13:16 +00003504 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003505 return false;
3506}
3507
3508/// ParseSwitch
3509/// Instruction
3510/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3511/// JumpTable
3512/// ::= (TypeAndValue ',' TypeAndValue)*
3513bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3514 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003515 Value *Cond;
3516 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003517 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3518 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003519 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003520 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3521 return true;
3522
Duncan Sands19d0b472010-02-16 11:11:14 +00003523 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003524 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003525
Chris Lattnerac161bf2009-01-02 07:01:27 +00003526 // Parse the jump table pairs.
3527 SmallPtrSet<Value*, 32> SeenCases;
3528 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3529 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003530 Value *Constant;
3531 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003532
Chris Lattnerac161bf2009-01-02 07:01:27 +00003533 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3534 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003535 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003536 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003537
Chris Lattnerac161bf2009-01-02 07:01:27 +00003538 if (!SeenCases.insert(Constant))
3539 return Error(CondLoc, "duplicate case value in switch");
3540 if (!isa<ConstantInt>(Constant))
3541 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003542
Chris Lattner3ed871f2009-10-27 19:13:16 +00003543 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003544 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003545
Chris Lattnerac161bf2009-01-02 07:01:27 +00003546 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003547
Chris Lattner3ed871f2009-10-27 19:13:16 +00003548 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003549 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3550 SI->addCase(Table[i].first, Table[i].second);
3551 Inst = SI;
3552 return false;
3553}
3554
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003555/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003556/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003557/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3558bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003559 LocTy AddrLoc;
3560 Value *Address;
3561 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003562 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3563 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003564 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003565
Duncan Sands19d0b472010-02-16 11:11:14 +00003566 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003567 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003568
Chris Lattner3ed871f2009-10-27 19:13:16 +00003569 // Parse the destination list.
3570 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003571
Chris Lattner3ed871f2009-10-27 19:13:16 +00003572 if (Lex.getKind() != lltok::rsquare) {
3573 BasicBlock *DestBB;
3574 if (ParseTypeAndBasicBlock(DestBB, PFS))
3575 return true;
3576 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003577
Chris Lattner3ed871f2009-10-27 19:13:16 +00003578 while (EatIfPresent(lltok::comma)) {
3579 if (ParseTypeAndBasicBlock(DestBB, PFS))
3580 return true;
3581 DestList.push_back(DestBB);
3582 }
3583 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003584
Chris Lattner3ed871f2009-10-27 19:13:16 +00003585 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3586 return true;
3587
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003588 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003589 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3590 IBI->addDestination(DestList[i]);
3591 Inst = IBI;
3592 return false;
3593}
3594
3595
Chris Lattnerac161bf2009-01-02 07:01:27 +00003596/// ParseInvoke
3597/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3598/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3599bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3600 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003601 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003602 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003603 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003604 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003605 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003606 LocTy RetTypeLoc;
3607 ValID CalleeID;
3608 SmallVector<ParamInfo, 16> ArgList;
3609
Chris Lattner3ed871f2009-10-27 19:13:16 +00003610 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003611 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003612 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003613 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003614 ParseValID(CalleeID) ||
3615 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003616 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3617 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003618 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003619 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003620 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003621 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003622 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003623
Chris Lattnerac161bf2009-01-02 07:01:27 +00003624 // If RetType is a non-function pointer type, then this is the short syntax
3625 // for the call, which means that RetType is just the return type. Infer the
3626 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003627 PointerType *PFTy = nullptr;
3628 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003629 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3630 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3631 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003632 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003633 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3634 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003635
Chris Lattnerac161bf2009-01-02 07:01:27 +00003636 if (!FunctionType::isValidReturnType(RetType))
3637 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003638
Owen Anderson4056ca92009-07-29 22:17:13 +00003639 Ty = FunctionType::get(RetType, ParamTypes, false);
3640 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003641 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003642
Chris Lattnerac161bf2009-01-02 07:01:27 +00003643 // Look up the callee.
3644 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003645 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003646
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003647 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003648 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003649 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003650 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3651 AttributeSet::ReturnIndex,
3652 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003653
Chris Lattnerac161bf2009-01-02 07:01:27 +00003654 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003655
Chris Lattnerac161bf2009-01-02 07:01:27 +00003656 // Loop through FunctionType's arguments and ensure they are specified
3657 // correctly. Also, gather any parameter attributes.
3658 FunctionType::param_iterator I = Ty->param_begin();
3659 FunctionType::param_iterator E = Ty->param_end();
3660 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003661 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003662 if (I != E) {
3663 ExpectedTy = *I++;
3664 } else if (!Ty->isVarArg()) {
3665 return Error(ArgList[i].Loc, "too many arguments specified");
3666 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003667
Chris Lattnerac161bf2009-01-02 07:01:27 +00003668 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3669 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003670 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003671 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003672 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3673 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003674 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3675 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003676 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003677
Chris Lattnerac161bf2009-01-02 07:01:27 +00003678 if (I != E)
3679 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003680
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003681 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003682 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3683 AttributeSet::FunctionIndex,
3684 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003685
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003686 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003687 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003688
Jay Foad5bd375a2011-07-15 08:37:34 +00003689 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003690 II->setCallingConv(CC);
3691 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003692 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003693 Inst = II;
3694 return false;
3695}
3696
Bill Wendlingf891bf82011-07-31 06:30:59 +00003697/// ParseResume
3698/// ::= 'resume' TypeAndValue
3699bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3700 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003701 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3702 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003703
Bill Wendlingf891bf82011-07-31 06:30:59 +00003704 ResumeInst *RI = ResumeInst::Create(Exn);
3705 Inst = RI;
3706 return false;
3707}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003708
3709//===----------------------------------------------------------------------===//
3710// Binary Operators.
3711//===----------------------------------------------------------------------===//
3712
3713/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003714/// ::= ArithmeticOps TypeAndValue ',' Value
3715///
3716/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3717/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003718bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003719 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003720 LocTy Loc; Value *LHS, *RHS;
3721 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3722 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3723 ParseValue(LHS->getType(), RHS, PFS))
3724 return true;
3725
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003726 bool Valid;
3727 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003728 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003729 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003730 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3731 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003732 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003733 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3734 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003735 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003736
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003737 if (!Valid)
3738 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003739
Chris Lattnerac161bf2009-01-02 07:01:27 +00003740 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3741 return false;
3742}
3743
3744/// ParseLogical
3745/// ::= ArithmeticOps TypeAndValue ',' Value {
3746bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3747 unsigned Opc) {
3748 LocTy Loc; Value *LHS, *RHS;
3749 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3750 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3751 ParseValue(LHS->getType(), RHS, PFS))
3752 return true;
3753
Duncan Sands9dff9be2010-02-15 16:12:20 +00003754 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003755 return Error(Loc,"instruction requires integer or integer vector operands");
3756
3757 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3758 return false;
3759}
3760
3761
3762/// ParseCompare
3763/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3764/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003765bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3766 unsigned Opc) {
3767 // Parse the integer/fp comparison predicate.
3768 LocTy Loc;
3769 unsigned Pred;
3770 Value *LHS, *RHS;
3771 if (ParseCmpPredicate(Pred, Opc) ||
3772 ParseTypeAndValue(LHS, Loc, PFS) ||
3773 ParseToken(lltok::comma, "expected ',' after compare value") ||
3774 ParseValue(LHS->getType(), RHS, PFS))
3775 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003776
Chris Lattnerac161bf2009-01-02 07:01:27 +00003777 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003778 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003779 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003780 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003781 } else {
3782 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003783 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003784 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003785 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003786 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003787 }
3788 return false;
3789}
3790
3791//===----------------------------------------------------------------------===//
3792// Other Instructions.
3793//===----------------------------------------------------------------------===//
3794
3795
3796/// ParseCast
3797/// ::= CastOpc TypeAndValue 'to' Type
3798bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3799 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003800 LocTy Loc;
3801 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003802 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003803 if (ParseTypeAndValue(Op, Loc, PFS) ||
3804 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3805 ParseType(DestTy))
3806 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003807
Chris Lattner89d856e2009-03-01 00:53:13 +00003808 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3809 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003810 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003811 getTypeString(Op->getType()) + "' to '" +
3812 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003813 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003814 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3815 return false;
3816}
3817
3818/// ParseSelect
3819/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3820bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3821 LocTy Loc;
3822 Value *Op0, *Op1, *Op2;
3823 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3824 ParseToken(lltok::comma, "expected ',' after select condition") ||
3825 ParseTypeAndValue(Op1, PFS) ||
3826 ParseToken(lltok::comma, "expected ',' after select value") ||
3827 ParseTypeAndValue(Op2, PFS))
3828 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003829
Chris Lattnerac161bf2009-01-02 07:01:27 +00003830 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3831 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003832
Chris Lattnerac161bf2009-01-02 07:01:27 +00003833 Inst = SelectInst::Create(Op0, Op1, Op2);
3834 return false;
3835}
3836
Chris Lattnerb55ab542009-01-05 08:18:44 +00003837/// ParseVA_Arg
3838/// ::= 'va_arg' TypeAndValue ',' Type
3839bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003840 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003841 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003842 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003843 if (ParseTypeAndValue(Op, PFS) ||
3844 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003845 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003846 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003847
Chris Lattnerb55ab542009-01-05 08:18:44 +00003848 if (!EltTy->isFirstClassType())
3849 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003850
3851 Inst = new VAArgInst(Op, EltTy);
3852 return false;
3853}
3854
3855/// ParseExtractElement
3856/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3857bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3858 LocTy Loc;
3859 Value *Op0, *Op1;
3860 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3861 ParseToken(lltok::comma, "expected ',' after extract value") ||
3862 ParseTypeAndValue(Op1, PFS))
3863 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003864
Chris Lattnerac161bf2009-01-02 07:01:27 +00003865 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3866 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003867
Eric Christopherc9742252009-07-25 02:28:41 +00003868 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003869 return false;
3870}
3871
3872/// ParseInsertElement
3873/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3874bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3875 LocTy Loc;
3876 Value *Op0, *Op1, *Op2;
3877 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3878 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3879 ParseTypeAndValue(Op1, PFS) ||
3880 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3881 ParseTypeAndValue(Op2, PFS))
3882 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003883
Chris Lattnerac161bf2009-01-02 07:01:27 +00003884 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003885 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003886
Chris Lattnerac161bf2009-01-02 07:01:27 +00003887 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3888 return false;
3889}
3890
3891/// ParseShuffleVector
3892/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3893bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3894 LocTy Loc;
3895 Value *Op0, *Op1, *Op2;
3896 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3897 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3898 ParseTypeAndValue(Op1, PFS) ||
3899 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3900 ParseTypeAndValue(Op2, PFS))
3901 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003902
Chris Lattnerac161bf2009-01-02 07:01:27 +00003903 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003904 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003905
Chris Lattnerac161bf2009-01-02 07:01:27 +00003906 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3907 return false;
3908}
3909
3910/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003911/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003912int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003913 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003914 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003915
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003916 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003917 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3918 ParseValue(Ty, Op0, PFS) ||
3919 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003920 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003921 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3922 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003923
Chris Lattnerf4f03422009-12-30 05:27:33 +00003924 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003925 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3926 while (1) {
3927 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003928
Chris Lattner3822f632009-01-02 08:05:26 +00003929 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003930 break;
3931
Chris Lattnerf4f03422009-12-30 05:27:33 +00003932 if (Lex.getKind() == lltok::MetadataVar) {
3933 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003934 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003935 }
Devang Patel8f842d32009-10-16 18:45:49 +00003936
Chris Lattner3822f632009-01-02 08:05:26 +00003937 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003938 ParseValue(Ty, Op0, PFS) ||
3939 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003940 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003941 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3942 return true;
3943 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003944
Chris Lattnerac161bf2009-01-02 07:01:27 +00003945 if (!Ty->isFirstClassType())
3946 return Error(TypeLoc, "phi node must have first class type");
3947
Jay Foad52131342011-03-30 11:28:46 +00003948 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003949 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3950 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3951 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003952 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003953}
3954
Bill Wendlingfae14752011-08-12 20:24:12 +00003955/// ParseLandingPad
3956/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3957/// Clause
3958/// ::= 'catch' TypeAndValue
3959/// ::= 'filter'
3960/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3961bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003962 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003963 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003964
3965 if (ParseType(Ty, TyLoc) ||
3966 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3967 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3968 return true;
3969
3970 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3971 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3972
3973 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3974 LandingPadInst::ClauseType CT;
3975 if (EatIfPresent(lltok::kw_catch))
3976 CT = LandingPadInst::Catch;
3977 else if (EatIfPresent(lltok::kw_filter))
3978 CT = LandingPadInst::Filter;
3979 else
3980 return TokError("expected 'catch' or 'filter' clause type");
3981
3982 Value *V; LocTy VLoc;
3983 if (ParseTypeAndValue(V, VLoc, PFS)) {
3984 delete LP;
3985 return true;
3986 }
3987
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00003988 // A 'catch' type expects a non-array constant. A filter clause expects an
3989 // array constant.
3990 if (CT == LandingPadInst::Catch) {
3991 if (isa<ArrayType>(V->getType()))
3992 Error(VLoc, "'catch' clause has an invalid type");
3993 } else {
3994 if (!isa<ArrayType>(V->getType()))
3995 Error(VLoc, "'filter' clause has an invalid type");
3996 }
3997
Bill Wendlingfae14752011-08-12 20:24:12 +00003998 LP->addClause(V);
3999 }
4000
4001 Inst = LP;
4002 return false;
4003}
4004
Chris Lattnerac161bf2009-01-02 07:01:27 +00004005/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004006/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4007/// ParameterList OptionalAttrs
4008/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4009/// ParameterList OptionalAttrs
4010/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004011/// ParameterList OptionalAttrs
4012bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004013 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004014 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004015 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004016 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004017 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004018 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004019 LocTy RetTypeLoc;
4020 ValID CalleeID;
4021 SmallVector<ParamInfo, 16> ArgList;
4022 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004023
Reid Kleckner5772b772014-04-24 20:14:34 +00004024 if ((TCK != CallInst::TCK_None &&
4025 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004026 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004027 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004028 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004029 ParseValID(CalleeID) ||
4030 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004031 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004032 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004033 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004034
Chris Lattnerac161bf2009-01-02 07:01:27 +00004035 // If RetType is a non-function pointer type, then this is the short syntax
4036 // for the call, which means that RetType is just the return type. Infer the
4037 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004038 PointerType *PFTy = nullptr;
4039 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004040 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4041 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4042 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004043 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004044 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4045 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004046
Chris Lattnerac161bf2009-01-02 07:01:27 +00004047 if (!FunctionType::isValidReturnType(RetType))
4048 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004049
Owen Anderson4056ca92009-07-29 22:17:13 +00004050 Ty = FunctionType::get(RetType, ParamTypes, false);
4051 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004052 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004053
Chris Lattnerac161bf2009-01-02 07:01:27 +00004054 // Look up the callee.
4055 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004056 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004057
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004058 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004059 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004060 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004061 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4062 AttributeSet::ReturnIndex,
4063 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004064
Chris Lattnerac161bf2009-01-02 07:01:27 +00004065 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004066
Chris Lattnerac161bf2009-01-02 07:01:27 +00004067 // Loop through FunctionType's arguments and ensure they are specified
4068 // correctly. Also, gather any parameter attributes.
4069 FunctionType::param_iterator I = Ty->param_begin();
4070 FunctionType::param_iterator E = Ty->param_end();
4071 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004072 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004073 if (I != E) {
4074 ExpectedTy = *I++;
4075 } else if (!Ty->isVarArg()) {
4076 return Error(ArgList[i].Loc, "too many arguments specified");
4077 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004078
Chris Lattnerac161bf2009-01-02 07:01:27 +00004079 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4080 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004081 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004082 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004083 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4084 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004085 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4086 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004087 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004088
Chris Lattnerac161bf2009-01-02 07:01:27 +00004089 if (I != E)
4090 return Error(CallLoc, "not enough parameters specified for call");
4091
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004092 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004093 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4094 AttributeSet::FunctionIndex,
4095 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004096
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004097 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004098 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004099
Jay Foad5bd375a2011-07-15 08:37:34 +00004100 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004101 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004102 CI->setCallingConv(CC);
4103 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004104 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004105 Inst = CI;
4106 return false;
4107}
4108
4109//===----------------------------------------------------------------------===//
4110// Memory Instructions.
4111//===----------------------------------------------------------------------===//
4112
4113/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004114/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004115int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004116 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004117 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004118 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004119 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004120
4121 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4122
Chris Lattner3822f632009-01-02 08:05:26 +00004123 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004124
Chris Lattnerb2f39502009-12-30 05:44:30 +00004125 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004126 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004127 if (Lex.getKind() == lltok::kw_align) {
4128 if (ParseOptionalAlignment(Alignment)) return true;
4129 } else if (Lex.getKind() == lltok::MetadataVar) {
4130 AteExtraComma = true;
4131 } else {
4132 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4133 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4134 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004135 }
4136 }
4137
Dan Gohman2140a742010-05-28 01:14:11 +00004138 if (Size && !Size->getType()->isIntegerTy())
4139 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004140
Reid Kleckner436c42e2014-01-17 23:58:17 +00004141 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4142 AI->setUsedWithInAlloca(IsInAlloca);
4143 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004144 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004145}
4146
4147/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004148/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004149/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004150/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004151int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004152 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004153 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004154 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004155 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004156 AtomicOrdering Ordering = NotAtomic;
4157 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004158
4159 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004160 isAtomic = true;
4161 Lex.Lex();
4162 }
4163
Chris Lattnerbc639292011-11-27 06:56:53 +00004164 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004165 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004166 isVolatile = true;
4167 Lex.Lex();
4168 }
4169
Chris Lattnerb2f39502009-12-30 05:44:30 +00004170 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004171 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004172 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4173 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004174
Duncan Sands19d0b472010-02-16 11:11:14 +00004175 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004176 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4177 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004178 if (isAtomic && !Alignment)
4179 return Error(Loc, "atomic load must have explicit non-zero alignment");
4180 if (Ordering == Release || Ordering == AcquireRelease)
4181 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004182
Eli Friedman59b66882011-08-09 23:02:53 +00004183 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004184 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004185}
4186
4187/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004188
4189/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4190/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004191/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004192int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004193 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004194 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004195 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004196 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004197 AtomicOrdering Ordering = NotAtomic;
4198 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004199
4200 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004201 isAtomic = true;
4202 Lex.Lex();
4203 }
4204
Chris Lattnerbc639292011-11-27 06:56:53 +00004205 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004206 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004207 isVolatile = true;
4208 Lex.Lex();
4209 }
4210
Chris Lattnerac161bf2009-01-02 07:01:27 +00004211 if (ParseTypeAndValue(Val, Loc, PFS) ||
4212 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004213 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004214 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004215 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004216 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004217
Duncan Sands19d0b472010-02-16 11:11:14 +00004218 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004219 return Error(PtrLoc, "store operand must be a pointer");
4220 if (!Val->getType()->isFirstClassType())
4221 return Error(Loc, "store operand must be a first class value");
4222 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4223 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004224 if (isAtomic && !Alignment)
4225 return Error(Loc, "atomic store must have explicit non-zero alignment");
4226 if (Ordering == Acquire || Ordering == AcquireRelease)
4227 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004228
Eli Friedman59b66882011-08-09 23:02:53 +00004229 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004230 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004231}
4232
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004233/// ParseCmpXchg
Eli Friedman02e737b2011-08-12 22:50:01 +00004234/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
Tim Northovere94a5182014-03-11 10:48:52 +00004235/// 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004236int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004237 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4238 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004239 AtomicOrdering SuccessOrdering = NotAtomic;
4240 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004241 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004242 bool isVolatile = false;
4243
4244 if (EatIfPresent(lltok::kw_volatile))
4245 isVolatile = true;
4246
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004247 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4248 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4249 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4250 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4251 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004252 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4253 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004254 return true;
4255
Tim Northovere94a5182014-03-11 10:48:52 +00004256 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004257 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004258 if (SuccessOrdering < FailureOrdering)
4259 return TokError("cmpxchg must be at least as ordered on success as failure");
4260 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4261 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004262 if (!Ptr->getType()->isPointerTy())
4263 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4264 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4265 return Error(CmpLoc, "compare value and pointer type do not match");
4266 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4267 return Error(NewLoc, "new value and pointer type do not match");
4268 if (!New->getType()->isIntegerTy())
4269 return Error(NewLoc, "cmpxchg operand must be an integer");
4270 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4271 if (Size < 8 || (Size & (Size - 1)))
4272 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4273 " integer");
4274
Tim Northovere94a5182014-03-11 10:48:52 +00004275 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
4276 FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004277 CXI->setVolatile(isVolatile);
4278 Inst = CXI;
4279 return AteExtraComma ? InstExtraComma : InstNormal;
4280}
4281
4282/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004283/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4284/// 'singlethread'? AtomicOrdering
4285int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004286 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4287 bool AteExtraComma = false;
4288 AtomicOrdering Ordering = NotAtomic;
4289 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004290 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004291 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004292
4293 if (EatIfPresent(lltok::kw_volatile))
4294 isVolatile = true;
4295
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004296 switch (Lex.getKind()) {
4297 default: return TokError("expected binary operation in atomicrmw");
4298 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4299 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4300 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4301 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4302 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4303 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4304 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4305 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4306 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4307 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4308 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4309 }
4310 Lex.Lex(); // Eat the operation.
4311
4312 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4313 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4314 ParseTypeAndValue(Val, ValLoc, PFS) ||
4315 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4316 return true;
4317
4318 if (Ordering == Unordered)
4319 return TokError("atomicrmw cannot be unordered");
4320 if (!Ptr->getType()->isPointerTy())
4321 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4322 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4323 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4324 if (!Val->getType()->isIntegerTy())
4325 return Error(ValLoc, "atomicrmw operand must be an integer");
4326 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4327 if (Size < 8 || (Size & (Size - 1)))
4328 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4329 " integer");
4330
4331 AtomicRMWInst *RMWI =
4332 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4333 RMWI->setVolatile(isVolatile);
4334 Inst = RMWI;
4335 return AteExtraComma ? InstExtraComma : InstNormal;
4336}
4337
Eli Friedmanfee02c62011-07-25 23:16:38 +00004338/// ParseFence
4339/// ::= 'fence' 'singlethread'? AtomicOrdering
4340int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4341 AtomicOrdering Ordering = NotAtomic;
4342 SynchronizationScope Scope = CrossThread;
4343 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4344 return true;
4345
4346 if (Ordering == Unordered)
4347 return TokError("fence cannot be unordered");
4348 if (Ordering == Monotonic)
4349 return TokError("fence cannot be monotonic");
4350
4351 Inst = new FenceInst(Context, Ordering, Scope);
4352 return InstNormal;
4353}
4354
Chris Lattnerac161bf2009-01-02 07:01:27 +00004355/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004356/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004357int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004358 Value *Ptr = nullptr;
4359 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004360 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004361
Dan Gohman16cbbe42009-07-29 15:58:36 +00004362 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004363
Chris Lattner3822f632009-01-02 08:05:26 +00004364 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004365
Eli Benderskyd9806682013-04-22 17:03:42 +00004366 Type *BaseType = Ptr->getType();
4367 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4368 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004369 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004370
Chris Lattnerac161bf2009-01-02 07:01:27 +00004371 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004372 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004373 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004374 if (Lex.getKind() == lltok::MetadataVar) {
4375 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004376 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004377 }
Chris Lattner3822f632009-01-02 08:05:26 +00004378 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004379 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004380 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004381 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4382 return Error(EltLoc, "getelementptr index type missmatch");
4383 if (Val->getType()->isVectorTy()) {
4384 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4385 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4386 if (ValNumEl != PtrNumEl)
4387 return Error(EltLoc,
4388 "getelementptr vector index has a wrong number of elements");
4389 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004390 Indices.push_back(Val);
4391 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004392
Eli Benderskyd9806682013-04-22 17:03:42 +00004393 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4394 return Error(Loc, "base element of getelementptr must be sized");
4395
4396 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004397 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004398 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004399 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004400 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004401 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004402}
4403
4404/// ParseExtractValue
4405/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004406int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004407 Value *Val; LocTy Loc;
4408 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004409 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004410 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004411 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004412 return true;
4413
Chris Lattner392be582010-02-12 20:49:41 +00004414 if (!Val->getType()->isAggregateType())
4415 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004416
Jay Foad57aa6362011-07-13 10:26:04 +00004417 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004418 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004419 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004420 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004421}
4422
4423/// ParseInsertValue
4424/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004425int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004426 Value *Val0, *Val1; LocTy Loc0, Loc1;
4427 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004428 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004429 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4430 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4431 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004432 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004433 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004434
Chris Lattner392be582010-02-12 20:49:41 +00004435 if (!Val0->getType()->isAggregateType())
4436 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004437
Jay Foad57aa6362011-07-13 10:26:04 +00004438 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004439 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004440 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004441 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004442}
Nick Lewycky49f89192009-04-04 07:22:01 +00004443
4444//===----------------------------------------------------------------------===//
4445// Embedded metadata.
4446//===----------------------------------------------------------------------===//
4447
4448/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004449/// ::= Element (',' Element)*
4450/// Element
4451/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004452bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004453 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004454 // Check for an empty list.
4455 if (Lex.getKind() == lltok::rbrace)
4456 return false;
4457
Nick Lewycky49f89192009-04-04 07:22:01 +00004458 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004459 // Null is a special case since it is typeless.
4460 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004461 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004462 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004463 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004464
Craig Topper2617dcc2014-04-15 06:32:26 +00004465 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004466 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004467 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004468 } while (EatIfPresent(lltok::comma));
4469
4470 return false;
4471}