blob: f0efa9414d6a59b7186f972312bfd24c76ffe87b [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;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000262 GlobalVariable::ThreadLocalMode TLM;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000263 if (ParseOptionalLinkage(Linkage) ||
264 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000265 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000266 ParseOptionalThreadLocal(TLM) ||
267 ParseGlobal("", SMLoc(), Linkage, true, Visibility, DLLStorageClass,
268 TLM))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000269 return true;
270 break;
271 }
272 case lltok::kw_default: // OptionalVisibility
273 case lltok::kw_hidden: // OptionalVisibility
274 case lltok::kw_protected: { // OptionalVisibility
Nico Rieck7157bb72014-01-14 15:22:47 +0000275 unsigned Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000276 GlobalVariable::ThreadLocalMode TLM;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000277 if (ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000278 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000279 ParseOptionalThreadLocal(TLM) ||
280 ParseGlobal("", SMLoc(), 0, false, Visibility, DLLStorageClass, TLM))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000281 return true;
282 break;
283 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000284
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000285 case lltok::kw_thread_local: { // OptionalThreadLocal
286 GlobalVariable::ThreadLocalMode TLM;
287 if (ParseOptionalThreadLocal(TLM) ||
288 ParseGlobal("", SMLoc(), 0, false, 0, 0, TLM))
289 return true;
290 break;
291 }
292
Chris Lattnerac161bf2009-01-02 07:01:27 +0000293 case lltok::kw_addrspace: // OptionalAddrSpace
294 case lltok::kw_constant: // GlobalType
295 case lltok::kw_global: // GlobalType
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000296 if (ParseGlobal("", SMLoc(), 0, false, 0, 0, GlobalValue::NotThreadLocal))
297 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000298 break;
Bill Wendlinga7c38772013-02-09 15:48:49 +0000299
300 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000301 }
302 }
303}
304
305
306/// toplevelentity
307/// ::= 'module' 'asm' STRINGCONSTANT
308bool LLParser::ParseModuleAsm() {
309 assert(Lex.getKind() == lltok::kw_module);
310 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000311
312 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000313 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
314 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000315
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000316 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317 return false;
318}
319
320/// toplevelentity
321/// ::= 'target' 'triple' '=' STRINGCONSTANT
322/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
323bool LLParser::ParseTargetDefinition() {
324 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000325 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000326 switch (Lex.Lex()) {
327 default: return TokError("unknown target property");
328 case lltok::kw_triple:
329 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000330 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
331 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000332 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000333 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000334 return false;
335 case lltok::kw_datalayout:
336 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000337 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
338 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000339 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000340 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000341 return false;
342 }
343}
344
Bill Wendling706d3d62012-11-28 08:41:48 +0000345/// toplevelentity
346/// ::= 'deplibs' '=' '[' ']'
347/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
348/// FIXME: Remove in 4.0. Currently parse, but ignore.
349bool LLParser::ParseDepLibs() {
350 assert(Lex.getKind() == lltok::kw_deplibs);
351 Lex.Lex();
352 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
353 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
354 return true;
355
356 if (EatIfPresent(lltok::rsquare))
357 return false;
358
359 do {
360 std::string Str;
361 if (ParseStringConstant(Str)) return true;
362 } while (EatIfPresent(lltok::comma));
363
364 return ParseToken(lltok::rsquare, "expected ']' at end of list");
365}
366
Dan Gohman466876b2009-08-12 23:32:33 +0000367/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000368/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000369bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000370 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000371 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000372 Lex.Lex(); // eat LocalVarID;
373
374 if (ParseToken(lltok::equal, "expected '=' after name") ||
375 ParseToken(lltok::kw_type, "expected 'type' after '='"))
376 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000377
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000378 if (TypeID >= NumberedTypes.size())
379 NumberedTypes.resize(TypeID+1);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000380
Craig Topper2617dcc2014-04-15 06:32:26 +0000381 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000382 if (ParseStructDefinition(TypeLoc, "",
383 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000384
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000385 if (!isa<StructType>(Result)) {
386 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
387 if (Entry.first)
388 return Error(TypeLoc, "non-struct types may not be recursive");
389 Entry.first = Result;
390 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000391 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000392
Chris Lattnerac161bf2009-01-02 07:01:27 +0000393 return false;
394}
395
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000396
Chris Lattnerac161bf2009-01-02 07:01:27 +0000397/// toplevelentity
398/// ::= LocalVar '=' 'type' type
399bool LLParser::ParseNamedType() {
400 std::string Name = Lex.getStrVal();
401 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000402 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000403
Chris Lattner3822f632009-01-02 08:05:26 +0000404 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000405 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000406 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000407
Craig Topper2617dcc2014-04-15 06:32:26 +0000408 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000409 if (ParseStructDefinition(NameLoc, Name,
410 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000411
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000412 if (!isa<StructType>(Result)) {
413 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
414 if (Entry.first)
415 return Error(NameLoc, "non-struct types may not be recursive");
416 Entry.first = Result;
417 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000418 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000419
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000420 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000421}
422
423
424/// toplevelentity
425/// ::= 'declare' FunctionHeader
426bool LLParser::ParseDeclare() {
427 assert(Lex.getKind() == lltok::kw_declare);
428 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000429
Chris Lattnerac161bf2009-01-02 07:01:27 +0000430 Function *F;
431 return ParseFunctionHeader(F, false);
432}
433
434/// toplevelentity
435/// ::= 'define' FunctionHeader '{' ...
436bool LLParser::ParseDefine() {
437 assert(Lex.getKind() == lltok::kw_define);
438 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000439
Chris Lattnerac161bf2009-01-02 07:01:27 +0000440 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000441 return ParseFunctionHeader(F, true) ||
442 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000443}
444
Chris Lattner3822f632009-01-02 08:05:26 +0000445/// ParseGlobalType
446/// ::= 'constant'
447/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000448bool LLParser::ParseGlobalType(bool &IsConstant) {
449 if (Lex.getKind() == lltok::kw_constant)
450 IsConstant = true;
451 else if (Lex.getKind() == lltok::kw_global)
452 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000453 else {
454 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000455 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000456 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000457 Lex.Lex();
458 return false;
459}
460
Dan Gohman466876b2009-08-12 23:32:33 +0000461/// ParseUnnamedGlobal:
462/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000463/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
464/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000465/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000466/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
467/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000468bool LLParser::ParseUnnamedGlobal() {
469 unsigned VarID = NumberedVals.size();
470 std::string Name;
471 LocTy NameLoc = Lex.getLoc();
472
473 // Handle the GlobalID form.
474 if (Lex.getKind() == lltok::GlobalID) {
475 if (Lex.getUIntVal() != VarID)
476 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000477 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000478 Lex.Lex(); // eat GlobalID;
479
480 if (ParseToken(lltok::equal, "expected '=' after name"))
481 return true;
482 }
483
484 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000485 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000486 GlobalVariable::ThreadLocalMode TLM;
Dan Gohman466876b2009-08-12 23:32:33 +0000487 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000488 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000489 ParseOptionalDLLStorageClass(DLLStorageClass) ||
490 ParseOptionalThreadLocal(TLM))
Dan Gohman466876b2009-08-12 23:32:33 +0000491 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000492
Dan Gohman466876b2009-08-12 23:32:33 +0000493 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000494 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000495 DLLStorageClass, TLM);
496 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
Dan Gohman466876b2009-08-12 23:32:33 +0000497}
498
Chris Lattnerac161bf2009-01-02 07:01:27 +0000499/// ParseNamedGlobal:
500/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000501/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
502/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000503bool LLParser::ParseNamedGlobal() {
504 assert(Lex.getKind() == lltok::GlobalVar);
505 LocTy NameLoc = Lex.getLoc();
506 std::string Name = Lex.getStrVal();
507 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000508
Chris Lattnerac161bf2009-01-02 07:01:27 +0000509 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000510 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000511 GlobalVariable::ThreadLocalMode TLM;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000512 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
513 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000514 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000515 ParseOptionalDLLStorageClass(DLLStorageClass) ||
516 ParseOptionalThreadLocal(TLM))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000517 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000518
Chris Lattnerac161bf2009-01-02 07:01:27 +0000519 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000520 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000521 DLLStorageClass, TLM);
522 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000523}
524
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000525// MDString:
526// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000527bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000528 std::string Str;
529 if (ParseStringConstant(Str)) return true;
Chris Lattner1797fc72009-12-29 21:53:55 +0000530 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000531 return false;
532}
533
534// MDNode:
535// ::= '!' MDNodeNumber
Chris Lattner8eff0152010-04-01 05:14:45 +0000536//
537/// This version of ParseMDNodeID returns the slot number and null in the case
538/// of a forward reference.
539bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
540 // !{ ..., !42, ... }
541 if (ParseUInt32(SlotNo)) return true;
542
543 // Check existing MDNode.
Craig Topper2617dcc2014-04-15 06:32:26 +0000544 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != nullptr)
Chris Lattner8eff0152010-04-01 05:14:45 +0000545 Result = NumberedMetadata[SlotNo];
546 else
Craig Topper2617dcc2014-04-15 06:32:26 +0000547 Result = nullptr;
Chris Lattner8eff0152010-04-01 05:14:45 +0000548 return false;
549}
550
Chris Lattner6dac02a2009-12-30 04:15:23 +0000551bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000552 // !{ ..., !42, ... }
553 unsigned MID = 0;
Chris Lattner8eff0152010-04-01 05:14:45 +0000554 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000555
Chris Lattner8eff0152010-04-01 05:14:45 +0000556 // If not a forward reference, just return it now.
557 if (Result) return false;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000558
Chris Lattner8eff0152010-04-01 05:14:45 +0000559 // Otherwise, create MDNode forward reference.
Dmitri Gribenko3238fb72013-05-05 00:40:33 +0000560 MDNode *FwdNode = MDNode::getTemporary(Context, None);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000561 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000562
Chris Lattnerfc58af22009-12-30 04:51:58 +0000563 if (NumberedMetadata.size() <= MID)
564 NumberedMetadata.resize(MID+1);
565 NumberedMetadata[MID] = FwdNode;
Chris Lattner1797fc72009-12-29 21:53:55 +0000566 Result = FwdNode;
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000567 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000568}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000569
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000570/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000571/// !foo = !{ !1, !2 }
572bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000573 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000574 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000575 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000576
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000577 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000578 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000579 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000580 return true;
581
Dan Gohman2637cc12010-07-21 23:38:33 +0000582 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000583 if (Lex.getKind() != lltok::rbrace)
584 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000585 if (ParseToken(lltok::exclaim, "Expected '!' here"))
586 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000587
Craig Topper2617dcc2014-04-15 06:32:26 +0000588 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000589 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000590 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000591 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000592
593 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
594 return true;
595
Devang Patelbe626972009-07-29 00:34:02 +0000596 return false;
597}
598
Devang Patel39e64d42009-07-01 19:21:12 +0000599/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000600/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000601bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000602 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000603 Lex.Lex();
604 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000605
606 LocTy TyLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +0000607 Type *Ty = nullptr;
Devang Patele059ba6e2009-07-23 01:07:34 +0000608 SmallVector<Value *, 16> Elts;
Chris Lattner278bc952009-12-29 22:40:21 +0000609 if (ParseUInt32(MetadataID) ||
610 ParseToken(lltok::equal, "expected '=' here") ||
611 ParseType(Ty, TyLoc) ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000612 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner278bc952009-12-29 22:40:21 +0000613 ParseToken(lltok::lbrace, "Expected '{' here") ||
Craig Topper2617dcc2014-04-15 06:32:26 +0000614 ParseMDNodeVector(Elts, nullptr) ||
Chris Lattner278bc952009-12-29 22:40:21 +0000615 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patele059ba6e2009-07-23 01:07:34 +0000616 return true;
617
Jay Foad5514afe2011-04-21 19:59:31 +0000618 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000619
Chris Lattnerfc58af22009-12-30 04:51:58 +0000620 // See if this was forward referenced, if so, handle it.
Chris Lattner218b22f2009-12-29 21:43:58 +0000621 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Pateld2541152009-07-08 19:23:54 +0000622 FI = ForwardRefMDNodes.find(MetadataID);
623 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman16a5d982010-08-20 22:02:26 +0000624 MDNode *Temp = FI->second.first;
625 Temp->replaceAllUsesWith(Init);
626 MDNode::deleteTemporary(Temp);
Devang Pateld2541152009-07-08 19:23:54 +0000627 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000628
Chris Lattnerfc58af22009-12-30 04:51:58 +0000629 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
630 } else {
631 if (MetadataID >= NumberedMetadata.size())
632 NumberedMetadata.resize(MetadataID+1);
633
Craig Topper2617dcc2014-04-15 06:32:26 +0000634 if (NumberedMetadata[MetadataID] != nullptr)
Chris Lattnerfc58af22009-12-30 04:51:58 +0000635 return TokError("Metadata id is already used");
636 NumberedMetadata[MetadataID] = Init;
Devang Pateld2541152009-07-08 19:23:54 +0000637 }
638
Devang Patel39e64d42009-07-01 19:21:12 +0000639 return false;
640}
641
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000642static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
643 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
644 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
645}
646
Chris Lattnerac161bf2009-01-02 07:01:27 +0000647/// ParseAlias:
Nico Rieck7157bb72014-01-14 15:22:47 +0000648/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
649/// OptionalLinkage Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000650/// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass 'alias'
651/// OptionalLinkage OptionalAddrSpace Type, Aliasee
652///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000653/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000654/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000655///
Nico Rieck7157bb72014-01-14 15:22:47 +0000656/// Everything through DLL storage class has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000657///
658bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000659 unsigned Visibility, unsigned DLLStorageClass,
660 GlobalVariable::ThreadLocalMode TLM) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000661 assert(Lex.getKind() == lltok::kw_alias);
662 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000663 LocTy LinkageLoc = Lex.getLoc();
Rafael Espindola78527052013-10-06 15:10:43 +0000664 unsigned L;
665 if (ParseOptionalLinkage(L))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000666 return true;
667
Rafael Espindola78527052013-10-06 15:10:43 +0000668 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
669
Rafael Espindolacaa43562013-10-09 16:07:32 +0000670 if(!GlobalAlias::isValidLinkage(Linkage))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000671 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000672
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000673 if (!isValidVisibilityForLinkage(Visibility, L))
674 return Error(LinkageLoc,
675 "symbol with local linkage must have default visibility");
676
Rafael Espindola6b238632014-05-16 19:35:39 +0000677 bool HasAddrSpace = Lex.getKind() == lltok::kw_addrspace;
678 unsigned AddrSpace;
679 LocTy AddrSpaceLoc = Lex.getLoc();
680 if (ParseOptionalAddrSpace(AddrSpace))
681 return true;
682
683 LocTy TyLoc = Lex.getLoc();
684 Type *Ty = nullptr;
685 if (ParseType(Ty))
686 return true;
687
688 bool DifferentType = EatIfPresent(lltok::comma);
689 if (HasAddrSpace && !DifferentType)
690 return Error(AddrSpaceLoc, "A type is required if addrspace is given");
691
692 Type *AliaseeType = nullptr;
693 if (DifferentType) {
694 if (ParseType(AliaseeType))
695 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000696 } else {
Rafael Espindola6b238632014-05-16 19:35:39 +0000697 AliaseeType = Ty;
698 auto *PTy = dyn_cast<PointerType>(Ty);
699 if (!PTy)
700 return Error(TyLoc, "An alias must have pointer type");
701 Ty = PTy->getElementType();
702 AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000703 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000704
Rafael Espindola6b238632014-05-16 19:35:39 +0000705 LocTy AliaseeLoc = Lex.getLoc();
706 Constant *C;
707 if (ParseGlobalValue(AliaseeType, C))
708 return true;
709
710 auto *Aliasee = dyn_cast<GlobalObject>(C);
711 if (!Aliasee)
712 return Error(AliaseeLoc, "Alias must point to function or variable");
713
714 assert(Aliasee->getType()->isPointerTy());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000715
716 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000717 std::unique_ptr<GlobalAlias> GA(
Rafael Espindolaf1bedd3742014-05-17 21:29:57 +0000718 GlobalAlias::create(Ty, AddrSpace, (GlobalValue::LinkageTypes)Linkage,
719 Name, Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000720 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000721 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000722 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000723
Chris Lattnerac161bf2009-01-02 07:01:27 +0000724 // See if this value already exists in the symbol table. If so, it is either
725 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000726 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000727 // See if this was a redefinition. If so, there is no entry in
728 // ForwardRefVals.
729 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
730 I = ForwardRefVals.find(Name);
731 if (I == ForwardRefVals.end())
732 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
733
734 // Otherwise, this was a definition of forward ref. Verify that types
735 // agree.
736 if (Val->getType() != GA->getType())
737 return Error(NameLoc,
738 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000739
Chris Lattnerac161bf2009-01-02 07:01:27 +0000740 // If they agree, just RAUW the old value with the alias and remove the
741 // forward ref info.
Rafael Espindola6b238632014-05-16 19:35:39 +0000742 for (auto *User : Val->users()) {
743 if (auto *GA = dyn_cast<GlobalAlias>(User))
744 return Error(NameLoc, "Alias is pointed by alias " + GA->getName());
745 }
746
Rafael Espindolaaa273822014-05-09 21:49:17 +0000747 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000748 Val->eraseFromParent();
749 ForwardRefVals.erase(I);
750 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000751
Chris Lattnerac161bf2009-01-02 07:01:27 +0000752 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000753 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000754 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000755
Rafael Espindolaaa273822014-05-09 21:49:17 +0000756 // The module owns this now
757 GA.release();
758
Chris Lattnerac161bf2009-01-02 07:01:27 +0000759 return false;
760}
761
762/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000763/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
764/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000765/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000766/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
767/// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000768/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000769///
David Majnemerc4ab61c2014-03-09 06:41:58 +0000770/// Everything up to and including OptionalDLLStorageClass has been parsed
771/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000772///
773bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
774 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000775 unsigned Visibility, unsigned DLLStorageClass,
776 GlobalVariable::ThreadLocalMode TLM) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000777 if (!isValidVisibilityForLinkage(Visibility, Linkage))
778 return Error(NameLoc,
779 "symbol with local linkage must have default visibility");
780
Chris Lattnerac161bf2009-01-02 07:01:27 +0000781 unsigned AddrSpace;
Shuxin Yang2e1890e2013-10-27 03:08:44 +0000782 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Rafael Espindola026d1522011-01-13 01:30:30 +0000783 LocTy UnnamedAddrLoc;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000784 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000785 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000786
Craig Topper2617dcc2014-04-15 06:32:26 +0000787 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000788 if (ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindola026d1522011-01-13 01:30:30 +0000789 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
790 &UnnamedAddrLoc) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000791 ParseOptionalToken(lltok::kw_externally_initialized,
792 IsExternallyInitialized,
793 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000794 ParseGlobalType(IsConstant) ||
795 ParseType(Ty, TyLoc))
796 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000797
Chris Lattnerac161bf2009-01-02 07:01:27 +0000798 // If the linkage is specified and is external, then no initializer is
799 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000800 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000801 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000802 Linkage != GlobalValue::ExternalLinkage)) {
803 if (ParseGlobalValue(Ty, Init))
804 return true;
805 }
806
Duncan Sands19d0b472010-02-16 11:11:14 +0000807 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000808 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000809
Craig Topper2617dcc2014-04-15 06:32:26 +0000810 GlobalVariable *GV = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000811
812 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000813 if (!Name.empty()) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000814 if (GlobalValue *GVal = M->getNamedValue(Name)) {
815 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
816 return Error(NameLoc, "redefinition of global '@" + Name + "'");
817 GV = cast<GlobalVariable>(GVal);
818 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000819 } else {
820 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
821 I = ForwardRefValIDs.find(NumberedVals.size());
822 if (I != ForwardRefValIDs.end()) {
823 GV = cast<GlobalVariable>(I->second.first);
824 ForwardRefValIDs.erase(I);
825 }
826 }
827
Craig Topper2617dcc2014-04-15 06:32:26 +0000828 if (!GV) {
829 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
830 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000831 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000832 } else {
833 if (GV->getType()->getElementType() != Ty)
834 return Error(TyLoc,
835 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000836
Chris Lattnerac161bf2009-01-02 07:01:27 +0000837 // Move the forward-reference to the correct spot in the module.
838 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
839 }
840
841 if (Name.empty())
842 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000843
Chris Lattnerac161bf2009-01-02 07:01:27 +0000844 // Set the parsed properties on the global.
845 if (Init)
846 GV->setInitializer(Init);
847 GV->setConstant(IsConstant);
848 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
849 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000850 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000851 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000852 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000853 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000854
Chris Lattnerac161bf2009-01-02 07:01:27 +0000855 // Parse attributes on the global.
856 while (Lex.getKind() == lltok::comma) {
857 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000858
Chris Lattnerac161bf2009-01-02 07:01:27 +0000859 if (Lex.getKind() == lltok::kw_section) {
860 Lex.Lex();
861 GV->setSection(Lex.getStrVal());
862 if (ParseToken(lltok::StringConstant, "expected global section string"))
863 return true;
864 } else if (Lex.getKind() == lltok::kw_align) {
865 unsigned Alignment;
866 if (ParseOptionalAlignment(Alignment)) return true;
867 GV->setAlignment(Alignment);
868 } else {
869 TokError("unknown global variable property!");
870 }
871 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000872
Chris Lattnerac161bf2009-01-02 07:01:27 +0000873 return false;
874}
875
Bill Wendling63b88192013-02-06 06:52:58 +0000876/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000877/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000878bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000879 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000880 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000881 Lex.Lex();
882
883 assert(Lex.getKind() == lltok::AttrGrpID);
Bill Wendling63b88192013-02-06 06:52:58 +0000884 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000885 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000886 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000887 Lex.Lex();
888
889 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000890 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000891 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000892 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000893 ParseToken(lltok::rbrace, "expected end of attribute group"))
894 return true;
895
Bill Wendlingb32b0412013-02-08 06:32:06 +0000896 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000897 return Error(AttrGrpLoc, "attribute group has no attributes");
898
899 return false;
900}
901
Bill Wendling8b0321d2013-02-08 00:52:31 +0000902/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000903/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000904bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
905 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000906 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000907 bool HaveError = false;
908
909 B.clear();
910
Bill Wendling63b88192013-02-06 06:52:58 +0000911 while (true) {
912 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000913 if (Token == lltok::kw_builtin)
914 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000915 switch (Token) {
916 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000917 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000918 return Error(Lex.getLoc(), "unterminated attribute group");
919 case lltok::rbrace:
920 // Finished.
921 return false;
922
Bill Wendlingb32b0412013-02-08 06:32:06 +0000923 case lltok::AttrGrpID: {
924 // Allow a function to reference an attribute group:
925 //
926 // define void @foo() #1 { ... }
927 if (inAttrGrp)
928 HaveError |=
929 Error(Lex.getLoc(),
930 "cannot have an attribute group reference in an attribute group");
931
932 unsigned AttrGrpNum = Lex.getUIntVal();
933 if (inAttrGrp) break;
934
935 // Save the reference to the attribute group. We'll fill it in later.
936 FwdRefAttrGrps.push_back(AttrGrpNum);
937 break;
938 }
Bill Wendling63b88192013-02-06 06:52:58 +0000939 // Target-dependent attributes:
940 case lltok::StringConstant: {
941 std::string Attr = Lex.getStrVal();
942 Lex.Lex();
943 std::string Val;
944 if (EatIfPresent(lltok::equal) &&
945 ParseStringConstant(Val))
946 return true;
947
948 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000949 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000950 }
951
952 // Target-independent attributes:
953 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000954 // As a hack, we allow function alignment to be initially parsed as an
955 // attribute on a function declaration/definition or added to an attribute
956 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000957 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000958 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000959 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000960 if (ParseToken(lltok::equal, "expected '=' here") ||
961 ParseUInt32(Alignment))
962 return true;
963 } else {
964 if (ParseOptionalAlignment(Alignment))
965 return true;
966 }
Bill Wendling63b88192013-02-06 06:52:58 +0000967 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000968 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000969 }
970 case lltok::kw_alignstack: {
971 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000972 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000973 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000974 if (ParseToken(lltok::equal, "expected '=' here") ||
975 ParseUInt32(Alignment))
976 return true;
977 } else {
978 if (ParseOptionalStackAlignment(Alignment))
979 return true;
980 }
Bill Wendling63b88192013-02-06 06:52:58 +0000981 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000982 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000983 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000984 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000985 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000986 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000987 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
988 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
989 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
990 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
991 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
992 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
993 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
994 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
995 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
996 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
997 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000998 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000999 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
1000 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1001 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
1002 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
1003 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
1004 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
1005 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
1006 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
1007 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
1008 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
1009 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +00001010
1011 // Error handling.
1012 case lltok::kw_inreg:
1013 case lltok::kw_signext:
1014 case lltok::kw_zeroext:
1015 HaveError |=
1016 Error(Lex.getLoc(),
1017 "invalid use of attribute on a function");
1018 break;
1019 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001020 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001021 case lltok::kw_nest:
1022 case lltok::kw_noalias:
1023 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001024 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +00001025 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001026 case lltok::kw_sret:
1027 HaveError |=
1028 Error(Lex.getLoc(),
1029 "invalid use of parameter-only attribute on a function");
1030 break;
Bill Wendling63b88192013-02-06 06:52:58 +00001031 }
1032
1033 Lex.Lex();
1034 }
1035}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001036
1037//===----------------------------------------------------------------------===//
1038// GlobalValue Reference/Resolution Routines.
1039//===----------------------------------------------------------------------===//
1040
1041/// GetGlobalVal - Get a value with the specified name or ID, creating a
1042/// forward reference record if needed. This can return null if the value
1043/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001044GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001045 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001046 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001047 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001048 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001049 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001050 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001051
Chris Lattnerac161bf2009-01-02 07:01:27 +00001052 // Look this name up in the normal function symbol table.
1053 GlobalValue *Val =
1054 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001055
Chris Lattnerac161bf2009-01-02 07:01:27 +00001056 // If this is a forward reference for the value, see if we already created a
1057 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001058 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001059 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1060 I = ForwardRefVals.find(Name);
1061 if (I != ForwardRefVals.end())
1062 Val = I->second.first;
1063 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001064
Chris Lattnerac161bf2009-01-02 07:01:27 +00001065 // If we have the value in the symbol table or fwd-ref table, return it.
1066 if (Val) {
1067 if (Val->getType() == Ty) return Val;
1068 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001069 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001070 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001071 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001072
Chris Lattnerac161bf2009-01-02 07:01:27 +00001073 // Otherwise, create a new forward reference for this value and remember it.
1074 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001075 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001076 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001077 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001078 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001079 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1080 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001081 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001082
Chris Lattnerac161bf2009-01-02 07:01:27 +00001083 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1084 return FwdVal;
1085}
1086
Chris Lattner229907c2011-07-18 04:54:35 +00001087GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1088 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001089 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001090 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001091 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001092 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001093
Craig Topper2617dcc2014-04-15 06:32:26 +00001094 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001095
Chris Lattnerac161bf2009-01-02 07:01:27 +00001096 // If this is a forward reference for the value, see if we already created a
1097 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001098 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001099 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1100 I = ForwardRefValIDs.find(ID);
1101 if (I != ForwardRefValIDs.end())
1102 Val = I->second.first;
1103 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001104
Chris Lattnerac161bf2009-01-02 07:01:27 +00001105 // If we have the value in the symbol table or fwd-ref table, return it.
1106 if (Val) {
1107 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001108 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001109 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001110 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001111 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001112
Chris Lattnerac161bf2009-01-02 07:01:27 +00001113 // Otherwise, create a new forward reference for this value and remember it.
1114 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001115 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001116 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001117 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001118 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001119 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001120
Chris Lattnerac161bf2009-01-02 07:01:27 +00001121 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1122 return FwdVal;
1123}
1124
1125
1126//===----------------------------------------------------------------------===//
1127// Helper Routines.
1128//===----------------------------------------------------------------------===//
1129
1130/// ParseToken - If the current token has the specified kind, eat it and return
1131/// success. Otherwise, emit the specified error and return failure.
1132bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1133 if (Lex.getKind() != T)
1134 return TokError(ErrMsg);
1135 Lex.Lex();
1136 return false;
1137}
1138
Chris Lattner3822f632009-01-02 08:05:26 +00001139/// ParseStringConstant
1140/// ::= StringConstant
1141bool LLParser::ParseStringConstant(std::string &Result) {
1142 if (Lex.getKind() != lltok::StringConstant)
1143 return TokError("expected string constant");
1144 Result = Lex.getStrVal();
1145 Lex.Lex();
1146 return false;
1147}
1148
1149/// ParseUInt32
1150/// ::= uint32
1151bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001152 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1153 return TokError("expected integer");
1154 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1155 if (Val64 != unsigned(Val64))
1156 return TokError("expected 32-bit integer (too large)");
1157 Val = Val64;
1158 Lex.Lex();
1159 return false;
1160}
1161
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001162/// ParseTLSModel
1163/// := 'localdynamic'
1164/// := 'initialexec'
1165/// := 'localexec'
1166bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1167 switch (Lex.getKind()) {
1168 default:
1169 return TokError("expected localdynamic, initialexec or localexec");
1170 case lltok::kw_localdynamic:
1171 TLM = GlobalVariable::LocalDynamicTLSModel;
1172 break;
1173 case lltok::kw_initialexec:
1174 TLM = GlobalVariable::InitialExecTLSModel;
1175 break;
1176 case lltok::kw_localexec:
1177 TLM = GlobalVariable::LocalExecTLSModel;
1178 break;
1179 }
1180
1181 Lex.Lex();
1182 return false;
1183}
1184
1185/// ParseOptionalThreadLocal
1186/// := /*empty*/
1187/// := 'thread_local'
1188/// := 'thread_local' '(' tlsmodel ')'
1189bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1190 TLM = GlobalVariable::NotThreadLocal;
1191 if (!EatIfPresent(lltok::kw_thread_local))
1192 return false;
1193
1194 TLM = GlobalVariable::GeneralDynamicTLSModel;
1195 if (Lex.getKind() == lltok::lparen) {
1196 Lex.Lex();
1197 return ParseTLSModel(TLM) ||
1198 ParseToken(lltok::rparen, "expected ')' after thread local model");
1199 }
1200 return false;
1201}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001202
1203/// ParseOptionalAddrSpace
1204/// := /*empty*/
1205/// := 'addrspace' '(' uint32 ')'
1206bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1207 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001208 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001209 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001210 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001211 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001212 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001213}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001214
Bill Wendling34c2eb22012-12-04 23:40:58 +00001215/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1216bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1217 bool HaveError = false;
1218
1219 B.clear();
1220
1221 while (1) {
1222 lltok::Kind Token = Lex.getKind();
1223 switch (Token) {
1224 default: // End of attributes.
1225 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001226 case lltok::kw_align: {
1227 unsigned Alignment;
1228 if (ParseOptionalAlignment(Alignment))
1229 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001230 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001231 continue;
1232 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001233 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Reid Klecknera534a382013-12-19 02:14:12 +00001234 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001235 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1236 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1237 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1238 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001239 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001240 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1241 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001242 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001243 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1244 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1245 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001246
Stephen Lin7577ed52013-04-20 13:16:13 +00001247 case lltok::kw_alignstack:
1248 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001249 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001250 case lltok::kw_inlinehint:
1251 case lltok::kw_minsize:
1252 case lltok::kw_naked:
1253 case lltok::kw_nobuiltin:
1254 case lltok::kw_noduplicate:
1255 case lltok::kw_noimplicitfloat:
1256 case lltok::kw_noinline:
1257 case lltok::kw_nonlazybind:
1258 case lltok::kw_noredzone:
1259 case lltok::kw_noreturn:
1260 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001261 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001262 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001263 case lltok::kw_returns_twice:
1264 case lltok::kw_sanitize_address:
1265 case lltok::kw_sanitize_memory:
1266 case lltok::kw_sanitize_thread:
1267 case lltok::kw_ssp:
1268 case lltok::kw_sspreq:
1269 case lltok::kw_sspstrong:
1270 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001271 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1272 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001273 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001274
Bill Wendling34c2eb22012-12-04 23:40:58 +00001275 Lex.Lex();
1276 }
1277}
1278
1279/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1280bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1281 bool HaveError = false;
1282
1283 B.clear();
1284
1285 while (1) {
1286 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001287 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001288 default: // End of attributes.
1289 return HaveError;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001290 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1291 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001292 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001293 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1294 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001295
Bill Wendling34c2eb22012-12-04 23:40:58 +00001296 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001297 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001298 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001299 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001300 case lltok::kw_nest:
1301 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001302 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001303 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001304 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001305 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001306
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001307 case lltok::kw_alignstack:
1308 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001309 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001310 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001311 case lltok::kw_inlinehint:
1312 case lltok::kw_minsize:
1313 case lltok::kw_naked:
1314 case lltok::kw_nobuiltin:
1315 case lltok::kw_noduplicate:
1316 case lltok::kw_noimplicitfloat:
1317 case lltok::kw_noinline:
1318 case lltok::kw_nonlazybind:
1319 case lltok::kw_noredzone:
1320 case lltok::kw_noreturn:
1321 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001322 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001323 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001324 case lltok::kw_returns_twice:
1325 case lltok::kw_sanitize_address:
1326 case lltok::kw_sanitize_memory:
1327 case lltok::kw_sanitize_thread:
1328 case lltok::kw_ssp:
1329 case lltok::kw_sspreq:
1330 case lltok::kw_sspstrong:
1331 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001332 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001333 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001334
1335 case lltok::kw_readnone:
1336 case lltok::kw_readonly:
1337 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001338 }
1339
Chris Lattnerac161bf2009-01-02 07:01:27 +00001340 Lex.Lex();
1341 }
1342}
1343
1344/// ParseOptionalLinkage
1345/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001346/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001347/// ::= 'internal'
1348/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001349/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001350/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001351/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001352/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001353/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001354/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001355/// ::= 'extern_weak'
1356/// ::= 'external'
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001357///
1358/// Deprecated Values:
1359/// ::= 'linker_private'
1360/// ::= 'linker_private_weak'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001361bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1362 HasLinkage = false;
1363 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001364 default: Res=GlobalValue::ExternalLinkage; return false;
1365 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001366 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1367 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1368 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1369 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1370 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001371 case lltok::kw_available_externally:
1372 Res = GlobalValue::AvailableExternallyLinkage;
1373 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001374 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001375 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001376 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1377 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001378
1379 case lltok::kw_linker_private:
1380 case lltok::kw_linker_private_weak:
Saleem Abdulrasoolefa31a92014-04-05 22:42:53 +00001381 Lex.Warning("'" + Lex.getStrVal() + "' is deprecated, treating as"
1382 " PrivateLinkage");
Saleem Abdulrasoolc1281352014-04-05 20:51:58 +00001383 Lex.Lex();
1384 // treat linker_private and linker_private_weak as PrivateLinkage
1385 Res = GlobalValue::PrivateLinkage;
1386 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001387 }
1388 Lex.Lex();
1389 HasLinkage = true;
1390 return false;
1391}
1392
1393/// ParseOptionalVisibility
1394/// ::= /*empty*/
1395/// ::= 'default'
1396/// ::= 'hidden'
1397/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001398///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001399bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1400 switch (Lex.getKind()) {
1401 default: Res = GlobalValue::DefaultVisibility; return false;
1402 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1403 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1404 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1405 }
1406 Lex.Lex();
1407 return false;
1408}
1409
Nico Rieck7157bb72014-01-14 15:22:47 +00001410/// ParseOptionalDLLStorageClass
1411/// ::= /*empty*/
1412/// ::= 'dllimport'
1413/// ::= 'dllexport'
1414///
1415bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1416 switch (Lex.getKind()) {
1417 default: Res = GlobalValue::DefaultStorageClass; return false;
1418 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1419 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1420 }
1421 Lex.Lex();
1422 return false;
1423}
1424
Chris Lattnerac161bf2009-01-02 07:01:27 +00001425/// ParseOptionalCallingConv
1426/// ::= /*empty*/
1427/// ::= 'ccc'
1428/// ::= 'fastcc'
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001429/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001430/// ::= 'coldcc'
1431/// ::= 'x86_stdcallcc'
1432/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001433/// ::= 'x86_thiscallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001434/// ::= 'arm_apcscc'
1435/// ::= 'arm_aapcscc'
1436/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001437/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001438/// ::= 'ptx_kernel'
1439/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001440/// ::= 'spir_func'
1441/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001442/// ::= 'x86_64_sysvcc'
1443/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001444/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001445/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001446/// ::= 'preserve_mostcc'
1447/// ::= 'preserve_allcc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001448/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001449///
Sandeep Patel68c5f472009-09-02 08:44:58 +00001450bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001451 switch (Lex.getKind()) {
1452 default: CC = CallingConv::C; return false;
1453 case lltok::kw_ccc: CC = CallingConv::C; break;
1454 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1455 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1456 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1457 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001458 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001459 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1460 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1461 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001462 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001463 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1464 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001465 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1466 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001467 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001468 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1469 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001470 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001471 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001472 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1473 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001474 case lltok::kw_cc: {
1475 unsigned ArbitraryCC;
1476 Lex.Lex();
David Blaikie46a9f012012-01-20 21:51:11 +00001477 if (ParseUInt32(ArbitraryCC))
Sandeep Patel68c5f472009-09-02 08:44:58 +00001478 return true;
David Blaikie46a9f012012-01-20 21:51:11 +00001479 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1480 return false;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001481 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001482 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001483
Chris Lattnerac161bf2009-01-02 07:01:27 +00001484 Lex.Lex();
1485 return false;
1486}
1487
Chris Lattner5c427632009-12-30 05:31:19 +00001488/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001489/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman338d9a42010-08-24 02:05:17 +00001490bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1491 PerFunctionState *PFS) {
Chris Lattner5c427632009-12-30 05:31:19 +00001492 do {
1493 if (Lex.getKind() != lltok::MetadataVar)
1494 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001495
Chris Lattner596760d2009-12-29 21:25:40 +00001496 std::string Name = Lex.getStrVal();
Benjamin Kramerb3bd0192011-12-06 11:50:26 +00001497 unsigned MDK = M->getMDKindID(Name);
Chris Lattner596760d2009-12-29 21:25:40 +00001498 Lex.Lex();
Chris Lattner8d58f2f2009-10-19 05:31:10 +00001499
Chris Lattner1797fc72009-12-29 21:53:55 +00001500 MDNode *Node;
Chris Lattner8eff0152010-04-01 05:14:45 +00001501 SMLoc Loc = Lex.getLoc();
Dan Gohmanc828c542010-08-24 02:24:03 +00001502
1503 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001504 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001505
Dan Gohmanf0715b12010-08-24 14:35:45 +00001506 // This code is similar to that of ParseMetadataValue, however it needs to
1507 // have special-case code for a forward reference; see the comments on
1508 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1509 // at the top level here.
Dan Gohmanc828c542010-08-24 02:24:03 +00001510 if (Lex.getKind() == lltok::lbrace) {
1511 ValID ID;
1512 if (ParseMetadataListValue(ID, PFS))
1513 return true;
1514 assert(ID.Kind == ValID::t_MDNode);
1515 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner8eff0152010-04-01 05:14:45 +00001516 } else {
Nick Lewycky912888f2010-09-30 21:04:13 +00001517 unsigned NodeID = 0;
Dan Gohmanc828c542010-08-24 02:24:03 +00001518 if (ParseMDNodeID(Node, NodeID))
1519 return true;
1520 if (Node) {
1521 // If we got the node, add it to the instruction.
1522 Inst->setMetadata(MDK, Node);
1523 } else {
1524 MDRef R = { Loc, MDK, NodeID };
1525 // Otherwise, remember that this should be resolved later.
1526 ForwardRefInstMetadata[Inst].push_back(R);
1527 }
Chris Lattner8eff0152010-04-01 05:14:45 +00001528 }
Chris Lattner596760d2009-12-29 21:25:40 +00001529
Manman Ren209b17c2013-09-28 00:22:27 +00001530 if (MDK == LLVMContext::MD_tbaa)
1531 InstsWithTBAATag.push_back(Inst);
1532
Chris Lattner596760d2009-12-29 21:25:40 +00001533 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001534 } while (EatIfPresent(lltok::comma));
1535 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001536}
1537
Chris Lattnerac161bf2009-01-02 07:01:27 +00001538/// ParseOptionalAlignment
1539/// ::= /* empty */
1540/// ::= 'align' 4
1541bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1542 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001543 if (!EatIfPresent(lltok::kw_align))
1544 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001545 LocTy AlignLoc = Lex.getLoc();
1546 if (ParseUInt32(Alignment)) return true;
1547 if (!isPowerOf2_32(Alignment))
1548 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001549 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001550 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001551 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001552}
1553
Chris Lattnerb2f39502009-12-30 05:44:30 +00001554/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001555/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001556/// ::= ',' align 4
1557///
1558/// This returns with AteExtraComma set to true if it ate an excess comma at the
1559/// end.
1560bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1561 bool &AteExtraComma) {
1562 AteExtraComma = false;
1563 while (EatIfPresent(lltok::comma)) {
1564 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001565 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001566 AteExtraComma = true;
1567 return false;
1568 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001569
Chris Lattner95b0ff42010-04-23 00:50:50 +00001570 if (Lex.getKind() != lltok::kw_align)
1571 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001572
Chris Lattner95b0ff42010-04-23 00:50:50 +00001573 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001574 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001575
Devang Patelea8a4b92009-09-17 23:04:48 +00001576 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001577}
1578
Eli Friedmanfee02c62011-07-25 23:16:38 +00001579/// ParseScopeAndOrdering
1580/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1581/// else: ::=
1582///
1583/// This sets Scope and Ordering to the parsed values.
1584bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1585 AtomicOrdering &Ordering) {
1586 if (!isAtomic)
1587 return false;
1588
1589 Scope = CrossThread;
1590 if (EatIfPresent(lltok::kw_singlethread))
1591 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001592
1593 return ParseOrdering(Ordering);
1594}
1595
1596/// ParseOrdering
1597/// ::= AtomicOrdering
1598///
1599/// This sets Ordering to the parsed value.
1600bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001601 switch (Lex.getKind()) {
1602 default: return TokError("Expected ordering on atomic instruction");
1603 case lltok::kw_unordered: Ordering = Unordered; break;
1604 case lltok::kw_monotonic: Ordering = Monotonic; break;
1605 case lltok::kw_acquire: Ordering = Acquire; break;
1606 case lltok::kw_release: Ordering = Release; break;
1607 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1608 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1609 }
1610 Lex.Lex();
1611 return false;
1612}
1613
Charles Davisbe5557e2010-02-12 00:31:15 +00001614/// ParseOptionalStackAlignment
1615/// ::= /* empty */
1616/// ::= 'alignstack' '(' 4 ')'
1617bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1618 Alignment = 0;
1619 if (!EatIfPresent(lltok::kw_alignstack))
1620 return false;
1621 LocTy ParenLoc = Lex.getLoc();
1622 if (!EatIfPresent(lltok::lparen))
1623 return Error(ParenLoc, "expected '('");
1624 LocTy AlignLoc = Lex.getLoc();
1625 if (ParseUInt32(Alignment)) return true;
1626 ParenLoc = Lex.getLoc();
1627 if (!EatIfPresent(lltok::rparen))
1628 return Error(ParenLoc, "expected ')'");
1629 if (!isPowerOf2_32(Alignment))
1630 return Error(AlignLoc, "stack alignment is not a power of two");
1631 return false;
1632}
Devang Patelea8a4b92009-09-17 23:04:48 +00001633
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001634/// ParseIndexList - This parses the index list for an insert/extractvalue
1635/// instruction. This sets AteExtraComma in the case where we eat an extra
1636/// comma at the end of the line and find that it is followed by metadata.
1637/// Clients that don't allow metadata can call the version of this function that
1638/// only takes one argument.
1639///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001640/// ParseIndexList
1641/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001642///
1643bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1644 bool &AteExtraComma) {
1645 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001646
Chris Lattnerac161bf2009-01-02 07:01:27 +00001647 if (Lex.getKind() != lltok::comma)
1648 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001649
Chris Lattner3822f632009-01-02 08:05:26 +00001650 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001651 if (Lex.getKind() == lltok::MetadataVar) {
1652 AteExtraComma = true;
1653 return false;
1654 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001655 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001656 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001657 Indices.push_back(Idx);
1658 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001659
Chris Lattnerac161bf2009-01-02 07:01:27 +00001660 return false;
1661}
1662
1663//===----------------------------------------------------------------------===//
1664// Type Parsing.
1665//===----------------------------------------------------------------------===//
1666
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001667/// ParseType - Parse a type.
1668bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1669 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001670 switch (Lex.getKind()) {
1671 default:
1672 return TokError("expected type");
1673 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001674 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001675 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001676 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001677 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001678 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001679 // Type ::= StructType
1680 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001681 return true;
1682 break;
1683 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001684 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001685 Lex.Lex(); // eat the lsquare.
1686 if (ParseArrayVectorType(Result, false))
1687 return true;
1688 break;
1689 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001690 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001691 Lex.Lex();
1692 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001693 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001694 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001695 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001696 } else if (ParseArrayVectorType(Result, true))
1697 return true;
1698 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001699 case lltok::LocalVar: {
1700 // Type ::= %foo
1701 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001702
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001703 // If the type hasn't been defined yet, create a forward definition and
1704 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001705 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001706 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001707 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001708 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001709 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001710 Lex.Lex();
1711 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001712 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001713
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001714 case lltok::LocalVarID: {
1715 // Type ::= %4
1716 if (Lex.getUIntVal() >= NumberedTypes.size())
1717 NumberedTypes.resize(Lex.getUIntVal()+1);
1718 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001719
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001720 // If the type hasn't been defined yet, create a forward definition and
1721 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001722 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001723 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001724 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001725 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001726 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001727 Lex.Lex();
1728 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001729 }
1730 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001731
1732 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001733 while (1) {
1734 switch (Lex.getKind()) {
1735 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001736 default:
1737 if (!AllowVoid && Result->isVoidTy())
1738 return Error(TypeLoc, "void type only allowed for function results");
1739 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001740
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001741 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001742 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001743 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001744 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001745 if (Result->isVoidTy())
1746 return TokError("pointers to void are invalid - use i8* instead");
1747 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001748 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001749 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001750 Lex.Lex();
1751 break;
1752
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001753 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001754 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001755 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001756 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001757 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001758 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001759 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001760 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001761 unsigned AddrSpace;
1762 if (ParseOptionalAddrSpace(AddrSpace) ||
1763 ParseToken(lltok::star, "expected '*' in address space"))
1764 return true;
1765
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001766 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001767 break;
1768 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001769
Chris Lattnerac161bf2009-01-02 07:01:27 +00001770 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1771 case lltok::lparen:
1772 if (ParseFunctionType(Result))
1773 return true;
1774 break;
1775 }
1776 }
1777}
1778
1779/// ParseParameterList
1780/// ::= '(' ')'
1781/// ::= '(' Arg (',' Arg)* ')'
1782/// Arg
1783/// ::= Type OptionalAttributes Value OptionalAttributes
1784bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1785 PerFunctionState &PFS) {
1786 if (ParseToken(lltok::lparen, "expected '(' in call"))
1787 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001788
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001789 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001790 while (Lex.getKind() != lltok::rparen) {
1791 // If this isn't the first argument, we need a comma.
1792 if (!ArgList.empty() &&
1793 ParseToken(lltok::comma, "expected ',' in argument list"))
1794 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001795
Chris Lattnerac161bf2009-01-02 07:01:27 +00001796 // Parse the argument.
1797 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001798 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001799 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001800 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001801 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001802 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001803
Chris Lattner5b4a9622009-12-30 02:11:14 +00001804 // Otherwise, handle normal operands.
Bill Wendling34c2eb22012-12-04 23:40:58 +00001805 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner5b4a9622009-12-30 02:11:14 +00001806 return true;
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001807 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1808 AttrIndex++,
1809 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001810 }
1811
1812 Lex.Lex(); // Lex the ')'.
1813 return false;
1814}
1815
1816
1817
Chris Lattner2ed06b42009-01-05 18:34:07 +00001818/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001819/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001820/// ::= '(' ArgTypeListI ')'
1821/// ArgTypeListI
1822/// ::= /*empty*/
1823/// ::= '...'
1824/// ::= ArgTypeList ',' '...'
1825/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001826///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001827bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1828 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001829 isVarArg = false;
1830 assert(Lex.getKind() == lltok::lparen);
1831 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001832
Chris Lattnerac161bf2009-01-02 07:01:27 +00001833 if (Lex.getKind() == lltok::rparen) {
1834 // empty
1835 } else if (Lex.getKind() == lltok::dotdotdot) {
1836 isVarArg = true;
1837 Lex.Lex();
1838 } else {
1839 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001840 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001841 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001842 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001843
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001844 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001845 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001846
Chris Lattnerfdd87902009-10-05 05:54:46 +00001847 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001848 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001849
Chris Lattnerdef19492011-06-17 06:36:20 +00001850 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001851 Name = Lex.getStrVal();
1852 Lex.Lex();
1853 }
Chris Lattner3822f632009-01-02 08:05:26 +00001854
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001855 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001856 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001857
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001858 unsigned AttrIndex = 1;
Bill Wendlingd079a442012-10-15 04:46:55 +00001859 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001860 AttributeSet::get(ArgTy->getContext(),
1861 AttrIndex++, Attrs), Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001862
Chris Lattner3822f632009-01-02 08:05:26 +00001863 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001864 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001865 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001866 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001867 break;
1868 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001869
Chris Lattnerac161bf2009-01-02 07:01:27 +00001870 // Otherwise must be an argument type.
1871 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001872 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001873
Chris Lattnerfdd87902009-10-05 05:54:46 +00001874 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001875 return Error(TypeLoc, "argument can not have void type");
1876
Chris Lattnerdef19492011-06-17 06:36:20 +00001877 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001878 Name = Lex.getStrVal();
1879 Lex.Lex();
1880 } else {
1881 Name = "";
1882 }
Chris Lattner3822f632009-01-02 08:05:26 +00001883
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001884 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001885 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001886
Bill Wendlingd079a442012-10-15 04:46:55 +00001887 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001888 AttributeSet::get(ArgTy->getContext(),
1889 AttrIndex++, Attrs),
Bill Wendlingd079a442012-10-15 04:46:55 +00001890 Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001891 }
1892 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001893
Chris Lattner3822f632009-01-02 08:05:26 +00001894 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001895}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001896
Chris Lattnerac161bf2009-01-02 07:01:27 +00001897/// ParseFunctionType
1898/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001899bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001900 assert(Lex.getKind() == lltok::lparen);
1901
Chris Lattnerce473c72009-01-05 08:04:33 +00001902 if (!FunctionType::isValidReturnType(Result))
1903 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001904
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001905 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001906 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001907 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001908 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001909
Chris Lattnerac161bf2009-01-02 07:01:27 +00001910 // Reject names on the arguments lists.
1911 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1912 if (!ArgList[i].Name.empty())
1913 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001914 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001915 return Error(ArgList[i].Loc,
1916 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001917 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001918
Jay Foadb804a2b2011-07-12 14:06:48 +00001919 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001920 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001921 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001922
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001923 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001924 return false;
1925}
1926
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001927/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1928/// other structs.
1929bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1930 SmallVector<Type*, 8> Elts;
1931 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001932
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001933 Result = StructType::get(Context, Elts, Packed);
1934 return false;
1935}
1936
1937/// ParseStructDefinition - Parse a struct in a 'type' definition.
1938bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1939 std::pair<Type*, LocTy> &Entry,
1940 Type *&ResultTy) {
1941 // If the type was already defined, diagnose the redefinition.
1942 if (Entry.first && !Entry.second.isValid())
1943 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001944
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001945 // If we have opaque, just return without filling in the definition for the
1946 // struct. This counts as a definition as far as the .ll file goes.
1947 if (EatIfPresent(lltok::kw_opaque)) {
1948 // This type is being defined, so clear the location to indicate this.
1949 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001950
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001951 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001952 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001953 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001954 ResultTy = Entry.first;
1955 return false;
1956 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001957
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001958 // If the type starts with '<', then it is either a packed struct or a vector.
1959 bool isPacked = EatIfPresent(lltok::less);
1960
1961 // If we don't have a struct, then we have a random type alias, which we
1962 // accept for compatibility with old files. These types are not allowed to be
1963 // forward referenced and not allowed to be recursive.
1964 if (Lex.getKind() != lltok::lbrace) {
1965 if (Entry.first)
1966 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001967
Craig Topper2617dcc2014-04-15 06:32:26 +00001968 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001969 if (isPacked)
1970 return ParseArrayVectorType(ResultTy, true);
1971 return ParseType(ResultTy);
1972 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001973
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001974 // This type is being defined, so clear the location to indicate this.
1975 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001976
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001977 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001978 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001979 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001980
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001981 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001982
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001983 SmallVector<Type*, 8> Body;
1984 if (ParseStructBody(Body) ||
1985 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1986 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001987
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001988 STy->setBody(Body, isPacked);
1989 ResultTy = STy;
1990 return false;
1991}
1992
1993
Chris Lattnerac161bf2009-01-02 07:01:27 +00001994/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001995/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00001996/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001997/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001998/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001999/// ::= '<' '{' Type (',' Type)* '}' '>'
2000bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002001 assert(Lex.getKind() == lltok::lbrace);
2002 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002003
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002004 // Handle the empty struct.
2005 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002006 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002007
Chris Lattnerf880ca22009-03-09 04:49:14 +00002008 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002009 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002010 if (ParseType(Ty)) return true;
2011 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002012
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002013 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002014 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002015
Chris Lattner3822f632009-01-02 08:05:26 +00002016 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00002017 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002018 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002019
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002020 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002021 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002022
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002023 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002024 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002025
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002026 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002027}
2028
2029/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2030/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002031/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002032/// ::= '[' APSINTVAL 'x' Types ']'
2033/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002034bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002035 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2036 Lex.getAPSIntVal().getBitWidth() > 64)
2037 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002038
Chris Lattnerac161bf2009-01-02 07:01:27 +00002039 LocTy SizeLoc = Lex.getLoc();
2040 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002041 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002042
Chris Lattner3822f632009-01-02 08:05:26 +00002043 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2044 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002045
2046 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002047 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002048 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002049
Chris Lattner3822f632009-01-02 08:05:26 +00002050 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2051 "expected end of sequential type"))
2052 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002053
Chris Lattnerac161bf2009-01-02 07:01:27 +00002054 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002055 if (Size == 0)
2056 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002057 if ((unsigned)Size != Size)
2058 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002059 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002060 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002061 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002062 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002063 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002064 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002065 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002066 }
2067 return false;
2068}
2069
2070//===----------------------------------------------------------------------===//
2071// Function Semantic Analysis.
2072//===----------------------------------------------------------------------===//
2073
Chris Lattner3432c622009-10-28 03:39:23 +00002074LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2075 int functionNumber)
2076 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002077
2078 // Insert unnamed arguments into the NumberedVals list.
2079 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2080 AI != E; ++AI)
2081 if (!AI->hasName())
2082 NumberedVals.push_back(AI);
2083}
2084
2085LLParser::PerFunctionState::~PerFunctionState() {
2086 // If there were any forward referenced non-basicblock values, delete them.
2087 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2088 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2089 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002090 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002091 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002092 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002093 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002094 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002095
Chris Lattnerac161bf2009-01-02 07:01:27 +00002096 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2097 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2098 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002099 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002100 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002101 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002102 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002103 }
2104}
2105
Chris Lattner3432c622009-10-28 03:39:23 +00002106bool LLParser::PerFunctionState::FinishFunction() {
2107 // Check to see if someone took the address of labels in this block.
2108 if (!P.ForwardRefBlockAddresses.empty()) {
2109 ValID FunctionID;
2110 if (!F.getName().empty()) {
2111 FunctionID.Kind = ValID::t_GlobalName;
2112 FunctionID.StrVal = F.getName();
2113 } else {
2114 FunctionID.Kind = ValID::t_GlobalID;
2115 FunctionID.UIntVal = FunctionNumber;
2116 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002117
Chris Lattner3432c622009-10-28 03:39:23 +00002118 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
2119 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
2120 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
2121 // Resolve all these references.
2122 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
2123 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002124
Chris Lattner3432c622009-10-28 03:39:23 +00002125 P.ForwardRefBlockAddresses.erase(FRBAI);
2126 }
2127 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002128
Chris Lattnerac161bf2009-01-02 07:01:27 +00002129 if (!ForwardRefVals.empty())
2130 return P.Error(ForwardRefVals.begin()->second.second,
2131 "use of undefined value '%" + ForwardRefVals.begin()->first +
2132 "'");
2133 if (!ForwardRefValIDs.empty())
2134 return P.Error(ForwardRefValIDs.begin()->second.second,
2135 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002136 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002137 return false;
2138}
2139
2140
2141/// GetVal - Get a value with the specified name or ID, creating a
2142/// forward reference record if needed. This can return null if the value
2143/// exists but does not have the right type.
2144Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002145 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002146 // Look this name up in the normal function symbol table.
2147 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002148
Chris Lattnerac161bf2009-01-02 07:01:27 +00002149 // If this is a forward reference for the value, see if we already created a
2150 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002151 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002152 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2153 I = ForwardRefVals.find(Name);
2154 if (I != ForwardRefVals.end())
2155 Val = I->second.first;
2156 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002157
Chris Lattnerac161bf2009-01-02 07:01:27 +00002158 // If we have the value in the symbol table or fwd-ref table, return it.
2159 if (Val) {
2160 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002161 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002162 P.Error(Loc, "'%" + Name + "' is not a basic block");
2163 else
2164 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002165 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002166 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002167 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002168
Chris Lattnerac161bf2009-01-02 07:01:27 +00002169 // Don't make placeholders with invalid type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002170 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002171 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002172 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002173 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002174
Chris Lattnerac161bf2009-01-02 07:01:27 +00002175 // Otherwise, create a new forward reference for this value and remember it.
2176 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002177 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002178 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002179 else
2180 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002181
Chris Lattnerac161bf2009-01-02 07:01:27 +00002182 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2183 return FwdVal;
2184}
2185
Chris Lattner229907c2011-07-18 04:54:35 +00002186Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002187 LocTy Loc) {
2188 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002189 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002190
Chris Lattnerac161bf2009-01-02 07:01:27 +00002191 // If this is a forward reference for the value, see if we already created a
2192 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002193 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002194 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2195 I = ForwardRefValIDs.find(ID);
2196 if (I != ForwardRefValIDs.end())
2197 Val = I->second.first;
2198 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002199
Chris Lattnerac161bf2009-01-02 07:01:27 +00002200 // If we have the value in the symbol table or fwd-ref table, return it.
2201 if (Val) {
2202 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002203 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002204 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002205 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002206 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002207 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002208 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002209 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002210
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002211 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002212 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002213 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002214 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002215
Chris Lattnerac161bf2009-01-02 07:01:27 +00002216 // Otherwise, create a new forward reference for this value and remember it.
2217 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002218 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002219 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002220 else
2221 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002222
Chris Lattnerac161bf2009-01-02 07:01:27 +00002223 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2224 return FwdVal;
2225}
2226
2227/// SetInstName - After an instruction is parsed and inserted into its
2228/// basic block, this installs its name.
2229bool LLParser::PerFunctionState::SetInstName(int NameID,
2230 const std::string &NameStr,
2231 LocTy NameLoc, Instruction *Inst) {
2232 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002233 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002234 if (NameID != -1 || !NameStr.empty())
2235 return P.Error(NameLoc, "instructions returning void cannot have a name");
2236 return false;
2237 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002238
Chris Lattnerac161bf2009-01-02 07:01:27 +00002239 // If this was a numbered instruction, verify that the instruction is the
2240 // expected value and resolve any forward references.
2241 if (NameStr.empty()) {
2242 // If neither a name nor an ID was specified, just use the next ID.
2243 if (NameID == -1)
2244 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002245
Chris Lattnerac161bf2009-01-02 07:01:27 +00002246 if (unsigned(NameID) != NumberedVals.size())
2247 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002248 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002249
Chris Lattnerac161bf2009-01-02 07:01:27 +00002250 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2251 ForwardRefValIDs.find(NameID);
2252 if (FI != ForwardRefValIDs.end()) {
2253 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002254 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002255 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002256 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002257 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002258 ForwardRefValIDs.erase(FI);
2259 }
2260
2261 NumberedVals.push_back(Inst);
2262 return false;
2263 }
2264
2265 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2266 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2267 FI = ForwardRefVals.find(NameStr);
2268 if (FI != ForwardRefVals.end()) {
2269 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002270 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002271 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002272 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002273 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002274 ForwardRefVals.erase(FI);
2275 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002276
Chris Lattnerac161bf2009-01-02 07:01:27 +00002277 // Set the name on the instruction.
2278 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002279
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002280 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002281 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002282 NameStr + "'");
2283 return false;
2284}
2285
2286/// GetBB - Get a basic block with the specified name or ID, creating a
2287/// forward reference record if needed.
2288BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2289 LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002290 return cast_or_null<BasicBlock>(GetVal(Name,
2291 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002292}
2293
2294BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002295 return cast_or_null<BasicBlock>(GetVal(ID,
2296 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002297}
2298
2299/// DefineBB - Define the specified basic block, which is either named or
2300/// unnamed. If there is an error, this returns null otherwise it returns
2301/// the block being defined.
2302BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2303 LocTy Loc) {
2304 BasicBlock *BB;
2305 if (Name.empty())
2306 BB = GetBB(NumberedVals.size(), Loc);
2307 else
2308 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002309 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002310
Chris Lattnerac161bf2009-01-02 07:01:27 +00002311 // Move the block to the end of the function. Forward ref'd blocks are
2312 // inserted wherever they happen to be referenced.
2313 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002314
Chris Lattnerac161bf2009-01-02 07:01:27 +00002315 // Remove the block from forward ref sets.
2316 if (Name.empty()) {
2317 ForwardRefValIDs.erase(NumberedVals.size());
2318 NumberedVals.push_back(BB);
2319 } else {
2320 // BB forward references are already in the function symbol table.
2321 ForwardRefVals.erase(Name);
2322 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002323
Chris Lattnerac161bf2009-01-02 07:01:27 +00002324 return BB;
2325}
2326
2327//===----------------------------------------------------------------------===//
2328// Constants.
2329//===----------------------------------------------------------------------===//
2330
2331/// ParseValID - Parse an abstract value that doesn't necessarily have a
2332/// type implied. For example, if we parse "4" we don't know what integer type
2333/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002334/// sanity. PFS is used to convert function-local operands of metadata (since
2335/// metadata operands are not just parsed here but also converted to values).
2336/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002337bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002338 ID.Loc = Lex.getLoc();
2339 switch (Lex.getKind()) {
2340 default: return TokError("expected value token");
2341 case lltok::GlobalID: // @42
2342 ID.UIntVal = Lex.getUIntVal();
2343 ID.Kind = ValID::t_GlobalID;
2344 break;
2345 case lltok::GlobalVar: // @foo
2346 ID.StrVal = Lex.getStrVal();
2347 ID.Kind = ValID::t_GlobalName;
2348 break;
2349 case lltok::LocalVarID: // %42
2350 ID.UIntVal = Lex.getUIntVal();
2351 ID.Kind = ValID::t_LocalID;
2352 break;
2353 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002354 ID.StrVal = Lex.getStrVal();
2355 ID.Kind = ValID::t_LocalName;
2356 break;
Dan Gohman8939ba332010-07-14 18:26:50 +00002357 case lltok::exclaim: // !42, !{...}, or !"foo"
2358 return ParseMetadataValue(ID, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002359 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002360 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002361 ID.Kind = ValID::t_APSInt;
2362 break;
2363 case lltok::APFloat:
2364 ID.APFloatVal = Lex.getAPFloatVal();
2365 ID.Kind = ValID::t_APFloat;
2366 break;
2367 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002368 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002369 ID.Kind = ValID::t_Constant;
2370 break;
2371 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002372 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002373 ID.Kind = ValID::t_Constant;
2374 break;
2375 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2376 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2377 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002378
Chris Lattnerac161bf2009-01-02 07:01:27 +00002379 case lltok::lbrace: {
2380 // ValID ::= '{' ConstVector '}'
2381 Lex.Lex();
2382 SmallVector<Constant*, 16> Elts;
2383 if (ParseGlobalValueVector(Elts) ||
2384 ParseToken(lltok::rbrace, "expected end of struct constant"))
2385 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002386
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002387 ID.ConstantStructElts = new Constant*[Elts.size()];
2388 ID.UIntVal = Elts.size();
2389 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2390 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002391 return false;
2392 }
2393 case lltok::less: {
2394 // ValID ::= '<' ConstVector '>' --> Vector.
2395 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2396 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002397 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002398
Chris Lattnerac161bf2009-01-02 07:01:27 +00002399 SmallVector<Constant*, 16> Elts;
2400 LocTy FirstEltLoc = Lex.getLoc();
2401 if (ParseGlobalValueVector(Elts) ||
2402 (isPackedStruct &&
2403 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2404 ParseToken(lltok::greater, "expected end of constant"))
2405 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002406
Chris Lattnerac161bf2009-01-02 07:01:27 +00002407 if (isPackedStruct) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002408 ID.ConstantStructElts = new Constant*[Elts.size()];
2409 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2410 ID.UIntVal = Elts.size();
2411 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002412 return false;
2413 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002414
Chris Lattnerac161bf2009-01-02 07:01:27 +00002415 if (Elts.empty())
2416 return Error(ID.Loc, "constant vector must not be empty");
2417
Duncan Sands9dff9be2010-02-15 16:12:20 +00002418 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002419 !Elts[0]->getType()->isFloatingPointTy() &&
2420 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002421 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002422 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002423
Chris Lattnerac161bf2009-01-02 07:01:27 +00002424 // Verify that all the vector elements have the same type.
2425 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2426 if (Elts[i]->getType() != Elts[0]->getType())
2427 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002428 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002429 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002430
Chris Lattner69229312011-02-15 00:14:00 +00002431 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002432 ID.Kind = ValID::t_Constant;
2433 return false;
2434 }
2435 case lltok::lsquare: { // Array Constant
2436 Lex.Lex();
2437 SmallVector<Constant*, 16> Elts;
2438 LocTy FirstEltLoc = Lex.getLoc();
2439 if (ParseGlobalValueVector(Elts) ||
2440 ParseToken(lltok::rsquare, "expected end of array constant"))
2441 return true;
2442
2443 // Handle empty element.
2444 if (Elts.empty()) {
2445 // Use undef instead of an array because it's inconvenient to determine
2446 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002447 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002448 return false;
2449 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002450
Chris Lattnerac161bf2009-01-02 07:01:27 +00002451 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002452 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002453 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002454
Owen Anderson4056ca92009-07-29 22:17:13 +00002455 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002456
Chris Lattnerac161bf2009-01-02 07:01:27 +00002457 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002458 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002459 if (Elts[i]->getType() != Elts[0]->getType())
2460 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002461 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002462 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002463 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002464
Jay Foad83be3612011-06-22 09:24:39 +00002465 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002466 ID.Kind = ValID::t_Constant;
2467 return false;
2468 }
2469 case lltok::kw_c: // c "foo"
2470 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002471 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2472 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002473 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2474 ID.Kind = ValID::t_Constant;
2475 return false;
2476
2477 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002478 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2479 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002480 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002481 Lex.Lex();
2482 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002483 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002484 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002485 ParseStringConstant(ID.StrVal) ||
2486 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002487 ParseToken(lltok::StringConstant, "expected constraint string"))
2488 return true;
2489 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002490 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002491 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002492 ID.Kind = ValID::t_InlineAsm;
2493 return false;
2494 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002495
Chris Lattner3432c622009-10-28 03:39:23 +00002496 case lltok::kw_blockaddress: {
2497 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2498 Lex.Lex();
2499
2500 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002501
Chris Lattner3432c622009-10-28 03:39:23 +00002502 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2503 ParseValID(Fn) ||
2504 ParseToken(lltok::comma, "expected comma in block address expression")||
2505 ParseValID(Label) ||
2506 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2507 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002508
Chris Lattner3432c622009-10-28 03:39:23 +00002509 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2510 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002511 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002512 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002513
Chris Lattner3432c622009-10-28 03:39:23 +00002514 // Make a global variable as a placeholder for this reference.
2515 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2516 false, GlobalValue::InternalLinkage,
Craig Topper2617dcc2014-04-15 06:32:26 +00002517 nullptr, "");
Chris Lattner3432c622009-10-28 03:39:23 +00002518 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2519 ID.ConstantVal = FwdRef;
2520 ID.Kind = ValID::t_Constant;
2521 return false;
2522 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002523
Chris Lattnerac161bf2009-01-02 07:01:27 +00002524 case lltok::kw_trunc:
2525 case lltok::kw_zext:
2526 case lltok::kw_sext:
2527 case lltok::kw_fptrunc:
2528 case lltok::kw_fpext:
2529 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002530 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002531 case lltok::kw_uitofp:
2532 case lltok::kw_sitofp:
2533 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002534 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002535 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002536 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002537 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002538 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002539 Constant *SrcVal;
2540 Lex.Lex();
2541 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2542 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002543 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002544 ParseType(DestTy) ||
2545 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2546 return true;
2547 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2548 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002549 getTypeString(SrcVal->getType()) + "' to '" +
2550 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002551 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002552 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002553 ID.Kind = ValID::t_Constant;
2554 return false;
2555 }
2556 case lltok::kw_extractvalue: {
2557 Lex.Lex();
2558 Constant *Val;
2559 SmallVector<unsigned, 4> Indices;
2560 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2561 ParseGlobalTypeAndValue(Val) ||
2562 ParseIndexList(Indices) ||
2563 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2564 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002565
Chris Lattner392be582010-02-12 20:49:41 +00002566 if (!Val->getType()->isAggregateType())
2567 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002568 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002569 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002570 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002571 ID.Kind = ValID::t_Constant;
2572 return false;
2573 }
2574 case lltok::kw_insertvalue: {
2575 Lex.Lex();
2576 Constant *Val0, *Val1;
2577 SmallVector<unsigned, 4> Indices;
2578 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2579 ParseGlobalTypeAndValue(Val0) ||
2580 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2581 ParseGlobalTypeAndValue(Val1) ||
2582 ParseIndexList(Indices) ||
2583 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2584 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002585 if (!Val0->getType()->isAggregateType())
2586 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002587 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002588 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002589 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002590 ID.Kind = ValID::t_Constant;
2591 return false;
2592 }
2593 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002594 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002595 unsigned PredVal, Opc = Lex.getUIntVal();
2596 Constant *Val0, *Val1;
2597 Lex.Lex();
2598 if (ParseCmpPredicate(PredVal, Opc) ||
2599 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2600 ParseGlobalTypeAndValue(Val0) ||
2601 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2602 ParseGlobalTypeAndValue(Val1) ||
2603 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2604 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002605
Chris Lattnerac161bf2009-01-02 07:01:27 +00002606 if (Val0->getType() != Val1->getType())
2607 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002608
Chris Lattnerac161bf2009-01-02 07:01:27 +00002609 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002610
Chris Lattnerac161bf2009-01-02 07:01:27 +00002611 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002612 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002613 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002614 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002615 } else {
2616 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002617 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002618 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002619 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002620 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002621 }
2622 ID.Kind = ValID::t_Constant;
2623 return false;
2624 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002625
Chris Lattnerac161bf2009-01-02 07:01:27 +00002626 // Binary Operators.
2627 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002628 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002629 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002630 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002631 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002632 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002633 case lltok::kw_udiv:
2634 case lltok::kw_sdiv:
2635 case lltok::kw_fdiv:
2636 case lltok::kw_urem:
2637 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002638 case lltok::kw_frem:
2639 case lltok::kw_shl:
2640 case lltok::kw_lshr:
2641 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002642 bool NUW = false;
2643 bool NSW = false;
2644 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002645 unsigned Opc = Lex.getUIntVal();
2646 Constant *Val0, *Val1;
2647 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002648 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002649 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2650 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002651 if (EatIfPresent(lltok::kw_nuw))
2652 NUW = true;
2653 if (EatIfPresent(lltok::kw_nsw)) {
2654 NSW = true;
2655 if (EatIfPresent(lltok::kw_nuw))
2656 NUW = true;
2657 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002658 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2659 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002660 if (EatIfPresent(lltok::kw_exact))
2661 Exact = true;
2662 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002663 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2664 ParseGlobalTypeAndValue(Val0) ||
2665 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2666 ParseGlobalTypeAndValue(Val1) ||
2667 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2668 return true;
2669 if (Val0->getType() != Val1->getType())
2670 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002671 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002672 if (NUW)
2673 return Error(ModifierLoc, "nuw only applies to integer operations");
2674 if (NSW)
2675 return Error(ModifierLoc, "nsw only applies to integer operations");
2676 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002677 // Check that the type is valid for the operator.
2678 switch (Opc) {
2679 case Instruction::Add:
2680 case Instruction::Sub:
2681 case Instruction::Mul:
2682 case Instruction::UDiv:
2683 case Instruction::SDiv:
2684 case Instruction::URem:
2685 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002686 case Instruction::Shl:
2687 case Instruction::AShr:
2688 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002689 if (!Val0->getType()->isIntOrIntVectorTy())
2690 return Error(ID.Loc, "constexpr requires integer operands");
2691 break;
2692 case Instruction::FAdd:
2693 case Instruction::FSub:
2694 case Instruction::FMul:
2695 case Instruction::FDiv:
2696 case Instruction::FRem:
2697 if (!Val0->getType()->isFPOrFPVectorTy())
2698 return Error(ID.Loc, "constexpr requires fp operands");
2699 break;
2700 default: llvm_unreachable("Unknown binary operator!");
2701 }
Dan Gohman1b849082009-09-07 23:54:19 +00002702 unsigned Flags = 0;
2703 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2704 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002705 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002706 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002707 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002708 ID.Kind = ValID::t_Constant;
2709 return false;
2710 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002711
Chris Lattnerac161bf2009-01-02 07:01:27 +00002712 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002713 case lltok::kw_and:
2714 case lltok::kw_or:
2715 case lltok::kw_xor: {
2716 unsigned Opc = Lex.getUIntVal();
2717 Constant *Val0, *Val1;
2718 Lex.Lex();
2719 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2720 ParseGlobalTypeAndValue(Val0) ||
2721 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2722 ParseGlobalTypeAndValue(Val1) ||
2723 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2724 return true;
2725 if (Val0->getType() != Val1->getType())
2726 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002727 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002728 return Error(ID.Loc,
2729 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002730 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002731 ID.Kind = ValID::t_Constant;
2732 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002733 }
2734
Chris Lattnerac161bf2009-01-02 07:01:27 +00002735 case lltok::kw_getelementptr:
2736 case lltok::kw_shufflevector:
2737 case lltok::kw_insertelement:
2738 case lltok::kw_extractelement:
2739 case lltok::kw_select: {
2740 unsigned Opc = Lex.getUIntVal();
2741 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002742 bool InBounds = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002743 Lex.Lex();
Dan Gohman1639c392009-07-27 21:53:46 +00002744 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002745 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002746 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2747 ParseGlobalValueVector(Elts) ||
2748 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2749 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002750
Chris Lattnerac161bf2009-01-02 07:01:27 +00002751 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002752 if (Elts.size() == 0 ||
2753 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002754 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002755
Jay Foaded8db7d2011-07-21 14:31:17 +00002756 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foadd1b78492011-07-25 09:48:08 +00002757 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002758 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad2f5fc8c2011-07-21 15:15:37 +00002759 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2760 InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002761 } else if (Opc == Instruction::Select) {
2762 if (Elts.size() != 3)
2763 return Error(ID.Loc, "expected three operands to select");
2764 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2765 Elts[2]))
2766 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002767 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002768 } else if (Opc == Instruction::ShuffleVector) {
2769 if (Elts.size() != 3)
2770 return Error(ID.Loc, "expected three operands to shufflevector");
2771 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2772 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002773 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002774 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002775 } else if (Opc == Instruction::ExtractElement) {
2776 if (Elts.size() != 2)
2777 return Error(ID.Loc, "expected two operands to extractelement");
2778 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2779 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002780 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002781 } else {
2782 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2783 if (Elts.size() != 3)
2784 return Error(ID.Loc, "expected three operands to insertelement");
2785 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2786 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002787 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002788 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002789 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002790
Chris Lattnerac161bf2009-01-02 07:01:27 +00002791 ID.Kind = ValID::t_Constant;
2792 return false;
2793 }
2794 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002795
Chris Lattnerac161bf2009-01-02 07:01:27 +00002796 Lex.Lex();
2797 return false;
2798}
2799
2800/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002801bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002802 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002803 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002804 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002805 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002806 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002807 if (V && !(C = dyn_cast<Constant>(V)))
2808 return Error(ID.Loc, "global values must be constants");
2809 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002810}
2811
Victor Hernandez9d75c962010-01-11 22:31:58 +00002812bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002813 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002814 return ParseType(Ty) ||
2815 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002816}
2817
2818/// ParseGlobalValueVector
2819/// ::= /*empty*/
2820/// ::= TypeAndValue (',' TypeAndValue)*
2821bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2822 // Empty list.
2823 if (Lex.getKind() == lltok::rbrace ||
2824 Lex.getKind() == lltok::rsquare ||
2825 Lex.getKind() == lltok::greater ||
2826 Lex.getKind() == lltok::rparen)
2827 return false;
2828
2829 Constant *C;
2830 if (ParseGlobalTypeAndValue(C)) return true;
2831 Elts.push_back(C);
2832
2833 while (EatIfPresent(lltok::comma)) {
2834 if (ParseGlobalTypeAndValue(C)) return true;
2835 Elts.push_back(C);
2836 }
2837
2838 return false;
2839}
2840
Dan Gohmanc828c542010-08-24 02:24:03 +00002841bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2842 assert(Lex.getKind() == lltok::lbrace);
2843 Lex.Lex();
2844
2845 SmallVector<Value*, 16> Elts;
2846 if (ParseMDNodeVector(Elts, PFS) ||
2847 ParseToken(lltok::rbrace, "expected end of metadata node"))
2848 return true;
2849
Jay Foad5514afe2011-04-21 19:59:31 +00002850 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002851 ID.Kind = ValID::t_MDNode;
2852 return false;
2853}
2854
Dan Gohman8939ba332010-07-14 18:26:50 +00002855/// ParseMetadataValue
2856/// ::= !42
2857/// ::= !{...}
2858/// ::= !"string"
2859bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2860 assert(Lex.getKind() == lltok::exclaim);
2861 Lex.Lex();
2862
2863 // MDNode:
2864 // !{ ... }
Dan Gohmanc828c542010-08-24 02:24:03 +00002865 if (Lex.getKind() == lltok::lbrace)
2866 return ParseMetadataListValue(ID, PFS);
Dan Gohman8939ba332010-07-14 18:26:50 +00002867
2868 // Standalone metadata reference
2869 // !42
2870 if (Lex.getKind() == lltok::APSInt) {
2871 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2872 ID.Kind = ValID::t_MDNode;
2873 return false;
2874 }
2875
2876 // MDString:
2877 // ::= '!' STRINGCONSTANT
2878 if (ParseMDString(ID.MDStringVal)) return true;
2879 ID.Kind = ValID::t_MDString;
2880 return false;
2881}
2882
Victor Hernandez9d75c962010-01-11 22:31:58 +00002883
2884//===----------------------------------------------------------------------===//
2885// Function Parsing.
2886//===----------------------------------------------------------------------===//
2887
Chris Lattner229907c2011-07-18 04:54:35 +00002888bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00002889 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002890 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002891 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002892
Chris Lattnerac161bf2009-01-02 07:01:27 +00002893 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002894 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002895 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2896 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002897 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002898 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00002899 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2900 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002901 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002902 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00002903 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002904 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00002905 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002906 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2907 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00002908 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00002909 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00002910 return false;
2911 }
2912 case ValID::t_MDNode:
2913 if (!Ty->isMetadataTy())
2914 return Error(ID.Loc, "metadata value must have metadata type");
2915 V = ID.MDNodeVal;
2916 return false;
2917 case ValID::t_MDString:
2918 if (!Ty->isMetadataTy())
2919 return Error(ID.Loc, "metadata value must have metadata type");
2920 V = ID.MDStringVal;
2921 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002922 case ValID::t_GlobalName:
2923 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002924 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002925 case ValID::t_GlobalID:
2926 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002927 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002928 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002929 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002930 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00002931 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00002932 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002933 return false;
2934 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002935 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002936 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2937 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002938
Dan Gohman518cda42011-12-17 00:04:22 +00002939 // The lexer has no type info, so builds all half, float, and double FP
2940 // constants as double. Fix this here. Long double does not need this.
2941 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002942 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00002943 if (Ty->isHalfTy())
2944 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2945 &Ignored);
2946 else if (Ty->isFloatTy())
2947 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2948 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002949 }
Owen Anderson69c464d2009-07-27 20:59:43 +00002950 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002951
Chris Lattner8f57d29e2009-01-05 18:24:23 +00002952 if (V->getType() != Ty)
2953 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002954 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002955
Chris Lattnerac161bf2009-01-02 07:01:27 +00002956 return false;
2957 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00002958 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002959 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002960 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002961 return false;
2962 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00002963 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002964 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00002965 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002966 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002967 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00002968 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00002969 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00002970 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00002971 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00002972 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002973 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00002974 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002975 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002976 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00002977 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002978 return false;
2979 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00002980 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002981 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00002982
Chris Lattnerac161bf2009-01-02 07:01:27 +00002983 V = ID.ConstantVal;
2984 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002985 case ValID::t_ConstantStruct:
2986 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00002987 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002988 if (ST->getNumElements() != ID.UIntVal)
2989 return Error(ID.Loc,
2990 "initializer with struct type has wrong # elements");
2991 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2992 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002993
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002994 // Verify that the elements are compatible with the structtype.
2995 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2996 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2997 return Error(ID.Loc, "element " + Twine(i) +
2998 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002999
Frits van Bommel717d7ed2011-07-18 12:00:32 +00003000 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
3001 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003002 } else
3003 return Error(ID.Loc, "constant expression type mismatch");
3004 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003005 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00003006 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003007}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003008
Chris Lattner229907c2011-07-18 04:54:35 +00003009bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003010 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003011 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003012 return ParseValID(ID, PFS) ||
3013 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003014}
3015
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003016bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003017 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003018 return ParseType(Ty) ||
3019 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003020}
3021
Chris Lattner3ed871f2009-10-27 19:13:16 +00003022bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
3023 PerFunctionState &PFS) {
3024 Value *V;
3025 Loc = Lex.getLoc();
3026 if (ParseTypeAndValue(V, PFS)) return true;
3027 if (!isa<BasicBlock>(V))
3028 return Error(Loc, "expected a basic block");
3029 BB = cast<BasicBlock>(V);
3030 return false;
3031}
3032
3033
Chris Lattnerac161bf2009-01-02 07:01:27 +00003034/// FunctionHeader
3035/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00003036/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003037/// OptionalAlign OptGC OptionalPrefix
Chris Lattnerac161bf2009-01-02 07:01:27 +00003038bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
3039 // Parse the linkage.
3040 LocTy LinkageLoc = Lex.getLoc();
3041 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003042
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00003043 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00003044 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00003045 AttrBuilder RetAttrs;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003046 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003047 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003048 LocTy RetTypeLoc = Lex.getLoc();
3049 if (ParseOptionalLinkage(Linkage) ||
3050 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00003051 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003052 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003053 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003054 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003055 return true;
3056
3057 // Verify that the linkage is ok.
3058 switch ((GlobalValue::LinkageTypes)Linkage) {
3059 case GlobalValue::ExternalLinkage:
3060 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00003061 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003062 if (isDefine)
3063 return Error(LinkageLoc, "invalid linkage for function definition");
3064 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00003065 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003066 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00003067 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00003068 case GlobalValue::LinkOnceAnyLinkage:
3069 case GlobalValue::LinkOnceODRLinkage:
3070 case GlobalValue::WeakAnyLinkage:
3071 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003072 if (!isDefine)
3073 return Error(LinkageLoc, "invalid linkage for function declaration");
3074 break;
3075 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00003076 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003077 return Error(LinkageLoc, "invalid function linkage type");
3078 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003079
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00003080 if (!isValidVisibilityForLinkage(Visibility, Linkage))
3081 return Error(LinkageLoc,
3082 "symbol with local linkage must have default visibility");
3083
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003084 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003085 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003086
Chris Lattnerac161bf2009-01-02 07:01:27 +00003087 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00003088
3089 std::string FunctionName;
3090 if (Lex.getKind() == lltok::GlobalVar) {
3091 FunctionName = Lex.getStrVal();
3092 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
3093 unsigned NameID = Lex.getUIntVal();
3094
3095 if (NameID != NumberedVals.size())
3096 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003097 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00003098 } else {
3099 return TokError("expected function name");
3100 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003101
Chris Lattner3822f632009-01-02 08:05:26 +00003102 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003103
Chris Lattner3822f632009-01-02 08:05:26 +00003104 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003105 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003106
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003107 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003108 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00003109 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003110 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00003111 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003112 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003113 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00003114 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003115 bool UnnamedAddr;
3116 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00003117 Constant *Prefix = nullptr;
Chris Lattner3822f632009-01-02 08:05:26 +00003118
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003119 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00003120 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
3121 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003122 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00003123 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00003124 (EatIfPresent(lltok::kw_section) &&
3125 ParseStringConstant(Section)) ||
3126 ParseOptionalAlignment(Alignment) ||
3127 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003128 ParseStringConstant(GC)) ||
3129 (EatIfPresent(lltok::kw_prefix) &&
3130 ParseGlobalTypeAndValue(Prefix)))
Chris Lattner3822f632009-01-02 08:05:26 +00003131 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003132
Michael Gottesman41748d72013-06-27 00:25:01 +00003133 if (FuncAttrs.contains(Attribute::Builtin))
3134 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00003135
Chris Lattnerac161bf2009-01-02 07:01:27 +00003136 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00003137 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00003138 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003139 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003140 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003141
Chris Lattnerac161bf2009-01-02 07:01:27 +00003142 // Okay, if we got here, the function is syntactically valid. Convert types
3143 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00003144 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00003145 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003146
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003147 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003148 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3149 AttributeSet::ReturnIndex,
3150 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003151
Chris Lattnerac161bf2009-01-02 07:01:27 +00003152 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003153 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003154 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3155 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003156 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3157 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003158 }
3159
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003160 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003161 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3162 AttributeSet::FunctionIndex,
3163 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003164
Bill Wendlinge94d8432012-12-07 23:16:57 +00003165 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003166
Bill Wendling749a43d2012-12-30 13:50:49 +00003167 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003168 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
3169
Chris Lattner229907c2011-07-18 04:54:35 +00003170 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00003171 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00003172 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003173
Craig Topper2617dcc2014-04-15 06:32:26 +00003174 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003175 if (!FunctionName.empty()) {
3176 // If this was a definition of a forward reference, remove the definition
3177 // from the forward reference table and fill in the forward ref.
3178 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
3179 ForwardRefVals.find(FunctionName);
3180 if (FRVI != ForwardRefVals.end()) {
3181 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00003182 if (!Fn)
3183 return Error(FRVI->second.second, "invalid forward reference to "
3184 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00003185 if (Fn->getType() != PFT)
3186 return Error(FRVI->second.second, "invalid forward reference to "
3187 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003188
Chris Lattnerac161bf2009-01-02 07:01:27 +00003189 ForwardRefVals.erase(FRVI);
3190 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00003191 // Reject redefinitions.
3192 return Error(NameLoc, "invalid redefinition of function '" +
3193 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00003194 } else if (M->getNamedValue(FunctionName)) {
3195 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003196 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003197
Dan Gohman399d6ae2009-08-29 23:37:49 +00003198 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003199 // If this is a definition of a forward referenced function, make sure the
3200 // types agree.
3201 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
3202 = ForwardRefValIDs.find(NumberedVals.size());
3203 if (I != ForwardRefValIDs.end()) {
3204 Fn = cast<Function>(I->second.first);
3205 if (Fn->getType() != PFT)
3206 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00003207 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003208 ForwardRefValIDs.erase(I);
3209 }
3210 }
3211
Craig Topper2617dcc2014-04-15 06:32:26 +00003212 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003213 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
3214 else // Move the forward-reference to the correct spot in the module.
3215 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
3216
3217 if (FunctionName.empty())
3218 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003219
Chris Lattnerac161bf2009-01-02 07:01:27 +00003220 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3221 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00003222 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003223 Fn->setCallingConv(CC);
3224 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00003225 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003226 Fn->setAlignment(Alignment);
3227 Fn->setSection(Section);
3228 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00003229 Fn->setPrefixData(Prefix);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003230 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003231
Chris Lattnerac161bf2009-01-02 07:01:27 +00003232 // Add all of the arguments we parsed to the function.
3233 Function::arg_iterator ArgIt = Fn->arg_begin();
3234 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3235 // If the argument has a name, insert it into the argument symbol table.
3236 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003237
Chris Lattnerac161bf2009-01-02 07:01:27 +00003238 // Set the name, if it conflicted, it will be auto-renamed.
3239 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003240
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00003241 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003242 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3243 ArgList[i].Name + "'");
3244 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003245
Chris Lattnerac161bf2009-01-02 07:01:27 +00003246 return false;
3247}
3248
3249
3250/// ParseFunctionBody
3251/// ::= '{' BasicBlock+ '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00003252///
3253bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00003254 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003255 return TokError("expected '{' in function body");
3256 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003257
Chris Lattner3432c622009-10-28 03:39:23 +00003258 int FunctionNumber = -1;
3259 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003260
Chris Lattner3432c622009-10-28 03:39:23 +00003261 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003262
Chris Lattnerbbddd962010-01-09 19:20:07 +00003263 // We need at least one basic block.
Chris Lattner4649a732011-06-17 06:42:57 +00003264 if (Lex.getKind() == lltok::rbrace)
Chris Lattnerbbddd962010-01-09 19:20:07 +00003265 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003266
Chris Lattner4649a732011-06-17 06:42:57 +00003267 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003268 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003269
Chris Lattnerac161bf2009-01-02 07:01:27 +00003270 // Eat the }.
3271 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003272
Chris Lattnerac161bf2009-01-02 07:01:27 +00003273 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00003274 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003275}
3276
3277/// ParseBasicBlock
3278/// ::= LabelStr? Instruction*
3279bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3280 // If this basic block starts out with a name, remember it.
3281 std::string Name;
3282 LocTy NameLoc = Lex.getLoc();
3283 if (Lex.getKind() == lltok::LabelStr) {
3284 Name = Lex.getStrVal();
3285 Lex.Lex();
3286 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003287
Chris Lattnerac161bf2009-01-02 07:01:27 +00003288 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003289 if (!BB) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003290
Chris Lattnerac161bf2009-01-02 07:01:27 +00003291 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003292
Chris Lattnerac161bf2009-01-02 07:01:27 +00003293 // Parse the instructions in this block until we get a terminator.
3294 Instruction *Inst;
3295 do {
3296 // This instruction may have three possibilities for a name: a) none
3297 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3298 LocTy NameLoc = Lex.getLoc();
3299 int NameID = -1;
3300 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003301
Chris Lattnerac161bf2009-01-02 07:01:27 +00003302 if (Lex.getKind() == lltok::LocalVarID) {
3303 NameID = Lex.getUIntVal();
3304 Lex.Lex();
3305 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3306 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00003307 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003308 NameStr = Lex.getStrVal();
3309 Lex.Lex();
3310 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3311 return true;
3312 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003313
Chris Lattner77b89dc2009-12-30 05:23:43 +00003314 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00003315 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00003316 case InstError: return true;
3317 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003318 BB->getInstList().push_back(Inst);
3319
Chris Lattner77b89dc2009-12-30 05:23:43 +00003320 // With a normal result, we check to see if the instruction is followed by
3321 // a comma and metadata.
3322 if (EatIfPresent(lltok::comma))
Dan Gohman338d9a42010-08-24 02:05:17 +00003323 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003324 return true;
3325 break;
3326 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00003327 BB->getInstList().push_back(Inst);
3328
Chris Lattner77b89dc2009-12-30 05:23:43 +00003329 // If the instruction parser ate an extra comma at the end of it, it
3330 // *must* be followed by metadata.
Dan Gohman338d9a42010-08-24 02:05:17 +00003331 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattner77b89dc2009-12-30 05:23:43 +00003332 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003333 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00003334 }
Devang Patelea8a4b92009-09-17 23:04:48 +00003335
Chris Lattnerac161bf2009-01-02 07:01:27 +00003336 // Set the name on the instruction.
3337 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3338 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003339
Chris Lattnerac161bf2009-01-02 07:01:27 +00003340 return false;
3341}
3342
3343//===----------------------------------------------------------------------===//
3344// Instruction Parsing.
3345//===----------------------------------------------------------------------===//
3346
3347/// ParseInstruction - Parse one of the many different instructions.
3348///
Chris Lattner77b89dc2009-12-30 05:23:43 +00003349int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3350 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003351 lltok::Kind Token = Lex.getKind();
3352 if (Token == lltok::Eof)
3353 return TokError("found end of file when expecting more instructions");
3354 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00003355 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00003356 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003357
Chris Lattnerac161bf2009-01-02 07:01:27 +00003358 switch (Token) {
3359 default: return Error(Loc, "expected instruction opcode");
3360 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00003361 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003362 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3363 case lltok::kw_br: return ParseBr(Inst, PFS);
3364 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003365 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003366 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00003367 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003368 // Binary Operators.
3369 case lltok::kw_add:
3370 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003371 case lltok::kw_mul:
3372 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00003373 bool NUW = EatIfPresent(lltok::kw_nuw);
3374 bool NSW = EatIfPresent(lltok::kw_nsw);
3375 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003376
Chris Lattnera676c0f2011-02-07 16:40:21 +00003377 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003378
Chris Lattnera676c0f2011-02-07 16:40:21 +00003379 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3380 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3381 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003382 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003383 case lltok::kw_fadd:
3384 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00003385 case lltok::kw_fmul:
3386 case lltok::kw_fdiv:
3387 case lltok::kw_frem: {
3388 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3389 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3390 if (Res != 0)
3391 return Res;
3392 if (FMF.any())
3393 Inst->setFastMathFlags(FMF);
3394 return 0;
3395 }
Dan Gohmana5b96452009-06-04 22:49:04 +00003396
Chris Lattner35315d02011-02-06 21:44:57 +00003397 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003398 case lltok::kw_udiv:
3399 case lltok::kw_lshr:
3400 case lltok::kw_ashr: {
3401 bool Exact = EatIfPresent(lltok::kw_exact);
3402
3403 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3404 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3405 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00003406 }
3407
Chris Lattnerac161bf2009-01-02 07:01:27 +00003408 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00003409 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003410 case lltok::kw_and:
3411 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00003412 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003413 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003414 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003415 // Casts.
3416 case lltok::kw_trunc:
3417 case lltok::kw_zext:
3418 case lltok::kw_sext:
3419 case lltok::kw_fptrunc:
3420 case lltok::kw_fpext:
3421 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003422 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003423 case lltok::kw_uitofp:
3424 case lltok::kw_sitofp:
3425 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003426 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003427 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00003428 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003429 // Other.
3430 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00003431 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003432 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3433 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3434 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3435 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00003436 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00003437 // Call.
3438 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
3439 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
3440 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003441 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00003442 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00003443 case lltok::kw_load: return ParseLoad(Inst, PFS);
3444 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00003445 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3446 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00003447 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003448 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3449 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3450 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3451 }
3452}
3453
3454/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3455bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003456 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003457 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003458 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003459 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3460 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3461 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3462 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3463 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3464 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3465 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3466 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3467 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3468 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3469 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3470 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3471 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3472 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3473 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3474 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3475 }
3476 } else {
3477 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00003478 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003479 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3480 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3481 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3482 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3483 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3484 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3485 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3486 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3487 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3488 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3489 }
3490 }
3491 Lex.Lex();
3492 return false;
3493}
3494
3495//===----------------------------------------------------------------------===//
3496// Terminator Instructions.
3497//===----------------------------------------------------------------------===//
3498
3499/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00003500/// ::= 'ret' void (',' !dbg, !1)*
3501/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00003502bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003503 PerFunctionState &PFS) {
3504 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00003505 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00003506 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003507
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003508 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003509
Chris Lattnerfdd87902009-10-05 05:54:46 +00003510 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003511 if (!ResType->isVoidTy())
3512 return Error(TypeLoc, "value doesn't match function result type '" +
3513 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003514
Owen Anderson55f1c092009-08-13 21:58:54 +00003515 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003516 return false;
3517 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003518
Chris Lattnerac161bf2009-01-02 07:01:27 +00003519 Value *RV;
3520 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003521
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003522 if (ResType != RV->getType())
3523 return Error(TypeLoc, "value doesn't match function result type '" +
3524 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003525
Owen Anderson55f1c092009-08-13 21:58:54 +00003526 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00003527 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003528}
3529
3530
3531/// ParseBr
3532/// ::= 'br' TypeAndValue
3533/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3534bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3535 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003536 Value *Op0;
3537 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003538 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003539
Chris Lattnerac161bf2009-01-02 07:01:27 +00003540 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3541 Inst = BranchInst::Create(BB);
3542 return false;
3543 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003544
Owen Anderson55f1c092009-08-13 21:58:54 +00003545 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003546 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003547
Chris Lattnerac161bf2009-01-02 07:01:27 +00003548 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003549 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003550 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003551 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003552 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003553
Chris Lattner3ed871f2009-10-27 19:13:16 +00003554 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003555 return false;
3556}
3557
3558/// ParseSwitch
3559/// Instruction
3560/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3561/// JumpTable
3562/// ::= (TypeAndValue ',' TypeAndValue)*
3563bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3564 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003565 Value *Cond;
3566 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003567 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3568 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003569 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003570 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3571 return true;
3572
Duncan Sands19d0b472010-02-16 11:11:14 +00003573 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003574 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003575
Chris Lattnerac161bf2009-01-02 07:01:27 +00003576 // Parse the jump table pairs.
3577 SmallPtrSet<Value*, 32> SeenCases;
3578 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3579 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003580 Value *Constant;
3581 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003582
Chris Lattnerac161bf2009-01-02 07:01:27 +00003583 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3584 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003585 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003586 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003587
Chris Lattnerac161bf2009-01-02 07:01:27 +00003588 if (!SeenCases.insert(Constant))
3589 return Error(CondLoc, "duplicate case value in switch");
3590 if (!isa<ConstantInt>(Constant))
3591 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003592
Chris Lattner3ed871f2009-10-27 19:13:16 +00003593 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003594 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003595
Chris Lattnerac161bf2009-01-02 07:01:27 +00003596 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003597
Chris Lattner3ed871f2009-10-27 19:13:16 +00003598 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003599 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3600 SI->addCase(Table[i].first, Table[i].second);
3601 Inst = SI;
3602 return false;
3603}
3604
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003605/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00003606/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003607/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3608bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003609 LocTy AddrLoc;
3610 Value *Address;
3611 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003612 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3613 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00003614 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003615
Duncan Sands19d0b472010-02-16 11:11:14 +00003616 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003617 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003618
Chris Lattner3ed871f2009-10-27 19:13:16 +00003619 // Parse the destination list.
3620 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003621
Chris Lattner3ed871f2009-10-27 19:13:16 +00003622 if (Lex.getKind() != lltok::rsquare) {
3623 BasicBlock *DestBB;
3624 if (ParseTypeAndBasicBlock(DestBB, PFS))
3625 return true;
3626 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003627
Chris Lattner3ed871f2009-10-27 19:13:16 +00003628 while (EatIfPresent(lltok::comma)) {
3629 if (ParseTypeAndBasicBlock(DestBB, PFS))
3630 return true;
3631 DestList.push_back(DestBB);
3632 }
3633 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003634
Chris Lattner3ed871f2009-10-27 19:13:16 +00003635 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3636 return true;
3637
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003638 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00003639 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3640 IBI->addDestination(DestList[i]);
3641 Inst = IBI;
3642 return false;
3643}
3644
3645
Chris Lattnerac161bf2009-01-02 07:01:27 +00003646/// ParseInvoke
3647/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3648/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3649bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3650 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00003651 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00003652 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00003653 LocTy NoBuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00003654 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00003655 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003656 LocTy RetTypeLoc;
3657 ValID CalleeID;
3658 SmallVector<ParamInfo, 16> ArgList;
3659
Chris Lattner3ed871f2009-10-27 19:13:16 +00003660 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003661 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00003662 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00003663 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003664 ParseValID(CalleeID) ||
3665 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00003666 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
3667 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003668 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003669 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003670 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00003671 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003672 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003673
Chris Lattnerac161bf2009-01-02 07:01:27 +00003674 // If RetType is a non-function pointer type, then this is the short syntax
3675 // for the call, which means that RetType is just the return type. Infer the
3676 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00003677 PointerType *PFTy = nullptr;
3678 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003679 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3680 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3681 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00003682 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003683 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3684 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003685
Chris Lattnerac161bf2009-01-02 07:01:27 +00003686 if (!FunctionType::isValidReturnType(RetType))
3687 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003688
Owen Anderson4056ca92009-07-29 22:17:13 +00003689 Ty = FunctionType::get(RetType, ParamTypes, false);
3690 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003691 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003692
Chris Lattnerac161bf2009-01-02 07:01:27 +00003693 // Look up the callee.
3694 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003695 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003696
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003697 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00003698 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003699 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003700 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3701 AttributeSet::ReturnIndex,
3702 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003703
Chris Lattnerac161bf2009-01-02 07:01:27 +00003704 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003705
Chris Lattnerac161bf2009-01-02 07:01:27 +00003706 // Loop through FunctionType's arguments and ensure they are specified
3707 // correctly. Also, gather any parameter attributes.
3708 FunctionType::param_iterator I = Ty->param_begin();
3709 FunctionType::param_iterator E = Ty->param_end();
3710 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003711 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003712 if (I != E) {
3713 ExpectedTy = *I++;
3714 } else if (!Ty->isVarArg()) {
3715 return Error(ArgList[i].Loc, "too many arguments specified");
3716 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003717
Chris Lattnerac161bf2009-01-02 07:01:27 +00003718 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3719 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003720 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003721 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00003722 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3723 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00003724 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3725 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003726 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003727
Chris Lattnerac161bf2009-01-02 07:01:27 +00003728 if (I != E)
3729 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003730
Bill Wendling3bef2dd2012-09-19 23:54:18 +00003731 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00003732 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3733 AttributeSet::FunctionIndex,
3734 FnAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003735
Bill Wendling3d7b0b82012-12-19 07:18:57 +00003736 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00003737 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003738
Jay Foad5bd375a2011-07-15 08:37:34 +00003739 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003740 II->setCallingConv(CC);
3741 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00003742 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003743 Inst = II;
3744 return false;
3745}
3746
Bill Wendlingf891bf82011-07-31 06:30:59 +00003747/// ParseResume
3748/// ::= 'resume' TypeAndValue
3749bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3750 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00003751 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3752 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003753
Bill Wendlingf891bf82011-07-31 06:30:59 +00003754 ResumeInst *RI = ResumeInst::Create(Exn);
3755 Inst = RI;
3756 return false;
3757}
Chris Lattnerac161bf2009-01-02 07:01:27 +00003758
3759//===----------------------------------------------------------------------===//
3760// Binary Operators.
3761//===----------------------------------------------------------------------===//
3762
3763/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003764/// ::= ArithmeticOps TypeAndValue ',' Value
3765///
3766/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3767/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00003768bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003769 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003770 LocTy Loc; Value *LHS, *RHS;
3771 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3772 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3773 ParseValue(LHS->getType(), RHS, PFS))
3774 return true;
3775
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003776 bool Valid;
3777 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003778 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003779 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00003780 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3781 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003782 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00003783 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3784 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003785 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003786
Chris Lattnereeefa9a2009-01-05 08:24:46 +00003787 if (!Valid)
3788 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003789
Chris Lattnerac161bf2009-01-02 07:01:27 +00003790 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3791 return false;
3792}
3793
3794/// ParseLogical
3795/// ::= ArithmeticOps TypeAndValue ',' Value {
3796bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3797 unsigned Opc) {
3798 LocTy Loc; Value *LHS, *RHS;
3799 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3800 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3801 ParseValue(LHS->getType(), RHS, PFS))
3802 return true;
3803
Duncan Sands9dff9be2010-02-15 16:12:20 +00003804 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003805 return Error(Loc,"instruction requires integer or integer vector operands");
3806
3807 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3808 return false;
3809}
3810
3811
3812/// ParseCompare
3813/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3814/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00003815bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3816 unsigned Opc) {
3817 // Parse the integer/fp comparison predicate.
3818 LocTy Loc;
3819 unsigned Pred;
3820 Value *LHS, *RHS;
3821 if (ParseCmpPredicate(Pred, Opc) ||
3822 ParseTypeAndValue(LHS, Loc, PFS) ||
3823 ParseToken(lltok::comma, "expected ',' after compare value") ||
3824 ParseValue(LHS->getType(), RHS, PFS))
3825 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003826
Chris Lattnerac161bf2009-01-02 07:01:27 +00003827 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003828 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003829 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003830 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003831 } else {
3832 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003833 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00003834 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003835 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003836 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003837 }
3838 return false;
3839}
3840
3841//===----------------------------------------------------------------------===//
3842// Other Instructions.
3843//===----------------------------------------------------------------------===//
3844
3845
3846/// ParseCast
3847/// ::= CastOpc TypeAndValue 'to' Type
3848bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3849 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003850 LocTy Loc;
3851 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003852 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003853 if (ParseTypeAndValue(Op, Loc, PFS) ||
3854 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3855 ParseType(DestTy))
3856 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003857
Chris Lattner89d856e2009-03-01 00:53:13 +00003858 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3859 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003860 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003861 getTypeString(Op->getType()) + "' to '" +
3862 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00003863 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003864 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3865 return false;
3866}
3867
3868/// ParseSelect
3869/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3870bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3871 LocTy Loc;
3872 Value *Op0, *Op1, *Op2;
3873 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3874 ParseToken(lltok::comma, "expected ',' after select condition") ||
3875 ParseTypeAndValue(Op1, PFS) ||
3876 ParseToken(lltok::comma, "expected ',' after select value") ||
3877 ParseTypeAndValue(Op2, PFS))
3878 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003879
Chris Lattnerac161bf2009-01-02 07:01:27 +00003880 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3881 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003882
Chris Lattnerac161bf2009-01-02 07:01:27 +00003883 Inst = SelectInst::Create(Op0, Op1, Op2);
3884 return false;
3885}
3886
Chris Lattnerb55ab542009-01-05 08:18:44 +00003887/// ParseVA_Arg
3888/// ::= 'va_arg' TypeAndValue ',' Type
3889bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003890 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00003891 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00003892 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003893 if (ParseTypeAndValue(Op, PFS) ||
3894 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00003895 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003896 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003897
Chris Lattnerb55ab542009-01-05 08:18:44 +00003898 if (!EltTy->isFirstClassType())
3899 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00003900
3901 Inst = new VAArgInst(Op, EltTy);
3902 return false;
3903}
3904
3905/// ParseExtractElement
3906/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3907bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3908 LocTy Loc;
3909 Value *Op0, *Op1;
3910 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3911 ParseToken(lltok::comma, "expected ',' after extract value") ||
3912 ParseTypeAndValue(Op1, PFS))
3913 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003914
Chris Lattnerac161bf2009-01-02 07:01:27 +00003915 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3916 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003917
Eric Christopherc9742252009-07-25 02:28:41 +00003918 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003919 return false;
3920}
3921
3922/// ParseInsertElement
3923/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3924bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3925 LocTy Loc;
3926 Value *Op0, *Op1, *Op2;
3927 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3928 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3929 ParseTypeAndValue(Op1, PFS) ||
3930 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3931 ParseTypeAndValue(Op2, PFS))
3932 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003933
Chris Lattnerac161bf2009-01-02 07:01:27 +00003934 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00003935 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003936
Chris Lattnerac161bf2009-01-02 07:01:27 +00003937 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3938 return false;
3939}
3940
3941/// ParseShuffleVector
3942/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3943bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3944 LocTy Loc;
3945 Value *Op0, *Op1, *Op2;
3946 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3947 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3948 ParseTypeAndValue(Op1, PFS) ||
3949 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3950 ParseTypeAndValue(Op2, PFS))
3951 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003952
Chris Lattnerac161bf2009-01-02 07:01:27 +00003953 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00003954 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003955
Chris Lattnerac161bf2009-01-02 07:01:27 +00003956 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3957 return false;
3958}
3959
3960/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00003961/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00003962int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003963 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003964 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003965
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003966 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003967 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3968 ParseValue(Ty, Op0, PFS) ||
3969 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003970 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003971 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3972 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003973
Chris Lattnerf4f03422009-12-30 05:27:33 +00003974 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003975 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3976 while (1) {
3977 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003978
Chris Lattner3822f632009-01-02 08:05:26 +00003979 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003980 break;
3981
Chris Lattnerf4f03422009-12-30 05:27:33 +00003982 if (Lex.getKind() == lltok::MetadataVar) {
3983 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00003984 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00003985 }
Devang Patel8f842d32009-10-16 18:45:49 +00003986
Chris Lattner3822f632009-01-02 08:05:26 +00003987 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003988 ParseValue(Ty, Op0, PFS) ||
3989 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00003990 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003991 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3992 return true;
3993 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003994
Chris Lattnerac161bf2009-01-02 07:01:27 +00003995 if (!Ty->isFirstClassType())
3996 return Error(TypeLoc, "phi node must have first class type");
3997
Jay Foad52131342011-03-30 11:28:46 +00003998 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00003999 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
4000 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
4001 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004002 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004003}
4004
Bill Wendlingfae14752011-08-12 20:24:12 +00004005/// ParseLandingPad
4006/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
4007/// Clause
4008/// ::= 'catch' TypeAndValue
4009/// ::= 'filter'
4010/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
4011bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004012 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004013 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00004014
4015 if (ParseType(Ty, TyLoc) ||
4016 ParseToken(lltok::kw_personality, "expected 'personality'") ||
4017 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
4018 return true;
4019
4020 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
4021 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
4022
4023 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
4024 LandingPadInst::ClauseType CT;
4025 if (EatIfPresent(lltok::kw_catch))
4026 CT = LandingPadInst::Catch;
4027 else if (EatIfPresent(lltok::kw_filter))
4028 CT = LandingPadInst::Filter;
4029 else
4030 return TokError("expected 'catch' or 'filter' clause type");
4031
4032 Value *V; LocTy VLoc;
4033 if (ParseTypeAndValue(V, VLoc, PFS)) {
4034 delete LP;
4035 return true;
4036 }
4037
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00004038 // A 'catch' type expects a non-array constant. A filter clause expects an
4039 // array constant.
4040 if (CT == LandingPadInst::Catch) {
4041 if (isa<ArrayType>(V->getType()))
4042 Error(VLoc, "'catch' clause has an invalid type");
4043 } else {
4044 if (!isa<ArrayType>(V->getType()))
4045 Error(VLoc, "'filter' clause has an invalid type");
4046 }
4047
Bill Wendlingfae14752011-08-12 20:24:12 +00004048 LP->addClause(V);
4049 }
4050
4051 Inst = LP;
4052 return false;
4053}
4054
Chris Lattnerac161bf2009-01-02 07:01:27 +00004055/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00004056/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
4057/// ParameterList OptionalAttrs
4058/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
4059/// ParameterList OptionalAttrs
4060/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004061/// ParameterList OptionalAttrs
4062bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00004063 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00004064 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004065 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004066 LocTy BuiltinLoc;
Sandeep Patel68c5f472009-09-02 08:44:58 +00004067 CallingConv::ID CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004068 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004069 LocTy RetTypeLoc;
4070 ValID CalleeID;
4071 SmallVector<ParamInfo, 16> ArgList;
4072 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004073
Reid Kleckner5772b772014-04-24 20:14:34 +00004074 if ((TCK != CallInst::TCK_None &&
4075 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004076 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004077 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004078 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004079 ParseValID(CalleeID) ||
4080 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004081 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004082 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004083 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004084
Chris Lattnerac161bf2009-01-02 07:01:27 +00004085 // If RetType is a non-function pointer type, then this is the short syntax
4086 // for the call, which means that RetType is just the return type. Infer the
4087 // rest of the function argument types from the arguments that are present.
Craig Topper2617dcc2014-04-15 06:32:26 +00004088 PointerType *PFTy = nullptr;
4089 FunctionType *Ty = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004090 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
4091 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
4092 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004093 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00004094 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4095 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004096
Chris Lattnerac161bf2009-01-02 07:01:27 +00004097 if (!FunctionType::isValidReturnType(RetType))
4098 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004099
Owen Anderson4056ca92009-07-29 22:17:13 +00004100 Ty = FunctionType::get(RetType, ParamTypes, false);
4101 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004102 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004103
Chris Lattnerac161bf2009-01-02 07:01:27 +00004104 // Look up the callee.
4105 Value *Callee;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004106 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004107
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004108 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004109 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004110 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004111 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4112 AttributeSet::ReturnIndex,
4113 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004114
Chris Lattnerac161bf2009-01-02 07:01:27 +00004115 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004116
Chris Lattnerac161bf2009-01-02 07:01:27 +00004117 // Loop through FunctionType's arguments and ensure they are specified
4118 // correctly. Also, gather any parameter attributes.
4119 FunctionType::param_iterator I = Ty->param_begin();
4120 FunctionType::param_iterator E = Ty->param_end();
4121 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004122 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004123 if (I != E) {
4124 ExpectedTy = *I++;
4125 } else if (!Ty->isVarArg()) {
4126 return Error(ArgList[i].Loc, "too many arguments specified");
4127 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004128
Chris Lattnerac161bf2009-01-02 07:01:27 +00004129 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4130 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004131 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004132 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004133 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4134 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004135 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4136 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004137 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004138
Chris Lattnerac161bf2009-01-02 07:01:27 +00004139 if (I != E)
4140 return Error(CallLoc, "not enough parameters specified for call");
4141
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004142 if (FnAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004143 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4144 AttributeSet::FunctionIndex,
4145 FnAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004146
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004147 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004148 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004149
Jay Foad5bd375a2011-07-15 08:37:34 +00004150 CallInst *CI = CallInst::Create(Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00004151 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004152 CI->setCallingConv(CC);
4153 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004154 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004155 Inst = CI;
4156 return false;
4157}
4158
4159//===----------------------------------------------------------------------===//
4160// Memory Instructions.
4161//===----------------------------------------------------------------------===//
4162
4163/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00004164/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00004165int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004166 Value *Size = nullptr;
Chris Lattner200e0752009-07-02 23:08:13 +00004167 LocTy SizeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004168 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00004169 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00004170
4171 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
4172
Chris Lattner3822f632009-01-02 08:05:26 +00004173 if (ParseType(Ty)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004174
Chris Lattnerb2f39502009-12-30 05:44:30 +00004175 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004176 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00004177 if (Lex.getKind() == lltok::kw_align) {
4178 if (ParseOptionalAlignment(Alignment)) return true;
4179 } else if (Lex.getKind() == lltok::MetadataVar) {
4180 AteExtraComma = true;
4181 } else {
4182 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
4183 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4184 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004185 }
4186 }
4187
Dan Gohman2140a742010-05-28 01:14:11 +00004188 if (Size && !Size->getType()->isIntegerTy())
4189 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004190
Reid Kleckner436c42e2014-01-17 23:58:17 +00004191 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
4192 AI->setUsedWithInAlloca(IsInAlloca);
4193 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00004194 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004195}
4196
4197/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00004198/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004199/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00004200/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004201int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004202 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004203 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004204 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004205 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004206 AtomicOrdering Ordering = NotAtomic;
4207 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004208
4209 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004210 isAtomic = true;
4211 Lex.Lex();
4212 }
4213
Chris Lattnerbc639292011-11-27 06:56:53 +00004214 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004215 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004216 isVolatile = true;
4217 Lex.Lex();
4218 }
4219
Chris Lattnerb2f39502009-12-30 05:44:30 +00004220 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004221 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004222 ParseOptionalCommaAlign(Alignment, AteExtraComma))
4223 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004224
Duncan Sands19d0b472010-02-16 11:11:14 +00004225 if (!Val->getType()->isPointerTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004226 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
4227 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00004228 if (isAtomic && !Alignment)
4229 return Error(Loc, "atomic load must have explicit non-zero alignment");
4230 if (Ordering == Release || Ordering == AcquireRelease)
4231 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004232
Eli Friedman59b66882011-08-09 23:02:53 +00004233 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004234 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004235}
4236
4237/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00004238
4239/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
4240/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00004241/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00004242int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004243 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00004244 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00004245 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004246 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00004247 AtomicOrdering Ordering = NotAtomic;
4248 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004249
4250 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004251 isAtomic = true;
4252 Lex.Lex();
4253 }
4254
Chris Lattnerbc639292011-11-27 06:56:53 +00004255 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00004256 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00004257 isVolatile = true;
4258 Lex.Lex();
4259 }
4260
Chris Lattnerac161bf2009-01-02 07:01:27 +00004261 if (ParseTypeAndValue(Val, Loc, PFS) ||
4262 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004263 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00004264 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00004265 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004266 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00004267
Duncan Sands19d0b472010-02-16 11:11:14 +00004268 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004269 return Error(PtrLoc, "store operand must be a pointer");
4270 if (!Val->getType()->isFirstClassType())
4271 return Error(Loc, "store operand must be a first class value");
4272 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4273 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00004274 if (isAtomic && !Alignment)
4275 return Error(Loc, "atomic store must have explicit non-zero alignment");
4276 if (Ordering == Acquire || Ordering == AcquireRelease)
4277 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004278
Eli Friedman59b66882011-08-09 23:02:53 +00004279 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00004280 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004281}
4282
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004283/// ParseCmpXchg
Eli Friedman02e737b2011-08-12 22:50:01 +00004284/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
Tim Northovere94a5182014-03-11 10:48:52 +00004285/// 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00004286int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004287 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4288 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00004289 AtomicOrdering SuccessOrdering = NotAtomic;
4290 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004291 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004292 bool isVolatile = false;
4293
4294 if (EatIfPresent(lltok::kw_volatile))
4295 isVolatile = true;
4296
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004297 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4298 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4299 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4300 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4301 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00004302 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
4303 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004304 return true;
4305
Tim Northovere94a5182014-03-11 10:48:52 +00004306 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004307 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00004308 if (SuccessOrdering < FailureOrdering)
4309 return TokError("cmpxchg must be at least as ordered on success as failure");
4310 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
4311 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004312 if (!Ptr->getType()->isPointerTy())
4313 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4314 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4315 return Error(CmpLoc, "compare value and pointer type do not match");
4316 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4317 return Error(NewLoc, "new value and pointer type do not match");
4318 if (!New->getType()->isIntegerTy())
4319 return Error(NewLoc, "cmpxchg operand must be an integer");
4320 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4321 if (Size < 8 || (Size & (Size - 1)))
4322 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4323 " integer");
4324
Tim Northovere94a5182014-03-11 10:48:52 +00004325 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
4326 FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004327 CXI->setVolatile(isVolatile);
4328 Inst = CXI;
4329 return AteExtraComma ? InstExtraComma : InstNormal;
4330}
4331
4332/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00004333/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4334/// 'singlethread'? AtomicOrdering
4335int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004336 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4337 bool AteExtraComma = false;
4338 AtomicOrdering Ordering = NotAtomic;
4339 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00004340 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004341 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00004342
4343 if (EatIfPresent(lltok::kw_volatile))
4344 isVolatile = true;
4345
Eli Friedmanc9a551e2011-07-28 21:48:00 +00004346 switch (Lex.getKind()) {
4347 default: return TokError("expected binary operation in atomicrmw");
4348 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4349 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4350 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4351 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4352 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4353 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4354 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4355 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4356 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4357 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4358 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4359 }
4360 Lex.Lex(); // Eat the operation.
4361
4362 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4363 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4364 ParseTypeAndValue(Val, ValLoc, PFS) ||
4365 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4366 return true;
4367
4368 if (Ordering == Unordered)
4369 return TokError("atomicrmw cannot be unordered");
4370 if (!Ptr->getType()->isPointerTy())
4371 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4372 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4373 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4374 if (!Val->getType()->isIntegerTy())
4375 return Error(ValLoc, "atomicrmw operand must be an integer");
4376 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4377 if (Size < 8 || (Size & (Size - 1)))
4378 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4379 " integer");
4380
4381 AtomicRMWInst *RMWI =
4382 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4383 RMWI->setVolatile(isVolatile);
4384 Inst = RMWI;
4385 return AteExtraComma ? InstExtraComma : InstNormal;
4386}
4387
Eli Friedmanfee02c62011-07-25 23:16:38 +00004388/// ParseFence
4389/// ::= 'fence' 'singlethread'? AtomicOrdering
4390int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4391 AtomicOrdering Ordering = NotAtomic;
4392 SynchronizationScope Scope = CrossThread;
4393 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4394 return true;
4395
4396 if (Ordering == Unordered)
4397 return TokError("fence cannot be unordered");
4398 if (Ordering == Monotonic)
4399 return TokError("fence cannot be monotonic");
4400
4401 Inst = new FenceInst(Context, Ordering, Scope);
4402 return InstNormal;
4403}
4404
Chris Lattnerac161bf2009-01-02 07:01:27 +00004405/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00004406/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00004407int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004408 Value *Ptr = nullptr;
4409 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004410 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00004411
Dan Gohman16cbbe42009-07-29 15:58:36 +00004412 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00004413
Chris Lattner3822f632009-01-02 08:05:26 +00004414 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004415
Eli Benderskyd9806682013-04-22 17:03:42 +00004416 Type *BaseType = Ptr->getType();
4417 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
4418 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004419 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004420
Chris Lattnerac161bf2009-01-02 07:01:27 +00004421 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004422 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00004423 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00004424 if (Lex.getKind() == lltok::MetadataVar) {
4425 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00004426 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004427 }
Chris Lattner3822f632009-01-02 08:05:26 +00004428 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00004429 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004430 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00004431 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4432 return Error(EltLoc, "getelementptr index type missmatch");
4433 if (Val->getType()->isVectorTy()) {
4434 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4435 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4436 if (ValNumEl != PtrNumEl)
4437 return Error(EltLoc,
4438 "getelementptr vector index has a wrong number of elements");
4439 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004440 Indices.push_back(Val);
4441 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004442
Eli Benderskyd9806682013-04-22 17:03:42 +00004443 if (!Indices.empty() && !BasePointerType->getElementType()->isSized())
4444 return Error(Loc, "base element of getelementptr must be sized");
4445
4446 if (!GetElementPtrInst::getIndexedType(BaseType, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004447 return Error(Loc, "invalid getelementptr indices");
Jay Foadd1b78492011-07-25 09:48:08 +00004448 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00004449 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00004450 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004451 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004452}
4453
4454/// ParseExtractValue
4455/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004456int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004457 Value *Val; LocTy Loc;
4458 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004459 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004460 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004461 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004462 return true;
4463
Chris Lattner392be582010-02-12 20:49:41 +00004464 if (!Val->getType()->isAggregateType())
4465 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004466
Jay Foad57aa6362011-07-13 10:26:04 +00004467 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004468 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004469 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004470 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004471}
4472
4473/// ParseInsertValue
4474/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00004475int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004476 Value *Val0, *Val1; LocTy Loc0, Loc1;
4477 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00004478 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004479 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4480 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4481 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00004482 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004483 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004484
Chris Lattner392be582010-02-12 20:49:41 +00004485 if (!Val0->getType()->isAggregateType())
4486 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004487
Jay Foad57aa6362011-07-13 10:26:04 +00004488 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004489 return Error(Loc0, "invalid indices for insertvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00004490 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00004491 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004492}
Nick Lewycky49f89192009-04-04 07:22:01 +00004493
4494//===----------------------------------------------------------------------===//
4495// Embedded metadata.
4496//===----------------------------------------------------------------------===//
4497
4498/// ParseMDNodeVector
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004499/// ::= Element (',' Element)*
4500/// Element
4501/// ::= 'null' | TypeAndValue
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00004502bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandezb8fd1522010-01-10 07:14:18 +00004503 PerFunctionState *PFS) {
Dan Gohman1e0213a2010-07-13 19:33:27 +00004504 // Check for an empty list.
4505 if (Lex.getKind() == lltok::rbrace)
4506 return false;
4507
Nick Lewycky49f89192009-04-04 07:22:01 +00004508 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004509 // Null is a special case since it is typeless.
4510 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004511 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00004512 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004513 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004514
Craig Topper2617dcc2014-04-15 06:32:26 +00004515 Value *V = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004516 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00004517 Elts.push_back(V);
Nick Lewycky49f89192009-04-04 07:22:01 +00004518 } while (EatIfPresent(lltok::comma));
4519
4520 return false;
4521}