blob: 22c21c62b92d47162e9186e8c73a6b1e4bc6d817 [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") ||
755 ParseAttributeValuePairs(ForwardRefAttrBuilder[VarID]) ||
756 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
765/// ParseAttributeValuePairs
766/// ::= <attr> | <attr> '=' <value>
767bool LLParser::ParseAttributeValuePairs(AttrBuilder &B) {
768 while (true) {
769 lltok::Kind Token = Lex.getKind();
770 switch (Token) {
771 default:
772 return Error(Lex.getLoc(), "unterminated attribute group");
773 case lltok::rbrace:
774 // Finished.
775 return false;
776
777 // Target-dependent attributes:
778 case lltok::StringConstant: {
779 std::string Attr = Lex.getStrVal();
780 Lex.Lex();
781 std::string Val;
782 if (EatIfPresent(lltok::equal) &&
783 ParseStringConstant(Val))
784 return true;
785
786 B.addAttribute(Attr, Val);
787 break;
788 }
789
790 // Target-independent attributes:
791 case lltok::kw_align: {
792 unsigned Alignment;
793 if (ParseToken(lltok::equal, "expected '=' here") ||
794 ParseUInt32(Alignment))
795 return true;
796 B.addAlignmentAttr(Alignment);
797 break;
798 }
799 case lltok::kw_alignstack: {
800 unsigned Alignment;
801 if (ParseToken(lltok::equal, "expected '=' here") ||
802 ParseUInt32(Alignment))
803 return true;
804 B.addStackAlignmentAttr(Alignment);
805 break;
806 }
807 case lltok::kw_address_safety: B.addAttribute(Attribute::AddressSafety); break;
808 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
809 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
810 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
811 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
812 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
813 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
814 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
815 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
816 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
817 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
818 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
819 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
820 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
821 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
822 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
823 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
824 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
825 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
826 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
827 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
828 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
829 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
830 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
831 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
832 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
833 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
834 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
835 }
836
837 Lex.Lex();
838 }
839}
Chris Lattnerdf986172009-01-02 07:01:27 +0000840
841//===----------------------------------------------------------------------===//
842// GlobalValue Reference/Resolution Routines.
843//===----------------------------------------------------------------------===//
844
845/// GetGlobalVal - Get a value with the specified name or ID, creating a
846/// forward reference record if needed. This can return null if the value
847/// exists but does not have the right type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000848GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerdf986172009-01-02 07:01:27 +0000849 LocTy Loc) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000850 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +0000851 if (PTy == 0) {
852 Error(Loc, "global variable reference must have pointer type");
853 return 0;
854 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000855
Chris Lattnerdf986172009-01-02 07:01:27 +0000856 // Look this name up in the normal function symbol table.
857 GlobalValue *Val =
858 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000859
Chris Lattnerdf986172009-01-02 07:01:27 +0000860 // If this is a forward reference for the value, see if we already created a
861 // forward ref record.
862 if (Val == 0) {
863 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
864 I = ForwardRefVals.find(Name);
865 if (I != ForwardRefVals.end())
866 Val = I->second.first;
867 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000868
Chris Lattnerdf986172009-01-02 07:01:27 +0000869 // If we have the value in the symbol table or fwd-ref table, return it.
870 if (Val) {
871 if (Val->getType() == Ty) return Val;
872 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +0000873 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +0000874 return 0;
875 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000876
Chris Lattnerdf986172009-01-02 07:01:27 +0000877 // Otherwise, create a new forward reference for this value and remember it.
878 GlobalValue *FwdVal;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000879 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000880 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattner1afcace2011-07-09 17:41:24 +0000881 else
Owen Andersone9b11b42009-07-08 19:03:57 +0000882 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Justin Holewinskieaff2d52012-11-16 21:03:47 +0000883 GlobalValue::ExternalWeakLinkage, 0, Name,
884 0, GlobalVariable::NotThreadLocal,
885 PTy->getAddressSpace());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000886
Chris Lattnerdf986172009-01-02 07:01:27 +0000887 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
888 return FwdVal;
889}
890
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000891GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
892 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +0000893 if (PTy == 0) {
894 Error(Loc, "global variable reference must have pointer type");
895 return 0;
896 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000897
Chris Lattnerdf986172009-01-02 07:01:27 +0000898 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000899
Chris Lattnerdf986172009-01-02 07:01:27 +0000900 // If this is a forward reference for the value, see if we already created a
901 // forward ref record.
902 if (Val == 0) {
903 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
904 I = ForwardRefValIDs.find(ID);
905 if (I != ForwardRefValIDs.end())
906 Val = I->second.first;
907 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000908
Chris Lattnerdf986172009-01-02 07:01:27 +0000909 // If we have the value in the symbol table or fwd-ref table, return it.
910 if (Val) {
911 if (Val->getType() == Ty) return Val;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000912 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +0000913 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +0000914 return 0;
915 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000916
Chris Lattnerdf986172009-01-02 07:01:27 +0000917 // Otherwise, create a new forward reference for this value and remember it.
918 GlobalValue *FwdVal;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000919 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000920 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattner1afcace2011-07-09 17:41:24 +0000921 else
Owen Andersone9b11b42009-07-08 19:03:57 +0000922 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
923 GlobalValue::ExternalWeakLinkage, 0, "");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000924
Chris Lattnerdf986172009-01-02 07:01:27 +0000925 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
926 return FwdVal;
927}
928
929
930//===----------------------------------------------------------------------===//
931// Helper Routines.
932//===----------------------------------------------------------------------===//
933
934/// ParseToken - If the current token has the specified kind, eat it and return
935/// success. Otherwise, emit the specified error and return failure.
936bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
937 if (Lex.getKind() != T)
938 return TokError(ErrMsg);
939 Lex.Lex();
940 return false;
941}
942
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000943/// ParseStringConstant
944/// ::= StringConstant
945bool LLParser::ParseStringConstant(std::string &Result) {
946 if (Lex.getKind() != lltok::StringConstant)
947 return TokError("expected string constant");
948 Result = Lex.getStrVal();
949 Lex.Lex();
950 return false;
951}
952
953/// ParseUInt32
954/// ::= uint32
955bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000956 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
957 return TokError("expected integer");
958 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
959 if (Val64 != unsigned(Val64))
960 return TokError("expected 32-bit integer (too large)");
961 Val = Val64;
962 Lex.Lex();
963 return false;
964}
965
Hans Wennborgce718ff2012-06-23 11:37:03 +0000966/// ParseTLSModel
967/// := 'localdynamic'
968/// := 'initialexec'
969/// := 'localexec'
970bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
971 switch (Lex.getKind()) {
972 default:
973 return TokError("expected localdynamic, initialexec or localexec");
974 case lltok::kw_localdynamic:
975 TLM = GlobalVariable::LocalDynamicTLSModel;
976 break;
977 case lltok::kw_initialexec:
978 TLM = GlobalVariable::InitialExecTLSModel;
979 break;
980 case lltok::kw_localexec:
981 TLM = GlobalVariable::LocalExecTLSModel;
982 break;
983 }
984
985 Lex.Lex();
986 return false;
987}
988
989/// ParseOptionalThreadLocal
990/// := /*empty*/
991/// := 'thread_local'
992/// := 'thread_local' '(' tlsmodel ')'
993bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
994 TLM = GlobalVariable::NotThreadLocal;
995 if (!EatIfPresent(lltok::kw_thread_local))
996 return false;
997
998 TLM = GlobalVariable::GeneralDynamicTLSModel;
999 if (Lex.getKind() == lltok::lparen) {
1000 Lex.Lex();
1001 return ParseTLSModel(TLM) ||
1002 ParseToken(lltok::rparen, "expected ')' after thread local model");
1003 }
1004 return false;
1005}
Chris Lattnerdf986172009-01-02 07:01:27 +00001006
1007/// ParseOptionalAddrSpace
1008/// := /*empty*/
1009/// := 'addrspace' '(' uint32 ')'
1010bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1011 AddrSpace = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001012 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerdf986172009-01-02 07:01:27 +00001013 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001014 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001015 ParseUInt32(AddrSpace) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00001016 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001017}
Chris Lattnerdf986172009-01-02 07:01:27 +00001018
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001019/// ParseOptionalFuncAttrs - Parse a potentially empty list of function attributes.
1020bool LLParser::ParseOptionalFuncAttrs(AttrBuilder &B) {
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001021 bool HaveError = false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001022
Bill Wendlingf385f4c2012-10-08 23:27:46 +00001023 B.clear();
1024
Chris Lattnerdf986172009-01-02 07:01:27 +00001025 while (1) {
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001026 lltok::Kind Token = Lex.getKind();
1027 switch (Token) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001028 default: // End of attributes.
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001029 return HaveError;
Charles Davis1e063d12010-02-12 00:31:15 +00001030 case lltok::kw_alignstack: {
1031 unsigned Alignment;
1032 if (ParseOptionalStackAlignment(Alignment))
1033 return true;
Bill Wendling03272442012-10-08 22:20:14 +00001034 B.addStackAlignmentAttr(Alignment);
Charles Davis1e063d12010-02-12 00:31:15 +00001035 continue;
1036 }
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001037 case lltok::kw_align: {
1038 // As a hack, we allow "align 2" on functions as a synonym for "alignstack
1039 // 2".
1040 unsigned Alignment;
1041 if (ParseOptionalAlignment(Alignment))
1042 return true;
1043 B.addAlignmentAttr(Alignment);
1044 continue;
1045 }
Bill Wendling034b94b2012-12-19 07:18:57 +00001046 case lltok::kw_address_safety: B.addAttribute(Attribute::AddressSafety); break;
1047 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
1048 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
1049 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
1050 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
1051 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
1052 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
1053 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
1054 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
1055 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
1056 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
1057 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
1058 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1059 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
1060 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
1061 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
1062 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
Bill Wendling114baee2013-01-23 06:41:41 +00001063 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
Bill Wendling034b94b2012-12-19 07:18:57 +00001064 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
James Molloy67ae1352012-12-20 16:04:27 +00001065 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
Charles Davis1e063d12010-02-12 00:31:15 +00001066
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001067 // Error handling.
1068 case lltok::kw_zeroext:
1069 case lltok::kw_signext:
1070 case lltok::kw_inreg:
1071 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on a function");
1072 break;
1073 case lltok::kw_sret: case lltok::kw_noalias:
1074 case lltok::kw_nocapture: case lltok::kw_byval:
1075 case lltok::kw_nest:
1076 HaveError |=
1077 Error(Lex.getLoc(), "invalid use of parameter-only attribute on a function");
1078 break;
1079 }
1080
1081 Lex.Lex();
1082 }
1083}
1084
1085/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1086bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1087 bool HaveError = false;
1088
1089 B.clear();
1090
1091 while (1) {
1092 lltok::Kind Token = Lex.getKind();
1093 switch (Token) {
1094 default: // End of attributes.
1095 return HaveError;
Chris Lattnerdf986172009-01-02 07:01:27 +00001096 case lltok::kw_align: {
1097 unsigned Alignment;
1098 if (ParseOptionalAlignment(Alignment))
1099 return true;
Bill Wendling03272442012-10-08 22:20:14 +00001100 B.addAlignmentAttr(Alignment);
Chris Lattnerdf986172009-01-02 07:01:27 +00001101 continue;
1102 }
Bill Wendling034b94b2012-12-19 07:18:57 +00001103 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
1104 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1105 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1106 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1107 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
1108 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1109 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1110 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davis1e063d12010-02-12 00:31:15 +00001111
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001112 case lltok::kw_noreturn: case lltok::kw_nounwind:
1113 case lltok::kw_uwtable: case lltok::kw_returns_twice:
1114 case lltok::kw_noinline: case lltok::kw_readnone:
1115 case lltok::kw_readonly: case lltok::kw_inlinehint:
1116 case lltok::kw_alwaysinline: case lltok::kw_optsize:
1117 case lltok::kw_ssp: case lltok::kw_sspreq:
1118 case lltok::kw_noredzone: case lltok::kw_noimplicitfloat:
1119 case lltok::kw_naked: case lltok::kw_nonlazybind:
1120 case lltok::kw_address_safety: case lltok::kw_minsize:
1121 case lltok::kw_alignstack:
1122 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1123 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001124 }
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001125
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001126 Lex.Lex();
1127 }
1128}
1129
1130/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1131bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1132 bool HaveError = false;
1133
1134 B.clear();
1135
1136 while (1) {
1137 lltok::Kind Token = Lex.getKind();
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001138 switch (Token) {
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001139 default: // End of attributes.
1140 return HaveError;
Bill Wendling034b94b2012-12-19 07:18:57 +00001141 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1142 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1143 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1144 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001145
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001146 // Error handling.
1147 case lltok::kw_sret: case lltok::kw_nocapture:
1148 case lltok::kw_byval: case lltok::kw_nest:
1149 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001150 break;
James Molloy67ae1352012-12-20 16:04:27 +00001151
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001152 case lltok::kw_noreturn: case lltok::kw_nounwind:
1153 case lltok::kw_uwtable: case lltok::kw_returns_twice:
1154 case lltok::kw_noinline: case lltok::kw_readnone:
1155 case lltok::kw_readonly: case lltok::kw_inlinehint:
1156 case lltok::kw_alwaysinline: case lltok::kw_optsize:
1157 case lltok::kw_ssp: case lltok::kw_sspreq:
Bill Wendling114baee2013-01-23 06:41:41 +00001158 case lltok::kw_sspstrong: case lltok::kw_noimplicitfloat:
1159 case lltok::kw_noredzone: case lltok::kw_naked:
1160 case lltok::kw_nonlazybind: case lltok::kw_address_safety:
1161 case lltok::kw_minsize: case lltok::kw_alignstack:
1162 case lltok::kw_align: case lltok::kw_noduplicate:
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001163 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendlingdc998cc2012-09-28 22:30:18 +00001164 break;
1165 }
1166
Chris Lattnerdf986172009-01-02 07:01:27 +00001167 Lex.Lex();
1168 }
1169}
1170
1171/// ParseOptionalLinkage
1172/// ::= /*empty*/
Rafael Espindolabb46f522009-01-15 20:18:42 +00001173/// ::= 'private'
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001174/// ::= 'linker_private'
Bill Wendling5e721d72010-07-01 21:55:59 +00001175/// ::= 'linker_private_weak'
Chris Lattnerdf986172009-01-02 07:01:27 +00001176/// ::= 'internal'
1177/// ::= 'weak'
Duncan Sands667d4b82009-03-07 15:45:40 +00001178/// ::= 'weak_odr'
Chris Lattnerdf986172009-01-02 07:01:27 +00001179/// ::= 'linkonce'
Duncan Sands667d4b82009-03-07 15:45:40 +00001180/// ::= 'linkonce_odr'
Bill Wendling32811be2012-08-17 18:33:14 +00001181/// ::= 'linkonce_odr_auto_hide'
Bill Wendling5e721d72010-07-01 21:55:59 +00001182/// ::= 'available_externally'
Chris Lattnerdf986172009-01-02 07:01:27 +00001183/// ::= 'appending'
1184/// ::= 'dllexport'
1185/// ::= 'common'
1186/// ::= 'dllimport'
1187/// ::= 'extern_weak'
1188/// ::= 'external'
1189bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1190 HasLinkage = false;
1191 switch (Lex.getKind()) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001192 default: Res=GlobalValue::ExternalLinkage; return false;
1193 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
1194 case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001195 case lltok::kw_linker_private_weak:
1196 Res = GlobalValue::LinkerPrivateWeakLinkage;
1197 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001198 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1199 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1200 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1201 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1202 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Bill Wendling32811be2012-08-17 18:33:14 +00001203 case lltok::kw_linkonce_odr_auto_hide:
1204 case lltok::kw_linker_private_weak_def_auto: // FIXME: For backwards compat.
1205 Res = GlobalValue::LinkOnceODRAutoHideLinkage;
1206 break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001207 case lltok::kw_available_externally:
1208 Res = GlobalValue::AvailableExternallyLinkage;
1209 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001210 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
1211 case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break;
1212 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
1213 case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break;
1214 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1215 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001216 }
1217 Lex.Lex();
1218 HasLinkage = true;
1219 return false;
1220}
1221
1222/// ParseOptionalVisibility
1223/// ::= /*empty*/
1224/// ::= 'default'
1225/// ::= 'hidden'
1226/// ::= 'protected'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001227///
Chris Lattnerdf986172009-01-02 07:01:27 +00001228bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1229 switch (Lex.getKind()) {
1230 default: Res = GlobalValue::DefaultVisibility; return false;
1231 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1232 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1233 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1234 }
1235 Lex.Lex();
1236 return false;
1237}
1238
1239/// ParseOptionalCallingConv
1240/// ::= /*empty*/
1241/// ::= 'ccc'
1242/// ::= 'fastcc'
Elena Demikhovsky35752222012-10-24 14:46:16 +00001243/// ::= 'kw_intel_ocl_bicc'
Chris Lattnerdf986172009-01-02 07:01:27 +00001244/// ::= 'coldcc'
1245/// ::= 'x86_stdcallcc'
1246/// ::= 'x86_fastcallcc'
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001247/// ::= 'x86_thiscallcc'
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001248/// ::= 'arm_apcscc'
1249/// ::= 'arm_aapcscc'
1250/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001251/// ::= 'msp430_intrcc'
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001252/// ::= 'ptx_kernel'
1253/// ::= 'ptx_device'
Micah Villmowe53d6052012-10-01 17:01:31 +00001254/// ::= 'spir_func'
1255/// ::= 'spir_kernel'
Chris Lattnerdf986172009-01-02 07:01:27 +00001256/// ::= 'cc' UINT
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001257///
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001258bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001259 switch (Lex.getKind()) {
1260 default: CC = CallingConv::C; return false;
1261 case lltok::kw_ccc: CC = CallingConv::C; break;
1262 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1263 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1264 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1265 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001266 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001267 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1268 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1269 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001270 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001271 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1272 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmowe53d6052012-10-01 17:01:31 +00001273 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1274 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovsky35752222012-10-24 14:46:16 +00001275 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001276 case lltok::kw_cc: {
1277 unsigned ArbitraryCC;
1278 Lex.Lex();
David Blaikie4d6ccb52012-01-20 21:51:11 +00001279 if (ParseUInt32(ArbitraryCC))
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001280 return true;
David Blaikie4d6ccb52012-01-20 21:51:11 +00001281 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1282 return false;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001283 }
Chris Lattnerdf986172009-01-02 07:01:27 +00001284 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001285
Chris Lattnerdf986172009-01-02 07:01:27 +00001286 Lex.Lex();
1287 return false;
1288}
1289
Chris Lattnerb8c46862009-12-30 05:31:19 +00001290/// ParseInstructionMetadata
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001291/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman9d072f52010-08-24 02:05:17 +00001292bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1293 PerFunctionState *PFS) {
Chris Lattnerb8c46862009-12-30 05:31:19 +00001294 do {
1295 if (Lex.getKind() != lltok::MetadataVar)
1296 return TokError("expected metadata after comma");
Devang Patel0475c912009-09-29 00:01:14 +00001297
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001298 std::string Name = Lex.getStrVal();
Benjamin Kramer85dadec2011-12-06 11:50:26 +00001299 unsigned MDK = M->getMDKindID(Name);
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001300 Lex.Lex();
Chris Lattner52e20312009-10-19 05:31:10 +00001301
Chris Lattner442ffa12009-12-29 21:53:55 +00001302 MDNode *Node;
Chris Lattner449c3102010-04-01 05:14:45 +00001303 SMLoc Loc = Lex.getLoc();
Dan Gohman309b3af2010-08-24 02:24:03 +00001304
1305 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattnere434d272009-12-30 04:56:59 +00001306 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001307
Dan Gohman68261142010-08-24 14:35:45 +00001308 // This code is similar to that of ParseMetadataValue, however it needs to
1309 // have special-case code for a forward reference; see the comments on
1310 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1311 // at the top level here.
Dan Gohman309b3af2010-08-24 02:24:03 +00001312 if (Lex.getKind() == lltok::lbrace) {
1313 ValID ID;
1314 if (ParseMetadataListValue(ID, PFS))
1315 return true;
1316 assert(ID.Kind == ValID::t_MDNode);
1317 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner449c3102010-04-01 05:14:45 +00001318 } else {
Nick Lewyckyc6877b42010-09-30 21:04:13 +00001319 unsigned NodeID = 0;
Dan Gohman309b3af2010-08-24 02:24:03 +00001320 if (ParseMDNodeID(Node, NodeID))
1321 return true;
1322 if (Node) {
1323 // If we got the node, add it to the instruction.
1324 Inst->setMetadata(MDK, Node);
1325 } else {
1326 MDRef R = { Loc, MDK, NodeID };
1327 // Otherwise, remember that this should be resolved later.
1328 ForwardRefInstMetadata[Inst].push_back(R);
1329 }
Chris Lattner449c3102010-04-01 05:14:45 +00001330 }
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001331
1332 // If this is the end of the list, we're done.
Chris Lattnerb8c46862009-12-30 05:31:19 +00001333 } while (EatIfPresent(lltok::comma));
1334 return false;
Devang Patelf633a062009-09-17 23:04:48 +00001335}
1336
Chris Lattnerdf986172009-01-02 07:01:27 +00001337/// ParseOptionalAlignment
1338/// ::= /* empty */
1339/// ::= 'align' 4
1340bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1341 Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001342 if (!EatIfPresent(lltok::kw_align))
1343 return false;
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001344 LocTy AlignLoc = Lex.getLoc();
1345 if (ParseUInt32(Alignment)) return true;
1346 if (!isPowerOf2_32(Alignment))
1347 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmane16829b2010-07-30 21:07:05 +00001348 if (Alignment > Value::MaximumAlignment)
Dan Gohman138aa2a2010-07-28 20:12:04 +00001349 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001350 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001351}
1352
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001353/// ParseOptionalCommaAlign
Michael Ilseman407a6162012-11-15 22:34:00 +00001354/// ::=
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001355/// ::= ',' align 4
1356///
1357/// This returns with AteExtraComma set to true if it ate an excess comma at the
1358/// end.
1359bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1360 bool &AteExtraComma) {
1361 AteExtraComma = false;
1362 while (EatIfPresent(lltok::comma)) {
1363 // Metadata at the end is an early exit.
Chris Lattner1d928312009-12-30 05:02:06 +00001364 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001365 AteExtraComma = true;
1366 return false;
1367 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001368
Chris Lattner093eed12010-04-23 00:50:50 +00001369 if (Lex.getKind() != lltok::kw_align)
1370 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sandsbf9fc532010-10-21 16:07:10 +00001371
Chris Lattner093eed12010-04-23 00:50:50 +00001372 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001373 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001374
Devang Patelf633a062009-09-17 23:04:48 +00001375 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001376}
1377
Eli Friedman47f35132011-07-25 23:16:38 +00001378/// ParseScopeAndOrdering
1379/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1380/// else: ::=
1381///
1382/// This sets Scope and Ordering to the parsed values.
1383bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1384 AtomicOrdering &Ordering) {
1385 if (!isAtomic)
1386 return false;
1387
1388 Scope = CrossThread;
1389 if (EatIfPresent(lltok::kw_singlethread))
1390 Scope = SingleThread;
1391 switch (Lex.getKind()) {
1392 default: return TokError("Expected ordering on atomic instruction");
1393 case lltok::kw_unordered: Ordering = Unordered; break;
1394 case lltok::kw_monotonic: Ordering = Monotonic; break;
1395 case lltok::kw_acquire: Ordering = Acquire; break;
1396 case lltok::kw_release: Ordering = Release; break;
1397 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1398 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1399 }
1400 Lex.Lex();
1401 return false;
1402}
1403
Charles Davis1e063d12010-02-12 00:31:15 +00001404/// ParseOptionalStackAlignment
1405/// ::= /* empty */
1406/// ::= 'alignstack' '(' 4 ')'
1407bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1408 Alignment = 0;
1409 if (!EatIfPresent(lltok::kw_alignstack))
1410 return false;
1411 LocTy ParenLoc = Lex.getLoc();
1412 if (!EatIfPresent(lltok::lparen))
1413 return Error(ParenLoc, "expected '('");
1414 LocTy AlignLoc = Lex.getLoc();
1415 if (ParseUInt32(Alignment)) return true;
1416 ParenLoc = Lex.getLoc();
1417 if (!EatIfPresent(lltok::rparen))
1418 return Error(ParenLoc, "expected ')'");
1419 if (!isPowerOf2_32(Alignment))
1420 return Error(AlignLoc, "stack alignment is not a power of two");
1421 return false;
1422}
Devang Patelf633a062009-09-17 23:04:48 +00001423
Chris Lattner628c13a2009-12-30 05:14:00 +00001424/// ParseIndexList - This parses the index list for an insert/extractvalue
1425/// instruction. This sets AteExtraComma in the case where we eat an extra
1426/// comma at the end of the line and find that it is followed by metadata.
1427/// Clients that don't allow metadata can call the version of this function that
1428/// only takes one argument.
1429///
Chris Lattnerdf986172009-01-02 07:01:27 +00001430/// ParseIndexList
1431/// ::= (',' uint32)+
Chris Lattner628c13a2009-12-30 05:14:00 +00001432///
1433bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1434 bool &AteExtraComma) {
1435 AteExtraComma = false;
Michael Ilseman407a6162012-11-15 22:34:00 +00001436
Chris Lattnerdf986172009-01-02 07:01:27 +00001437 if (Lex.getKind() != lltok::comma)
1438 return TokError("expected ',' as start of index list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001439
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001440 while (EatIfPresent(lltok::comma)) {
Chris Lattner628c13a2009-12-30 05:14:00 +00001441 if (Lex.getKind() == lltok::MetadataVar) {
1442 AteExtraComma = true;
1443 return false;
1444 }
Nick Lewycky28815c42010-09-29 23:32:20 +00001445 unsigned Idx = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001446 if (ParseUInt32(Idx)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001447 Indices.push_back(Idx);
1448 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001449
Chris Lattnerdf986172009-01-02 07:01:27 +00001450 return false;
1451}
1452
1453//===----------------------------------------------------------------------===//
1454// Type Parsing.
1455//===----------------------------------------------------------------------===//
1456
Chris Lattner1afcace2011-07-09 17:41:24 +00001457/// ParseType - Parse a type.
1458bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1459 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001460 switch (Lex.getKind()) {
1461 default:
1462 return TokError("expected type");
1463 case lltok::Type:
Chris Lattner1afcace2011-07-09 17:41:24 +00001464 // Type ::= 'float' | 'void' (etc)
Chris Lattnerdf986172009-01-02 07:01:27 +00001465 Result = Lex.getTyVal();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001466 Lex.Lex();
Chris Lattnerdf986172009-01-02 07:01:27 +00001467 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001468 case lltok::lbrace:
Chris Lattner1afcace2011-07-09 17:41:24 +00001469 // Type ::= StructType
1470 if (ParseAnonStructType(Result, false))
Chris Lattnerdf986172009-01-02 07:01:27 +00001471 return true;
1472 break;
1473 case lltok::lsquare:
Chris Lattner1afcace2011-07-09 17:41:24 +00001474 // Type ::= '[' ... ']'
Chris Lattnerdf986172009-01-02 07:01:27 +00001475 Lex.Lex(); // eat the lsquare.
1476 if (ParseArrayVectorType(Result, false))
1477 return true;
1478 break;
1479 case lltok::less: // Either vector or packed struct.
Chris Lattner1afcace2011-07-09 17:41:24 +00001480 // Type ::= '<' ... '>'
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001481 Lex.Lex();
1482 if (Lex.getKind() == lltok::lbrace) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001483 if (ParseAnonStructType(Result, true) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001484 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerdf986172009-01-02 07:01:27 +00001485 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001486 } else if (ParseArrayVectorType(Result, true))
1487 return true;
1488 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001489 case lltok::LocalVar: {
1490 // Type ::= %foo
1491 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman407a6162012-11-15 22:34:00 +00001492
Chris Lattner1afcace2011-07-09 17:41:24 +00001493 // If the type hasn't been defined yet, create a forward definition and
1494 // remember where that forward def'n was seen (in case it never is defined).
1495 if (Entry.first == 0) {
Chris Lattner3ebb6492011-08-12 18:06:37 +00001496 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattner1afcace2011-07-09 17:41:24 +00001497 Entry.second = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001498 }
Chris Lattner1afcace2011-07-09 17:41:24 +00001499 Result = Entry.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001500 Lex.Lex();
1501 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001502 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001503
Chris Lattner1afcace2011-07-09 17:41:24 +00001504 case lltok::LocalVarID: {
1505 // Type ::= %4
1506 if (Lex.getUIntVal() >= NumberedTypes.size())
1507 NumberedTypes.resize(Lex.getUIntVal()+1);
1508 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman407a6162012-11-15 22:34:00 +00001509
Chris Lattner1afcace2011-07-09 17:41:24 +00001510 // If the type hasn't been defined yet, create a forward definition and
1511 // remember where that forward def'n was seen (in case it never is defined).
1512 if (Entry.first == 0) {
Chris Lattner3ebb6492011-08-12 18:06:37 +00001513 Entry.first = StructType::create(Context);
Chris Lattner1afcace2011-07-09 17:41:24 +00001514 Entry.second = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001515 }
Chris Lattner1afcace2011-07-09 17:41:24 +00001516 Result = Entry.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001517 Lex.Lex();
1518 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001519 }
1520 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001521
1522 // Parse the type suffixes.
Chris Lattnerdf986172009-01-02 07:01:27 +00001523 while (1) {
1524 switch (Lex.getKind()) {
1525 // End of type.
Chris Lattner1afcace2011-07-09 17:41:24 +00001526 default:
1527 if (!AllowVoid && Result->isVoidTy())
1528 return Error(TypeLoc, "void type only allowed for function results");
1529 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001530
Chris Lattner1afcace2011-07-09 17:41:24 +00001531 // Type ::= Type '*'
Chris Lattnerdf986172009-01-02 07:01:27 +00001532 case lltok::star:
Chris Lattner1afcace2011-07-09 17:41:24 +00001533 if (Result->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001534 return TokError("basic block pointers are invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001535 if (Result->isVoidTy())
1536 return TokError("pointers to void are invalid - use i8* instead");
1537 if (!PointerType::isValidElementType(Result))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001538 return TokError("pointer to this type is invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001539 Result = PointerType::getUnqual(Result);
Chris Lattnerdf986172009-01-02 07:01:27 +00001540 Lex.Lex();
1541 break;
1542
Chris Lattner1afcace2011-07-09 17:41:24 +00001543 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerdf986172009-01-02 07:01:27 +00001544 case lltok::kw_addrspace: {
Chris Lattner1afcace2011-07-09 17:41:24 +00001545 if (Result->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001546 return TokError("basic block pointers are invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001547 if (Result->isVoidTy())
Dan Gohmanb9070d32009-02-09 17:41:21 +00001548 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattner1afcace2011-07-09 17:41:24 +00001549 if (!PointerType::isValidElementType(Result))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001550 return TokError("pointer to this type is invalid");
Chris Lattnerdf986172009-01-02 07:01:27 +00001551 unsigned AddrSpace;
1552 if (ParseOptionalAddrSpace(AddrSpace) ||
1553 ParseToken(lltok::star, "expected '*' in address space"))
1554 return true;
1555
Chris Lattner1afcace2011-07-09 17:41:24 +00001556 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerdf986172009-01-02 07:01:27 +00001557 break;
1558 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001559
Chris Lattnerdf986172009-01-02 07:01:27 +00001560 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1561 case lltok::lparen:
1562 if (ParseFunctionType(Result))
1563 return true;
1564 break;
1565 }
1566 }
1567}
1568
1569/// ParseParameterList
1570/// ::= '(' ')'
1571/// ::= '(' Arg (',' Arg)* ')'
1572/// Arg
1573/// ::= Type OptionalAttributes Value OptionalAttributes
1574bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1575 PerFunctionState &PFS) {
1576 if (ParseToken(lltok::lparen, "expected '(' in call"))
1577 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001578
Bill Wendling73dee182013-01-31 00:29:54 +00001579 unsigned AttrIndex = 1;
Chris Lattnerdf986172009-01-02 07:01:27 +00001580 while (Lex.getKind() != lltok::rparen) {
1581 // If this isn't the first argument, we need a comma.
1582 if (!ArgList.empty() &&
1583 ParseToken(lltok::comma, "expected ',' in argument list"))
1584 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001585
Chris Lattnerdf986172009-01-02 07:01:27 +00001586 // Parse the argument.
1587 LocTy ArgLoc;
Chris Lattner1afcace2011-07-09 17:41:24 +00001588 Type *ArgTy = 0;
Bill Wendling702cc912012-10-15 20:35:56 +00001589 AttrBuilder ArgAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00001590 Value *V;
Victor Hernandez19715562009-12-03 23:40:58 +00001591 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00001592 return true;
Victor Hernandez19715562009-12-03 23:40:58 +00001593
Chris Lattner287881d2009-12-30 02:11:14 +00001594 // Otherwise, handle normal operands.
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001595 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
Chris Lattner287881d2009-12-30 02:11:14 +00001596 return true;
Bill Wendling73dee182013-01-31 00:29:54 +00001597 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1598 AttrIndex++,
1599 ArgAttrs)));
Chris Lattnerdf986172009-01-02 07:01:27 +00001600 }
1601
1602 Lex.Lex(); // Lex the ')'.
1603 return false;
1604}
1605
1606
1607
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001608/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattner1afcace2011-07-09 17:41:24 +00001609/// prototype.
Chris Lattnerdf986172009-01-02 07:01:27 +00001610/// ::= '(' ArgTypeListI ')'
1611/// ArgTypeListI
1612/// ::= /*empty*/
1613/// ::= '...'
1614/// ::= ArgTypeList ',' '...'
1615/// ::= ArgType (',' ArgType)*
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001616///
Chris Lattner1afcace2011-07-09 17:41:24 +00001617bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1618 bool &isVarArg){
Chris Lattnerdf986172009-01-02 07:01:27 +00001619 isVarArg = false;
1620 assert(Lex.getKind() == lltok::lparen);
1621 Lex.Lex(); // eat the (.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001622
Chris Lattnerdf986172009-01-02 07:01:27 +00001623 if (Lex.getKind() == lltok::rparen) {
1624 // empty
1625 } else if (Lex.getKind() == lltok::dotdotdot) {
1626 isVarArg = true;
1627 Lex.Lex();
1628 } else {
1629 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001630 Type *ArgTy = 0;
Bill Wendling702cc912012-10-15 20:35:56 +00001631 AttrBuilder Attrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00001632 std::string Name;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001633
Chris Lattner1afcace2011-07-09 17:41:24 +00001634 if (ParseType(ArgTy) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001635 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001636
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001637 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001638 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001639
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00001640 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001641 Name = Lex.getStrVal();
1642 Lex.Lex();
1643 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001644
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001645 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001646 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001647
Bill Wendling73dee182013-01-31 00:29:54 +00001648 unsigned AttrIndex = 1;
Bill Wendlingcb3de0b2012-10-15 04:46:55 +00001649 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendling73dee182013-01-31 00:29:54 +00001650 AttributeSet::get(ArgTy->getContext(),
1651 AttrIndex++, Attrs), Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001652
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001653 while (EatIfPresent(lltok::comma)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001654 // Handle ... at end of arg list.
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001655 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001656 isVarArg = true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001657 break;
1658 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001659
Chris Lattnerdf986172009-01-02 07:01:27 +00001660 // Otherwise must be an argument type.
1661 TypeLoc = Lex.getLoc();
Bill Wendlinge01b81b2012-12-04 23:40:58 +00001662 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001663
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001664 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001665 return Error(TypeLoc, "argument can not have void type");
1666
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00001667 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001668 Name = Lex.getStrVal();
1669 Lex.Lex();
1670 } else {
1671 Name = "";
1672 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001673
Chris Lattner1afcace2011-07-09 17:41:24 +00001674 if (!ArgTy->isFirstClassType())
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001675 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001676
Bill Wendlingcb3de0b2012-10-15 04:46:55 +00001677 ArgList.push_back(ArgInfo(TypeLoc, ArgTy,
Bill Wendling73dee182013-01-31 00:29:54 +00001678 AttributeSet::get(ArgTy->getContext(),
1679 AttrIndex++, Attrs),
Bill Wendlingcb3de0b2012-10-15 04:46:55 +00001680 Name));
Chris Lattnerdf986172009-01-02 07:01:27 +00001681 }
1682 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001683
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001684 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerdf986172009-01-02 07:01:27 +00001685}
Daniel Dunbara279bc32009-09-20 02:20:51 +00001686
Chris Lattnerdf986172009-01-02 07:01:27 +00001687/// ParseFunctionType
1688/// ::= Type ArgumentList OptionalAttrs
Chris Lattner1afcace2011-07-09 17:41:24 +00001689bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001690 assert(Lex.getKind() == lltok::lparen);
1691
Chris Lattnerd77d04c2009-01-05 08:04:33 +00001692 if (!FunctionType::isValidReturnType(Result))
1693 return TokError("invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001694
Chris Lattner1afcace2011-07-09 17:41:24 +00001695 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerdf986172009-01-02 07:01:27 +00001696 bool isVarArg;
Chris Lattner1afcace2011-07-09 17:41:24 +00001697 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerdf986172009-01-02 07:01:27 +00001698 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001699
Chris Lattnerdf986172009-01-02 07:01:27 +00001700 // Reject names on the arguments lists.
1701 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1702 if (!ArgList[i].Name.empty())
1703 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendling73dee182013-01-31 00:29:54 +00001704 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattnera16546a2011-06-17 17:37:13 +00001705 return Error(ArgList[i].Loc,
1706 "argument attributes invalid in function type");
Chris Lattnerdf986172009-01-02 07:01:27 +00001707 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001708
Jay Foad5fdd6c82011-07-12 14:06:48 +00001709 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerdf986172009-01-02 07:01:27 +00001710 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattner1afcace2011-07-09 17:41:24 +00001711 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001712
Chris Lattner1afcace2011-07-09 17:41:24 +00001713 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerdf986172009-01-02 07:01:27 +00001714 return false;
1715}
1716
Chris Lattner1afcace2011-07-09 17:41:24 +00001717/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1718/// other structs.
1719bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1720 SmallVector<Type*, 8> Elts;
1721 if (ParseStructBody(Elts)) return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00001722
Chris Lattner1afcace2011-07-09 17:41:24 +00001723 Result = StructType::get(Context, Elts, Packed);
1724 return false;
1725}
1726
1727/// ParseStructDefinition - Parse a struct in a 'type' definition.
1728bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1729 std::pair<Type*, LocTy> &Entry,
1730 Type *&ResultTy) {
1731 // If the type was already defined, diagnose the redefinition.
1732 if (Entry.first && !Entry.second.isValid())
1733 return Error(TypeLoc, "redefinition of type");
Michael Ilseman407a6162012-11-15 22:34:00 +00001734
Chris Lattner1afcace2011-07-09 17:41:24 +00001735 // If we have opaque, just return without filling in the definition for the
1736 // struct. This counts as a definition as far as the .ll file goes.
1737 if (EatIfPresent(lltok::kw_opaque)) {
1738 // This type is being defined, so clear the location to indicate this.
1739 Entry.second = SMLoc();
Michael Ilseman407a6162012-11-15 22:34:00 +00001740
Chris Lattner1afcace2011-07-09 17:41:24 +00001741 // If this type number has never been uttered, create it.
1742 if (Entry.first == 0)
Chris Lattner3ebb6492011-08-12 18:06:37 +00001743 Entry.first = StructType::create(Context, Name);
Chris Lattner1afcace2011-07-09 17:41:24 +00001744 ResultTy = Entry.first;
1745 return false;
1746 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001747
Chris Lattner1afcace2011-07-09 17:41:24 +00001748 // If the type starts with '<', then it is either a packed struct or a vector.
1749 bool isPacked = EatIfPresent(lltok::less);
1750
1751 // If we don't have a struct, then we have a random type alias, which we
1752 // accept for compatibility with old files. These types are not allowed to be
1753 // forward referenced and not allowed to be recursive.
1754 if (Lex.getKind() != lltok::lbrace) {
1755 if (Entry.first)
1756 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman407a6162012-11-15 22:34:00 +00001757
Chris Lattner1afcace2011-07-09 17:41:24 +00001758 ResultTy = 0;
1759 if (isPacked)
1760 return ParseArrayVectorType(ResultTy, true);
1761 return ParseType(ResultTy);
1762 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001763
Chris Lattner1afcace2011-07-09 17:41:24 +00001764 // This type is being defined, so clear the location to indicate this.
1765 Entry.second = SMLoc();
Michael Ilseman407a6162012-11-15 22:34:00 +00001766
Chris Lattner1afcace2011-07-09 17:41:24 +00001767 // If this type number has never been uttered, create it.
1768 if (Entry.first == 0)
Chris Lattner3ebb6492011-08-12 18:06:37 +00001769 Entry.first = StructType::create(Context, Name);
Michael Ilseman407a6162012-11-15 22:34:00 +00001770
Chris Lattner1afcace2011-07-09 17:41:24 +00001771 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman407a6162012-11-15 22:34:00 +00001772
Chris Lattner1afcace2011-07-09 17:41:24 +00001773 SmallVector<Type*, 8> Body;
1774 if (ParseStructBody(Body) ||
1775 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1776 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00001777
Chris Lattner1afcace2011-07-09 17:41:24 +00001778 STy->setBody(Body, isPacked);
1779 ResultTy = STy;
1780 return false;
1781}
1782
1783
Chris Lattnerdf986172009-01-02 07:01:27 +00001784/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattner1afcace2011-07-09 17:41:24 +00001785/// StructType
Chris Lattnerdf986172009-01-02 07:01:27 +00001786/// ::= '{' '}'
Chris Lattner1afcace2011-07-09 17:41:24 +00001787/// ::= '{' Type (',' Type)* '}'
Chris Lattnerdf986172009-01-02 07:01:27 +00001788/// ::= '<' '{' '}' '>'
Chris Lattner1afcace2011-07-09 17:41:24 +00001789/// ::= '<' '{' Type (',' Type)* '}' '>'
1790bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001791 assert(Lex.getKind() == lltok::lbrace);
1792 Lex.Lex(); // Consume the '{'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001793
Chris Lattner1afcace2011-07-09 17:41:24 +00001794 // Handle the empty struct.
1795 if (EatIfPresent(lltok::rbrace))
Chris Lattnerdf986172009-01-02 07:01:27 +00001796 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001797
Chris Lattnera9a9e072009-03-09 04:49:14 +00001798 LocTy EltTyLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001799 Type *Ty = 0;
1800 if (ParseType(Ty)) return true;
1801 Body.push_back(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001802
Chris Lattner1afcace2011-07-09 17:41:24 +00001803 if (!StructType::isValidElementType(Ty))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001804 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001805
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001806 while (EatIfPresent(lltok::comma)) {
Chris Lattnera9a9e072009-03-09 04:49:14 +00001807 EltTyLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001808 if (ParseType(Ty)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001809
Chris Lattner1afcace2011-07-09 17:41:24 +00001810 if (!StructType::isValidElementType(Ty))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001811 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001812
Chris Lattner1afcace2011-07-09 17:41:24 +00001813 Body.push_back(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00001814 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001815
Chris Lattner1afcace2011-07-09 17:41:24 +00001816 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerdf986172009-01-02 07:01:27 +00001817}
1818
1819/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1820/// token has already been consumed.
Chris Lattner1afcace2011-07-09 17:41:24 +00001821/// Type
Chris Lattnerdf986172009-01-02 07:01:27 +00001822/// ::= '[' APSINTVAL 'x' Types ']'
1823/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattner1afcace2011-07-09 17:41:24 +00001824bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001825 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1826 Lex.getAPSIntVal().getBitWidth() > 64)
1827 return TokError("expected number in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001828
Chris Lattnerdf986172009-01-02 07:01:27 +00001829 LocTy SizeLoc = Lex.getLoc();
1830 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001831 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001832
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001833 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1834 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001835
1836 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001837 Type *EltTy = 0;
1838 if (ParseType(EltTy)) return true;
Chris Lattnera9a9e072009-03-09 04:49:14 +00001839
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001840 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
1841 "expected end of sequential type"))
1842 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001843
Chris Lattnerdf986172009-01-02 07:01:27 +00001844 if (isVector) {
Chris Lattner452e2622009-02-28 18:12:41 +00001845 if (Size == 0)
1846 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerdf986172009-01-02 07:01:27 +00001847 if ((unsigned)Size != Size)
1848 return Error(SizeLoc, "size too large for vector");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001849 if (!VectorType::isValidElementType(EltTy))
Duncan Sands2333e292012-11-13 12:59:33 +00001850 return Error(TypeLoc, "invalid vector element type");
Owen Andersondebcb012009-07-29 22:17:13 +00001851 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerdf986172009-01-02 07:01:27 +00001852 } else {
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001853 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001854 return Error(TypeLoc, "invalid array element type");
Chris Lattner1afcace2011-07-09 17:41:24 +00001855 Result = ArrayType::get(EltTy, Size);
Chris Lattnerdf986172009-01-02 07:01:27 +00001856 }
1857 return false;
1858}
1859
1860//===----------------------------------------------------------------------===//
1861// Function Semantic Analysis.
1862//===----------------------------------------------------------------------===//
1863
Chris Lattner09d9ef42009-10-28 03:39:23 +00001864LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
1865 int functionNumber)
1866 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001867
1868 // Insert unnamed arguments into the NumberedVals list.
1869 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1870 AI != E; ++AI)
1871 if (!AI->hasName())
1872 NumberedVals.push_back(AI);
1873}
1874
1875LLParser::PerFunctionState::~PerFunctionState() {
1876 // If there were any forward referenced non-basicblock values, delete them.
1877 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1878 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1879 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001880 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001881 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001882 delete I->second.first;
1883 I->second.first = 0;
1884 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001885
Chris Lattnerdf986172009-01-02 07:01:27 +00001886 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1887 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1888 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001889 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001890 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001891 delete I->second.first;
1892 I->second.first = 0;
1893 }
1894}
1895
Chris Lattner09d9ef42009-10-28 03:39:23 +00001896bool LLParser::PerFunctionState::FinishFunction() {
1897 // Check to see if someone took the address of labels in this block.
1898 if (!P.ForwardRefBlockAddresses.empty()) {
1899 ValID FunctionID;
1900 if (!F.getName().empty()) {
1901 FunctionID.Kind = ValID::t_GlobalName;
1902 FunctionID.StrVal = F.getName();
1903 } else {
1904 FunctionID.Kind = ValID::t_GlobalID;
1905 FunctionID.UIntVal = FunctionNumber;
1906 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001907
Chris Lattner09d9ef42009-10-28 03:39:23 +00001908 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
1909 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
1910 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
1911 // Resolve all these references.
1912 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
1913 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00001914
Chris Lattner09d9ef42009-10-28 03:39:23 +00001915 P.ForwardRefBlockAddresses.erase(FRBAI);
1916 }
1917 }
Michael Ilseman407a6162012-11-15 22:34:00 +00001918
Chris Lattnerdf986172009-01-02 07:01:27 +00001919 if (!ForwardRefVals.empty())
1920 return P.Error(ForwardRefVals.begin()->second.second,
1921 "use of undefined value '%" + ForwardRefVals.begin()->first +
1922 "'");
1923 if (!ForwardRefValIDs.empty())
1924 return P.Error(ForwardRefValIDs.begin()->second.second,
1925 "use of undefined value '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001926 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001927 return false;
1928}
1929
1930
1931/// GetVal - Get a value with the specified name or ID, creating a
1932/// forward reference record if needed. This can return null if the value
1933/// exists but does not have the right type.
1934Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001935 Type *Ty, LocTy Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001936 // Look this name up in the normal function symbol table.
1937 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001938
Chris Lattnerdf986172009-01-02 07:01:27 +00001939 // If this is a forward reference for the value, see if we already created a
1940 // forward ref record.
1941 if (Val == 0) {
1942 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1943 I = ForwardRefVals.find(Name);
1944 if (I != ForwardRefVals.end())
1945 Val = I->second.first;
1946 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001947
Chris Lattnerdf986172009-01-02 07:01:27 +00001948 // If we have the value in the symbol table or fwd-ref table, return it.
1949 if (Val) {
1950 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001951 if (Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001952 P.Error(Loc, "'%" + Name + "' is not a basic block");
1953 else
1954 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001955 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001956 return 0;
1957 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001958
Chris Lattnerdf986172009-01-02 07:01:27 +00001959 // Don't make placeholders with invalid type.
Chris Lattner1afcace2011-07-09 17:41:24 +00001960 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001961 P.Error(Loc, "invalid use of a non-first-class type");
1962 return 0;
1963 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001964
Chris Lattnerdf986172009-01-02 07:01:27 +00001965 // Otherwise, create a new forward reference for this value and remember it.
1966 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001967 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001968 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001969 else
1970 FwdVal = new Argument(Ty, Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001971
Chris Lattnerdf986172009-01-02 07:01:27 +00001972 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1973 return FwdVal;
1974}
1975
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001976Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerdf986172009-01-02 07:01:27 +00001977 LocTy Loc) {
1978 // Look this name up in the normal function symbol table.
1979 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001980
Chris Lattnerdf986172009-01-02 07:01:27 +00001981 // If this is a forward reference for the value, see if we already created a
1982 // forward ref record.
1983 if (Val == 0) {
1984 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1985 I = ForwardRefValIDs.find(ID);
1986 if (I != ForwardRefValIDs.end())
1987 Val = I->second.first;
1988 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001989
Chris Lattnerdf986172009-01-02 07:01:27 +00001990 // If we have the value in the symbol table or fwd-ref table, return it.
1991 if (Val) {
1992 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001993 if (Ty->isLabelTy())
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001994 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerdf986172009-01-02 07:01:27 +00001995 else
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001996 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001997 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001998 return 0;
1999 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002000
Chris Lattner1afcace2011-07-09 17:41:24 +00002001 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002002 P.Error(Loc, "invalid use of a non-first-class type");
2003 return 0;
2004 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002005
Chris Lattnerdf986172009-01-02 07:01:27 +00002006 // Otherwise, create a new forward reference for this value and remember it.
2007 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002008 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00002009 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00002010 else
2011 FwdVal = new Argument(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002012
Chris Lattnerdf986172009-01-02 07:01:27 +00002013 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2014 return FwdVal;
2015}
2016
2017/// SetInstName - After an instruction is parsed and inserted into its
2018/// basic block, this installs its name.
2019bool LLParser::PerFunctionState::SetInstName(int NameID,
2020 const std::string &NameStr,
2021 LocTy NameLoc, Instruction *Inst) {
2022 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002023 if (Inst->getType()->isVoidTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002024 if (NameID != -1 || !NameStr.empty())
2025 return P.Error(NameLoc, "instructions returning void cannot have a name");
2026 return false;
2027 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002028
Chris Lattnerdf986172009-01-02 07:01:27 +00002029 // If this was a numbered instruction, verify that the instruction is the
2030 // expected value and resolve any forward references.
2031 if (NameStr.empty()) {
2032 // If neither a name nor an ID was specified, just use the next ID.
2033 if (NameID == -1)
2034 NameID = NumberedVals.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002035
Chris Lattnerdf986172009-01-02 07:01:27 +00002036 if (unsigned(NameID) != NumberedVals.size())
2037 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002038 Twine(NumberedVals.size()) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002039
Chris Lattnerdf986172009-01-02 07:01:27 +00002040 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2041 ForwardRefValIDs.find(NameID);
2042 if (FI != ForwardRefValIDs.end()) {
2043 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002044 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002045 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002046 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2b4f28e2009-09-02 15:02:57 +00002047 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00002048 ForwardRefValIDs.erase(FI);
2049 }
2050
2051 NumberedVals.push_back(Inst);
2052 return false;
2053 }
2054
2055 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2056 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2057 FI = ForwardRefVals.find(NameStr);
2058 if (FI != ForwardRefVals.end()) {
2059 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002060 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002061 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002062 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes531552a2009-09-02 14:22:03 +00002063 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00002064 ForwardRefVals.erase(FI);
2065 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002066
Chris Lattnerdf986172009-01-02 07:01:27 +00002067 // Set the name on the instruction.
2068 Inst->setName(NameStr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002069
Benjamin Krameraf812352010-10-16 11:28:23 +00002070 if (Inst->getName() != NameStr)
Daniel Dunbara279bc32009-09-20 02:20:51 +00002071 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00002072 NameStr + "'");
2073 return false;
2074}
2075
2076/// GetBB - Get a basic block with the specified name or ID, creating a
2077/// forward reference record if needed.
2078BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2079 LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002080 return cast_or_null<BasicBlock>(GetVal(Name,
2081 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00002082}
2083
2084BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002085 return cast_or_null<BasicBlock>(GetVal(ID,
2086 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00002087}
2088
2089/// DefineBB - Define the specified basic block, which is either named or
2090/// unnamed. If there is an error, this returns null otherwise it returns
2091/// the block being defined.
2092BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2093 LocTy Loc) {
2094 BasicBlock *BB;
2095 if (Name.empty())
2096 BB = GetBB(NumberedVals.size(), Loc);
2097 else
2098 BB = GetBB(Name, Loc);
2099 if (BB == 0) return 0; // Already diagnosed error.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002100
Chris Lattnerdf986172009-01-02 07:01:27 +00002101 // Move the block to the end of the function. Forward ref'd blocks are
2102 // inserted wherever they happen to be referenced.
2103 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002104
Chris Lattnerdf986172009-01-02 07:01:27 +00002105 // Remove the block from forward ref sets.
2106 if (Name.empty()) {
2107 ForwardRefValIDs.erase(NumberedVals.size());
2108 NumberedVals.push_back(BB);
2109 } else {
2110 // BB forward references are already in the function symbol table.
2111 ForwardRefVals.erase(Name);
2112 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002113
Chris Lattnerdf986172009-01-02 07:01:27 +00002114 return BB;
2115}
2116
2117//===----------------------------------------------------------------------===//
2118// Constants.
2119//===----------------------------------------------------------------------===//
2120
2121/// ParseValID - Parse an abstract value that doesn't necessarily have a
2122/// type implied. For example, if we parse "4" we don't know what integer type
2123/// it has. The value will later be combined with its type and checked for
Victor Hernandez24e64df2010-01-10 07:14:18 +00002124/// sanity. PFS is used to convert function-local operands of metadata (since
2125/// metadata operands are not just parsed here but also converted to values).
2126/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezbf170d42010-01-05 22:22:14 +00002127bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002128 ID.Loc = Lex.getLoc();
2129 switch (Lex.getKind()) {
2130 default: return TokError("expected value token");
2131 case lltok::GlobalID: // @42
2132 ID.UIntVal = Lex.getUIntVal();
2133 ID.Kind = ValID::t_GlobalID;
2134 break;
2135 case lltok::GlobalVar: // @foo
2136 ID.StrVal = Lex.getStrVal();
2137 ID.Kind = ValID::t_GlobalName;
2138 break;
2139 case lltok::LocalVarID: // %42
2140 ID.UIntVal = Lex.getUIntVal();
2141 ID.Kind = ValID::t_LocalID;
2142 break;
2143 case lltok::LocalVar: // %foo
Chris Lattnerdf986172009-01-02 07:01:27 +00002144 ID.StrVal = Lex.getStrVal();
2145 ID.Kind = ValID::t_LocalName;
2146 break;
Dan Gohman83448032010-07-14 18:26:50 +00002147 case lltok::exclaim: // !42, !{...}, or !"foo"
2148 return ParseMetadataValue(ID, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002149 case lltok::APSInt:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002150 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002151 ID.Kind = ValID::t_APSInt;
2152 break;
2153 case lltok::APFloat:
2154 ID.APFloatVal = Lex.getAPFloatVal();
2155 ID.Kind = ValID::t_APFloat;
2156 break;
2157 case lltok::kw_true:
Owen Anderson5defacc2009-07-31 17:39:07 +00002158 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002159 ID.Kind = ValID::t_Constant;
2160 break;
2161 case lltok::kw_false:
Owen Anderson5defacc2009-07-31 17:39:07 +00002162 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002163 ID.Kind = ValID::t_Constant;
2164 break;
2165 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2166 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2167 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002168
Chris Lattnerdf986172009-01-02 07:01:27 +00002169 case lltok::lbrace: {
2170 // ValID ::= '{' ConstVector '}'
2171 Lex.Lex();
2172 SmallVector<Constant*, 16> Elts;
2173 if (ParseGlobalValueVector(Elts) ||
2174 ParseToken(lltok::rbrace, "expected end of struct constant"))
2175 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002176
Chris Lattner1afcace2011-07-09 17:41:24 +00002177 ID.ConstantStructElts = new Constant*[Elts.size()];
2178 ID.UIntVal = Elts.size();
2179 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2180 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerdf986172009-01-02 07:01:27 +00002181 return false;
2182 }
2183 case lltok::less: {
2184 // ValID ::= '<' ConstVector '>' --> Vector.
2185 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2186 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002187 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002188
Chris Lattnerdf986172009-01-02 07:01:27 +00002189 SmallVector<Constant*, 16> Elts;
2190 LocTy FirstEltLoc = Lex.getLoc();
2191 if (ParseGlobalValueVector(Elts) ||
2192 (isPackedStruct &&
2193 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2194 ParseToken(lltok::greater, "expected end of constant"))
2195 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002196
Chris Lattnerdf986172009-01-02 07:01:27 +00002197 if (isPackedStruct) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002198 ID.ConstantStructElts = new Constant*[Elts.size()];
2199 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
2200 ID.UIntVal = Elts.size();
2201 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerdf986172009-01-02 07:01:27 +00002202 return false;
2203 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002204
Chris Lattnerdf986172009-01-02 07:01:27 +00002205 if (Elts.empty())
2206 return Error(ID.Loc, "constant vector must not be empty");
2207
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002208 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem16087692011-12-05 06:29:09 +00002209 !Elts[0]->getType()->isFloatingPointTy() &&
2210 !Elts[0]->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002211 return Error(FirstEltLoc,
Nadav Rotem16087692011-12-05 06:29:09 +00002212 "vector elements must have integer, pointer or floating point type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002213
Chris Lattnerdf986172009-01-02 07:01:27 +00002214 // Verify that all the vector elements have the same type.
2215 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2216 if (Elts[i]->getType() != Elts[0]->getType())
2217 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002218 "vector element #" + Twine(i) +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002219 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002220
Chris Lattner2ca5c862011-02-15 00:14:00 +00002221 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00002222 ID.Kind = ValID::t_Constant;
2223 return false;
2224 }
2225 case lltok::lsquare: { // Array Constant
2226 Lex.Lex();
2227 SmallVector<Constant*, 16> Elts;
2228 LocTy FirstEltLoc = Lex.getLoc();
2229 if (ParseGlobalValueVector(Elts) ||
2230 ParseToken(lltok::rsquare, "expected end of array constant"))
2231 return true;
2232
2233 // Handle empty element.
2234 if (Elts.empty()) {
2235 // Use undef instead of an array because it's inconvenient to determine
2236 // the element type at this point, there being no elements to examine.
Chris Lattner081b5052009-01-05 07:52:51 +00002237 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerdf986172009-01-02 07:01:27 +00002238 return false;
2239 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002240
Chris Lattnerdf986172009-01-02 07:01:27 +00002241 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002242 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002243 getTypeString(Elts[0]->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002244
Owen Andersondebcb012009-07-29 22:17:13 +00002245 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002246
Chris Lattnerdf986172009-01-02 07:01:27 +00002247 // Verify all elements are correct type!
Chris Lattner6d6b3cc2009-01-02 08:49:06 +00002248 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002249 if (Elts[i]->getType() != Elts[0]->getType())
2250 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002251 "array element #" + Twine(i) +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002252 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00002253 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002254
Jay Foad26701082011-06-22 09:24:39 +00002255 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00002256 ID.Kind = ValID::t_Constant;
2257 return false;
2258 }
2259 case lltok::kw_c: // c "foo"
2260 Lex.Lex();
Chris Lattner18c7f802012-02-05 02:29:43 +00002261 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2262 false);
Chris Lattnerdf986172009-01-02 07:01:27 +00002263 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2264 ID.Kind = ValID::t_Constant;
2265 return false;
2266
2267 case lltok::kw_asm: {
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002268 // ValID ::= 'asm' SideEffect? AlignStack? STRINGCONSTANT ',' STRINGCONSTANT
Chad Rosier581600b2012-09-05 19:00:49 +00002269 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerdf986172009-01-02 07:01:27 +00002270 Lex.Lex();
2271 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002272 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosier581600b2012-09-05 19:00:49 +00002273 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002274 ParseStringConstant(ID.StrVal) ||
2275 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002276 ParseToken(lltok::StringConstant, "expected constraint string"))
2277 return true;
2278 ID.StrVal2 = Lex.getStrVal();
Chad Rosier36547342012-09-05 00:08:17 +00002279 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosier581600b2012-09-05 19:00:49 +00002280 (unsigned(AsmDialect)<<2);
Chris Lattnerdf986172009-01-02 07:01:27 +00002281 ID.Kind = ValID::t_InlineAsm;
2282 return false;
2283 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002284
Chris Lattner09d9ef42009-10-28 03:39:23 +00002285 case lltok::kw_blockaddress: {
2286 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2287 Lex.Lex();
2288
2289 ValID Fn, Label;
2290 LocTy FnLoc, LabelLoc;
Michael Ilseman407a6162012-11-15 22:34:00 +00002291
Chris Lattner09d9ef42009-10-28 03:39:23 +00002292 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2293 ParseValID(Fn) ||
2294 ParseToken(lltok::comma, "expected comma in block address expression")||
2295 ParseValID(Label) ||
2296 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2297 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00002298
Chris Lattner09d9ef42009-10-28 03:39:23 +00002299 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2300 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattnercdfc9402009-11-01 01:27:45 +00002301 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner09d9ef42009-10-28 03:39:23 +00002302 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman407a6162012-11-15 22:34:00 +00002303
Chris Lattner09d9ef42009-10-28 03:39:23 +00002304 // Make a global variable as a placeholder for this reference.
2305 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2306 false, GlobalValue::InternalLinkage,
2307 0, "");
2308 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2309 ID.ConstantVal = FwdRef;
2310 ID.Kind = ValID::t_Constant;
2311 return false;
2312 }
Michael Ilseman407a6162012-11-15 22:34:00 +00002313
Chris Lattnerdf986172009-01-02 07:01:27 +00002314 case lltok::kw_trunc:
2315 case lltok::kw_zext:
2316 case lltok::kw_sext:
2317 case lltok::kw_fptrunc:
2318 case lltok::kw_fpext:
2319 case lltok::kw_bitcast:
2320 case lltok::kw_uitofp:
2321 case lltok::kw_sitofp:
2322 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002323 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00002324 case lltok::kw_inttoptr:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002325 case lltok::kw_ptrtoint: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002326 unsigned Opc = Lex.getUIntVal();
Chris Lattner1afcace2011-07-09 17:41:24 +00002327 Type *DestTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002328 Constant *SrcVal;
2329 Lex.Lex();
2330 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2331 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman24b108b2009-06-15 21:52:11 +00002332 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002333 ParseType(DestTy) ||
2334 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2335 return true;
2336 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2337 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002338 getTypeString(SrcVal->getType()) + "' to '" +
2339 getTypeString(DestTy) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002340 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Andersonfba933c2009-07-01 23:57:11 +00002341 SrcVal, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00002342 ID.Kind = ValID::t_Constant;
2343 return false;
2344 }
2345 case lltok::kw_extractvalue: {
2346 Lex.Lex();
2347 Constant *Val;
2348 SmallVector<unsigned, 4> Indices;
2349 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2350 ParseGlobalTypeAndValue(Val) ||
2351 ParseIndexList(Indices) ||
2352 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2353 return true;
Devang Patele8bc45a2009-11-03 19:06:07 +00002354
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002355 if (!Val->getType()->isAggregateType())
2356 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002357 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002358 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002359 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerdf986172009-01-02 07:01:27 +00002360 ID.Kind = ValID::t_Constant;
2361 return false;
2362 }
2363 case lltok::kw_insertvalue: {
2364 Lex.Lex();
2365 Constant *Val0, *Val1;
2366 SmallVector<unsigned, 4> Indices;
2367 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2368 ParseGlobalTypeAndValue(Val0) ||
2369 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2370 ParseGlobalTypeAndValue(Val1) ||
2371 ParseIndexList(Indices) ||
2372 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2373 return true;
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002374 if (!Val0->getType()->isAggregateType())
2375 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002376 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002377 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002378 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerdf986172009-01-02 07:01:27 +00002379 ID.Kind = ValID::t_Constant;
2380 return false;
2381 }
2382 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002383 case lltok::kw_fcmp: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002384 unsigned PredVal, Opc = Lex.getUIntVal();
2385 Constant *Val0, *Val1;
2386 Lex.Lex();
2387 if (ParseCmpPredicate(PredVal, Opc) ||
2388 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2389 ParseGlobalTypeAndValue(Val0) ||
2390 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2391 ParseGlobalTypeAndValue(Val1) ||
2392 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2393 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002394
Chris Lattnerdf986172009-01-02 07:01:27 +00002395 if (Val0->getType() != Val1->getType())
2396 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002397
Chris Lattnerdf986172009-01-02 07:01:27 +00002398 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002399
Chris Lattnerdf986172009-01-02 07:01:27 +00002400 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002401 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002402 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002403 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002404 } else {
2405 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002406 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem16087692011-12-05 06:29:09 +00002407 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002408 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002409 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002410 }
2411 ID.Kind = ValID::t_Constant;
2412 return false;
2413 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002414
Chris Lattnerdf986172009-01-02 07:01:27 +00002415 // Binary Operators.
2416 case lltok::kw_add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002417 case lltok::kw_fadd:
Chris Lattnerdf986172009-01-02 07:01:27 +00002418 case lltok::kw_sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002419 case lltok::kw_fsub:
Chris Lattnerdf986172009-01-02 07:01:27 +00002420 case lltok::kw_mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002421 case lltok::kw_fmul:
Chris Lattnerdf986172009-01-02 07:01:27 +00002422 case lltok::kw_udiv:
2423 case lltok::kw_sdiv:
2424 case lltok::kw_fdiv:
2425 case lltok::kw_urem:
2426 case lltok::kw_srem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002427 case lltok::kw_frem:
2428 case lltok::kw_shl:
2429 case lltok::kw_lshr:
2430 case lltok::kw_ashr: {
Dan Gohman59858cf2009-07-27 16:11:46 +00002431 bool NUW = false;
2432 bool NSW = false;
2433 bool Exact = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002434 unsigned Opc = Lex.getUIntVal();
2435 Constant *Val0, *Val1;
2436 Lex.Lex();
Dan Gohman59858cf2009-07-27 16:11:46 +00002437 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnerf067d582011-02-07 16:40:21 +00002438 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2439 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002440 if (EatIfPresent(lltok::kw_nuw))
2441 NUW = true;
2442 if (EatIfPresent(lltok::kw_nsw)) {
2443 NSW = true;
2444 if (EatIfPresent(lltok::kw_nuw))
2445 NUW = true;
2446 }
Chris Lattnerf067d582011-02-07 16:40:21 +00002447 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2448 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002449 if (EatIfPresent(lltok::kw_exact))
2450 Exact = true;
2451 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002452 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2453 ParseGlobalTypeAndValue(Val0) ||
2454 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2455 ParseGlobalTypeAndValue(Val1) ||
2456 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2457 return true;
2458 if (Val0->getType() != Val1->getType())
2459 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002460 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002461 if (NUW)
2462 return Error(ModifierLoc, "nuw only applies to integer operations");
2463 if (NSW)
2464 return Error(ModifierLoc, "nsw only applies to integer operations");
2465 }
Dan Gohman1eaac532010-05-03 22:44:19 +00002466 // Check that the type is valid for the operator.
2467 switch (Opc) {
2468 case Instruction::Add:
2469 case Instruction::Sub:
2470 case Instruction::Mul:
2471 case Instruction::UDiv:
2472 case Instruction::SDiv:
2473 case Instruction::URem:
2474 case Instruction::SRem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002475 case Instruction::Shl:
2476 case Instruction::AShr:
2477 case Instruction::LShr:
Dan Gohman1eaac532010-05-03 22:44:19 +00002478 if (!Val0->getType()->isIntOrIntVectorTy())
2479 return Error(ID.Loc, "constexpr requires integer operands");
2480 break;
2481 case Instruction::FAdd:
2482 case Instruction::FSub:
2483 case Instruction::FMul:
2484 case Instruction::FDiv:
2485 case Instruction::FRem:
2486 if (!Val0->getType()->isFPOrFPVectorTy())
2487 return Error(ID.Loc, "constexpr requires fp operands");
2488 break;
2489 default: llvm_unreachable("Unknown binary operator!");
2490 }
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002491 unsigned Flags = 0;
2492 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2493 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00002494 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002495 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman59858cf2009-07-27 16:11:46 +00002496 ID.ConstantVal = C;
Chris Lattnerdf986172009-01-02 07:01:27 +00002497 ID.Kind = ValID::t_Constant;
2498 return false;
2499 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002500
Chris Lattnerdf986172009-01-02 07:01:27 +00002501 // Logical Operations
Chris Lattnerdf986172009-01-02 07:01:27 +00002502 case lltok::kw_and:
2503 case lltok::kw_or:
2504 case lltok::kw_xor: {
2505 unsigned Opc = Lex.getUIntVal();
2506 Constant *Val0, *Val1;
2507 Lex.Lex();
2508 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2509 ParseGlobalTypeAndValue(Val0) ||
2510 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2511 ParseGlobalTypeAndValue(Val1) ||
2512 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2513 return true;
2514 if (Val0->getType() != Val1->getType())
2515 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002516 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002517 return Error(ID.Loc,
2518 "constexpr requires integer or integer vector operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002519 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002520 ID.Kind = ValID::t_Constant;
2521 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002522 }
2523
Chris Lattnerdf986172009-01-02 07:01:27 +00002524 case lltok::kw_getelementptr:
2525 case lltok::kw_shufflevector:
2526 case lltok::kw_insertelement:
2527 case lltok::kw_extractelement:
2528 case lltok::kw_select: {
2529 unsigned Opc = Lex.getUIntVal();
2530 SmallVector<Constant*, 16> Elts;
Dan Gohmandd8004d2009-07-27 21:53:46 +00002531 bool InBounds = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002532 Lex.Lex();
Dan Gohmandd8004d2009-07-27 21:53:46 +00002533 if (Opc == Instruction::GetElementPtr)
Dan Gohmandcb40a32009-07-29 15:58:36 +00002534 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002535 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2536 ParseGlobalValueVector(Elts) ||
2537 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2538 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002539
Chris Lattnerdf986172009-01-02 07:01:27 +00002540 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem16087692011-12-05 06:29:09 +00002541 if (Elts.size() == 0 ||
2542 !Elts[0]->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002543 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002544
Jay Foaddab3d292011-07-21 14:31:17 +00002545 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foada9203102011-07-25 09:48:08 +00002546 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002547 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad4b5e2072011-07-21 15:15:37 +00002548 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2549 InBounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002550 } else if (Opc == Instruction::Select) {
2551 if (Elts.size() != 3)
2552 return Error(ID.Loc, "expected three operands to select");
2553 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2554 Elts[2]))
2555 return Error(ID.Loc, Reason);
Owen Andersonbaf3c402009-07-29 18:55:55 +00002556 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002557 } else if (Opc == Instruction::ShuffleVector) {
2558 if (Elts.size() != 3)
2559 return Error(ID.Loc, "expected three operands to shufflevector");
2560 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2561 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Andersonfba933c2009-07-01 23:57:11 +00002562 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002563 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002564 } else if (Opc == Instruction::ExtractElement) {
2565 if (Elts.size() != 2)
2566 return Error(ID.Loc, "expected two operands to extractelement");
2567 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2568 return Error(ID.Loc, "invalid extractelement operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002569 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002570 } else {
2571 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2572 if (Elts.size() != 3)
2573 return Error(ID.Loc, "expected three operands to insertelement");
2574 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2575 return Error(ID.Loc, "invalid insertelement operands");
Owen Andersonfba933c2009-07-01 23:57:11 +00002576 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002577 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002578 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002579
Chris Lattnerdf986172009-01-02 07:01:27 +00002580 ID.Kind = ValID::t_Constant;
2581 return false;
2582 }
2583 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002584
Chris Lattnerdf986172009-01-02 07:01:27 +00002585 Lex.Lex();
2586 return false;
2587}
2588
2589/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002590bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Victor Hernandez92f238d2010-01-11 22:31:58 +00002591 C = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002592 ValID ID;
Victor Hernandez92f238d2010-01-11 22:31:58 +00002593 Value *V = NULL;
2594 bool Parsed = ParseValID(ID) ||
2595 ConvertValIDToValue(Ty, ID, V, NULL);
2596 if (V && !(C = dyn_cast<Constant>(V)))
2597 return Error(ID.Loc, "global values must be constants");
2598 return Parsed;
Chris Lattnerdf986172009-01-02 07:01:27 +00002599}
2600
Victor Hernandez92f238d2010-01-11 22:31:58 +00002601bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002602 Type *Ty = 0;
2603 return ParseType(Ty) ||
2604 ParseGlobalValue(Ty, V);
Victor Hernandez92f238d2010-01-11 22:31:58 +00002605}
2606
2607/// ParseGlobalValueVector
2608/// ::= /*empty*/
2609/// ::= TypeAndValue (',' TypeAndValue)*
2610bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2611 // Empty list.
2612 if (Lex.getKind() == lltok::rbrace ||
2613 Lex.getKind() == lltok::rsquare ||
2614 Lex.getKind() == lltok::greater ||
2615 Lex.getKind() == lltok::rparen)
2616 return false;
2617
2618 Constant *C;
2619 if (ParseGlobalTypeAndValue(C)) return true;
2620 Elts.push_back(C);
2621
2622 while (EatIfPresent(lltok::comma)) {
2623 if (ParseGlobalTypeAndValue(C)) return true;
2624 Elts.push_back(C);
2625 }
2626
2627 return false;
2628}
2629
Dan Gohman309b3af2010-08-24 02:24:03 +00002630bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2631 assert(Lex.getKind() == lltok::lbrace);
2632 Lex.Lex();
2633
2634 SmallVector<Value*, 16> Elts;
2635 if (ParseMDNodeVector(Elts, PFS) ||
2636 ParseToken(lltok::rbrace, "expected end of metadata node"))
2637 return true;
2638
Jay Foadec9186b2011-04-21 19:59:31 +00002639 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohman309b3af2010-08-24 02:24:03 +00002640 ID.Kind = ValID::t_MDNode;
2641 return false;
2642}
2643
Dan Gohman83448032010-07-14 18:26:50 +00002644/// ParseMetadataValue
2645/// ::= !42
2646/// ::= !{...}
2647/// ::= !"string"
2648bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2649 assert(Lex.getKind() == lltok::exclaim);
2650 Lex.Lex();
2651
2652 // MDNode:
2653 // !{ ... }
Dan Gohman309b3af2010-08-24 02:24:03 +00002654 if (Lex.getKind() == lltok::lbrace)
2655 return ParseMetadataListValue(ID, PFS);
Dan Gohman83448032010-07-14 18:26:50 +00002656
2657 // Standalone metadata reference
2658 // !42
2659 if (Lex.getKind() == lltok::APSInt) {
2660 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2661 ID.Kind = ValID::t_MDNode;
2662 return false;
2663 }
2664
2665 // MDString:
2666 // ::= '!' STRINGCONSTANT
2667 if (ParseMDString(ID.MDStringVal)) return true;
2668 ID.Kind = ValID::t_MDString;
2669 return false;
2670}
2671
Victor Hernandez92f238d2010-01-11 22:31:58 +00002672
2673//===----------------------------------------------------------------------===//
2674// Function Parsing.
2675//===----------------------------------------------------------------------===//
2676
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002677bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez92f238d2010-01-11 22:31:58 +00002678 PerFunctionState *PFS) {
Duncan Sands1df98592010-02-16 11:11:14 +00002679 if (Ty->isFunctionTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002680 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002681
Chris Lattnerdf986172009-01-02 07:01:27 +00002682 switch (ID.Kind) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002683 case ValID::t_LocalID:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002684 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2685 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
2686 return (V == 0);
Chris Lattnerdf986172009-01-02 07:01:27 +00002687 case ValID::t_LocalName:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002688 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2689 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
2690 return (V == 0);
2691 case ValID::t_InlineAsm: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002692 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman407a6162012-11-15 22:34:00 +00002693 FunctionType *FTy =
Victor Hernandez92f238d2010-01-11 22:31:58 +00002694 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2695 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2696 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosier36547342012-09-05 00:08:17 +00002697 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosier581600b2012-09-05 19:00:49 +00002698 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez92f238d2010-01-11 22:31:58 +00002699 return false;
2700 }
2701 case ValID::t_MDNode:
2702 if (!Ty->isMetadataTy())
2703 return Error(ID.Loc, "metadata value must have metadata type");
2704 V = ID.MDNodeVal;
2705 return false;
2706 case ValID::t_MDString:
2707 if (!Ty->isMetadataTy())
2708 return Error(ID.Loc, "metadata value must have metadata type");
2709 V = ID.MDStringVal;
2710 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002711 case ValID::t_GlobalName:
2712 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2713 return V == 0;
2714 case ValID::t_GlobalID:
2715 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2716 return V == 0;
2717 case ValID::t_APSInt:
Duncan Sands1df98592010-02-16 11:11:14 +00002718 if (!Ty->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002719 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad40f8f622010-12-07 08:25:19 +00002720 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersoneed707b2009-07-24 23:12:02 +00002721 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002722 return false;
2723 case ValID::t_APFloat:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002724 if (!Ty->isFloatingPointTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002725 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2726 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002727
Dan Gohmance163392011-12-17 00:04:22 +00002728 // The lexer has no type info, so builds all half, float, and double FP
2729 // constants as double. Fix this here. Long double does not need this.
2730 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002731 bool Ignored;
Dan Gohmance163392011-12-17 00:04:22 +00002732 if (Ty->isHalfTy())
2733 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
2734 &Ignored);
2735 else if (Ty->isFloatTy())
2736 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2737 &Ignored);
Chris Lattnerdf986172009-01-02 07:01:27 +00002738 }
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002739 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002740
Chris Lattner959873d2009-01-05 18:24:23 +00002741 if (V->getType() != Ty)
2742 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002743 getTypeString(Ty) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002744
Chris Lattnerdf986172009-01-02 07:01:27 +00002745 return false;
2746 case ValID::t_Null:
Duncan Sands1df98592010-02-16 11:11:14 +00002747 if (!Ty->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002748 return Error(ID.Loc, "null must be a pointer type");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002749 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerdf986172009-01-02 07:01:27 +00002750 return false;
2751 case ValID::t_Undef:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002752 // FIXME: LabelTy should not be a first-class type.
Chris Lattner1afcace2011-07-09 17:41:24 +00002753 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002754 return Error(ID.Loc, "invalid type for undef constant");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002755 V = UndefValue::get(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002756 return false;
Chris Lattner081b5052009-01-05 07:52:51 +00002757 case ValID::t_EmptyArray:
Duncan Sands1df98592010-02-16 11:11:14 +00002758 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner081b5052009-01-05 07:52:51 +00002759 return Error(ID.Loc, "invalid empty array initializer");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002760 V = UndefValue::get(Ty);
Chris Lattner081b5052009-01-05 07:52:51 +00002761 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002762 case ValID::t_Zero:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002763 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002764 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002765 return Error(ID.Loc, "invalid type for null constant");
Owen Andersona7235ea2009-07-31 20:28:14 +00002766 V = Constant::getNullValue(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002767 return false;
2768 case ValID::t_Constant:
Chris Lattner61c70e92010-08-28 04:09:24 +00002769 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerdf986172009-01-02 07:01:27 +00002770 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002771
Chris Lattnerdf986172009-01-02 07:01:27 +00002772 V = ID.ConstantVal;
2773 return false;
Chris Lattner1afcace2011-07-09 17:41:24 +00002774 case ValID::t_ConstantStruct:
2775 case ValID::t_PackedConstantStruct:
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002776 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002777 if (ST->getNumElements() != ID.UIntVal)
2778 return Error(ID.Loc,
2779 "initializer with struct type has wrong # elements");
2780 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2781 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman407a6162012-11-15 22:34:00 +00002782
Chris Lattner1afcace2011-07-09 17:41:24 +00002783 // Verify that the elements are compatible with the structtype.
2784 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2785 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2786 return Error(ID.Loc, "element " + Twine(i) +
2787 " of struct initializer doesn't match struct element type");
Michael Ilseman407a6162012-11-15 22:34:00 +00002788
Frits van Bommel39b5abf2011-07-18 12:00:32 +00002789 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2790 ID.UIntVal));
Chris Lattner1afcace2011-07-09 17:41:24 +00002791 } else
2792 return Error(ID.Loc, "constant expression type mismatch");
2793 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002794 }
Chandler Carruth732f05c2012-01-10 18:08:01 +00002795 llvm_unreachable("Invalid ValID");
Chris Lattnerdf986172009-01-02 07:01:27 +00002796}
Daniel Dunbara279bc32009-09-20 02:20:51 +00002797
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002798bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002799 V = 0;
2800 ValID ID;
Chris Lattner1afcace2011-07-09 17:41:24 +00002801 return ParseValID(ID, PFS) ||
2802 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002803}
2804
Chris Lattner1afcace2011-07-09 17:41:24 +00002805bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
2806 Type *Ty = 0;
2807 return ParseType(Ty) ||
2808 ParseValue(Ty, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002809}
2810
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002811bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2812 PerFunctionState &PFS) {
2813 Value *V;
2814 Loc = Lex.getLoc();
2815 if (ParseTypeAndValue(V, PFS)) return true;
2816 if (!isa<BasicBlock>(V))
2817 return Error(Loc, "expected a basic block");
2818 BB = cast<BasicBlock>(V);
2819 return false;
2820}
2821
2822
Chris Lattnerdf986172009-01-02 07:01:27 +00002823/// FunctionHeader
2824/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindolabea46262011-01-08 16:42:36 +00002825/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Chris Lattnerdf986172009-01-02 07:01:27 +00002826/// OptionalAlign OptGC
2827bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2828 // Parse the linkage.
2829 LocTy LinkageLoc = Lex.getLoc();
2830 unsigned Linkage;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002831
Kostya Serebryany164b86b2012-01-20 17:56:17 +00002832 unsigned Visibility;
Bill Wendling702cc912012-10-15 20:35:56 +00002833 AttrBuilder RetAttrs;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002834 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00002835 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002836 LocTy RetTypeLoc = Lex.getLoc();
2837 if (ParseOptionalLinkage(Linkage) ||
2838 ParseOptionalVisibility(Visibility) ||
2839 ParseOptionalCallingConv(CC) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00002840 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00002841 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerdf986172009-01-02 07:01:27 +00002842 return true;
2843
2844 // Verify that the linkage is ok.
2845 switch ((GlobalValue::LinkageTypes)Linkage) {
2846 case GlobalValue::ExternalLinkage:
2847 break; // always ok.
2848 case GlobalValue::DLLImportLinkage:
Duncan Sands5f4ee1f2009-03-11 08:08:06 +00002849 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002850 if (isDefine)
2851 return Error(LinkageLoc, "invalid linkage for function definition");
2852 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +00002853 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +00002854 case GlobalValue::LinkerPrivateLinkage:
Bill Wendling5e721d72010-07-01 21:55:59 +00002855 case GlobalValue::LinkerPrivateWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002856 case GlobalValue::InternalLinkage:
Nick Lewycky55f64db2009-04-13 07:02:02 +00002857 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002858 case GlobalValue::LinkOnceAnyLinkage:
2859 case GlobalValue::LinkOnceODRLinkage:
Bill Wendling32811be2012-08-17 18:33:14 +00002860 case GlobalValue::LinkOnceODRAutoHideLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002861 case GlobalValue::WeakAnyLinkage:
2862 case GlobalValue::WeakODRLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002863 case GlobalValue::DLLExportLinkage:
2864 if (!isDefine)
2865 return Error(LinkageLoc, "invalid linkage for function declaration");
2866 break;
2867 case GlobalValue::AppendingLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +00002868 case GlobalValue::CommonLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002869 return Error(LinkageLoc, "invalid function linkage type");
2870 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002871
Chris Lattner1afcace2011-07-09 17:41:24 +00002872 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerdf986172009-01-02 07:01:27 +00002873 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002874
Chris Lattnerdf986172009-01-02 07:01:27 +00002875 LocTy NameLoc = Lex.getLoc();
Chris Lattnerf570e622009-02-18 21:48:13 +00002876
2877 std::string FunctionName;
2878 if (Lex.getKind() == lltok::GlobalVar) {
2879 FunctionName = Lex.getStrVal();
2880 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
2881 unsigned NameID = Lex.getUIntVal();
2882
2883 if (NameID != NumberedVals.size())
2884 return TokError("function expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002885 Twine(NumberedVals.size()) + "'");
Chris Lattnerf570e622009-02-18 21:48:13 +00002886 } else {
2887 return TokError("expected function name");
2888 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002889
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002890 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002891
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002892 if (Lex.getKind() != lltok::lparen)
Chris Lattnerdf986172009-01-02 07:01:27 +00002893 return TokError("expected '(' in function argument list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002894
Chris Lattner1afcace2011-07-09 17:41:24 +00002895 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerdf986172009-01-02 07:01:27 +00002896 bool isVarArg;
Bill Wendling702cc912012-10-15 20:35:56 +00002897 AttrBuilder FuncAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00002898 std::string Section;
Chris Lattnerdf986172009-01-02 07:01:27 +00002899 unsigned Alignment;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002900 std::string GC;
Rafael Espindola3971df52011-01-25 19:09:56 +00002901 bool UnnamedAddr;
2902 LocTy UnnamedAddrLoc;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002903
Chris Lattner1afcace2011-07-09 17:41:24 +00002904 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola3971df52011-01-25 19:09:56 +00002905 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
2906 &UnnamedAddrLoc) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00002907 ParseOptionalFuncAttrs(FuncAttrs) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002908 (EatIfPresent(lltok::kw_section) &&
2909 ParseStringConstant(Section)) ||
2910 ParseOptionalAlignment(Alignment) ||
2911 (EatIfPresent(lltok::kw_gc) &&
2912 ParseStringConstant(GC)))
2913 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00002914
2915 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingf385f4c2012-10-08 23:27:46 +00002916 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendlingef99fe82012-09-21 15:26:31 +00002917 Alignment = FuncAttrs.getAlignment();
Bill Wendling034b94b2012-12-19 07:18:57 +00002918 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerdf986172009-01-02 07:01:27 +00002919 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002920
Chris Lattnerdf986172009-01-02 07:01:27 +00002921 // Okay, if we got here, the function is syntactically valid. Convert types
2922 // and do semantic checks.
Jay Foad5fdd6c82011-07-12 14:06:48 +00002923 std::vector<Type*> ParamTypeList;
Bill Wendlinga1683d62013-01-27 02:24:02 +00002924 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002925
Bill Wendlinge603fe42012-09-19 23:54:18 +00002926 if (RetAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00002927 Attrs.push_back(AttributeSet::get(RetType->getContext(),
2928 AttributeSet::ReturnIndex,
2929 RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002930
Chris Lattnerdf986172009-01-02 07:01:27 +00002931 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002932 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendling73dee182013-01-31 00:29:54 +00002933 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
2934 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlinga1683d62013-01-27 02:24:02 +00002935 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
2936 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002937 }
2938
Bill Wendlinge603fe42012-09-19 23:54:18 +00002939 if (FuncAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00002940 Attrs.push_back(AttributeSet::get(RetType->getContext(),
2941 AttributeSet::FunctionIndex,
2942 FuncAttrs));
Chris Lattnerdf986172009-01-02 07:01:27 +00002943
Bill Wendling99faa3b2012-12-07 23:16:57 +00002944 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002945
Bill Wendling94e94b32012-12-30 13:50:49 +00002946 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002947 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2948
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002949 FunctionType *FT =
Owen Andersondebcb012009-07-29 22:17:13 +00002950 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002951 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerdf986172009-01-02 07:01:27 +00002952
2953 Fn = 0;
2954 if (!FunctionName.empty()) {
2955 // If this was a definition of a forward reference, remove the definition
2956 // from the forward reference table and fill in the forward ref.
2957 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2958 ForwardRefVals.find(FunctionName);
2959 if (FRVI != ForwardRefVals.end()) {
2960 Fn = M->getFunction(FunctionName);
Nick Lewycky64ea2752012-10-11 00:38:25 +00002961 if (!Fn)
2962 return Error(FRVI->second.second, "invalid forward reference to "
2963 "function as global value!");
Chris Lattnerf1cfb952010-04-20 04:49:11 +00002964 if (Fn->getType() != PFT)
2965 return Error(FRVI->second.second, "invalid forward reference to "
2966 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman407a6162012-11-15 22:34:00 +00002967
Chris Lattnerdf986172009-01-02 07:01:27 +00002968 ForwardRefVals.erase(FRVI);
2969 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattnerd5890992011-06-17 07:06:44 +00002970 // Reject redefinitions.
2971 return Error(NameLoc, "invalid redefinition of function '" +
2972 FunctionName + "'");
Chris Lattner1d871c52009-10-25 23:22:50 +00002973 } else if (M->getNamedValue(FunctionName)) {
2974 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002975 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002976
Dan Gohman41905542009-08-29 23:37:49 +00002977 } else {
Chris Lattnerdf986172009-01-02 07:01:27 +00002978 // If this is a definition of a forward referenced function, make sure the
2979 // types agree.
2980 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
2981 = ForwardRefValIDs.find(NumberedVals.size());
2982 if (I != ForwardRefValIDs.end()) {
2983 Fn = cast<Function>(I->second.first);
2984 if (Fn->getType() != PFT)
2985 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002986 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerdf986172009-01-02 07:01:27 +00002987 ForwardRefValIDs.erase(I);
2988 }
2989 }
2990
2991 if (Fn == 0)
2992 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2993 else // Move the forward-reference to the correct spot in the module.
2994 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2995
2996 if (FunctionName.empty())
2997 NumberedVals.push_back(Fn);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002998
Chris Lattnerdf986172009-01-02 07:01:27 +00002999 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
3000 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
3001 Fn->setCallingConv(CC);
3002 Fn->setAttributes(PAL);
Rafael Espindolabea46262011-01-08 16:42:36 +00003003 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerdf986172009-01-02 07:01:27 +00003004 Fn->setAlignment(Alignment);
3005 Fn->setSection(Section);
3006 if (!GC.empty()) Fn->setGC(GC.c_str());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003007
Chris Lattnerdf986172009-01-02 07:01:27 +00003008 // Add all of the arguments we parsed to the function.
3009 Function::arg_iterator ArgIt = Fn->arg_begin();
3010 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
3011 // If the argument has a name, insert it into the argument symbol table.
3012 if (ArgList[i].Name.empty()) continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003013
Chris Lattnerdf986172009-01-02 07:01:27 +00003014 // Set the name, if it conflicted, it will be auto-renamed.
3015 ArgIt->setName(ArgList[i].Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003016
Benjamin Krameraf812352010-10-16 11:28:23 +00003017 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerdf986172009-01-02 07:01:27 +00003018 return Error(ArgList[i].Loc, "redefinition of argument '%" +
3019 ArgList[i].Name + "'");
3020 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003021
Chris Lattnerdf986172009-01-02 07:01:27 +00003022 return false;
3023}
3024
3025
3026/// ParseFunctionBody
3027/// ::= '{' BasicBlock+ '}'
Chris Lattnerdf986172009-01-02 07:01:27 +00003028///
3029bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner6b7c89e2011-06-17 06:42:57 +00003030 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerdf986172009-01-02 07:01:27 +00003031 return TokError("expected '{' in function body");
3032 Lex.Lex(); // eat the {.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003033
Chris Lattner09d9ef42009-10-28 03:39:23 +00003034 int FunctionNumber = -1;
3035 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman407a6162012-11-15 22:34:00 +00003036
Chris Lattner09d9ef42009-10-28 03:39:23 +00003037 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003038
Chris Lattner2fdf8db2010-01-09 19:20:07 +00003039 // We need at least one basic block.
Chris Lattner6b7c89e2011-06-17 06:42:57 +00003040 if (Lex.getKind() == lltok::rbrace)
Chris Lattner2fdf8db2010-01-09 19:20:07 +00003041 return TokError("function body requires at least one basic block");
Michael Ilseman407a6162012-11-15 22:34:00 +00003042
Chris Lattner6b7c89e2011-06-17 06:42:57 +00003043 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerdf986172009-01-02 07:01:27 +00003044 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003045
Chris Lattnerdf986172009-01-02 07:01:27 +00003046 // Eat the }.
3047 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003048
Chris Lattnerdf986172009-01-02 07:01:27 +00003049 // Verify function is ok.
Chris Lattner09d9ef42009-10-28 03:39:23 +00003050 return PFS.FinishFunction();
Chris Lattnerdf986172009-01-02 07:01:27 +00003051}
3052
3053/// ParseBasicBlock
3054/// ::= LabelStr? Instruction*
3055bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
3056 // If this basic block starts out with a name, remember it.
3057 std::string Name;
3058 LocTy NameLoc = Lex.getLoc();
3059 if (Lex.getKind() == lltok::LabelStr) {
3060 Name = Lex.getStrVal();
3061 Lex.Lex();
3062 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003063
Chris Lattnerdf986172009-01-02 07:01:27 +00003064 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
3065 if (BB == 0) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003066
Chris Lattnerdf986172009-01-02 07:01:27 +00003067 std::string NameStr;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003068
Chris Lattnerdf986172009-01-02 07:01:27 +00003069 // Parse the instructions in this block until we get a terminator.
3070 Instruction *Inst;
Chris Lattner1340dd32009-12-30 05:48:36 +00003071 SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +00003072 do {
3073 // This instruction may have three possibilities for a name: a) none
3074 // specified, b) name specified "%foo =", c) number specified: "%4 =".
3075 LocTy NameLoc = Lex.getLoc();
3076 int NameID = -1;
3077 NameStr = "";
Daniel Dunbara279bc32009-09-20 02:20:51 +00003078
Chris Lattnerdf986172009-01-02 07:01:27 +00003079 if (Lex.getKind() == lltok::LocalVarID) {
3080 NameID = Lex.getUIntVal();
3081 Lex.Lex();
3082 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
3083 return true;
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00003084 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003085 NameStr = Lex.getStrVal();
3086 Lex.Lex();
3087 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
3088 return true;
3089 }
Devang Patelf633a062009-09-17 23:04:48 +00003090
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003091 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Topper85814382012-02-07 05:05:23 +00003092 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003093 case InstError: return true;
3094 case InstNormal:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00003095 BB->getInstList().push_back(Inst);
3096
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003097 // With a normal result, we check to see if the instruction is followed by
3098 // a comma and metadata.
3099 if (EatIfPresent(lltok::comma))
Dan Gohman9d072f52010-08-24 02:05:17 +00003100 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003101 return true;
3102 break;
3103 case InstExtraComma:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00003104 BB->getInstList().push_back(Inst);
3105
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003106 // If the instruction parser ate an extra comma at the end of it, it
3107 // *must* be followed by metadata.
Dan Gohman9d072f52010-08-24 02:05:17 +00003108 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003109 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003110 break;
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003111 }
Devang Patelf633a062009-09-17 23:04:48 +00003112
Chris Lattnerdf986172009-01-02 07:01:27 +00003113 // Set the name on the instruction.
3114 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
3115 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003116
Chris Lattnerdf986172009-01-02 07:01:27 +00003117 return false;
3118}
3119
3120//===----------------------------------------------------------------------===//
3121// Instruction Parsing.
3122//===----------------------------------------------------------------------===//
3123
3124/// ParseInstruction - Parse one of the many different instructions.
3125///
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003126int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
3127 PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003128 lltok::Kind Token = Lex.getKind();
3129 if (Token == lltok::Eof)
3130 return TokError("found end of file when expecting more instructions");
3131 LocTy Loc = Lex.getLoc();
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003132 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00003133 Lex.Lex(); // Eat the keyword.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003134
Chris Lattnerdf986172009-01-02 07:01:27 +00003135 switch (Token) {
3136 default: return Error(Loc, "expected instruction opcode");
3137 // Terminator Instructions.
Owen Anderson1d0be152009-08-13 21:58:54 +00003138 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003139 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
3140 case lltok::kw_br: return ParseBr(Inst, PFS);
3141 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +00003142 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003143 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003144 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003145 // Binary Operators.
3146 case lltok::kw_add:
3147 case lltok::kw_sub:
Chris Lattnerf067d582011-02-07 16:40:21 +00003148 case lltok::kw_mul:
3149 case lltok::kw_shl: {
Chris Lattnerf067d582011-02-07 16:40:21 +00003150 bool NUW = EatIfPresent(lltok::kw_nuw);
3151 bool NSW = EatIfPresent(lltok::kw_nsw);
3152 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman407a6162012-11-15 22:34:00 +00003153
Chris Lattnerf067d582011-02-07 16:40:21 +00003154 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003155
Chris Lattnerf067d582011-02-07 16:40:21 +00003156 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3157 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3158 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003159 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00003160 case lltok::kw_fadd:
3161 case lltok::kw_fsub:
Michael Ilseman15c13d32012-11-27 00:42:44 +00003162 case lltok::kw_fmul:
3163 case lltok::kw_fdiv:
3164 case lltok::kw_frem: {
3165 FastMathFlags FMF = EatFastMathFlagsIfPresent();
3166 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
3167 if (Res != 0)
3168 return Res;
3169 if (FMF.any())
3170 Inst->setFastMathFlags(FMF);
3171 return 0;
3172 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00003173
Chris Lattner35bda892011-02-06 21:44:57 +00003174 case lltok::kw_sdiv:
Chris Lattnerf067d582011-02-07 16:40:21 +00003175 case lltok::kw_udiv:
3176 case lltok::kw_lshr:
3177 case lltok::kw_ashr: {
3178 bool Exact = EatIfPresent(lltok::kw_exact);
3179
3180 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3181 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3182 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003183 }
3184
Chris Lattnerdf986172009-01-02 07:01:27 +00003185 case lltok::kw_urem:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003186 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003187 case lltok::kw_and:
3188 case lltok::kw_or:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003189 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003190 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003191 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003192 // Casts.
3193 case lltok::kw_trunc:
3194 case lltok::kw_zext:
3195 case lltok::kw_sext:
3196 case lltok::kw_fptrunc:
3197 case lltok::kw_fpext:
3198 case lltok::kw_bitcast:
3199 case lltok::kw_uitofp:
3200 case lltok::kw_sitofp:
3201 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00003202 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00003203 case lltok::kw_inttoptr:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003204 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003205 // Other.
3206 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +00003207 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003208 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3209 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3210 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3211 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlinge6e88262011-08-12 20:24:12 +00003212 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003213 case lltok::kw_call: return ParseCall(Inst, PFS, false);
3214 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
3215 // Memory.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003216 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003217 case lltok::kw_load: return ParseLoad(Inst, PFS);
3218 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedmanf03bb262011-08-12 22:50:01 +00003219 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
3220 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedman47f35132011-07-25 23:16:38 +00003221 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003222 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3223 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3224 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3225 }
3226}
3227
3228/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3229bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003230 if (Opc == Instruction::FCmp) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003231 switch (Lex.getKind()) {
David Tweedd80d6082013-01-07 13:32:38 +00003232 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerdf986172009-01-02 07:01:27 +00003233 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3234 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3235 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3236 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3237 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3238 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3239 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3240 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3241 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3242 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3243 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3244 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3245 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3246 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3247 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3248 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3249 }
3250 } else {
3251 switch (Lex.getKind()) {
David Tweedd80d6082013-01-07 13:32:38 +00003252 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerdf986172009-01-02 07:01:27 +00003253 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3254 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3255 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3256 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3257 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3258 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3259 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3260 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3261 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3262 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3263 }
3264 }
3265 Lex.Lex();
3266 return false;
3267}
3268
3269//===----------------------------------------------------------------------===//
3270// Terminator Instructions.
3271//===----------------------------------------------------------------------===//
3272
3273/// ParseRet - Parse a return instruction.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003274/// ::= 'ret' void (',' !dbg, !1)*
3275/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner437544f2011-06-17 06:49:41 +00003276bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattner1afcace2011-07-09 17:41:24 +00003277 PerFunctionState &PFS) {
3278 SMLoc TypeLoc = Lex.getLoc();
3279 Type *Ty = 0;
Chris Lattnera9a9e072009-03-09 04:49:14 +00003280 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003281
Chris Lattner1afcace2011-07-09 17:41:24 +00003282 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman407a6162012-11-15 22:34:00 +00003283
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00003284 if (Ty->isVoidTy()) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003285 if (!ResType->isVoidTy())
3286 return Error(TypeLoc, "value doesn't match function result type '" +
3287 getTypeString(ResType) + "'");
Michael Ilseman407a6162012-11-15 22:34:00 +00003288
Owen Anderson1d0be152009-08-13 21:58:54 +00003289 Inst = ReturnInst::Create(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00003290 return false;
3291 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003292
Chris Lattnerdf986172009-01-02 07:01:27 +00003293 Value *RV;
3294 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003295
Chris Lattner1afcace2011-07-09 17:41:24 +00003296 if (ResType != RV->getType())
3297 return Error(TypeLoc, "value doesn't match function result type '" +
3298 getTypeString(ResType) + "'");
Michael Ilseman407a6162012-11-15 22:34:00 +00003299
Owen Anderson1d0be152009-08-13 21:58:54 +00003300 Inst = ReturnInst::Create(Context, RV);
Chris Lattner437544f2011-06-17 06:49:41 +00003301 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003302}
3303
3304
3305/// ParseBr
3306/// ::= 'br' TypeAndValue
3307/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3308bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3309 LocTy Loc, Loc2;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003310 Value *Op0;
3311 BasicBlock *Op1, *Op2;
Chris Lattnerdf986172009-01-02 07:01:27 +00003312 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003313
Chris Lattnerdf986172009-01-02 07:01:27 +00003314 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3315 Inst = BranchInst::Create(BB);
3316 return false;
3317 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003318
Owen Anderson1d0be152009-08-13 21:58:54 +00003319 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerdf986172009-01-02 07:01:27 +00003320 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003321
Chris Lattnerdf986172009-01-02 07:01:27 +00003322 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003323 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003324 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003325 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003326 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003327
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003328 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerdf986172009-01-02 07:01:27 +00003329 return false;
3330}
3331
3332/// ParseSwitch
3333/// Instruction
3334/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3335/// JumpTable
3336/// ::= (TypeAndValue ',' TypeAndValue)*
3337bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3338 LocTy CondLoc, BBLoc;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003339 Value *Cond;
3340 BasicBlock *DefaultBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003341 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3342 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003343 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003344 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3345 return true;
3346
Duncan Sands1df98592010-02-16 11:11:14 +00003347 if (!Cond->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003348 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003349
Chris Lattnerdf986172009-01-02 07:01:27 +00003350 // Parse the jump table pairs.
3351 SmallPtrSet<Value*, 32> SeenCases;
3352 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3353 while (Lex.getKind() != lltok::rsquare) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003354 Value *Constant;
3355 BasicBlock *DestBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003356
Chris Lattnerdf986172009-01-02 07:01:27 +00003357 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3358 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003359 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003360 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003361
Chris Lattnerdf986172009-01-02 07:01:27 +00003362 if (!SeenCases.insert(Constant))
3363 return Error(CondLoc, "duplicate case value in switch");
3364 if (!isa<ConstantInt>(Constant))
3365 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003366
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003367 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerdf986172009-01-02 07:01:27 +00003368 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003369
Chris Lattnerdf986172009-01-02 07:01:27 +00003370 Lex.Lex(); // Eat the ']'.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003371
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003372 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003373 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3374 SI->addCase(Table[i].first, Table[i].second);
3375 Inst = SI;
3376 return false;
3377}
3378
Chris Lattnerab21db72009-10-28 00:19:10 +00003379/// ParseIndirectBr
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003380/// Instruction
Chris Lattnerab21db72009-10-28 00:19:10 +00003381/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3382bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003383 LocTy AddrLoc;
3384 Value *Address;
3385 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerab21db72009-10-28 00:19:10 +00003386 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3387 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003388 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00003389
Duncan Sands1df98592010-02-16 11:11:14 +00003390 if (!Address->getType()->isPointerTy())
Chris Lattnerab21db72009-10-28 00:19:10 +00003391 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman407a6162012-11-15 22:34:00 +00003392
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003393 // Parse the destination list.
3394 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman407a6162012-11-15 22:34:00 +00003395
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003396 if (Lex.getKind() != lltok::rsquare) {
3397 BasicBlock *DestBB;
3398 if (ParseTypeAndBasicBlock(DestBB, PFS))
3399 return true;
3400 DestList.push_back(DestBB);
Michael Ilseman407a6162012-11-15 22:34:00 +00003401
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003402 while (EatIfPresent(lltok::comma)) {
3403 if (ParseTypeAndBasicBlock(DestBB, PFS))
3404 return true;
3405 DestList.push_back(DestBB);
3406 }
3407 }
Michael Ilseman407a6162012-11-15 22:34:00 +00003408
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003409 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3410 return true;
3411
Chris Lattnerab21db72009-10-28 00:19:10 +00003412 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003413 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3414 IBI->addDestination(DestList[i]);
3415 Inst = IBI;
3416 return false;
3417}
3418
3419
Chris Lattnerdf986172009-01-02 07:01:27 +00003420/// ParseInvoke
3421/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3422/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3423bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3424 LocTy CallLoc = Lex.getLoc();
Bill Wendling702cc912012-10-15 20:35:56 +00003425 AttrBuilder RetAttrs, FnAttrs;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003426 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00003427 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003428 LocTy RetTypeLoc;
3429 ValID CalleeID;
3430 SmallVector<ParamInfo, 16> ArgList;
3431
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003432 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003433 if (ParseOptionalCallingConv(CC) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00003434 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003435 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003436 ParseValID(CalleeID) ||
3437 ParseParameterList(ArgList, PFS) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00003438 ParseOptionalFuncAttrs(FnAttrs) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003439 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003440 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003441 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003442 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003443 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003444
Chris Lattnerdf986172009-01-02 07:01:27 +00003445 // If RetType is a non-function pointer type, then this is the short syntax
3446 // for the call, which means that RetType is just the return type. Infer the
3447 // rest of the function argument types from the arguments that are present.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003448 PointerType *PFTy = 0;
3449 FunctionType *Ty = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003450 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3451 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3452 // Pull out the types of all of the arguments...
Jay Foad5fdd6c82011-07-12 14:06:48 +00003453 std::vector<Type*> ParamTypes;
Chris Lattnerdf986172009-01-02 07:01:27 +00003454 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3455 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003456
Chris Lattnerdf986172009-01-02 07:01:27 +00003457 if (!FunctionType::isValidReturnType(RetType))
3458 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003459
Owen Andersondebcb012009-07-29 22:17:13 +00003460 Ty = FunctionType::get(RetType, ParamTypes, false);
3461 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003462 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003463
Chris Lattnerdf986172009-01-02 07:01:27 +00003464 // Look up the callee.
3465 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003466 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003467
Bill Wendling034b94b2012-12-19 07:18:57 +00003468 // Set up the Attribute for the function.
Bill Wendlinga1683d62013-01-27 02:24:02 +00003469 SmallVector<AttributeSet, 8> Attrs;
Bill Wendlinge603fe42012-09-19 23:54:18 +00003470 if (RetAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003471 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3472 AttributeSet::ReturnIndex,
3473 RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003474
Chris Lattnerdf986172009-01-02 07:01:27 +00003475 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003476
Chris Lattnerdf986172009-01-02 07:01:27 +00003477 // Loop through FunctionType's arguments and ensure they are specified
3478 // correctly. Also, gather any parameter attributes.
3479 FunctionType::param_iterator I = Ty->param_begin();
3480 FunctionType::param_iterator E = Ty->param_end();
3481 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003482 Type *ExpectedTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003483 if (I != E) {
3484 ExpectedTy = *I++;
3485 } else if (!Ty->isVarArg()) {
3486 return Error(ArgList[i].Loc, "too many arguments specified");
3487 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003488
Chris Lattnerdf986172009-01-02 07:01:27 +00003489 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3490 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003491 getTypeString(ExpectedTy) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003492 Args.push_back(ArgList[i].V);
Bill Wendling73dee182013-01-31 00:29:54 +00003493 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3494 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlinga1683d62013-01-27 02:24:02 +00003495 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3496 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003497 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003498
Chris Lattnerdf986172009-01-02 07:01:27 +00003499 if (I != E)
3500 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003501
Bill Wendlinge603fe42012-09-19 23:54:18 +00003502 if (FnAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003503 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3504 AttributeSet::FunctionIndex,
3505 FnAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003506
Bill Wendling034b94b2012-12-19 07:18:57 +00003507 // Finish off the Attribute and check them
Bill Wendling99faa3b2012-12-07 23:16:57 +00003508 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003509
Jay Foada3efbb12011-07-15 08:37:34 +00003510 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerdf986172009-01-02 07:01:27 +00003511 II->setCallingConv(CC);
3512 II->setAttributes(PAL);
3513 Inst = II;
3514 return false;
3515}
3516
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003517/// ParseResume
3518/// ::= 'resume' TypeAndValue
3519bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3520 Value *Exn; LocTy ExnLoc;
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003521 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3522 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003523
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003524 ResumeInst *RI = ResumeInst::Create(Exn);
3525 Inst = RI;
3526 return false;
3527}
Chris Lattnerdf986172009-01-02 07:01:27 +00003528
3529//===----------------------------------------------------------------------===//
3530// Binary Operators.
3531//===----------------------------------------------------------------------===//
3532
3533/// ParseArithmetic
Chris Lattnere914b592009-01-05 08:24:46 +00003534/// ::= ArithmeticOps TypeAndValue ',' Value
3535///
3536/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3537/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerdf986172009-01-02 07:01:27 +00003538bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnere914b592009-01-05 08:24:46 +00003539 unsigned Opc, unsigned OperandType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003540 LocTy Loc; Value *LHS, *RHS;
3541 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3542 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3543 ParseValue(LHS->getType(), RHS, PFS))
3544 return true;
3545
Chris Lattnere914b592009-01-05 08:24:46 +00003546 bool Valid;
3547 switch (OperandType) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003548 default: llvm_unreachable("Unknown operand type!");
Chris Lattnere914b592009-01-05 08:24:46 +00003549 case 0: // int or FP.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003550 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3551 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnere914b592009-01-05 08:24:46 +00003552 break;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003553 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3554 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnere914b592009-01-05 08:24:46 +00003555 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003556
Chris Lattnere914b592009-01-05 08:24:46 +00003557 if (!Valid)
3558 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003559
Chris Lattnerdf986172009-01-02 07:01:27 +00003560 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3561 return false;
3562}
3563
3564/// ParseLogical
3565/// ::= ArithmeticOps TypeAndValue ',' Value {
3566bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3567 unsigned Opc) {
3568 LocTy Loc; Value *LHS, *RHS;
3569 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3570 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3571 ParseValue(LHS->getType(), RHS, PFS))
3572 return true;
3573
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003574 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003575 return Error(Loc,"instruction requires integer or integer vector operands");
3576
3577 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3578 return false;
3579}
3580
3581
3582/// ParseCompare
3583/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3584/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerdf986172009-01-02 07:01:27 +00003585bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3586 unsigned Opc) {
3587 // Parse the integer/fp comparison predicate.
3588 LocTy Loc;
3589 unsigned Pred;
3590 Value *LHS, *RHS;
3591 if (ParseCmpPredicate(Pred, Opc) ||
3592 ParseTypeAndValue(LHS, Loc, PFS) ||
3593 ParseToken(lltok::comma, "expected ',' after compare value") ||
3594 ParseValue(LHS->getType(), RHS, PFS))
3595 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003596
Chris Lattnerdf986172009-01-02 07:01:27 +00003597 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003598 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003599 return Error(Loc, "fcmp requires floating point operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003600 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003601 } else {
3602 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003603 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem16087692011-12-05 06:29:09 +00003604 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003605 return Error(Loc, "icmp requires integer operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003606 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003607 }
3608 return false;
3609}
3610
3611//===----------------------------------------------------------------------===//
3612// Other Instructions.
3613//===----------------------------------------------------------------------===//
3614
3615
3616/// ParseCast
3617/// ::= CastOpc TypeAndValue 'to' Type
3618bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3619 unsigned Opc) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003620 LocTy Loc;
3621 Value *Op;
3622 Type *DestTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003623 if (ParseTypeAndValue(Op, Loc, PFS) ||
3624 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3625 ParseType(DestTy))
3626 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003627
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003628 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3629 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00003630 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003631 getTypeString(Op->getType()) + "' to '" +
3632 getTypeString(DestTy) + "'");
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003633 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003634 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3635 return false;
3636}
3637
3638/// ParseSelect
3639/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3640bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3641 LocTy Loc;
3642 Value *Op0, *Op1, *Op2;
3643 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3644 ParseToken(lltok::comma, "expected ',' after select condition") ||
3645 ParseTypeAndValue(Op1, PFS) ||
3646 ParseToken(lltok::comma, "expected ',' after select value") ||
3647 ParseTypeAndValue(Op2, PFS))
3648 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003649
Chris Lattnerdf986172009-01-02 07:01:27 +00003650 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3651 return Error(Loc, Reason);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003652
Chris Lattnerdf986172009-01-02 07:01:27 +00003653 Inst = SelectInst::Create(Op0, Op1, Op2);
3654 return false;
3655}
3656
Chris Lattner0088a5c2009-01-05 08:18:44 +00003657/// ParseVA_Arg
3658/// ::= 'va_arg' TypeAndValue ',' Type
3659bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003660 Value *Op;
Chris Lattner1afcace2011-07-09 17:41:24 +00003661 Type *EltTy = 0;
Chris Lattner0088a5c2009-01-05 08:18:44 +00003662 LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003663 if (ParseTypeAndValue(Op, PFS) ||
3664 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattner0088a5c2009-01-05 08:18:44 +00003665 ParseType(EltTy, TypeLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003666 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003667
Chris Lattner0088a5c2009-01-05 08:18:44 +00003668 if (!EltTy->isFirstClassType())
3669 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003670
3671 Inst = new VAArgInst(Op, EltTy);
3672 return false;
3673}
3674
3675/// ParseExtractElement
3676/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3677bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3678 LocTy Loc;
3679 Value *Op0, *Op1;
3680 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3681 ParseToken(lltok::comma, "expected ',' after extract value") ||
3682 ParseTypeAndValue(Op1, PFS))
3683 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003684
Chris Lattnerdf986172009-01-02 07:01:27 +00003685 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3686 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003687
Eric Christophera3500da2009-07-25 02:28:41 +00003688 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003689 return false;
3690}
3691
3692/// ParseInsertElement
3693/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3694bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3695 LocTy Loc;
3696 Value *Op0, *Op1, *Op2;
3697 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3698 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3699 ParseTypeAndValue(Op1, PFS) ||
3700 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3701 ParseTypeAndValue(Op2, PFS))
3702 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003703
Chris Lattnerdf986172009-01-02 07:01:27 +00003704 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopher0aaf4e92009-07-23 01:01:32 +00003705 return Error(Loc, "invalid insertelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003706
Chris Lattnerdf986172009-01-02 07:01:27 +00003707 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3708 return false;
3709}
3710
3711/// ParseShuffleVector
3712/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3713bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3714 LocTy Loc;
3715 Value *Op0, *Op1, *Op2;
3716 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3717 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3718 ParseTypeAndValue(Op1, PFS) ||
3719 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3720 ParseTypeAndValue(Op2, PFS))
3721 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003722
Chris Lattnerdf986172009-01-02 07:01:27 +00003723 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperaf393682012-02-01 23:43:12 +00003724 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003725
Chris Lattnerdf986172009-01-02 07:01:27 +00003726 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3727 return false;
3728}
3729
3730/// ParsePHI
Chris Lattnerc6e20092009-10-18 05:27:44 +00003731/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003732int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003733 Type *Ty = 0; LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003734 Value *Op0, *Op1;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003735
Chris Lattner1afcace2011-07-09 17:41:24 +00003736 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003737 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3738 ParseValue(Ty, Op0, PFS) ||
3739 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003740 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003741 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3742 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003743
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003744 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003745 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3746 while (1) {
3747 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003748
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003749 if (!EatIfPresent(lltok::comma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003750 break;
3751
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003752 if (Lex.getKind() == lltok::MetadataVar) {
3753 AteExtraComma = true;
Devang Patela43d46f2009-10-16 18:45:49 +00003754 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003755 }
Devang Patela43d46f2009-10-16 18:45:49 +00003756
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003757 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003758 ParseValue(Ty, Op0, PFS) ||
3759 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003760 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003761 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3762 return true;
3763 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003764
Chris Lattnerdf986172009-01-02 07:01:27 +00003765 if (!Ty->isFirstClassType())
3766 return Error(TypeLoc, "phi node must have first class type");
3767
Jay Foad3ecfc862011-03-30 11:28:46 +00003768 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003769 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3770 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3771 Inst = PN;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003772 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003773}
3774
Bill Wendlinge6e88262011-08-12 20:24:12 +00003775/// ParseLandingPad
3776/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3777/// Clause
3778/// ::= 'catch' TypeAndValue
3779/// ::= 'filter'
3780/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3781bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
3782 Type *Ty = 0; LocTy TyLoc;
3783 Value *PersFn; LocTy PersFnLoc;
Bill Wendlinge6e88262011-08-12 20:24:12 +00003784
3785 if (ParseType(Ty, TyLoc) ||
3786 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3787 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3788 return true;
3789
3790 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3791 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3792
3793 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3794 LandingPadInst::ClauseType CT;
3795 if (EatIfPresent(lltok::kw_catch))
3796 CT = LandingPadInst::Catch;
3797 else if (EatIfPresent(lltok::kw_filter))
3798 CT = LandingPadInst::Filter;
3799 else
3800 return TokError("expected 'catch' or 'filter' clause type");
3801
3802 Value *V; LocTy VLoc;
3803 if (ParseTypeAndValue(V, VLoc, PFS)) {
3804 delete LP;
3805 return true;
3806 }
3807
Bill Wendling746c8822011-08-12 20:52:25 +00003808 // A 'catch' type expects a non-array constant. A filter clause expects an
3809 // array constant.
3810 if (CT == LandingPadInst::Catch) {
3811 if (isa<ArrayType>(V->getType()))
3812 Error(VLoc, "'catch' clause has an invalid type");
3813 } else {
3814 if (!isa<ArrayType>(V->getType()))
3815 Error(VLoc, "'filter' clause has an invalid type");
3816 }
3817
Bill Wendlinge6e88262011-08-12 20:24:12 +00003818 LP->addClause(V);
3819 }
3820
3821 Inst = LP;
3822 return false;
3823}
3824
Chris Lattnerdf986172009-01-02 07:01:27 +00003825/// ParseCall
3826/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3827/// ParameterList OptionalAttrs
3828bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3829 bool isTail) {
Bill Wendling702cc912012-10-15 20:35:56 +00003830 AttrBuilder RetAttrs, FnAttrs;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003831 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00003832 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003833 LocTy RetTypeLoc;
3834 ValID CalleeID;
3835 SmallVector<ParamInfo, 16> ArgList;
3836 LocTy CallLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003837
Chris Lattnerdf986172009-01-02 07:01:27 +00003838 if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3839 ParseOptionalCallingConv(CC) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00003840 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003841 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003842 ParseValID(CalleeID) ||
3843 ParseParameterList(ArgList, PFS) ||
Bill Wendlinge01b81b2012-12-04 23:40:58 +00003844 ParseOptionalFuncAttrs(FnAttrs))
Chris Lattnerdf986172009-01-02 07:01:27 +00003845 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003846
Chris Lattnerdf986172009-01-02 07:01:27 +00003847 // If RetType is a non-function pointer type, then this is the short syntax
3848 // for the call, which means that RetType is just the return type. Infer the
3849 // rest of the function argument types from the arguments that are present.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003850 PointerType *PFTy = 0;
3851 FunctionType *Ty = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003852 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3853 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3854 // Pull out the types of all of the arguments...
Jay Foad5fdd6c82011-07-12 14:06:48 +00003855 std::vector<Type*> ParamTypes;
Eli Friedman83b4a972010-07-24 23:06:59 +00003856 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3857 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003858
Chris Lattnerdf986172009-01-02 07:01:27 +00003859 if (!FunctionType::isValidReturnType(RetType))
3860 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003861
Owen Andersondebcb012009-07-29 22:17:13 +00003862 Ty = FunctionType::get(RetType, ParamTypes, false);
3863 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003864 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003865
Chris Lattnerdf986172009-01-02 07:01:27 +00003866 // Look up the callee.
3867 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003868 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003869
Bill Wendling034b94b2012-12-19 07:18:57 +00003870 // Set up the Attribute for the function.
Bill Wendlinga1683d62013-01-27 02:24:02 +00003871 SmallVector<AttributeSet, 8> Attrs;
Bill Wendlinge603fe42012-09-19 23:54:18 +00003872 if (RetAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003873 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3874 AttributeSet::ReturnIndex,
3875 RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003876
Chris Lattnerdf986172009-01-02 07:01:27 +00003877 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003878
Chris Lattnerdf986172009-01-02 07:01:27 +00003879 // Loop through FunctionType's arguments and ensure they are specified
3880 // correctly. Also, gather any parameter attributes.
3881 FunctionType::param_iterator I = Ty->param_begin();
3882 FunctionType::param_iterator E = Ty->param_end();
3883 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003884 Type *ExpectedTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003885 if (I != E) {
3886 ExpectedTy = *I++;
3887 } else if (!Ty->isVarArg()) {
3888 return Error(ArgList[i].Loc, "too many arguments specified");
3889 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003890
Chris Lattnerdf986172009-01-02 07:01:27 +00003891 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3892 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003893 getTypeString(ExpectedTy) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003894 Args.push_back(ArgList[i].V);
Bill Wendling73dee182013-01-31 00:29:54 +00003895 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
3896 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlinga1683d62013-01-27 02:24:02 +00003897 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
3898 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003899 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003900
Chris Lattnerdf986172009-01-02 07:01:27 +00003901 if (I != E)
3902 return Error(CallLoc, "not enough parameters specified for call");
3903
Bill Wendlinge603fe42012-09-19 23:54:18 +00003904 if (FnAttrs.hasAttributes())
Bill Wendlinga1683d62013-01-27 02:24:02 +00003905 Attrs.push_back(AttributeSet::get(RetType->getContext(),
3906 AttributeSet::FunctionIndex,
3907 FnAttrs));
Chris Lattnerdf986172009-01-02 07:01:27 +00003908
Bill Wendling034b94b2012-12-19 07:18:57 +00003909 // Finish off the Attribute and check them
Bill Wendling99faa3b2012-12-07 23:16:57 +00003910 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003911
Jay Foada3efbb12011-07-15 08:37:34 +00003912 CallInst *CI = CallInst::Create(Callee, Args);
Chris Lattnerdf986172009-01-02 07:01:27 +00003913 CI->setTailCall(isTail);
3914 CI->setCallingConv(CC);
3915 CI->setAttributes(PAL);
3916 Inst = CI;
3917 return false;
3918}
3919
3920//===----------------------------------------------------------------------===//
3921// Memory Instructions.
3922//===----------------------------------------------------------------------===//
3923
3924/// ParseAlloc
Devang Patelf633a062009-09-17 23:04:48 +00003925/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
Chris Lattnerf3a789d2011-06-17 03:16:47 +00003926int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003927 Value *Size = 0;
Chris Lattnereeb4a842009-07-02 23:08:13 +00003928 LocTy SizeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003929 unsigned Alignment = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +00003930 Type *Ty = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003931 if (ParseType(Ty)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003932
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003933 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003934 if (EatIfPresent(lltok::comma)) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003935 if (Lex.getKind() == lltok::kw_align) {
3936 if (ParseOptionalAlignment(Alignment)) return true;
3937 } else if (Lex.getKind() == lltok::MetadataVar) {
3938 AteExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003939 } else {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003940 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3941 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3942 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003943 }
3944 }
3945
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003946 if (Size && !Size->getType()->isIntegerTy())
3947 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003948
Chris Lattnerf3a789d2011-06-17 03:16:47 +00003949 Inst = new AllocaInst(Ty, Size, Alignment);
3950 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003951}
3952
3953/// ParseLoad
Eli Friedmanf03bb262011-08-12 22:50:01 +00003954/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman407a6162012-11-15 22:34:00 +00003955/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedmanf03bb262011-08-12 22:50:01 +00003956/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003957int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003958 Value *Val; LocTy Loc;
Devang Patelf633a062009-09-17 23:04:48 +00003959 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003960 bool AteExtraComma = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003961 bool isAtomic = false;
Eli Friedman21006d42011-08-09 23:02:53 +00003962 AtomicOrdering Ordering = NotAtomic;
3963 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003964
3965 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00003966 isAtomic = true;
3967 Lex.Lex();
3968 }
3969
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003970 bool isVolatile = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003971 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00003972 isVolatile = true;
3973 Lex.Lex();
3974 }
3975
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003976 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman21006d42011-08-09 23:02:53 +00003977 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003978 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3979 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003980
Duncan Sands1df98592010-02-16 11:11:14 +00003981 if (!Val->getType()->isPointerTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003982 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3983 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman21006d42011-08-09 23:02:53 +00003984 if (isAtomic && !Alignment)
3985 return Error(Loc, "atomic load must have explicit non-zero alignment");
3986 if (Ordering == Release || Ordering == AcquireRelease)
3987 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003988
Eli Friedman21006d42011-08-09 23:02:53 +00003989 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003990 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003991}
3992
3993/// ParseStore
Eli Friedmanf03bb262011-08-12 22:50:01 +00003994
3995/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
3996/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman21006d42011-08-09 23:02:53 +00003997/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerfbe910e2011-11-27 06:56:53 +00003998int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003999 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelf633a062009-09-17 23:04:48 +00004000 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00004001 bool AteExtraComma = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004002 bool isAtomic = false;
Eli Friedman21006d42011-08-09 23:02:53 +00004003 AtomicOrdering Ordering = NotAtomic;
4004 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004005
4006 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00004007 isAtomic = true;
4008 Lex.Lex();
4009 }
4010
Chris Lattnerfbe910e2011-11-27 06:56:53 +00004011 bool isVolatile = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004012 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedmanf03bb262011-08-12 22:50:01 +00004013 isVolatile = true;
4014 Lex.Lex();
4015 }
4016
Chris Lattnerdf986172009-01-02 07:01:27 +00004017 if (ParseTypeAndValue(Val, Loc, PFS) ||
4018 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00004019 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman21006d42011-08-09 23:02:53 +00004020 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00004021 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00004022 return true;
Devang Patelf633a062009-09-17 23:04:48 +00004023
Duncan Sands1df98592010-02-16 11:11:14 +00004024 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00004025 return Error(PtrLoc, "store operand must be a pointer");
4026 if (!Val->getType()->isFirstClassType())
4027 return Error(Loc, "store operand must be a first class value");
4028 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4029 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman21006d42011-08-09 23:02:53 +00004030 if (isAtomic && !Alignment)
4031 return Error(Loc, "atomic store must have explicit non-zero alignment");
4032 if (Ordering == Acquire || Ordering == AcquireRelease)
4033 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbara279bc32009-09-20 02:20:51 +00004034
Eli Friedman21006d42011-08-09 23:02:53 +00004035 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00004036 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00004037}
4038
Eli Friedmanff030482011-07-28 21:48:00 +00004039/// ParseCmpXchg
Eli Friedmanf03bb262011-08-12 22:50:01 +00004040/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
4041/// 'singlethread'? AtomicOrdering
4042int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanff030482011-07-28 21:48:00 +00004043 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
4044 bool AteExtraComma = false;
4045 AtomicOrdering Ordering = NotAtomic;
4046 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004047 bool isVolatile = false;
4048
4049 if (EatIfPresent(lltok::kw_volatile))
4050 isVolatile = true;
4051
Eli Friedmanff030482011-07-28 21:48:00 +00004052 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4053 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
4054 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
4055 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
4056 ParseTypeAndValue(New, NewLoc, PFS) ||
4057 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4058 return true;
4059
4060 if (Ordering == Unordered)
4061 return TokError("cmpxchg cannot be unordered");
4062 if (!Ptr->getType()->isPointerTy())
4063 return Error(PtrLoc, "cmpxchg operand must be a pointer");
4064 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
4065 return Error(CmpLoc, "compare value and pointer type do not match");
4066 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
4067 return Error(NewLoc, "new value and pointer type do not match");
4068 if (!New->getType()->isIntegerTy())
4069 return Error(NewLoc, "cmpxchg operand must be an integer");
4070 unsigned Size = New->getType()->getPrimitiveSizeInBits();
4071 if (Size < 8 || (Size & (Size - 1)))
4072 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
4073 " integer");
4074
4075 AtomicCmpXchgInst *CXI =
4076 new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, Scope);
4077 CXI->setVolatile(isVolatile);
4078 Inst = CXI;
4079 return AteExtraComma ? InstExtraComma : InstNormal;
4080}
4081
4082/// ParseAtomicRMW
Eli Friedmanf03bb262011-08-12 22:50:01 +00004083/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
4084/// 'singlethread'? AtomicOrdering
4085int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanff030482011-07-28 21:48:00 +00004086 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
4087 bool AteExtraComma = false;
4088 AtomicOrdering Ordering = NotAtomic;
4089 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004090 bool isVolatile = false;
Eli Friedmanff030482011-07-28 21:48:00 +00004091 AtomicRMWInst::BinOp Operation;
Eli Friedmanf03bb262011-08-12 22:50:01 +00004092
4093 if (EatIfPresent(lltok::kw_volatile))
4094 isVolatile = true;
4095
Eli Friedmanff030482011-07-28 21:48:00 +00004096 switch (Lex.getKind()) {
4097 default: return TokError("expected binary operation in atomicrmw");
4098 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
4099 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
4100 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
4101 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
4102 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
4103 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
4104 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
4105 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
4106 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
4107 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
4108 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
4109 }
4110 Lex.Lex(); // Eat the operation.
4111
4112 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
4113 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
4114 ParseTypeAndValue(Val, ValLoc, PFS) ||
4115 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4116 return true;
4117
4118 if (Ordering == Unordered)
4119 return TokError("atomicrmw cannot be unordered");
4120 if (!Ptr->getType()->isPointerTy())
4121 return Error(PtrLoc, "atomicrmw operand must be a pointer");
4122 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
4123 return Error(ValLoc, "atomicrmw value and pointer type do not match");
4124 if (!Val->getType()->isIntegerTy())
4125 return Error(ValLoc, "atomicrmw operand must be an integer");
4126 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
4127 if (Size < 8 || (Size & (Size - 1)))
4128 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
4129 " integer");
4130
4131 AtomicRMWInst *RMWI =
4132 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
4133 RMWI->setVolatile(isVolatile);
4134 Inst = RMWI;
4135 return AteExtraComma ? InstExtraComma : InstNormal;
4136}
4137
Eli Friedman47f35132011-07-25 23:16:38 +00004138/// ParseFence
4139/// ::= 'fence' 'singlethread'? AtomicOrdering
4140int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
4141 AtomicOrdering Ordering = NotAtomic;
4142 SynchronizationScope Scope = CrossThread;
4143 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
4144 return true;
4145
4146 if (Ordering == Unordered)
4147 return TokError("fence cannot be unordered");
4148 if (Ordering == Monotonic)
4149 return TokError("fence cannot be monotonic");
4150
4151 Inst = new FenceInst(Context, Ordering, Scope);
4152 return InstNormal;
4153}
4154
Chris Lattnerdf986172009-01-02 07:01:27 +00004155/// ParseGetElementPtr
Dan Gohmandd8004d2009-07-27 21:53:46 +00004156/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004157int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Nadav Rotem16087692011-12-05 06:29:09 +00004158 Value *Ptr = 0;
4159 Value *Val = 0;
4160 LocTy Loc, EltLoc;
Dan Gohmandd8004d2009-07-27 21:53:46 +00004161
Dan Gohmandcb40a32009-07-29 15:58:36 +00004162 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohmandd8004d2009-07-27 21:53:46 +00004163
Chris Lattner3ed88ef2009-01-02 08:05:26 +00004164 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00004165
Nadav Rotem16087692011-12-05 06:29:09 +00004166 if (!Ptr->getType()->getScalarType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00004167 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00004168
Chris Lattnerdf986172009-01-02 07:01:27 +00004169 SmallVector<Value*, 16> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004170 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00004171 while (EatIfPresent(lltok::comma)) {
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004172 if (Lex.getKind() == lltok::MetadataVar) {
4173 AteExtraComma = true;
Devang Patel6225d642009-10-13 18:49:55 +00004174 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004175 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00004176 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem16087692011-12-05 06:29:09 +00004177 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00004178 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem16087692011-12-05 06:29:09 +00004179 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
4180 return Error(EltLoc, "getelementptr index type missmatch");
4181 if (Val->getType()->isVectorTy()) {
4182 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
4183 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
4184 if (ValNumEl != PtrNumEl)
4185 return Error(EltLoc,
4186 "getelementptr vector index has a wrong number of elements");
4187 }
Chris Lattnerdf986172009-01-02 07:01:27 +00004188 Indices.push_back(Val);
4189 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00004190
Jay Foada9203102011-07-25 09:48:08 +00004191 if (!GetElementPtrInst::getIndexedType(Ptr->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00004192 return Error(Loc, "invalid getelementptr indices");
Jay Foada9203102011-07-25 09:48:08 +00004193 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohmandd8004d2009-07-27 21:53:46 +00004194 if (InBounds)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00004195 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004196 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00004197}
4198
4199/// ParseExtractValue
4200/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004201int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00004202 Value *Val; LocTy Loc;
4203 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004204 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00004205 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004206 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00004207 return true;
4208
Chris Lattnerfdfeb692010-02-12 20:49:41 +00004209 if (!Val->getType()->isAggregateType())
4210 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00004211
Jay Foadfc6d3a42011-07-13 10:26:04 +00004212 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00004213 return Error(Loc, "invalid indices for extractvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00004214 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004215 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00004216}
4217
4218/// ParseInsertValue
4219/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004220int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00004221 Value *Val0, *Val1; LocTy Loc0, Loc1;
4222 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004223 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00004224 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
4225 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
4226 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004227 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00004228 return true;
Michael Ilseman407a6162012-11-15 22:34:00 +00004229
Chris Lattnerfdfeb692010-02-12 20:49:41 +00004230 if (!Val0->getType()->isAggregateType())
4231 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00004232
Jay Foadfc6d3a42011-07-13 10:26:04 +00004233 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00004234 return Error(Loc0, "invalid indices for insertvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00004235 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00004236 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00004237}
Nick Lewycky21cc4462009-04-04 07:22:01 +00004238
4239//===----------------------------------------------------------------------===//
4240// Embedded metadata.
4241//===----------------------------------------------------------------------===//
4242
4243/// ParseMDNodeVector
Nick Lewyckycb337992009-05-10 20:57:05 +00004244/// ::= Element (',' Element)*
4245/// Element
4246/// ::= 'null' | TypeAndValue
Victor Hernandezbf170d42010-01-05 22:22:14 +00004247bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandez24e64df2010-01-10 07:14:18 +00004248 PerFunctionState *PFS) {
Dan Gohmanac809752010-07-13 19:33:27 +00004249 // Check for an empty list.
4250 if (Lex.getKind() == lltok::rbrace)
4251 return false;
4252
Nick Lewycky21cc4462009-04-04 07:22:01 +00004253 do {
Chris Lattnera7352392009-12-30 04:42:57 +00004254 // Null is a special case since it is typeless.
4255 if (EatIfPresent(lltok::kw_null)) {
4256 Elts.push_back(0);
4257 continue;
Nick Lewyckycb337992009-05-10 20:57:05 +00004258 }
Michael Ilseman407a6162012-11-15 22:34:00 +00004259
Chris Lattnera7352392009-12-30 04:42:57 +00004260 Value *V = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +00004261 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckycb337992009-05-10 20:57:05 +00004262 Elts.push_back(V);
Nick Lewycky21cc4462009-04-04 07:22:01 +00004263 } while (EatIfPresent(lltok::comma));
4264
4265 return false;
4266}