blob: 3282e8a23ba76d5def453622d8e456aebbf778a5 [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the parser class for .ll files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth91065212014-03-05 10:34:14 +000016#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/CallingConv.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/InlineAsm.h"
21#include "llvm/IR/Instructions.h"
Manman Ren209b17c2013-09-28 00:22:27 +000022#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/IR/ValueSymbolTable.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000027#include "llvm/Support/raw_ostream.h"
28using namespace llvm;
29
Chris Lattner229907c2011-07-18 04:54:35 +000030static std::string getTypeString(Type *T) {
Chris Lattner0f214eb2011-06-18 21:18:23 +000031 std::string Result;
32 raw_string_ostream Tmp(Result);
33 Tmp << *T;
34 return Tmp.str();
35}
36
Chris Lattner3822f632009-01-02 08:05:26 +000037/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000038bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000039 // Prime the lexer.
40 Lex.Lex();
41
Chris Lattnerad6f3352009-01-04 20:44:11 +000042 return ParseTopLevelEntities() ||
43 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000044}
45
46/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
47/// module.
48bool LLParser::ValidateEndOfModule() {
Chris Lattner8eff0152010-04-01 05:14:45 +000049 // Handle any instruction metadata forward references.
50 if (!ForwardRefInstMetadata.empty()) {
51 for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
52 I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
53 I != E; ++I) {
54 Instruction *Inst = I->first;
55 const std::vector<MDRef> &MDList = I->second;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000056
Chris Lattner8eff0152010-04-01 05:14:45 +000057 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
58 unsigned SlotNo = MDList[i].MDSlot;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Craig Topper2617dcc2014-04-15 06:32:26 +000060 if (SlotNo >= NumberedMetadata.size() ||
61 NumberedMetadata[SlotNo] == nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +000062 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +000063 Twine(SlotNo) + "'");
Chris Lattner8eff0152010-04-01 05:14:45 +000064 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
65 }
66 }
67 ForwardRefInstMetadata.clear();
68 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000069
Manman Ren209b17c2013-09-28 00:22:27 +000070 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
71 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
72
Bill Wendlingb32b0412013-02-08 06:32:06 +000073 // Handle any function attribute group forward references.
74 for (std::map<Value*, std::vector<unsigned> >::iterator
75 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
76 I != E; ++I) {
77 Value *V = I->first;
78 std::vector<unsigned> &Vec = I->second;
79 AttrBuilder B;
80
81 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
82 VI != VE; ++VI)
83 B.merge(NumberedAttrBuilders[*VI]);
84
85 if (Function *Fn = dyn_cast<Function>(V)) {
86 AttributeSet AS = Fn->getAttributes();
87 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
88 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
89 AS.getFnAttributes());
90
91 FnAttrs.merge(B);
92
93 // If the alignment was parsed as an attribute, move to the alignment
94 // field.
95 if (FnAttrs.hasAlignmentAttr()) {
96 Fn->setAlignment(FnAttrs.getAlignment());
97 FnAttrs.removeAttribute(Attribute::Alignment);
98 }
99
100 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
101 AttributeSet::get(Context,
102 AttributeSet::FunctionIndex,
103 FnAttrs));
104 Fn->setAttributes(AS);
105 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
106 AttributeSet AS = CI->getAttributes();
107 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
108 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
109 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000110 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000111 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
112 AttributeSet::get(Context,
113 AttributeSet::FunctionIndex,
114 FnAttrs));
115 CI->setAttributes(AS);
116 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
117 AttributeSet AS = II->getAttributes();
118 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
119 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
120 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000121 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000122 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
123 AttributeSet::get(Context,
124 AttributeSet::FunctionIndex,
125 FnAttrs));
126 II->setAttributes(AS);
127 } else {
128 llvm_unreachable("invalid object with forward attribute group reference");
129 }
130 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000131
Chris Lattner3432c622009-10-28 03:39:23 +0000132 // If there are entries in ForwardRefBlockAddresses at this point, they are
133 // references after the function was defined. Resolve those now.
134 while (!ForwardRefBlockAddresses.empty()) {
135 // Okay, we are referencing an already-parsed function, resolve them now.
Craig Topper2617dcc2014-04-15 06:32:26 +0000136 Function *TheFn = nullptr;
Chris Lattner3432c622009-10-28 03:39:23 +0000137 const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
138 if (Fn.Kind == ValID::t_GlobalName)
139 TheFn = M->getFunction(Fn.StrVal);
140 else if (Fn.UIntVal < NumberedVals.size())
141 TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000142
Craig Topper2617dcc2014-04-15 06:32:26 +0000143 if (!TheFn)
Chris Lattner3432c622009-10-28 03:39:23 +0000144 return Error(Fn.Loc, "unknown function referenced by blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000145
Chris Lattner3432c622009-10-28 03:39:23 +0000146 // Resolve all these references.
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000147 if (ResolveForwardRefBlockAddresses(TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000148 ForwardRefBlockAddresses.begin()->second,
Craig Topper2617dcc2014-04-15 06:32:26 +0000149 nullptr))
Chris Lattner3432c622009-10-28 03:39:23 +0000150 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000151
Chris Lattner3432c622009-10-28 03:39:23 +0000152 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
153 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000154
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000155 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)
156 if (NumberedTypes[i].second.isValid())
157 return Error(NumberedTypes[i].second,
158 "use of undefined type '%" + Twine(i) + "'");
159
160 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
161 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
162 if (I->second.second.isValid())
163 return Error(I->second.second,
164 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000165
Chris Lattnerac161bf2009-01-02 07:01:27 +0000166 if (!ForwardRefVals.empty())
167 return Error(ForwardRefVals.begin()->second.second,
168 "use of undefined value '@" + ForwardRefVals.begin()->first +
169 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000170
Chris Lattnerac161bf2009-01-02 07:01:27 +0000171 if (!ForwardRefValIDs.empty())
172 return Error(ForwardRefValIDs.begin()->second.second,
173 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000174 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000175
Devang Pateld2541152009-07-08 19:23:54 +0000176 if (!ForwardRefMDNodes.empty())
177 return Error(ForwardRefMDNodes.begin()->second.second,
178 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000179 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000180
Devang Pateld2541152009-07-08 19:23:54 +0000181
Chris Lattnerac161bf2009-01-02 07:01:27 +0000182 // Look for intrinsic functions and CallInst that need to be upgraded
183 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
184 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000185
Manman Ren8b4306c2013-12-02 21:29:56 +0000186 UpgradeDebugInfo(*M);
187
Chris Lattnerac161bf2009-01-02 07:01:27 +0000188 return false;
189}
190
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000191bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner3432c622009-10-28 03:39:23 +0000192 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
193 PerFunctionState *PFS) {
194 // Loop over all the references, resolving them.
195 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
196 BasicBlock *Res;
Chris Lattneraa99c942009-11-01 01:27:45 +0000197 if (PFS) {
Chris Lattner3432c622009-10-28 03:39:23 +0000198 if (Refs[i].first.Kind == ValID::t_LocalName)
199 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattneraa99c942009-11-01 01:27:45 +0000200 else
Chris Lattner3432c622009-10-28 03:39:23 +0000201 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
202 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
203 return Error(Refs[i].first.Loc,
Chris Lattnera38a4df2009-11-02 18:28:45 +0000204 "cannot take address of numeric label after the function is defined");
Chris Lattner3432c622009-10-28 03:39:23 +0000205 } else {
206 Res = dyn_cast_or_null<BasicBlock>(
207 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
208 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000209
Craig Topper2617dcc2014-04-15 06:32:26 +0000210 if (!Res)
Chris Lattner3432c622009-10-28 03:39:23 +0000211 return Error(Refs[i].first.Loc,
212 "referenced value is not a basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000213
Chris Lattner3432c622009-10-28 03:39:23 +0000214 // Get the BlockAddress for this and update references to use it.
215 BlockAddress *BA = BlockAddress::get(TheFn, Res);
216 Refs[i].second->replaceAllUsesWith(BA);
217 Refs[i].second->eraseFromParent();
218 }
219 return false;
220}
221
222
Chris Lattnerac161bf2009-01-02 07:01:27 +0000223//===----------------------------------------------------------------------===//
224// Top-Level Entities
225//===----------------------------------------------------------------------===//
226
227bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000228 while (1) {
229 switch (Lex.getKind()) {
230 default: return TokError("expected top-level entity");
231 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000232 case lltok::kw_declare: if (ParseDeclare()) return true; break;
233 case lltok::kw_define: if (ParseDefine()) return true; break;
234 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
235 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000236 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000237 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000238 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000239 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000240 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000241 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000242 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000243
244 // The Global variable production with no name can have many different
245 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000246 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
247 // OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000248 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000249 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000250 case lltok::kw_internal: // OptionalLinkage
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +0000251 case lltok::kw_linker_private: // Obsolete OptionalLinkage
252 case lltok::kw_linker_private_weak: // Obsolete OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000253 case lltok::kw_weak: // OptionalLinkage
254 case lltok::kw_weak_odr: // OptionalLinkage
255 case lltok::kw_linkonce: // OptionalLinkage
256 case lltok::kw_linkonce_odr: // OptionalLinkage
257 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000258 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000259 case lltok::kw_extern_weak: // OptionalLinkage
260 case lltok::kw_external: { // OptionalLinkage
Nico Rieck7157bb72014-01-14 15:22:47 +0000261 unsigned Linkage, Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000262 if (ParseOptionalLinkage(Linkage) ||
263 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000264 ParseOptionalDLLStorageClass(DLLStorageClass) ||
265 ParseGlobal("", SMLoc(), Linkage, true, Visibility, DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000266 return true;
267 break;
268 }
269 case lltok::kw_default: // OptionalVisibility
270 case lltok::kw_hidden: // OptionalVisibility
271 case lltok::kw_protected: { // OptionalVisibility
Nico Rieck7157bb72014-01-14 15:22:47 +0000272 unsigned Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000273 if (ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000274 ParseOptionalDLLStorageClass(DLLStorageClass) ||
275 ParseGlobal("", SMLoc(), 0, false, Visibility, DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000276 return true;
277 break;
278 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000279
Chris Lattnerac161bf2009-01-02 07:01:27 +0000280 case lltok::kw_thread_local: // OptionalThreadLocal
281 case lltok::kw_addrspace: // OptionalAddrSpace
282 case lltok::kw_constant: // GlobalType
283 case lltok::kw_global: // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000284 if (ParseGlobal("", SMLoc(), 0, false, 0, 0)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000285 break;
Bill Wendlinga7c38772013-02-09 15:48:49 +0000286
287 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000288 }
289 }
290}
291
292
293/// toplevelentity
294/// ::= 'module' 'asm' STRINGCONSTANT
295bool LLParser::ParseModuleAsm() {
296 assert(Lex.getKind() == lltok::kw_module);
297 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000298
299 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000300 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
301 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000302
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000303 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000304 return false;
305}
306
307/// toplevelentity
308/// ::= 'target' 'triple' '=' STRINGCONSTANT
309/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
310bool LLParser::ParseTargetDefinition() {
311 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000312 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000313 switch (Lex.Lex()) {
314 default: return TokError("unknown target property");
315 case lltok::kw_triple:
316 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000317 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
318 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000319 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000320 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000321 return false;
322 case lltok::kw_datalayout:
323 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000324 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
325 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000326 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000327 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000328 return false;
329 }
330}
331
Bill Wendling706d3d62012-11-28 08:41:48 +0000332/// toplevelentity
333/// ::= 'deplibs' '=' '[' ']'
334/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
335/// FIXME: Remove in 4.0. Currently parse, but ignore.
336bool LLParser::ParseDepLibs() {
337 assert(Lex.getKind() == lltok::kw_deplibs);
338 Lex.Lex();
339 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
340 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
341 return true;
342
343 if (EatIfPresent(lltok::rsquare))
344 return false;
345
346 do {
347 std::string Str;
348 if (ParseStringConstant(Str)) return true;
349 } while (EatIfPresent(lltok::comma));
350
351 return ParseToken(lltok::rsquare, "expected ']' at end of list");
352}
353
Dan Gohman466876b2009-08-12 23:32:33 +0000354/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000355/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000356bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000357 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000358 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000359 Lex.Lex(); // eat LocalVarID;
360
361 if (ParseToken(lltok::equal, "expected '=' after name") ||
362 ParseToken(lltok::kw_type, "expected 'type' after '='"))
363 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000364
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000365 if (TypeID >= NumberedTypes.size())
366 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000367
Craig Topper2617dcc2014-04-15 06:32:26 +0000368 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000369 if (ParseStructDefinition(TypeLoc, "",
370 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000371
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000372 if (!isa<StructType>(Result)) {
373 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
374 if (Entry.first)
375 return Error(TypeLoc, "non-struct types may not be recursive");
376 Entry.first = Result;
377 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000378 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000379
Chris Lattnerac161bf2009-01-02 07:01:27 +0000380 return false;
381}
382
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000383
Chris Lattnerac161bf2009-01-02 07:01:27 +0000384/// toplevelentity
385/// ::= LocalVar '=' 'type' type
386bool LLParser::ParseNamedType() {
387 std::string Name = Lex.getStrVal();
388 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000389 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000390
Chris Lattner3822f632009-01-02 08:05:26 +0000391 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000392 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000393 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000394
Craig Topper2617dcc2014-04-15 06:32:26 +0000395 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000396 if (ParseStructDefinition(NameLoc, Name,
397 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000398
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000399 if (!isa<StructType>(Result)) {
400 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
401 if (Entry.first)
402 return Error(NameLoc, "non-struct types may not be recursive");
403 Entry.first = Result;
404 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000405 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000406
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000407 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000408}
409
410
411/// toplevelentity
412/// ::= 'declare' FunctionHeader
413bool LLParser::ParseDeclare() {
414 assert(Lex.getKind() == lltok::kw_declare);
415 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000416
Chris Lattnerac161bf2009-01-02 07:01:27 +0000417 Function *F;
418 return ParseFunctionHeader(F, false);
419}
420
421/// toplevelentity
422/// ::= 'define' FunctionHeader '{' ...
423bool LLParser::ParseDefine() {
424 assert(Lex.getKind() == lltok::kw_define);
425 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000426
Chris Lattnerac161bf2009-01-02 07:01:27 +0000427 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000428 return ParseFunctionHeader(F, true) ||
429 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000430}
431
Chris Lattner3822f632009-01-02 08:05:26 +0000432/// ParseGlobalType
433/// ::= 'constant'
434/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000435bool LLParser::ParseGlobalType(bool &IsConstant) {
436 if (Lex.getKind() == lltok::kw_constant)
437 IsConstant = true;
438 else if (Lex.getKind() == lltok::kw_global)
439 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000440 else {
441 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000442 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000443 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000444 Lex.Lex();
445 return false;
446}
447
Dan Gohman466876b2009-08-12 23:32:33 +0000448/// ParseUnnamedGlobal:
449/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000450/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
451/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000452/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000453/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
454/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000455bool LLParser::ParseUnnamedGlobal() {
456 unsigned VarID = NumberedVals.size();
457 std::string Name;
458 LocTy NameLoc = Lex.getLoc();
459
460 // Handle the GlobalID form.
461 if (Lex.getKind() == lltok::GlobalID) {
462 if (Lex.getUIntVal() != VarID)
463 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000464 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000465 Lex.Lex(); // eat GlobalID;
466
467 if (ParseToken(lltok::equal, "expected '=' after name"))
468 return true;
469 }
470
471 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000472 unsigned Linkage, Visibility, DLLStorageClass;
Dan Gohman466876b2009-08-12 23:32:33 +0000473 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000474 ParseOptionalVisibility(Visibility) ||
475 ParseOptionalDLLStorageClass(DLLStorageClass))
Dan Gohman466876b2009-08-12 23:32:33 +0000476 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000477
Dan Gohman466876b2009-08-12 23:32:33 +0000478 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000479 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
480 DLLStorageClass);
481 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
Dan Gohman466876b2009-08-12 23:32:33 +0000482}
483
Chris Lattnerac161bf2009-01-02 07:01:27 +0000484/// ParseNamedGlobal:
485/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000486/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
487/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000488bool LLParser::ParseNamedGlobal() {
489 assert(Lex.getKind() == lltok::GlobalVar);
490 LocTy NameLoc = Lex.getLoc();
491 std::string Name = Lex.getStrVal();
492 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000493
Chris Lattnerac161bf2009-01-02 07:01:27 +0000494 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000495 unsigned Linkage, Visibility, DLLStorageClass;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000496 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
497 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000498 ParseOptionalVisibility(Visibility) ||
499 ParseOptionalDLLStorageClass(DLLStorageClass))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000500 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000501
Chris Lattnerac161bf2009-01-02 07:01:27 +0000502 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000503 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
504 DLLStorageClass);
505 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000506}
507
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000508// MDString:
509// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000510bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000511 std::string Str;
512 if (ParseStringConstant(Str)) return true;
Chris Lattner1797fc72009-12-29 21:53:55 +0000513 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000514 return false;
515}
516
517// MDNode:
518// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000519//
520/// This version of ParseMDNodeID returns the slot number and null in the case
521/// of a forward reference.
522bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
523 // !{ ..., !42, ... }
524 if (ParseUInt32(SlotNo)) return true;
525
526 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000527 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000528 Result = NumberedMetadata[SlotNo];
529 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000530 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000531 return false;
532}
533
Chris Lattner6dac02a2009-12-30 04:15:23 +0000534bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000535 // !{ ..., !42, ... }
536 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000537 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000538
Chris Lattner8eff0152010-04-01 05:14:45 +0000539 // If not a forward reference, just return it now.
540 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000541
Chris Lattner8eff0152010-04-01 05:14:45 +0000542 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000543 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000544 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000545
Chris Lattnerfc58af22009-12-30 04:51:58 +0000546 if (NumberedMetadata.size() <= MID)
547 NumberedMetadata.resize(MID+1);
548 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000549 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000550 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000551}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000552
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000553/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000554/// !foo = !{ !1, !2 }
555bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000556 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000557 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000558 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000559
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000560 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000561 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000562 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000563 return true;
564
Dan Gohman2637cc12010-07-21 23:38:33 +0000565 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000566 if (Lex.getKind() != lltok::rbrace)
567 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000568 if (ParseToken(lltok::exclaim, "Expected '!' here"))
569 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000570
Craig Topper2617dcc2014-04-15 06:32:26 +0000571 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000572 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000573 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000574 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000575
576 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
577 return true;
578
Devang Patelbe626972009-07-29 00:34:02 +0000579 return false;
580}
581
Devang Patel39e64d42009-07-01 19:21:12 +0000582/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000583/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000584bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000585 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000586 Lex.Lex();
587 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000588
589 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000590 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000591 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000592 if (ParseUInt32(MetadataID) ||
593 ParseToken(lltok::equal, "expected '=' here") ||
594 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000595 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000596 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000597 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000598 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000599 return true;
600
Jay Foad5514afe2011-04-21 19:59:31 +0000601 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000602
Chris Lattnerfc58af22009-12-30 04:51:58 +0000603 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000604 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000605 FI = ForwardRefMDNodes.find(MetadataID);
606 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000607 MDNode *Temp = FI->second.first;
608 Temp->replaceAllUsesWith(Init);
609 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000610 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000611
Chris Lattnerfc58af22009-12-30 04:51:58 +0000612 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
613 } else {
614 if (MetadataID >= NumberedMetadata.size())
615 NumberedMetadata.resize(MetadataID+1);
616
Craig Topper2617dcc2014-04-15 06:32:26 +0000617 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000618 return TokError("Metadata id is already used");
619 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000620 }
621
Devang Patel39e64d42009-07-01 19:21:12 +0000622 return false;
623}
624
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000625static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
626 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
627 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
628}
629
Chris Lattnerac161bf2009-01-02 07:01:27 +0000630/// ParseAlias:
Nico Rieck7157bb72014-01-14 15:22:47 +0000631/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
632/// OptionalLinkage Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000633/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
634/// OptionalLinkage OptionalAddrSpace Type, Aliasee
635///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000636/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000637/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000638///
Nico Rieck7157bb72014-01-14 15:22:47 +0000639/// Everything through DLL storage class has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000640///
641bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Nico Rieck7157bb72014-01-14 15:22:47 +0000642 unsigned Visibility, unsigned DLLStorageClass) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000643 assert(Lex.getKind() == lltok::kw_alias);
644 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000645 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000646 unsigned L;
647 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000648 return true;
649
Rafael Espindola78527052013-10-06 15:10:43 +0000650 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
651
Rafael Espindolacaa43562013-10-09 16:07:32 +0000652 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000653 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000654
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000655 if (!isValidVisibilityForLinkage(Visibility, L))
656 return Error(LinkageLoc,
657 "symbol with local linkage must have default visibility");
658
Rafael Espindola6b238632014-05-16 19:35:39 +0000659 bool HasAddrSpace = Lex.getKind() == lltok::kw_addrspace;
660 unsigned AddrSpace;
661 LocTy AddrSpaceLoc = Lex.getLoc();
662 if (ParseOptionalAddrSpace(AddrSpace))
663 return true;
664
665 LocTy TyLoc = Lex.getLoc();
666 Type *Ty = nullptr;
667 if (ParseType(Ty))
668 return true;
669
670 bool DifferentType = EatIfPresent(lltok::comma);
671 if (HasAddrSpace && !DifferentType)
672 return Error(AddrSpaceLoc, "A type is required if addrspace is given");
673
674 Type *AliaseeType = nullptr;
675 if (DifferentType) {
676 if (ParseType(AliaseeType))
677 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000678 } else {
Rafael Espindola6b238632014-05-16 19:35:39 +0000679 AliaseeType = Ty;
680 auto *PTy = dyn_cast<PointerType>(Ty);
681 if (!PTy)
682 return Error(TyLoc, "An alias must have pointer type");
683 Ty = PTy->getElementType();
684 AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000685 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000686
Rafael Espindola6b238632014-05-16 19:35:39 +0000687 LocTy AliaseeLoc = Lex.getLoc();
688 Constant *C;
689 if (ParseGlobalValue(AliaseeType, C))
690 return true;
691
692 auto *Aliasee = dyn_cast<GlobalObject>(C);
693 if (!Aliasee)
694 return Error(AliaseeLoc, "Alias must point to function or variable");
695
696 assert(Aliasee->getType()->isPointerTy());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000697
698 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000699 std::unique_ptr<GlobalAlias> GA(
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000700 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
701 Name, Aliasee, /*Parent*/ nullptr));
Chris Lattnerac161bf2009-01-02 07:01:27 +0000702 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000703 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000704
Chris Lattnerac161bf2009-01-02 07:01:27 +0000705 // See if this value already exists in the symbol table. If so, it is either
706 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000707 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000708 // See if this was a redefinition. If so, there is no entry in
709 // ForwardRefVals.
710 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
711 I = ForwardRefVals.find(Name);
712 if (I == ForwardRefVals.end())
713 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
714
715 // Otherwise, this was a definition of forward ref. Verify that types
716 // agree.
717 if (Val->getType() != GA->getType())
718 return Error(NameLoc,
719 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000720
Chris Lattnerac161bf2009-01-02 07:01:27 +0000721 // If they agree, just RAUW the old value with the alias and remove the
722 // forward ref info.
Rafael Espindola6b238632014-05-16 19:35:39 +0000723 for (auto *User : Val->users()) {
724 if (auto *GA = dyn_cast<GlobalAlias>(User))
725 return Error(NameLoc, "Alias is pointed by alias " + GA->getName());
726 }
727
Rafael Espindolaaa273822014-05-09 21:49:17 +0000728 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000729 Val->eraseFromParent();
730 ForwardRefVals.erase(I);
731 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000732
Chris Lattnerac161bf2009-01-02 07:01:27 +0000733 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000734 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000735 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000736
Rafael Espindolaaa273822014-05-09 21:49:17 +0000737 // The module owns this now
738 GA.release();
739
Chris Lattnerac161bf2009-01-02 07:01:27 +0000740 return false;
741}
742
743/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000744/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
745/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000746/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000747/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
748/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000749/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000750///
David Majnemerc4ab61c2014-03-09 06:41:58 +0000751/// Everything up to and including OptionalDLLStorageClass has been parsed
752/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000753///
754bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
755 unsigned Linkage, bool HasLinkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000756 unsigned Visibility, unsigned DLLStorageClass) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000757 if (!isValidVisibilityForLinkage(Visibility, Linkage))
758 return Error(NameLoc,
759 "symbol with local linkage must have default visibility");
760
Chris Lattnerac161bf2009-01-02 07:01:27 +0000761 unsigned AddrSpace;
Shuxin Yang2e1890e2013-10-27 03:08:44 +0000762 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000763 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola026d1522011-01-13 01:30:30 +0000764 LocTy UnnamedAddrLoc;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000765 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000766 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000767
Craig Topper2617dcc2014-04-15 06:32:26 +0000768 Type *Ty = nullptr;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000769 if (ParseOptionalThreadLocal(TLM) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000770 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindola026d1522011-01-13 01:30:30 +0000771 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
772 &UnnamedAddrLoc) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000773 ParseOptionalToken(lltok::kw_externally_initialized,
774 IsExternallyInitialized,
775 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000776 ParseGlobalType(IsConstant) ||
777 ParseType(Ty, TyLoc))
778 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000779
Chris Lattnerac161bf2009-01-02 07:01:27 +0000780 // If the linkage is specified and is external, then no initializer is
781 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000782 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000783 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000784 Linkage != GlobalValue::ExternalLinkage)) {
785 if (ParseGlobalValue(Ty, Init))
786 return true;
787 }
788
Duncan Sands19d0b472010-02-16 11:11:14 +0000789 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000790 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000791
Craig Topper2617dcc2014-04-15 06:32:26 +0000792 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000793
794 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000795 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000796 if (GlobalValue *GVal = M->getNamedValue(Name)) {
797 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
798 return Error(NameLoc, "redefinition of global '@" + Name + "'");
799 GV = cast<GlobalVariable>(GVal);
800 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000801 } else {
802 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
803 I = ForwardRefValIDs.find(NumberedVals.size());
804 if (I != ForwardRefValIDs.end()) {
805 GV = cast<GlobalVariable>(I->second.first);
806 ForwardRefValIDs.erase(I);
807 }
808 }
809
Craig Topper2617dcc2014-04-15 06:32:26 +0000810 if (!GV) {
811 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
812 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000813 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000814 } else {
815 if (GV->getType()->getElementType() != Ty)
816 return Error(TyLoc,
817 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000818
Chris Lattnerac161bf2009-01-02 07:01:27 +0000819 // Move the forward-reference to the correct spot in the module.
820 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
821 }
822
823 if (Name.empty())
824 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000825
Chris Lattnerac161bf2009-01-02 07:01:27 +0000826 // Set the parsed properties on the global.
827 if (Init)
828 GV->setInitializer(Init);
829 GV->setConstant(IsConstant);
830 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
831 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000832 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000833 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000834 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000835 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000836
Chris Lattnerac161bf2009-01-02 07:01:27 +0000837 // Parse attributes on the global.
838 while (Lex.getKind() == lltok::comma) {
839 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000840
Chris Lattnerac161bf2009-01-02 07:01:27 +0000841 if (Lex.getKind() == lltok::kw_section) {
842 Lex.Lex();
843 GV->setSection(Lex.getStrVal());
844 if (ParseToken(lltok::StringConstant, "expected global section string"))
845 return true;
846 } else if (Lex.getKind() == lltok::kw_align) {
847 unsigned Alignment;
848 if (ParseOptionalAlignment(Alignment)) return true;
849 GV->setAlignment(Alignment);
850 } else {
851 TokError("unknown global variable property!");
852 }
853 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000854
Chris Lattnerac161bf2009-01-02 07:01:27 +0000855 return false;
856}
857
Bill Wendling63b88192013-02-06 06:52:58 +0000858/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000859/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000860bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000861 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000862 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000863 Lex.Lex();
864
865 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000866 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000867 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000868 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000869 Lex.Lex();
870
871 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000872 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000873 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000874 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000875 ParseToken(lltok::rbrace, "expected end of attribute group"))
876 return true;
877
Bill Wendlingb32b0412013-02-08 06:32:06 +0000878 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000879 return Error(AttrGrpLoc, "attribute group has no attributes");
880
881 return false;
882}
883
Bill Wendling8b0321d2013-02-08 00:52:31 +0000884/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000885/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000886bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
887 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000888 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000889 bool HaveError = false;
890
891 B.clear();
892
Bill Wendling63b88192013-02-06 06:52:58 +0000893 while (true) {
894 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000895 if (Token == lltok::kw_builtin)
896 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000897 switch (Token) {
898 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000899 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000900 return Error(Lex.getLoc(), "unterminated attribute group");
901 case lltok::rbrace:
902 // Finished.
903 return false;
904
Bill Wendlingb32b0412013-02-08 06:32:06 +0000905 case lltok::AttrGrpID: {
906 // Allow a function to reference an attribute group:
907 //
908 // define void @foo() #1 { ... }
909 if (inAttrGrp)
910 HaveError |=
911 Error(Lex.getLoc(),
912 "cannot have an attribute group reference in an attribute group");
913
914 unsigned AttrGrpNum = Lex.getUIntVal();
915 if (inAttrGrp) break;
916
917 // Save the reference to the attribute group. We'll fill it in later.
918 FwdRefAttrGrps.push_back(AttrGrpNum);
919 break;
920 }
Bill Wendling63b88192013-02-06 06:52:58 +0000921 // Target-dependent attributes:
922 case lltok::StringConstant: {
923 std::string Attr = Lex.getStrVal();
924 Lex.Lex();
925 std::string Val;
926 if (EatIfPresent(lltok::equal) &&
927 ParseStringConstant(Val))
928 return true;
929
930 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000931 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000932 }
933
934 // Target-independent attributes:
935 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000936 // As a hack, we allow function alignment to be initially parsed as an
937 // attribute on a function declaration/definition or added to an attribute
938 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000939 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000940 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000941 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000942 if (ParseToken(lltok::equal, "expected '=' here") ||
943 ParseUInt32(Alignment))
944 return true;
945 } else {
946 if (ParseOptionalAlignment(Alignment))
947 return true;
948 }
Bill Wendling63b88192013-02-06 06:52:58 +0000949 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000950 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000951 }
952 case lltok::kw_alignstack: {
953 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000954 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000955 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000956 if (ParseToken(lltok::equal, "expected '=' here") ||
957 ParseUInt32(Alignment))
958 return true;
959 } else {
960 if (ParseOptionalStackAlignment(Alignment))
961 return true;
962 }
Bill Wendling63b88192013-02-06 06:52:58 +0000963 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000964 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000965 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000966 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000967 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000968 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000969 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
970 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
971 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
972 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
973 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
974 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
975 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
976 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
977 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
978 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
979 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000980 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000981 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
982 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
983 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
984 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
985 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
986 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
987 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
988 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
989 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
990 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
991 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000992
993 // Error handling.
994 case lltok::kw_inreg:
995 case lltok::kw_signext:
996 case lltok::kw_zeroext:
997 HaveError |=
998 Error(Lex.getLoc(),
999 "invalid use of attribute on a function");
1000 break;
1001 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001002 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001003 case lltok::kw_nest:
1004 case lltok::kw_noalias:
1005 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001006 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +00001007 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001008 case lltok::kw_sret:
1009 HaveError |=
1010 Error(Lex.getLoc(),
1011 "invalid use of parameter-only attribute on a function");
1012 break;
Bill Wendling63b88192013-02-06 06:52:58 +00001013 }
1014
1015 Lex.Lex();
1016 }
1017}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001018
1019//===----------------------------------------------------------------------===//
1020// GlobalValue Reference/Resolution Routines.
1021//===----------------------------------------------------------------------===//
1022
1023/// GetGlobalVal - Get a value with the specified name or ID, creating a
1024/// forward reference record if needed. This can return null if the value
1025/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001026GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001027 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001028 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001029 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001030 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001031 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001032 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001033
Chris Lattnerac161bf2009-01-02 07:01:27 +00001034 // Look this name up in the normal function symbol table.
1035 GlobalValue *Val =
1036 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001037
Chris Lattnerac161bf2009-01-02 07:01:27 +00001038 // If this is a forward reference for the value, see if we already created a
1039 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001040 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001041 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1042 I = ForwardRefVals.find(Name);
1043 if (I != ForwardRefVals.end())
1044 Val = I->second.first;
1045 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001046
Chris Lattnerac161bf2009-01-02 07:01:27 +00001047 // If we have the value in the symbol table or fwd-ref table, return it.
1048 if (Val) {
1049 if (Val->getType() == Ty) return Val;
1050 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001051 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001052 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001053 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001054
Chris Lattnerac161bf2009-01-02 07:01:27 +00001055 // Otherwise, create a new forward reference for this value and remember it.
1056 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001057 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001058 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001059 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001060 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001061 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1062 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001063 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001064
Chris Lattnerac161bf2009-01-02 07:01:27 +00001065 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1066 return FwdVal;
1067}
1068
Chris Lattner229907c2011-07-18 04:54:35 +00001069GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1070 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001071 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001072 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001073 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001074 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001075
Craig Topper2617dcc2014-04-15 06:32:26 +00001076 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001077
Chris Lattnerac161bf2009-01-02 07:01:27 +00001078 // If this is a forward reference for the value, see if we already created a
1079 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001080 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001081 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1082 I = ForwardRefValIDs.find(ID);
1083 if (I != ForwardRefValIDs.end())
1084 Val = I->second.first;
1085 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001086
Chris Lattnerac161bf2009-01-02 07:01:27 +00001087 // If we have the value in the symbol table or fwd-ref table, return it.
1088 if (Val) {
1089 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001090 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001091 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001092 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001093 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001094
Chris Lattnerac161bf2009-01-02 07:01:27 +00001095 // Otherwise, create a new forward reference for this value and remember it.
1096 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001097 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001098 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001099 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001100 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001101 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001102
Chris Lattnerac161bf2009-01-02 07:01:27 +00001103 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1104 return FwdVal;
1105}
1106
1107
1108//===----------------------------------------------------------------------===//
1109// Helper Routines.
1110//===----------------------------------------------------------------------===//
1111
1112/// ParseToken - If the current token has the specified kind, eat it and return
1113/// success. Otherwise, emit the specified error and return failure.
1114bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1115 if (Lex.getKind() != T)
1116 return TokError(ErrMsg);
1117 Lex.Lex();
1118 return false;
1119}
1120
Chris Lattner3822f632009-01-02 08:05:26 +00001121/// ParseStringConstant
1122/// ::= StringConstant
1123bool LLParser::ParseStringConstant(std::string &Result) {
1124 if (Lex.getKind() != lltok::StringConstant)
1125 return TokError("expected string constant");
1126 Result = Lex.getStrVal();
1127 Lex.Lex();
1128 return false;
1129}
1130
1131/// ParseUInt32
1132/// ::= uint32
1133bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001134 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1135 return TokError("expected integer");
1136 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1137 if (Val64 != unsigned(Val64))
1138 return TokError("expected 32-bit integer (too large)");
1139 Val = Val64;
1140 Lex.Lex();
1141 return false;
1142}
1143
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001144/// ParseTLSModel
1145/// := 'localdynamic'
1146/// := 'initialexec'
1147/// := 'localexec'
1148bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1149 switch (Lex.getKind()) {
1150 default:
1151 return TokError("expected localdynamic, initialexec or localexec");
1152 case lltok::kw_localdynamic:
1153 TLM = GlobalVariable::LocalDynamicTLSModel;
1154 break;
1155 case lltok::kw_initialexec:
1156 TLM = GlobalVariable::InitialExecTLSModel;
1157 break;
1158 case lltok::kw_localexec:
1159 TLM = GlobalVariable::LocalExecTLSModel;
1160 break;
1161 }
1162
1163 Lex.Lex();
1164 return false;
1165}
1166
1167/// ParseOptionalThreadLocal
1168/// := /*empty*/
1169/// := 'thread_local'
1170/// := 'thread_local' '(' tlsmodel ')'
1171bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1172 TLM = GlobalVariable::NotThreadLocal;
1173 if (!EatIfPresent(lltok::kw_thread_local))
1174 return false;
1175
1176 TLM = GlobalVariable::GeneralDynamicTLSModel;
1177 if (Lex.getKind() == lltok::lparen) {
1178 Lex.Lex();
1179 return ParseTLSModel(TLM) ||
1180 ParseToken(lltok::rparen, "expected ')' after thread local model");
1181 }
1182 return false;
1183}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001184
1185/// ParseOptionalAddrSpace
1186/// := /*empty*/
1187/// := 'addrspace' '(' uint32 ')'
1188bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1189 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001190 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001191 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001192 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001193 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001194 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001195}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001196
Bill Wendling34c2eb22012-12-04 23:40:58 +00001197/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1198bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1199 bool HaveError = false;
1200
1201 B.clear();
1202
1203 while (1) {
1204 lltok::Kind Token = Lex.getKind();
1205 switch (Token) {
1206 default: // End of attributes.
1207 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001208 case lltok::kw_align: {
1209 unsigned Alignment;
1210 if (ParseOptionalAlignment(Alignment))
1211 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001212 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001213 continue;
1214 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001215 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001216 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001217 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1218 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1219 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1220 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001221 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001222 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1223 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001224 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001225 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1226 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1227 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001228
Stephen Lin7577ed52013-04-20 13:16:13 +00001229 case lltok::kw_alignstack:
1230 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001231 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001232 case lltok::kw_inlinehint:
1233 case lltok::kw_minsize:
1234 case lltok::kw_naked:
1235 case lltok::kw_nobuiltin:
1236 case lltok::kw_noduplicate:
1237 case lltok::kw_noimplicitfloat:
1238 case lltok::kw_noinline:
1239 case lltok::kw_nonlazybind:
1240 case lltok::kw_noredzone:
1241 case lltok::kw_noreturn:
1242 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001243 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001244 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001245 case lltok::kw_returns_twice:
1246 case lltok::kw_sanitize_address:
1247 case lltok::kw_sanitize_memory:
1248 case lltok::kw_sanitize_thread:
1249 case lltok::kw_ssp:
1250 case lltok::kw_sspreq:
1251 case lltok::kw_sspstrong:
1252 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001253 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1254 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001255 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001256
Bill Wendling34c2eb22012-12-04 23:40:58 +00001257 Lex.Lex();
1258 }
1259}
1260
1261/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1262bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1263 bool HaveError = false;
1264
1265 B.clear();
1266
1267 while (1) {
1268 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001269 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001270 default: // End of attributes.
1271 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001272 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1273 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001274 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001275 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1276 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001277
Bill Wendling34c2eb22012-12-04 23:40:58 +00001278 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001279 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001280 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001281 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001282 case lltok::kw_nest:
1283 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001284 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001285 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001286 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001287 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001288
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001289 case lltok::kw_alignstack:
1290 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001291 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001292 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001293 case lltok::kw_inlinehint:
1294 case lltok::kw_minsize:
1295 case lltok::kw_naked:
1296 case lltok::kw_nobuiltin:
1297 case lltok::kw_noduplicate:
1298 case lltok::kw_noimplicitfloat:
1299 case lltok::kw_noinline:
1300 case lltok::kw_nonlazybind:
1301 case lltok::kw_noredzone:
1302 case lltok::kw_noreturn:
1303 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001304 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001305 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001306 case lltok::kw_returns_twice:
1307 case lltok::kw_sanitize_address:
1308 case lltok::kw_sanitize_memory:
1309 case lltok::kw_sanitize_thread:
1310 case lltok::kw_ssp:
1311 case lltok::kw_sspreq:
1312 case lltok::kw_sspstrong:
1313 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001314 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001315 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001316
1317 case lltok::kw_readnone:
1318 case lltok::kw_readonly:
1319 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001320 }
1321
Chris Lattnerac161bf2009-01-02 07:01:27 +00001322 Lex.Lex();
1323 }
1324}
1325
1326/// ParseOptionalLinkage
1327/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001328/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001329/// ::= 'internal'
1330/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001331/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001332/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001333/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001334/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001335/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001336/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001337/// ::= 'extern_weak'
1338/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001339///
1340/// Deprecated Values:
1341/// ::= 'linker_private'
1342/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001343bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1344 HasLinkage = false;
1345 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001346 default: Res=GlobalValue::ExternalLinkage; return false;
1347 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001348 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1349 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1350 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1351 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1352 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001353 case lltok::kw_available_externally:
1354 Res = GlobalValue::AvailableExternallyLinkage;
1355 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001356 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001357 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001358 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1359 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001360
1361 case lltok::kw_linker_private:
1362 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001363 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1364 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001365 Lex.Lex();
1366 // treat linker_private and linker_private_weak as PrivateLinkage
1367 Res = GlobalValue::PrivateLinkage;
1368 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001369 }
1370 Lex.Lex();
1371 HasLinkage = true;
1372 return false;
1373}
1374
1375/// ParseOptionalVisibility
1376/// ::= /*empty*/
1377/// ::= 'default'
1378/// ::= 'hidden'
1379/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001380///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001381bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1382 switch (Lex.getKind()) {
1383 default: Res = GlobalValue::DefaultVisibility; return false;
1384 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1385 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1386 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1387 }
1388 Lex.Lex();
1389 return false;
1390}
1391
Nico Rieck7157bb72014-01-14 15:22:47 +00001392/// ParseOptionalDLLStorageClass
1393/// ::= /*empty*/
1394/// ::= 'dllimport'
1395/// ::= 'dllexport'
1396///
1397bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1398 switch (Lex.getKind()) {
1399 default: Res = GlobalValue::DefaultStorageClass; return false;
1400 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1401 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1402 }
1403 Lex.Lex();
1404 return false;
1405}
1406
Chris Lattnerac161bf2009-01-02 07:01:27 +00001407/// ParseOptionalCallingConv
1408/// ::= /*empty*/
1409/// ::= 'ccc'
1410/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001411/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001412/// ::= 'coldcc'
1413/// ::= 'x86_stdcallcc'
1414/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001415/// ::= 'x86_thiscallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001416/// ::= 'arm_apcscc'
1417/// ::= 'arm_aapcscc'
1418/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001419/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001420/// ::= 'ptx_kernel'
1421/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001422/// ::= 'spir_func'
1423/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001424/// ::= 'x86_64_sysvcc'
1425/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001426/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001427/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001428/// ::= 'preserve_mostcc'
1429/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001430/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001431///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001432bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001433 switch (Lex.getKind()) {
1434 default: CC = CallingConv::C; return false;
1435 case lltok::kw_ccc: CC = CallingConv::C; break;
1436 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1437 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1438 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1439 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001440 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001441 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1442 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1443 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001444 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001445 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1446 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001447 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1448 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001449 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001450 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1451 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001452 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001453 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001454 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1455 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001456 case lltok::kw_cc: {
1457 unsigned ArbitraryCC;
1458 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001459 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001460 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001461 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1462 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001463 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001464 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001465
Chris Lattnerac161bf2009-01-02 07:01:27 +00001466 Lex.Lex();
1467 return false;
1468}
1469
Chris Lattner5c427632009-12-30 05:31:19 +00001470/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001471/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001472bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1473 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001474 do {
1475 if (Lex.getKind() != lltok::MetadataVar)
1476 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001477
Chris Lattner596760d2009-12-29 21:25:40 +00001478 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001479 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001480 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001481
Chris Lattner1797fc72009-12-29 21:53:55 +00001482 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001483 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001484
1485 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001486 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001487
Dan Gohmanf0715b12010-08-24 14:35:45 +00001488 // This code is similar to that of ParseMetadataValue, however it needs to
1489 // have special-case code for a forward reference; see the comments on
1490 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1491 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001492 if (Lex.getKind() == lltok::lbrace) {
1493 ValID ID;
1494 if (ParseMetadataListValue(ID, PFS))
1495 return true;
1496 assert(ID.Kind == ValID::t_MDNode);
1497 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001498 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001499 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001500 if (ParseMDNodeID(Node, NodeID))
1501 return true;
1502 if (Node) {
1503 // If we got the node, add it to the instruction.
1504 Inst->setMetadata(MDK, Node);
1505 } else {
1506 MDRef R = { Loc, MDK, NodeID };
1507 // Otherwise, remember that this should be resolved later.
1508 ForwardRefInstMetadata[Inst].push_back(R);
1509 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001510 }
Chris Lattner596760d2009-12-29 21:25:40 +00001511
Manman Ren209b17c2013-09-28 00:22:27 +00001512 if (MDK == LLVMContext::MD_tbaa)
1513 InstsWithTBAATag.push_back(Inst);
1514
Chris Lattner596760d2009-12-29 21:25:40 +00001515 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001516 } while (EatIfPresent(lltok::comma));
1517 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001518}
1519
Chris Lattnerac161bf2009-01-02 07:01:27 +00001520/// ParseOptionalAlignment
1521/// ::= /* empty */
1522/// ::= 'align' 4
1523bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1524 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001525 if (!EatIfPresent(lltok::kw_align))
1526 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001527 LocTy AlignLoc = Lex.getLoc();
1528 if (ParseUInt32(Alignment)) return true;
1529 if (!isPowerOf2_32(Alignment))
1530 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001531 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001532 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001533 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001534}
1535
Chris Lattnerb2f39502009-12-30 05:44:30 +00001536/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001537/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001538/// ::= ',' align 4
1539///
1540/// This returns with AteExtraComma set to true if it ate an excess comma at the
1541/// end.
1542bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1543 bool &AteExtraComma) {
1544 AteExtraComma = false;
1545 while (EatIfPresent(lltok::comma)) {
1546 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001547 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001548 AteExtraComma = true;
1549 return false;
1550 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001551
Chris Lattner95b0ff42010-04-23 00:50:50 +00001552 if (Lex.getKind() != lltok::kw_align)
1553 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001554
Chris Lattner95b0ff42010-04-23 00:50:50 +00001555 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001556 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001557
Devang Patelea8a4b92009-09-17 23:04:48 +00001558 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001559}
1560
Eli Friedmanfee02c62011-07-25 23:16:38 +00001561/// ParseScopeAndOrdering
1562/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1563/// else: ::=
1564///
1565/// This sets Scope and Ordering to the parsed values.
1566bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1567 AtomicOrdering &Ordering) {
1568 if (!isAtomic)
1569 return false;
1570
1571 Scope = CrossThread;
1572 if (EatIfPresent(lltok::kw_singlethread))
1573 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001574
1575 return ParseOrdering(Ordering);
1576}
1577
1578/// ParseOrdering
1579/// ::= AtomicOrdering
1580///
1581/// This sets Ordering to the parsed value.
1582bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001583 switch (Lex.getKind()) {
1584 default: return TokError("Expected ordering on atomic instruction");
1585 case lltok::kw_unordered: Ordering = Unordered; break;
1586 case lltok::kw_monotonic: Ordering = Monotonic; break;
1587 case lltok::kw_acquire: Ordering = Acquire; break;
1588 case lltok::kw_release: Ordering = Release; break;
1589 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1590 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1591 }
1592 Lex.Lex();
1593 return false;
1594}
1595
Charles Davisbe5557e2010-02-12 00:31:15 +00001596/// ParseOptionalStackAlignment
1597/// ::= /* empty */
1598/// ::= 'alignstack' '(' 4 ')'
1599bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1600 Alignment = 0;
1601 if (!EatIfPresent(lltok::kw_alignstack))
1602 return false;
1603 LocTy ParenLoc = Lex.getLoc();
1604 if (!EatIfPresent(lltok::lparen))
1605 return Error(ParenLoc, "expected '('");
1606 LocTy AlignLoc = Lex.getLoc();
1607 if (ParseUInt32(Alignment)) return true;
1608 ParenLoc = Lex.getLoc();
1609 if (!EatIfPresent(lltok::rparen))
1610 return Error(ParenLoc, "expected ')'");
1611 if (!isPowerOf2_32(Alignment))
1612 return Error(AlignLoc, "stack alignment is not a power of two");
1613 return false;
1614}
Devang Patelea8a4b92009-09-17 23:04:48 +00001615
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001616/// ParseIndexList - This parses the index list for an insert/extractvalue
1617/// instruction. This sets AteExtraComma in the case where we eat an extra
1618/// comma at the end of the line and find that it is followed by metadata.
1619/// Clients that don't allow metadata can call the version of this function that
1620/// only takes one argument.
1621///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001622/// ParseIndexList
1623/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001624///
1625bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1626 bool &AteExtraComma) {
1627 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001628
Chris Lattnerac161bf2009-01-02 07:01:27 +00001629 if (Lex.getKind() != lltok::comma)
1630 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001631
Chris Lattner3822f632009-01-02 08:05:26 +00001632 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001633 if (Lex.getKind() == lltok::MetadataVar) {
1634 AteExtraComma = true;
1635 return false;
1636 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001637 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001638 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001639 Indices.push_back(Idx);
1640 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001641
Chris Lattnerac161bf2009-01-02 07:01:27 +00001642 return false;
1643}
1644
1645//===----------------------------------------------------------------------===//
1646// Type Parsing.
1647//===----------------------------------------------------------------------===//
1648
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001649/// ParseType - Parse a type.
1650bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1651 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001652 switch (Lex.getKind()) {
1653 default:
1654 return TokError("expected type");
1655 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001656 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001657 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001658 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001659 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001660 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001661 // Type ::= StructType
1662 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001663 return true;
1664 break;
1665 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001666 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001667 Lex.Lex(); // eat the lsquare.
1668 if (ParseArrayVectorType(Result, false))
1669 return true;
1670 break;
1671 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001672 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001673 Lex.Lex();
1674 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001675 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001676 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001677 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001678 } else if (ParseArrayVectorType(Result, true))
1679 return true;
1680 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001681 case lltok::LocalVar: {
1682 // Type ::= %foo
1683 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001684
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001685 // If the type hasn't been defined yet, create a forward definition and
1686 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001687 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001688 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001689 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001690 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001691 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001692 Lex.Lex();
1693 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001694 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001695
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001696 case lltok::LocalVarID: {
1697 // Type ::= %4
1698 if (Lex.getUIntVal() >= NumberedTypes.size())
1699 NumberedTypes.resize(Lex.getUIntVal()+1);
1700 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001701
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001702 // If the type hasn't been defined yet, create a forward definition and
1703 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001704 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001705 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001706 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001707 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001708 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001709 Lex.Lex();
1710 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001711 }
1712 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001713
1714 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001715 while (1) {
1716 switch (Lex.getKind()) {
1717 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001718 default:
1719 if (!AllowVoid && Result->isVoidTy())
1720 return Error(TypeLoc, "void type only allowed for function results");
1721 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001722
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001723 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001724 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001725 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001726 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001727 if (Result->isVoidTy())
1728 return TokError("pointers to void are invalid - use i8* instead");
1729 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001730 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001731 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001732 Lex.Lex();
1733 break;
1734
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001735 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001736 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001737 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001738 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001739 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001740 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001741 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001742 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001743 unsigned AddrSpace;
1744 if (ParseOptionalAddrSpace(AddrSpace) ||
1745 ParseToken(lltok::star, "expected '*' in address space"))
1746 return true;
1747
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001748 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001749 break;
1750 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001751
Chris Lattnerac161bf2009-01-02 07:01:27 +00001752 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1753 case lltok::lparen:
1754 if (ParseFunctionType(Result))
1755 return true;
1756 break;
1757 }
1758 }
1759}
1760
1761/// ParseParameterList
1762/// ::= '(' ')'
1763/// ::= '(' Arg (',' Arg)* ')'
1764/// Arg
1765/// ::= Type OptionalAttributes Value OptionalAttributes
1766bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1767 PerFunctionState &PFS) {
1768 if (ParseToken(lltok::lparen, "expected '(' in call"))
1769 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001770
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001771 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001772 while (Lex.getKind() != lltok::rparen) {
1773 // If this isn't the first argument, we need a comma.
1774 if (!ArgList.empty() &&
1775 ParseToken(lltok::comma, "expected ',' in argument list"))
1776 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001777
Chris Lattnerac161bf2009-01-02 07:01:27 +00001778 // Parse the argument.
1779 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001780 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001781 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001782 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001783 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001784 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001785
Chris Lattner5b4a9622009-12-30 02:11:14 +00001786 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001787 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001788 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001789 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1790 AttrIndex++,
1791 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001792 }
1793
1794 Lex.Lex(); // Lex the ')'.
1795 return false;
1796}
1797
1798
1799
Chris Lattner2ed06b42009-01-05 18:34:07 +00001800/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001801/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001802/// ::= '(' ArgTypeListI ')'
1803/// ArgTypeListI
1804/// ::= /*empty*/
1805/// ::= '...'
1806/// ::= ArgTypeList ',' '...'
1807/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001808///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001809bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1810 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001811 isVarArg = false;
1812 assert(Lex.getKind() == lltok::lparen);
1813 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001814
Chris Lattnerac161bf2009-01-02 07:01:27 +00001815 if (Lex.getKind() == lltok::rparen) {
1816 // empty
1817 } else if (Lex.getKind() == lltok::dotdotdot) {
1818 isVarArg = true;
1819 Lex.Lex();
1820 } else {
1821 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001822 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001823 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001824 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001825
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001826 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001827 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001828
Chris Lattnerfdd87902009-10-05 05:54:46 +00001829 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001830 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001831
Chris Lattnerdef19492011-06-17 06:36:20 +00001832 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001833 Name = Lex.getStrVal();
1834 Lex.Lex();
1835 }
Chris Lattner3822f632009-01-02 08:05:26 +00001836
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001837 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001838 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001839
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001840 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001841 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001842 AttributeSet::get(ArgTy->getContext(),
1843 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001844
Chris Lattner3822f632009-01-02 08:05:26 +00001845 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001846 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001847 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001848 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001849 break;
1850 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001851
Chris Lattnerac161bf2009-01-02 07:01:27 +00001852 // Otherwise must be an argument type.
1853 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001854 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001855
Chris Lattnerfdd87902009-10-05 05:54:46 +00001856 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001857 return Error(TypeLoc, "argument can not have void type");
1858
Chris Lattnerdef19492011-06-17 06:36:20 +00001859 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001860 Name = Lex.getStrVal();
1861 Lex.Lex();
1862 } else {
1863 Name = "";
1864 }
Chris Lattner3822f632009-01-02 08:05:26 +00001865
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001866 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001867 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001868
Bill Wendlingd079a442012-10-15 04:46:55 +00001869 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001870 AttributeSet::get(ArgTy->getContext(),
1871 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001872 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001873 }
1874 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001875
Chris Lattner3822f632009-01-02 08:05:26 +00001876 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001877}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001878
Chris Lattnerac161bf2009-01-02 07:01:27 +00001879/// ParseFunctionType
1880/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001881bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001882 assert(Lex.getKind() == lltok::lparen);
1883
Chris Lattnerce473c72009-01-05 08:04:33 +00001884 if (!FunctionType::isValidReturnType(Result))
1885 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001886
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001887 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001888 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001889 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001890 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001891
Chris Lattnerac161bf2009-01-02 07:01:27 +00001892 // Reject names on the arguments lists.
1893 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1894 if (!ArgList[i].Name.empty())
1895 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001896 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001897 return Error(ArgList[i].Loc,
1898 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001899 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001900
Jay Foadb804a2b2011-07-12 14:06:48 +00001901 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001902 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001903 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001904
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001905 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001906 return false;
1907}
1908
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001909/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1910/// other structs.
1911bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1912 SmallVector<Type*, 8> Elts;
1913 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001914
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001915 Result = StructType::get(Context, Elts, Packed);
1916 return false;
1917}
1918
1919/// ParseStructDefinition - Parse a struct in a 'type' definition.
1920bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1921 std::pair<Type*, LocTy> &Entry,
1922 Type *&ResultTy) {
1923 // If the type was already defined, diagnose the redefinition.
1924 if (Entry.first && !Entry.second.isValid())
1925 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001926
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001927 // If we have opaque, just return without filling in the definition for the
1928 // struct. This counts as a definition as far as the .ll file goes.
1929 if (EatIfPresent(lltok::kw_opaque)) {
1930 // This type is being defined, so clear the location to indicate this.
1931 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001932
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001933 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001934 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001935 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001936 ResultTy = Entry.first;
1937 return false;
1938 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001939
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001940 // If the type starts with '<', then it is either a packed struct or a vector.
1941 bool isPacked = EatIfPresent(lltok::less);
1942
1943 // If we don't have a struct, then we have a random type alias, which we
1944 // accept for compatibility with old files. These types are not allowed to be
1945 // forward referenced and not allowed to be recursive.
1946 if (Lex.getKind() != lltok::lbrace) {
1947 if (Entry.first)
1948 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001949
Craig Topper2617dcc2014-04-15 06:32:26 +00001950 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001951 if (isPacked)
1952 return ParseArrayVectorType(ResultTy, true);
1953 return ParseType(ResultTy);
1954 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001955
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001956 // This type is being defined, so clear the location to indicate this.
1957 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001958
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001959 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001960 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001961 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001962
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001963 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001964
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001965 SmallVector<Type*, 8> Body;
1966 if (ParseStructBody(Body) ||
1967 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1968 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001969
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001970 STy->setBody(Body, isPacked);
1971 ResultTy = STy;
1972 return false;
1973}
1974
1975
Chris Lattnerac161bf2009-01-02 07:01:27 +00001976/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001977/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001978/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001979/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001980/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001981/// ::= '<' '{' Type (',' Type)* '}' '>'
1982bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001983 assert(Lex.getKind() == lltok::lbrace);
1984 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001985
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001986 // Handle the empty struct.
1987 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001988 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001989
Chris Lattnerf880ca22009-03-09 04:49:14 +00001990 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001991 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001992 if (ParseType(Ty)) return true;
1993 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001994
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001995 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001996 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001997
Chris Lattner3822f632009-01-02 08:05:26 +00001998 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00001999 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002000 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002001
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002002 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002003 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002004
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002005 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002006 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002007
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002008 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002009}
2010
2011/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2012/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002013/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002014/// ::= '[' APSINTVAL 'x' Types ']'
2015/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002016bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002017 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2018 Lex.getAPSIntVal().getBitWidth() > 64)
2019 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002020
Chris Lattnerac161bf2009-01-02 07:01:27 +00002021 LocTy SizeLoc = Lex.getLoc();
2022 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002023 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002024
Chris Lattner3822f632009-01-02 08:05:26 +00002025 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2026 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002027
2028 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002029 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002030 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002031
Chris Lattner3822f632009-01-02 08:05:26 +00002032 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2033 "expected end of sequential type"))
2034 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002035
Chris Lattnerac161bf2009-01-02 07:01:27 +00002036 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002037 if (Size == 0)
2038 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002039 if ((unsigned)Size != Size)
2040 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002041 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002042 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002043 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002044 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002045 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002046 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002047 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002048 }
2049 return false;
2050}
2051
2052//===----------------------------------------------------------------------===//
2053// Function Semantic Analysis.
2054//===----------------------------------------------------------------------===//
2055
Chris Lattner3432c622009-10-28 03:39:23 +00002056LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2057 int functionNumber)
2058 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002059
2060 // Insert unnamed arguments into the NumberedVals list.
2061 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2062 AI != E; ++AI)
2063 if (!AI->hasName())
2064 NumberedVals.push_back(AI);
2065}
2066
2067LLParser::PerFunctionState::~PerFunctionState() {
2068 // If there were any forward referenced non-basicblock values, delete them.
2069 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2070 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2071 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002072 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002073 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002074 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002075 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002076 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002077
Chris Lattnerac161bf2009-01-02 07:01:27 +00002078 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2079 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2080 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002081 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002082 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002083 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002084 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002085 }
2086}
2087
Chris Lattner3432c622009-10-28 03:39:23 +00002088bool LLParser::PerFunctionState::FinishFunction() {
2089 // Check to see if someone took the address of labels in this block.
2090 if (!P.ForwardRefBlockAddresses.empty()) {
2091 ValID FunctionID;
2092 if (!F.getName().empty()) {
2093 FunctionID.Kind = ValID::t_GlobalName;
2094 FunctionID.StrVal = F.getName();
2095 } else {
2096 FunctionID.Kind = ValID::t_GlobalID;
2097 FunctionID.UIntVal = FunctionNumber;
2098 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002099
Chris Lattner3432c622009-10-28 03:39:23 +00002100 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2101 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2102 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2103 // Resolve all these references.
2104 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2105 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002106
Chris Lattner3432c622009-10-28 03:39:23 +00002107 P.ForwardRefBlockAddresses.erase(FRBAI);
2108 }
2109 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002110
Chris Lattnerac161bf2009-01-02 07:01:27 +00002111 if (!ForwardRefVals.empty())
2112 return P.Error(ForwardRefVals.begin()->second.second,
2113 "use of undefined value '%" + ForwardRefVals.begin()->first +
2114 "'");
2115 if (!ForwardRefValIDs.empty())
2116 return P.Error(ForwardRefValIDs.begin()->second.second,
2117 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002118 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002119 return false;
2120}
2121
2122
2123/// GetVal - Get a value with the specified name or ID, creating a
2124/// forward reference record if needed. This can return null if the value
2125/// exists but does not have the right type.
2126Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002127 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002128 // Look this name up in the normal function symbol table.
2129 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002130
Chris Lattnerac161bf2009-01-02 07:01:27 +00002131 // If this is a forward reference for the value, see if we already created a
2132 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002133 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002134 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2135 I = ForwardRefVals.find(Name);
2136 if (I != ForwardRefVals.end())
2137 Val = I->second.first;
2138 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002139
Chris Lattnerac161bf2009-01-02 07:01:27 +00002140 // If we have the value in the symbol table or fwd-ref table, return it.
2141 if (Val) {
2142 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002143 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002144 P.Error(Loc, "'%" + Name + "' is not a basic block");
2145 else
2146 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002147 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002148 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002149 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002150
Chris Lattnerac161bf2009-01-02 07:01:27 +00002151 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002152 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002153 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002154 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002155 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002156
Chris Lattnerac161bf2009-01-02 07:01:27 +00002157 // Otherwise, create a new forward reference for this value and remember it.
2158 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002159 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002160 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002161 else
2162 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002163
Chris Lattnerac161bf2009-01-02 07:01:27 +00002164 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2165 return FwdVal;
2166}
2167
Chris Lattner229907c2011-07-18 04:54:35 +00002168Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002169 LocTy Loc) {
2170 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002171 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002172
Chris Lattnerac161bf2009-01-02 07:01:27 +00002173 // If this is a forward reference for the value, see if we already created a
2174 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002175 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002176 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2177 I = ForwardRefValIDs.find(ID);
2178 if (I != ForwardRefValIDs.end())
2179 Val = I->second.first;
2180 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002181
Chris Lattnerac161bf2009-01-02 07:01:27 +00002182 // If we have the value in the symbol table or fwd-ref table, return it.
2183 if (Val) {
2184 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002185 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002186 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002187 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002188 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002189 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002190 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002191 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002192
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002193 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002194 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002195 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002196 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002197
Chris Lattnerac161bf2009-01-02 07:01:27 +00002198 // Otherwise, create a new forward reference for this value and remember it.
2199 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002200 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002201 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002202 else
2203 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002204
Chris Lattnerac161bf2009-01-02 07:01:27 +00002205 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2206 return FwdVal;
2207}
2208
2209/// SetInstName - After an instruction is parsed and inserted into its
2210/// basic block, this installs its name.
2211bool LLParser::PerFunctionState::SetInstName(int NameID,
2212 const std::string &NameStr,
2213 LocTy NameLoc, Instruction *Inst) {
2214 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002215 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002216 if (NameID != -1 || !NameStr.empty())
2217 return P.Error(NameLoc, "instructions returning void cannot have a name");
2218 return false;
2219 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002220
Chris Lattnerac161bf2009-01-02 07:01:27 +00002221 // If this was a numbered instruction, verify that the instruction is the
2222 // expected value and resolve any forward references.
2223 if (NameStr.empty()) {
2224 // If neither a name nor an ID was specified, just use the next ID.
2225 if (NameID == -1)
2226 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002227
Chris Lattnerac161bf2009-01-02 07:01:27 +00002228 if (unsigned(NameID) != NumberedVals.size())
2229 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002230 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002231
Chris Lattnerac161bf2009-01-02 07:01:27 +00002232 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2233 ForwardRefValIDs.find(NameID);
2234 if (FI != ForwardRefValIDs.end()) {
2235 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002236 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002237 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002238 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002239 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002240 ForwardRefValIDs.erase(FI);
2241 }
2242
2243 NumberedVals.push_back(Inst);
2244 return false;
2245 }
2246
2247 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2248 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2249 FI = ForwardRefVals.find(NameStr);
2250 if (FI != ForwardRefVals.end()) {
2251 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002252 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002253 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002254 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002255 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002256 ForwardRefVals.erase(FI);
2257 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002258
Chris Lattnerac161bf2009-01-02 07:01:27 +00002259 // Set the name on the instruction.
2260 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002261
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002262 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002263 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002264 NameStr + "'");
2265 return false;
2266}
2267
2268/// GetBB - Get a basic block with the specified name or ID, creating a
2269/// forward reference record if needed.
2270BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2271 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002272 return cast_or_null<BasicBlock>(GetVal(Name,
2273 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002274}
2275
2276BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002277 return cast_or_null<BasicBlock>(GetVal(ID,
2278 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002279}
2280
2281/// DefineBB - Define the specified basic block, which is either named or
2282/// unnamed. If there is an error, this returns null otherwise it returns
2283/// the block being defined.
2284BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2285 LocTy Loc) {
2286 BasicBlock *BB;
2287 if (Name.empty())
2288 BB = GetBB(NumberedVals.size(), Loc);
2289 else
2290 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002291 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002292
Chris Lattnerac161bf2009-01-02 07:01:27 +00002293 // Move the block to the end of the function. Forward ref'd blocks are
2294 // inserted wherever they happen to be referenced.
2295 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002296
Chris Lattnerac161bf2009-01-02 07:01:27 +00002297 // Remove the block from forward ref sets.
2298 if (Name.empty()) {
2299 ForwardRefValIDs.erase(NumberedVals.size());
2300 NumberedVals.push_back(BB);
2301 } else {
2302 // BB forward references are already in the function symbol table.
2303 ForwardRefVals.erase(Name);
2304 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002305
Chris Lattnerac161bf2009-01-02 07:01:27 +00002306 return BB;
2307}
2308
2309//===----------------------------------------------------------------------===//
2310// Constants.
2311//===----------------------------------------------------------------------===//
2312
2313/// ParseValID - Parse an abstract value that doesn't necessarily have a
2314/// type implied. For example, if we parse "4" we don't know what integer type
2315/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002316/// sanity. PFS is used to convert function-local operands of metadata (since
2317/// metadata operands are not just parsed here but also converted to values).
2318/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002319bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002320 ID.Loc = Lex.getLoc();
2321 switch (Lex.getKind()) {
2322 default: return TokError("expected value token");
2323 case lltok::GlobalID: // @42
2324 ID.UIntVal = Lex.getUIntVal();
2325 ID.Kind = ValID::t_GlobalID;
2326 break;
2327 case lltok::GlobalVar: // @foo
2328 ID.StrVal = Lex.getStrVal();
2329 ID.Kind = ValID::t_GlobalName;
2330 break;
2331 case lltok::LocalVarID: // %42
2332 ID.UIntVal = Lex.getUIntVal();
2333 ID.Kind = ValID::t_LocalID;
2334 break;
2335 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002336 ID.StrVal = Lex.getStrVal();
2337 ID.Kind = ValID::t_LocalName;
2338 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002339 case lltok::exclaim: // !42, !{...}, or !"foo"
2340 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002341 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002342 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002343 ID.Kind = ValID::t_APSInt;
2344 break;
2345 case lltok::APFloat:
2346 ID.APFloatVal = Lex.getAPFloatVal();
2347 ID.Kind = ValID::t_APFloat;
2348 break;
2349 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002350 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002351 ID.Kind = ValID::t_Constant;
2352 break;
2353 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002354 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002355 ID.Kind = ValID::t_Constant;
2356 break;
2357 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2358 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2359 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002360
Chris Lattnerac161bf2009-01-02 07:01:27 +00002361 case lltok::lbrace: {
2362 // ValID ::= '{' ConstVector '}'
2363 Lex.Lex();
2364 SmallVector<Constant*, 16> Elts;
2365 if (ParseGlobalValueVector(Elts) ||
2366 ParseToken(lltok::rbrace, "expected end of struct constant"))
2367 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002368
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002369 ID.ConstantStructElts = new Constant*[Elts.size()];
2370 ID.UIntVal = Elts.size();
2371 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2372 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002373 return false;
2374 }
2375 case lltok::less: {
2376 // ValID ::= '<' ConstVector '>' --> Vector.
2377 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2378 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002379 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002380
Chris Lattnerac161bf2009-01-02 07:01:27 +00002381 SmallVector<Constant*, 16> Elts;
2382 LocTy FirstEltLoc = Lex.getLoc();
2383 if (ParseGlobalValueVector(Elts) ||
2384 (isPackedStruct &&
2385 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2386 ParseToken(lltok::greater, "expected end of constant"))
2387 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002388
Chris Lattnerac161bf2009-01-02 07:01:27 +00002389 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002390 ID.ConstantStructElts = new Constant*[Elts.size()];
2391 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2392 ID.UIntVal = Elts.size();
2393 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002394 return false;
2395 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002396
Chris Lattnerac161bf2009-01-02 07:01:27 +00002397 if (Elts.empty())
2398 return Error(ID.Loc, "constant vector must not be empty");
2399
Duncan Sands9dff9be2010-02-15 16:12:20 +00002400 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002401 !Elts[0]->getType()->isFloatingPointTy() &&
2402 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002403 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002404 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002405
Chris Lattnerac161bf2009-01-02 07:01:27 +00002406 // Verify that all the vector elements have the same type.
2407 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2408 if (Elts[i]->getType() != Elts[0]->getType())
2409 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002410 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002411 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002412
Chris Lattner69229312011-02-15 00:14:00 +00002413 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002414 ID.Kind = ValID::t_Constant;
2415 return false;
2416 }
2417 case lltok::lsquare: { // Array Constant
2418 Lex.Lex();
2419 SmallVector<Constant*, 16> Elts;
2420 LocTy FirstEltLoc = Lex.getLoc();
2421 if (ParseGlobalValueVector(Elts) ||
2422 ParseToken(lltok::rsquare, "expected end of array constant"))
2423 return true;
2424
2425 // Handle empty element.
2426 if (Elts.empty()) {
2427 // Use undef instead of an array because it's inconvenient to determine
2428 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002429 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002430 return false;
2431 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002432
Chris Lattnerac161bf2009-01-02 07:01:27 +00002433 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002434 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002435 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002436
Owen Anderson4056ca92009-07-29 22:17:13 +00002437 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002438
Chris Lattnerac161bf2009-01-02 07:01:27 +00002439 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002440 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002441 if (Elts[i]->getType() != Elts[0]->getType())
2442 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002443 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002444 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002445 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002446
Jay Foad83be3612011-06-22 09:24:39 +00002447 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002448 ID.Kind = ValID::t_Constant;
2449 return false;
2450 }
2451 case lltok::kw_c: // c "foo"
2452 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002453 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2454 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002455 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2456 ID.Kind = ValID::t_Constant;
2457 return false;
2458
2459 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002460 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2461 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002462 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002463 Lex.Lex();
2464 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002465 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002466 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002467 ParseStringConstant(ID.StrVal) ||
2468 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002469 ParseToken(lltok::StringConstant, "expected constraint string"))
2470 return true;
2471 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002472 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002473 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002474 ID.Kind = ValID::t_InlineAsm;
2475 return false;
2476 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002477
Chris Lattner3432c622009-10-28 03:39:23 +00002478 case lltok::kw_blockaddress: {
2479 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2480 Lex.Lex();
2481
2482 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002483
Chris Lattner3432c622009-10-28 03:39:23 +00002484 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2485 ParseValID(Fn) ||
2486 ParseToken(lltok::comma, "expected comma in block address expression")||
2487 ParseValID(Label) ||
2488 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2489 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002490
Chris Lattner3432c622009-10-28 03:39:23 +00002491 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2492 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002493 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002494 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002495
Chris Lattner3432c622009-10-28 03:39:23 +00002496 // Make a global variable as a placeholder for this reference.
2497 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2498 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002499 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002500 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2501 ID.ConstantVal = FwdRef;
2502 ID.Kind = ValID::t_Constant;
2503 return false;
2504 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002505
Chris Lattnerac161bf2009-01-02 07:01:27 +00002506 case lltok::kw_trunc:
2507 case lltok::kw_zext:
2508 case lltok::kw_sext:
2509 case lltok::kw_fptrunc:
2510 case lltok::kw_fpext:
2511 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002512 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002513 case lltok::kw_uitofp:
2514 case lltok::kw_sitofp:
2515 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002516 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002517 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002518 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002519 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002520 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002521 Constant *SrcVal;
2522 Lex.Lex();
2523 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2524 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002525 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002526 ParseType(DestTy) ||
2527 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2528 return true;
2529 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2530 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002531 getTypeString(SrcVal->getType()) + "' to '" +
2532 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002533 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002534 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002535 ID.Kind = ValID::t_Constant;
2536 return false;
2537 }
2538 case lltok::kw_extractvalue: {
2539 Lex.Lex();
2540 Constant *Val;
2541 SmallVector<unsigned, 4> Indices;
2542 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2543 ParseGlobalTypeAndValue(Val) ||
2544 ParseIndexList(Indices) ||
2545 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2546 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002547
Chris Lattner392be582010-02-12 20:49:41 +00002548 if (!Val->getType()->isAggregateType())
2549 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002550 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002551 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002552 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002553 ID.Kind = ValID::t_Constant;
2554 return false;
2555 }
2556 case lltok::kw_insertvalue: {
2557 Lex.Lex();
2558 Constant *Val0, *Val1;
2559 SmallVector<unsigned, 4> Indices;
2560 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2561 ParseGlobalTypeAndValue(Val0) ||
2562 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2563 ParseGlobalTypeAndValue(Val1) ||
2564 ParseIndexList(Indices) ||
2565 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2566 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002567 if (!Val0->getType()->isAggregateType())
2568 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002569 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002570 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002571 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002572 ID.Kind = ValID::t_Constant;
2573 return false;
2574 }
2575 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002576 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002577 unsigned PredVal, Opc = Lex.getUIntVal();
2578 Constant *Val0, *Val1;
2579 Lex.Lex();
2580 if (ParseCmpPredicate(PredVal, Opc) ||
2581 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2582 ParseGlobalTypeAndValue(Val0) ||
2583 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2584 ParseGlobalTypeAndValue(Val1) ||
2585 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2586 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002587
Chris Lattnerac161bf2009-01-02 07:01:27 +00002588 if (Val0->getType() != Val1->getType())
2589 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002590
Chris Lattnerac161bf2009-01-02 07:01:27 +00002591 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002592
Chris Lattnerac161bf2009-01-02 07:01:27 +00002593 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002594 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002595 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002596 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002597 } else {
2598 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002599 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002600 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002601 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002602 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002603 }
2604 ID.Kind = ValID::t_Constant;
2605 return false;
2606 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002607
Chris Lattnerac161bf2009-01-02 07:01:27 +00002608 // Binary Operators.
2609 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002610 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002611 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002612 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002613 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002614 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002615 case lltok::kw_udiv:
2616 case lltok::kw_sdiv:
2617 case lltok::kw_fdiv:
2618 case lltok::kw_urem:
2619 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002620 case lltok::kw_frem:
2621 case lltok::kw_shl:
2622 case lltok::kw_lshr:
2623 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002624 bool NUW = false;
2625 bool NSW = false;
2626 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002627 unsigned Opc = Lex.getUIntVal();
2628 Constant *Val0, *Val1;
2629 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002630 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002631 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2632 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002633 if (EatIfPresent(lltok::kw_nuw))
2634 NUW = true;
2635 if (EatIfPresent(lltok::kw_nsw)) {
2636 NSW = true;
2637 if (EatIfPresent(lltok::kw_nuw))
2638 NUW = true;
2639 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002640 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2641 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002642 if (EatIfPresent(lltok::kw_exact))
2643 Exact = true;
2644 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002645 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2646 ParseGlobalTypeAndValue(Val0) ||
2647 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2648 ParseGlobalTypeAndValue(Val1) ||
2649 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2650 return true;
2651 if (Val0->getType() != Val1->getType())
2652 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002653 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002654 if (NUW)
2655 return Error(ModifierLoc, "nuw only applies to integer operations");
2656 if (NSW)
2657 return Error(ModifierLoc, "nsw only applies to integer operations");
2658 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002659 // Check that the type is valid for the operator.
2660 switch (Opc) {
2661 case Instruction::Add:
2662 case Instruction::Sub:
2663 case Instruction::Mul:
2664 case Instruction::UDiv:
2665 case Instruction::SDiv:
2666 case Instruction::URem:
2667 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002668 case Instruction::Shl:
2669 case Instruction::AShr:
2670 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002671 if (!Val0->getType()->isIntOrIntVectorTy())
2672 return Error(ID.Loc, "constexpr requires integer operands");
2673 break;
2674 case Instruction::FAdd:
2675 case Instruction::FSub:
2676 case Instruction::FMul:
2677 case Instruction::FDiv:
2678 case Instruction::FRem:
2679 if (!Val0->getType()->isFPOrFPVectorTy())
2680 return Error(ID.Loc, "constexpr requires fp operands");
2681 break;
2682 default: llvm_unreachable("Unknown binary operator!");
2683 }
Dan Gohman1b849082009-09-07 23:54:19 +00002684 unsigned Flags = 0;
2685 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2686 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002687 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002688 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002689 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002690 ID.Kind = ValID::t_Constant;
2691 return false;
2692 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002693
Chris Lattnerac161bf2009-01-02 07:01:27 +00002694 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002695 case lltok::kw_and:
2696 case lltok::kw_or:
2697 case lltok::kw_xor: {
2698 unsigned Opc = Lex.getUIntVal();
2699 Constant *Val0, *Val1;
2700 Lex.Lex();
2701 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2702 ParseGlobalTypeAndValue(Val0) ||
2703 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2704 ParseGlobalTypeAndValue(Val1) ||
2705 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2706 return true;
2707 if (Val0->getType() != Val1->getType())
2708 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002709 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002710 return Error(ID.Loc,
2711 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002712 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002713 ID.Kind = ValID::t_Constant;
2714 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002715 }
2716
Chris Lattnerac161bf2009-01-02 07:01:27 +00002717 case lltok::kw_getelementptr:
2718 case lltok::kw_shufflevector:
2719 case lltok::kw_insertelement:
2720 case lltok::kw_extractelement:
2721 case lltok::kw_select: {
2722 unsigned Opc = Lex.getUIntVal();
2723 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002724 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002725 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002726 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002727 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002728 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2729 ParseGlobalValueVector(Elts) ||
2730 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2731 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002732
Chris Lattnerac161bf2009-01-02 07:01:27 +00002733 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002734 if (Elts.size() == 0 ||
2735 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002736 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002737
Jay Foaded8db7d2011-07-21 14:31:17 +00002738 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002739 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002740 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002741 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2742 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002743 } else if (Opc == Instruction::Select) {
2744 if (Elts.size() != 3)
2745 return Error(ID.Loc, "expected three operands to select");
2746 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2747 Elts[2]))
2748 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002749 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002750 } else if (Opc == Instruction::ShuffleVector) {
2751 if (Elts.size() != 3)
2752 return Error(ID.Loc, "expected three operands to shufflevector");
2753 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2754 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002755 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002756 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002757 } else if (Opc == Instruction::ExtractElement) {
2758 if (Elts.size() != 2)
2759 return Error(ID.Loc, "expected two operands to extractelement");
2760 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2761 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002762 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002763 } else {
2764 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2765 if (Elts.size() != 3)
2766 return Error(ID.Loc, "expected three operands to insertelement");
2767 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2768 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002769 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002770 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002771 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002772
Chris Lattnerac161bf2009-01-02 07:01:27 +00002773 ID.Kind = ValID::t_Constant;
2774 return false;
2775 }
2776 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002777
Chris Lattnerac161bf2009-01-02 07:01:27 +00002778 Lex.Lex();
2779 return false;
2780}
2781
2782/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002783bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002784 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002785 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002786 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002787 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002788 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002789 if (V && !(C = dyn_cast<Constant>(V)))
2790 return Error(ID.Loc, "global values must be constants");
2791 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002792}
2793
Victor Hernandez9d75c962010-01-11 22:31:58 +00002794bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002795 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002796 return ParseType(Ty) ||
2797 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002798}
2799
2800/// ParseGlobalValueVector
2801/// ::= /*empty*/
2802/// ::= TypeAndValue (',' TypeAndValue)*
2803bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2804 // Empty list.
2805 if (Lex.getKind() == lltok::rbrace ||
2806 Lex.getKind() == lltok::rsquare ||
2807 Lex.getKind() == lltok::greater ||
2808 Lex.getKind() == lltok::rparen)
2809 return false;
2810
2811 Constant *C;
2812 if (ParseGlobalTypeAndValue(C)) return true;
2813 Elts.push_back(C);
2814
2815 while (EatIfPresent(lltok::comma)) {
2816 if (ParseGlobalTypeAndValue(C)) return true;
2817 Elts.push_back(C);
2818 }
2819
2820 return false;
2821}
2822
Dan Gohmanc828c542010-08-24 02:24:03 +00002823bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2824 assert(Lex.getKind() == lltok::lbrace);
2825 Lex.Lex();
2826
2827 SmallVector<Value*, 16> Elts;
2828 if (ParseMDNodeVector(Elts, PFS) ||
2829 ParseToken(lltok::rbrace, "expected end of metadata node"))
2830 return true;
2831
Jay Foad5514afe2011-04-21 19:59:31 +00002832 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002833 ID.Kind = ValID::t_MDNode;
2834 return false;
2835}
2836
Dan Gohman8939ba332010-07-14 18:26:50 +00002837/// ParseMetadataValue
2838/// ::= !42
2839/// ::= !{...}
2840/// ::= !"string"
2841bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2842 assert(Lex.getKind() == lltok::exclaim);
2843 Lex.Lex();
2844
2845 // MDNode:
2846 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002847 if (Lex.getKind() == lltok::lbrace)
2848 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002849
2850 // Standalone metadata reference
2851 // !42
2852 if (Lex.getKind() == lltok::APSInt) {
2853 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2854 ID.Kind = ValID::t_MDNode;
2855 return false;
2856 }
2857
2858 // MDString:
2859 // ::= '!' STRINGCONSTANT
2860 if (ParseMDString(ID.MDStringVal)) return true;
2861 ID.Kind = ValID::t_MDString;
2862 return false;
2863}
2864
Victor Hernandez9d75c962010-01-11 22:31:58 +00002865
2866//===----------------------------------------------------------------------===//
2867// Function Parsing.
2868//===----------------------------------------------------------------------===//
2869
Chris Lattner229907c2011-07-18 04:54:35 +00002870bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002871 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002872 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002873 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002874
Chris Lattnerac161bf2009-01-02 07:01:27 +00002875 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002876 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002877 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2878 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002879 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002880 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002881 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2882 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002883 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002884 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002885 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002886 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002887 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002888 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2889 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002890 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002891 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002892 return false;
2893 }
2894 case ValID::t_MDNode:
2895 if (!Ty->isMetadataTy())
2896 return Error(ID.Loc, "metadata value must have metadata type");
2897 V = ID.MDNodeVal;
2898 return false;
2899 case ValID::t_MDString:
2900 if (!Ty->isMetadataTy())
2901 return Error(ID.Loc, "metadata value must have metadata type");
2902 V = ID.MDStringVal;
2903 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002904 case ValID::t_GlobalName:
2905 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002906 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002907 case ValID::t_GlobalID:
2908 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002909 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002910 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002911 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002912 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002913 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002914 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002915 return false;
2916 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002917 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002918 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2919 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002920
Dan Gohman518cda42011-12-17 00:04:22 +00002921 // The lexer has no type info, so builds all half, float, and double FP
2922 // constants as double. Fix this here. Long double does not need this.
2923 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002924 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002925 if (Ty->isHalfTy())
2926 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2927 &Ignored);
2928 else if (Ty->isFloatTy())
2929 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2930 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002931 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002932 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002933
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002934 if (V->getType() != Ty)
2935 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002936 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002937
Chris Lattnerac161bf2009-01-02 07:01:27 +00002938 return false;
2939 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002940 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002941 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002942 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002943 return false;
2944 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002945 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002946 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002947 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002948 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002949 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002950 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002951 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002952 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002953 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002954 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002955 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002956 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002957 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002958 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002959 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002960 return false;
2961 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002962 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002963 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002964
Chris Lattnerac161bf2009-01-02 07:01:27 +00002965 V = ID.ConstantVal;
2966 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002967 case ValID::t_ConstantStruct:
2968 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002969 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002970 if (ST->getNumElements() != ID.UIntVal)
2971 return Error(ID.Loc,
2972 "initializer with struct type has wrong # elements");
2973 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2974 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002975
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002976 // Verify that the elements are compatible with the structtype.
2977 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2978 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2979 return Error(ID.Loc, "element " + Twine(i) +
2980 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002981
Frits van Bommel717d7ed2011-07-18 12:00:32 +00002982 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2983 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002984 } else
2985 return Error(ID.Loc, "constant expression type mismatch");
2986 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002987 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00002988 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002989}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002990
Chris Lattner229907c2011-07-18 04:54:35 +00002991bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002992 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002993 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002994 return ParseValID(ID, PFS) ||
2995 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002996}
2997
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002998bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002999 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003000 return ParseType(Ty) ||
3001 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003002}
3003
Chris Lattner3ed871f2009-10-27 19:13:16 +00003004bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
3005 PerFunctionState &PFS) {
3006 Value *V;
3007 Loc = Lex.getLoc();
3008 if (ParseTypeAndValue(V, PFS)) return true;
3009 if (!isa<BasicBlock>(V))
3010 return Error(Loc, "expected a basic block");
3011 BB = cast<BasicBlock>(V);
3012 return false;
3013}
3014
3015
Chris Lattnerac161bf2009-01-02 07:01:27 +00003016/// FunctionHeader
3017/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00003018/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003019/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00003020bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
3021 // Parse the linkage.
3022 LocTy LinkageLoc = Lex.getLoc();
3023 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003024
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00003025 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00003026 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00003027 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003028 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003029 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003030 LocTy RetTypeLoc = Lex.getLoc();
3031 if (ParseOptionalLinkage(Linkage) ||
3032 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003033 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003034 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003035 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003036 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003037 return true;
3038
3039 // Verify that the linkage is ok.
3040 switch ((GlobalValue::LinkageTypes)Linkage) {
3041 case GlobalValue::ExternalLinkage:
3042 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003043 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003044 if (isDefine)
3045 return Error(LinkageLoc, "invalid linkage for function definition");
3046 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003047 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003048 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003049 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003050 case GlobalValue::LinkOnceAnyLinkage:
3051 case GlobalValue::LinkOnceODRLinkage:
3052 case GlobalValue::WeakAnyLinkage:
3053 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003054 if (!isDefine)
3055 return Error(LinkageLoc, "invalid linkage for function declaration");
3056 break;
3057 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003058 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003059 return Error(LinkageLoc, "invalid function linkage type");
3060 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003061
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003062 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3063 return Error(LinkageLoc,
3064 "symbol with local linkage must have default visibility");
3065
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003066 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003067 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003068
Chris Lattnerac161bf2009-01-02 07:01:27 +00003069 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003070
3071 std::string FunctionName;
3072 if (Lex.getKind() == lltok::GlobalVar) {
3073 FunctionName = Lex.getStrVal();
3074 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3075 unsigned NameID = Lex.getUIntVal();
3076
3077 if (NameID != NumberedVals.size())
3078 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003079 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003080 } else {
3081 return TokError("expected function name");
3082 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003083
Chris Lattner3822f632009-01-02 08:05:26 +00003084 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003085
Chris Lattner3822f632009-01-02 08:05:26 +00003086 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003087 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003088
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003089 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003090 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003091 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003092 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003093 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003094 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003095 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003096 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003097 bool UnnamedAddr;
3098 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003099 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003100
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003101 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003102 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3103 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003104 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003105 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003106 (EatIfPresent(lltok::kw_section) &&
3107 ParseStringConstant(Section)) ||
3108 ParseOptionalAlignment(Alignment) ||
3109 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003110 ParseStringConstant(GC)) ||
3111 (EatIfPresent(lltok::kw_prefix) &&
3112 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003113 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003114
Michael Gottesman41748d72013-06-27 00:25:01 +00003115 if (FuncAttrs.contains(Attribute::Builtin))
3116 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003117
Chris Lattnerac161bf2009-01-02 07:01:27 +00003118 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003119 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003120 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003121 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003122 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003123
Chris Lattnerac161bf2009-01-02 07:01:27 +00003124 // Okay, if we got here, the function is syntactically valid. Convert types
3125 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003126 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003127 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003128
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003129 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003130 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3131 AttributeSet::ReturnIndex,
3132 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003133
Chris Lattnerac161bf2009-01-02 07:01:27 +00003134 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003135 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003136 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3137 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003138 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3139 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003140 }
3141
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003142 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003143 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3144 AttributeSet::FunctionIndex,
3145 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003146
Bill Wendlinge94d8432012-12-07 23:16:57 +00003147 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003148
Bill Wendling749a43d2012-12-30 13:50:49 +00003149 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003150 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3151
Chris Lattner229907c2011-07-18 04:54:35 +00003152 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003153 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003154 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003155
Craig Topper2617dcc2014-04-15 06:32:26 +00003156 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003157 if (!FunctionName.empty()) {
3158 // If this was a definition of a forward reference, remove the definition
3159 // from the forward reference table and fill in the forward ref.
3160 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3161 ForwardRefVals.find(FunctionName);
3162 if (FRVI != ForwardRefVals.end()) {
3163 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003164 if (!Fn)
3165 return Error(FRVI->second.second, "invalid forward reference to "
3166 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003167 if (Fn->getType() != PFT)
3168 return Error(FRVI->second.second, "invalid forward reference to "
3169 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003170
Chris Lattnerac161bf2009-01-02 07:01:27 +00003171 ForwardRefVals.erase(FRVI);
3172 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003173 // Reject redefinitions.
3174 return Error(NameLoc, "invalid redefinition of function '" +
3175 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003176 } else if (M->getNamedValue(FunctionName)) {
3177 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003178 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003179
Dan Gohman399d6ae2009-08-29 23:37:49 +00003180 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003181 // If this is a definition of a forward referenced function, make sure the
3182 // types agree.
3183 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3184 = ForwardRefValIDs.find(NumberedVals.size());
3185 if (I != ForwardRefValIDs.end()) {
3186 Fn = cast<Function>(I->second.first);
3187 if (Fn->getType() != PFT)
3188 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003189 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003190 ForwardRefValIDs.erase(I);
3191 }
3192 }
3193
Craig Topper2617dcc2014-04-15 06:32:26 +00003194 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003195 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3196 else // Move the forward-reference to the correct spot in the module.
3197 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3198
3199 if (FunctionName.empty())
3200 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003201
Chris Lattnerac161bf2009-01-02 07:01:27 +00003202 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3203 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003204 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003205 Fn->setCallingConv(CC);
3206 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003207 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003208 Fn->setAlignment(Alignment);
3209 Fn->setSection(Section);
3210 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003211 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003212 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003213
Chris Lattnerac161bf2009-01-02 07:01:27 +00003214 // Add all of the arguments we parsed to the function.
3215 Function::arg_iterator ArgIt = Fn->arg_begin();
3216 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3217 // If the argument has a name, insert it into the argument symbol table.
3218 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003219
Chris Lattnerac161bf2009-01-02 07:01:27 +00003220 // Set the name, if it conflicted, it will be auto-renamed.
3221 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003222
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003223 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003224 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3225 ArgList[i].Name + "'");
3226 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003227
Chris Lattnerac161bf2009-01-02 07:01:27 +00003228 return false;
3229}
3230
3231
3232/// ParseFunctionBody
3233/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003234///
3235bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003236 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003237 return TokError("expected '{' in function body");
3238 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003239
Chris Lattner3432c622009-10-28 03:39:23 +00003240 int FunctionNumber = -1;
3241 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003242
Chris Lattner3432c622009-10-28 03:39:23 +00003243 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003244
Chris Lattnerbbddd962010-01-09 19:20:07 +00003245 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003246 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003247 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003248
Chris Lattner4649a732011-06-17 06:42:57 +00003249 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003250 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003251
Chris Lattnerac161bf2009-01-02 07:01:27 +00003252 // Eat the }.
3253 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003254
Chris Lattnerac161bf2009-01-02 07:01:27 +00003255 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003256 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003257}
3258
3259/// ParseBasicBlock
3260/// ::= LabelStr? Instruction*
3261bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3262 // If this basic block starts out with a name, remember it.
3263 std::string Name;
3264 LocTy NameLoc = Lex.getLoc();
3265 if (Lex.getKind() == lltok::LabelStr) {
3266 Name = Lex.getStrVal();
3267 Lex.Lex();
3268 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003269
Chris Lattnerac161bf2009-01-02 07:01:27 +00003270 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003271 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003272
Chris Lattnerac161bf2009-01-02 07:01:27 +00003273 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003274
Chris Lattnerac161bf2009-01-02 07:01:27 +00003275 // Parse the instructions in this block until we get a terminator.
3276 Instruction *Inst;
3277 do {
3278 // This instruction may have three possibilities for a name: a) none
3279 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3280 LocTy NameLoc = Lex.getLoc();
3281 int NameID = -1;
3282 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003283
Chris Lattnerac161bf2009-01-02 07:01:27 +00003284 if (Lex.getKind() == lltok::LocalVarID) {
3285 NameID = Lex.getUIntVal();
3286 Lex.Lex();
3287 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3288 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003289 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003290 NameStr = Lex.getStrVal();
3291 Lex.Lex();
3292 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3293 return true;
3294 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003295
Chris Lattner77b89dc2009-12-30 05:23:43 +00003296 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003297 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003298 case InstError: return true;
3299 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003300 BB->getInstList().push_back(Inst);
3301
Chris Lattner77b89dc2009-12-30 05:23:43 +00003302 // With a normal result, we check to see if the instruction is followed by
3303 // a comma and metadata.
3304 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003305 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003306 return true;
3307 break;
3308 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003309 BB->getInstList().push_back(Inst);
3310
Chris Lattner77b89dc2009-12-30 05:23:43 +00003311 // If the instruction parser ate an extra comma at the end of it, it
3312 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003313 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003314 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003315 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003316 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003317
Chris Lattnerac161bf2009-01-02 07:01:27 +00003318 // Set the name on the instruction.
3319 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3320 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003321
Chris Lattnerac161bf2009-01-02 07:01:27 +00003322 return false;
3323}
3324
3325//===----------------------------------------------------------------------===//
3326// Instruction Parsing.
3327//===----------------------------------------------------------------------===//
3328
3329/// ParseInstruction - Parse one of the many different instructions.
3330///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003331int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3332 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003333 lltok::Kind Token = Lex.getKind();
3334 if (Token == lltok::Eof)
3335 return TokError("found end of file when expecting more instructions");
3336 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003337 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003338 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003339
Chris Lattnerac161bf2009-01-02 07:01:27 +00003340 switch (Token) {
3341 default: return Error(Loc, "expected instruction opcode");
3342 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003343 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003344 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3345 case lltok::kw_br: return ParseBr(Inst, PFS);
3346 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003347 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003348 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003349 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003350 // Binary Operators.
3351 case lltok::kw_add:
3352 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003353 case lltok::kw_mul:
3354 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003355 bool NUW = EatIfPresent(lltok::kw_nuw);
3356 bool NSW = EatIfPresent(lltok::kw_nsw);
3357 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003358
Chris Lattnera676c0f2011-02-07 16:40:21 +00003359 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003360
Chris Lattnera676c0f2011-02-07 16:40:21 +00003361 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3362 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3363 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003364 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003365 case lltok::kw_fadd:
3366 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003367 case lltok::kw_fmul:
3368 case lltok::kw_fdiv:
3369 case lltok::kw_frem: {
3370 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3371 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3372 if (Res != 0)
3373 return Res;
3374 if (FMF.any())
3375 Inst->setFastMathFlags(FMF);
3376 return 0;
3377 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003378
Chris Lattner35315d02011-02-06 21:44:57 +00003379 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003380 case lltok::kw_udiv:
3381 case lltok::kw_lshr:
3382 case lltok::kw_ashr: {
3383 bool Exact = EatIfPresent(lltok::kw_exact);
3384
3385 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3386 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3387 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003388 }
3389
Chris Lattnerac161bf2009-01-02 07:01:27 +00003390 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003391 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003392 case lltok::kw_and:
3393 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003394 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003395 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003396 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003397 // Casts.
3398 case lltok::kw_trunc:
3399 case lltok::kw_zext:
3400 case lltok::kw_sext:
3401 case lltok::kw_fptrunc:
3402 case lltok::kw_fpext:
3403 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003404 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003405 case lltok::kw_uitofp:
3406 case lltok::kw_sitofp:
3407 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003408 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003409 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003410 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003411 // Other.
3412 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003413 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003414 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3415 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3416 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3417 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003418 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003419 // Call.
3420 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3421 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3422 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003423 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003424 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003425 case lltok::kw_load: return ParseLoad(Inst, PFS);
3426 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003427 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3428 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003429 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003430 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3431 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3432 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3433 }
3434}
3435
3436/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3437bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003438 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003439 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003440 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003441 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3442 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3443 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3444 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3445 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3446 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3447 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3448 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3449 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3450 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3451 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3452 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3453 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3454 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3455 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3456 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3457 }
3458 } else {
3459 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003460 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003461 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3462 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3463 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3464 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3465 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3466 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3467 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3468 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3469 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3470 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3471 }
3472 }
3473 Lex.Lex();
3474 return false;
3475}
3476
3477//===----------------------------------------------------------------------===//
3478// Terminator Instructions.
3479//===----------------------------------------------------------------------===//
3480
3481/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003482/// ::= 'ret' void (',' !dbg, !1)*
3483/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003484bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003485 PerFunctionState &PFS) {
3486 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003487 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003488 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003489
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003490 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003491
Chris Lattnerfdd87902009-10-05 05:54:46 +00003492 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003493 if (!ResType->isVoidTy())
3494 return Error(TypeLoc, "value doesn't match function result type '" +
3495 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003496
Owen Anderson55f1c092009-08-13 21:58:54 +00003497 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003498 return false;
3499 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003500
Chris Lattnerac161bf2009-01-02 07:01:27 +00003501 Value *RV;
3502 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003503
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003504 if (ResType != RV->getType())
3505 return Error(TypeLoc, "value doesn't match function result type '" +
3506 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003507
Owen Anderson55f1c092009-08-13 21:58:54 +00003508 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003509 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003510}
3511
3512
3513/// ParseBr
3514/// ::= 'br' TypeAndValue
3515/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3516bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3517 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003518 Value *Op0;
3519 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003520 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003521
Chris Lattnerac161bf2009-01-02 07:01:27 +00003522 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3523 Inst = BranchInst::Create(BB);
3524 return false;
3525 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003526
Owen Anderson55f1c092009-08-13 21:58:54 +00003527 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003528 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003529
Chris Lattnerac161bf2009-01-02 07:01:27 +00003530 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003531 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003532 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003533 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003534 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003535
Chris Lattner3ed871f2009-10-27 19:13:16 +00003536 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003537 return false;
3538}
3539
3540/// ParseSwitch
3541/// Instruction
3542/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3543/// JumpTable
3544/// ::= (TypeAndValue ',' TypeAndValue)*
3545bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3546 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003547 Value *Cond;
3548 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003549 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3550 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003551 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003552 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3553 return true;
3554
Duncan Sands19d0b472010-02-16 11:11:14 +00003555 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003556 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003557
Chris Lattnerac161bf2009-01-02 07:01:27 +00003558 // Parse the jump table pairs.
3559 SmallPtrSet<Value*, 32> SeenCases;
3560 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3561 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003562 Value *Constant;
3563 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003564
Chris Lattnerac161bf2009-01-02 07:01:27 +00003565 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3566 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003567 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003568 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003569
Chris Lattnerac161bf2009-01-02 07:01:27 +00003570 if (!SeenCases.insert(Constant))
3571 return Error(CondLoc, "duplicate case value in switch");
3572 if (!isa<ConstantInt>(Constant))
3573 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003574
Chris Lattner3ed871f2009-10-27 19:13:16 +00003575 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003576 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003577
Chris Lattnerac161bf2009-01-02 07:01:27 +00003578 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003579
Chris Lattner3ed871f2009-10-27 19:13:16 +00003580 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003581 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3582 SI->addCase(Table[i].first, Table[i].second);
3583 Inst = SI;
3584 return false;
3585}
3586
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003587/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003588/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003589/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3590bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003591 LocTy AddrLoc;
3592 Value *Address;
3593 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003594 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3595 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003596 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003597
Duncan Sands19d0b472010-02-16 11:11:14 +00003598 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003599 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003600
Chris Lattner3ed871f2009-10-27 19:13:16 +00003601 // Parse the destination list.
3602 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003603
Chris Lattner3ed871f2009-10-27 19:13:16 +00003604 if (Lex.getKind() != lltok::rsquare) {
3605 BasicBlock *DestBB;
3606 if (ParseTypeAndBasicBlock(DestBB, PFS))
3607 return true;
3608 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003609
Chris Lattner3ed871f2009-10-27 19:13:16 +00003610 while (EatIfPresent(lltok::comma)) {
3611 if (ParseTypeAndBasicBlock(DestBB, PFS))
3612 return true;
3613 DestList.push_back(DestBB);
3614 }
3615 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003616
Chris Lattner3ed871f2009-10-27 19:13:16 +00003617 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3618 return true;
3619
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003620 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003621 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3622 IBI->addDestination(DestList[i]);
3623 Inst = IBI;
3624 return false;
3625}
3626
3627
Chris Lattnerac161bf2009-01-02 07:01:27 +00003628/// ParseInvoke
3629/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3630/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3631bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3632 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003633 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003634 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003635 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003636 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003637 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003638 LocTy RetTypeLoc;
3639 ValID CalleeID;
3640 SmallVector<ParamInfo, 16> ArgList;
3641
Chris Lattner3ed871f2009-10-27 19:13:16 +00003642 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003643 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003644 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003645 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003646 ParseValID(CalleeID) ||
3647 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003648 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3649 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003650 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003651 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003652 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003653 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003654 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003655
Chris Lattnerac161bf2009-01-02 07:01:27 +00003656 // If RetType is a non-function pointer type, then this is the short syntax
3657 // for the call, which means that RetType is just the return type. Infer the
3658 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003659 PointerType *PFTy = nullptr;
3660 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003661 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3662 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3663 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003664 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003665 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3666 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003667
Chris Lattnerac161bf2009-01-02 07:01:27 +00003668 if (!FunctionType::isValidReturnType(RetType))
3669 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003670
Owen Anderson4056ca92009-07-29 22:17:13 +00003671 Ty = FunctionType::get(RetType, ParamTypes, false);
3672 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003673 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003674
Chris Lattnerac161bf2009-01-02 07:01:27 +00003675 // Look up the callee.
3676 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003677 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003678
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003679 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003680 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003681 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003682 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3683 AttributeSet::ReturnIndex,
3684 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003685
Chris Lattnerac161bf2009-01-02 07:01:27 +00003686 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003687
Chris Lattnerac161bf2009-01-02 07:01:27 +00003688 // Loop through FunctionType's arguments and ensure they are specified
3689 // correctly. Also, gather any parameter attributes.
3690 FunctionType::param_iterator I = Ty->param_begin();
3691 FunctionType::param_iterator E = Ty->param_end();
3692 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003693 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003694 if (I != E) {
3695 ExpectedTy = *I++;
3696 } else if (!Ty->isVarArg()) {
3697 return Error(ArgList[i].Loc, "too many arguments specified");
3698 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003699
Chris Lattnerac161bf2009-01-02 07:01:27 +00003700 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3701 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003702 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003703 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003704 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3705 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003706 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3707 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003708 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003709
Chris Lattnerac161bf2009-01-02 07:01:27 +00003710 if (I != E)
3711 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003712
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003713 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003714 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3715 AttributeSet::FunctionIndex,
3716 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003717
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003718 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003719 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003720
Jay Foad5bd375a2011-07-15 08:37:34 +00003721 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003722 II->setCallingConv(CC);
3723 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003724 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003725 Inst = II;
3726 return false;
3727}
3728
Bill Wendlingf891bf82011-07-31 06:30:59 +00003729/// ParseResume
3730/// ::= 'resume' TypeAndValue
3731bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3732 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003733 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3734 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003735
Bill Wendlingf891bf82011-07-31 06:30:59 +00003736 ResumeInst *RI = ResumeInst::Create(Exn);
3737 Inst = RI;
3738 return false;
3739}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003740
3741//===----------------------------------------------------------------------===//
3742// Binary Operators.
3743//===----------------------------------------------------------------------===//
3744
3745/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003746/// ::= ArithmeticOps TypeAndValue ',' Value
3747///
3748/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3749/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003750bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003751 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003752 LocTy Loc; Value *LHS, *RHS;
3753 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3754 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3755 ParseValue(LHS->getType(), RHS, PFS))
3756 return true;
3757
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003758 bool Valid;
3759 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003760 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003761 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003762 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3763 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003764 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003765 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3766 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003767 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003768
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003769 if (!Valid)
3770 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003771
Chris Lattnerac161bf2009-01-02 07:01:27 +00003772 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3773 return false;
3774}
3775
3776/// ParseLogical
3777/// ::= ArithmeticOps TypeAndValue ',' Value {
3778bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3779 unsigned Opc) {
3780 LocTy Loc; Value *LHS, *RHS;
3781 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3782 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3783 ParseValue(LHS->getType(), RHS, PFS))
3784 return true;
3785
Duncan Sands9dff9be2010-02-15 16:12:20 +00003786 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003787 return Error(Loc,"instruction requires integer or integer vector operands");
3788
3789 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3790 return false;
3791}
3792
3793
3794/// ParseCompare
3795/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3796/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003797bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3798 unsigned Opc) {
3799 // Parse the integer/fp comparison predicate.
3800 LocTy Loc;
3801 unsigned Pred;
3802 Value *LHS, *RHS;
3803 if (ParseCmpPredicate(Pred, Opc) ||
3804 ParseTypeAndValue(LHS, Loc, PFS) ||
3805 ParseToken(lltok::comma, "expected ',' after compare value") ||
3806 ParseValue(LHS->getType(), RHS, PFS))
3807 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003808
Chris Lattnerac161bf2009-01-02 07:01:27 +00003809 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003810 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003811 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003812 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003813 } else {
3814 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003815 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003816 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003817 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003818 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003819 }
3820 return false;
3821}
3822
3823//===----------------------------------------------------------------------===//
3824// Other Instructions.
3825//===----------------------------------------------------------------------===//
3826
3827
3828/// ParseCast
3829/// ::= CastOpc TypeAndValue 'to' Type
3830bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3831 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003832 LocTy Loc;
3833 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003834 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003835 if (ParseTypeAndValue(Op, Loc, PFS) ||
3836 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3837 ParseType(DestTy))
3838 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003839
Chris Lattner89d856e2009-03-01 00:53:13 +00003840 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3841 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003842 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003843 getTypeString(Op->getType()) + "' to '" +
3844 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003845 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003846 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3847 return false;
3848}
3849
3850/// ParseSelect
3851/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3852bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3853 LocTy Loc;
3854 Value *Op0, *Op1, *Op2;
3855 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3856 ParseToken(lltok::comma, "expected ',' after select condition") ||
3857 ParseTypeAndValue(Op1, PFS) ||
3858 ParseToken(lltok::comma, "expected ',' after select value") ||
3859 ParseTypeAndValue(Op2, PFS))
3860 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003861
Chris Lattnerac161bf2009-01-02 07:01:27 +00003862 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3863 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003864
Chris Lattnerac161bf2009-01-02 07:01:27 +00003865 Inst = SelectInst::Create(Op0, Op1, Op2);
3866 return false;
3867}
3868
Chris Lattnerb55ab542009-01-05 08:18:44 +00003869/// ParseVA_Arg
3870/// ::= 'va_arg' TypeAndValue ',' Type
3871bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003872 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003873 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003874 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003875 if (ParseTypeAndValue(Op, PFS) ||
3876 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003877 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003878 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003879
Chris Lattnerb55ab542009-01-05 08:18:44 +00003880 if (!EltTy->isFirstClassType())
3881 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003882
3883 Inst = new VAArgInst(Op, EltTy);
3884 return false;
3885}
3886
3887/// ParseExtractElement
3888/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3889bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3890 LocTy Loc;
3891 Value *Op0, *Op1;
3892 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3893 ParseToken(lltok::comma, "expected ',' after extract value") ||
3894 ParseTypeAndValue(Op1, PFS))
3895 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003896
Chris Lattnerac161bf2009-01-02 07:01:27 +00003897 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3898 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003899
Eric Christopherc9742252009-07-25 02:28:41 +00003900 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003901 return false;
3902}
3903
3904/// ParseInsertElement
3905/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3906bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3907 LocTy Loc;
3908 Value *Op0, *Op1, *Op2;
3909 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3910 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3911 ParseTypeAndValue(Op1, PFS) ||
3912 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3913 ParseTypeAndValue(Op2, PFS))
3914 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003915
Chris Lattnerac161bf2009-01-02 07:01:27 +00003916 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003917 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003918
Chris Lattnerac161bf2009-01-02 07:01:27 +00003919 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3920 return false;
3921}
3922
3923/// ParseShuffleVector
3924/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3925bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3926 LocTy Loc;
3927 Value *Op0, *Op1, *Op2;
3928 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3929 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3930 ParseTypeAndValue(Op1, PFS) ||
3931 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3932 ParseTypeAndValue(Op2, PFS))
3933 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003934
Chris Lattnerac161bf2009-01-02 07:01:27 +00003935 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003936 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003937
Chris Lattnerac161bf2009-01-02 07:01:27 +00003938 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3939 return false;
3940}
3941
3942/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003943/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003944int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003945 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003946 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003947
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003948 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003949 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3950 ParseValue(Ty, Op0, PFS) ||
3951 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003952 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003953 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3954 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003955
Chris Lattnerf4f03422009-12-30 05:27:33 +00003956 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003957 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3958 while (1) {
3959 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003960
Chris Lattner3822f632009-01-02 08:05:26 +00003961 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003962 break;
3963
Chris Lattnerf4f03422009-12-30 05:27:33 +00003964 if (Lex.getKind() == lltok::MetadataVar) {
3965 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003966 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003967 }
Devang Patel8f842d32009-10-16 18:45:49 +00003968
Chris Lattner3822f632009-01-02 08:05:26 +00003969 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003970 ParseValue(Ty, Op0, PFS) ||
3971 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003972 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003973 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3974 return true;
3975 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003976
Chris Lattnerac161bf2009-01-02 07:01:27 +00003977 if (!Ty->isFirstClassType())
3978 return Error(TypeLoc, "phi node must have first class type");
3979
Jay Foad52131342011-03-30 11:28:46 +00003980 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003981 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3982 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3983 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003984 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003985}
3986
Bill Wendlingfae14752011-08-12 20:24:12 +00003987/// ParseLandingPad
3988/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3989/// Clause
3990/// ::= 'catch' TypeAndValue
3991/// ::= 'filter'
3992/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3993bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003994 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003995 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00003996
3997 if (ParseType(Ty, TyLoc) ||
3998 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3999 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
4000 return true;
4001
4002 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
4003 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
4004
4005 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
4006 LandingPadInst::ClauseType CT;
4007 if (EatIfPresent(lltok::kw_catch))
4008 CT = LandingPadInst::Catch;
4009 else if (EatIfPresent(lltok::kw_filter))
4010 CT = LandingPadInst::Filter;
4011 else
4012 return TokError("expected 'catch' or 'filter' clause type");
4013
4014 Value *V; LocTy VLoc;
4015 if (ParseTypeAndValue(V, VLoc, PFS)) {
4016 delete LP;
4017 return true;
4018 }
4019
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00004020 // A 'catch' type expects a non-array constant. A filter clause expects an
4021 // array constant.
4022 if (CT == LandingPadInst::Catch) {
4023 if (isa<ArrayType>(V->getType()))
4024 Error(VLoc, "'catch' clause has an invalid type");
4025 } else {
4026 if (!isa<ArrayType>(V->getType()))
4027 Error(VLoc, "'filter' clause has an invalid type");
4028 }
4029
Bill Wendlingfae14752011-08-12 20:24:12 +00004030 LP->addClause(V);
4031 }
4032
4033 Inst = LP;
4034 return false;
4035}
4036
Chris Lattnerac161bf2009-01-02 07:01:27 +00004037/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004038/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4039/// ParameterList OptionalAttrs
4040/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4041/// ParameterList OptionalAttrs
4042/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004043/// ParameterList OptionalAttrs
4044bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004045 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004046 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004047 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004048 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004049 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004050 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004051 LocTy RetTypeLoc;
4052 ValID CalleeID;
4053 SmallVector<ParamInfo, 16> ArgList;
4054 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004055
Reid Kleckner5772b772014-04-24 20:14:34 +00004056 if ((TCK != CallInst::TCK_None &&
4057 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004058 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004059 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004060 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004061 ParseValID(CalleeID) ||
4062 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004063 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004064 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004065 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004066
Chris Lattnerac161bf2009-01-02 07:01:27 +00004067 // If RetType is a non-function pointer type, then this is the short syntax
4068 // for the call, which means that RetType is just the return type. Infer the
4069 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004070 PointerType *PFTy = nullptr;
4071 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004072 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4073 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4074 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004075 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004076 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4077 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004078
Chris Lattnerac161bf2009-01-02 07:01:27 +00004079 if (!FunctionType::isValidReturnType(RetType))
4080 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004081
Owen Anderson4056ca92009-07-29 22:17:13 +00004082 Ty = FunctionType::get(RetType, ParamTypes, false);
4083 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004084 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004085
Chris Lattnerac161bf2009-01-02 07:01:27 +00004086 // Look up the callee.
4087 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004088 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004089
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004090 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004091 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004092 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004093 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4094 AttributeSet::ReturnIndex,
4095 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004096
Chris Lattnerac161bf2009-01-02 07:01:27 +00004097 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004098
Chris Lattnerac161bf2009-01-02 07:01:27 +00004099 // Loop through FunctionType's arguments and ensure they are specified
4100 // correctly. Also, gather any parameter attributes.
4101 FunctionType::param_iterator I = Ty->param_begin();
4102 FunctionType::param_iterator E = Ty->param_end();
4103 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004104 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004105 if (I != E) {
4106 ExpectedTy = *I++;
4107 } else if (!Ty->isVarArg()) {
4108 return Error(ArgList[i].Loc, "too many arguments specified");
4109 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004110
Chris Lattnerac161bf2009-01-02 07:01:27 +00004111 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4112 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004113 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004114 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004115 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4116 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004117 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4118 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004119 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004120
Chris Lattnerac161bf2009-01-02 07:01:27 +00004121 if (I != E)
4122 return Error(CallLoc, "not enough parameters specified for call");
4123
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004124 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004125 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4126 AttributeSet::FunctionIndex,
4127 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004128
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004129 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004130 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004131
Jay Foad5bd375a2011-07-15 08:37:34 +00004132 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004133 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004134 CI->setCallingConv(CC);
4135 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004136 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004137 Inst = CI;
4138 return false;
4139}
4140
4141//===----------------------------------------------------------------------===//
4142// Memory Instructions.
4143//===----------------------------------------------------------------------===//
4144
4145/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004146/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004147int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004148 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004149 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004150 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004151 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004152
4153 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4154
Chris Lattner3822f632009-01-02 08:05:26 +00004155 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004156
Chris Lattnerb2f39502009-12-30 05:44:30 +00004157 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004158 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004159 if (Lex.getKind() == lltok::kw_align) {
4160 if (ParseOptionalAlignment(Alignment)) return true;
4161 } else if (Lex.getKind() == lltok::MetadataVar) {
4162 AteExtraComma = true;
4163 } else {
4164 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4165 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4166 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004167 }
4168 }
4169
Dan Gohman2140a742010-05-28 01:14:11 +00004170 if (Size && !Size->getType()->isIntegerTy())
4171 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004172
Reid Kleckner436c42e2014-01-17 23:58:17 +00004173 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4174 AI->setUsedWithInAlloca(IsInAlloca);
4175 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004176 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004177}
4178
4179/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004180/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004181/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004182/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004183int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004184 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004185 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004186 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004187 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004188 AtomicOrdering Ordering = NotAtomic;
4189 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004190
4191 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004192 isAtomic = true;
4193 Lex.Lex();
4194 }
4195
Chris Lattnerbc639292011-11-27 06:56:53 +00004196 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004197 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004198 isVolatile = true;
4199 Lex.Lex();
4200 }
4201
Chris Lattnerb2f39502009-12-30 05:44:30 +00004202 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004203 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004204 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4205 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004206
Duncan Sands19d0b472010-02-16 11:11:14 +00004207 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004208 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4209 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004210 if (isAtomic && !Alignment)
4211 return Error(Loc, "atomic load must have explicit non-zero alignment");
4212 if (Ordering == Release || Ordering == AcquireRelease)
4213 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004214
Eli Friedman59b66882011-08-09 23:02:53 +00004215 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004216 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004217}
4218
4219/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004220
4221/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4222/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004223/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004224int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004225 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004226 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004227 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004228 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004229 AtomicOrdering Ordering = NotAtomic;
4230 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004231
4232 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004233 isAtomic = true;
4234 Lex.Lex();
4235 }
4236
Chris Lattnerbc639292011-11-27 06:56:53 +00004237 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004238 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004239 isVolatile = true;
4240 Lex.Lex();
4241 }
4242
Chris Lattnerac161bf2009-01-02 07:01:27 +00004243 if (ParseTypeAndValue(Val, Loc, PFS) ||
4244 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004245 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004246 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004247 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004248 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004249
Duncan Sands19d0b472010-02-16 11:11:14 +00004250 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004251 return Error(PtrLoc, "store operand must be a pointer");
4252 if (!Val->getType()->isFirstClassType())
4253 return Error(Loc, "store operand must be a first class value");
4254 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4255 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004256 if (isAtomic && !Alignment)
4257 return Error(Loc, "atomic store must have explicit non-zero alignment");
4258 if (Ordering == Acquire || Ordering == AcquireRelease)
4259 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004260
Eli Friedman59b66882011-08-09 23:02:53 +00004261 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004262 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004263}
4264
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004265/// ParseCmpXchg
Eli Friedman02e737b2011-08-12 22:50:01 +00004266/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
Tim Northovere94a5182014-03-11 10:48:52 +00004267/// 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004268int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004269 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4270 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004271 AtomicOrdering SuccessOrdering = NotAtomic;
4272 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004273 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004274 bool isVolatile = false;
4275
4276 if (EatIfPresent(lltok::kw_volatile))
4277 isVolatile = true;
4278
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004279 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4280 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4281 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4282 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4283 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004284 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4285 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004286 return true;
4287
Tim Northovere94a5182014-03-11 10:48:52 +00004288 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004289 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004290 if (SuccessOrdering < FailureOrdering)
4291 return TokError("cmpxchg must be at least as ordered on success as failure");
4292 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4293 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004294 if (!Ptr->getType()->isPointerTy())
4295 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4296 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4297 return Error(CmpLoc, "compare value and pointer type do not match");
4298 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4299 return Error(NewLoc, "new value and pointer type do not match");
4300 if (!New->getType()->isIntegerTy())
4301 return Error(NewLoc, "cmpxchg operand must be an integer");
4302 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4303 if (Size < 8 || (Size & (Size - 1)))
4304 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4305 " integer");
4306
Tim Northovere94a5182014-03-11 10:48:52 +00004307 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
4308 FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004309 CXI->setVolatile(isVolatile);
4310 Inst = CXI;
4311 return AteExtraComma ? InstExtraComma : InstNormal;
4312}
4313
4314/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004315/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4316/// 'singlethread'? AtomicOrdering
4317int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004318 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4319 bool AteExtraComma = false;
4320 AtomicOrdering Ordering = NotAtomic;
4321 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004322 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004323 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004324
4325 if (EatIfPresent(lltok::kw_volatile))
4326 isVolatile = true;
4327
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004328 switch (Lex.getKind()) {
4329 default: return TokError("expected binary operation in atomicrmw");
4330 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4331 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4332 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4333 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4334 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4335 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4336 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4337 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4338 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4339 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4340 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4341 }
4342 Lex.Lex(); // Eat the operation.
4343
4344 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4345 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4346 ParseTypeAndValue(Val, ValLoc, PFS) ||
4347 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4348 return true;
4349
4350 if (Ordering == Unordered)
4351 return TokError("atomicrmw cannot be unordered");
4352 if (!Ptr->getType()->isPointerTy())
4353 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4354 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4355 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4356 if (!Val->getType()->isIntegerTy())
4357 return Error(ValLoc, "atomicrmw operand must be an integer");
4358 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4359 if (Size < 8 || (Size & (Size - 1)))
4360 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4361 " integer");
4362
4363 AtomicRMWInst *RMWI =
4364 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4365 RMWI->setVolatile(isVolatile);
4366 Inst = RMWI;
4367 return AteExtraComma ? InstExtraComma : InstNormal;
4368}
4369
Eli Friedmanfee02c62011-07-25 23:16:38 +00004370/// ParseFence
4371/// ::= 'fence' 'singlethread'? AtomicOrdering
4372int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4373 AtomicOrdering Ordering = NotAtomic;
4374 SynchronizationScope Scope = CrossThread;
4375 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4376 return true;
4377
4378 if (Ordering == Unordered)
4379 return TokError("fence cannot be unordered");
4380 if (Ordering == Monotonic)
4381 return TokError("fence cannot be monotonic");
4382
4383 Inst = new FenceInst(Context, Ordering, Scope);
4384 return InstNormal;
4385}
4386
Chris Lattnerac161bf2009-01-02 07:01:27 +00004387/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004388/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004389int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004390 Value *Ptr = nullptr;
4391 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004392 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004393
Dan Gohman16cbbe42009-07-29 15:58:36 +00004394 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004395
Chris Lattner3822f632009-01-02 08:05:26 +00004396 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004397
Eli Benderskyd9806682013-04-22 17:03:42 +00004398 Type *BaseType = Ptr->getType();
4399 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4400 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004401 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004402
Chris Lattnerac161bf2009-01-02 07:01:27 +00004403 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004404 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004405 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004406 if (Lex.getKind() == lltok::MetadataVar) {
4407 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004408 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004409 }
Chris Lattner3822f632009-01-02 08:05:26 +00004410 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004411 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004412 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004413 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4414 return Error(EltLoc, "getelementptr index type missmatch");
4415 if (Val->getType()->isVectorTy()) {
4416 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4417 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4418 if (ValNumEl != PtrNumEl)
4419 return Error(EltLoc,
4420 "getelementptr vector index has a wrong number of elements");
4421 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004422 Indices.push_back(Val);
4423 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004424
Eli Benderskyd9806682013-04-22 17:03:42 +00004425 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4426 return Error(Loc, "base element of getelementptr must be sized");
4427
4428 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004429 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004430 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004431 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004432 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004433 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004434}
4435
4436/// ParseExtractValue
4437/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004438int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004439 Value *Val; LocTy Loc;
4440 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004441 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004442 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004443 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004444 return true;
4445
Chris Lattner392be582010-02-12 20:49:41 +00004446 if (!Val->getType()->isAggregateType())
4447 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004448
Jay Foad57aa6362011-07-13 10:26:04 +00004449 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004450 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004451 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004452 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004453}
4454
4455/// ParseInsertValue
4456/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004457int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004458 Value *Val0, *Val1; LocTy Loc0, Loc1;
4459 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004460 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004461 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4462 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4463 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004464 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004465 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004466
Chris Lattner392be582010-02-12 20:49:41 +00004467 if (!Val0->getType()->isAggregateType())
4468 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004469
Jay Foad57aa6362011-07-13 10:26:04 +00004470 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004471 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004472 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004473 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004474}
Nick Lewycky49f89192009-04-04 07:22:01 +00004475
4476//===----------------------------------------------------------------------===//
4477// Embedded metadata.
4478//===----------------------------------------------------------------------===//
4479
4480/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004481/// ::= Element (',' Element)*
4482/// Element
4483/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004484bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004485 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004486 // Check for an empty list.
4487 if (Lex.getKind() == lltok::rbrace)
4488 return false;
4489
Nick Lewycky49f89192009-04-04 07:22:01 +00004490 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004491 // Null is a special case since it is typeless.
4492 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004493 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004494 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004495 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004496
Craig Topper2617dcc2014-04-15 06:32:26 +00004497 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004498 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004499 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004500 } while (EatIfPresent(lltok::comma));
4501
4502 return false;
4503}