blob: 56422393e4618180cfbc0474798be6dcacb41894 [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
Chris Lattnerac161bf2009-01-02 07:01:27 +0000625/// ParseAlias:
Nico Rieck7157bb72014-01-14 15:22:47 +0000626/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
627/// OptionalLinkage Aliasee
Chris Lattnerac161bf2009-01-02 07:01:27 +0000628/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000629/// ::= TypeAndValue
630/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
Dan Gohman1639c392009-07-27 21:53:46 +0000631/// ::= 'getelementptr' 'inbounds'? '(' ... ')'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000632///
Nico Rieck7157bb72014-01-14 15:22:47 +0000633/// Everything through DLL storage class has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000634///
635bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Nico Rieck7157bb72014-01-14 15:22:47 +0000636 unsigned Visibility, unsigned DLLStorageClass) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000637 assert(Lex.getKind() == lltok::kw_alias);
638 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000639 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000640 unsigned L;
641 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000642 return true;
643
Rafael Espindola78527052013-10-06 15:10:43 +0000644 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
645
Rafael Espindolacaa43562013-10-09 16:07:32 +0000646 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000647 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000648
Chris Lattnerac161bf2009-01-02 07:01:27 +0000649 Constant *Aliasee;
650 LocTy AliaseeLoc = Lex.getLoc();
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000651 if (Lex.getKind() != lltok::kw_bitcast &&
652 Lex.getKind() != lltok::kw_getelementptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000653 if (ParseGlobalTypeAndValue(Aliasee)) return true;
654 } else {
655 // The bitcast dest type is not present, it is implied by the dest type.
656 ValID ID;
657 if (ParseValID(ID)) return true;
658 if (ID.Kind != ValID::t_Constant)
659 return Error(AliaseeLoc, "invalid aliasee");
660 Aliasee = ID.ConstantVal;
661 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000662
Duncan Sands19d0b472010-02-16 11:11:14 +0000663 if (!Aliasee->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +0000664 return Error(AliaseeLoc, "alias must have pointer type");
665
666 // Okay, create the alias but do not insert it into the module yet.
667 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
668 (GlobalValue::LinkageTypes)Linkage, Name,
669 Aliasee);
670 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000671 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000672
Chris Lattnerac161bf2009-01-02 07:01:27 +0000673 // See if this value already exists in the symbol table. If so, it is either
674 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000675 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000676 // See if this was a redefinition. If so, there is no entry in
677 // ForwardRefVals.
678 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
679 I = ForwardRefVals.find(Name);
680 if (I == ForwardRefVals.end())
681 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
682
683 // Otherwise, this was a definition of forward ref. Verify that types
684 // agree.
685 if (Val->getType() != GA->getType())
686 return Error(NameLoc,
687 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000688
Chris Lattnerac161bf2009-01-02 07:01:27 +0000689 // If they agree, just RAUW the old value with the alias and remove the
690 // forward ref info.
691 Val->replaceAllUsesWith(GA);
692 Val->eraseFromParent();
693 ForwardRefVals.erase(I);
694 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000695
Chris Lattnerac161bf2009-01-02 07:01:27 +0000696 // Insert into the module, we know its name won't collide now.
697 M->getAliasList().push_back(GA);
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000698 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000699
Chris Lattnerac161bf2009-01-02 07:01:27 +0000700 return false;
701}
702
703/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000704/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
705/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000706/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000707/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
708/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000709/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000710///
David Majnemerc4ab61c2014-03-09 06:41:58 +0000711/// Everything up to and including OptionalDLLStorageClass has been parsed
712/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000713///
714bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
715 unsigned Linkage, bool HasLinkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000716 unsigned Visibility, unsigned DLLStorageClass) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000717 unsigned AddrSpace;
Shuxin Yang2e1890e2013-10-27 03:08:44 +0000718 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000719 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola026d1522011-01-13 01:30:30 +0000720 LocTy UnnamedAddrLoc;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000721 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000722 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000723
Craig Topper2617dcc2014-04-15 06:32:26 +0000724 Type *Ty = nullptr;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000725 if (ParseOptionalThreadLocal(TLM) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000726 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindola026d1522011-01-13 01:30:30 +0000727 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
728 &UnnamedAddrLoc) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000729 ParseOptionalToken(lltok::kw_externally_initialized,
730 IsExternallyInitialized,
731 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000732 ParseGlobalType(IsConstant) ||
733 ParseType(Ty, TyLoc))
734 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000735
Chris Lattnerac161bf2009-01-02 07:01:27 +0000736 // If the linkage is specified and is external, then no initializer is
737 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000738 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000739 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000740 Linkage != GlobalValue::ExternalLinkage)) {
741 if (ParseGlobalValue(Ty, Init))
742 return true;
743 }
744
Duncan Sands19d0b472010-02-16 11:11:14 +0000745 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000746 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000747
Craig Topper2617dcc2014-04-15 06:32:26 +0000748 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000749
750 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000751 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000752 if (GlobalValue *GVal = M->getNamedValue(Name)) {
753 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
754 return Error(NameLoc, "redefinition of global '@" + Name + "'");
755 GV = cast<GlobalVariable>(GVal);
756 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000757 } else {
758 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
759 I = ForwardRefValIDs.find(NumberedVals.size());
760 if (I != ForwardRefValIDs.end()) {
761 GV = cast<GlobalVariable>(I->second.first);
762 ForwardRefValIDs.erase(I);
763 }
764 }
765
Craig Topper2617dcc2014-04-15 06:32:26 +0000766 if (!GV) {
767 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
768 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000769 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000770 } else {
771 if (GV->getType()->getElementType() != Ty)
772 return Error(TyLoc,
773 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000774
Chris Lattnerac161bf2009-01-02 07:01:27 +0000775 // Move the forward-reference to the correct spot in the module.
776 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
777 }
778
779 if (Name.empty())
780 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000781
Chris Lattnerac161bf2009-01-02 07:01:27 +0000782 // Set the parsed properties on the global.
783 if (Init)
784 GV->setInitializer(Init);
785 GV->setConstant(IsConstant);
786 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
787 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000788 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000789 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000790 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000791 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000792
Chris Lattnerac161bf2009-01-02 07:01:27 +0000793 // Parse attributes on the global.
794 while (Lex.getKind() == lltok::comma) {
795 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000796
Chris Lattnerac161bf2009-01-02 07:01:27 +0000797 if (Lex.getKind() == lltok::kw_section) {
798 Lex.Lex();
799 GV->setSection(Lex.getStrVal());
800 if (ParseToken(lltok::StringConstant, "expected global section string"))
801 return true;
802 } else if (Lex.getKind() == lltok::kw_align) {
803 unsigned Alignment;
804 if (ParseOptionalAlignment(Alignment)) return true;
805 GV->setAlignment(Alignment);
806 } else {
807 TokError("unknown global variable property!");
808 }
809 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000810
Chris Lattnerac161bf2009-01-02 07:01:27 +0000811 return false;
812}
813
Bill Wendling63b88192013-02-06 06:52:58 +0000814/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000815/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000816bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000817 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000818 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000819 Lex.Lex();
820
821 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000822 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000823 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000824 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000825 Lex.Lex();
826
827 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000828 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000829 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000830 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000831 ParseToken(lltok::rbrace, "expected end of attribute group"))
832 return true;
833
Bill Wendlingb32b0412013-02-08 06:32:06 +0000834 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000835 return Error(AttrGrpLoc, "attribute group has no attributes");
836
837 return false;
838}
839
Bill Wendling8b0321d2013-02-08 00:52:31 +0000840/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000841/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000842bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
843 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000844 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000845 bool HaveError = false;
846
847 B.clear();
848
Bill Wendling63b88192013-02-06 06:52:58 +0000849 while (true) {
850 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000851 if (Token == lltok::kw_builtin)
852 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000853 switch (Token) {
854 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000855 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000856 return Error(Lex.getLoc(), "unterminated attribute group");
857 case lltok::rbrace:
858 // Finished.
859 return false;
860
Bill Wendlingb32b0412013-02-08 06:32:06 +0000861 case lltok::AttrGrpID: {
862 // Allow a function to reference an attribute group:
863 //
864 // define void @foo() #1 { ... }
865 if (inAttrGrp)
866 HaveError |=
867 Error(Lex.getLoc(),
868 "cannot have an attribute group reference in an attribute group");
869
870 unsigned AttrGrpNum = Lex.getUIntVal();
871 if (inAttrGrp) break;
872
873 // Save the reference to the attribute group. We'll fill it in later.
874 FwdRefAttrGrps.push_back(AttrGrpNum);
875 break;
876 }
Bill Wendling63b88192013-02-06 06:52:58 +0000877 // Target-dependent attributes:
878 case lltok::StringConstant: {
879 std::string Attr = Lex.getStrVal();
880 Lex.Lex();
881 std::string Val;
882 if (EatIfPresent(lltok::equal) &&
883 ParseStringConstant(Val))
884 return true;
885
886 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000887 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000888 }
889
890 // Target-independent attributes:
891 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000892 // As a hack, we allow function alignment to be initially parsed as an
893 // attribute on a function declaration/definition or added to an attribute
894 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000895 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000896 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000897 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000898 if (ParseToken(lltok::equal, "expected '=' here") ||
899 ParseUInt32(Alignment))
900 return true;
901 } else {
902 if (ParseOptionalAlignment(Alignment))
903 return true;
904 }
Bill Wendling63b88192013-02-06 06:52:58 +0000905 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000906 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000907 }
908 case lltok::kw_alignstack: {
909 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000910 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000911 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000912 if (ParseToken(lltok::equal, "expected '=' here") ||
913 ParseUInt32(Alignment))
914 return true;
915 } else {
916 if (ParseOptionalStackAlignment(Alignment))
917 return true;
918 }
Bill Wendling63b88192013-02-06 06:52:58 +0000919 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000920 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000921 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000922 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000923 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000924 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000925 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
926 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
927 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
928 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
929 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
930 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
931 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
932 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
933 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
934 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
935 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000936 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000937 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
938 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
939 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
940 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
941 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
942 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
943 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
944 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
945 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
946 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
947 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000948
949 // Error handling.
950 case lltok::kw_inreg:
951 case lltok::kw_signext:
952 case lltok::kw_zeroext:
953 HaveError |=
954 Error(Lex.getLoc(),
955 "invalid use of attribute on a function");
956 break;
957 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +0000958 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000959 case lltok::kw_nest:
960 case lltok::kw_noalias:
961 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +0000962 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000963 case lltok::kw_sret:
964 HaveError |=
965 Error(Lex.getLoc(),
966 "invalid use of parameter-only attribute on a function");
967 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000968 }
969
970 Lex.Lex();
971 }
972}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000973
974//===----------------------------------------------------------------------===//
975// GlobalValue Reference/Resolution Routines.
976//===----------------------------------------------------------------------===//
977
978/// GetGlobalVal - Get a value with the specified name or ID, creating a
979/// forward reference record if needed. This can return null if the value
980/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000981GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000982 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +0000983 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +0000984 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000985 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +0000986 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000987 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000988
Chris Lattnerac161bf2009-01-02 07:01:27 +0000989 // Look this name up in the normal function symbol table.
990 GlobalValue *Val =
991 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000992
Chris Lattnerac161bf2009-01-02 07:01:27 +0000993 // If this is a forward reference for the value, see if we already created a
994 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +0000995 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000996 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
997 I = ForwardRefVals.find(Name);
998 if (I != ForwardRefVals.end())
999 Val = I->second.first;
1000 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001001
Chris Lattnerac161bf2009-01-02 07:01:27 +00001002 // If we have the value in the symbol table or fwd-ref table, return it.
1003 if (Val) {
1004 if (Val->getType() == Ty) return Val;
1005 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001006 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001007 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001008 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001009
Chris Lattnerac161bf2009-01-02 07:01:27 +00001010 // Otherwise, create a new forward reference for this value and remember it.
1011 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001012 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001013 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001014 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001015 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001016 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1017 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001018 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001019
Chris Lattnerac161bf2009-01-02 07:01:27 +00001020 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1021 return FwdVal;
1022}
1023
Chris Lattner229907c2011-07-18 04:54:35 +00001024GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1025 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001026 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001027 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001028 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001029 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001030
Craig Topper2617dcc2014-04-15 06:32:26 +00001031 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001032
Chris Lattnerac161bf2009-01-02 07:01:27 +00001033 // If this is a forward reference for the value, see if we already created a
1034 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001035 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001036 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1037 I = ForwardRefValIDs.find(ID);
1038 if (I != ForwardRefValIDs.end())
1039 Val = I->second.first;
1040 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001041
Chris Lattnerac161bf2009-01-02 07:01:27 +00001042 // If we have the value in the symbol table or fwd-ref table, return it.
1043 if (Val) {
1044 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001045 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001046 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001047 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001048 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001049
Chris Lattnerac161bf2009-01-02 07:01:27 +00001050 // Otherwise, create a new forward reference for this value and remember it.
1051 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001052 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001053 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001054 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001055 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001056 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001057
Chris Lattnerac161bf2009-01-02 07:01:27 +00001058 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1059 return FwdVal;
1060}
1061
1062
1063//===----------------------------------------------------------------------===//
1064// Helper Routines.
1065//===----------------------------------------------------------------------===//
1066
1067/// ParseToken - If the current token has the specified kind, eat it and return
1068/// success. Otherwise, emit the specified error and return failure.
1069bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1070 if (Lex.getKind() != T)
1071 return TokError(ErrMsg);
1072 Lex.Lex();
1073 return false;
1074}
1075
Chris Lattner3822f632009-01-02 08:05:26 +00001076/// ParseStringConstant
1077/// ::= StringConstant
1078bool LLParser::ParseStringConstant(std::string &Result) {
1079 if (Lex.getKind() != lltok::StringConstant)
1080 return TokError("expected string constant");
1081 Result = Lex.getStrVal();
1082 Lex.Lex();
1083 return false;
1084}
1085
1086/// ParseUInt32
1087/// ::= uint32
1088bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001089 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1090 return TokError("expected integer");
1091 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1092 if (Val64 != unsigned(Val64))
1093 return TokError("expected 32-bit integer (too large)");
1094 Val = Val64;
1095 Lex.Lex();
1096 return false;
1097}
1098
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001099/// ParseTLSModel
1100/// := 'localdynamic'
1101/// := 'initialexec'
1102/// := 'localexec'
1103bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1104 switch (Lex.getKind()) {
1105 default:
1106 return TokError("expected localdynamic, initialexec or localexec");
1107 case lltok::kw_localdynamic:
1108 TLM = GlobalVariable::LocalDynamicTLSModel;
1109 break;
1110 case lltok::kw_initialexec:
1111 TLM = GlobalVariable::InitialExecTLSModel;
1112 break;
1113 case lltok::kw_localexec:
1114 TLM = GlobalVariable::LocalExecTLSModel;
1115 break;
1116 }
1117
1118 Lex.Lex();
1119 return false;
1120}
1121
1122/// ParseOptionalThreadLocal
1123/// := /*empty*/
1124/// := 'thread_local'
1125/// := 'thread_local' '(' tlsmodel ')'
1126bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1127 TLM = GlobalVariable::NotThreadLocal;
1128 if (!EatIfPresent(lltok::kw_thread_local))
1129 return false;
1130
1131 TLM = GlobalVariable::GeneralDynamicTLSModel;
1132 if (Lex.getKind() == lltok::lparen) {
1133 Lex.Lex();
1134 return ParseTLSModel(TLM) ||
1135 ParseToken(lltok::rparen, "expected ')' after thread local model");
1136 }
1137 return false;
1138}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001139
1140/// ParseOptionalAddrSpace
1141/// := /*empty*/
1142/// := 'addrspace' '(' uint32 ')'
1143bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1144 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001145 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001146 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001147 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001148 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001149 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001150}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001151
Bill Wendling34c2eb22012-12-04 23:40:58 +00001152/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1153bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1154 bool HaveError = false;
1155
1156 B.clear();
1157
1158 while (1) {
1159 lltok::Kind Token = Lex.getKind();
1160 switch (Token) {
1161 default: // End of attributes.
1162 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001163 case lltok::kw_align: {
1164 unsigned Alignment;
1165 if (ParseOptionalAlignment(Alignment))
1166 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001167 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001168 continue;
1169 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001170 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001171 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001172 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1173 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1174 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1175 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001176 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1177 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001178 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001179 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1180 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1181 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001182
Stephen Lin7577ed52013-04-20 13:16:13 +00001183 case lltok::kw_alignstack:
1184 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001185 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001186 case lltok::kw_inlinehint:
1187 case lltok::kw_minsize:
1188 case lltok::kw_naked:
1189 case lltok::kw_nobuiltin:
1190 case lltok::kw_noduplicate:
1191 case lltok::kw_noimplicitfloat:
1192 case lltok::kw_noinline:
1193 case lltok::kw_nonlazybind:
1194 case lltok::kw_noredzone:
1195 case lltok::kw_noreturn:
1196 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001197 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001198 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001199 case lltok::kw_returns_twice:
1200 case lltok::kw_sanitize_address:
1201 case lltok::kw_sanitize_memory:
1202 case lltok::kw_sanitize_thread:
1203 case lltok::kw_ssp:
1204 case lltok::kw_sspreq:
1205 case lltok::kw_sspstrong:
1206 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001207 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1208 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001209 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001210
Bill Wendling34c2eb22012-12-04 23:40:58 +00001211 Lex.Lex();
1212 }
1213}
1214
1215/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1216bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1217 bool HaveError = false;
1218
1219 B.clear();
1220
1221 while (1) {
1222 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001223 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001224 default: // End of attributes.
1225 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001226 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1227 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1228 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1229 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001230
Bill Wendling34c2eb22012-12-04 23:40:58 +00001231 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001232 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001233 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001234 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001235 case lltok::kw_nest:
1236 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001237 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001238 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001239 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001240 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001241
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001242 case lltok::kw_alignstack:
1243 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001244 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001245 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001246 case lltok::kw_inlinehint:
1247 case lltok::kw_minsize:
1248 case lltok::kw_naked:
1249 case lltok::kw_nobuiltin:
1250 case lltok::kw_noduplicate:
1251 case lltok::kw_noimplicitfloat:
1252 case lltok::kw_noinline:
1253 case lltok::kw_nonlazybind:
1254 case lltok::kw_noredzone:
1255 case lltok::kw_noreturn:
1256 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001257 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001258 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001259 case lltok::kw_returns_twice:
1260 case lltok::kw_sanitize_address:
1261 case lltok::kw_sanitize_memory:
1262 case lltok::kw_sanitize_thread:
1263 case lltok::kw_ssp:
1264 case lltok::kw_sspreq:
1265 case lltok::kw_sspstrong:
1266 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001267 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001268 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001269
1270 case lltok::kw_readnone:
1271 case lltok::kw_readonly:
1272 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001273 }
1274
Chris Lattnerac161bf2009-01-02 07:01:27 +00001275 Lex.Lex();
1276 }
1277}
1278
1279/// ParseOptionalLinkage
1280/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001281/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001282/// ::= 'internal'
1283/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001284/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001285/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001286/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001287/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001288/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001289/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001290/// ::= 'extern_weak'
1291/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001292///
1293/// Deprecated Values:
1294/// ::= 'linker_private'
1295/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001296bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1297 HasLinkage = false;
1298 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001299 default: Res=GlobalValue::ExternalLinkage; return false;
1300 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001301 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1302 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1303 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1304 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1305 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001306 case lltok::kw_available_externally:
1307 Res = GlobalValue::AvailableExternallyLinkage;
1308 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001309 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001310 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001311 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1312 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001313
1314 case lltok::kw_linker_private:
1315 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001316 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1317 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001318 Lex.Lex();
1319 // treat linker_private and linker_private_weak as PrivateLinkage
1320 Res = GlobalValue::PrivateLinkage;
1321 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001322 }
1323 Lex.Lex();
1324 HasLinkage = true;
1325 return false;
1326}
1327
1328/// ParseOptionalVisibility
1329/// ::= /*empty*/
1330/// ::= 'default'
1331/// ::= 'hidden'
1332/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001333///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001334bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1335 switch (Lex.getKind()) {
1336 default: Res = GlobalValue::DefaultVisibility; return false;
1337 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1338 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1339 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1340 }
1341 Lex.Lex();
1342 return false;
1343}
1344
Nico Rieck7157bb72014-01-14 15:22:47 +00001345/// ParseOptionalDLLStorageClass
1346/// ::= /*empty*/
1347/// ::= 'dllimport'
1348/// ::= 'dllexport'
1349///
1350bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1351 switch (Lex.getKind()) {
1352 default: Res = GlobalValue::DefaultStorageClass; return false;
1353 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1354 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1355 }
1356 Lex.Lex();
1357 return false;
1358}
1359
Chris Lattnerac161bf2009-01-02 07:01:27 +00001360/// ParseOptionalCallingConv
1361/// ::= /*empty*/
1362/// ::= 'ccc'
1363/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001364/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001365/// ::= 'coldcc'
1366/// ::= 'x86_stdcallcc'
1367/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001368/// ::= 'x86_thiscallcc'
Reid Kleckner1c843222014-01-31 17:41:22 +00001369/// ::= 'x86_cdeclmethodcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001370/// ::= 'arm_apcscc'
1371/// ::= 'arm_aapcscc'
1372/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001373/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001374/// ::= 'ptx_kernel'
1375/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001376/// ::= 'spir_func'
1377/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001378/// ::= 'x86_64_sysvcc'
1379/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001380/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001381/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001382/// ::= 'preserve_mostcc'
1383/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001384/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001385///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001386bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001387 switch (Lex.getKind()) {
1388 default: CC = CallingConv::C; return false;
1389 case lltok::kw_ccc: CC = CallingConv::C; break;
1390 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1391 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1392 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1393 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001394 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner1c843222014-01-31 17:41:22 +00001395 case lltok::kw_x86_cdeclmethodcc:CC = CallingConv::X86_CDeclMethod; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001396 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1397 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1398 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001399 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001400 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1401 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001402 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1403 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001404 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001405 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1406 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001407 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001408 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001409 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1410 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001411 case lltok::kw_cc: {
1412 unsigned ArbitraryCC;
1413 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001414 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001415 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001416 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1417 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001418 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001419 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001420
Chris Lattnerac161bf2009-01-02 07:01:27 +00001421 Lex.Lex();
1422 return false;
1423}
1424
Chris Lattner5c427632009-12-30 05:31:19 +00001425/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001426/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001427bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1428 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001429 do {
1430 if (Lex.getKind() != lltok::MetadataVar)
1431 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001432
Chris Lattner596760d2009-12-29 21:25:40 +00001433 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001434 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001435 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001436
Chris Lattner1797fc72009-12-29 21:53:55 +00001437 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001438 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001439
1440 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001441 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001442
Dan Gohmanf0715b12010-08-24 14:35:45 +00001443 // This code is similar to that of ParseMetadataValue, however it needs to
1444 // have special-case code for a forward reference; see the comments on
1445 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1446 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001447 if (Lex.getKind() == lltok::lbrace) {
1448 ValID ID;
1449 if (ParseMetadataListValue(ID, PFS))
1450 return true;
1451 assert(ID.Kind == ValID::t_MDNode);
1452 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001453 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001454 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001455 if (ParseMDNodeID(Node, NodeID))
1456 return true;
1457 if (Node) {
1458 // If we got the node, add it to the instruction.
1459 Inst->setMetadata(MDK, Node);
1460 } else {
1461 MDRef R = { Loc, MDK, NodeID };
1462 // Otherwise, remember that this should be resolved later.
1463 ForwardRefInstMetadata[Inst].push_back(R);
1464 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001465 }
Chris Lattner596760d2009-12-29 21:25:40 +00001466
Manman Ren209b17c2013-09-28 00:22:27 +00001467 if (MDK == LLVMContext::MD_tbaa)
1468 InstsWithTBAATag.push_back(Inst);
1469
Chris Lattner596760d2009-12-29 21:25:40 +00001470 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001471 } while (EatIfPresent(lltok::comma));
1472 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001473}
1474
Chris Lattnerac161bf2009-01-02 07:01:27 +00001475/// ParseOptionalAlignment
1476/// ::= /* empty */
1477/// ::= 'align' 4
1478bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1479 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001480 if (!EatIfPresent(lltok::kw_align))
1481 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001482 LocTy AlignLoc = Lex.getLoc();
1483 if (ParseUInt32(Alignment)) return true;
1484 if (!isPowerOf2_32(Alignment))
1485 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001486 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001487 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001488 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001489}
1490
Chris Lattnerb2f39502009-12-30 05:44:30 +00001491/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001492/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001493/// ::= ',' align 4
1494///
1495/// This returns with AteExtraComma set to true if it ate an excess comma at the
1496/// end.
1497bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1498 bool &AteExtraComma) {
1499 AteExtraComma = false;
1500 while (EatIfPresent(lltok::comma)) {
1501 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001502 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001503 AteExtraComma = true;
1504 return false;
1505 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001506
Chris Lattner95b0ff42010-04-23 00:50:50 +00001507 if (Lex.getKind() != lltok::kw_align)
1508 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001509
Chris Lattner95b0ff42010-04-23 00:50:50 +00001510 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001511 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001512
Devang Patelea8a4b92009-09-17 23:04:48 +00001513 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001514}
1515
Eli Friedmanfee02c62011-07-25 23:16:38 +00001516/// ParseScopeAndOrdering
1517/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1518/// else: ::=
1519///
1520/// This sets Scope and Ordering to the parsed values.
1521bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1522 AtomicOrdering &Ordering) {
1523 if (!isAtomic)
1524 return false;
1525
1526 Scope = CrossThread;
1527 if (EatIfPresent(lltok::kw_singlethread))
1528 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001529
1530 return ParseOrdering(Ordering);
1531}
1532
1533/// ParseOrdering
1534/// ::= AtomicOrdering
1535///
1536/// This sets Ordering to the parsed value.
1537bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001538 switch (Lex.getKind()) {
1539 default: return TokError("Expected ordering on atomic instruction");
1540 case lltok::kw_unordered: Ordering = Unordered; break;
1541 case lltok::kw_monotonic: Ordering = Monotonic; break;
1542 case lltok::kw_acquire: Ordering = Acquire; break;
1543 case lltok::kw_release: Ordering = Release; break;
1544 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1545 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1546 }
1547 Lex.Lex();
1548 return false;
1549}
1550
Charles Davisbe5557e2010-02-12 00:31:15 +00001551/// ParseOptionalStackAlignment
1552/// ::= /* empty */
1553/// ::= 'alignstack' '(' 4 ')'
1554bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1555 Alignment = 0;
1556 if (!EatIfPresent(lltok::kw_alignstack))
1557 return false;
1558 LocTy ParenLoc = Lex.getLoc();
1559 if (!EatIfPresent(lltok::lparen))
1560 return Error(ParenLoc, "expected '('");
1561 LocTy AlignLoc = Lex.getLoc();
1562 if (ParseUInt32(Alignment)) return true;
1563 ParenLoc = Lex.getLoc();
1564 if (!EatIfPresent(lltok::rparen))
1565 return Error(ParenLoc, "expected ')'");
1566 if (!isPowerOf2_32(Alignment))
1567 return Error(AlignLoc, "stack alignment is not a power of two");
1568 return false;
1569}
Devang Patelea8a4b92009-09-17 23:04:48 +00001570
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001571/// ParseIndexList - This parses the index list for an insert/extractvalue
1572/// instruction. This sets AteExtraComma in the case where we eat an extra
1573/// comma at the end of the line and find that it is followed by metadata.
1574/// Clients that don't allow metadata can call the version of this function that
1575/// only takes one argument.
1576///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001577/// ParseIndexList
1578/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001579///
1580bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1581 bool &AteExtraComma) {
1582 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001583
Chris Lattnerac161bf2009-01-02 07:01:27 +00001584 if (Lex.getKind() != lltok::comma)
1585 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001586
Chris Lattner3822f632009-01-02 08:05:26 +00001587 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001588 if (Lex.getKind() == lltok::MetadataVar) {
1589 AteExtraComma = true;
1590 return false;
1591 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001592 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001593 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001594 Indices.push_back(Idx);
1595 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001596
Chris Lattnerac161bf2009-01-02 07:01:27 +00001597 return false;
1598}
1599
1600//===----------------------------------------------------------------------===//
1601// Type Parsing.
1602//===----------------------------------------------------------------------===//
1603
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001604/// ParseType - Parse a type.
1605bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1606 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001607 switch (Lex.getKind()) {
1608 default:
1609 return TokError("expected type");
1610 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001611 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001612 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001613 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001614 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001615 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001616 // Type ::= StructType
1617 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001618 return true;
1619 break;
1620 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001621 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001622 Lex.Lex(); // eat the lsquare.
1623 if (ParseArrayVectorType(Result, false))
1624 return true;
1625 break;
1626 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001627 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001628 Lex.Lex();
1629 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001630 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001631 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001632 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001633 } else if (ParseArrayVectorType(Result, true))
1634 return true;
1635 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001636 case lltok::LocalVar: {
1637 // Type ::= %foo
1638 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001639
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001640 // If the type hasn't been defined yet, create a forward definition and
1641 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001642 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001643 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001644 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001645 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001646 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001647 Lex.Lex();
1648 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001649 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001650
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001651 case lltok::LocalVarID: {
1652 // Type ::= %4
1653 if (Lex.getUIntVal() >= NumberedTypes.size())
1654 NumberedTypes.resize(Lex.getUIntVal()+1);
1655 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001656
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001657 // If the type hasn't been defined yet, create a forward definition and
1658 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001659 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001660 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001661 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001662 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001663 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001664 Lex.Lex();
1665 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001666 }
1667 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001668
1669 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001670 while (1) {
1671 switch (Lex.getKind()) {
1672 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001673 default:
1674 if (!AllowVoid && Result->isVoidTy())
1675 return Error(TypeLoc, "void type only allowed for function results");
1676 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001677
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001678 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001679 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001680 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001681 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001682 if (Result->isVoidTy())
1683 return TokError("pointers to void are invalid - use i8* instead");
1684 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001685 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001686 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001687 Lex.Lex();
1688 break;
1689
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001690 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001691 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001692 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001693 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001694 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001695 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001696 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001697 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001698 unsigned AddrSpace;
1699 if (ParseOptionalAddrSpace(AddrSpace) ||
1700 ParseToken(lltok::star, "expected '*' in address space"))
1701 return true;
1702
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001703 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001704 break;
1705 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001706
Chris Lattnerac161bf2009-01-02 07:01:27 +00001707 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1708 case lltok::lparen:
1709 if (ParseFunctionType(Result))
1710 return true;
1711 break;
1712 }
1713 }
1714}
1715
1716/// ParseParameterList
1717/// ::= '(' ')'
1718/// ::= '(' Arg (',' Arg)* ')'
1719/// Arg
1720/// ::= Type OptionalAttributes Value OptionalAttributes
1721bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1722 PerFunctionState &PFS) {
1723 if (ParseToken(lltok::lparen, "expected '(' in call"))
1724 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001725
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001726 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001727 while (Lex.getKind() != lltok::rparen) {
1728 // If this isn't the first argument, we need a comma.
1729 if (!ArgList.empty() &&
1730 ParseToken(lltok::comma, "expected ',' in argument list"))
1731 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001732
Chris Lattnerac161bf2009-01-02 07:01:27 +00001733 // Parse the argument.
1734 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001735 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001736 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001737 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001738 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001739 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001740
Chris Lattner5b4a9622009-12-30 02:11:14 +00001741 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001742 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001743 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001744 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1745 AttrIndex++,
1746 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001747 }
1748
1749 Lex.Lex(); // Lex the ')'.
1750 return false;
1751}
1752
1753
1754
Chris Lattner2ed06b42009-01-05 18:34:07 +00001755/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001756/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001757/// ::= '(' ArgTypeListI ')'
1758/// ArgTypeListI
1759/// ::= /*empty*/
1760/// ::= '...'
1761/// ::= ArgTypeList ',' '...'
1762/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001763///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001764bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1765 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001766 isVarArg = false;
1767 assert(Lex.getKind() == lltok::lparen);
1768 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001769
Chris Lattnerac161bf2009-01-02 07:01:27 +00001770 if (Lex.getKind() == lltok::rparen) {
1771 // empty
1772 } else if (Lex.getKind() == lltok::dotdotdot) {
1773 isVarArg = true;
1774 Lex.Lex();
1775 } else {
1776 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001777 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001778 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001779 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001780
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001781 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001782 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001783
Chris Lattnerfdd87902009-10-05 05:54:46 +00001784 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001785 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001786
Chris Lattnerdef19492011-06-17 06:36:20 +00001787 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001788 Name = Lex.getStrVal();
1789 Lex.Lex();
1790 }
Chris Lattner3822f632009-01-02 08:05:26 +00001791
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001792 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001793 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001794
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001795 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001796 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001797 AttributeSet::get(ArgTy->getContext(),
1798 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001799
Chris Lattner3822f632009-01-02 08:05:26 +00001800 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001801 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001802 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001803 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001804 break;
1805 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001806
Chris Lattnerac161bf2009-01-02 07:01:27 +00001807 // Otherwise must be an argument type.
1808 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001809 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001810
Chris Lattnerfdd87902009-10-05 05:54:46 +00001811 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001812 return Error(TypeLoc, "argument can not have void type");
1813
Chris Lattnerdef19492011-06-17 06:36:20 +00001814 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001815 Name = Lex.getStrVal();
1816 Lex.Lex();
1817 } else {
1818 Name = "";
1819 }
Chris Lattner3822f632009-01-02 08:05:26 +00001820
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001821 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001822 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001823
Bill Wendlingd079a442012-10-15 04:46:55 +00001824 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001825 AttributeSet::get(ArgTy->getContext(),
1826 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001827 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001828 }
1829 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001830
Chris Lattner3822f632009-01-02 08:05:26 +00001831 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001832}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001833
Chris Lattnerac161bf2009-01-02 07:01:27 +00001834/// ParseFunctionType
1835/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001836bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001837 assert(Lex.getKind() == lltok::lparen);
1838
Chris Lattnerce473c72009-01-05 08:04:33 +00001839 if (!FunctionType::isValidReturnType(Result))
1840 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001841
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001842 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001843 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001844 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001845 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001846
Chris Lattnerac161bf2009-01-02 07:01:27 +00001847 // Reject names on the arguments lists.
1848 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1849 if (!ArgList[i].Name.empty())
1850 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001851 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001852 return Error(ArgList[i].Loc,
1853 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001854 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001855
Jay Foadb804a2b2011-07-12 14:06:48 +00001856 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001857 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001858 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001859
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001860 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001861 return false;
1862}
1863
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001864/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1865/// other structs.
1866bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1867 SmallVector<Type*, 8> Elts;
1868 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001869
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001870 Result = StructType::get(Context, Elts, Packed);
1871 return false;
1872}
1873
1874/// ParseStructDefinition - Parse a struct in a 'type' definition.
1875bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1876 std::pair<Type*, LocTy> &Entry,
1877 Type *&ResultTy) {
1878 // If the type was already defined, diagnose the redefinition.
1879 if (Entry.first && !Entry.second.isValid())
1880 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001881
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001882 // If we have opaque, just return without filling in the definition for the
1883 // struct. This counts as a definition as far as the .ll file goes.
1884 if (EatIfPresent(lltok::kw_opaque)) {
1885 // This type is being defined, so clear the location to indicate this.
1886 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001887
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001888 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001889 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001890 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001891 ResultTy = Entry.first;
1892 return false;
1893 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001894
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001895 // If the type starts with '<', then it is either a packed struct or a vector.
1896 bool isPacked = EatIfPresent(lltok::less);
1897
1898 // If we don't have a struct, then we have a random type alias, which we
1899 // accept for compatibility with old files. These types are not allowed to be
1900 // forward referenced and not allowed to be recursive.
1901 if (Lex.getKind() != lltok::lbrace) {
1902 if (Entry.first)
1903 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001904
Craig Topper2617dcc2014-04-15 06:32:26 +00001905 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001906 if (isPacked)
1907 return ParseArrayVectorType(ResultTy, true);
1908 return ParseType(ResultTy);
1909 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001910
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001911 // This type is being defined, so clear the location to indicate this.
1912 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001913
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001914 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001915 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001916 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001917
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001918 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001919
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001920 SmallVector<Type*, 8> Body;
1921 if (ParseStructBody(Body) ||
1922 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1923 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001924
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001925 STy->setBody(Body, isPacked);
1926 ResultTy = STy;
1927 return false;
1928}
1929
1930
Chris Lattnerac161bf2009-01-02 07:01:27 +00001931/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001932/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001933/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001934/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001935/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001936/// ::= '<' '{' Type (',' Type)* '}' '>'
1937bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001938 assert(Lex.getKind() == lltok::lbrace);
1939 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001940
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001941 // Handle the empty struct.
1942 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001943 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001944
Chris Lattnerf880ca22009-03-09 04:49:14 +00001945 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001946 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001947 if (ParseType(Ty)) return true;
1948 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001949
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001950 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001951 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001952
Chris Lattner3822f632009-01-02 08:05:26 +00001953 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001954 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001955 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001956
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001957 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001958 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001959
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001960 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001961 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001962
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001963 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001964}
1965
1966/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1967/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001968/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00001969/// ::= '[' APSINTVAL 'x' Types ']'
1970/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001971bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001972 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1973 Lex.getAPSIntVal().getBitWidth() > 64)
1974 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001975
Chris Lattnerac161bf2009-01-02 07:01:27 +00001976 LocTy SizeLoc = Lex.getLoc();
1977 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00001978 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001979
Chris Lattner3822f632009-01-02 08:05:26 +00001980 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1981 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001982
1983 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001984 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001985 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00001986
Chris Lattner3822f632009-01-02 08:05:26 +00001987 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
1988 "expected end of sequential type"))
1989 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001990
Chris Lattnerac161bf2009-01-02 07:01:27 +00001991 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00001992 if (Size == 0)
1993 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001994 if ((unsigned)Size != Size)
1995 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001996 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00001997 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00001998 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001999 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002000 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002001 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002002 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002003 }
2004 return false;
2005}
2006
2007//===----------------------------------------------------------------------===//
2008// Function Semantic Analysis.
2009//===----------------------------------------------------------------------===//
2010
Chris Lattner3432c622009-10-28 03:39:23 +00002011LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2012 int functionNumber)
2013 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002014
2015 // Insert unnamed arguments into the NumberedVals list.
2016 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2017 AI != E; ++AI)
2018 if (!AI->hasName())
2019 NumberedVals.push_back(AI);
2020}
2021
2022LLParser::PerFunctionState::~PerFunctionState() {
2023 // If there were any forward referenced non-basicblock values, delete them.
2024 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2025 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2026 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002027 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002028 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002029 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002030 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002031 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002032
Chris Lattnerac161bf2009-01-02 07:01:27 +00002033 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2034 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2035 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002036 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002037 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002038 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002039 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002040 }
2041}
2042
Chris Lattner3432c622009-10-28 03:39:23 +00002043bool LLParser::PerFunctionState::FinishFunction() {
2044 // Check to see if someone took the address of labels in this block.
2045 if (!P.ForwardRefBlockAddresses.empty()) {
2046 ValID FunctionID;
2047 if (!F.getName().empty()) {
2048 FunctionID.Kind = ValID::t_GlobalName;
2049 FunctionID.StrVal = F.getName();
2050 } else {
2051 FunctionID.Kind = ValID::t_GlobalID;
2052 FunctionID.UIntVal = FunctionNumber;
2053 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002054
Chris Lattner3432c622009-10-28 03:39:23 +00002055 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2056 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2057 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2058 // Resolve all these references.
2059 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2060 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002061
Chris Lattner3432c622009-10-28 03:39:23 +00002062 P.ForwardRefBlockAddresses.erase(FRBAI);
2063 }
2064 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002065
Chris Lattnerac161bf2009-01-02 07:01:27 +00002066 if (!ForwardRefVals.empty())
2067 return P.Error(ForwardRefVals.begin()->second.second,
2068 "use of undefined value '%" + ForwardRefVals.begin()->first +
2069 "'");
2070 if (!ForwardRefValIDs.empty())
2071 return P.Error(ForwardRefValIDs.begin()->second.second,
2072 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002073 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002074 return false;
2075}
2076
2077
2078/// GetVal - Get a value with the specified name or ID, creating a
2079/// forward reference record if needed. This can return null if the value
2080/// exists but does not have the right type.
2081Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002082 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002083 // Look this name up in the normal function symbol table.
2084 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002085
Chris Lattnerac161bf2009-01-02 07:01:27 +00002086 // If this is a forward reference for the value, see if we already created a
2087 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002088 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002089 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2090 I = ForwardRefVals.find(Name);
2091 if (I != ForwardRefVals.end())
2092 Val = I->second.first;
2093 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002094
Chris Lattnerac161bf2009-01-02 07:01:27 +00002095 // If we have the value in the symbol table or fwd-ref table, return it.
2096 if (Val) {
2097 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002098 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002099 P.Error(Loc, "'%" + Name + "' is not a basic block");
2100 else
2101 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002102 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002103 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002104 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002105
Chris Lattnerac161bf2009-01-02 07:01:27 +00002106 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002107 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002108 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002109 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002110 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002111
Chris Lattnerac161bf2009-01-02 07:01:27 +00002112 // Otherwise, create a new forward reference for this value and remember it.
2113 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002114 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002115 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002116 else
2117 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002118
Chris Lattnerac161bf2009-01-02 07:01:27 +00002119 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2120 return FwdVal;
2121}
2122
Chris Lattner229907c2011-07-18 04:54:35 +00002123Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002124 LocTy Loc) {
2125 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002126 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002127
Chris Lattnerac161bf2009-01-02 07:01:27 +00002128 // If this is a forward reference for the value, see if we already created a
2129 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002130 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002131 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2132 I = ForwardRefValIDs.find(ID);
2133 if (I != ForwardRefValIDs.end())
2134 Val = I->second.first;
2135 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002136
Chris Lattnerac161bf2009-01-02 07:01:27 +00002137 // If we have the value in the symbol table or fwd-ref table, return it.
2138 if (Val) {
2139 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002140 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002141 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002142 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002143 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002144 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002145 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002146 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002147
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002148 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002149 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002150 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002151 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002152
Chris Lattnerac161bf2009-01-02 07:01:27 +00002153 // Otherwise, create a new forward reference for this value and remember it.
2154 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002155 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002156 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002157 else
2158 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002159
Chris Lattnerac161bf2009-01-02 07:01:27 +00002160 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2161 return FwdVal;
2162}
2163
2164/// SetInstName - After an instruction is parsed and inserted into its
2165/// basic block, this installs its name.
2166bool LLParser::PerFunctionState::SetInstName(int NameID,
2167 const std::string &NameStr,
2168 LocTy NameLoc, Instruction *Inst) {
2169 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002170 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002171 if (NameID != -1 || !NameStr.empty())
2172 return P.Error(NameLoc, "instructions returning void cannot have a name");
2173 return false;
2174 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002175
Chris Lattnerac161bf2009-01-02 07:01:27 +00002176 // If this was a numbered instruction, verify that the instruction is the
2177 // expected value and resolve any forward references.
2178 if (NameStr.empty()) {
2179 // If neither a name nor an ID was specified, just use the next ID.
2180 if (NameID == -1)
2181 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002182
Chris Lattnerac161bf2009-01-02 07:01:27 +00002183 if (unsigned(NameID) != NumberedVals.size())
2184 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002185 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002186
Chris Lattnerac161bf2009-01-02 07:01:27 +00002187 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2188 ForwardRefValIDs.find(NameID);
2189 if (FI != ForwardRefValIDs.end()) {
2190 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002191 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002192 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002193 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002194 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002195 ForwardRefValIDs.erase(FI);
2196 }
2197
2198 NumberedVals.push_back(Inst);
2199 return false;
2200 }
2201
2202 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2203 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2204 FI = ForwardRefVals.find(NameStr);
2205 if (FI != ForwardRefVals.end()) {
2206 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002207 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002208 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002209 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002210 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002211 ForwardRefVals.erase(FI);
2212 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002213
Chris Lattnerac161bf2009-01-02 07:01:27 +00002214 // Set the name on the instruction.
2215 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002216
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002217 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002218 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002219 NameStr + "'");
2220 return false;
2221}
2222
2223/// GetBB - Get a basic block with the specified name or ID, creating a
2224/// forward reference record if needed.
2225BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2226 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002227 return cast_or_null<BasicBlock>(GetVal(Name,
2228 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002229}
2230
2231BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002232 return cast_or_null<BasicBlock>(GetVal(ID,
2233 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002234}
2235
2236/// DefineBB - Define the specified basic block, which is either named or
2237/// unnamed. If there is an error, this returns null otherwise it returns
2238/// the block being defined.
2239BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2240 LocTy Loc) {
2241 BasicBlock *BB;
2242 if (Name.empty())
2243 BB = GetBB(NumberedVals.size(), Loc);
2244 else
2245 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002246 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002247
Chris Lattnerac161bf2009-01-02 07:01:27 +00002248 // Move the block to the end of the function. Forward ref'd blocks are
2249 // inserted wherever they happen to be referenced.
2250 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002251
Chris Lattnerac161bf2009-01-02 07:01:27 +00002252 // Remove the block from forward ref sets.
2253 if (Name.empty()) {
2254 ForwardRefValIDs.erase(NumberedVals.size());
2255 NumberedVals.push_back(BB);
2256 } else {
2257 // BB forward references are already in the function symbol table.
2258 ForwardRefVals.erase(Name);
2259 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002260
Chris Lattnerac161bf2009-01-02 07:01:27 +00002261 return BB;
2262}
2263
2264//===----------------------------------------------------------------------===//
2265// Constants.
2266//===----------------------------------------------------------------------===//
2267
2268/// ParseValID - Parse an abstract value that doesn't necessarily have a
2269/// type implied. For example, if we parse "4" we don't know what integer type
2270/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002271/// sanity. PFS is used to convert function-local operands of metadata (since
2272/// metadata operands are not just parsed here but also converted to values).
2273/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002274bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002275 ID.Loc = Lex.getLoc();
2276 switch (Lex.getKind()) {
2277 default: return TokError("expected value token");
2278 case lltok::GlobalID: // @42
2279 ID.UIntVal = Lex.getUIntVal();
2280 ID.Kind = ValID::t_GlobalID;
2281 break;
2282 case lltok::GlobalVar: // @foo
2283 ID.StrVal = Lex.getStrVal();
2284 ID.Kind = ValID::t_GlobalName;
2285 break;
2286 case lltok::LocalVarID: // %42
2287 ID.UIntVal = Lex.getUIntVal();
2288 ID.Kind = ValID::t_LocalID;
2289 break;
2290 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002291 ID.StrVal = Lex.getStrVal();
2292 ID.Kind = ValID::t_LocalName;
2293 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002294 case lltok::exclaim: // !42, !{...}, or !"foo"
2295 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002296 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002297 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002298 ID.Kind = ValID::t_APSInt;
2299 break;
2300 case lltok::APFloat:
2301 ID.APFloatVal = Lex.getAPFloatVal();
2302 ID.Kind = ValID::t_APFloat;
2303 break;
2304 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002305 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002306 ID.Kind = ValID::t_Constant;
2307 break;
2308 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002309 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002310 ID.Kind = ValID::t_Constant;
2311 break;
2312 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2313 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2314 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002315
Chris Lattnerac161bf2009-01-02 07:01:27 +00002316 case lltok::lbrace: {
2317 // ValID ::= '{' ConstVector '}'
2318 Lex.Lex();
2319 SmallVector<Constant*, 16> Elts;
2320 if (ParseGlobalValueVector(Elts) ||
2321 ParseToken(lltok::rbrace, "expected end of struct constant"))
2322 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002323
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002324 ID.ConstantStructElts = new Constant*[Elts.size()];
2325 ID.UIntVal = Elts.size();
2326 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2327 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002328 return false;
2329 }
2330 case lltok::less: {
2331 // ValID ::= '<' ConstVector '>' --> Vector.
2332 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2333 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002334 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002335
Chris Lattnerac161bf2009-01-02 07:01:27 +00002336 SmallVector<Constant*, 16> Elts;
2337 LocTy FirstEltLoc = Lex.getLoc();
2338 if (ParseGlobalValueVector(Elts) ||
2339 (isPackedStruct &&
2340 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2341 ParseToken(lltok::greater, "expected end of constant"))
2342 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002343
Chris Lattnerac161bf2009-01-02 07:01:27 +00002344 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002345 ID.ConstantStructElts = new Constant*[Elts.size()];
2346 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2347 ID.UIntVal = Elts.size();
2348 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002349 return false;
2350 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002351
Chris Lattnerac161bf2009-01-02 07:01:27 +00002352 if (Elts.empty())
2353 return Error(ID.Loc, "constant vector must not be empty");
2354
Duncan Sands9dff9be2010-02-15 16:12:20 +00002355 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002356 !Elts[0]->getType()->isFloatingPointTy() &&
2357 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002358 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002359 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002360
Chris Lattnerac161bf2009-01-02 07:01:27 +00002361 // Verify that all the vector elements have the same type.
2362 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2363 if (Elts[i]->getType() != Elts[0]->getType())
2364 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002365 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002366 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002367
Chris Lattner69229312011-02-15 00:14:00 +00002368 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002369 ID.Kind = ValID::t_Constant;
2370 return false;
2371 }
2372 case lltok::lsquare: { // Array Constant
2373 Lex.Lex();
2374 SmallVector<Constant*, 16> Elts;
2375 LocTy FirstEltLoc = Lex.getLoc();
2376 if (ParseGlobalValueVector(Elts) ||
2377 ParseToken(lltok::rsquare, "expected end of array constant"))
2378 return true;
2379
2380 // Handle empty element.
2381 if (Elts.empty()) {
2382 // Use undef instead of an array because it's inconvenient to determine
2383 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002384 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002385 return false;
2386 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002387
Chris Lattnerac161bf2009-01-02 07:01:27 +00002388 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002389 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002390 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002391
Owen Anderson4056ca92009-07-29 22:17:13 +00002392 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002393
Chris Lattnerac161bf2009-01-02 07:01:27 +00002394 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002395 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002396 if (Elts[i]->getType() != Elts[0]->getType())
2397 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002398 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002399 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002400 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002401
Jay Foad83be3612011-06-22 09:24:39 +00002402 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002403 ID.Kind = ValID::t_Constant;
2404 return false;
2405 }
2406 case lltok::kw_c: // c "foo"
2407 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002408 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2409 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002410 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2411 ID.Kind = ValID::t_Constant;
2412 return false;
2413
2414 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002415 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2416 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002417 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002418 Lex.Lex();
2419 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002420 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002421 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002422 ParseStringConstant(ID.StrVal) ||
2423 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002424 ParseToken(lltok::StringConstant, "expected constraint string"))
2425 return true;
2426 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002427 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002428 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002429 ID.Kind = ValID::t_InlineAsm;
2430 return false;
2431 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002432
Chris Lattner3432c622009-10-28 03:39:23 +00002433 case lltok::kw_blockaddress: {
2434 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2435 Lex.Lex();
2436
2437 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002438
Chris Lattner3432c622009-10-28 03:39:23 +00002439 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2440 ParseValID(Fn) ||
2441 ParseToken(lltok::comma, "expected comma in block address expression")||
2442 ParseValID(Label) ||
2443 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2444 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002445
Chris Lattner3432c622009-10-28 03:39:23 +00002446 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2447 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002448 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002449 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002450
Chris Lattner3432c622009-10-28 03:39:23 +00002451 // Make a global variable as a placeholder for this reference.
2452 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2453 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002454 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002455 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2456 ID.ConstantVal = FwdRef;
2457 ID.Kind = ValID::t_Constant;
2458 return false;
2459 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002460
Chris Lattnerac161bf2009-01-02 07:01:27 +00002461 case lltok::kw_trunc:
2462 case lltok::kw_zext:
2463 case lltok::kw_sext:
2464 case lltok::kw_fptrunc:
2465 case lltok::kw_fpext:
2466 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002467 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002468 case lltok::kw_uitofp:
2469 case lltok::kw_sitofp:
2470 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002471 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002472 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002473 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002474 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002475 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002476 Constant *SrcVal;
2477 Lex.Lex();
2478 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2479 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002480 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002481 ParseType(DestTy) ||
2482 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2483 return true;
2484 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2485 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002486 getTypeString(SrcVal->getType()) + "' to '" +
2487 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002488 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002489 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002490 ID.Kind = ValID::t_Constant;
2491 return false;
2492 }
2493 case lltok::kw_extractvalue: {
2494 Lex.Lex();
2495 Constant *Val;
2496 SmallVector<unsigned, 4> Indices;
2497 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2498 ParseGlobalTypeAndValue(Val) ||
2499 ParseIndexList(Indices) ||
2500 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2501 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002502
Chris Lattner392be582010-02-12 20:49:41 +00002503 if (!Val->getType()->isAggregateType())
2504 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002505 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002506 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002507 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002508 ID.Kind = ValID::t_Constant;
2509 return false;
2510 }
2511 case lltok::kw_insertvalue: {
2512 Lex.Lex();
2513 Constant *Val0, *Val1;
2514 SmallVector<unsigned, 4> Indices;
2515 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2516 ParseGlobalTypeAndValue(Val0) ||
2517 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2518 ParseGlobalTypeAndValue(Val1) ||
2519 ParseIndexList(Indices) ||
2520 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2521 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002522 if (!Val0->getType()->isAggregateType())
2523 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002524 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002525 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002526 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002527 ID.Kind = ValID::t_Constant;
2528 return false;
2529 }
2530 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002531 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002532 unsigned PredVal, Opc = Lex.getUIntVal();
2533 Constant *Val0, *Val1;
2534 Lex.Lex();
2535 if (ParseCmpPredicate(PredVal, Opc) ||
2536 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2537 ParseGlobalTypeAndValue(Val0) ||
2538 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2539 ParseGlobalTypeAndValue(Val1) ||
2540 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2541 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002542
Chris Lattnerac161bf2009-01-02 07:01:27 +00002543 if (Val0->getType() != Val1->getType())
2544 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002545
Chris Lattnerac161bf2009-01-02 07:01:27 +00002546 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002547
Chris Lattnerac161bf2009-01-02 07:01:27 +00002548 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002549 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002550 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002551 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002552 } else {
2553 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002554 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002555 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002556 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002557 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002558 }
2559 ID.Kind = ValID::t_Constant;
2560 return false;
2561 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002562
Chris Lattnerac161bf2009-01-02 07:01:27 +00002563 // Binary Operators.
2564 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002565 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002566 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002567 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002568 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002569 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002570 case lltok::kw_udiv:
2571 case lltok::kw_sdiv:
2572 case lltok::kw_fdiv:
2573 case lltok::kw_urem:
2574 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002575 case lltok::kw_frem:
2576 case lltok::kw_shl:
2577 case lltok::kw_lshr:
2578 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002579 bool NUW = false;
2580 bool NSW = false;
2581 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002582 unsigned Opc = Lex.getUIntVal();
2583 Constant *Val0, *Val1;
2584 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002585 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002586 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2587 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002588 if (EatIfPresent(lltok::kw_nuw))
2589 NUW = true;
2590 if (EatIfPresent(lltok::kw_nsw)) {
2591 NSW = true;
2592 if (EatIfPresent(lltok::kw_nuw))
2593 NUW = true;
2594 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002595 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2596 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002597 if (EatIfPresent(lltok::kw_exact))
2598 Exact = true;
2599 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002600 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2601 ParseGlobalTypeAndValue(Val0) ||
2602 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2603 ParseGlobalTypeAndValue(Val1) ||
2604 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2605 return true;
2606 if (Val0->getType() != Val1->getType())
2607 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002608 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002609 if (NUW)
2610 return Error(ModifierLoc, "nuw only applies to integer operations");
2611 if (NSW)
2612 return Error(ModifierLoc, "nsw only applies to integer operations");
2613 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002614 // Check that the type is valid for the operator.
2615 switch (Opc) {
2616 case Instruction::Add:
2617 case Instruction::Sub:
2618 case Instruction::Mul:
2619 case Instruction::UDiv:
2620 case Instruction::SDiv:
2621 case Instruction::URem:
2622 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002623 case Instruction::Shl:
2624 case Instruction::AShr:
2625 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002626 if (!Val0->getType()->isIntOrIntVectorTy())
2627 return Error(ID.Loc, "constexpr requires integer operands");
2628 break;
2629 case Instruction::FAdd:
2630 case Instruction::FSub:
2631 case Instruction::FMul:
2632 case Instruction::FDiv:
2633 case Instruction::FRem:
2634 if (!Val0->getType()->isFPOrFPVectorTy())
2635 return Error(ID.Loc, "constexpr requires fp operands");
2636 break;
2637 default: llvm_unreachable("Unknown binary operator!");
2638 }
Dan Gohman1b849082009-09-07 23:54:19 +00002639 unsigned Flags = 0;
2640 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2641 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002642 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002643 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002644 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002645 ID.Kind = ValID::t_Constant;
2646 return false;
2647 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002648
Chris Lattnerac161bf2009-01-02 07:01:27 +00002649 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002650 case lltok::kw_and:
2651 case lltok::kw_or:
2652 case lltok::kw_xor: {
2653 unsigned Opc = Lex.getUIntVal();
2654 Constant *Val0, *Val1;
2655 Lex.Lex();
2656 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2657 ParseGlobalTypeAndValue(Val0) ||
2658 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2659 ParseGlobalTypeAndValue(Val1) ||
2660 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2661 return true;
2662 if (Val0->getType() != Val1->getType())
2663 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002664 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002665 return Error(ID.Loc,
2666 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002667 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002668 ID.Kind = ValID::t_Constant;
2669 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002670 }
2671
Chris Lattnerac161bf2009-01-02 07:01:27 +00002672 case lltok::kw_getelementptr:
2673 case lltok::kw_shufflevector:
2674 case lltok::kw_insertelement:
2675 case lltok::kw_extractelement:
2676 case lltok::kw_select: {
2677 unsigned Opc = Lex.getUIntVal();
2678 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002679 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002680 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002681 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002682 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002683 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2684 ParseGlobalValueVector(Elts) ||
2685 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2686 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002687
Chris Lattnerac161bf2009-01-02 07:01:27 +00002688 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002689 if (Elts.size() == 0 ||
2690 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002691 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002692
Jay Foaded8db7d2011-07-21 14:31:17 +00002693 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002694 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002695 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002696 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2697 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002698 } else if (Opc == Instruction::Select) {
2699 if (Elts.size() != 3)
2700 return Error(ID.Loc, "expected three operands to select");
2701 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2702 Elts[2]))
2703 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002704 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002705 } else if (Opc == Instruction::ShuffleVector) {
2706 if (Elts.size() != 3)
2707 return Error(ID.Loc, "expected three operands to shufflevector");
2708 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2709 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002710 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002711 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002712 } else if (Opc == Instruction::ExtractElement) {
2713 if (Elts.size() != 2)
2714 return Error(ID.Loc, "expected two operands to extractelement");
2715 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2716 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002717 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002718 } else {
2719 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2720 if (Elts.size() != 3)
2721 return Error(ID.Loc, "expected three operands to insertelement");
2722 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2723 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002724 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002725 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002726 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002727
Chris Lattnerac161bf2009-01-02 07:01:27 +00002728 ID.Kind = ValID::t_Constant;
2729 return false;
2730 }
2731 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002732
Chris Lattnerac161bf2009-01-02 07:01:27 +00002733 Lex.Lex();
2734 return false;
2735}
2736
2737/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002738bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002739 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002740 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002741 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002742 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002743 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002744 if (V && !(C = dyn_cast<Constant>(V)))
2745 return Error(ID.Loc, "global values must be constants");
2746 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002747}
2748
Victor Hernandez9d75c962010-01-11 22:31:58 +00002749bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002750 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002751 return ParseType(Ty) ||
2752 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002753}
2754
2755/// ParseGlobalValueVector
2756/// ::= /*empty*/
2757/// ::= TypeAndValue (',' TypeAndValue)*
2758bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2759 // Empty list.
2760 if (Lex.getKind() == lltok::rbrace ||
2761 Lex.getKind() == lltok::rsquare ||
2762 Lex.getKind() == lltok::greater ||
2763 Lex.getKind() == lltok::rparen)
2764 return false;
2765
2766 Constant *C;
2767 if (ParseGlobalTypeAndValue(C)) return true;
2768 Elts.push_back(C);
2769
2770 while (EatIfPresent(lltok::comma)) {
2771 if (ParseGlobalTypeAndValue(C)) return true;
2772 Elts.push_back(C);
2773 }
2774
2775 return false;
2776}
2777
Dan Gohmanc828c542010-08-24 02:24:03 +00002778bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2779 assert(Lex.getKind() == lltok::lbrace);
2780 Lex.Lex();
2781
2782 SmallVector<Value*, 16> Elts;
2783 if (ParseMDNodeVector(Elts, PFS) ||
2784 ParseToken(lltok::rbrace, "expected end of metadata node"))
2785 return true;
2786
Jay Foad5514afe2011-04-21 19:59:31 +00002787 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002788 ID.Kind = ValID::t_MDNode;
2789 return false;
2790}
2791
Dan Gohman8939ba332010-07-14 18:26:50 +00002792/// ParseMetadataValue
2793/// ::= !42
2794/// ::= !{...}
2795/// ::= !"string"
2796bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2797 assert(Lex.getKind() == lltok::exclaim);
2798 Lex.Lex();
2799
2800 // MDNode:
2801 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002802 if (Lex.getKind() == lltok::lbrace)
2803 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002804
2805 // Standalone metadata reference
2806 // !42
2807 if (Lex.getKind() == lltok::APSInt) {
2808 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2809 ID.Kind = ValID::t_MDNode;
2810 return false;
2811 }
2812
2813 // MDString:
2814 // ::= '!' STRINGCONSTANT
2815 if (ParseMDString(ID.MDStringVal)) return true;
2816 ID.Kind = ValID::t_MDString;
2817 return false;
2818}
2819
Victor Hernandez9d75c962010-01-11 22:31:58 +00002820
2821//===----------------------------------------------------------------------===//
2822// Function Parsing.
2823//===----------------------------------------------------------------------===//
2824
Chris Lattner229907c2011-07-18 04:54:35 +00002825bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002826 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002827 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002828 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002829
Chris Lattnerac161bf2009-01-02 07:01:27 +00002830 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002831 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002832 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2833 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002834 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002835 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002836 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2837 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002838 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002839 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002840 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002841 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002842 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002843 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2844 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002845 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002846 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002847 return false;
2848 }
2849 case ValID::t_MDNode:
2850 if (!Ty->isMetadataTy())
2851 return Error(ID.Loc, "metadata value must have metadata type");
2852 V = ID.MDNodeVal;
2853 return false;
2854 case ValID::t_MDString:
2855 if (!Ty->isMetadataTy())
2856 return Error(ID.Loc, "metadata value must have metadata type");
2857 V = ID.MDStringVal;
2858 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002859 case ValID::t_GlobalName:
2860 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002861 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002862 case ValID::t_GlobalID:
2863 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002864 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002865 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002866 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002867 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002868 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002869 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002870 return false;
2871 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002872 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002873 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2874 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002875
Dan Gohman518cda42011-12-17 00:04:22 +00002876 // The lexer has no type info, so builds all half, float, and double FP
2877 // constants as double. Fix this here. Long double does not need this.
2878 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002879 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002880 if (Ty->isHalfTy())
2881 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2882 &Ignored);
2883 else if (Ty->isFloatTy())
2884 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2885 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002886 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002887 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002888
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002889 if (V->getType() != Ty)
2890 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002891 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002892
Chris Lattnerac161bf2009-01-02 07:01:27 +00002893 return false;
2894 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002895 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002896 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002897 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002898 return false;
2899 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002900 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002901 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002902 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002903 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002904 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002905 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002906 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002907 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002908 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002909 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002910 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002911 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002912 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002913 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002914 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002915 return false;
2916 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002917 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002918 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002919
Chris Lattnerac161bf2009-01-02 07:01:27 +00002920 V = ID.ConstantVal;
2921 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002922 case ValID::t_ConstantStruct:
2923 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002924 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002925 if (ST->getNumElements() != ID.UIntVal)
2926 return Error(ID.Loc,
2927 "initializer with struct type has wrong # elements");
2928 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2929 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002930
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002931 // Verify that the elements are compatible with the structtype.
2932 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2933 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2934 return Error(ID.Loc, "element " + Twine(i) +
2935 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002936
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002937 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2938 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002939 } else
2940 return Error(ID.Loc, "constant expression type mismatch");
2941 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002942 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002943 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002944}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002945
Chris Lattner229907c2011-07-18 04:54:35 +00002946bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002947 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002948 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002949 return ParseValID(ID, PFS) ||
2950 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002951}
2952
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002953bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002954 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002955 return ParseType(Ty) ||
2956 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002957}
2958
Chris Lattner3ed871f2009-10-27 19:13:16 +00002959bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2960 PerFunctionState &PFS) {
2961 Value *V;
2962 Loc = Lex.getLoc();
2963 if (ParseTypeAndValue(V, PFS)) return true;
2964 if (!isa<BasicBlock>(V))
2965 return Error(Loc, "expected a basic block");
2966 BB = cast<BasicBlock>(V);
2967 return false;
2968}
2969
2970
Chris Lattnerac161bf2009-01-02 07:01:27 +00002971/// FunctionHeader
2972/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00002973/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00002974/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00002975bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2976 // Parse the linkage.
2977 LocTy LinkageLoc = Lex.getLoc();
2978 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002979
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00002980 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00002981 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00002982 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00002983 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00002984 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002985 LocTy RetTypeLoc = Lex.getLoc();
2986 if (ParseOptionalLinkage(Linkage) ||
2987 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00002988 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002989 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00002990 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00002991 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002992 return true;
2993
2994 // Verify that the linkage is ok.
2995 switch ((GlobalValue::LinkageTypes)Linkage) {
2996 case GlobalValue::ExternalLinkage:
2997 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00002998 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002999 if (isDefine)
3000 return Error(LinkageLoc, "invalid linkage for function definition");
3001 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003002 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003003 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003004 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003005 case GlobalValue::LinkOnceAnyLinkage:
3006 case GlobalValue::LinkOnceODRLinkage:
3007 case GlobalValue::WeakAnyLinkage:
3008 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003009 if (!isDefine)
3010 return Error(LinkageLoc, "invalid linkage for function declaration");
3011 break;
3012 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003013 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003014 return Error(LinkageLoc, "invalid function linkage type");
3015 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003016
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003017 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003018 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003019
Chris Lattnerac161bf2009-01-02 07:01:27 +00003020 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003021
3022 std::string FunctionName;
3023 if (Lex.getKind() == lltok::GlobalVar) {
3024 FunctionName = Lex.getStrVal();
3025 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3026 unsigned NameID = Lex.getUIntVal();
3027
3028 if (NameID != NumberedVals.size())
3029 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003030 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003031 } else {
3032 return TokError("expected function name");
3033 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003034
Chris Lattner3822f632009-01-02 08:05:26 +00003035 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003036
Chris Lattner3822f632009-01-02 08:05:26 +00003037 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003038 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003039
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003040 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003041 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003042 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003043 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003044 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003045 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003046 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003047 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003048 bool UnnamedAddr;
3049 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003050 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003051
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003052 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003053 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3054 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003055 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003056 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003057 (EatIfPresent(lltok::kw_section) &&
3058 ParseStringConstant(Section)) ||
3059 ParseOptionalAlignment(Alignment) ||
3060 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003061 ParseStringConstant(GC)) ||
3062 (EatIfPresent(lltok::kw_prefix) &&
3063 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003064 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003065
Michael Gottesman41748d72013-06-27 00:25:01 +00003066 if (FuncAttrs.contains(Attribute::Builtin))
3067 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003068
Chris Lattnerac161bf2009-01-02 07:01:27 +00003069 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003070 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003071 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003072 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003073 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003074
Chris Lattnerac161bf2009-01-02 07:01:27 +00003075 // Okay, if we got here, the function is syntactically valid. Convert types
3076 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003077 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003078 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003079
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003080 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003081 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3082 AttributeSet::ReturnIndex,
3083 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003084
Chris Lattnerac161bf2009-01-02 07:01:27 +00003085 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003086 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003087 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3088 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003089 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3090 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003091 }
3092
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003093 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003094 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3095 AttributeSet::FunctionIndex,
3096 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003097
Bill Wendlinge94d8432012-12-07 23:16:57 +00003098 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003099
Bill Wendling749a43d2012-12-30 13:50:49 +00003100 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003101 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3102
Chris Lattner229907c2011-07-18 04:54:35 +00003103 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003104 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003105 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003106
Craig Topper2617dcc2014-04-15 06:32:26 +00003107 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003108 if (!FunctionName.empty()) {
3109 // If this was a definition of a forward reference, remove the definition
3110 // from the forward reference table and fill in the forward ref.
3111 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3112 ForwardRefVals.find(FunctionName);
3113 if (FRVI != ForwardRefVals.end()) {
3114 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003115 if (!Fn)
3116 return Error(FRVI->second.second, "invalid forward reference to "
3117 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003118 if (Fn->getType() != PFT)
3119 return Error(FRVI->second.second, "invalid forward reference to "
3120 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003121
Chris Lattnerac161bf2009-01-02 07:01:27 +00003122 ForwardRefVals.erase(FRVI);
3123 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003124 // Reject redefinitions.
3125 return Error(NameLoc, "invalid redefinition of function '" +
3126 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003127 } else if (M->getNamedValue(FunctionName)) {
3128 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003129 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003130
Dan Gohman399d6ae2009-08-29 23:37:49 +00003131 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003132 // If this is a definition of a forward referenced function, make sure the
3133 // types agree.
3134 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3135 = ForwardRefValIDs.find(NumberedVals.size());
3136 if (I != ForwardRefValIDs.end()) {
3137 Fn = cast<Function>(I->second.first);
3138 if (Fn->getType() != PFT)
3139 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003140 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003141 ForwardRefValIDs.erase(I);
3142 }
3143 }
3144
Craig Topper2617dcc2014-04-15 06:32:26 +00003145 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003146 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3147 else // Move the forward-reference to the correct spot in the module.
3148 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3149
3150 if (FunctionName.empty())
3151 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003152
Chris Lattnerac161bf2009-01-02 07:01:27 +00003153 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3154 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003155 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003156 Fn->setCallingConv(CC);
3157 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003158 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003159 Fn->setAlignment(Alignment);
3160 Fn->setSection(Section);
3161 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003162 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003163 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003164
Chris Lattnerac161bf2009-01-02 07:01:27 +00003165 // Add all of the arguments we parsed to the function.
3166 Function::arg_iterator ArgIt = Fn->arg_begin();
3167 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3168 // If the argument has a name, insert it into the argument symbol table.
3169 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003170
Chris Lattnerac161bf2009-01-02 07:01:27 +00003171 // Set the name, if it conflicted, it will be auto-renamed.
3172 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003173
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003174 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003175 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3176 ArgList[i].Name + "'");
3177 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003178
Chris Lattnerac161bf2009-01-02 07:01:27 +00003179 return false;
3180}
3181
3182
3183/// ParseFunctionBody
3184/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003185///
3186bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003187 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003188 return TokError("expected '{' in function body");
3189 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003190
Chris Lattner3432c622009-10-28 03:39:23 +00003191 int FunctionNumber = -1;
3192 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003193
Chris Lattner3432c622009-10-28 03:39:23 +00003194 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003195
Chris Lattnerbbddd962010-01-09 19:20:07 +00003196 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003197 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003198 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003199
Chris Lattner4649a732011-06-17 06:42:57 +00003200 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003201 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003202
Chris Lattnerac161bf2009-01-02 07:01:27 +00003203 // Eat the }.
3204 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003205
Chris Lattnerac161bf2009-01-02 07:01:27 +00003206 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003207 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003208}
3209
3210/// ParseBasicBlock
3211/// ::= LabelStr? Instruction*
3212bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3213 // If this basic block starts out with a name, remember it.
3214 std::string Name;
3215 LocTy NameLoc = Lex.getLoc();
3216 if (Lex.getKind() == lltok::LabelStr) {
3217 Name = Lex.getStrVal();
3218 Lex.Lex();
3219 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003220
Chris Lattnerac161bf2009-01-02 07:01:27 +00003221 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003222 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003223
Chris Lattnerac161bf2009-01-02 07:01:27 +00003224 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003225
Chris Lattnerac161bf2009-01-02 07:01:27 +00003226 // Parse the instructions in this block until we get a terminator.
3227 Instruction *Inst;
3228 do {
3229 // This instruction may have three possibilities for a name: a) none
3230 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3231 LocTy NameLoc = Lex.getLoc();
3232 int NameID = -1;
3233 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003234
Chris Lattnerac161bf2009-01-02 07:01:27 +00003235 if (Lex.getKind() == lltok::LocalVarID) {
3236 NameID = Lex.getUIntVal();
3237 Lex.Lex();
3238 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3239 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003240 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003241 NameStr = Lex.getStrVal();
3242 Lex.Lex();
3243 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3244 return true;
3245 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003246
Chris Lattner77b89dc2009-12-30 05:23:43 +00003247 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003248 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003249 case InstError: return true;
3250 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003251 BB->getInstList().push_back(Inst);
3252
Chris Lattner77b89dc2009-12-30 05:23:43 +00003253 // With a normal result, we check to see if the instruction is followed by
3254 // a comma and metadata.
3255 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003256 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003257 return true;
3258 break;
3259 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003260 BB->getInstList().push_back(Inst);
3261
Chris Lattner77b89dc2009-12-30 05:23:43 +00003262 // If the instruction parser ate an extra comma at the end of it, it
3263 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003264 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003265 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003266 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003267 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003268
Chris Lattnerac161bf2009-01-02 07:01:27 +00003269 // Set the name on the instruction.
3270 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3271 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003272
Chris Lattnerac161bf2009-01-02 07:01:27 +00003273 return false;
3274}
3275
3276//===----------------------------------------------------------------------===//
3277// Instruction Parsing.
3278//===----------------------------------------------------------------------===//
3279
3280/// ParseInstruction - Parse one of the many different instructions.
3281///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003282int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3283 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003284 lltok::Kind Token = Lex.getKind();
3285 if (Token == lltok::Eof)
3286 return TokError("found end of file when expecting more instructions");
3287 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003288 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003289 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003290
Chris Lattnerac161bf2009-01-02 07:01:27 +00003291 switch (Token) {
3292 default: return Error(Loc, "expected instruction opcode");
3293 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003294 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003295 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3296 case lltok::kw_br: return ParseBr(Inst, PFS);
3297 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003298 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003299 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003300 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003301 // Binary Operators.
3302 case lltok::kw_add:
3303 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003304 case lltok::kw_mul:
3305 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003306 bool NUW = EatIfPresent(lltok::kw_nuw);
3307 bool NSW = EatIfPresent(lltok::kw_nsw);
3308 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003309
Chris Lattnera676c0f2011-02-07 16:40:21 +00003310 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003311
Chris Lattnera676c0f2011-02-07 16:40:21 +00003312 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3313 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3314 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003315 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003316 case lltok::kw_fadd:
3317 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003318 case lltok::kw_fmul:
3319 case lltok::kw_fdiv:
3320 case lltok::kw_frem: {
3321 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3322 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3323 if (Res != 0)
3324 return Res;
3325 if (FMF.any())
3326 Inst->setFastMathFlags(FMF);
3327 return 0;
3328 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003329
Chris Lattner35315d02011-02-06 21:44:57 +00003330 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003331 case lltok::kw_udiv:
3332 case lltok::kw_lshr:
3333 case lltok::kw_ashr: {
3334 bool Exact = EatIfPresent(lltok::kw_exact);
3335
3336 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3337 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3338 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003339 }
3340
Chris Lattnerac161bf2009-01-02 07:01:27 +00003341 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003342 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003343 case lltok::kw_and:
3344 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003345 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003346 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003347 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003348 // Casts.
3349 case lltok::kw_trunc:
3350 case lltok::kw_zext:
3351 case lltok::kw_sext:
3352 case lltok::kw_fptrunc:
3353 case lltok::kw_fpext:
3354 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003355 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003356 case lltok::kw_uitofp:
3357 case lltok::kw_sitofp:
3358 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003359 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003360 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003361 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003362 // Other.
3363 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003364 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003365 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3366 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3367 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3368 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003369 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003370 // Call.
3371 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3372 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3373 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003374 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003375 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003376 case lltok::kw_load: return ParseLoad(Inst, PFS);
3377 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003378 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3379 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003380 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003381 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3382 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3383 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3384 }
3385}
3386
3387/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3388bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003389 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003390 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003391 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003392 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3393 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3394 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3395 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3396 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3397 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3398 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3399 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3400 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3401 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3402 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3403 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3404 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3405 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3406 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3407 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3408 }
3409 } else {
3410 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003411 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003412 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3413 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3414 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3415 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3416 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3417 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3418 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3419 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3420 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3421 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3422 }
3423 }
3424 Lex.Lex();
3425 return false;
3426}
3427
3428//===----------------------------------------------------------------------===//
3429// Terminator Instructions.
3430//===----------------------------------------------------------------------===//
3431
3432/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003433/// ::= 'ret' void (',' !dbg, !1)*
3434/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003435bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003436 PerFunctionState &PFS) {
3437 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003438 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003439 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003440
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003441 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003442
Chris Lattnerfdd87902009-10-05 05:54:46 +00003443 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003444 if (!ResType->isVoidTy())
3445 return Error(TypeLoc, "value doesn't match function result type '" +
3446 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003447
Owen Anderson55f1c092009-08-13 21:58:54 +00003448 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003449 return false;
3450 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003451
Chris Lattnerac161bf2009-01-02 07:01:27 +00003452 Value *RV;
3453 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003454
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003455 if (ResType != RV->getType())
3456 return Error(TypeLoc, "value doesn't match function result type '" +
3457 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003458
Owen Anderson55f1c092009-08-13 21:58:54 +00003459 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003460 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003461}
3462
3463
3464/// ParseBr
3465/// ::= 'br' TypeAndValue
3466/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3467bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3468 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003469 Value *Op0;
3470 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003471 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003472
Chris Lattnerac161bf2009-01-02 07:01:27 +00003473 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3474 Inst = BranchInst::Create(BB);
3475 return false;
3476 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003477
Owen Anderson55f1c092009-08-13 21:58:54 +00003478 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003479 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003480
Chris Lattnerac161bf2009-01-02 07:01:27 +00003481 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003482 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003483 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003484 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003485 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003486
Chris Lattner3ed871f2009-10-27 19:13:16 +00003487 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003488 return false;
3489}
3490
3491/// ParseSwitch
3492/// Instruction
3493/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3494/// JumpTable
3495/// ::= (TypeAndValue ',' TypeAndValue)*
3496bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3497 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003498 Value *Cond;
3499 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003500 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3501 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003502 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003503 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3504 return true;
3505
Duncan Sands19d0b472010-02-16 11:11:14 +00003506 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003507 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003508
Chris Lattnerac161bf2009-01-02 07:01:27 +00003509 // Parse the jump table pairs.
3510 SmallPtrSet<Value*, 32> SeenCases;
3511 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3512 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003513 Value *Constant;
3514 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003515
Chris Lattnerac161bf2009-01-02 07:01:27 +00003516 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3517 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003518 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003519 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003520
Chris Lattnerac161bf2009-01-02 07:01:27 +00003521 if (!SeenCases.insert(Constant))
3522 return Error(CondLoc, "duplicate case value in switch");
3523 if (!isa<ConstantInt>(Constant))
3524 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003525
Chris Lattner3ed871f2009-10-27 19:13:16 +00003526 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003527 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003528
Chris Lattnerac161bf2009-01-02 07:01:27 +00003529 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003530
Chris Lattner3ed871f2009-10-27 19:13:16 +00003531 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003532 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3533 SI->addCase(Table[i].first, Table[i].second);
3534 Inst = SI;
3535 return false;
3536}
3537
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003538/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003539/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003540/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3541bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003542 LocTy AddrLoc;
3543 Value *Address;
3544 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003545 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3546 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003547 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003548
Duncan Sands19d0b472010-02-16 11:11:14 +00003549 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003550 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003551
Chris Lattner3ed871f2009-10-27 19:13:16 +00003552 // Parse the destination list.
3553 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003554
Chris Lattner3ed871f2009-10-27 19:13:16 +00003555 if (Lex.getKind() != lltok::rsquare) {
3556 BasicBlock *DestBB;
3557 if (ParseTypeAndBasicBlock(DestBB, PFS))
3558 return true;
3559 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003560
Chris Lattner3ed871f2009-10-27 19:13:16 +00003561 while (EatIfPresent(lltok::comma)) {
3562 if (ParseTypeAndBasicBlock(DestBB, PFS))
3563 return true;
3564 DestList.push_back(DestBB);
3565 }
3566 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003567
Chris Lattner3ed871f2009-10-27 19:13:16 +00003568 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3569 return true;
3570
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003571 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003572 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3573 IBI->addDestination(DestList[i]);
3574 Inst = IBI;
3575 return false;
3576}
3577
3578
Chris Lattnerac161bf2009-01-02 07:01:27 +00003579/// ParseInvoke
3580/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3581/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3582bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3583 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003584 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003585 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003586 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003587 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003588 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003589 LocTy RetTypeLoc;
3590 ValID CalleeID;
3591 SmallVector<ParamInfo, 16> ArgList;
3592
Chris Lattner3ed871f2009-10-27 19:13:16 +00003593 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003594 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003595 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003596 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003597 ParseValID(CalleeID) ||
3598 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003599 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3600 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003601 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003602 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003603 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003604 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003605 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003606
Chris Lattnerac161bf2009-01-02 07:01:27 +00003607 // If RetType is a non-function pointer type, then this is the short syntax
3608 // for the call, which means that RetType is just the return type. Infer the
3609 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003610 PointerType *PFTy = nullptr;
3611 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003612 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3613 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3614 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003615 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003616 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3617 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003618
Chris Lattnerac161bf2009-01-02 07:01:27 +00003619 if (!FunctionType::isValidReturnType(RetType))
3620 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003621
Owen Anderson4056ca92009-07-29 22:17:13 +00003622 Ty = FunctionType::get(RetType, ParamTypes, false);
3623 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003624 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003625
Chris Lattnerac161bf2009-01-02 07:01:27 +00003626 // Look up the callee.
3627 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003628 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003629
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003630 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003631 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003632 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003633 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3634 AttributeSet::ReturnIndex,
3635 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003636
Chris Lattnerac161bf2009-01-02 07:01:27 +00003637 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003638
Chris Lattnerac161bf2009-01-02 07:01:27 +00003639 // Loop through FunctionType's arguments and ensure they are specified
3640 // correctly. Also, gather any parameter attributes.
3641 FunctionType::param_iterator I = Ty->param_begin();
3642 FunctionType::param_iterator E = Ty->param_end();
3643 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003644 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003645 if (I != E) {
3646 ExpectedTy = *I++;
3647 } else if (!Ty->isVarArg()) {
3648 return Error(ArgList[i].Loc, "too many arguments specified");
3649 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003650
Chris Lattnerac161bf2009-01-02 07:01:27 +00003651 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3652 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003653 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003654 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003655 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3656 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003657 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3658 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003659 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003660
Chris Lattnerac161bf2009-01-02 07:01:27 +00003661 if (I != E)
3662 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003663
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003664 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003665 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3666 AttributeSet::FunctionIndex,
3667 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003668
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003669 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003670 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003671
Jay Foad5bd375a2011-07-15 08:37:34 +00003672 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003673 II->setCallingConv(CC);
3674 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003675 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003676 Inst = II;
3677 return false;
3678}
3679
Bill Wendlingf891bf82011-07-31 06:30:59 +00003680/// ParseResume
3681/// ::= 'resume' TypeAndValue
3682bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3683 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003684 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3685 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003686
Bill Wendlingf891bf82011-07-31 06:30:59 +00003687 ResumeInst *RI = ResumeInst::Create(Exn);
3688 Inst = RI;
3689 return false;
3690}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003691
3692//===----------------------------------------------------------------------===//
3693// Binary Operators.
3694//===----------------------------------------------------------------------===//
3695
3696/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003697/// ::= ArithmeticOps TypeAndValue ',' Value
3698///
3699/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3700/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003701bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003702 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003703 LocTy Loc; Value *LHS, *RHS;
3704 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3705 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3706 ParseValue(LHS->getType(), RHS, PFS))
3707 return true;
3708
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003709 bool Valid;
3710 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003711 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003712 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003713 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3714 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003715 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003716 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3717 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003718 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003719
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003720 if (!Valid)
3721 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003722
Chris Lattnerac161bf2009-01-02 07:01:27 +00003723 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3724 return false;
3725}
3726
3727/// ParseLogical
3728/// ::= ArithmeticOps TypeAndValue ',' Value {
3729bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3730 unsigned Opc) {
3731 LocTy Loc; Value *LHS, *RHS;
3732 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3733 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3734 ParseValue(LHS->getType(), RHS, PFS))
3735 return true;
3736
Duncan Sands9dff9be2010-02-15 16:12:20 +00003737 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003738 return Error(Loc,"instruction requires integer or integer vector operands");
3739
3740 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3741 return false;
3742}
3743
3744
3745/// ParseCompare
3746/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3747/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003748bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3749 unsigned Opc) {
3750 // Parse the integer/fp comparison predicate.
3751 LocTy Loc;
3752 unsigned Pred;
3753 Value *LHS, *RHS;
3754 if (ParseCmpPredicate(Pred, Opc) ||
3755 ParseTypeAndValue(LHS, Loc, PFS) ||
3756 ParseToken(lltok::comma, "expected ',' after compare value") ||
3757 ParseValue(LHS->getType(), RHS, PFS))
3758 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003759
Chris Lattnerac161bf2009-01-02 07:01:27 +00003760 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003761 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003762 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003763 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003764 } else {
3765 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003766 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003767 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003768 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003769 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003770 }
3771 return false;
3772}
3773
3774//===----------------------------------------------------------------------===//
3775// Other Instructions.
3776//===----------------------------------------------------------------------===//
3777
3778
3779/// ParseCast
3780/// ::= CastOpc TypeAndValue 'to' Type
3781bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3782 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003783 LocTy Loc;
3784 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003785 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003786 if (ParseTypeAndValue(Op, Loc, PFS) ||
3787 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3788 ParseType(DestTy))
3789 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003790
Chris Lattner89d856e2009-03-01 00:53:13 +00003791 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3792 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003793 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003794 getTypeString(Op->getType()) + "' to '" +
3795 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003796 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003797 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3798 return false;
3799}
3800
3801/// ParseSelect
3802/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3803bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3804 LocTy Loc;
3805 Value *Op0, *Op1, *Op2;
3806 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3807 ParseToken(lltok::comma, "expected ',' after select condition") ||
3808 ParseTypeAndValue(Op1, PFS) ||
3809 ParseToken(lltok::comma, "expected ',' after select value") ||
3810 ParseTypeAndValue(Op2, PFS))
3811 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003812
Chris Lattnerac161bf2009-01-02 07:01:27 +00003813 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3814 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003815
Chris Lattnerac161bf2009-01-02 07:01:27 +00003816 Inst = SelectInst::Create(Op0, Op1, Op2);
3817 return false;
3818}
3819
Chris Lattnerb55ab542009-01-05 08:18:44 +00003820/// ParseVA_Arg
3821/// ::= 'va_arg' TypeAndValue ',' Type
3822bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003823 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003824 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003825 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003826 if (ParseTypeAndValue(Op, PFS) ||
3827 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003828 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003829 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003830
Chris Lattnerb55ab542009-01-05 08:18:44 +00003831 if (!EltTy->isFirstClassType())
3832 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003833
3834 Inst = new VAArgInst(Op, EltTy);
3835 return false;
3836}
3837
3838/// ParseExtractElement
3839/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3840bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3841 LocTy Loc;
3842 Value *Op0, *Op1;
3843 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3844 ParseToken(lltok::comma, "expected ',' after extract value") ||
3845 ParseTypeAndValue(Op1, PFS))
3846 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003847
Chris Lattnerac161bf2009-01-02 07:01:27 +00003848 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3849 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003850
Eric Christopherc9742252009-07-25 02:28:41 +00003851 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003852 return false;
3853}
3854
3855/// ParseInsertElement
3856/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3857bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3858 LocTy Loc;
3859 Value *Op0, *Op1, *Op2;
3860 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3861 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3862 ParseTypeAndValue(Op1, PFS) ||
3863 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3864 ParseTypeAndValue(Op2, PFS))
3865 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003866
Chris Lattnerac161bf2009-01-02 07:01:27 +00003867 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003868 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003869
Chris Lattnerac161bf2009-01-02 07:01:27 +00003870 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3871 return false;
3872}
3873
3874/// ParseShuffleVector
3875/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3876bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3877 LocTy Loc;
3878 Value *Op0, *Op1, *Op2;
3879 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3880 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3881 ParseTypeAndValue(Op1, PFS) ||
3882 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3883 ParseTypeAndValue(Op2, PFS))
3884 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003885
Chris Lattnerac161bf2009-01-02 07:01:27 +00003886 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003887 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003888
Chris Lattnerac161bf2009-01-02 07:01:27 +00003889 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3890 return false;
3891}
3892
3893/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003894/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003895int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003896 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003897 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003898
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003899 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003900 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3901 ParseValue(Ty, Op0, PFS) ||
3902 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003903 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003904 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3905 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003906
Chris Lattnerf4f03422009-12-30 05:27:33 +00003907 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003908 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3909 while (1) {
3910 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003911
Chris Lattner3822f632009-01-02 08:05:26 +00003912 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003913 break;
3914
Chris Lattnerf4f03422009-12-30 05:27:33 +00003915 if (Lex.getKind() == lltok::MetadataVar) {
3916 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003917 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003918 }
Devang Patel8f842d32009-10-16 18:45:49 +00003919
Chris Lattner3822f632009-01-02 08:05:26 +00003920 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003921 ParseValue(Ty, Op0, PFS) ||
3922 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003923 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003924 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3925 return true;
3926 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003927
Chris Lattnerac161bf2009-01-02 07:01:27 +00003928 if (!Ty->isFirstClassType())
3929 return Error(TypeLoc, "phi node must have first class type");
3930
Jay Foad52131342011-03-30 11:28:46 +00003931 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003932 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3933 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3934 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003935 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003936}
3937
Bill Wendlingfae14752011-08-12 20:24:12 +00003938/// ParseLandingPad
3939/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3940/// Clause
3941/// ::= 'catch' TypeAndValue
3942/// ::= 'filter'
3943/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3944bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003945 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003946 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003947
3948 if (ParseType(Ty, TyLoc) ||
3949 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3950 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3951 return true;
3952
3953 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3954 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3955
3956 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3957 LandingPadInst::ClauseType CT;
3958 if (EatIfPresent(lltok::kw_catch))
3959 CT = LandingPadInst::Catch;
3960 else if (EatIfPresent(lltok::kw_filter))
3961 CT = LandingPadInst::Filter;
3962 else
3963 return TokError("expected 'catch' or 'filter' clause type");
3964
3965 Value *V; LocTy VLoc;
3966 if (ParseTypeAndValue(V, VLoc, PFS)) {
3967 delete LP;
3968 return true;
3969 }
3970
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00003971 // A 'catch' type expects a non-array constant. A filter clause expects an
3972 // array constant.
3973 if (CT == LandingPadInst::Catch) {
3974 if (isa<ArrayType>(V->getType()))
3975 Error(VLoc, "'catch' clause has an invalid type");
3976 } else {
3977 if (!isa<ArrayType>(V->getType()))
3978 Error(VLoc, "'filter' clause has an invalid type");
3979 }
3980
Bill Wendlingfae14752011-08-12 20:24:12 +00003981 LP->addClause(V);
3982 }
3983
3984 Inst = LP;
3985 return false;
3986}
3987
Chris Lattnerac161bf2009-01-02 07:01:27 +00003988/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00003989/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
3990/// ParameterList OptionalAttrs
3991/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
3992/// ParameterList OptionalAttrs
3993/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003994/// ParameterList OptionalAttrs
3995bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00003996 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00003997 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003998 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003999 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004000 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004001 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004002 LocTy RetTypeLoc;
4003 ValID CalleeID;
4004 SmallVector<ParamInfo, 16> ArgList;
4005 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004006
Reid Kleckner5772b772014-04-24 20:14:34 +00004007 if ((TCK != CallInst::TCK_None &&
4008 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004009 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004010 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004011 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004012 ParseValID(CalleeID) ||
4013 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004014 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004015 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004016 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004017
Chris Lattnerac161bf2009-01-02 07:01:27 +00004018 // If RetType is a non-function pointer type, then this is the short syntax
4019 // for the call, which means that RetType is just the return type. Infer the
4020 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004021 PointerType *PFTy = nullptr;
4022 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004023 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4024 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4025 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004026 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004027 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4028 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004029
Chris Lattnerac161bf2009-01-02 07:01:27 +00004030 if (!FunctionType::isValidReturnType(RetType))
4031 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004032
Owen Anderson4056ca92009-07-29 22:17:13 +00004033 Ty = FunctionType::get(RetType, ParamTypes, false);
4034 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004035 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004036
Chris Lattnerac161bf2009-01-02 07:01:27 +00004037 // Look up the callee.
4038 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004039 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004040
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004041 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004042 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004043 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004044 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4045 AttributeSet::ReturnIndex,
4046 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004047
Chris Lattnerac161bf2009-01-02 07:01:27 +00004048 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004049
Chris Lattnerac161bf2009-01-02 07:01:27 +00004050 // Loop through FunctionType's arguments and ensure they are specified
4051 // correctly. Also, gather any parameter attributes.
4052 FunctionType::param_iterator I = Ty->param_begin();
4053 FunctionType::param_iterator E = Ty->param_end();
4054 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004055 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004056 if (I != E) {
4057 ExpectedTy = *I++;
4058 } else if (!Ty->isVarArg()) {
4059 return Error(ArgList[i].Loc, "too many arguments specified");
4060 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004061
Chris Lattnerac161bf2009-01-02 07:01:27 +00004062 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4063 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004064 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004065 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004066 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4067 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004068 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4069 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004070 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004071
Chris Lattnerac161bf2009-01-02 07:01:27 +00004072 if (I != E)
4073 return Error(CallLoc, "not enough parameters specified for call");
4074
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004075 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004076 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4077 AttributeSet::FunctionIndex,
4078 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004079
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004080 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004081 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004082
Jay Foad5bd375a2011-07-15 08:37:34 +00004083 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004084 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004085 CI->setCallingConv(CC);
4086 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004087 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004088 Inst = CI;
4089 return false;
4090}
4091
4092//===----------------------------------------------------------------------===//
4093// Memory Instructions.
4094//===----------------------------------------------------------------------===//
4095
4096/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004097/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004098int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004099 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004100 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004101 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004102 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004103
4104 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4105
Chris Lattner3822f632009-01-02 08:05:26 +00004106 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004107
Chris Lattnerb2f39502009-12-30 05:44:30 +00004108 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004109 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004110 if (Lex.getKind() == lltok::kw_align) {
4111 if (ParseOptionalAlignment(Alignment)) return true;
4112 } else if (Lex.getKind() == lltok::MetadataVar) {
4113 AteExtraComma = true;
4114 } else {
4115 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4116 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4117 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004118 }
4119 }
4120
Dan Gohman2140a742010-05-28 01:14:11 +00004121 if (Size && !Size->getType()->isIntegerTy())
4122 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004123
Reid Kleckner436c42e2014-01-17 23:58:17 +00004124 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4125 AI->setUsedWithInAlloca(IsInAlloca);
4126 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004127 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004128}
4129
4130/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004131/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004132/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004133/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004134int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004135 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004136 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004137 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004138 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004139 AtomicOrdering Ordering = NotAtomic;
4140 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004141
4142 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004143 isAtomic = true;
4144 Lex.Lex();
4145 }
4146
Chris Lattnerbc639292011-11-27 06:56:53 +00004147 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004148 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004149 isVolatile = true;
4150 Lex.Lex();
4151 }
4152
Chris Lattnerb2f39502009-12-30 05:44:30 +00004153 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004154 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004155 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4156 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004157
Duncan Sands19d0b472010-02-16 11:11:14 +00004158 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004159 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4160 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004161 if (isAtomic && !Alignment)
4162 return Error(Loc, "atomic load must have explicit non-zero alignment");
4163 if (Ordering == Release || Ordering == AcquireRelease)
4164 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004165
Eli Friedman59b66882011-08-09 23:02:53 +00004166 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004167 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004168}
4169
4170/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004171
4172/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4173/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004174/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004175int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004176 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004177 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004178 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004179 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004180 AtomicOrdering Ordering = NotAtomic;
4181 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004182
4183 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004184 isAtomic = true;
4185 Lex.Lex();
4186 }
4187
Chris Lattnerbc639292011-11-27 06:56:53 +00004188 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004189 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004190 isVolatile = true;
4191 Lex.Lex();
4192 }
4193
Chris Lattnerac161bf2009-01-02 07:01:27 +00004194 if (ParseTypeAndValue(Val, Loc, PFS) ||
4195 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004196 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004197 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004198 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004199 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004200
Duncan Sands19d0b472010-02-16 11:11:14 +00004201 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004202 return Error(PtrLoc, "store operand must be a pointer");
4203 if (!Val->getType()->isFirstClassType())
4204 return Error(Loc, "store operand must be a first class value");
4205 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4206 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004207 if (isAtomic && !Alignment)
4208 return Error(Loc, "atomic store must have explicit non-zero alignment");
4209 if (Ordering == Acquire || Ordering == AcquireRelease)
4210 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004211
Eli Friedman59b66882011-08-09 23:02:53 +00004212 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004213 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004214}
4215
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004216/// ParseCmpXchg
Eli Friedman02e737b2011-08-12 22:50:01 +00004217/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
Tim Northovere94a5182014-03-11 10:48:52 +00004218/// 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004219int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004220 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4221 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004222 AtomicOrdering SuccessOrdering = NotAtomic;
4223 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004224 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004225 bool isVolatile = false;
4226
4227 if (EatIfPresent(lltok::kw_volatile))
4228 isVolatile = true;
4229
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004230 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4231 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4232 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4233 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4234 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004235 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4236 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004237 return true;
4238
Tim Northovere94a5182014-03-11 10:48:52 +00004239 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004240 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004241 if (SuccessOrdering < FailureOrdering)
4242 return TokError("cmpxchg must be at least as ordered on success as failure");
4243 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4244 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004245 if (!Ptr->getType()->isPointerTy())
4246 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4247 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4248 return Error(CmpLoc, "compare value and pointer type do not match");
4249 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4250 return Error(NewLoc, "new value and pointer type do not match");
4251 if (!New->getType()->isIntegerTy())
4252 return Error(NewLoc, "cmpxchg operand must be an integer");
4253 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4254 if (Size < 8 || (Size & (Size - 1)))
4255 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4256 " integer");
4257
Tim Northovere94a5182014-03-11 10:48:52 +00004258 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
4259 FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004260 CXI->setVolatile(isVolatile);
4261 Inst = CXI;
4262 return AteExtraComma ? InstExtraComma : InstNormal;
4263}
4264
4265/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004266/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4267/// 'singlethread'? AtomicOrdering
4268int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004269 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4270 bool AteExtraComma = false;
4271 AtomicOrdering Ordering = NotAtomic;
4272 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004273 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004274 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004275
4276 if (EatIfPresent(lltok::kw_volatile))
4277 isVolatile = true;
4278
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004279 switch (Lex.getKind()) {
4280 default: return TokError("expected binary operation in atomicrmw");
4281 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4282 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4283 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4284 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4285 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4286 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4287 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4288 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4289 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4290 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4291 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4292 }
4293 Lex.Lex(); // Eat the operation.
4294
4295 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4296 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4297 ParseTypeAndValue(Val, ValLoc, PFS) ||
4298 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4299 return true;
4300
4301 if (Ordering == Unordered)
4302 return TokError("atomicrmw cannot be unordered");
4303 if (!Ptr->getType()->isPointerTy())
4304 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4305 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4306 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4307 if (!Val->getType()->isIntegerTy())
4308 return Error(ValLoc, "atomicrmw operand must be an integer");
4309 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4310 if (Size < 8 || (Size & (Size - 1)))
4311 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4312 " integer");
4313
4314 AtomicRMWInst *RMWI =
4315 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4316 RMWI->setVolatile(isVolatile);
4317 Inst = RMWI;
4318 return AteExtraComma ? InstExtraComma : InstNormal;
4319}
4320
Eli Friedmanfee02c62011-07-25 23:16:38 +00004321/// ParseFence
4322/// ::= 'fence' 'singlethread'? AtomicOrdering
4323int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4324 AtomicOrdering Ordering = NotAtomic;
4325 SynchronizationScope Scope = CrossThread;
4326 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4327 return true;
4328
4329 if (Ordering == Unordered)
4330 return TokError("fence cannot be unordered");
4331 if (Ordering == Monotonic)
4332 return TokError("fence cannot be monotonic");
4333
4334 Inst = new FenceInst(Context, Ordering, Scope);
4335 return InstNormal;
4336}
4337
Chris Lattnerac161bf2009-01-02 07:01:27 +00004338/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004339/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004340int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004341 Value *Ptr = nullptr;
4342 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004343 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004344
Dan Gohman16cbbe42009-07-29 15:58:36 +00004345 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004346
Chris Lattner3822f632009-01-02 08:05:26 +00004347 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004348
Eli Benderskyd9806682013-04-22 17:03:42 +00004349 Type *BaseType = Ptr->getType();
4350 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4351 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004352 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004353
Chris Lattnerac161bf2009-01-02 07:01:27 +00004354 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004355 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004356 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004357 if (Lex.getKind() == lltok::MetadataVar) {
4358 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004359 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004360 }
Chris Lattner3822f632009-01-02 08:05:26 +00004361 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004362 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004363 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004364 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4365 return Error(EltLoc, "getelementptr index type missmatch");
4366 if (Val->getType()->isVectorTy()) {
4367 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4368 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4369 if (ValNumEl != PtrNumEl)
4370 return Error(EltLoc,
4371 "getelementptr vector index has a wrong number of elements");
4372 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004373 Indices.push_back(Val);
4374 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004375
Eli Benderskyd9806682013-04-22 17:03:42 +00004376 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4377 return Error(Loc, "base element of getelementptr must be sized");
4378
4379 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004380 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004381 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004382 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004383 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004384 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004385}
4386
4387/// ParseExtractValue
4388/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004389int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004390 Value *Val; LocTy Loc;
4391 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004392 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004393 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004394 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004395 return true;
4396
Chris Lattner392be582010-02-12 20:49:41 +00004397 if (!Val->getType()->isAggregateType())
4398 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004399
Jay Foad57aa6362011-07-13 10:26:04 +00004400 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004401 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004402 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004403 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004404}
4405
4406/// ParseInsertValue
4407/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004408int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004409 Value *Val0, *Val1; LocTy Loc0, Loc1;
4410 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004411 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004412 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4413 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4414 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004415 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004416 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004417
Chris Lattner392be582010-02-12 20:49:41 +00004418 if (!Val0->getType()->isAggregateType())
4419 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004420
Jay Foad57aa6362011-07-13 10:26:04 +00004421 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004422 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004423 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004424 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004425}
Nick Lewycky49f89192009-04-04 07:22:01 +00004426
4427//===----------------------------------------------------------------------===//
4428// Embedded metadata.
4429//===----------------------------------------------------------------------===//
4430
4431/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004432/// ::= Element (',' Element)*
4433/// Element
4434/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004435bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004436 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004437 // Check for an empty list.
4438 if (Lex.getKind() == lltok::rbrace)
4439 return false;
4440
Nick Lewycky49f89192009-04-04 07:22:01 +00004441 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004442 // Null is a special case since it is typeless.
4443 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004444 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004445 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004446 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004447
Craig Topper2617dcc2014-04-15 06:32:26 +00004448 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004449 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004450 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004451 } while (EatIfPresent(lltok::comma));
4452
4453 return false;
4454}