blob: ba3db3406578c5e09e21a5925ef8cbe68794266f [file] [log] [blame]
Chris Lattnerdf986172009-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 Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000016#include "llvm/AutoUpgrade.h"
Chandler Carruth0b8c9a82013-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"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/Operator.h"
24#include "llvm/IR/ValueSymbolTable.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000025#include "llvm/Support/ErrorHandling.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000026#include "llvm/Support/raw_ostream.h"
27using namespace llvm;
28
Chris Lattnerdb125cf2011-07-18 04:54:35 +000029static std::string getTypeString(Type *T) {
Chris Lattner0cd0d882011-06-18 21:18:23 +000030 std::string Result;
31 raw_string_ostream Tmp(Result);
32 Tmp << *T;
33 return Tmp.str();
34}
35
Chris Lattner3ed88ef2009-01-02 08:05:26 +000036/// Run: module ::= toplevelentity*
Chris Lattnerad7d1e22009-01-04 20:44:11 +000037bool LLParser::Run() {
Chris Lattner3ed88ef2009-01-02 08:05:26 +000038 // Prime the lexer.
39 Lex.Lex();
40
Chris Lattnerad7d1e22009-01-04 20:44:11 +000041 return ParseTopLevelEntities() ||
42 ValidateEndOfModule();
Chris Lattnerdf986172009-01-02 07:01:27 +000043}
44
45/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
46/// module.
47bool LLParser::ValidateEndOfModule() {
Chris Lattner449c3102010-04-01 05:14:45 +000048 // Handle any instruction metadata forward references.
49 if (!ForwardRefInstMetadata.empty()) {
50 for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
51 I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
52 I != E; ++I) {
53 Instruction *Inst = I->first;
54 const std::vector<MDRef> &MDList = I->second;
Michael Ilseman407a6162012-11-15 22:34:00 +000055
Chris Lattner449c3102010-04-01 05:14:45 +000056 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
57 unsigned SlotNo = MDList[i].MDSlot;
Michael Ilseman407a6162012-11-15 22:34:00 +000058
Chris Lattner449c3102010-04-01 05:14:45 +000059 if (SlotNo >= NumberedMetadata.size() || NumberedMetadata[SlotNo] == 0)
60 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +000061 Twine(SlotNo) + "'");
Chris Lattner449c3102010-04-01 05:14:45 +000062 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
63 }
64 }
65 ForwardRefInstMetadata.clear();
66 }
Michael Ilseman407a6162012-11-15 22:34:00 +000067
68
Chris Lattner09d9ef42009-10-28 03:39:23 +000069 // If there are entries in ForwardRefBlockAddresses at this point, they are
70 // references after the function was defined. Resolve those now.
71 while (!ForwardRefBlockAddresses.empty()) {
72 // Okay, we are referencing an already-parsed function, resolve them now.
73 Function *TheFn = 0;
74 const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
75 if (Fn.Kind == ValID::t_GlobalName)
76 TheFn = M->getFunction(Fn.StrVal);
77 else if (Fn.UIntVal < NumberedVals.size())
78 TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
Michael Ilseman407a6162012-11-15 22:34:00 +000079
Chris Lattner09d9ef42009-10-28 03:39:23 +000080 if (TheFn == 0)
81 return Error(Fn.Loc, "unknown function referenced by blockaddress");
Michael Ilseman407a6162012-11-15 22:34:00 +000082
Chris Lattner09d9ef42009-10-28 03:39:23 +000083 // Resolve all these references.
Michael Ilseman407a6162012-11-15 22:34:00 +000084 if (ResolveForwardRefBlockAddresses(TheFn,
Chris Lattner09d9ef42009-10-28 03:39:23 +000085 ForwardRefBlockAddresses.begin()->second,
86 0))
87 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +000088
Chris Lattner09d9ef42009-10-28 03:39:23 +000089 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
90 }
Michael Ilseman407a6162012-11-15 22:34:00 +000091
Chris Lattner1afcace2011-07-09 17:41:24 +000092 for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i)
93 if (NumberedTypes[i].second.isValid())
94 return Error(NumberedTypes[i].second,
95 "use of undefined type '%" + Twine(i) + "'");
96
97 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
98 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
99 if (I->second.second.isValid())
100 return Error(I->second.second,
101 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000102
Chris Lattnerdf986172009-01-02 07:01:27 +0000103 if (!ForwardRefVals.empty())
104 return Error(ForwardRefVals.begin()->second.second,
105 "use of undefined value '@" + ForwardRefVals.begin()->first +
106 "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000107
Chris Lattnerdf986172009-01-02 07:01:27 +0000108 if (!ForwardRefValIDs.empty())
109 return Error(ForwardRefValIDs.begin()->second.second,
110 "use of undefined value '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000111 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000112
Devang Patel1c7eea62009-07-08 19:23:54 +0000113 if (!ForwardRefMDNodes.empty())
114 return Error(ForwardRefMDNodes.begin()->second.second,
115 "use of undefined metadata '!" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000116 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000117
Devang Patel1c7eea62009-07-08 19:23:54 +0000118
Chris Lattnerdf986172009-01-02 07:01:27 +0000119 // Look for intrinsic functions and CallInst that need to be upgraded
120 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
121 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbara279bc32009-09-20 02:20:51 +0000122
Chris Lattnerdf986172009-01-02 07:01:27 +0000123 return false;
124}
125
Michael Ilseman407a6162012-11-15 22:34:00 +0000126bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
Chris Lattner09d9ef42009-10-28 03:39:23 +0000127 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
128 PerFunctionState *PFS) {
129 // Loop over all the references, resolving them.
130 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
131 BasicBlock *Res;
Chris Lattnercdfc9402009-11-01 01:27:45 +0000132 if (PFS) {
Chris Lattner09d9ef42009-10-28 03:39:23 +0000133 if (Refs[i].first.Kind == ValID::t_LocalName)
134 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattnercdfc9402009-11-01 01:27:45 +0000135 else
Chris Lattner09d9ef42009-10-28 03:39:23 +0000136 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
137 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
138 return Error(Refs[i].first.Loc,
Chris Lattneree7644d2009-11-02 18:28:45 +0000139 "cannot take address of numeric label after the function is defined");
Chris Lattner09d9ef42009-10-28 03:39:23 +0000140 } else {
141 Res = dyn_cast_or_null<BasicBlock>(
142 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
143 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000144
Chris Lattnercdfc9402009-11-01 01:27:45 +0000145 if (Res == 0)
Chris Lattner09d9ef42009-10-28 03:39:23 +0000146 return Error(Refs[i].first.Loc,
147 "referenced value is not a basic block");
Michael Ilseman407a6162012-11-15 22:34:00 +0000148
Chris Lattner09d9ef42009-10-28 03:39:23 +0000149 // Get the BlockAddress for this and update references to use it.
150 BlockAddress *BA = BlockAddress::get(TheFn, Res);
151 Refs[i].second->replaceAllUsesWith(BA);
152 Refs[i].second->eraseFromParent();
153 }
154 return false;
155}
156
157
Chris Lattnerdf986172009-01-02 07:01:27 +0000158//===----------------------------------------------------------------------===//
159// Top-Level Entities
160//===----------------------------------------------------------------------===//
161
162bool LLParser::ParseTopLevelEntities() {
Chris Lattnerdf986172009-01-02 07:01:27 +0000163 while (1) {
164 switch (Lex.getKind()) {
165 default: return TokError("expected top-level entity");
166 case lltok::Eof: return false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000167 case lltok::kw_declare: if (ParseDeclare()) return true; break;
168 case lltok::kw_define: if (ParseDefine()) return true; break;
169 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
170 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling3defc0b2012-11-28 08:41:48 +0000171 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman3845e502009-08-12 23:32:33 +0000172 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000173 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman3845e502009-08-12 23:32:33 +0000174 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000175 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattnere434d272009-12-30 04:56:59 +0000176 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000177 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
178 case lltok::AttrGrpID: if (ParseUnnamedAttrGrp()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000179
180 // The Global variable production with no name can have many different
181 // optional leading prefixes, the production is:
182 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000183 // OptionalAddrSpace OptionalUnNammedAddr
184 // ('constant'|'global') ...
Bill Wendling5e721d72010-07-01 21:55:59 +0000185 case lltok::kw_private: // OptionalLinkage
186 case lltok::kw_linker_private: // OptionalLinkage
187 case lltok::kw_linker_private_weak: // OptionalLinkage
Bill Wendling32811be2012-08-17 18:33:14 +0000188 case lltok::kw_linker_private_weak_def_auto: // FIXME: backwards compat.
Bill Wendling5e721d72010-07-01 21:55:59 +0000189 case lltok::kw_internal: // OptionalLinkage
190 case lltok::kw_weak: // OptionalLinkage
191 case lltok::kw_weak_odr: // OptionalLinkage
192 case lltok::kw_linkonce: // OptionalLinkage
193 case lltok::kw_linkonce_odr: // OptionalLinkage
Bill Wendling32811be2012-08-17 18:33:14 +0000194 case lltok::kw_linkonce_odr_auto_hide: // OptionalLinkage
Bill Wendling5e721d72010-07-01 21:55:59 +0000195 case lltok::kw_appending: // OptionalLinkage
196 case lltok::kw_dllexport: // OptionalLinkage
197 case lltok::kw_common: // OptionalLinkage
198 case lltok::kw_dllimport: // OptionalLinkage
199 case lltok::kw_extern_weak: // OptionalLinkage
200 case lltok::kw_external: { // OptionalLinkage
Chris Lattnerdf986172009-01-02 07:01:27 +0000201 unsigned Linkage, Visibility;
202 if (ParseOptionalLinkage(Linkage) ||
203 ParseOptionalVisibility(Visibility) ||
Chris Lattnereeb4a842009-07-02 23:08:13 +0000204 ParseGlobal("", SMLoc(), Linkage, true, Visibility))
Chris Lattnerdf986172009-01-02 07:01:27 +0000205 return true;
206 break;
207 }
208 case lltok::kw_default: // OptionalVisibility
209 case lltok::kw_hidden: // OptionalVisibility
210 case lltok::kw_protected: { // OptionalVisibility
211 unsigned Visibility;
212 if (ParseOptionalVisibility(Visibility) ||
Chris Lattnereeb4a842009-07-02 23:08:13 +0000213 ParseGlobal("", SMLoc(), 0, false, Visibility))
Chris Lattnerdf986172009-01-02 07:01:27 +0000214 return true;
215 break;
216 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000217
Chris Lattnerdf986172009-01-02 07:01:27 +0000218 case lltok::kw_thread_local: // OptionalThreadLocal
219 case lltok::kw_addrspace: // OptionalAddrSpace
220 case lltok::kw_constant: // GlobalType
221 case lltok::kw_global: // GlobalType
Chris Lattnereeb4a842009-07-02 23:08:13 +0000222 if (ParseGlobal("", SMLoc(), 0, false, 0)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +0000223 break;
224 }
225 }
226}
227
228
229/// toplevelentity
230/// ::= 'module' 'asm' STRINGCONSTANT
231bool LLParser::ParseModuleAsm() {
232 assert(Lex.getKind() == lltok::kw_module);
233 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000234
235 std::string AsmStr;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000236 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
237 ParseStringConstant(AsmStr)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000238
Rafael Espindola38c4e532011-03-02 04:14:42 +0000239 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000240 return false;
241}
242
243/// toplevelentity
244/// ::= 'target' 'triple' '=' STRINGCONSTANT
245/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
246bool LLParser::ParseTargetDefinition() {
247 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000248 std::string Str;
Chris Lattnerdf986172009-01-02 07:01:27 +0000249 switch (Lex.Lex()) {
250 default: return TokError("unknown target property");
251 case lltok::kw_triple:
252 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000253 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
254 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000255 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000256 M->setTargetTriple(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000257 return false;
258 case lltok::kw_datalayout:
259 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000260 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
261 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000262 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000263 M->setDataLayout(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000264 return false;
265 }
266}
267
Bill Wendling3defc0b2012-11-28 08:41:48 +0000268/// toplevelentity
269/// ::= 'deplibs' '=' '[' ']'
270/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
271/// FIXME: Remove in 4.0. Currently parse, but ignore.
272bool LLParser::ParseDepLibs() {
273 assert(Lex.getKind() == lltok::kw_deplibs);
274 Lex.Lex();
275 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
276 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
277 return true;
278
279 if (EatIfPresent(lltok::rsquare))
280 return false;
281
282 do {
283 std::string Str;
284 if (ParseStringConstant(Str)) return true;
285 } while (EatIfPresent(lltok::comma));
286
287 return ParseToken(lltok::rsquare, "expected ']' at end of list");
288}
289
Dan Gohman3845e502009-08-12 23:32:33 +0000290/// ParseUnnamedType:
Dan Gohman3845e502009-08-12 23:32:33 +0000291/// ::= LocalVarID '=' 'type' type
Chris Lattnerdf986172009-01-02 07:01:27 +0000292bool LLParser::ParseUnnamedType() {
Chris Lattneredcaca82011-06-18 23:51:31 +0000293 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +0000294 unsigned TypeID = Lex.getUIntVal();
Chris Lattnera53616d2011-06-19 00:03:46 +0000295 Lex.Lex(); // eat LocalVarID;
296
297 if (ParseToken(lltok::equal, "expected '=' after name") ||
298 ParseToken(lltok::kw_type, "expected 'type' after '='"))
299 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +0000300
Chris Lattner1afcace2011-07-09 17:41:24 +0000301 if (TypeID >= NumberedTypes.size())
302 NumberedTypes.resize(TypeID+1);
Michael Ilseman407a6162012-11-15 22:34:00 +0000303
Chris Lattner1afcace2011-07-09 17:41:24 +0000304 Type *Result = 0;
305 if (ParseStructDefinition(TypeLoc, "",
306 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman407a6162012-11-15 22:34:00 +0000307
Chris Lattner1afcace2011-07-09 17:41:24 +0000308 if (!isa<StructType>(Result)) {
309 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
310 if (Entry.first)
311 return Error(TypeLoc, "non-struct types may not be recursive");
312 Entry.first = Result;
313 Entry.second = SMLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +0000314 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000315
Chris Lattnerdf986172009-01-02 07:01:27 +0000316 return false;
317}
318
Chris Lattner1afcace2011-07-09 17:41:24 +0000319
Chris Lattnerdf986172009-01-02 07:01:27 +0000320/// toplevelentity
321/// ::= LocalVar '=' 'type' type
322bool LLParser::ParseNamedType() {
323 std::string Name = Lex.getStrVal();
324 LocTy NameLoc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000325 Lex.Lex(); // eat LocalVar.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000326
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000327 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattner1afcace2011-07-09 17:41:24 +0000328 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000329 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +0000330
Chris Lattner1afcace2011-07-09 17:41:24 +0000331 Type *Result = 0;
332 if (ParseStructDefinition(NameLoc, Name,
333 NamedTypes[Name], Result)) return true;
Michael Ilseman407a6162012-11-15 22:34:00 +0000334
Chris Lattner1afcace2011-07-09 17:41:24 +0000335 if (!isa<StructType>(Result)) {
336 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
337 if (Entry.first)
338 return Error(NameLoc, "non-struct types may not be recursive");
339 Entry.first = Result;
340 Entry.second = SMLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +0000341 }
Michael Ilseman407a6162012-11-15 22:34:00 +0000342
Chris Lattner1afcace2011-07-09 17:41:24 +0000343 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000344}
345
346
347/// toplevelentity
348/// ::= 'declare' FunctionHeader
349bool LLParser::ParseDeclare() {
350 assert(Lex.getKind() == lltok::kw_declare);
351 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000352
Chris Lattnerdf986172009-01-02 07:01:27 +0000353 Function *F;
354 return ParseFunctionHeader(F, false);
355}
356
357/// toplevelentity
358/// ::= 'define' FunctionHeader '{' ...
359bool LLParser::ParseDefine() {
360 assert(Lex.getKind() == lltok::kw_define);
361 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000362
Chris Lattnerdf986172009-01-02 07:01:27 +0000363 Function *F;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000364 return ParseFunctionHeader(F, true) ||
365 ParseFunctionBody(*F);
Chris Lattnerdf986172009-01-02 07:01:27 +0000366}
367
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000368/// ParseGlobalType
369/// ::= 'constant'
370/// ::= 'global'
Chris Lattnerdf986172009-01-02 07:01:27 +0000371bool LLParser::ParseGlobalType(bool &IsConstant) {
372 if (Lex.getKind() == lltok::kw_constant)
373 IsConstant = true;
374 else if (Lex.getKind() == lltok::kw_global)
375 IsConstant = false;
Duncan Sands35b51072009-02-10 16:24:55 +0000376 else {
377 IsConstant = false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000378 return TokError("expected 'global' or 'constant'");
Duncan Sands35b51072009-02-10 16:24:55 +0000379 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000380 Lex.Lex();
381 return false;
382}
383
Dan Gohman3845e502009-08-12 23:32:33 +0000384/// ParseUnnamedGlobal:
385/// OptionalVisibility ALIAS ...
386/// OptionalLinkage OptionalVisibility ... -> global variable
387/// GlobalID '=' OptionalVisibility ALIAS ...
388/// GlobalID '=' OptionalLinkage OptionalVisibility ... -> global variable
389bool LLParser::ParseUnnamedGlobal() {
390 unsigned VarID = NumberedVals.size();
391 std::string Name;
392 LocTy NameLoc = Lex.getLoc();
393
394 // Handle the GlobalID form.
395 if (Lex.getKind() == lltok::GlobalID) {
396 if (Lex.getUIntVal() != VarID)
397 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000398 Twine(VarID) + "'");
Dan Gohman3845e502009-08-12 23:32:33 +0000399 Lex.Lex(); // eat GlobalID;
400
401 if (ParseToken(lltok::equal, "expected '=' after name"))
402 return true;
403 }
404
405 bool HasLinkage;
406 unsigned Linkage, Visibility;
407 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
408 ParseOptionalVisibility(Visibility))
409 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000410
Dan Gohman3845e502009-08-12 23:32:33 +0000411 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
412 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
413 return ParseAlias(Name, NameLoc, Visibility);
414}
415
Chris Lattnerdf986172009-01-02 07:01:27 +0000416/// ParseNamedGlobal:
417/// GlobalVar '=' OptionalVisibility ALIAS ...
418/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable
419bool LLParser::ParseNamedGlobal() {
420 assert(Lex.getKind() == lltok::GlobalVar);
421 LocTy NameLoc = Lex.getLoc();
422 std::string Name = Lex.getStrVal();
423 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000424
Chris Lattnerdf986172009-01-02 07:01:27 +0000425 bool HasLinkage;
426 unsigned Linkage, Visibility;
427 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
428 ParseOptionalLinkage(Linkage, HasLinkage) ||
429 ParseOptionalVisibility(Visibility))
430 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000431
Chris Lattnerdf986172009-01-02 07:01:27 +0000432 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
433 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
434 return ParseAlias(Name, NameLoc, Visibility);
435}
436
Devang Patel256be962009-07-20 19:00:08 +0000437// MDString:
438// ::= '!' STRINGCONSTANT
Chris Lattner442ffa12009-12-29 21:53:55 +0000439bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000440 std::string Str;
441 if (ParseStringConstant(Str)) return true;
Chris Lattner442ffa12009-12-29 21:53:55 +0000442 Result = MDString::get(Context, Str);
Devang Patel256be962009-07-20 19:00:08 +0000443 return false;
444}
445
446// MDNode:
447// ::= '!' MDNodeNumber
Chris Lattner449c3102010-04-01 05:14:45 +0000448//
449/// This version of ParseMDNodeID returns the slot number and null in the case
450/// of a forward reference.
451bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
452 // !{ ..., !42, ... }
453 if (ParseUInt32(SlotNo)) return true;
454
455 // Check existing MDNode.
456 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != 0)
457 Result = NumberedMetadata[SlotNo];
458 else
459 Result = 0;
460 return false;
461}
462
Chris Lattner4a72efc2009-12-30 04:15:23 +0000463bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000464 // !{ ..., !42, ... }
465 unsigned MID = 0;
Chris Lattner449c3102010-04-01 05:14:45 +0000466 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000467
Chris Lattner449c3102010-04-01 05:14:45 +0000468 // If not a forward reference, just return it now.
469 if (Result) return false;
Devang Patel256be962009-07-20 19:00:08 +0000470
Chris Lattner449c3102010-04-01 05:14:45 +0000471 // Otherwise, create MDNode forward reference.
Jay Foadec9186b2011-04-21 19:59:31 +0000472 MDNode *FwdNode = MDNode::getTemporary(Context, ArrayRef<Value*>());
Devang Patel256be962009-07-20 19:00:08 +0000473 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Michael Ilseman407a6162012-11-15 22:34:00 +0000474
Chris Lattner0834e6a2009-12-30 04:51:58 +0000475 if (NumberedMetadata.size() <= MID)
476 NumberedMetadata.resize(MID+1);
477 NumberedMetadata[MID] = FwdNode;
Chris Lattner442ffa12009-12-29 21:53:55 +0000478 Result = FwdNode;
Devang Patel256be962009-07-20 19:00:08 +0000479 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000480}
Devang Patel256be962009-07-20 19:00:08 +0000481
Chris Lattner84d03b12009-12-29 22:35:39 +0000482/// ParseNamedMetadata:
Devang Pateleff2ab62009-07-29 00:34:02 +0000483/// !foo = !{ !1, !2 }
484bool LLParser::ParseNamedMetadata() {
Chris Lattner1d928312009-12-30 05:02:06 +0000485 assert(Lex.getKind() == lltok::MetadataVar);
Devang Pateleff2ab62009-07-29 00:34:02 +0000486 std::string Name = Lex.getStrVal();
Chris Lattner1d928312009-12-30 05:02:06 +0000487 Lex.Lex();
Devang Pateleff2ab62009-07-29 00:34:02 +0000488
Chris Lattner84d03b12009-12-29 22:35:39 +0000489 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattnere434d272009-12-30 04:56:59 +0000490 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner84d03b12009-12-29 22:35:39 +0000491 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Pateleff2ab62009-07-29 00:34:02 +0000492 return true;
493
Dan Gohman17aa92c2010-07-21 23:38:33 +0000494 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000495 if (Lex.getKind() != lltok::rbrace)
496 do {
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000497 if (ParseToken(lltok::exclaim, "Expected '!' here"))
498 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +0000499
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000500 MDNode *N = 0;
501 if (ParseMDNodeID(N)) return true;
Dan Gohman17aa92c2010-07-21 23:38:33 +0000502 NMD->addOperand(N);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000503 } while (EatIfPresent(lltok::comma));
Devang Pateleff2ab62009-07-29 00:34:02 +0000504
505 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
506 return true;
507
Devang Pateleff2ab62009-07-29 00:34:02 +0000508 return false;
509}
510
Devang Patel923078c2009-07-01 19:21:12 +0000511/// ParseStandaloneMetadata:
Daniel Dunbara279bc32009-09-20 02:20:51 +0000512/// !42 = !{...}
Devang Patel923078c2009-07-01 19:21:12 +0000513bool LLParser::ParseStandaloneMetadata() {
Chris Lattnere434d272009-12-30 04:56:59 +0000514 assert(Lex.getKind() == lltok::exclaim);
Devang Patel923078c2009-07-01 19:21:12 +0000515 Lex.Lex();
516 unsigned MetadataID = 0;
Devang Patel923078c2009-07-01 19:21:12 +0000517
518 LocTy TyLoc;
Chris Lattner1afcace2011-07-09 17:41:24 +0000519 Type *Ty = 0;
Devang Patel104cf9e2009-07-23 01:07:34 +0000520 SmallVector<Value *, 16> Elts;
Chris Lattner3f5132a2009-12-29 22:40:21 +0000521 if (ParseUInt32(MetadataID) ||
522 ParseToken(lltok::equal, "expected '=' here") ||
523 ParseType(Ty, TyLoc) ||
Chris Lattnere434d272009-12-30 04:56:59 +0000524 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000525 ParseToken(lltok::lbrace, "Expected '{' here") ||
Victor Hernandez24e64df2010-01-10 07:14:18 +0000526 ParseMDNodeVector(Elts, NULL) ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000527 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patel104cf9e2009-07-23 01:07:34 +0000528 return true;
529
Jay Foadec9186b2011-04-21 19:59:31 +0000530 MDNode *Init = MDNode::get(Context, Elts);
Michael Ilseman407a6162012-11-15 22:34:00 +0000531
Chris Lattner0834e6a2009-12-30 04:51:58 +0000532 // See if this was forward referenced, if so, handle it.
Chris Lattnere80250e2009-12-29 21:43:58 +0000533 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Patel1c7eea62009-07-08 19:23:54 +0000534 FI = ForwardRefMDNodes.find(MetadataID);
535 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman489b29b2010-08-20 22:02:26 +0000536 MDNode *Temp = FI->second.first;
537 Temp->replaceAllUsesWith(Init);
538 MDNode::deleteTemporary(Temp);
Devang Patel1c7eea62009-07-08 19:23:54 +0000539 ForwardRefMDNodes.erase(FI);
Michael Ilseman407a6162012-11-15 22:34:00 +0000540
Chris Lattner0834e6a2009-12-30 04:51:58 +0000541 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
542 } else {
543 if (MetadataID >= NumberedMetadata.size())
544 NumberedMetadata.resize(MetadataID+1);
545
546 if (NumberedMetadata[MetadataID] != 0)
547 return TokError("Metadata id is already used");
548 NumberedMetadata[MetadataID] = Init;
Devang Patel1c7eea62009-07-08 19:23:54 +0000549 }
550
Devang Patel923078c2009-07-01 19:21:12 +0000551 return false;
552}
553
Chris Lattnerdf986172009-01-02 07:01:27 +0000554/// ParseAlias:
555/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
556/// Aliasee
Chris Lattner040f7582009-04-25 21:26:00 +0000557/// ::= TypeAndValue
558/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
Dan Gohmandd8004d2009-07-27 21:53:46 +0000559/// ::= 'getelementptr' 'inbounds'? '(' ... ')'
Chris Lattnerdf986172009-01-02 07:01:27 +0000560///
561/// Everything through visibility has already been parsed.
562///
563bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
564 unsigned Visibility) {
565 assert(Lex.getKind() == lltok::kw_alias);
566 Lex.Lex();
567 unsigned Linkage;
568 LocTy LinkageLoc = Lex.getLoc();
569 if (ParseOptionalLinkage(Linkage))
570 return true;
571
572 if (Linkage != GlobalValue::ExternalLinkage &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000573 Linkage != GlobalValue::WeakAnyLinkage &&
574 Linkage != GlobalValue::WeakODRLinkage &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000575 Linkage != GlobalValue::InternalLinkage &&
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000576 Linkage != GlobalValue::PrivateLinkage &&
Bill Wendling5e721d72010-07-01 21:55:59 +0000577 Linkage != GlobalValue::LinkerPrivateLinkage &&
Bill Wendling32811be2012-08-17 18:33:14 +0000578 Linkage != GlobalValue::LinkerPrivateWeakLinkage)
Chris Lattnerdf986172009-01-02 07:01:27 +0000579 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000580
Chris Lattnerdf986172009-01-02 07:01:27 +0000581 Constant *Aliasee;
582 LocTy AliaseeLoc = Lex.getLoc();
Chris Lattner040f7582009-04-25 21:26:00 +0000583 if (Lex.getKind() != lltok::kw_bitcast &&
584 Lex.getKind() != lltok::kw_getelementptr) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000585 if (ParseGlobalTypeAndValue(Aliasee)) return true;
586 } else {
587 // The bitcast dest type is not present, it is implied by the dest type.
588 ValID ID;
589 if (ParseValID(ID)) return true;
590 if (ID.Kind != ValID::t_Constant)
591 return Error(AliaseeLoc, "invalid aliasee");
592 Aliasee = ID.ConstantVal;
593 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000594
Duncan Sands1df98592010-02-16 11:11:14 +0000595 if (!Aliasee->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +0000596 return Error(AliaseeLoc, "alias must have pointer type");
597
598 // Okay, create the alias but do not insert it into the module yet.
599 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
600 (GlobalValue::LinkageTypes)Linkage, Name,
601 Aliasee);
602 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000603
Chris Lattnerdf986172009-01-02 07:01:27 +0000604 // See if this value already exists in the symbol table. If so, it is either
605 // a redefinition or a definition of a forward reference.
Chris Lattner1d871c52009-10-25 23:22:50 +0000606 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000607 // See if this was a redefinition. If so, there is no entry in
608 // ForwardRefVals.
609 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
610 I = ForwardRefVals.find(Name);
611 if (I == ForwardRefVals.end())
612 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
613
614 // Otherwise, this was a definition of forward ref. Verify that types
615 // agree.
616 if (Val->getType() != GA->getType())
617 return Error(NameLoc,
618 "forward reference and definition of alias have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000619
Chris Lattnerdf986172009-01-02 07:01:27 +0000620 // If they agree, just RAUW the old value with the alias and remove the
621 // forward ref info.
622 Val->replaceAllUsesWith(GA);
623 Val->eraseFromParent();
624 ForwardRefVals.erase(I);
625 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000626
Chris Lattnerdf986172009-01-02 07:01:27 +0000627 // Insert into the module, we know its name won't collide now.
628 M->getAliasList().push_back(GA);
Benjamin Krameraf812352010-10-16 11:28:23 +0000629 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000630
Chris Lattnerdf986172009-01-02 07:01:27 +0000631 return false;
632}
633
634/// ParseGlobal
635/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
Michael Gottesmana2de37c2013-02-05 05:57:38 +0000636/// OptionalAddrSpace OptionalUnNammedAddr
637/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000638/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
Michael Gottesmana2de37c2013-02-05 05:57:38 +0000639/// OptionalAddrSpace OptionalUnNammedAddr
640/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000641///
642/// Everything through visibility has been parsed already.
643///
644bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
645 unsigned Linkage, bool HasLinkage,
646 unsigned Visibility) {
647 unsigned AddrSpace;
Michael Gottesmana2de37c2013-02-05 05:57:38 +0000648 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
Hans Wennborgce718ff2012-06-23 11:37:03 +0000649 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindolad72479c2011-01-13 01:30:30 +0000650 LocTy UnnamedAddrLoc;
Michael Gottesmana2de37c2013-02-05 05:57:38 +0000651 LocTy IsExternallyInitializedLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +0000652 LocTy TyLoc;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000653
Chris Lattner1afcace2011-07-09 17:41:24 +0000654 Type *Ty = 0;
Hans Wennborgce718ff2012-06-23 11:37:03 +0000655 if (ParseOptionalThreadLocal(TLM) ||
Chris Lattnerdf986172009-01-02 07:01:27 +0000656 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindolad72479c2011-01-13 01:30:30 +0000657 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
658 &UnnamedAddrLoc) ||
Michael Gottesmana2de37c2013-02-05 05:57:38 +0000659 ParseOptionalToken(lltok::kw_externally_initialized,
660 IsExternallyInitialized,
661 &IsExternallyInitializedLoc) ||
Chris Lattnerdf986172009-01-02 07:01:27 +0000662 ParseGlobalType(IsConstant) ||
663 ParseType(Ty, TyLoc))
664 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000665
Chris Lattnerdf986172009-01-02 07:01:27 +0000666 // If the linkage is specified and is external, then no initializer is
667 // present.
668 Constant *Init = 0;
669 if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage &&
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000670 Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerdf986172009-01-02 07:01:27 +0000671 Linkage != GlobalValue::ExternalLinkage)) {
672 if (ParseGlobalValue(Ty, Init))
673 return true;
674 }
675
Duncan Sands1df98592010-02-16 11:11:14 +0000676 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattner4a2f1122009-02-08 20:00:15 +0000677 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000678
Chris Lattnerdf986172009-01-02 07:01:27 +0000679 GlobalVariable *GV = 0;
680
681 // See if the global was forward referenced, if so, use the global.
Chris Lattner91dad872009-02-02 07:24:28 +0000682 if (!Name.empty()) {
Chris Lattner1d871c52009-10-25 23:22:50 +0000683 if (GlobalValue *GVal = M->getNamedValue(Name)) {
684 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
685 return Error(NameLoc, "redefinition of global '@" + Name + "'");
686 GV = cast<GlobalVariable>(GVal);
687 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000688 } else {
689 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
690 I = ForwardRefValIDs.find(NumberedVals.size());
691 if (I != ForwardRefValIDs.end()) {
692 GV = cast<GlobalVariable>(I->second.first);
693 ForwardRefValIDs.erase(I);
694 }
695 }
696
697 if (GV == 0) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000698 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0,
Hans Wennborgce718ff2012-06-23 11:37:03 +0000699 Name, 0, GlobalVariable::NotThreadLocal,
700 AddrSpace);
Chris Lattnerdf986172009-01-02 07:01:27 +0000701 } else {
702 if (GV->getType()->getElementType() != Ty)
703 return Error(TyLoc,
704 "forward reference and definition of global have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000705
Chris Lattnerdf986172009-01-02 07:01:27 +0000706 // Move the forward-reference to the correct spot in the module.
707 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
708 }
709
710 if (Name.empty())
711 NumberedVals.push_back(GV);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000712
Chris Lattnerdf986172009-01-02 07:01:27 +0000713 // Set the parsed properties on the global.
714 if (Init)
715 GV->setInitializer(Init);
716 GV->setConstant(IsConstant);
717 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
718 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Michael Gottesmana2de37c2013-02-05 05:57:38 +0000719 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgce718ff2012-06-23 11:37:03 +0000720 GV->setThreadLocalMode(TLM);
Rafael Espindolabea46262011-01-08 16:42:36 +0000721 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000722
Chris Lattnerdf986172009-01-02 07:01:27 +0000723 // Parse attributes on the global.
724 while (Lex.getKind() == lltok::comma) {
725 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000726
Chris Lattnerdf986172009-01-02 07:01:27 +0000727 if (Lex.getKind() == lltok::kw_section) {
728 Lex.Lex();
729 GV->setSection(Lex.getStrVal());
730 if (ParseToken(lltok::StringConstant, "expected global section string"))
731 return true;
732 } else if (Lex.getKind() == lltok::kw_align) {
733 unsigned Alignment;
734 if (ParseOptionalAlignment(Alignment)) return true;
735 GV->setAlignment(Alignment);
736 } else {
737 TokError("unknown global variable property!");
738 }
739 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000740
Chris Lattnerdf986172009-01-02 07:01:27 +0000741 return false;
742}
743
Bill Wendling95ce4c22013-02-06 06:52:58 +0000744/// ParseUnnamedAttrGrp
745/// ::= AttrGrpID '=' '{' AttrValPair+ '}'
746bool LLParser::ParseUnnamedAttrGrp() {
747 assert(Lex.getKind() == lltok::AttrGrpID);
748 LocTy AttrGrpLoc = Lex.getLoc();
749 unsigned VarID = Lex.getUIntVal();
750 Lex.Lex();
751
752 if (ParseToken(lltok::equal, "expected '=' here") ||
753 ParseToken(lltok::kw_attributes, "expected 'attributes' keyword here") ||
754 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendlingea007fa2013-02-08 00:52:31 +0000755 ParseFnAttributeValuePairs(ForwardRefAttrBuilder[VarID], true) ||
Bill Wendling95ce4c22013-02-06 06:52:58 +0000756 ParseToken(lltok::rbrace, "expected end of attribute group"))
757 return true;
758
759 if (!ForwardRefAttrBuilder[VarID].hasAttributes())
760 return Error(AttrGrpLoc, "attribute group has no attributes");
761
762 return false;
763}
764
Bill Wendlingea007fa2013-02-08 00:52:31 +0000765/// ParseFnAttributeValuePairs
Bill Wendling95ce4c22013-02-06 06:52:58 +0000766/// ::= <attr> | <attr> '=' <value>
Bill Wendlingea007fa2013-02-08 00:52:31 +0000767bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B, bool inAttrGrp) {
768 bool HaveError = false;
769
770 B.clear();
771
Bill Wendling95ce4c22013-02-06 06:52:58 +0000772 while (true) {
773 lltok::Kind Token = Lex.getKind();
774 switch (Token) {
775 default:
Bill Wendlingea007fa2013-02-08 00:52:31 +0000776 if (!inAttrGrp) return HaveError;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000777 return Error(Lex.getLoc(), "unterminated attribute group");
778 case lltok::rbrace:
779 // Finished.
780 return false;
781
782 // Target-dependent attributes:
783 case lltok::StringConstant: {
784 std::string Attr = Lex.getStrVal();
785 Lex.Lex();
786 std::string Val;
787 if (EatIfPresent(lltok::equal) &&
788 ParseStringConstant(Val))
789 return true;
790
791 B.addAttribute(Attr, Val);
792 break;
793 }
794
795 // Target-independent attributes:
796 case lltok::kw_align: {
Bill Wendlingea007fa2013-02-08 00:52:31 +0000797 // As a hack, we allow "align 2" on functions as a synonym for "alignstack
798 // 2".
Bill Wendling95ce4c22013-02-06 06:52:58 +0000799 unsigned Alignment;
Bill Wendlingea007fa2013-02-08 00:52:31 +0000800 if (inAttrGrp) {
801 if (ParseToken(lltok::equal, "expected '=' here") ||
802 ParseUInt32(Alignment))
803 return true;
804 } else {
805 if (ParseOptionalAlignment(Alignment))
806 return true;
807 }
Bill Wendling95ce4c22013-02-06 06:52:58 +0000808 B.addAlignmentAttr(Alignment);
Bill Wendlingea007fa2013-02-08 00:52:31 +0000809 continue;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000810 }
811 case lltok::kw_alignstack: {
812 unsigned Alignment;
Bill Wendlingea007fa2013-02-08 00:52:31 +0000813 if (inAttrGrp) {
814 if (ParseToken(lltok::equal, "expected '=' here") ||
815 ParseUInt32(Alignment))
816 return true;
817 } else {
818 if (ParseOptionalStackAlignment(Alignment))
819 return true;
820 }
Bill Wendling95ce4c22013-02-06 06:52:58 +0000821 B.addStackAlignmentAttr(Alignment);
Bill Wendlingea007fa2013-02-08 00:52:31 +0000822 continue;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000823 }
824 case lltok::kw_address_safety: B.addAttribute(Attribute::AddressSafety); break;
825 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000826 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000827 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
828 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000829 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
830 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
831 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
832 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
833 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
834 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
835 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
836 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
837 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
838 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
839 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000840 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
841 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
842 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
843 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendlingea007fa2013-02-08 00:52:31 +0000844
845 // Error handling.
846 case lltok::kw_inreg:
847 case lltok::kw_signext:
848 case lltok::kw_zeroext:
849 HaveError |=
850 Error(Lex.getLoc(),
851 "invalid use of attribute on a function");
852 break;
853 case lltok::kw_byval:
854 case lltok::kw_nest:
855 case lltok::kw_noalias:
856 case lltok::kw_nocapture:
857 case lltok::kw_sret:
858 HaveError |=
859 Error(Lex.getLoc(),
860 "invalid use of parameter-only attribute on a function");
861 break;
Bill Wendling95ce4c22013-02-06 06:52:58 +0000862 }
863
864 Lex.Lex();
865 }
866}
Chris Lattnerdf986172009-01-02 07:01:27 +0000867
868//===----------------------------------------------------------------------===//
869// GlobalValue Reference/Resolution Routines.
870//===----------------------------------------------------------------------===//
871
872/// GetGlobalVal - Get a value with the specified name or ID, creating a
873/// forward reference record if needed. This can return null if the value
874/// exists but does not have the right type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000875GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerdf986172009-01-02 07:01:27 +0000876 LocTy Loc) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000877 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +0000878 if (PTy == 0) {
879 Error(Loc, "global variable reference must have pointer type");
880 return 0;
881 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000882
Chris Lattnerdf986172009-01-02 07:01:27 +0000883 // Look this name up in the normal function symbol table.
884 GlobalValue *Val =
885 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000886
Chris Lattnerdf986172009-01-02 07:01:27 +0000887 // If this is a forward reference for the value, see if we already created a
888 // forward ref record.
889 if (Val == 0) {
890 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
891 I = ForwardRefVals.find(Name);
892 if (I != ForwardRefVals.end())
893 Val = I->second.first;
894 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000895
Chris Lattnerdf986172009-01-02 07:01:27 +0000896 // If we have the value in the symbol table or fwd-ref table, return it.
897 if (Val) {
898 if (Val->getType() == Ty) return Val;
899 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +0000900 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +0000901 return 0;
902 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000903
Chris Lattnerdf986172009-01-02 07:01:27 +0000904 // Otherwise, create a new forward reference for this value and remember it.
905 GlobalValue *FwdVal;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000906 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000907 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattner1afcace2011-07-09 17:41:24 +0000908 else
Owen Andersone9b11b42009-07-08 19:03:57 +0000909 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Justin Holewinskieaff2d52012-11-16 21:03:47 +0000910 GlobalValue::ExternalWeakLinkage, 0, Name,
911 0, GlobalVariable::NotThreadLocal,
912 PTy->getAddressSpace());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000913
Chris Lattnerdf986172009-01-02 07:01:27 +0000914 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
915 return FwdVal;
916}
917
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000918GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
919 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +0000920 if (PTy == 0) {
921 Error(Loc, "global variable reference must have pointer type");
922 return 0;
923 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000924
Chris Lattnerdf986172009-01-02 07:01:27 +0000925 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000926
Chris Lattnerdf986172009-01-02 07:01:27 +0000927 // If this is a forward reference for the value, see if we already created a
928 // forward ref record.
929 if (Val == 0) {
930 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
931 I = ForwardRefValIDs.find(ID);
932 if (I != ForwardRefValIDs.end())
933 Val = I->second.first;
934 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000935
Chris Lattnerdf986172009-01-02 07:01:27 +0000936 // If we have the value in the symbol table or fwd-ref table, return it.
937 if (Val) {
938 if (Val->getType() == Ty) return Val;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000939 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +0000940 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +0000941 return 0;
942 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000943
Chris Lattnerdf986172009-01-02 07:01:27 +0000944 // Otherwise, create a new forward reference for this value and remember it.
945 GlobalValue *FwdVal;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000946 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000947 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattner1afcace2011-07-09 17:41:24 +0000948 else
Owen Andersone9b11b42009-07-08 19:03:57 +0000949 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
950 GlobalValue::ExternalWeakLinkage, 0, "");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000951
Chris Lattnerdf986172009-01-02 07:01:27 +0000952 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
953 return FwdVal;
954}
955
956
957//===----------------------------------------------------------------------===//
958// Helper Routines.
959//===----------------------------------------------------------------------===//
960
961/// ParseToken - If the current token has the specified kind, eat it and return
962/// success. Otherwise, emit the specified error and return failure.
963bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
964 if (Lex.getKind() != T)
965 return TokError(ErrMsg);
966 Lex.Lex();
967 return false;
968}
969
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000970/// ParseStringConstant
971/// ::= StringConstant
972bool LLParser::ParseStringConstant(std::string &Result) {
973 if (Lex.getKind() != lltok::StringConstant)
974 return TokError("expected string constant");
975 Result = Lex.getStrVal();
976 Lex.Lex();
977 return false;
978}
979
980/// ParseUInt32
981/// ::= uint32
982bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000983 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
984 return TokError("expected integer");
985 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
986 if (Val64 != unsigned(Val64))
987 return TokError("expected 32-bit integer (too large)");
988 Val = Val64;
989 Lex.Lex();
990 return false;
991}
992
Hans Wennborgce718ff2012-06-23 11:37:03 +0000993/// ParseTLSModel
994/// := 'localdynamic'
995/// := 'initialexec'
996/// := 'localexec'
997bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
998 switch (Lex.getKind()) {
999 default:
1000 return TokError("expected localdynamic, initialexec or localexec");
1001 case lltok::kw_localdynamic:
1002 TLM = GlobalVariable::LocalDynamicTLSModel;
1003 break;
1004 case lltok::kw_initialexec:
1005 TLM = GlobalVariable::InitialExecTLSModel;
1006 break;
1007 case lltok::kw_localexec:
1008 TLM = GlobalVariable::LocalExecTLSModel;
1009 break;
1010 }
1011
1012 Lex.Lex();
1013 return false;
1014}
1015
1016/// ParseOptionalThreadLocal
1017/// := /*empty*/
1018/// := 'thread_local'
1019/// := 'thread_local' '(' tlsmodel ')'
1020bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1021 TLM = GlobalVariable::NotThreadLocal;
1022 if (!EatIfPresent(lltok::kw_thread_local))
1023 return false;
1024
1025 TLM = GlobalVariable::GeneralDynamicTLSModel;
1026 if (Lex.getKind() == lltok::lparen) {
1027 Lex.Lex();
1028 return ParseTLSModel(TLM) ||
1029 ParseToken(lltok::rparen, "expected ')' after thread local model");
1030 }
1031 return false;
1032}
Chris Lattnerdf986172009-01-02 07:01:27 +00001033
1034/// ParseOptionalAddrSpace
1035/// := /*empty*/
1036/// := 'addrspace' '(' uint32 ')'
1037bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1038 AddrSpace = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001039 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerdf986172009-01-02 07:01:27 +00001040 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001041 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001042 ParseUInt32(AddrSpace) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00001043 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001044}
Chris Lattnerdf986172009-01-02 07:01:27 +00001045
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001046/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1047bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1048 bool HaveError = false;
1049
1050 B.clear();
1051
1052 while (1) {
1053 lltok::Kind Token = Lex.getKind();
1054 switch (Token) {
1055 default: // End of attributes.
1056 return HaveError;
Chris Lattnerdf986172009-01-02 07:01:27 +00001057 case lltok::kw_align: {
1058 unsigned Alignment;
1059 if (ParseOptionalAlignment(Alignment))
1060 return true;
Bill Wendling03272442012-10-08 22:20:14 +00001061 B.addAlignmentAttr(Alignment);
Chris Lattnerdf986172009-01-02 07:01:27 +00001062 continue;
1063 }
Bill Wendling034b94b2012-12-19 07:18:57 +00001064 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
1065 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1066 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1067 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1068 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
1069 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1070 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1071 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davis1e063d12010-02-12 00:31:15 +00001072
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001073 case lltok::kw_noreturn: case lltok::kw_nounwind:
1074 case lltok::kw_uwtable: case lltok::kw_returns_twice:
1075 case lltok::kw_noinline: case lltok::kw_readnone:
1076 case lltok::kw_readonly: case lltok::kw_inlinehint:
1077 case lltok::kw_alwaysinline: case lltok::kw_optsize:
1078 case lltok::kw_ssp: case lltok::kw_sspreq:
1079 case lltok::kw_noredzone: case lltok::kw_noimplicitfloat:
1080 case lltok::kw_naked: case lltok::kw_nonlazybind:
1081 case lltok::kw_address_safety: case lltok::kw_minsize:
1082 case lltok::kw_alignstack:
1083 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1084 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001085 }
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001086
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001087 Lex.Lex();
1088 }
1089}
1090
1091/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1092bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1093 bool HaveError = false;
1094
1095 B.clear();
1096
1097 while (1) {
1098 lltok::Kind Token = Lex.getKind();
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001099 switch (Token) {
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001100 default: // End of attributes.
1101 return HaveError;
Bill Wendling034b94b2012-12-19 07:18:57 +00001102 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1103 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1104 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1105 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001106
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001107 // Error handling.
1108 case lltok::kw_sret: case lltok::kw_nocapture:
1109 case lltok::kw_byval: case lltok::kw_nest:
1110 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001111 break;
James Molloy67ae1352012-12-20 16:04:27 +00001112
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001113 case lltok::kw_noreturn: case lltok::kw_nounwind:
1114 case lltok::kw_uwtable: case lltok::kw_returns_twice:
1115 case lltok::kw_noinline: case lltok::kw_readnone:
1116 case lltok::kw_readonly: case lltok::kw_inlinehint:
1117 case lltok::kw_alwaysinline: case lltok::kw_optsize:
1118 case lltok::kw_ssp: case lltok::kw_sspreq:
Bill Wendling114baee2013-01-23 06:41:41 +00001119 case lltok::kw_sspstrong: case lltok::kw_noimplicitfloat:
1120 case lltok::kw_noredzone: case lltok::kw_naked:
1121 case lltok::kw_nonlazybind: case lltok::kw_address_safety:
1122 case lltok::kw_minsize: case lltok::kw_alignstack:
1123 case lltok::kw_align: case lltok::kw_noduplicate:
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001124 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001125 break;
1126 }
1127
Chris Lattnerdf986172009-01-02 07:01:27 +00001128 Lex.Lex();
1129 }
1130}
1131
1132/// ParseOptionalLinkage
1133/// ::= /*empty*/
Rafael Espindolabb46f522009-01-15 20:18:42 +00001134/// ::= 'private'
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001135/// ::= 'linker_private'
Bill Wendling5e721d72010-07-01 21:55:59 +00001136/// ::= 'linker_private_weak'
Chris Lattnerdf986172009-01-02 07:01:27 +00001137/// ::= 'internal'
1138/// ::= 'weak'
Duncan Sands667d4b82009-03-07 15:45:40 +00001139/// ::= 'weak_odr'
Chris Lattnerdf986172009-01-02 07:01:27 +00001140/// ::= 'linkonce'
Duncan Sands667d4b82009-03-07 15:45:40 +00001141/// ::= 'linkonce_odr'
Bill Wendling32811be2012-08-17 18:33:14 +00001142/// ::= 'linkonce_odr_auto_hide'
Bill Wendling5e721d72010-07-01 21:55:59 +00001143/// ::= 'available_externally'
Chris Lattnerdf986172009-01-02 07:01:27 +00001144/// ::= 'appending'
1145/// ::= 'dllexport'
1146/// ::= 'common'
1147/// ::= 'dllimport'
1148/// ::= 'extern_weak'
1149/// ::= 'external'
1150bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1151 HasLinkage = false;
1152 switch (Lex.getKind()) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001153 default: Res=GlobalValue::ExternalLinkage; return false;
1154 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
1155 case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001156 case lltok::kw_linker_private_weak:
1157 Res = GlobalValue::LinkerPrivateWeakLinkage;
1158 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001159 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1160 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1161 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1162 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1163 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Bill Wendling32811be2012-08-17 18:33:14 +00001164 case lltok::kw_linkonce_odr_auto_hide:
1165 case lltok::kw_linker_private_weak_def_auto: // FIXME: For backwards compat.
1166 Res = GlobalValue::LinkOnceODRAutoHideLinkage;
1167 break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001168 case lltok::kw_available_externally:
1169 Res = GlobalValue::AvailableExternallyLinkage;
1170 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001171 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
1172 case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break;
1173 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
1174 case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break;
1175 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1176 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001177 }
1178 Lex.Lex();
1179 HasLinkage = true;
1180 return false;
1181}
1182
1183/// ParseOptionalVisibility
1184/// ::= /*empty*/
1185/// ::= 'default'
1186/// ::= 'hidden'
1187/// ::= 'protected'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001188///
Chris Lattnerdf986172009-01-02 07:01:27 +00001189bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1190 switch (Lex.getKind()) {
1191 default: Res = GlobalValue::DefaultVisibility; return false;
1192 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1193 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1194 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1195 }
1196 Lex.Lex();
1197 return false;
1198}
1199
1200/// ParseOptionalCallingConv
1201/// ::= /*empty*/
1202/// ::= 'ccc'
1203/// ::= 'fastcc'
Elena Demikhovsky35752222012-10-24 14:46:16 +00001204/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerdf986172009-01-02 07:01:27 +00001205/// ::= 'coldcc'
1206/// ::= 'x86_stdcallcc'
1207/// ::= 'x86_fastcallcc'
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001208/// ::= 'x86_thiscallcc'
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001209/// ::= 'arm_apcscc'
1210/// ::= 'arm_aapcscc'
1211/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001212/// ::= 'msp430_intrcc'
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001213/// ::= 'ptx_kernel'
1214/// ::= 'ptx_device'
Micah Villmowe53d6052012-10-01 17:01:31 +00001215/// ::= 'spir_func'
1216/// ::= 'spir_kernel'
Chris Lattnerdf986172009-01-02 07:01:27 +00001217/// ::= 'cc' UINT
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001218///
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001219bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001220 switch (Lex.getKind()) {
1221 default: CC = CallingConv::C; return false;
1222 case lltok::kw_ccc: CC = CallingConv::C; break;
1223 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1224 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1225 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1226 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001227 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001228 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1229 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1230 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001231 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001232 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1233 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmowe53d6052012-10-01 17:01:31 +00001234 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1235 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovsky35752222012-10-24 14:46:16 +00001236 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001237 case lltok::kw_cc: {
1238 unsigned ArbitraryCC;
1239 Lex.Lex();
David Blaikie4d6ccb52012-01-20 21:51:11 +00001240 if (ParseUInt32(ArbitraryCC))
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001241 return true;
David Blaikie4d6ccb52012-01-20 21:51:11 +00001242 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1243 return false;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001244 }
Chris Lattnerdf986172009-01-02 07:01:27 +00001245 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001246
Chris Lattnerdf986172009-01-02 07:01:27 +00001247 Lex.Lex();
1248 return false;
1249}
1250
Chris Lattnerb8c46862009-12-30 05:31:19 +00001251/// ParseInstructionMetadata
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001252/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman9d072f52010-08-24 02:05:17 +00001253bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1254 PerFunctionState *PFS) {
Chris Lattnerb8c46862009-12-30 05:31:19 +00001255 do {
1256 if (Lex.getKind() != lltok::MetadataVar)
1257 return TokError("expected metadata after comma");
Devang Patel0475c912009-09-29 00:01:14 +00001258
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001259 std::string Name = Lex.getStrVal();
Benjamin Kramer85dadec2011-12-06 11:50:26 +00001260 unsigned MDK = M->getMDKindID(Name);
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001261 Lex.Lex();
Chris Lattner52e20312009-10-19 05:31:10 +00001262
Chris Lattner442ffa12009-12-29 21:53:55 +00001263 MDNode *Node;
Chris Lattner449c3102010-04-01 05:14:45 +00001264 SMLoc Loc = Lex.getLoc();
Dan Gohman309b3af2010-08-24 02:24:03 +00001265
1266 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattnere434d272009-12-30 04:56:59 +00001267 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001268
Dan Gohman68261142010-08-24 14:35:45 +00001269 // This code is similar to that of ParseMetadataValue, however it needs to
1270 // have special-case code for a forward reference; see the comments on
1271 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1272 // at the top level here.
Dan Gohman309b3af2010-08-24 02:24:03 +00001273 if (Lex.getKind() == lltok::lbrace) {
1274 ValID ID;
1275 if (ParseMetadataListValue(ID, PFS))
1276 return true;
1277 assert(ID.Kind == ValID::t_MDNode);
1278 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner449c3102010-04-01 05:14:45 +00001279 } else {
Nick Lewyckyc6877b42010-09-30 21:04:13 +00001280 unsigned NodeID = 0;
Dan Gohman309b3af2010-08-24 02:24:03 +00001281 if (ParseMDNodeID(Node, NodeID))
1282 return true;
1283 if (Node) {
1284 // If we got the node, add it to the instruction.
1285 Inst->setMetadata(MDK, Node);
1286 } else {
1287 MDRef R = { Loc, MDK, NodeID };
1288 // Otherwise, remember that this should be resolved later.
1289 ForwardRefInstMetadata[Inst].push_back(R);
1290 }
Chris Lattner449c3102010-04-01 05:14:45 +00001291 }
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001292
1293 // If this is the end of the list, we're done.
Chris Lattnerb8c46862009-12-30 05:31:19 +00001294 } while (EatIfPresent(lltok::comma));
1295 return false;
Devang Patelf633a062009-09-17 23:04:48 +00001296}
1297
Chris Lattnerdf986172009-01-02 07:01:27 +00001298/// ParseOptionalAlignment
1299/// ::= /* empty */
1300/// ::= 'align' 4
1301bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1302 Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001303 if (!EatIfPresent(lltok::kw_align))
1304 return false;
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001305 LocTy AlignLoc = Lex.getLoc();
1306 if (ParseUInt32(Alignment)) return true;
1307 if (!isPowerOf2_32(Alignment))
1308 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmane16829b2010-07-30 21:07:05 +00001309 if (Alignment > Value::MaximumAlignment)
Dan Gohman138aa2a2010-07-28 20:12:04 +00001310 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001311 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001312}
1313
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001314/// ParseOptionalCommaAlign
Michael Ilseman407a6162012-11-15 22:34:00 +00001315/// ::=
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001316/// ::= ',' align 4
1317///
1318/// This returns with AteExtraComma set to true if it ate an excess comma at the
1319/// end.
1320bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1321 bool &AteExtraComma) {
1322 AteExtraComma = false;
1323 while (EatIfPresent(lltok::comma)) {
1324 // Metadata at the end is an early exit.
Chris Lattner1d928312009-12-30 05:02:06 +00001325 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001326 AteExtraComma = true;
1327 return false;
1328 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001329
Chris Lattner093eed12010-04-23 00:50:50 +00001330 if (Lex.getKind() != lltok::kw_align)
1331 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sandsbf9fc532010-10-21 16:07:10 +00001332
Chris Lattner093eed12010-04-23 00:50:50 +00001333 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001334 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001335
Devang Patelf633a062009-09-17 23:04:48 +00001336 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001337}
1338
Eli Friedman47f35132011-07-25 23:16:38 +00001339/// ParseScopeAndOrdering
1340/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1341/// else: ::=
1342///
1343/// This sets Scope and Ordering to the parsed values.
1344bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1345 AtomicOrdering &Ordering) {
1346 if (!isAtomic)
1347 return false;
1348
1349 Scope = CrossThread;
1350 if (EatIfPresent(lltok::kw_singlethread))
1351 Scope = SingleThread;
1352 switch (Lex.getKind()) {
1353 default: return TokError("Expected ordering on atomic instruction");
1354 case lltok::kw_unordered: Ordering = Unordered; break;
1355 case lltok::kw_monotonic: Ordering = Monotonic; break;
1356 case lltok::kw_acquire: Ordering = Acquire; break;
1357 case lltok::kw_release: Ordering = Release; break;
1358 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1359 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1360 }
1361 Lex.Lex();
1362 return false;
1363}
1364
Charles Davis1e063d12010-02-12 00:31:15 +00001365/// ParseOptionalStackAlignment
1366/// ::= /* empty */
1367/// ::= 'alignstack' '(' 4 ')'
1368bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1369 Alignment = 0;
1370 if (!EatIfPresent(lltok::kw_alignstack))
1371 return false;
1372 LocTy ParenLoc = Lex.getLoc();
1373 if (!EatIfPresent(lltok::lparen))
1374 return Error(ParenLoc, "expected '('");
1375 LocTy AlignLoc = Lex.getLoc();
1376 if (ParseUInt32(Alignment)) return true;
1377 ParenLoc = Lex.getLoc();
1378 if (!EatIfPresent(lltok::rparen))
1379 return Error(ParenLoc, "expected ')'");
1380 if (!isPowerOf2_32(Alignment))
1381 return Error(AlignLoc, "stack alignment is not a power of two");
1382 return false;
1383}
Devang Patelf633a062009-09-17 23:04:48 +00001384
Chris Lattner628c13a2009-12-30 05:14:00 +00001385/// ParseIndexList - This parses the index list for an insert/extractvalue
1386/// instruction. This sets AteExtraComma in the case where we eat an extra
1387/// comma at the end of the line and find that it is followed by metadata.
1388/// Clients that don't allow metadata can call the version of this function that
1389/// only takes one argument.
1390///
Chris Lattnerdf986172009-01-02 07:01:27 +00001391/// ParseIndexList
1392/// ::= (',' uint32)+
Chris Lattner628c13a2009-12-30 05:14:00 +00001393///
1394bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1395 bool &AteExtraComma) {
1396 AteExtraComma = false;
Michael Ilseman407a6162012-11-15 22:34:00 +00001397
Chris Lattnerdf986172009-01-02 07:01:27 +00001398 if (Lex.getKind() != lltok::comma)
1399 return TokError("expected ',' as start of index list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001400
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001401 while (EatIfPresent(lltok::comma)) {
Chris Lattner628c13a2009-12-30 05:14:00 +00001402 if (Lex.getKind() == lltok::MetadataVar) {
1403 AteExtraComma = true;
1404 return false;
1405 }
Nick Lewycky28815c42010-09-29 23:32:20 +00001406 unsigned Idx = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001407 if (ParseUInt32(Idx)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001408 Indices.push_back(Idx);
1409 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001410
Chris Lattnerdf986172009-01-02 07:01:27 +00001411 return false;
1412}
1413
1414//===----------------------------------------------------------------------===//
1415// Type Parsing.
1416//===----------------------------------------------------------------------===//
1417
Chris Lattner1afcace2011-07-09 17:41:24 +00001418/// ParseType - Parse a type.
1419bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1420 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001421 switch (Lex.getKind()) {
1422 default:
1423 return TokError("expected type");
1424 case lltok::Type:
Chris Lattner1afcace2011-07-09 17:41:24 +00001425 // Type ::= 'float' | 'void' (etc)
Chris Lattnerdf986172009-01-02 07:01:27 +00001426 Result = Lex.getTyVal();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001427 Lex.Lex();
Chris Lattnerdf986172009-01-02 07:01:27 +00001428 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001429 case lltok::lbrace:
Chris Lattner1afcace2011-07-09 17:41:24 +00001430 // Type ::= StructType
1431 if (ParseAnonStructType(Result, false))
Chris Lattnerdf986172009-01-02 07:01:27 +00001432 return true;
1433 break;
1434 case lltok::lsquare:
Chris Lattner1afcace2011-07-09 17:41:24 +00001435 // Type ::= '[' ... ']'
Chris Lattnerdf986172009-01-02 07:01:27 +00001436 Lex.Lex(); // eat the lsquare.
1437 if (ParseArrayVectorType(Result, false))
1438 return true;
1439 break;
1440 case lltok::less: // Either vector or packed struct.
Chris Lattner1afcace2011-07-09 17:41:24 +00001441 // Type ::= '<' ... '>'
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001442 Lex.Lex();
1443 if (Lex.getKind() == lltok::lbrace) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001444 if (ParseAnonStructType(Result, true) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001445 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerdf986172009-01-02 07:01:27 +00001446 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001447 } else if (ParseArrayVectorType(Result, true))
1448 return true;
1449 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001450 case lltok::LocalVar: {
1451 // Type ::= %foo
1452 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman407a6162012-11-15 22:34:00 +00001453
Chris Lattner1afcace2011-07-09 17:41:24 +00001454 // If the type hasn't been defined yet, create a forward definition and
1455 // remember where that forward def'n was seen (in case it never is defined).
1456 if (Entry.first == 0) {
Chris Lattner3ebb6492011-08-12 18:06:37 +00001457 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattner1afcace2011-07-09 17:41:24 +00001458 Entry.second = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001459 }
Chris Lattner1afcace2011-07-09 17:41:24 +00001460 Result = Entry.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001461 Lex.Lex();
1462 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001463 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001464
Chris Lattner1afcace2011-07-09 17:41:24 +00001465 case lltok::LocalVarID: {
1466 // Type ::= %4
1467 if (Lex.getUIntVal() >= NumberedTypes.size())
1468 NumberedTypes.resize(Lex.getUIntVal()+1);
1469 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman407a6162012-11-15 22:34:00 +00001470
Chris Lattner1afcace2011-07-09 17:41:24 +00001471 // If the type hasn't been defined yet, create a forward definition and
1472 // remember where that forward def'n was seen (in case it never is defined).
1473 if (Entry.first == 0) {
Chris Lattner3ebb6492011-08-12 18:06:37 +00001474 Entry.first = StructType::create(Context);
Chris Lattner1afcace2011-07-09 17:41:24 +00001475 Entry.second = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001476 }
Chris Lattner1afcace2011-07-09 17:41:24 +00001477 Result = Entry.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001478 Lex.Lex();
1479 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001480 }
1481 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001482
1483 // Parse the type suffixes.
Chris Lattnerdf986172009-01-02 07:01:27 +00001484 while (1) {
1485 switch (Lex.getKind()) {
1486 // End of type.
Chris Lattner1afcace2011-07-09 17:41:24 +00001487 default:
1488 if (!AllowVoid && Result->isVoidTy())
1489 return Error(TypeLoc, "void type only allowed for function results");
1490 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001491
Chris Lattner1afcace2011-07-09 17:41:24 +00001492 // Type ::= Type '*'
Chris Lattnerdf986172009-01-02 07:01:27 +00001493 case lltok::star:
Chris Lattner1afcace2011-07-09 17:41:24 +00001494 if (Result->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001495 return TokError("basic block pointers are invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001496 if (Result->isVoidTy())
1497 return TokError("pointers to void are invalid - use i8* instead");
1498 if (!PointerType::isValidElementType(Result))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001499 return TokError("pointer to this type is invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001500 Result = PointerType::getUnqual(Result);
Chris Lattnerdf986172009-01-02 07:01:27 +00001501 Lex.Lex();
1502 break;
1503
Chris Lattner1afcace2011-07-09 17:41:24 +00001504 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerdf986172009-01-02 07:01:27 +00001505 case lltok::kw_addrspace: {
Chris Lattner1afcace2011-07-09 17:41:24 +00001506 if (Result->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001507 return TokError("basic block pointers are invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001508 if (Result->isVoidTy())
Dan Gohmanb9070d32009-02-09 17:41:21 +00001509 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattner1afcace2011-07-09 17:41:24 +00001510 if (!PointerType::isValidElementType(Result))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001511 return TokError("pointer to this type is invalid");
Chris Lattnerdf986172009-01-02 07:01:27 +00001512 unsigned AddrSpace;
1513 if (ParseOptionalAddrSpace(AddrSpace) ||
1514 ParseToken(lltok::star, "expected '*' in address space"))
1515 return true;
1516
Chris Lattner1afcace2011-07-09 17:41:24 +00001517 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerdf986172009-01-02 07:01:27 +00001518 break;
1519 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001520
Chris Lattnerdf986172009-01-02 07:01:27 +00001521 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1522 case lltok::lparen:
1523 if (ParseFunctionType(Result))
1524 return true;
1525 break;
1526 }
1527 }
1528}
1529
1530/// ParseParameterList
1531/// ::= '(' ')'
1532/// ::= '(' Arg (',' Arg)* ')'
1533/// Arg
1534/// ::= Type OptionalAttributes Value OptionalAttributes
1535bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1536 PerFunctionState &PFS) {
1537 if (ParseToken(lltok::lparen, "expected '(' in call"))
1538 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001539
Bill Wendling73dee182013-01-31 00:29:54 +00001540 unsigned AttrIndex = 1;
Chris Lattnerdf986172009-01-02 07:01:27 +00001541 while (Lex.getKind() != lltok::rparen) {
1542 // If this isn't the first argument, we need a comma.
1543 if (!ArgList.empty() &&
1544 ParseToken(lltok::comma, "expected ',' in argument list"))
1545 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001546
Chris Lattnerdf986172009-01-02 07:01:27 +00001547 // Parse the argument.
1548 LocTy ArgLoc;
Chris Lattner1afcace2011-07-09 17:41:24 +00001549 Type *ArgTy = 0;
Bill Wendling702cc912012-10-15 20:35:56 +00001550 AttrBuilder ArgAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00001551 Value *V;
Victor Hernandez19715562009-12-03 23:40:58 +00001552 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00001553 return true;
Victor Hernandez19715562009-12-03 23:40:58 +00001554
Chris Lattner287881d2009-12-30 02:11:14 +00001555 // Otherwise, handle normal operands.
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001556 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner287881d2009-12-30 02:11:14 +00001557 return true;
Bill Wendling73dee182013-01-31 00:29:54 +00001558 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1559 AttrIndex++,
1560 ArgAttrs)));
Chris Lattnerdf986172009-01-02 07:01:27 +00001561 }
1562
1563 Lex.Lex(); // Lex the ')'.
1564 return false;
1565}
1566
1567
1568
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001569/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattner1afcace2011-07-09 17:41:24 +00001570/// prototype.
Chris Lattnerdf986172009-01-02 07:01:27 +00001571/// ::= '(' ArgTypeListI ')'
1572/// ArgTypeListI
1573/// ::= /*empty*/
1574/// ::= '...'
1575/// ::= ArgTypeList ',' '...'
1576/// ::= ArgType (',' ArgType)*
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001577///
Chris Lattner1afcace2011-07-09 17:41:24 +00001578bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1579 bool &isVarArg){
Chris Lattnerdf986172009-01-02 07:01:27 +00001580 isVarArg = false;
1581 assert(Lex.getKind() == lltok::lparen);
1582 Lex.Lex(); // eat the (.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001583
Chris Lattnerdf986172009-01-02 07:01:27 +00001584 if (Lex.getKind() == lltok::rparen) {
1585 // empty
1586 } else if (Lex.getKind() == lltok::dotdotdot) {
1587 isVarArg = true;
1588 Lex.Lex();
1589 } else {
1590 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001591 Type *ArgTy = 0;
Bill Wendling702cc912012-10-15 20:35:56 +00001592 AttrBuilder Attrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00001593 std::string Name;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001594
Chris Lattner1afcace2011-07-09 17:41:24 +00001595 if (ParseType(ArgTy) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001596 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001597
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001598 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001599 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001600
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00001601 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001602 Name = Lex.getStrVal();
1603 Lex.Lex();
1604 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001605
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001606 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001607 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001608
Bill Wendling73dee182013-01-31 00:29:54 +00001609 unsigned AttrIndex = 1;
Bill Wendlingcb3de0b2012-10-15 04:46:55 +00001610 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendling73dee182013-01-31 00:29:54 +00001611 AttributeSet::get(ArgTy->getContext(),
1612 AttrIndex++, Attrs), Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001613
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001614 while (EatIfPresent(lltok::comma)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001615 // Handle ... at end of arg list.
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001616 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001617 isVarArg = true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001618 break;
1619 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001620
Chris Lattnerdf986172009-01-02 07:01:27 +00001621 // Otherwise must be an argument type.
1622 TypeLoc = Lex.getLoc();
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001623 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001624
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001625 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001626 return Error(TypeLoc, "argument can not have void type");
1627
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00001628 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001629 Name = Lex.getStrVal();
1630 Lex.Lex();
1631 } else {
1632 Name = "";
1633 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001634
Chris Lattner1afcace2011-07-09 17:41:24 +00001635 if (!ArgTy->isFirstClassType())
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001636 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001637
Bill Wendlingcb3de0b2012-10-15 04:46:55 +00001638 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendling73dee182013-01-31 00:29:54 +00001639 AttributeSet::get(ArgTy->getContext(),
1640 AttrIndex++, Attrs),
Bill Wendlingcb3de0b2012-10-15 04:46:55 +00001641 Name));
Chris Lattnerdf986172009-01-02 07:01:27 +00001642 }
1643 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001644
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001645 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerdf986172009-01-02 07:01:27 +00001646}
Daniel Dunbara279bc32009-09-20 02:20:51 +00001647
Chris Lattnerdf986172009-01-02 07:01:27 +00001648/// ParseFunctionType
1649/// ::= Type ArgumentList OptionalAttrs
Chris Lattner1afcace2011-07-09 17:41:24 +00001650bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001651 assert(Lex.getKind() == lltok::lparen);
1652
Chris Lattnerd77d04c2009-01-05 08:04:33 +00001653 if (!FunctionType::isValidReturnType(Result))
1654 return TokError("invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001655
Chris Lattner1afcace2011-07-09 17:41:24 +00001656 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerdf986172009-01-02 07:01:27 +00001657 bool isVarArg;
Chris Lattner1afcace2011-07-09 17:41:24 +00001658 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerdf986172009-01-02 07:01:27 +00001659 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001660
Chris Lattnerdf986172009-01-02 07:01:27 +00001661 // Reject names on the arguments lists.
1662 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1663 if (!ArgList[i].Name.empty())
1664 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendling73dee182013-01-31 00:29:54 +00001665 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattnera16546a2011-06-17 17:37:13 +00001666 return Error(ArgList[i].Loc,
1667 "argument attributes invalid in function type");
Chris Lattnerdf986172009-01-02 07:01:27 +00001668 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001669
Jay Foad5fdd6c82011-07-12 14:06:48 +00001670 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerdf986172009-01-02 07:01:27 +00001671 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattner1afcace2011-07-09 17:41:24 +00001672 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001673
Chris Lattner1afcace2011-07-09 17:41:24 +00001674 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerdf986172009-01-02 07:01:27 +00001675 return false;
1676}
1677
Chris Lattner1afcace2011-07-09 17:41:24 +00001678/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1679/// other structs.
1680bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1681 SmallVector<Type*, 8> Elts;
1682 if (ParseStructBody(Elts)) return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00001683
Chris Lattner1afcace2011-07-09 17:41:24 +00001684 Result = StructType::get(Context, Elts, Packed);
1685 return false;
1686}
1687
1688/// ParseStructDefinition - Parse a struct in a 'type' definition.
1689bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1690 std::pair<Type*, LocTy> &Entry,
1691 Type *&ResultTy) {
1692 // If the type was already defined, diagnose the redefinition.
1693 if (Entry.first && !Entry.second.isValid())
1694 return Error(TypeLoc, "redefinition of type");
Michael Ilseman407a6162012-11-15 22:34:00 +00001695
Chris Lattner1afcace2011-07-09 17:41:24 +00001696 // If we have opaque, just return without filling in the definition for the
1697 // struct. This counts as a definition as far as the .ll file goes.
1698 if (EatIfPresent(lltok::kw_opaque)) {
1699 // This type is being defined, so clear the location to indicate this.
1700 Entry.second = SMLoc();
Michael Ilseman407a6162012-11-15 22:34:00 +00001701
Chris Lattner1afcace2011-07-09 17:41:24 +00001702 // If this type number has never been uttered, create it.
1703 if (Entry.first == 0)
Chris Lattner3ebb6492011-08-12 18:06:37 +00001704 Entry.first = StructType::create(Context, Name);
Chris Lattner1afcace2011-07-09 17:41:24 +00001705 ResultTy = Entry.first;
1706 return false;
1707 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001708
Chris Lattner1afcace2011-07-09 17:41:24 +00001709 // If the type starts with '<', then it is either a packed struct or a vector.
1710 bool isPacked = EatIfPresent(lltok::less);
1711
1712 // If we don't have a struct, then we have a random type alias, which we
1713 // accept for compatibility with old files. These types are not allowed to be
1714 // forward referenced and not allowed to be recursive.
1715 if (Lex.getKind() != lltok::lbrace) {
1716 if (Entry.first)
1717 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman407a6162012-11-15 22:34:00 +00001718
Chris Lattner1afcace2011-07-09 17:41:24 +00001719 ResultTy = 0;
1720 if (isPacked)
1721 return ParseArrayVectorType(ResultTy, true);
1722 return ParseType(ResultTy);
1723 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001724
Chris Lattner1afcace2011-07-09 17:41:24 +00001725 // This type is being defined, so clear the location to indicate this.
1726 Entry.second = SMLoc();
Michael Ilseman407a6162012-11-15 22:34:00 +00001727
Chris Lattner1afcace2011-07-09 17:41:24 +00001728 // If this type number has never been uttered, create it.
1729 if (Entry.first == 0)
Chris Lattner3ebb6492011-08-12 18:06:37 +00001730 Entry.first = StructType::create(Context, Name);
Michael Ilseman407a6162012-11-15 22:34:00 +00001731
Chris Lattner1afcace2011-07-09 17:41:24 +00001732 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman407a6162012-11-15 22:34:00 +00001733
Chris Lattner1afcace2011-07-09 17:41:24 +00001734 SmallVector<Type*, 8> Body;
1735 if (ParseStructBody(Body) ||
1736 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1737 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00001738
Chris Lattner1afcace2011-07-09 17:41:24 +00001739 STy->setBody(Body, isPacked);
1740 ResultTy = STy;
1741 return false;
1742}
1743
1744
Chris Lattnerdf986172009-01-02 07:01:27 +00001745/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattner1afcace2011-07-09 17:41:24 +00001746/// StructType
Chris Lattnerdf986172009-01-02 07:01:27 +00001747/// ::= '{' '}'
Chris Lattner1afcace2011-07-09 17:41:24 +00001748/// ::= '{' Type (',' Type)* '}'
Chris Lattnerdf986172009-01-02 07:01:27 +00001749/// ::= '<' '{' '}' '>'
Chris Lattner1afcace2011-07-09 17:41:24 +00001750/// ::= '<' '{' Type (',' Type)* '}' '>'
1751bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001752 assert(Lex.getKind() == lltok::lbrace);
1753 Lex.Lex(); // Consume the '{'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001754
Chris Lattner1afcace2011-07-09 17:41:24 +00001755 // Handle the empty struct.
1756 if (EatIfPresent(lltok::rbrace))
Chris Lattnerdf986172009-01-02 07:01:27 +00001757 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001758
Chris Lattnera9a9e072009-03-09 04:49:14 +00001759 LocTy EltTyLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001760 Type *Ty = 0;
1761 if (ParseType(Ty)) return true;
1762 Body.push_back(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001763
Chris Lattner1afcace2011-07-09 17:41:24 +00001764 if (!StructType::isValidElementType(Ty))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001765 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001766
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001767 while (EatIfPresent(lltok::comma)) {
Chris Lattnera9a9e072009-03-09 04:49:14 +00001768 EltTyLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001769 if (ParseType(Ty)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001770
Chris Lattner1afcace2011-07-09 17:41:24 +00001771 if (!StructType::isValidElementType(Ty))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001772 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001773
Chris Lattner1afcace2011-07-09 17:41:24 +00001774 Body.push_back(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00001775 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001776
Chris Lattner1afcace2011-07-09 17:41:24 +00001777 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerdf986172009-01-02 07:01:27 +00001778}
1779
1780/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1781/// token has already been consumed.
Chris Lattner1afcace2011-07-09 17:41:24 +00001782/// Type
Chris Lattnerdf986172009-01-02 07:01:27 +00001783/// ::= '[' APSINTVAL 'x' Types ']'
1784/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattner1afcace2011-07-09 17:41:24 +00001785bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001786 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1787 Lex.getAPSIntVal().getBitWidth() > 64)
1788 return TokError("expected number in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001789
Chris Lattnerdf986172009-01-02 07:01:27 +00001790 LocTy SizeLoc = Lex.getLoc();
1791 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001792 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001793
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001794 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1795 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001796
1797 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001798 Type *EltTy = 0;
1799 if (ParseType(EltTy)) return true;
Chris Lattnera9a9e072009-03-09 04:49:14 +00001800
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001801 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
1802 "expected end of sequential type"))
1803 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001804
Chris Lattnerdf986172009-01-02 07:01:27 +00001805 if (isVector) {
Chris Lattner452e2622009-02-28 18:12:41 +00001806 if (Size == 0)
1807 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerdf986172009-01-02 07:01:27 +00001808 if ((unsigned)Size != Size)
1809 return Error(SizeLoc, "size too large for vector");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001810 if (!VectorType::isValidElementType(EltTy))
Duncan Sands2333e292012-11-13 12:59:33 +00001811 return Error(TypeLoc, "invalid vector element type");
Owen Andersondebcb012009-07-29 22:17:13 +00001812 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerdf986172009-01-02 07:01:27 +00001813 } else {
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001814 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001815 return Error(TypeLoc, "invalid array element type");
Chris Lattner1afcace2011-07-09 17:41:24 +00001816 Result = ArrayType::get(EltTy, Size);
Chris Lattnerdf986172009-01-02 07:01:27 +00001817 }
1818 return false;
1819}
1820
1821//===----------------------------------------------------------------------===//
1822// Function Semantic Analysis.
1823//===----------------------------------------------------------------------===//
1824
Chris Lattner09d9ef42009-10-28 03:39:23 +00001825LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
1826 int functionNumber)
1827 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001828
1829 // Insert unnamed arguments into the NumberedVals list.
1830 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1831 AI != E; ++AI)
1832 if (!AI->hasName())
1833 NumberedVals.push_back(AI);
1834}
1835
1836LLParser::PerFunctionState::~PerFunctionState() {
1837 // If there were any forward referenced non-basicblock values, delete them.
1838 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1839 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1840 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001841 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001842 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001843 delete I->second.first;
1844 I->second.first = 0;
1845 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001846
Chris Lattnerdf986172009-01-02 07:01:27 +00001847 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1848 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1849 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001850 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001851 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001852 delete I->second.first;
1853 I->second.first = 0;
1854 }
1855}
1856
Chris Lattner09d9ef42009-10-28 03:39:23 +00001857bool LLParser::PerFunctionState::FinishFunction() {
1858 // Check to see if someone took the address of labels in this block.
1859 if (!P.ForwardRefBlockAddresses.empty()) {
1860 ValID FunctionID;
1861 if (!F.getName().empty()) {
1862 FunctionID.Kind = ValID::t_GlobalName;
1863 FunctionID.StrVal = F.getName();
1864 } else {
1865 FunctionID.Kind = ValID::t_GlobalID;
1866 FunctionID.UIntVal = FunctionNumber;
1867 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001868
Chris Lattner09d9ef42009-10-28 03:39:23 +00001869 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
1870 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
1871 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
1872 // Resolve all these references.
1873 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
1874 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00001875
Chris Lattner09d9ef42009-10-28 03:39:23 +00001876 P.ForwardRefBlockAddresses.erase(FRBAI);
1877 }
1878 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001879
Chris Lattnerdf986172009-01-02 07:01:27 +00001880 if (!ForwardRefVals.empty())
1881 return P.Error(ForwardRefVals.begin()->second.second,
1882 "use of undefined value '%" + ForwardRefVals.begin()->first +
1883 "'");
1884 if (!ForwardRefValIDs.empty())
1885 return P.Error(ForwardRefValIDs.begin()->second.second,
1886 "use of undefined value '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001887 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001888 return false;
1889}
1890
1891
1892/// GetVal - Get a value with the specified name or ID, creating a
1893/// forward reference record if needed. This can return null if the value
1894/// exists but does not have the right type.
1895Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001896 Type *Ty, LocTy Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001897 // Look this name up in the normal function symbol table.
1898 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001899
Chris Lattnerdf986172009-01-02 07:01:27 +00001900 // If this is a forward reference for the value, see if we already created a
1901 // forward ref record.
1902 if (Val == 0) {
1903 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1904 I = ForwardRefVals.find(Name);
1905 if (I != ForwardRefVals.end())
1906 Val = I->second.first;
1907 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001908
Chris Lattnerdf986172009-01-02 07:01:27 +00001909 // If we have the value in the symbol table or fwd-ref table, return it.
1910 if (Val) {
1911 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001912 if (Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001913 P.Error(Loc, "'%" + Name + "' is not a basic block");
1914 else
1915 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001916 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001917 return 0;
1918 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001919
Chris Lattnerdf986172009-01-02 07:01:27 +00001920 // Don't make placeholders with invalid type.
Chris Lattner1afcace2011-07-09 17:41:24 +00001921 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001922 P.Error(Loc, "invalid use of a non-first-class type");
1923 return 0;
1924 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001925
Chris Lattnerdf986172009-01-02 07:01:27 +00001926 // Otherwise, create a new forward reference for this value and remember it.
1927 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001928 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001929 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001930 else
1931 FwdVal = new Argument(Ty, Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001932
Chris Lattnerdf986172009-01-02 07:01:27 +00001933 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1934 return FwdVal;
1935}
1936
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001937Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerdf986172009-01-02 07:01:27 +00001938 LocTy Loc) {
1939 // Look this name up in the normal function symbol table.
1940 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001941
Chris Lattnerdf986172009-01-02 07:01:27 +00001942 // If this is a forward reference for the value, see if we already created a
1943 // forward ref record.
1944 if (Val == 0) {
1945 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1946 I = ForwardRefValIDs.find(ID);
1947 if (I != ForwardRefValIDs.end())
1948 Val = I->second.first;
1949 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001950
Chris Lattnerdf986172009-01-02 07:01:27 +00001951 // If we have the value in the symbol table or fwd-ref table, return it.
1952 if (Val) {
1953 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001954 if (Ty->isLabelTy())
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001955 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerdf986172009-01-02 07:01:27 +00001956 else
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001957 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001958 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001959 return 0;
1960 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001961
Chris Lattner1afcace2011-07-09 17:41:24 +00001962 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001963 P.Error(Loc, "invalid use of a non-first-class type");
1964 return 0;
1965 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001966
Chris Lattnerdf986172009-01-02 07:01:27 +00001967 // Otherwise, create a new forward reference for this value and remember it.
1968 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001969 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001970 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001971 else
1972 FwdVal = new Argument(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001973
Chris Lattnerdf986172009-01-02 07:01:27 +00001974 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1975 return FwdVal;
1976}
1977
1978/// SetInstName - After an instruction is parsed and inserted into its
1979/// basic block, this installs its name.
1980bool LLParser::PerFunctionState::SetInstName(int NameID,
1981 const std::string &NameStr,
1982 LocTy NameLoc, Instruction *Inst) {
1983 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001984 if (Inst->getType()->isVoidTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001985 if (NameID != -1 || !NameStr.empty())
1986 return P.Error(NameLoc, "instructions returning void cannot have a name");
1987 return false;
1988 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001989
Chris Lattnerdf986172009-01-02 07:01:27 +00001990 // If this was a numbered instruction, verify that the instruction is the
1991 // expected value and resolve any forward references.
1992 if (NameStr.empty()) {
1993 // If neither a name nor an ID was specified, just use the next ID.
1994 if (NameID == -1)
1995 NameID = NumberedVals.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001996
Chris Lattnerdf986172009-01-02 07:01:27 +00001997 if (unsigned(NameID) != NumberedVals.size())
1998 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001999 Twine(NumberedVals.size()) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002000
Chris Lattnerdf986172009-01-02 07:01:27 +00002001 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2002 ForwardRefValIDs.find(NameID);
2003 if (FI != ForwardRefValIDs.end()) {
2004 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002005 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002006 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002007 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2b4f28e2009-09-02 15:02:57 +00002008 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00002009 ForwardRefValIDs.erase(FI);
2010 }
2011
2012 NumberedVals.push_back(Inst);
2013 return false;
2014 }
2015
2016 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2017 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2018 FI = ForwardRefVals.find(NameStr);
2019 if (FI != ForwardRefVals.end()) {
2020 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002021 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002022 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002023 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes531552a2009-09-02 14:22:03 +00002024 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00002025 ForwardRefVals.erase(FI);
2026 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002027
Chris Lattnerdf986172009-01-02 07:01:27 +00002028 // Set the name on the instruction.
2029 Inst->setName(NameStr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002030
Benjamin Krameraf812352010-10-16 11:28:23 +00002031 if (Inst->getName() != NameStr)
Daniel Dunbara279bc32009-09-20 02:20:51 +00002032 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00002033 NameStr + "'");
2034 return false;
2035}
2036
2037/// GetBB - Get a basic block with the specified name or ID, creating a
2038/// forward reference record if needed.
2039BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2040 LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002041 return cast_or_null<BasicBlock>(GetVal(Name,
2042 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00002043}
2044
2045BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002046 return cast_or_null<BasicBlock>(GetVal(ID,
2047 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00002048}
2049
2050/// DefineBB - Define the specified basic block, which is either named or
2051/// unnamed. If there is an error, this returns null otherwise it returns
2052/// the block being defined.
2053BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2054 LocTy Loc) {
2055 BasicBlock *BB;
2056 if (Name.empty())
2057 BB = GetBB(NumberedVals.size(), Loc);
2058 else
2059 BB = GetBB(Name, Loc);
2060 if (BB == 0) return 0; // Already diagnosed error.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002061
Chris Lattnerdf986172009-01-02 07:01:27 +00002062 // Move the block to the end of the function. Forward ref'd blocks are
2063 // inserted wherever they happen to be referenced.
2064 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002065
Chris Lattnerdf986172009-01-02 07:01:27 +00002066 // Remove the block from forward ref sets.
2067 if (Name.empty()) {
2068 ForwardRefValIDs.erase(NumberedVals.size());
2069 NumberedVals.push_back(BB);
2070 } else {
2071 // BB forward references are already in the function symbol table.
2072 ForwardRefVals.erase(Name);
2073 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002074
Chris Lattnerdf986172009-01-02 07:01:27 +00002075 return BB;
2076}
2077
2078//===----------------------------------------------------------------------===//
2079// Constants.
2080//===----------------------------------------------------------------------===//
2081
2082/// ParseValID - Parse an abstract value that doesn't necessarily have a
2083/// type implied. For example, if we parse "4" we don't know what integer type
2084/// it has. The value will later be combined with its type and checked for
Victor Hernandez24e64df2010-01-10 07:14:18 +00002085/// sanity. PFS is used to convert function-local operands of metadata (since
2086/// metadata operands are not just parsed here but also converted to values).
2087/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezbf170d42010-01-05 22:22:14 +00002088bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002089 ID.Loc = Lex.getLoc();
2090 switch (Lex.getKind()) {
2091 default: return TokError("expected value token");
2092 case lltok::GlobalID: // @42
2093 ID.UIntVal = Lex.getUIntVal();
2094 ID.Kind = ValID::t_GlobalID;
2095 break;
2096 case lltok::GlobalVar: // @foo
2097 ID.StrVal = Lex.getStrVal();
2098 ID.Kind = ValID::t_GlobalName;
2099 break;
2100 case lltok::LocalVarID: // %42
2101 ID.UIntVal = Lex.getUIntVal();
2102 ID.Kind = ValID::t_LocalID;
2103 break;
2104 case lltok::LocalVar: // %foo
Chris Lattnerdf986172009-01-02 07:01:27 +00002105 ID.StrVal = Lex.getStrVal();
2106 ID.Kind = ValID::t_LocalName;
2107 break;
Dan Gohman83448032010-07-14 18:26:50 +00002108 case lltok::exclaim: // !42, !{...}, or !"foo"
2109 return ParseMetadataValue(ID, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002110 case lltok::APSInt:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002111 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002112 ID.Kind = ValID::t_APSInt;
2113 break;
2114 case lltok::APFloat:
2115 ID.APFloatVal = Lex.getAPFloatVal();
2116 ID.Kind = ValID::t_APFloat;
2117 break;
2118 case lltok::kw_true:
Owen Anderson5defacc2009-07-31 17:39:07 +00002119 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002120 ID.Kind = ValID::t_Constant;
2121 break;
2122 case lltok::kw_false:
Owen Anderson5defacc2009-07-31 17:39:07 +00002123 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002124 ID.Kind = ValID::t_Constant;
2125 break;
2126 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2127 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2128 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002129
Chris Lattnerdf986172009-01-02 07:01:27 +00002130 case lltok::lbrace: {
2131 // ValID ::= '{' ConstVector '}'
2132 Lex.Lex();
2133 SmallVector<Constant*, 16> Elts;
2134 if (ParseGlobalValueVector(Elts) ||
2135 ParseToken(lltok::rbrace, "expected end of struct constant"))
2136 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002137
Chris Lattner1afcace2011-07-09 17:41:24 +00002138 ID.ConstantStructElts = new Constant*[Elts.size()];
2139 ID.UIntVal = Elts.size();
2140 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2141 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerdf986172009-01-02 07:01:27 +00002142 return false;
2143 }
2144 case lltok::less: {
2145 // ValID ::= '<' ConstVector '>' --> Vector.
2146 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2147 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002148 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002149
Chris Lattnerdf986172009-01-02 07:01:27 +00002150 SmallVector<Constant*, 16> Elts;
2151 LocTy FirstEltLoc = Lex.getLoc();
2152 if (ParseGlobalValueVector(Elts) ||
2153 (isPackedStruct &&
2154 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2155 ParseToken(lltok::greater, "expected end of constant"))
2156 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002157
Chris Lattnerdf986172009-01-02 07:01:27 +00002158 if (isPackedStruct) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002159 ID.ConstantStructElts = new Constant*[Elts.size()];
2160 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2161 ID.UIntVal = Elts.size();
2162 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerdf986172009-01-02 07:01:27 +00002163 return false;
2164 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002165
Chris Lattnerdf986172009-01-02 07:01:27 +00002166 if (Elts.empty())
2167 return Error(ID.Loc, "constant vector must not be empty");
2168
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002169 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem16087692011-12-05 06:29:09 +00002170 !Elts[0]->getType()->isFloatingPointTy() &&
2171 !Elts[0]->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002172 return Error(FirstEltLoc,
Nadav Rotem16087692011-12-05 06:29:09 +00002173 "vector elements must have integer, pointer or floating point type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002174
Chris Lattnerdf986172009-01-02 07:01:27 +00002175 // Verify that all the vector elements have the same type.
2176 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2177 if (Elts[i]->getType() != Elts[0]->getType())
2178 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002179 "vector element #" + Twine(i) +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002180 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002181
Chris Lattner2ca5c862011-02-15 00:14:00 +00002182 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00002183 ID.Kind = ValID::t_Constant;
2184 return false;
2185 }
2186 case lltok::lsquare: { // Array Constant
2187 Lex.Lex();
2188 SmallVector<Constant*, 16> Elts;
2189 LocTy FirstEltLoc = Lex.getLoc();
2190 if (ParseGlobalValueVector(Elts) ||
2191 ParseToken(lltok::rsquare, "expected end of array constant"))
2192 return true;
2193
2194 // Handle empty element.
2195 if (Elts.empty()) {
2196 // Use undef instead of an array because it's inconvenient to determine
2197 // the element type at this point, there being no elements to examine.
Chris Lattner081b5052009-01-05 07:52:51 +00002198 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerdf986172009-01-02 07:01:27 +00002199 return false;
2200 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002201
Chris Lattnerdf986172009-01-02 07:01:27 +00002202 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002203 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002204 getTypeString(Elts[0]->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002205
Owen Andersondebcb012009-07-29 22:17:13 +00002206 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002207
Chris Lattnerdf986172009-01-02 07:01:27 +00002208 // Verify all elements are correct type!
Chris Lattner6d6b3cc2009-01-02 08:49:06 +00002209 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002210 if (Elts[i]->getType() != Elts[0]->getType())
2211 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002212 "array element #" + Twine(i) +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002213 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00002214 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002215
Jay Foad26701082011-06-22 09:24:39 +00002216 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00002217 ID.Kind = ValID::t_Constant;
2218 return false;
2219 }
2220 case lltok::kw_c: // c "foo"
2221 Lex.Lex();
Chris Lattner18c7f802012-02-05 02:29:43 +00002222 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2223 false);
Chris Lattnerdf986172009-01-02 07:01:27 +00002224 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2225 ID.Kind = ValID::t_Constant;
2226 return false;
2227
2228 case lltok::kw_asm: {
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002229 // ValID ::= 'asm' SideEffect? AlignStack? STRINGCONSTANT ',' STRINGCONSTANT
Chad Rosier581600b2012-09-05 19:00:49 +00002230 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerdf986172009-01-02 07:01:27 +00002231 Lex.Lex();
2232 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002233 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosier581600b2012-09-05 19:00:49 +00002234 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002235 ParseStringConstant(ID.StrVal) ||
2236 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002237 ParseToken(lltok::StringConstant, "expected constraint string"))
2238 return true;
2239 ID.StrVal2 = Lex.getStrVal();
Chad Rosier36547342012-09-05 00:08:17 +00002240 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosier581600b2012-09-05 19:00:49 +00002241 (unsigned(AsmDialect)<<2);
Chris Lattnerdf986172009-01-02 07:01:27 +00002242 ID.Kind = ValID::t_InlineAsm;
2243 return false;
2244 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002245
Chris Lattner09d9ef42009-10-28 03:39:23 +00002246 case lltok::kw_blockaddress: {
2247 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2248 Lex.Lex();
2249
2250 ValID Fn, Label;
2251 LocTy FnLoc, LabelLoc;
Michael Ilseman407a6162012-11-15 22:34:00 +00002252
Chris Lattner09d9ef42009-10-28 03:39:23 +00002253 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2254 ParseValID(Fn) ||
2255 ParseToken(lltok::comma, "expected comma in block address expression")||
2256 ParseValID(Label) ||
2257 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2258 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00002259
Chris Lattner09d9ef42009-10-28 03:39:23 +00002260 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2261 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattnercdfc9402009-11-01 01:27:45 +00002262 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner09d9ef42009-10-28 03:39:23 +00002263 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman407a6162012-11-15 22:34:00 +00002264
Chris Lattner09d9ef42009-10-28 03:39:23 +00002265 // Make a global variable as a placeholder for this reference.
2266 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2267 false, GlobalValue::InternalLinkage,
2268 0, "");
2269 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2270 ID.ConstantVal = FwdRef;
2271 ID.Kind = ValID::t_Constant;
2272 return false;
2273 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002274
Chris Lattnerdf986172009-01-02 07:01:27 +00002275 case lltok::kw_trunc:
2276 case lltok::kw_zext:
2277 case lltok::kw_sext:
2278 case lltok::kw_fptrunc:
2279 case lltok::kw_fpext:
2280 case lltok::kw_bitcast:
2281 case lltok::kw_uitofp:
2282 case lltok::kw_sitofp:
2283 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002284 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00002285 case lltok::kw_inttoptr:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002286 case lltok::kw_ptrtoint: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002287 unsigned Opc = Lex.getUIntVal();
Chris Lattner1afcace2011-07-09 17:41:24 +00002288 Type *DestTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002289 Constant *SrcVal;
2290 Lex.Lex();
2291 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2292 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman24b108b2009-06-15 21:52:11 +00002293 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002294 ParseType(DestTy) ||
2295 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2296 return true;
2297 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2298 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002299 getTypeString(SrcVal->getType()) + "' to '" +
2300 getTypeString(DestTy) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002301 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Andersonfba933c2009-07-01 23:57:11 +00002302 SrcVal, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00002303 ID.Kind = ValID::t_Constant;
2304 return false;
2305 }
2306 case lltok::kw_extractvalue: {
2307 Lex.Lex();
2308 Constant *Val;
2309 SmallVector<unsigned, 4> Indices;
2310 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2311 ParseGlobalTypeAndValue(Val) ||
2312 ParseIndexList(Indices) ||
2313 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2314 return true;
Devang Patele8bc45a2009-11-03 19:06:07 +00002315
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002316 if (!Val->getType()->isAggregateType())
2317 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002318 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002319 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002320 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerdf986172009-01-02 07:01:27 +00002321 ID.Kind = ValID::t_Constant;
2322 return false;
2323 }
2324 case lltok::kw_insertvalue: {
2325 Lex.Lex();
2326 Constant *Val0, *Val1;
2327 SmallVector<unsigned, 4> Indices;
2328 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2329 ParseGlobalTypeAndValue(Val0) ||
2330 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2331 ParseGlobalTypeAndValue(Val1) ||
2332 ParseIndexList(Indices) ||
2333 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2334 return true;
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002335 if (!Val0->getType()->isAggregateType())
2336 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002337 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002338 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002339 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerdf986172009-01-02 07:01:27 +00002340 ID.Kind = ValID::t_Constant;
2341 return false;
2342 }
2343 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002344 case lltok::kw_fcmp: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002345 unsigned PredVal, Opc = Lex.getUIntVal();
2346 Constant *Val0, *Val1;
2347 Lex.Lex();
2348 if (ParseCmpPredicate(PredVal, Opc) ||
2349 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2350 ParseGlobalTypeAndValue(Val0) ||
2351 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2352 ParseGlobalTypeAndValue(Val1) ||
2353 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2354 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002355
Chris Lattnerdf986172009-01-02 07:01:27 +00002356 if (Val0->getType() != Val1->getType())
2357 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002358
Chris Lattnerdf986172009-01-02 07:01:27 +00002359 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002360
Chris Lattnerdf986172009-01-02 07:01:27 +00002361 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002362 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002363 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002364 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002365 } else {
2366 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002367 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem16087692011-12-05 06:29:09 +00002368 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002369 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002370 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002371 }
2372 ID.Kind = ValID::t_Constant;
2373 return false;
2374 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002375
Chris Lattnerdf986172009-01-02 07:01:27 +00002376 // Binary Operators.
2377 case lltok::kw_add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002378 case lltok::kw_fadd:
Chris Lattnerdf986172009-01-02 07:01:27 +00002379 case lltok::kw_sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002380 case lltok::kw_fsub:
Chris Lattnerdf986172009-01-02 07:01:27 +00002381 case lltok::kw_mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002382 case lltok::kw_fmul:
Chris Lattnerdf986172009-01-02 07:01:27 +00002383 case lltok::kw_udiv:
2384 case lltok::kw_sdiv:
2385 case lltok::kw_fdiv:
2386 case lltok::kw_urem:
2387 case lltok::kw_srem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002388 case lltok::kw_frem:
2389 case lltok::kw_shl:
2390 case lltok::kw_lshr:
2391 case lltok::kw_ashr: {
Dan Gohman59858cf2009-07-27 16:11:46 +00002392 bool NUW = false;
2393 bool NSW = false;
2394 bool Exact = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002395 unsigned Opc = Lex.getUIntVal();
2396 Constant *Val0, *Val1;
2397 Lex.Lex();
Dan Gohman59858cf2009-07-27 16:11:46 +00002398 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnerf067d582011-02-07 16:40:21 +00002399 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2400 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002401 if (EatIfPresent(lltok::kw_nuw))
2402 NUW = true;
2403 if (EatIfPresent(lltok::kw_nsw)) {
2404 NSW = true;
2405 if (EatIfPresent(lltok::kw_nuw))
2406 NUW = true;
2407 }
Chris Lattnerf067d582011-02-07 16:40:21 +00002408 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2409 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002410 if (EatIfPresent(lltok::kw_exact))
2411 Exact = true;
2412 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002413 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2414 ParseGlobalTypeAndValue(Val0) ||
2415 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2416 ParseGlobalTypeAndValue(Val1) ||
2417 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2418 return true;
2419 if (Val0->getType() != Val1->getType())
2420 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002421 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002422 if (NUW)
2423 return Error(ModifierLoc, "nuw only applies to integer operations");
2424 if (NSW)
2425 return Error(ModifierLoc, "nsw only applies to integer operations");
2426 }
Dan Gohman1eaac532010-05-03 22:44:19 +00002427 // Check that the type is valid for the operator.
2428 switch (Opc) {
2429 case Instruction::Add:
2430 case Instruction::Sub:
2431 case Instruction::Mul:
2432 case Instruction::UDiv:
2433 case Instruction::SDiv:
2434 case Instruction::URem:
2435 case Instruction::SRem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002436 case Instruction::Shl:
2437 case Instruction::AShr:
2438 case Instruction::LShr:
Dan Gohman1eaac532010-05-03 22:44:19 +00002439 if (!Val0->getType()->isIntOrIntVectorTy())
2440 return Error(ID.Loc, "constexpr requires integer operands");
2441 break;
2442 case Instruction::FAdd:
2443 case Instruction::FSub:
2444 case Instruction::FMul:
2445 case Instruction::FDiv:
2446 case Instruction::FRem:
2447 if (!Val0->getType()->isFPOrFPVectorTy())
2448 return Error(ID.Loc, "constexpr requires fp operands");
2449 break;
2450 default: llvm_unreachable("Unknown binary operator!");
2451 }
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002452 unsigned Flags = 0;
2453 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2454 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00002455 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002456 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman59858cf2009-07-27 16:11:46 +00002457 ID.ConstantVal = C;
Chris Lattnerdf986172009-01-02 07:01:27 +00002458 ID.Kind = ValID::t_Constant;
2459 return false;
2460 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002461
Chris Lattnerdf986172009-01-02 07:01:27 +00002462 // Logical Operations
Chris Lattnerdf986172009-01-02 07:01:27 +00002463 case lltok::kw_and:
2464 case lltok::kw_or:
2465 case lltok::kw_xor: {
2466 unsigned Opc = Lex.getUIntVal();
2467 Constant *Val0, *Val1;
2468 Lex.Lex();
2469 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2470 ParseGlobalTypeAndValue(Val0) ||
2471 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2472 ParseGlobalTypeAndValue(Val1) ||
2473 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2474 return true;
2475 if (Val0->getType() != Val1->getType())
2476 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002477 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002478 return Error(ID.Loc,
2479 "constexpr requires integer or integer vector operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002480 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002481 ID.Kind = ValID::t_Constant;
2482 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002483 }
2484
Chris Lattnerdf986172009-01-02 07:01:27 +00002485 case lltok::kw_getelementptr:
2486 case lltok::kw_shufflevector:
2487 case lltok::kw_insertelement:
2488 case lltok::kw_extractelement:
2489 case lltok::kw_select: {
2490 unsigned Opc = Lex.getUIntVal();
2491 SmallVector<Constant*, 16> Elts;
Dan Gohmandd8004d2009-07-27 21:53:46 +00002492 bool InBounds = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002493 Lex.Lex();
Dan Gohmandd8004d2009-07-27 21:53:46 +00002494 if (Opc == Instruction::GetElementPtr)
Dan Gohmandcb40a32009-07-29 15:58:36 +00002495 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002496 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2497 ParseGlobalValueVector(Elts) ||
2498 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2499 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002500
Chris Lattnerdf986172009-01-02 07:01:27 +00002501 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem16087692011-12-05 06:29:09 +00002502 if (Elts.size() == 0 ||
2503 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002504 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002505
Jay Foaddab3d292011-07-21 14:31:17 +00002506 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foada9203102011-07-25 09:48:08 +00002507 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002508 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad4b5e2072011-07-21 15:15:37 +00002509 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2510 InBounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002511 } else if (Opc == Instruction::Select) {
2512 if (Elts.size() != 3)
2513 return Error(ID.Loc, "expected three operands to select");
2514 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2515 Elts[2]))
2516 return Error(ID.Loc, Reason);
Owen Andersonbaf3c402009-07-29 18:55:55 +00002517 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002518 } else if (Opc == Instruction::ShuffleVector) {
2519 if (Elts.size() != 3)
2520 return Error(ID.Loc, "expected three operands to shufflevector");
2521 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2522 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Andersonfba933c2009-07-01 23:57:11 +00002523 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002524 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002525 } else if (Opc == Instruction::ExtractElement) {
2526 if (Elts.size() != 2)
2527 return Error(ID.Loc, "expected two operands to extractelement");
2528 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2529 return Error(ID.Loc, "invalid extractelement operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002530 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002531 } else {
2532 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2533 if (Elts.size() != 3)
2534 return Error(ID.Loc, "expected three operands to insertelement");
2535 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2536 return Error(ID.Loc, "invalid insertelement operands");
Owen Andersonfba933c2009-07-01 23:57:11 +00002537 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002538 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002539 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002540
Chris Lattnerdf986172009-01-02 07:01:27 +00002541 ID.Kind = ValID::t_Constant;
2542 return false;
2543 }
2544 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002545
Chris Lattnerdf986172009-01-02 07:01:27 +00002546 Lex.Lex();
2547 return false;
2548}
2549
2550/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002551bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Victor Hernandez92f238d2010-01-11 22:31:58 +00002552 C = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002553 ValID ID;
Victor Hernandez92f238d2010-01-11 22:31:58 +00002554 Value *V = NULL;
2555 bool Parsed = ParseValID(ID) ||
2556 ConvertValIDToValue(Ty, ID, V, NULL);
2557 if (V && !(C = dyn_cast<Constant>(V)))
2558 return Error(ID.Loc, "global values must be constants");
2559 return Parsed;
Chris Lattnerdf986172009-01-02 07:01:27 +00002560}
2561
Victor Hernandez92f238d2010-01-11 22:31:58 +00002562bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002563 Type *Ty = 0;
2564 return ParseType(Ty) ||
2565 ParseGlobalValue(Ty, V);
Victor Hernandez92f238d2010-01-11 22:31:58 +00002566}
2567
2568/// ParseGlobalValueVector
2569/// ::= /*empty*/
2570/// ::= TypeAndValue (',' TypeAndValue)*
2571bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2572 // Empty list.
2573 if (Lex.getKind() == lltok::rbrace ||
2574 Lex.getKind() == lltok::rsquare ||
2575 Lex.getKind() == lltok::greater ||
2576 Lex.getKind() == lltok::rparen)
2577 return false;
2578
2579 Constant *C;
2580 if (ParseGlobalTypeAndValue(C)) return true;
2581 Elts.push_back(C);
2582
2583 while (EatIfPresent(lltok::comma)) {
2584 if (ParseGlobalTypeAndValue(C)) return true;
2585 Elts.push_back(C);
2586 }
2587
2588 return false;
2589}
2590
Dan Gohman309b3af2010-08-24 02:24:03 +00002591bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2592 assert(Lex.getKind() == lltok::lbrace);
2593 Lex.Lex();
2594
2595 SmallVector<Value*, 16> Elts;
2596 if (ParseMDNodeVector(Elts, PFS) ||
2597 ParseToken(lltok::rbrace, "expected end of metadata node"))
2598 return true;
2599
Jay Foadec9186b2011-04-21 19:59:31 +00002600 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohman309b3af2010-08-24 02:24:03 +00002601 ID.Kind = ValID::t_MDNode;
2602 return false;
2603}
2604
Dan Gohman83448032010-07-14 18:26:50 +00002605/// ParseMetadataValue
2606/// ::= !42
2607/// ::= !{...}
2608/// ::= !"string"
2609bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2610 assert(Lex.getKind() == lltok::exclaim);
2611 Lex.Lex();
2612
2613 // MDNode:
2614 // !{ ... }
Dan Gohman309b3af2010-08-24 02:24:03 +00002615 if (Lex.getKind() == lltok::lbrace)
2616 return ParseMetadataListValue(ID, PFS);
Dan Gohman83448032010-07-14 18:26:50 +00002617
2618 // Standalone metadata reference
2619 // !42
2620 if (Lex.getKind() == lltok::APSInt) {
2621 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2622 ID.Kind = ValID::t_MDNode;
2623 return false;
2624 }
2625
2626 // MDString:
2627 // ::= '!' STRINGCONSTANT
2628 if (ParseMDString(ID.MDStringVal)) return true;
2629 ID.Kind = ValID::t_MDString;
2630 return false;
2631}
2632
Victor Hernandez92f238d2010-01-11 22:31:58 +00002633
2634//===----------------------------------------------------------------------===//
2635// Function Parsing.
2636//===----------------------------------------------------------------------===//
2637
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002638bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez92f238d2010-01-11 22:31:58 +00002639 PerFunctionState *PFS) {
Duncan Sands1df98592010-02-16 11:11:14 +00002640 if (Ty->isFunctionTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002641 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002642
Chris Lattnerdf986172009-01-02 07:01:27 +00002643 switch (ID.Kind) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002644 case ValID::t_LocalID:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002645 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2646 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
2647 return (V == 0);
Chris Lattnerdf986172009-01-02 07:01:27 +00002648 case ValID::t_LocalName:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002649 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2650 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
2651 return (V == 0);
2652 case ValID::t_InlineAsm: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002653 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman407a6162012-11-15 22:34:00 +00002654 FunctionType *FTy =
Victor Hernandez92f238d2010-01-11 22:31:58 +00002655 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2656 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2657 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosier36547342012-09-05 00:08:17 +00002658 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosier581600b2012-09-05 19:00:49 +00002659 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez92f238d2010-01-11 22:31:58 +00002660 return false;
2661 }
2662 case ValID::t_MDNode:
2663 if (!Ty->isMetadataTy())
2664 return Error(ID.Loc, "metadata value must have metadata type");
2665 V = ID.MDNodeVal;
2666 return false;
2667 case ValID::t_MDString:
2668 if (!Ty->isMetadataTy())
2669 return Error(ID.Loc, "metadata value must have metadata type");
2670 V = ID.MDStringVal;
2671 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002672 case ValID::t_GlobalName:
2673 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2674 return V == 0;
2675 case ValID::t_GlobalID:
2676 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2677 return V == 0;
2678 case ValID::t_APSInt:
Duncan Sands1df98592010-02-16 11:11:14 +00002679 if (!Ty->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002680 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad40f8f622010-12-07 08:25:19 +00002681 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersoneed707b2009-07-24 23:12:02 +00002682 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002683 return false;
2684 case ValID::t_APFloat:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002685 if (!Ty->isFloatingPointTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002686 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2687 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002688
Dan Gohmance163392011-12-17 00:04:22 +00002689 // The lexer has no type info, so builds all half, float, and double FP
2690 // constants as double. Fix this here. Long double does not need this.
2691 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002692 bool Ignored;
Dan Gohmance163392011-12-17 00:04:22 +00002693 if (Ty->isHalfTy())
2694 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2695 &Ignored);
2696 else if (Ty->isFloatTy())
2697 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2698 &Ignored);
Chris Lattnerdf986172009-01-02 07:01:27 +00002699 }
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002700 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002701
Chris Lattner959873d2009-01-05 18:24:23 +00002702 if (V->getType() != Ty)
2703 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002704 getTypeString(Ty) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002705
Chris Lattnerdf986172009-01-02 07:01:27 +00002706 return false;
2707 case ValID::t_Null:
Duncan Sands1df98592010-02-16 11:11:14 +00002708 if (!Ty->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002709 return Error(ID.Loc, "null must be a pointer type");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002710 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerdf986172009-01-02 07:01:27 +00002711 return false;
2712 case ValID::t_Undef:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002713 // FIXME: LabelTy should not be a first-class type.
Chris Lattner1afcace2011-07-09 17:41:24 +00002714 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002715 return Error(ID.Loc, "invalid type for undef constant");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002716 V = UndefValue::get(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002717 return false;
Chris Lattner081b5052009-01-05 07:52:51 +00002718 case ValID::t_EmptyArray:
Duncan Sands1df98592010-02-16 11:11:14 +00002719 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner081b5052009-01-05 07:52:51 +00002720 return Error(ID.Loc, "invalid empty array initializer");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002721 V = UndefValue::get(Ty);
Chris Lattner081b5052009-01-05 07:52:51 +00002722 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002723 case ValID::t_Zero:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002724 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002725 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002726 return Error(ID.Loc, "invalid type for null constant");
Owen Andersona7235ea2009-07-31 20:28:14 +00002727 V = Constant::getNullValue(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002728 return false;
2729 case ValID::t_Constant:
Chris Lattner61c70e92010-08-28 04:09:24 +00002730 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerdf986172009-01-02 07:01:27 +00002731 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002732
Chris Lattnerdf986172009-01-02 07:01:27 +00002733 V = ID.ConstantVal;
2734 return false;
Chris Lattner1afcace2011-07-09 17:41:24 +00002735 case ValID::t_ConstantStruct:
2736 case ValID::t_PackedConstantStruct:
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002737 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002738 if (ST->getNumElements() != ID.UIntVal)
2739 return Error(ID.Loc,
2740 "initializer with struct type has wrong # elements");
2741 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2742 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman407a6162012-11-15 22:34:00 +00002743
Chris Lattner1afcace2011-07-09 17:41:24 +00002744 // Verify that the elements are compatible with the structtype.
2745 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2746 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2747 return Error(ID.Loc, "element " + Twine(i) +
2748 " of struct initializer doesn't match struct element type");
Michael Ilseman407a6162012-11-15 22:34:00 +00002749
Frits van Bommel39b5abf2011-07-18 12:00:32 +00002750 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2751 ID.UIntVal));
Chris Lattner1afcace2011-07-09 17:41:24 +00002752 } else
2753 return Error(ID.Loc, "constant expression type mismatch");
2754 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002755 }
Chandler Carruth732f05c2012-01-10 18:08:01 +00002756 llvm_unreachable("Invalid ValID");
Chris Lattnerdf986172009-01-02 07:01:27 +00002757}
Daniel Dunbara279bc32009-09-20 02:20:51 +00002758
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002759bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002760 V = 0;
2761 ValID ID;
Chris Lattner1afcace2011-07-09 17:41:24 +00002762 return ParseValID(ID, PFS) ||
2763 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002764}
2765
Chris Lattner1afcace2011-07-09 17:41:24 +00002766bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
2767 Type *Ty = 0;
2768 return ParseType(Ty) ||
2769 ParseValue(Ty, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002770}
2771
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002772bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2773 PerFunctionState &PFS) {
2774 Value *V;
2775 Loc = Lex.getLoc();
2776 if (ParseTypeAndValue(V, PFS)) return true;
2777 if (!isa<BasicBlock>(V))
2778 return Error(Loc, "expected a basic block");
2779 BB = cast<BasicBlock>(V);
2780 return false;
2781}
2782
2783
Chris Lattnerdf986172009-01-02 07:01:27 +00002784/// FunctionHeader
2785/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindolabea46262011-01-08 16:42:36 +00002786/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Chris Lattnerdf986172009-01-02 07:01:27 +00002787/// OptionalAlign OptGC
2788bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2789 // Parse the linkage.
2790 LocTy LinkageLoc = Lex.getLoc();
2791 unsigned Linkage;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002792
Kostya Serebryany164b86b2012-01-20 17:56:17 +00002793 unsigned Visibility;
Bill Wendling702cc912012-10-15 20:35:56 +00002794 AttrBuilder RetAttrs;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002795 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00002796 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002797 LocTy RetTypeLoc = Lex.getLoc();
2798 if (ParseOptionalLinkage(Linkage) ||
2799 ParseOptionalVisibility(Visibility) ||
2800 ParseOptionalCallingConv(CC) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00002801 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00002802 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerdf986172009-01-02 07:01:27 +00002803 return true;
2804
2805 // Verify that the linkage is ok.
2806 switch ((GlobalValue::LinkageTypes)Linkage) {
2807 case GlobalValue::ExternalLinkage:
2808 break; // always ok.
2809 case GlobalValue::DLLImportLinkage:
Duncan Sands5f4ee1f2009-03-11 08:08:06 +00002810 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002811 if (isDefine)
2812 return Error(LinkageLoc, "invalid linkage for function definition");
2813 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +00002814 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +00002815 case GlobalValue::LinkerPrivateLinkage:
Bill Wendling5e721d72010-07-01 21:55:59 +00002816 case GlobalValue::LinkerPrivateWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002817 case GlobalValue::InternalLinkage:
Nick Lewycky55f64db2009-04-13 07:02:02 +00002818 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002819 case GlobalValue::LinkOnceAnyLinkage:
2820 case GlobalValue::LinkOnceODRLinkage:
Bill Wendling32811be2012-08-17 18:33:14 +00002821 case GlobalValue::LinkOnceODRAutoHideLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002822 case GlobalValue::WeakAnyLinkage:
2823 case GlobalValue::WeakODRLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002824 case GlobalValue::DLLExportLinkage:
2825 if (!isDefine)
2826 return Error(LinkageLoc, "invalid linkage for function declaration");
2827 break;
2828 case GlobalValue::AppendingLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +00002829 case GlobalValue::CommonLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002830 return Error(LinkageLoc, "invalid function linkage type");
2831 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002832
Chris Lattner1afcace2011-07-09 17:41:24 +00002833 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerdf986172009-01-02 07:01:27 +00002834 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002835
Chris Lattnerdf986172009-01-02 07:01:27 +00002836 LocTy NameLoc = Lex.getLoc();
Chris Lattnerf570e622009-02-18 21:48:13 +00002837
2838 std::string FunctionName;
2839 if (Lex.getKind() == lltok::GlobalVar) {
2840 FunctionName = Lex.getStrVal();
2841 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
2842 unsigned NameID = Lex.getUIntVal();
2843
2844 if (NameID != NumberedVals.size())
2845 return TokError("function expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002846 Twine(NumberedVals.size()) + "'");
Chris Lattnerf570e622009-02-18 21:48:13 +00002847 } else {
2848 return TokError("expected function name");
2849 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002850
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002851 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002852
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002853 if (Lex.getKind() != lltok::lparen)
Chris Lattnerdf986172009-01-02 07:01:27 +00002854 return TokError("expected '(' in function argument list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002855
Chris Lattner1afcace2011-07-09 17:41:24 +00002856 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerdf986172009-01-02 07:01:27 +00002857 bool isVarArg;
Bill Wendling702cc912012-10-15 20:35:56 +00002858 AttrBuilder FuncAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00002859 std::string Section;
Chris Lattnerdf986172009-01-02 07:01:27 +00002860 unsigned Alignment;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002861 std::string GC;
Rafael Espindola3971df52011-01-25 19:09:56 +00002862 bool UnnamedAddr;
2863 LocTy UnnamedAddrLoc;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002864
Chris Lattner1afcace2011-07-09 17:41:24 +00002865 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola3971df52011-01-25 19:09:56 +00002866 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
2867 &UnnamedAddrLoc) ||
Bill Wendlingea007fa2013-02-08 00:52:31 +00002868 ParseFnAttributeValuePairs(FuncAttrs, false) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002869 (EatIfPresent(lltok::kw_section) &&
2870 ParseStringConstant(Section)) ||
2871 ParseOptionalAlignment(Alignment) ||
2872 (EatIfPresent(lltok::kw_gc) &&
2873 ParseStringConstant(GC)))
2874 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00002875
2876 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingf385f4c2012-10-08 23:27:46 +00002877 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendlingef99fe82012-09-21 15:26:31 +00002878 Alignment = FuncAttrs.getAlignment();
Bill Wendling034b94b2012-12-19 07:18:57 +00002879 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerdf986172009-01-02 07:01:27 +00002880 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002881
Chris Lattnerdf986172009-01-02 07:01:27 +00002882 // Okay, if we got here, the function is syntactically valid. Convert types
2883 // and do semantic checks.
Jay Foad5fdd6c82011-07-12 14:06:48 +00002884 std::vector<Type*> ParamTypeList;
Bill Wendlinga1683d62013-01-27 02:24:02 +00002885 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002886
Bill Wendlinge603fe42012-09-19 23:54:18 +00002887 if (RetAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00002888 Attrs.push_back(AttributeSet::get(RetType->getContext(),
2889 AttributeSet::ReturnIndex,
2890 RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002891
Chris Lattnerdf986172009-01-02 07:01:27 +00002892 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002893 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendling73dee182013-01-31 00:29:54 +00002894 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
2895 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlinga1683d62013-01-27 02:24:02 +00002896 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
2897 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002898 }
2899
Bill Wendlinge603fe42012-09-19 23:54:18 +00002900 if (FuncAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00002901 Attrs.push_back(AttributeSet::get(RetType->getContext(),
2902 AttributeSet::FunctionIndex,
2903 FuncAttrs));
Chris Lattnerdf986172009-01-02 07:01:27 +00002904
Bill Wendling99faa3b2012-12-07 23:16:57 +00002905 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002906
Bill Wendling94e94b32012-12-30 13:50:49 +00002907 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002908 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2909
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002910 FunctionType *FT =
Owen Andersondebcb012009-07-29 22:17:13 +00002911 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002912 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerdf986172009-01-02 07:01:27 +00002913
2914 Fn = 0;
2915 if (!FunctionName.empty()) {
2916 // If this was a definition of a forward reference, remove the definition
2917 // from the forward reference table and fill in the forward ref.
2918 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2919 ForwardRefVals.find(FunctionName);
2920 if (FRVI != ForwardRefVals.end()) {
2921 Fn = M->getFunction(FunctionName);
Nick Lewycky64ea2752012-10-11 00:38:25 +00002922 if (!Fn)
2923 return Error(FRVI->second.second, "invalid forward reference to "
2924 "function as global value!");
Chris Lattnerf1cfb952010-04-20 04:49:11 +00002925 if (Fn->getType() != PFT)
2926 return Error(FRVI->second.second, "invalid forward reference to "
2927 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman407a6162012-11-15 22:34:00 +00002928
Chris Lattnerdf986172009-01-02 07:01:27 +00002929 ForwardRefVals.erase(FRVI);
2930 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattnerd5890992011-06-17 07:06:44 +00002931 // Reject redefinitions.
2932 return Error(NameLoc, "invalid redefinition of function '" +
2933 FunctionName + "'");
Chris Lattner1d871c52009-10-25 23:22:50 +00002934 } else if (M->getNamedValue(FunctionName)) {
2935 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002936 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002937
Dan Gohman41905542009-08-29 23:37:49 +00002938 } else {
Chris Lattnerdf986172009-01-02 07:01:27 +00002939 // If this is a definition of a forward referenced function, make sure the
2940 // types agree.
2941 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
2942 = ForwardRefValIDs.find(NumberedVals.size());
2943 if (I != ForwardRefValIDs.end()) {
2944 Fn = cast<Function>(I->second.first);
2945 if (Fn->getType() != PFT)
2946 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002947 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerdf986172009-01-02 07:01:27 +00002948 ForwardRefValIDs.erase(I);
2949 }
2950 }
2951
2952 if (Fn == 0)
2953 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2954 else // Move the forward-reference to the correct spot in the module.
2955 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2956
2957 if (FunctionName.empty())
2958 NumberedVals.push_back(Fn);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002959
Chris Lattnerdf986172009-01-02 07:01:27 +00002960 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
2961 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
2962 Fn->setCallingConv(CC);
2963 Fn->setAttributes(PAL);
Rafael Espindolabea46262011-01-08 16:42:36 +00002964 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerdf986172009-01-02 07:01:27 +00002965 Fn->setAlignment(Alignment);
2966 Fn->setSection(Section);
2967 if (!GC.empty()) Fn->setGC(GC.c_str());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002968
Chris Lattnerdf986172009-01-02 07:01:27 +00002969 // Add all of the arguments we parsed to the function.
2970 Function::arg_iterator ArgIt = Fn->arg_begin();
2971 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
2972 // If the argument has a name, insert it into the argument symbol table.
2973 if (ArgList[i].Name.empty()) continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002974
Chris Lattnerdf986172009-01-02 07:01:27 +00002975 // Set the name, if it conflicted, it will be auto-renamed.
2976 ArgIt->setName(ArgList[i].Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002977
Benjamin Krameraf812352010-10-16 11:28:23 +00002978 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerdf986172009-01-02 07:01:27 +00002979 return Error(ArgList[i].Loc, "redefinition of argument '%" +
2980 ArgList[i].Name + "'");
2981 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002982
Chris Lattnerdf986172009-01-02 07:01:27 +00002983 return false;
2984}
2985
2986
2987/// ParseFunctionBody
2988/// ::= '{' BasicBlock+ '}'
Chris Lattnerdf986172009-01-02 07:01:27 +00002989///
2990bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner6b7c89e2011-06-17 06:42:57 +00002991 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerdf986172009-01-02 07:01:27 +00002992 return TokError("expected '{' in function body");
2993 Lex.Lex(); // eat the {.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002994
Chris Lattner09d9ef42009-10-28 03:39:23 +00002995 int FunctionNumber = -1;
2996 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman407a6162012-11-15 22:34:00 +00002997
Chris Lattner09d9ef42009-10-28 03:39:23 +00002998 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002999
Chris Lattner2fdf8db2010-01-09 19:20:07 +00003000 // We need at least one basic block.
Chris Lattner6b7c89e2011-06-17 06:42:57 +00003001 if (Lex.getKind() == lltok::rbrace)
Chris Lattner2fdf8db2010-01-09 19:20:07 +00003002 return TokError("function body requires at least one basic block");
Michael Ilseman407a6162012-11-15 22:34:00 +00003003
Chris Lattner6b7c89e2011-06-17 06:42:57 +00003004 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerdf986172009-01-02 07:01:27 +00003005 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003006
Chris Lattnerdf986172009-01-02 07:01:27 +00003007 // Eat the }.
3008 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003009
Chris Lattnerdf986172009-01-02 07:01:27 +00003010 // Verify function is ok.
Chris Lattner09d9ef42009-10-28 03:39:23 +00003011 return PFS.FinishFunction();
Chris Lattnerdf986172009-01-02 07:01:27 +00003012}
3013
3014/// ParseBasicBlock
3015/// ::= LabelStr? Instruction*
3016bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3017 // If this basic block starts out with a name, remember it.
3018 std::string Name;
3019 LocTy NameLoc = Lex.getLoc();
3020 if (Lex.getKind() == lltok::LabelStr) {
3021 Name = Lex.getStrVal();
3022 Lex.Lex();
3023 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003024
Chris Lattnerdf986172009-01-02 07:01:27 +00003025 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
3026 if (BB == 0) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003027
Chris Lattnerdf986172009-01-02 07:01:27 +00003028 std::string NameStr;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003029
Chris Lattnerdf986172009-01-02 07:01:27 +00003030 // Parse the instructions in this block until we get a terminator.
3031 Instruction *Inst;
Chris Lattner1340dd32009-12-30 05:48:36 +00003032 SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +00003033 do {
3034 // This instruction may have three possibilities for a name: a) none
3035 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3036 LocTy NameLoc = Lex.getLoc();
3037 int NameID = -1;
3038 NameStr = "";
Daniel Dunbara279bc32009-09-20 02:20:51 +00003039
Chris Lattnerdf986172009-01-02 07:01:27 +00003040 if (Lex.getKind() == lltok::LocalVarID) {
3041 NameID = Lex.getUIntVal();
3042 Lex.Lex();
3043 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3044 return true;
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00003045 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003046 NameStr = Lex.getStrVal();
3047 Lex.Lex();
3048 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3049 return true;
3050 }
Devang Patelf633a062009-09-17 23:04:48 +00003051
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003052 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Topper85814382012-02-07 05:05:23 +00003053 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003054 case InstError: return true;
3055 case InstNormal:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00003056 BB->getInstList().push_back(Inst);
3057
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003058 // With a normal result, we check to see if the instruction is followed by
3059 // a comma and metadata.
3060 if (EatIfPresent(lltok::comma))
Dan Gohman9d072f52010-08-24 02:05:17 +00003061 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003062 return true;
3063 break;
3064 case InstExtraComma:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00003065 BB->getInstList().push_back(Inst);
3066
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003067 // If the instruction parser ate an extra comma at the end of it, it
3068 // *must* be followed by metadata.
Dan Gohman9d072f52010-08-24 02:05:17 +00003069 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003070 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003071 break;
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003072 }
Devang Patelf633a062009-09-17 23:04:48 +00003073
Chris Lattnerdf986172009-01-02 07:01:27 +00003074 // Set the name on the instruction.
3075 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3076 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003077
Chris Lattnerdf986172009-01-02 07:01:27 +00003078 return false;
3079}
3080
3081//===----------------------------------------------------------------------===//
3082// Instruction Parsing.
3083//===----------------------------------------------------------------------===//
3084
3085/// ParseInstruction - Parse one of the many different instructions.
3086///
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003087int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3088 PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003089 lltok::Kind Token = Lex.getKind();
3090 if (Token == lltok::Eof)
3091 return TokError("found end of file when expecting more instructions");
3092 LocTy Loc = Lex.getLoc();
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003093 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00003094 Lex.Lex(); // Eat the keyword.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003095
Chris Lattnerdf986172009-01-02 07:01:27 +00003096 switch (Token) {
3097 default: return Error(Loc, "expected instruction opcode");
3098 // Terminator Instructions.
Owen Anderson1d0be152009-08-13 21:58:54 +00003099 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003100 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3101 case lltok::kw_br: return ParseBr(Inst, PFS);
3102 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +00003103 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003104 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003105 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003106 // Binary Operators.
3107 case lltok::kw_add:
3108 case lltok::kw_sub:
Chris Lattnerf067d582011-02-07 16:40:21 +00003109 case lltok::kw_mul:
3110 case lltok::kw_shl: {
Chris Lattnerf067d582011-02-07 16:40:21 +00003111 bool NUW = EatIfPresent(lltok::kw_nuw);
3112 bool NSW = EatIfPresent(lltok::kw_nsw);
3113 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman407a6162012-11-15 22:34:00 +00003114
Chris Lattnerf067d582011-02-07 16:40:21 +00003115 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003116
Chris Lattnerf067d582011-02-07 16:40:21 +00003117 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3118 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3119 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003120 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00003121 case lltok::kw_fadd:
3122 case lltok::kw_fsub:
Michael Ilseman15c13d32012-11-27 00:42:44 +00003123 case lltok::kw_fmul:
3124 case lltok::kw_fdiv:
3125 case lltok::kw_frem: {
3126 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3127 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3128 if (Res != 0)
3129 return Res;
3130 if (FMF.any())
3131 Inst->setFastMathFlags(FMF);
3132 return 0;
3133 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00003134
Chris Lattner35bda892011-02-06 21:44:57 +00003135 case lltok::kw_sdiv:
Chris Lattnerf067d582011-02-07 16:40:21 +00003136 case lltok::kw_udiv:
3137 case lltok::kw_lshr:
3138 case lltok::kw_ashr: {
3139 bool Exact = EatIfPresent(lltok::kw_exact);
3140
3141 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3142 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3143 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003144 }
3145
Chris Lattnerdf986172009-01-02 07:01:27 +00003146 case lltok::kw_urem:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003147 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003148 case lltok::kw_and:
3149 case lltok::kw_or:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003150 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003151 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003152 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003153 // Casts.
3154 case lltok::kw_trunc:
3155 case lltok::kw_zext:
3156 case lltok::kw_sext:
3157 case lltok::kw_fptrunc:
3158 case lltok::kw_fpext:
3159 case lltok::kw_bitcast:
3160 case lltok::kw_uitofp:
3161 case lltok::kw_sitofp:
3162 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00003163 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00003164 case lltok::kw_inttoptr:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003165 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003166 // Other.
3167 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +00003168 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003169 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3170 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3171 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3172 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlinge6e88262011-08-12 20:24:12 +00003173 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003174 case lltok::kw_call: return ParseCall(Inst, PFS, false);
3175 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
3176 // Memory.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003177 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003178 case lltok::kw_load: return ParseLoad(Inst, PFS);
3179 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedmanf03bb262011-08-12 22:50:01 +00003180 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3181 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedman47f35132011-07-25 23:16:38 +00003182 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003183 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3184 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3185 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3186 }
3187}
3188
3189/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3190bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003191 if (Opc == Instruction::FCmp) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003192 switch (Lex.getKind()) {
David Tweedd80d6082013-01-07 13:32:38 +00003193 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerdf986172009-01-02 07:01:27 +00003194 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3195 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3196 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3197 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3198 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3199 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3200 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3201 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3202 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3203 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3204 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3205 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3206 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3207 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3208 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3209 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3210 }
3211 } else {
3212 switch (Lex.getKind()) {
David Tweedd80d6082013-01-07 13:32:38 +00003213 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerdf986172009-01-02 07:01:27 +00003214 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3215 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3216 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3217 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3218 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3219 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3220 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3221 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3222 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3223 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3224 }
3225 }
3226 Lex.Lex();
3227 return false;
3228}
3229
3230//===----------------------------------------------------------------------===//
3231// Terminator Instructions.
3232//===----------------------------------------------------------------------===//
3233
3234/// ParseRet - Parse a return instruction.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003235/// ::= 'ret' void (',' !dbg, !1)*
3236/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner437544f2011-06-17 06:49:41 +00003237bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattner1afcace2011-07-09 17:41:24 +00003238 PerFunctionState &PFS) {
3239 SMLoc TypeLoc = Lex.getLoc();
3240 Type *Ty = 0;
Chris Lattnera9a9e072009-03-09 04:49:14 +00003241 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003242
Chris Lattner1afcace2011-07-09 17:41:24 +00003243 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman407a6162012-11-15 22:34:00 +00003244
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00003245 if (Ty->isVoidTy()) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003246 if (!ResType->isVoidTy())
3247 return Error(TypeLoc, "value doesn't match function result type '" +
3248 getTypeString(ResType) + "'");
Michael Ilseman407a6162012-11-15 22:34:00 +00003249
Owen Anderson1d0be152009-08-13 21:58:54 +00003250 Inst = ReturnInst::Create(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00003251 return false;
3252 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003253
Chris Lattnerdf986172009-01-02 07:01:27 +00003254 Value *RV;
3255 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003256
Chris Lattner1afcace2011-07-09 17:41:24 +00003257 if (ResType != RV->getType())
3258 return Error(TypeLoc, "value doesn't match function result type '" +
3259 getTypeString(ResType) + "'");
Michael Ilseman407a6162012-11-15 22:34:00 +00003260
Owen Anderson1d0be152009-08-13 21:58:54 +00003261 Inst = ReturnInst::Create(Context, RV);
Chris Lattner437544f2011-06-17 06:49:41 +00003262 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003263}
3264
3265
3266/// ParseBr
3267/// ::= 'br' TypeAndValue
3268/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3269bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3270 LocTy Loc, Loc2;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003271 Value *Op0;
3272 BasicBlock *Op1, *Op2;
Chris Lattnerdf986172009-01-02 07:01:27 +00003273 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003274
Chris Lattnerdf986172009-01-02 07:01:27 +00003275 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3276 Inst = BranchInst::Create(BB);
3277 return false;
3278 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003279
Owen Anderson1d0be152009-08-13 21:58:54 +00003280 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerdf986172009-01-02 07:01:27 +00003281 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003282
Chris Lattnerdf986172009-01-02 07:01:27 +00003283 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003284 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003285 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003286 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003287 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003288
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003289 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerdf986172009-01-02 07:01:27 +00003290 return false;
3291}
3292
3293/// ParseSwitch
3294/// Instruction
3295/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3296/// JumpTable
3297/// ::= (TypeAndValue ',' TypeAndValue)*
3298bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3299 LocTy CondLoc, BBLoc;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003300 Value *Cond;
3301 BasicBlock *DefaultBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003302 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3303 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003304 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003305 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3306 return true;
3307
Duncan Sands1df98592010-02-16 11:11:14 +00003308 if (!Cond->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003309 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003310
Chris Lattnerdf986172009-01-02 07:01:27 +00003311 // Parse the jump table pairs.
3312 SmallPtrSet<Value*, 32> SeenCases;
3313 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3314 while (Lex.getKind() != lltok::rsquare) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003315 Value *Constant;
3316 BasicBlock *DestBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003317
Chris Lattnerdf986172009-01-02 07:01:27 +00003318 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3319 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003320 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003321 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003322
Chris Lattnerdf986172009-01-02 07:01:27 +00003323 if (!SeenCases.insert(Constant))
3324 return Error(CondLoc, "duplicate case value in switch");
3325 if (!isa<ConstantInt>(Constant))
3326 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003327
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003328 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerdf986172009-01-02 07:01:27 +00003329 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003330
Chris Lattnerdf986172009-01-02 07:01:27 +00003331 Lex.Lex(); // Eat the ']'.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003332
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003333 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003334 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3335 SI->addCase(Table[i].first, Table[i].second);
3336 Inst = SI;
3337 return false;
3338}
3339
Chris Lattnerab21db72009-10-28 00:19:10 +00003340/// ParseIndirectBr
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003341/// Instruction
Chris Lattnerab21db72009-10-28 00:19:10 +00003342/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3343bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003344 LocTy AddrLoc;
3345 Value *Address;
3346 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerab21db72009-10-28 00:19:10 +00003347 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3348 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003349 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003350
Duncan Sands1df98592010-02-16 11:11:14 +00003351 if (!Address->getType()->isPointerTy())
Chris Lattnerab21db72009-10-28 00:19:10 +00003352 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman407a6162012-11-15 22:34:00 +00003353
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003354 // Parse the destination list.
3355 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman407a6162012-11-15 22:34:00 +00003356
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003357 if (Lex.getKind() != lltok::rsquare) {
3358 BasicBlock *DestBB;
3359 if (ParseTypeAndBasicBlock(DestBB, PFS))
3360 return true;
3361 DestList.push_back(DestBB);
Michael Ilseman407a6162012-11-15 22:34:00 +00003362
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003363 while (EatIfPresent(lltok::comma)) {
3364 if (ParseTypeAndBasicBlock(DestBB, PFS))
3365 return true;
3366 DestList.push_back(DestBB);
3367 }
3368 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003369
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003370 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3371 return true;
3372
Chris Lattnerab21db72009-10-28 00:19:10 +00003373 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003374 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3375 IBI->addDestination(DestList[i]);
3376 Inst = IBI;
3377 return false;
3378}
3379
3380
Chris Lattnerdf986172009-01-02 07:01:27 +00003381/// ParseInvoke
3382/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3383/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3384bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3385 LocTy CallLoc = Lex.getLoc();
Bill Wendling702cc912012-10-15 20:35:56 +00003386 AttrBuilder RetAttrs, FnAttrs;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003387 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00003388 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003389 LocTy RetTypeLoc;
3390 ValID CalleeID;
3391 SmallVector<ParamInfo, 16> ArgList;
3392
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003393 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003394 if (ParseOptionalCallingConv(CC) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00003395 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003396 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003397 ParseValID(CalleeID) ||
3398 ParseParameterList(ArgList, PFS) ||
Bill Wendlingea007fa2013-02-08 00:52:31 +00003399 ParseFnAttributeValuePairs(FnAttrs, false) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003400 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003401 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003402 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003403 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003404 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003405
Chris Lattnerdf986172009-01-02 07:01:27 +00003406 // If RetType is a non-function pointer type, then this is the short syntax
3407 // for the call, which means that RetType is just the return type. Infer the
3408 // rest of the function argument types from the arguments that are present.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003409 PointerType *PFTy = 0;
3410 FunctionType *Ty = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003411 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3412 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3413 // Pull out the types of all of the arguments...
Jay Foad5fdd6c82011-07-12 14:06:48 +00003414 std::vector<Type*> ParamTypes;
Chris Lattnerdf986172009-01-02 07:01:27 +00003415 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3416 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003417
Chris Lattnerdf986172009-01-02 07:01:27 +00003418 if (!FunctionType::isValidReturnType(RetType))
3419 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003420
Owen Andersondebcb012009-07-29 22:17:13 +00003421 Ty = FunctionType::get(RetType, ParamTypes, false);
3422 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003423 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003424
Chris Lattnerdf986172009-01-02 07:01:27 +00003425 // Look up the callee.
3426 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003427 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003428
Bill Wendling034b94b2012-12-19 07:18:57 +00003429 // Set up the Attribute for the function.
Bill Wendlinga1683d62013-01-27 02:24:02 +00003430 SmallVector<AttributeSet, 8> Attrs;
Bill Wendlinge603fe42012-09-19 23:54:18 +00003431 if (RetAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003432 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3433 AttributeSet::ReturnIndex,
3434 RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003435
Chris Lattnerdf986172009-01-02 07:01:27 +00003436 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003437
Chris Lattnerdf986172009-01-02 07:01:27 +00003438 // Loop through FunctionType's arguments and ensure they are specified
3439 // correctly. Also, gather any parameter attributes.
3440 FunctionType::param_iterator I = Ty->param_begin();
3441 FunctionType::param_iterator E = Ty->param_end();
3442 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003443 Type *ExpectedTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003444 if (I != E) {
3445 ExpectedTy = *I++;
3446 } else if (!Ty->isVarArg()) {
3447 return Error(ArgList[i].Loc, "too many arguments specified");
3448 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003449
Chris Lattnerdf986172009-01-02 07:01:27 +00003450 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3451 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003452 getTypeString(ExpectedTy) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003453 Args.push_back(ArgList[i].V);
Bill Wendling73dee182013-01-31 00:29:54 +00003454 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3455 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlinga1683d62013-01-27 02:24:02 +00003456 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3457 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003458 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003459
Chris Lattnerdf986172009-01-02 07:01:27 +00003460 if (I != E)
3461 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003462
Bill Wendlinge603fe42012-09-19 23:54:18 +00003463 if (FnAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003464 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3465 AttributeSet::FunctionIndex,
3466 FnAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003467
Bill Wendling034b94b2012-12-19 07:18:57 +00003468 // Finish off the Attribute and check them
Bill Wendling99faa3b2012-12-07 23:16:57 +00003469 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003470
Jay Foada3efbb12011-07-15 08:37:34 +00003471 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerdf986172009-01-02 07:01:27 +00003472 II->setCallingConv(CC);
3473 II->setAttributes(PAL);
3474 Inst = II;
3475 return false;
3476}
3477
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003478/// ParseResume
3479/// ::= 'resume' TypeAndValue
3480bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3481 Value *Exn; LocTy ExnLoc;
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003482 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3483 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003484
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003485 ResumeInst *RI = ResumeInst::Create(Exn);
3486 Inst = RI;
3487 return false;
3488}
Chris Lattnerdf986172009-01-02 07:01:27 +00003489
3490//===----------------------------------------------------------------------===//
3491// Binary Operators.
3492//===----------------------------------------------------------------------===//
3493
3494/// ParseArithmetic
Chris Lattnere914b592009-01-05 08:24:46 +00003495/// ::= ArithmeticOps TypeAndValue ',' Value
3496///
3497/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3498/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerdf986172009-01-02 07:01:27 +00003499bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnere914b592009-01-05 08:24:46 +00003500 unsigned Opc, unsigned OperandType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003501 LocTy Loc; Value *LHS, *RHS;
3502 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3503 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3504 ParseValue(LHS->getType(), RHS, PFS))
3505 return true;
3506
Chris Lattnere914b592009-01-05 08:24:46 +00003507 bool Valid;
3508 switch (OperandType) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003509 default: llvm_unreachable("Unknown operand type!");
Chris Lattnere914b592009-01-05 08:24:46 +00003510 case 0: // int or FP.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003511 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3512 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnere914b592009-01-05 08:24:46 +00003513 break;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003514 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3515 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnere914b592009-01-05 08:24:46 +00003516 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003517
Chris Lattnere914b592009-01-05 08:24:46 +00003518 if (!Valid)
3519 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003520
Chris Lattnerdf986172009-01-02 07:01:27 +00003521 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3522 return false;
3523}
3524
3525/// ParseLogical
3526/// ::= ArithmeticOps TypeAndValue ',' Value {
3527bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3528 unsigned Opc) {
3529 LocTy Loc; Value *LHS, *RHS;
3530 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3531 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3532 ParseValue(LHS->getType(), RHS, PFS))
3533 return true;
3534
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003535 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003536 return Error(Loc,"instruction requires integer or integer vector operands");
3537
3538 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3539 return false;
3540}
3541
3542
3543/// ParseCompare
3544/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3545/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerdf986172009-01-02 07:01:27 +00003546bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3547 unsigned Opc) {
3548 // Parse the integer/fp comparison predicate.
3549 LocTy Loc;
3550 unsigned Pred;
3551 Value *LHS, *RHS;
3552 if (ParseCmpPredicate(Pred, Opc) ||
3553 ParseTypeAndValue(LHS, Loc, PFS) ||
3554 ParseToken(lltok::comma, "expected ',' after compare value") ||
3555 ParseValue(LHS->getType(), RHS, PFS))
3556 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003557
Chris Lattnerdf986172009-01-02 07:01:27 +00003558 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003559 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003560 return Error(Loc, "fcmp requires floating point operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003561 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003562 } else {
3563 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003564 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem16087692011-12-05 06:29:09 +00003565 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003566 return Error(Loc, "icmp requires integer operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003567 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003568 }
3569 return false;
3570}
3571
3572//===----------------------------------------------------------------------===//
3573// Other Instructions.
3574//===----------------------------------------------------------------------===//
3575
3576
3577/// ParseCast
3578/// ::= CastOpc TypeAndValue 'to' Type
3579bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3580 unsigned Opc) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003581 LocTy Loc;
3582 Value *Op;
3583 Type *DestTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003584 if (ParseTypeAndValue(Op, Loc, PFS) ||
3585 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3586 ParseType(DestTy))
3587 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003588
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003589 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3590 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00003591 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003592 getTypeString(Op->getType()) + "' to '" +
3593 getTypeString(DestTy) + "'");
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003594 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003595 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3596 return false;
3597}
3598
3599/// ParseSelect
3600/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3601bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3602 LocTy Loc;
3603 Value *Op0, *Op1, *Op2;
3604 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3605 ParseToken(lltok::comma, "expected ',' after select condition") ||
3606 ParseTypeAndValue(Op1, PFS) ||
3607 ParseToken(lltok::comma, "expected ',' after select value") ||
3608 ParseTypeAndValue(Op2, PFS))
3609 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003610
Chris Lattnerdf986172009-01-02 07:01:27 +00003611 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3612 return Error(Loc, Reason);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003613
Chris Lattnerdf986172009-01-02 07:01:27 +00003614 Inst = SelectInst::Create(Op0, Op1, Op2);
3615 return false;
3616}
3617
Chris Lattner0088a5c2009-01-05 08:18:44 +00003618/// ParseVA_Arg
3619/// ::= 'va_arg' TypeAndValue ',' Type
3620bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003621 Value *Op;
Chris Lattner1afcace2011-07-09 17:41:24 +00003622 Type *EltTy = 0;
Chris Lattner0088a5c2009-01-05 08:18:44 +00003623 LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003624 if (ParseTypeAndValue(Op, PFS) ||
3625 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattner0088a5c2009-01-05 08:18:44 +00003626 ParseType(EltTy, TypeLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003627 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003628
Chris Lattner0088a5c2009-01-05 08:18:44 +00003629 if (!EltTy->isFirstClassType())
3630 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003631
3632 Inst = new VAArgInst(Op, EltTy);
3633 return false;
3634}
3635
3636/// ParseExtractElement
3637/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3638bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3639 LocTy Loc;
3640 Value *Op0, *Op1;
3641 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3642 ParseToken(lltok::comma, "expected ',' after extract value") ||
3643 ParseTypeAndValue(Op1, PFS))
3644 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003645
Chris Lattnerdf986172009-01-02 07:01:27 +00003646 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3647 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003648
Eric Christophera3500da2009-07-25 02:28:41 +00003649 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003650 return false;
3651}
3652
3653/// ParseInsertElement
3654/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3655bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3656 LocTy Loc;
3657 Value *Op0, *Op1, *Op2;
3658 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3659 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3660 ParseTypeAndValue(Op1, PFS) ||
3661 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3662 ParseTypeAndValue(Op2, PFS))
3663 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003664
Chris Lattnerdf986172009-01-02 07:01:27 +00003665 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopher0aaf4e92009-07-23 01:01:32 +00003666 return Error(Loc, "invalid insertelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003667
Chris Lattnerdf986172009-01-02 07:01:27 +00003668 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3669 return false;
3670}
3671
3672/// ParseShuffleVector
3673/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3674bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3675 LocTy Loc;
3676 Value *Op0, *Op1, *Op2;
3677 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3678 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3679 ParseTypeAndValue(Op1, PFS) ||
3680 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3681 ParseTypeAndValue(Op2, PFS))
3682 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003683
Chris Lattnerdf986172009-01-02 07:01:27 +00003684 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperaf393682012-02-01 23:43:12 +00003685 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003686
Chris Lattnerdf986172009-01-02 07:01:27 +00003687 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3688 return false;
3689}
3690
3691/// ParsePHI
Chris Lattnerc6e20092009-10-18 05:27:44 +00003692/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003693int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003694 Type *Ty = 0; LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003695 Value *Op0, *Op1;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003696
Chris Lattner1afcace2011-07-09 17:41:24 +00003697 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003698 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3699 ParseValue(Ty, Op0, PFS) ||
3700 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003701 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003702 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3703 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003704
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003705 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003706 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3707 while (1) {
3708 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003709
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003710 if (!EatIfPresent(lltok::comma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003711 break;
3712
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003713 if (Lex.getKind() == lltok::MetadataVar) {
3714 AteExtraComma = true;
Devang Patela43d46f2009-10-16 18:45:49 +00003715 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003716 }
Devang Patela43d46f2009-10-16 18:45:49 +00003717
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003718 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003719 ParseValue(Ty, Op0, PFS) ||
3720 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003721 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003722 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3723 return true;
3724 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003725
Chris Lattnerdf986172009-01-02 07:01:27 +00003726 if (!Ty->isFirstClassType())
3727 return Error(TypeLoc, "phi node must have first class type");
3728
Jay Foad3ecfc862011-03-30 11:28:46 +00003729 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003730 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3731 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3732 Inst = PN;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003733 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003734}
3735
Bill Wendlinge6e88262011-08-12 20:24:12 +00003736/// ParseLandingPad
3737/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3738/// Clause
3739/// ::= 'catch' TypeAndValue
3740/// ::= 'filter'
3741/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3742bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
3743 Type *Ty = 0; LocTy TyLoc;
3744 Value *PersFn; LocTy PersFnLoc;
Bill Wendlinge6e88262011-08-12 20:24:12 +00003745
3746 if (ParseType(Ty, TyLoc) ||
3747 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3748 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3749 return true;
3750
3751 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3752 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3753
3754 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3755 LandingPadInst::ClauseType CT;
3756 if (EatIfPresent(lltok::kw_catch))
3757 CT = LandingPadInst::Catch;
3758 else if (EatIfPresent(lltok::kw_filter))
3759 CT = LandingPadInst::Filter;
3760 else
3761 return TokError("expected 'catch' or 'filter' clause type");
3762
3763 Value *V; LocTy VLoc;
3764 if (ParseTypeAndValue(V, VLoc, PFS)) {
3765 delete LP;
3766 return true;
3767 }
3768
Bill Wendling746c8822011-08-12 20:52:25 +00003769 // A 'catch' type expects a non-array constant. A filter clause expects an
3770 // array constant.
3771 if (CT == LandingPadInst::Catch) {
3772 if (isa<ArrayType>(V->getType()))
3773 Error(VLoc, "'catch' clause has an invalid type");
3774 } else {
3775 if (!isa<ArrayType>(V->getType()))
3776 Error(VLoc, "'filter' clause has an invalid type");
3777 }
3778
Bill Wendlinge6e88262011-08-12 20:24:12 +00003779 LP->addClause(V);
3780 }
3781
3782 Inst = LP;
3783 return false;
3784}
3785
Chris Lattnerdf986172009-01-02 07:01:27 +00003786/// ParseCall
3787/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3788/// ParameterList OptionalAttrs
3789bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3790 bool isTail) {
Bill Wendling702cc912012-10-15 20:35:56 +00003791 AttrBuilder RetAttrs, FnAttrs;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003792 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00003793 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003794 LocTy RetTypeLoc;
3795 ValID CalleeID;
3796 SmallVector<ParamInfo, 16> ArgList;
3797 LocTy CallLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003798
Chris Lattnerdf986172009-01-02 07:01:27 +00003799 if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3800 ParseOptionalCallingConv(CC) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00003801 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003802 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003803 ParseValID(CalleeID) ||
3804 ParseParameterList(ArgList, PFS) ||
Bill Wendlingea007fa2013-02-08 00:52:31 +00003805 ParseFnAttributeValuePairs(FnAttrs, false))
Chris Lattnerdf986172009-01-02 07:01:27 +00003806 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003807
Chris Lattnerdf986172009-01-02 07:01:27 +00003808 // If RetType is a non-function pointer type, then this is the short syntax
3809 // for the call, which means that RetType is just the return type. Infer the
3810 // rest of the function argument types from the arguments that are present.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003811 PointerType *PFTy = 0;
3812 FunctionType *Ty = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003813 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3814 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3815 // Pull out the types of all of the arguments...
Jay Foad5fdd6c82011-07-12 14:06:48 +00003816 std::vector<Type*> ParamTypes;
Eli Friedman83b4a972010-07-24 23:06:59 +00003817 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3818 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003819
Chris Lattnerdf986172009-01-02 07:01:27 +00003820 if (!FunctionType::isValidReturnType(RetType))
3821 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003822
Owen Andersondebcb012009-07-29 22:17:13 +00003823 Ty = FunctionType::get(RetType, ParamTypes, false);
3824 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003825 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003826
Chris Lattnerdf986172009-01-02 07:01:27 +00003827 // Look up the callee.
3828 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003829 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003830
Bill Wendling034b94b2012-12-19 07:18:57 +00003831 // Set up the Attribute for the function.
Bill Wendlinga1683d62013-01-27 02:24:02 +00003832 SmallVector<AttributeSet, 8> Attrs;
Bill Wendlinge603fe42012-09-19 23:54:18 +00003833 if (RetAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003834 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3835 AttributeSet::ReturnIndex,
3836 RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003837
Chris Lattnerdf986172009-01-02 07:01:27 +00003838 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003839
Chris Lattnerdf986172009-01-02 07:01:27 +00003840 // Loop through FunctionType's arguments and ensure they are specified
3841 // correctly. Also, gather any parameter attributes.
3842 FunctionType::param_iterator I = Ty->param_begin();
3843 FunctionType::param_iterator E = Ty->param_end();
3844 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003845 Type *ExpectedTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003846 if (I != E) {
3847 ExpectedTy = *I++;
3848 } else if (!Ty->isVarArg()) {
3849 return Error(ArgList[i].Loc, "too many arguments specified");
3850 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003851
Chris Lattnerdf986172009-01-02 07:01:27 +00003852 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3853 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003854 getTypeString(ExpectedTy) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003855 Args.push_back(ArgList[i].V);
Bill Wendling73dee182013-01-31 00:29:54 +00003856 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3857 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlinga1683d62013-01-27 02:24:02 +00003858 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3859 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003860 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003861
Chris Lattnerdf986172009-01-02 07:01:27 +00003862 if (I != E)
3863 return Error(CallLoc, "not enough parameters specified for call");
3864
Bill Wendlinge603fe42012-09-19 23:54:18 +00003865 if (FnAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003866 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3867 AttributeSet::FunctionIndex,
3868 FnAttrs));
Chris Lattnerdf986172009-01-02 07:01:27 +00003869
Bill Wendling034b94b2012-12-19 07:18:57 +00003870 // Finish off the Attribute and check them
Bill Wendling99faa3b2012-12-07 23:16:57 +00003871 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003872
Jay Foada3efbb12011-07-15 08:37:34 +00003873 CallInst *CI = CallInst::Create(Callee, Args);
Chris Lattnerdf986172009-01-02 07:01:27 +00003874 CI->setTailCall(isTail);
3875 CI->setCallingConv(CC);
3876 CI->setAttributes(PAL);
3877 Inst = CI;
3878 return false;
3879}
3880
3881//===----------------------------------------------------------------------===//
3882// Memory Instructions.
3883//===----------------------------------------------------------------------===//
3884
3885/// ParseAlloc
Devang Patelf633a062009-09-17 23:04:48 +00003886/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
Chris Lattnerf3a789d2011-06-17 03:16:47 +00003887int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003888 Value *Size = 0;
Chris Lattnereeb4a842009-07-02 23:08:13 +00003889 LocTy SizeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003890 unsigned Alignment = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +00003891 Type *Ty = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003892 if (ParseType(Ty)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003893
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003894 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003895 if (EatIfPresent(lltok::comma)) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003896 if (Lex.getKind() == lltok::kw_align) {
3897 if (ParseOptionalAlignment(Alignment)) return true;
3898 } else if (Lex.getKind() == lltok::MetadataVar) {
3899 AteExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003900 } else {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003901 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3902 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3903 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003904 }
3905 }
3906
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003907 if (Size && !Size->getType()->isIntegerTy())
3908 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003909
Chris Lattnerf3a789d2011-06-17 03:16:47 +00003910 Inst = new AllocaInst(Ty, Size, Alignment);
3911 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003912}
3913
3914/// ParseLoad
Eli Friedmanf03bb262011-08-12 22:50:01 +00003915/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman407a6162012-11-15 22:34:00 +00003916/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedmanf03bb262011-08-12 22:50:01 +00003917/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003918int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003919 Value *Val; LocTy Loc;
Devang Patelf633a062009-09-17 23:04:48 +00003920 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003921 bool AteExtraComma = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003922 bool isAtomic = false;
Eli Friedman21006d42011-08-09 23:02:53 +00003923 AtomicOrdering Ordering = NotAtomic;
3924 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003925
3926 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00003927 isAtomic = true;
3928 Lex.Lex();
3929 }
3930
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003931 bool isVolatile = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003932 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00003933 isVolatile = true;
3934 Lex.Lex();
3935 }
3936
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003937 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman21006d42011-08-09 23:02:53 +00003938 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003939 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3940 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003941
Duncan Sands1df98592010-02-16 11:11:14 +00003942 if (!Val->getType()->isPointerTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003943 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3944 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman21006d42011-08-09 23:02:53 +00003945 if (isAtomic && !Alignment)
3946 return Error(Loc, "atomic load must have explicit non-zero alignment");
3947 if (Ordering == Release || Ordering == AcquireRelease)
3948 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003949
Eli Friedman21006d42011-08-09 23:02:53 +00003950 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003951 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003952}
3953
3954/// ParseStore
Eli Friedmanf03bb262011-08-12 22:50:01 +00003955
3956/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
3957/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman21006d42011-08-09 23:02:53 +00003958/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003959int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003960 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelf633a062009-09-17 23:04:48 +00003961 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003962 bool AteExtraComma = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003963 bool isAtomic = false;
Eli Friedman21006d42011-08-09 23:02:53 +00003964 AtomicOrdering Ordering = NotAtomic;
3965 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003966
3967 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00003968 isAtomic = true;
3969 Lex.Lex();
3970 }
3971
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003972 bool isVolatile = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003973 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00003974 isVolatile = true;
3975 Lex.Lex();
3976 }
3977
Chris Lattnerdf986172009-01-02 07:01:27 +00003978 if (ParseTypeAndValue(Val, Loc, PFS) ||
3979 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003980 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman21006d42011-08-09 23:02:53 +00003981 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003982 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003983 return true;
Devang Patelf633a062009-09-17 23:04:48 +00003984
Duncan Sands1df98592010-02-16 11:11:14 +00003985 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003986 return Error(PtrLoc, "store operand must be a pointer");
3987 if (!Val->getType()->isFirstClassType())
3988 return Error(Loc, "store operand must be a first class value");
3989 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3990 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman21006d42011-08-09 23:02:53 +00003991 if (isAtomic && !Alignment)
3992 return Error(Loc, "atomic store must have explicit non-zero alignment");
3993 if (Ordering == Acquire || Ordering == AcquireRelease)
3994 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003995
Eli Friedman21006d42011-08-09 23:02:53 +00003996 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003997 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003998}
3999
Eli Friedmanff030482011-07-28 21:48:00 +00004000/// ParseCmpXchg
Eli Friedmanf03bb262011-08-12 22:50:01 +00004001/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
4002/// 'singlethread'? AtomicOrdering
4003int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanff030482011-07-28 21:48:00 +00004004 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4005 bool AteExtraComma = false;
4006 AtomicOrdering Ordering = NotAtomic;
4007 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004008 bool isVolatile = false;
4009
4010 if (EatIfPresent(lltok::kw_volatile))
4011 isVolatile = true;
4012
Eli Friedmanff030482011-07-28 21:48:00 +00004013 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4014 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4015 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4016 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4017 ParseTypeAndValue(New, NewLoc, PFS) ||
4018 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4019 return true;
4020
4021 if (Ordering == Unordered)
4022 return TokError("cmpxchg cannot be unordered");
4023 if (!Ptr->getType()->isPointerTy())
4024 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4025 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4026 return Error(CmpLoc, "compare value and pointer type do not match");
4027 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4028 return Error(NewLoc, "new value and pointer type do not match");
4029 if (!New->getType()->isIntegerTy())
4030 return Error(NewLoc, "cmpxchg operand must be an integer");
4031 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4032 if (Size < 8 || (Size & (Size - 1)))
4033 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4034 " integer");
4035
4036 AtomicCmpXchgInst *CXI =
4037 new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, Scope);
4038 CXI->setVolatile(isVolatile);
4039 Inst = CXI;
4040 return AteExtraComma ? InstExtraComma : InstNormal;
4041}
4042
4043/// ParseAtomicRMW
Eli Friedmanf03bb262011-08-12 22:50:01 +00004044/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4045/// 'singlethread'? AtomicOrdering
4046int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanff030482011-07-28 21:48:00 +00004047 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4048 bool AteExtraComma = false;
4049 AtomicOrdering Ordering = NotAtomic;
4050 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004051 bool isVolatile = false;
Eli Friedmanff030482011-07-28 21:48:00 +00004052 AtomicRMWInst::BinOp Operation;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004053
4054 if (EatIfPresent(lltok::kw_volatile))
4055 isVolatile = true;
4056
Eli Friedmanff030482011-07-28 21:48:00 +00004057 switch (Lex.getKind()) {
4058 default: return TokError("expected binary operation in atomicrmw");
4059 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4060 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4061 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4062 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4063 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4064 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4065 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4066 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4067 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4068 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4069 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4070 }
4071 Lex.Lex(); // Eat the operation.
4072
4073 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4074 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4075 ParseTypeAndValue(Val, ValLoc, PFS) ||
4076 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4077 return true;
4078
4079 if (Ordering == Unordered)
4080 return TokError("atomicrmw cannot be unordered");
4081 if (!Ptr->getType()->isPointerTy())
4082 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4083 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4084 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4085 if (!Val->getType()->isIntegerTy())
4086 return Error(ValLoc, "atomicrmw operand must be an integer");
4087 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4088 if (Size < 8 || (Size & (Size - 1)))
4089 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4090 " integer");
4091
4092 AtomicRMWInst *RMWI =
4093 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4094 RMWI->setVolatile(isVolatile);
4095 Inst = RMWI;
4096 return AteExtraComma ? InstExtraComma : InstNormal;
4097}
4098
Eli Friedman47f35132011-07-25 23:16:38 +00004099/// ParseFence
4100/// ::= 'fence' 'singlethread'? AtomicOrdering
4101int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4102 AtomicOrdering Ordering = NotAtomic;
4103 SynchronizationScope Scope = CrossThread;
4104 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4105 return true;
4106
4107 if (Ordering == Unordered)
4108 return TokError("fence cannot be unordered");
4109 if (Ordering == Monotonic)
4110 return TokError("fence cannot be monotonic");
4111
4112 Inst = new FenceInst(Context, Ordering, Scope);
4113 return InstNormal;
4114}
4115
Chris Lattnerdf986172009-01-02 07:01:27 +00004116/// ParseGetElementPtr
Dan Gohmandd8004d2009-07-27 21:53:46 +00004117/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004118int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Nadav Rotem16087692011-12-05 06:29:09 +00004119 Value *Ptr = 0;
4120 Value *Val = 0;
4121 LocTy Loc, EltLoc;
Dan Gohmandd8004d2009-07-27 21:53:46 +00004122
Dan Gohmandcb40a32009-07-29 15:58:36 +00004123 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohmandd8004d2009-07-27 21:53:46 +00004124
Chris Lattner3ed88ef2009-01-02 08:05:26 +00004125 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00004126
Nadav Rotem16087692011-12-05 06:29:09 +00004127 if (!Ptr->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00004128 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00004129
Chris Lattnerdf986172009-01-02 07:01:27 +00004130 SmallVector<Value*, 16> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004131 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00004132 while (EatIfPresent(lltok::comma)) {
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004133 if (Lex.getKind() == lltok::MetadataVar) {
4134 AteExtraComma = true;
Devang Patel6225d642009-10-13 18:49:55 +00004135 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004136 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00004137 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem16087692011-12-05 06:29:09 +00004138 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00004139 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem16087692011-12-05 06:29:09 +00004140 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4141 return Error(EltLoc, "getelementptr index type missmatch");
4142 if (Val->getType()->isVectorTy()) {
4143 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4144 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4145 if (ValNumEl != PtrNumEl)
4146 return Error(EltLoc,
4147 "getelementptr vector index has a wrong number of elements");
4148 }
Chris Lattnerdf986172009-01-02 07:01:27 +00004149 Indices.push_back(Val);
4150 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00004151
Jay Foada9203102011-07-25 09:48:08 +00004152 if (!GetElementPtrInst::getIndexedType(Ptr->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00004153 return Error(Loc, "invalid getelementptr indices");
Jay Foada9203102011-07-25 09:48:08 +00004154 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohmandd8004d2009-07-27 21:53:46 +00004155 if (InBounds)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00004156 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004157 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00004158}
4159
4160/// ParseExtractValue
4161/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004162int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00004163 Value *Val; LocTy Loc;
4164 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004165 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00004166 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004167 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00004168 return true;
4169
Chris Lattnerfdfeb692010-02-12 20:49:41 +00004170 if (!Val->getType()->isAggregateType())
4171 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00004172
Jay Foadfc6d3a42011-07-13 10:26:04 +00004173 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00004174 return Error(Loc, "invalid indices for extractvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00004175 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004176 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00004177}
4178
4179/// ParseInsertValue
4180/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004181int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00004182 Value *Val0, *Val1; LocTy Loc0, Loc1;
4183 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004184 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00004185 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4186 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4187 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004188 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00004189 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00004190
Chris Lattnerfdfeb692010-02-12 20:49:41 +00004191 if (!Val0->getType()->isAggregateType())
4192 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00004193
Jay Foadfc6d3a42011-07-13 10:26:04 +00004194 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00004195 return Error(Loc0, "invalid indices for insertvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00004196 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004197 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00004198}
Nick Lewycky21cc4462009-04-04 07:22:01 +00004199
4200//===----------------------------------------------------------------------===//
4201// Embedded metadata.
4202//===----------------------------------------------------------------------===//
4203
4204/// ParseMDNodeVector
Nick Lewyckycb337992009-05-10 20:57:05 +00004205/// ::= Element (',' Element)*
4206/// Element
4207/// ::= 'null' | TypeAndValue
Victor Hernandezbf170d42010-01-05 22:22:14 +00004208bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandez24e64df2010-01-10 07:14:18 +00004209 PerFunctionState *PFS) {
Dan Gohmanac809752010-07-13 19:33:27 +00004210 // Check for an empty list.
4211 if (Lex.getKind() == lltok::rbrace)
4212 return false;
4213
Nick Lewycky21cc4462009-04-04 07:22:01 +00004214 do {
Chris Lattnera7352392009-12-30 04:42:57 +00004215 // Null is a special case since it is typeless.
4216 if (EatIfPresent(lltok::kw_null)) {
4217 Elts.push_back(0);
4218 continue;
Nick Lewyckycb337992009-05-10 20:57:05 +00004219 }
Michael Ilseman407a6162012-11-15 22:34:00 +00004220
Chris Lattnera7352392009-12-30 04:42:57 +00004221 Value *V = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +00004222 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckycb337992009-05-10 20:57:05 +00004223 Elts.push_back(V);
Nick Lewycky21cc4462009-04-04 07:22:01 +00004224 } while (EatIfPresent(lltok::comma));
4225
4226 return false;
4227}