blob: cafaab01afd98eedbe9d599d068c26fd660d2326 [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"
15#include "llvm/AutoUpgrade.h"
16#include "llvm/CallingConv.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/InlineAsm.h"
20#include "llvm/Instructions.h"
21#include "llvm/Module.h"
Dan Gohman1224c382009-07-20 21:19:07 +000022#include "llvm/Operator.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000023#include "llvm/ValueSymbolTable.h"
24#include "llvm/ADT/SmallPtrSet.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;
55
56 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
57 unsigned SlotNo = MDList[i].MDSlot;
58
59 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 }
67
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]);
79
80 if (TheFn == 0)
81 return Error(Fn.Loc, "unknown function referenced by blockaddress");
82
83 // Resolve all these references.
84 if (ResolveForwardRefBlockAddresses(TheFn,
85 ForwardRefBlockAddresses.begin()->second,
86 0))
87 return true;
88
89 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
90 }
91
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
Bill Wendlingdf77a712011-08-27 06:11:03 +0000123 // Upgrade to new EH scheme. N.B. This will go away in 3.1.
124 UpgradeExceptionHandling(M);
125
Devang Patele4b27562009-08-28 23:24:31 +0000126 // Check debug info intrinsics.
127 CheckDebugInfoIntrinsics(M);
Chris Lattnerdf986172009-01-02 07:01:27 +0000128 return false;
129}
130
Chris Lattner09d9ef42009-10-28 03:39:23 +0000131bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
132 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
133 PerFunctionState *PFS) {
134 // Loop over all the references, resolving them.
135 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
136 BasicBlock *Res;
Chris Lattnercdfc9402009-11-01 01:27:45 +0000137 if (PFS) {
Chris Lattner09d9ef42009-10-28 03:39:23 +0000138 if (Refs[i].first.Kind == ValID::t_LocalName)
139 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattnercdfc9402009-11-01 01:27:45 +0000140 else
Chris Lattner09d9ef42009-10-28 03:39:23 +0000141 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
142 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
143 return Error(Refs[i].first.Loc,
Chris Lattneree7644d2009-11-02 18:28:45 +0000144 "cannot take address of numeric label after the function is defined");
Chris Lattner09d9ef42009-10-28 03:39:23 +0000145 } else {
146 Res = dyn_cast_or_null<BasicBlock>(
147 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
148 }
149
Chris Lattnercdfc9402009-11-01 01:27:45 +0000150 if (Res == 0)
Chris Lattner09d9ef42009-10-28 03:39:23 +0000151 return Error(Refs[i].first.Loc,
152 "referenced value is not a basic block");
153
154 // Get the BlockAddress for this and update references to use it.
155 BlockAddress *BA = BlockAddress::get(TheFn, Res);
156 Refs[i].second->replaceAllUsesWith(BA);
157 Refs[i].second->eraseFromParent();
158 }
159 return false;
160}
161
162
Chris Lattnerdf986172009-01-02 07:01:27 +0000163//===----------------------------------------------------------------------===//
164// Top-Level Entities
165//===----------------------------------------------------------------------===//
166
167bool LLParser::ParseTopLevelEntities() {
Chris Lattnerdf986172009-01-02 07:01:27 +0000168 while (1) {
169 switch (Lex.getKind()) {
170 default: return TokError("expected top-level entity");
171 case lltok::Eof: return false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000172 case lltok::kw_declare: if (ParseDeclare()) return true; break;
173 case lltok::kw_define: if (ParseDefine()) return true; break;
174 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
175 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
176 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman3845e502009-08-12 23:32:33 +0000177 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000178 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman3845e502009-08-12 23:32:33 +0000179 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000180 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattnere434d272009-12-30 04:56:59 +0000181 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Chris Lattner1d928312009-12-30 05:02:06 +0000182 case lltok::MetadataVar: if (ParseNamedMetadata()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000183
184 // The Global variable production with no name can have many different
185 // optional leading prefixes, the production is:
186 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000187 // OptionalAddrSpace OptionalUnNammedAddr
188 // ('constant'|'global') ...
Bill Wendling5e721d72010-07-01 21:55:59 +0000189 case lltok::kw_private: // OptionalLinkage
190 case lltok::kw_linker_private: // OptionalLinkage
191 case lltok::kw_linker_private_weak: // OptionalLinkage
Bill Wendling55ae5152010-08-20 22:05:50 +0000192 case lltok::kw_linker_private_weak_def_auto: // OptionalLinkage
Bill Wendling5e721d72010-07-01 21:55:59 +0000193 case lltok::kw_internal: // OptionalLinkage
194 case lltok::kw_weak: // OptionalLinkage
195 case lltok::kw_weak_odr: // OptionalLinkage
196 case lltok::kw_linkonce: // OptionalLinkage
197 case lltok::kw_linkonce_odr: // OptionalLinkage
198 case lltok::kw_appending: // OptionalLinkage
199 case lltok::kw_dllexport: // OptionalLinkage
200 case lltok::kw_common: // OptionalLinkage
201 case lltok::kw_dllimport: // OptionalLinkage
202 case lltok::kw_extern_weak: // OptionalLinkage
203 case lltok::kw_external: { // OptionalLinkage
Chris Lattnerdf986172009-01-02 07:01:27 +0000204 unsigned Linkage, Visibility;
205 if (ParseOptionalLinkage(Linkage) ||
206 ParseOptionalVisibility(Visibility) ||
Chris Lattnereeb4a842009-07-02 23:08:13 +0000207 ParseGlobal("", SMLoc(), Linkage, true, Visibility))
Chris Lattnerdf986172009-01-02 07:01:27 +0000208 return true;
209 break;
210 }
211 case lltok::kw_default: // OptionalVisibility
212 case lltok::kw_hidden: // OptionalVisibility
213 case lltok::kw_protected: { // OptionalVisibility
214 unsigned Visibility;
215 if (ParseOptionalVisibility(Visibility) ||
Chris Lattnereeb4a842009-07-02 23:08:13 +0000216 ParseGlobal("", SMLoc(), 0, false, Visibility))
Chris Lattnerdf986172009-01-02 07:01:27 +0000217 return true;
218 break;
219 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000220
Chris Lattnerdf986172009-01-02 07:01:27 +0000221 case lltok::kw_thread_local: // OptionalThreadLocal
222 case lltok::kw_addrspace: // OptionalAddrSpace
223 case lltok::kw_constant: // GlobalType
224 case lltok::kw_global: // GlobalType
Chris Lattnereeb4a842009-07-02 23:08:13 +0000225 if (ParseGlobal("", SMLoc(), 0, false, 0)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +0000226 break;
227 }
228 }
229}
230
231
232/// toplevelentity
233/// ::= 'module' 'asm' STRINGCONSTANT
234bool LLParser::ParseModuleAsm() {
235 assert(Lex.getKind() == lltok::kw_module);
236 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000237
238 std::string AsmStr;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000239 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
240 ParseStringConstant(AsmStr)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000241
Rafael Espindola38c4e532011-03-02 04:14:42 +0000242 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000243 return false;
244}
245
246/// toplevelentity
247/// ::= 'target' 'triple' '=' STRINGCONSTANT
248/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
249bool LLParser::ParseTargetDefinition() {
250 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000251 std::string Str;
Chris Lattnerdf986172009-01-02 07:01:27 +0000252 switch (Lex.Lex()) {
253 default: return TokError("unknown target property");
254 case lltok::kw_triple:
255 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000256 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
257 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000258 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000259 M->setTargetTriple(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000260 return false;
261 case lltok::kw_datalayout:
262 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000263 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
264 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000265 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000266 M->setDataLayout(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000267 return false;
268 }
269}
270
271/// toplevelentity
272/// ::= 'deplibs' '=' '[' ']'
273/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
274bool LLParser::ParseDepLibs() {
275 assert(Lex.getKind() == lltok::kw_deplibs);
Chris Lattnerdf986172009-01-02 07:01:27 +0000276 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000277 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
278 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
279 return true;
280
281 if (EatIfPresent(lltok::rsquare))
282 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000283
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000284 std::string Str;
285 if (ParseStringConstant(Str)) return true;
286 M->addLibrary(Str);
287
288 while (EatIfPresent(lltok::comma)) {
289 if (ParseStringConstant(Str)) return true;
290 M->addLibrary(Str);
291 }
292
293 return ParseToken(lltok::rsquare, "expected ']' at end of list");
Chris Lattnerdf986172009-01-02 07:01:27 +0000294}
295
Dan Gohman3845e502009-08-12 23:32:33 +0000296/// ParseUnnamedType:
Dan Gohman3845e502009-08-12 23:32:33 +0000297/// ::= LocalVarID '=' 'type' type
Chris Lattnerdf986172009-01-02 07:01:27 +0000298bool LLParser::ParseUnnamedType() {
Chris Lattneredcaca82011-06-18 23:51:31 +0000299 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +0000300 unsigned TypeID = Lex.getUIntVal();
Chris Lattnera53616d2011-06-19 00:03:46 +0000301 Lex.Lex(); // eat LocalVarID;
302
303 if (ParseToken(lltok::equal, "expected '=' after name") ||
304 ParseToken(lltok::kw_type, "expected 'type' after '='"))
305 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +0000306
Chris Lattner1afcace2011-07-09 17:41:24 +0000307 if (TypeID >= NumberedTypes.size())
308 NumberedTypes.resize(TypeID+1);
309
310 Type *Result = 0;
311 if (ParseStructDefinition(TypeLoc, "",
312 NumberedTypes[TypeID], Result)) return true;
313
314 if (!isa<StructType>(Result)) {
315 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
316 if (Entry.first)
317 return Error(TypeLoc, "non-struct types may not be recursive");
318 Entry.first = Result;
319 Entry.second = SMLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +0000320 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000321
Chris Lattnerdf986172009-01-02 07:01:27 +0000322 return false;
323}
324
Chris Lattner1afcace2011-07-09 17:41:24 +0000325
Chris Lattnerdf986172009-01-02 07:01:27 +0000326/// toplevelentity
327/// ::= LocalVar '=' 'type' type
328bool LLParser::ParseNamedType() {
329 std::string Name = Lex.getStrVal();
330 LocTy NameLoc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000331 Lex.Lex(); // eat LocalVar.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000332
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000333 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattner1afcace2011-07-09 17:41:24 +0000334 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000335 return true;
Chris Lattner1afcace2011-07-09 17:41:24 +0000336
337 Type *Result = 0;
338 if (ParseStructDefinition(NameLoc, Name,
339 NamedTypes[Name], Result)) return true;
340
341 if (!isa<StructType>(Result)) {
342 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
343 if (Entry.first)
344 return Error(NameLoc, "non-struct types may not be recursive");
345 Entry.first = Result;
346 Entry.second = SMLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +0000347 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000348
349 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000350}
351
352
353/// toplevelentity
354/// ::= 'declare' FunctionHeader
355bool LLParser::ParseDeclare() {
356 assert(Lex.getKind() == lltok::kw_declare);
357 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000358
Chris Lattnerdf986172009-01-02 07:01:27 +0000359 Function *F;
360 return ParseFunctionHeader(F, false);
361}
362
363/// toplevelentity
364/// ::= 'define' FunctionHeader '{' ...
365bool LLParser::ParseDefine() {
366 assert(Lex.getKind() == lltok::kw_define);
367 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000368
Chris Lattnerdf986172009-01-02 07:01:27 +0000369 Function *F;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000370 return ParseFunctionHeader(F, true) ||
371 ParseFunctionBody(*F);
Chris Lattnerdf986172009-01-02 07:01:27 +0000372}
373
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000374/// ParseGlobalType
375/// ::= 'constant'
376/// ::= 'global'
Chris Lattnerdf986172009-01-02 07:01:27 +0000377bool LLParser::ParseGlobalType(bool &IsConstant) {
378 if (Lex.getKind() == lltok::kw_constant)
379 IsConstant = true;
380 else if (Lex.getKind() == lltok::kw_global)
381 IsConstant = false;
Duncan Sands35b51072009-02-10 16:24:55 +0000382 else {
383 IsConstant = false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000384 return TokError("expected 'global' or 'constant'");
Duncan Sands35b51072009-02-10 16:24:55 +0000385 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000386 Lex.Lex();
387 return false;
388}
389
Dan Gohman3845e502009-08-12 23:32:33 +0000390/// ParseUnnamedGlobal:
391/// OptionalVisibility ALIAS ...
392/// OptionalLinkage OptionalVisibility ... -> global variable
393/// GlobalID '=' OptionalVisibility ALIAS ...
394/// GlobalID '=' OptionalLinkage OptionalVisibility ... -> global variable
395bool LLParser::ParseUnnamedGlobal() {
396 unsigned VarID = NumberedVals.size();
397 std::string Name;
398 LocTy NameLoc = Lex.getLoc();
399
400 // Handle the GlobalID form.
401 if (Lex.getKind() == lltok::GlobalID) {
402 if (Lex.getUIntVal() != VarID)
403 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000404 Twine(VarID) + "'");
Dan Gohman3845e502009-08-12 23:32:33 +0000405 Lex.Lex(); // eat GlobalID;
406
407 if (ParseToken(lltok::equal, "expected '=' after name"))
408 return true;
409 }
410
411 bool HasLinkage;
412 unsigned Linkage, Visibility;
413 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
414 ParseOptionalVisibility(Visibility))
415 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000416
Dan Gohman3845e502009-08-12 23:32:33 +0000417 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
418 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
419 return ParseAlias(Name, NameLoc, Visibility);
420}
421
Chris Lattnerdf986172009-01-02 07:01:27 +0000422/// ParseNamedGlobal:
423/// GlobalVar '=' OptionalVisibility ALIAS ...
424/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable
425bool LLParser::ParseNamedGlobal() {
426 assert(Lex.getKind() == lltok::GlobalVar);
427 LocTy NameLoc = Lex.getLoc();
428 std::string Name = Lex.getStrVal();
429 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000430
Chris Lattnerdf986172009-01-02 07:01:27 +0000431 bool HasLinkage;
432 unsigned Linkage, Visibility;
433 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
434 ParseOptionalLinkage(Linkage, HasLinkage) ||
435 ParseOptionalVisibility(Visibility))
436 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000437
Chris Lattnerdf986172009-01-02 07:01:27 +0000438 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
439 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
440 return ParseAlias(Name, NameLoc, Visibility);
441}
442
Devang Patel256be962009-07-20 19:00:08 +0000443// MDString:
444// ::= '!' STRINGCONSTANT
Chris Lattner442ffa12009-12-29 21:53:55 +0000445bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000446 std::string Str;
447 if (ParseStringConstant(Str)) return true;
Chris Lattner442ffa12009-12-29 21:53:55 +0000448 Result = MDString::get(Context, Str);
Devang Patel256be962009-07-20 19:00:08 +0000449 return false;
450}
451
452// MDNode:
453// ::= '!' MDNodeNumber
Chris Lattner449c3102010-04-01 05:14:45 +0000454//
455/// This version of ParseMDNodeID returns the slot number and null in the case
456/// of a forward reference.
457bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
458 // !{ ..., !42, ... }
459 if (ParseUInt32(SlotNo)) return true;
460
461 // Check existing MDNode.
462 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != 0)
463 Result = NumberedMetadata[SlotNo];
464 else
465 Result = 0;
466 return false;
467}
468
Chris Lattner4a72efc2009-12-30 04:15:23 +0000469bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000470 // !{ ..., !42, ... }
471 unsigned MID = 0;
Chris Lattner449c3102010-04-01 05:14:45 +0000472 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000473
Chris Lattner449c3102010-04-01 05:14:45 +0000474 // If not a forward reference, just return it now.
475 if (Result) return false;
Devang Patel256be962009-07-20 19:00:08 +0000476
Chris Lattner449c3102010-04-01 05:14:45 +0000477 // Otherwise, create MDNode forward reference.
Jay Foadec9186b2011-04-21 19:59:31 +0000478 MDNode *FwdNode = MDNode::getTemporary(Context, ArrayRef<Value*>());
Devang Patel256be962009-07-20 19:00:08 +0000479 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Chris Lattner0834e6a2009-12-30 04:51:58 +0000480
481 if (NumberedMetadata.size() <= MID)
482 NumberedMetadata.resize(MID+1);
483 NumberedMetadata[MID] = FwdNode;
Chris Lattner442ffa12009-12-29 21:53:55 +0000484 Result = FwdNode;
Devang Patel256be962009-07-20 19:00:08 +0000485 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000486}
Devang Patel256be962009-07-20 19:00:08 +0000487
Chris Lattner84d03b12009-12-29 22:35:39 +0000488/// ParseNamedMetadata:
Devang Pateleff2ab62009-07-29 00:34:02 +0000489/// !foo = !{ !1, !2 }
490bool LLParser::ParseNamedMetadata() {
Chris Lattner1d928312009-12-30 05:02:06 +0000491 assert(Lex.getKind() == lltok::MetadataVar);
Devang Pateleff2ab62009-07-29 00:34:02 +0000492 std::string Name = Lex.getStrVal();
Chris Lattner1d928312009-12-30 05:02:06 +0000493 Lex.Lex();
Devang Pateleff2ab62009-07-29 00:34:02 +0000494
Chris Lattner84d03b12009-12-29 22:35:39 +0000495 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattnere434d272009-12-30 04:56:59 +0000496 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner84d03b12009-12-29 22:35:39 +0000497 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Pateleff2ab62009-07-29 00:34:02 +0000498 return true;
499
Dan Gohman17aa92c2010-07-21 23:38:33 +0000500 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000501 if (Lex.getKind() != lltok::rbrace)
502 do {
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000503 if (ParseToken(lltok::exclaim, "Expected '!' here"))
504 return true;
Chris Lattner442ffa12009-12-29 21:53:55 +0000505
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000506 MDNode *N = 0;
507 if (ParseMDNodeID(N)) return true;
Dan Gohman17aa92c2010-07-21 23:38:33 +0000508 NMD->addOperand(N);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000509 } while (EatIfPresent(lltok::comma));
Devang Pateleff2ab62009-07-29 00:34:02 +0000510
511 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
512 return true;
513
Devang Pateleff2ab62009-07-29 00:34:02 +0000514 return false;
515}
516
Devang Patel923078c2009-07-01 19:21:12 +0000517/// ParseStandaloneMetadata:
Daniel Dunbara279bc32009-09-20 02:20:51 +0000518/// !42 = !{...}
Devang Patel923078c2009-07-01 19:21:12 +0000519bool LLParser::ParseStandaloneMetadata() {
Chris Lattnere434d272009-12-30 04:56:59 +0000520 assert(Lex.getKind() == lltok::exclaim);
Devang Patel923078c2009-07-01 19:21:12 +0000521 Lex.Lex();
522 unsigned MetadataID = 0;
Devang Patel923078c2009-07-01 19:21:12 +0000523
524 LocTy TyLoc;
Chris Lattner1afcace2011-07-09 17:41:24 +0000525 Type *Ty = 0;
Devang Patel104cf9e2009-07-23 01:07:34 +0000526 SmallVector<Value *, 16> Elts;
Chris Lattner3f5132a2009-12-29 22:40:21 +0000527 if (ParseUInt32(MetadataID) ||
528 ParseToken(lltok::equal, "expected '=' here") ||
529 ParseType(Ty, TyLoc) ||
Chris Lattnere434d272009-12-30 04:56:59 +0000530 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000531 ParseToken(lltok::lbrace, "Expected '{' here") ||
Victor Hernandez24e64df2010-01-10 07:14:18 +0000532 ParseMDNodeVector(Elts, NULL) ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000533 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patel104cf9e2009-07-23 01:07:34 +0000534 return true;
535
Jay Foadec9186b2011-04-21 19:59:31 +0000536 MDNode *Init = MDNode::get(Context, Elts);
Chris Lattner0834e6a2009-12-30 04:51:58 +0000537
538 // See if this was forward referenced, if so, handle it.
Chris Lattnere80250e2009-12-29 21:43:58 +0000539 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Patel1c7eea62009-07-08 19:23:54 +0000540 FI = ForwardRefMDNodes.find(MetadataID);
541 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman489b29b2010-08-20 22:02:26 +0000542 MDNode *Temp = FI->second.first;
543 Temp->replaceAllUsesWith(Init);
544 MDNode::deleteTemporary(Temp);
Devang Patel1c7eea62009-07-08 19:23:54 +0000545 ForwardRefMDNodes.erase(FI);
Chris Lattner0834e6a2009-12-30 04:51:58 +0000546
547 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
548 } else {
549 if (MetadataID >= NumberedMetadata.size())
550 NumberedMetadata.resize(MetadataID+1);
551
552 if (NumberedMetadata[MetadataID] != 0)
553 return TokError("Metadata id is already used");
554 NumberedMetadata[MetadataID] = Init;
Devang Patel1c7eea62009-07-08 19:23:54 +0000555 }
556
Devang Patel923078c2009-07-01 19:21:12 +0000557 return false;
558}
559
Chris Lattnerdf986172009-01-02 07:01:27 +0000560/// ParseAlias:
561/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
562/// Aliasee
Chris Lattner040f7582009-04-25 21:26:00 +0000563/// ::= TypeAndValue
564/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
Dan Gohmandd8004d2009-07-27 21:53:46 +0000565/// ::= 'getelementptr' 'inbounds'? '(' ... ')'
Chris Lattnerdf986172009-01-02 07:01:27 +0000566///
567/// Everything through visibility has already been parsed.
568///
569bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
570 unsigned Visibility) {
571 assert(Lex.getKind() == lltok::kw_alias);
572 Lex.Lex();
573 unsigned Linkage;
574 LocTy LinkageLoc = Lex.getLoc();
575 if (ParseOptionalLinkage(Linkage))
576 return true;
577
578 if (Linkage != GlobalValue::ExternalLinkage &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000579 Linkage != GlobalValue::WeakAnyLinkage &&
580 Linkage != GlobalValue::WeakODRLinkage &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000581 Linkage != GlobalValue::InternalLinkage &&
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000582 Linkage != GlobalValue::PrivateLinkage &&
Bill Wendling5e721d72010-07-01 21:55:59 +0000583 Linkage != GlobalValue::LinkerPrivateLinkage &&
Bill Wendling55ae5152010-08-20 22:05:50 +0000584 Linkage != GlobalValue::LinkerPrivateWeakLinkage &&
585 Linkage != GlobalValue::LinkerPrivateWeakDefAutoLinkage)
Chris Lattnerdf986172009-01-02 07:01:27 +0000586 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000587
Chris Lattnerdf986172009-01-02 07:01:27 +0000588 Constant *Aliasee;
589 LocTy AliaseeLoc = Lex.getLoc();
Chris Lattner040f7582009-04-25 21:26:00 +0000590 if (Lex.getKind() != lltok::kw_bitcast &&
591 Lex.getKind() != lltok::kw_getelementptr) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000592 if (ParseGlobalTypeAndValue(Aliasee)) return true;
593 } else {
594 // The bitcast dest type is not present, it is implied by the dest type.
595 ValID ID;
596 if (ParseValID(ID)) return true;
597 if (ID.Kind != ValID::t_Constant)
598 return Error(AliaseeLoc, "invalid aliasee");
599 Aliasee = ID.ConstantVal;
600 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000601
Duncan Sands1df98592010-02-16 11:11:14 +0000602 if (!Aliasee->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +0000603 return Error(AliaseeLoc, "alias must have pointer type");
604
605 // Okay, create the alias but do not insert it into the module yet.
606 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
607 (GlobalValue::LinkageTypes)Linkage, Name,
608 Aliasee);
609 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000610
Chris Lattnerdf986172009-01-02 07:01:27 +0000611 // See if this value already exists in the symbol table. If so, it is either
612 // a redefinition or a definition of a forward reference.
Chris Lattner1d871c52009-10-25 23:22:50 +0000613 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000614 // See if this was a redefinition. If so, there is no entry in
615 // ForwardRefVals.
616 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
617 I = ForwardRefVals.find(Name);
618 if (I == ForwardRefVals.end())
619 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
620
621 // Otherwise, this was a definition of forward ref. Verify that types
622 // agree.
623 if (Val->getType() != GA->getType())
624 return Error(NameLoc,
625 "forward reference and definition of alias have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000626
Chris Lattnerdf986172009-01-02 07:01:27 +0000627 // If they agree, just RAUW the old value with the alias and remove the
628 // forward ref info.
629 Val->replaceAllUsesWith(GA);
630 Val->eraseFromParent();
631 ForwardRefVals.erase(I);
632 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000633
Chris Lattnerdf986172009-01-02 07:01:27 +0000634 // Insert into the module, we know its name won't collide now.
635 M->getAliasList().push_back(GA);
Benjamin Krameraf812352010-10-16 11:28:23 +0000636 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000637
Chris Lattnerdf986172009-01-02 07:01:27 +0000638 return false;
639}
640
641/// ParseGlobal
642/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000643/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000644/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000645/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000646///
647/// Everything through visibility has been parsed already.
648///
649bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
650 unsigned Linkage, bool HasLinkage,
651 unsigned Visibility) {
652 unsigned AddrSpace;
Rafael Espindolabea46262011-01-08 16:42:36 +0000653 bool ThreadLocal, IsConstant, UnnamedAddr;
Rafael Espindolad72479c2011-01-13 01:30:30 +0000654 LocTy UnnamedAddrLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +0000655 LocTy TyLoc;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000656
Chris Lattner1afcace2011-07-09 17:41:24 +0000657 Type *Ty = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +0000658 if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
659 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindolad72479c2011-01-13 01:30:30 +0000660 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
661 &UnnamedAddrLoc) ||
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,
Owen Andersone9b11b42009-07-08 19:03:57 +0000699 Name, 0, false, AddrSpace);
Chris Lattnerdf986172009-01-02 07:01:27 +0000700 } else {
701 if (GV->getType()->getElementType() != Ty)
702 return Error(TyLoc,
703 "forward reference and definition of global have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000704
Chris Lattnerdf986172009-01-02 07:01:27 +0000705 // Move the forward-reference to the correct spot in the module.
706 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
707 }
708
709 if (Name.empty())
710 NumberedVals.push_back(GV);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000711
Chris Lattnerdf986172009-01-02 07:01:27 +0000712 // Set the parsed properties on the global.
713 if (Init)
714 GV->setInitializer(Init);
715 GV->setConstant(IsConstant);
716 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
717 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
718 GV->setThreadLocal(ThreadLocal);
Rafael Espindolabea46262011-01-08 16:42:36 +0000719 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000720
Chris Lattnerdf986172009-01-02 07:01:27 +0000721 // Parse attributes on the global.
722 while (Lex.getKind() == lltok::comma) {
723 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000724
Chris Lattnerdf986172009-01-02 07:01:27 +0000725 if (Lex.getKind() == lltok::kw_section) {
726 Lex.Lex();
727 GV->setSection(Lex.getStrVal());
728 if (ParseToken(lltok::StringConstant, "expected global section string"))
729 return true;
730 } else if (Lex.getKind() == lltok::kw_align) {
731 unsigned Alignment;
732 if (ParseOptionalAlignment(Alignment)) return true;
733 GV->setAlignment(Alignment);
734 } else {
735 TokError("unknown global variable property!");
736 }
737 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000738
Chris Lattnerdf986172009-01-02 07:01:27 +0000739 return false;
740}
741
742
743//===----------------------------------------------------------------------===//
744// GlobalValue Reference/Resolution Routines.
745//===----------------------------------------------------------------------===//
746
747/// GetGlobalVal - Get a value with the specified name or ID, creating a
748/// forward reference record if needed. This can return null if the value
749/// exists but does not have the right type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000750GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerdf986172009-01-02 07:01:27 +0000751 LocTy Loc) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000752 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +0000753 if (PTy == 0) {
754 Error(Loc, "global variable reference must have pointer type");
755 return 0;
756 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000757
Chris Lattnerdf986172009-01-02 07:01:27 +0000758 // Look this name up in the normal function symbol table.
759 GlobalValue *Val =
760 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000761
Chris Lattnerdf986172009-01-02 07:01:27 +0000762 // If this is a forward reference for the value, see if we already created a
763 // forward ref record.
764 if (Val == 0) {
765 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
766 I = ForwardRefVals.find(Name);
767 if (I != ForwardRefVals.end())
768 Val = I->second.first;
769 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000770
Chris Lattnerdf986172009-01-02 07:01:27 +0000771 // If we have the value in the symbol table or fwd-ref table, return it.
772 if (Val) {
773 if (Val->getType() == Ty) return Val;
774 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +0000775 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +0000776 return 0;
777 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000778
Chris Lattnerdf986172009-01-02 07:01:27 +0000779 // Otherwise, create a new forward reference for this value and remember it.
780 GlobalValue *FwdVal;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000781 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000782 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattner1afcace2011-07-09 17:41:24 +0000783 else
Owen Andersone9b11b42009-07-08 19:03:57 +0000784 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
785 GlobalValue::ExternalWeakLinkage, 0, Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000786
Chris Lattnerdf986172009-01-02 07:01:27 +0000787 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
788 return FwdVal;
789}
790
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000791GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
792 PointerType *PTy = dyn_cast<PointerType>(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +0000793 if (PTy == 0) {
794 Error(Loc, "global variable reference must have pointer type");
795 return 0;
796 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000797
Chris Lattnerdf986172009-01-02 07:01:27 +0000798 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000799
Chris Lattnerdf986172009-01-02 07:01:27 +0000800 // If this is a forward reference for the value, see if we already created a
801 // forward ref record.
802 if (Val == 0) {
803 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
804 I = ForwardRefValIDs.find(ID);
805 if (I != ForwardRefValIDs.end())
806 Val = I->second.first;
807 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000808
Chris Lattnerdf986172009-01-02 07:01:27 +0000809 // If we have the value in the symbol table or fwd-ref table, return it.
810 if (Val) {
811 if (Val->getType() == Ty) return Val;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000812 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +0000813 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +0000814 return 0;
815 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000816
Chris Lattnerdf986172009-01-02 07:01:27 +0000817 // Otherwise, create a new forward reference for this value and remember it.
818 GlobalValue *FwdVal;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000819 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000820 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattner1afcace2011-07-09 17:41:24 +0000821 else
Owen Andersone9b11b42009-07-08 19:03:57 +0000822 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
823 GlobalValue::ExternalWeakLinkage, 0, "");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000824
Chris Lattnerdf986172009-01-02 07:01:27 +0000825 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
826 return FwdVal;
827}
828
829
830//===----------------------------------------------------------------------===//
831// Helper Routines.
832//===----------------------------------------------------------------------===//
833
834/// ParseToken - If the current token has the specified kind, eat it and return
835/// success. Otherwise, emit the specified error and return failure.
836bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
837 if (Lex.getKind() != T)
838 return TokError(ErrMsg);
839 Lex.Lex();
840 return false;
841}
842
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000843/// ParseStringConstant
844/// ::= StringConstant
845bool LLParser::ParseStringConstant(std::string &Result) {
846 if (Lex.getKind() != lltok::StringConstant)
847 return TokError("expected string constant");
848 Result = Lex.getStrVal();
849 Lex.Lex();
850 return false;
851}
852
853/// ParseUInt32
854/// ::= uint32
855bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000856 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
857 return TokError("expected integer");
858 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
859 if (Val64 != unsigned(Val64))
860 return TokError("expected 32-bit integer (too large)");
861 Val = Val64;
862 Lex.Lex();
863 return false;
864}
865
866
867/// ParseOptionalAddrSpace
868/// := /*empty*/
869/// := 'addrspace' '(' uint32 ')'
870bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
871 AddrSpace = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000872 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerdf986172009-01-02 07:01:27 +0000873 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000874 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000875 ParseUInt32(AddrSpace) ||
Chris Lattnerdf986172009-01-02 07:01:27 +0000876 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000877}
Chris Lattnerdf986172009-01-02 07:01:27 +0000878
879/// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind
880/// indicates what kind of attribute list this is: 0: function arg, 1: result,
881/// 2: function attr.
882bool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) {
883 Attrs = Attribute::None;
884 LocTy AttrLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000885
Chris Lattnerdf986172009-01-02 07:01:27 +0000886 while (1) {
887 switch (Lex.getKind()) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000888 default: // End of attributes.
889 if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly))
890 return Error(AttrLoc, "invalid use of function-only attribute");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000891
Chris Lattnerf3a789d2011-06-17 03:16:47 +0000892 // As a hack, we allow "align 2" on functions as a synonym for
893 // "alignstack 2".
894 if (AttrKind == 2 &&
895 (Attrs & ~(Attribute::FunctionOnly | Attribute::Alignment)))
896 return Error(AttrLoc, "invalid use of attribute on a function");
897
898 if (AttrKind != 0 && (Attrs & Attribute::ParameterOnly))
Chris Lattnerdf986172009-01-02 07:01:27 +0000899 return Error(AttrLoc, "invalid use of parameter-only attribute");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000900
Chris Lattnerdf986172009-01-02 07:01:27 +0000901 return false;
Devang Patel578efa92009-06-05 21:57:13 +0000902 case lltok::kw_zeroext: Attrs |= Attribute::ZExt; break;
903 case lltok::kw_signext: Attrs |= Attribute::SExt; break;
904 case lltok::kw_inreg: Attrs |= Attribute::InReg; break;
905 case lltok::kw_sret: Attrs |= Attribute::StructRet; break;
906 case lltok::kw_noalias: Attrs |= Attribute::NoAlias; break;
907 case lltok::kw_nocapture: Attrs |= Attribute::NoCapture; break;
908 case lltok::kw_byval: Attrs |= Attribute::ByVal; break;
909 case lltok::kw_nest: Attrs |= Attribute::Nest; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000910
Devang Patel578efa92009-06-05 21:57:13 +0000911 case lltok::kw_noreturn: Attrs |= Attribute::NoReturn; break;
912 case lltok::kw_nounwind: Attrs |= Attribute::NoUnwind; break;
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000913 case lltok::kw_uwtable: Attrs |= Attribute::UWTable; break;
Rafael Espindola25456ef2011-10-03 14:45:37 +0000914 case lltok::kw_returns_twice: Attrs |= Attribute::ReturnsTwice; break;
Devang Patel578efa92009-06-05 21:57:13 +0000915 case lltok::kw_noinline: Attrs |= Attribute::NoInline; break;
916 case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break;
917 case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break;
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000918 case lltok::kw_inlinehint: Attrs |= Attribute::InlineHint; break;
Devang Patel578efa92009-06-05 21:57:13 +0000919 case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break;
920 case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break;
921 case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break;
922 case lltok::kw_sspreq: Attrs |= Attribute::StackProtectReq; break;
923 case lltok::kw_noredzone: Attrs |= Attribute::NoRedZone; break;
924 case lltok::kw_noimplicitfloat: Attrs |= Attribute::NoImplicitFloat; break;
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000925 case lltok::kw_naked: Attrs |= Attribute::Naked; break;
John McCall3a3465b2011-06-15 20:36:13 +0000926 case lltok::kw_nonlazybind: Attrs |= Attribute::NonLazyBind; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000927
Charles Davis1e063d12010-02-12 00:31:15 +0000928 case lltok::kw_alignstack: {
929 unsigned Alignment;
930 if (ParseOptionalStackAlignment(Alignment))
931 return true;
932 Attrs |= Attribute::constructStackAlignmentFromInt(Alignment);
933 continue;
934 }
935
Chris Lattnerdf986172009-01-02 07:01:27 +0000936 case lltok::kw_align: {
937 unsigned Alignment;
938 if (ParseOptionalAlignment(Alignment))
939 return true;
940 Attrs |= Attribute::constructAlignmentFromInt(Alignment);
941 continue;
942 }
Charles Davis1e063d12010-02-12 00:31:15 +0000943
Chris Lattnerdf986172009-01-02 07:01:27 +0000944 }
945 Lex.Lex();
946 }
947}
948
949/// ParseOptionalLinkage
950/// ::= /*empty*/
Rafael Espindolabb46f522009-01-15 20:18:42 +0000951/// ::= 'private'
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000952/// ::= 'linker_private'
Bill Wendling5e721d72010-07-01 21:55:59 +0000953/// ::= 'linker_private_weak'
Bill Wendling55ae5152010-08-20 22:05:50 +0000954/// ::= 'linker_private_weak_def_auto'
Chris Lattnerdf986172009-01-02 07:01:27 +0000955/// ::= 'internal'
956/// ::= 'weak'
Duncan Sands667d4b82009-03-07 15:45:40 +0000957/// ::= 'weak_odr'
Chris Lattnerdf986172009-01-02 07:01:27 +0000958/// ::= 'linkonce'
Duncan Sands667d4b82009-03-07 15:45:40 +0000959/// ::= 'linkonce_odr'
Bill Wendling5e721d72010-07-01 21:55:59 +0000960/// ::= 'available_externally'
Chris Lattnerdf986172009-01-02 07:01:27 +0000961/// ::= 'appending'
962/// ::= 'dllexport'
963/// ::= 'common'
964/// ::= 'dllimport'
965/// ::= 'extern_weak'
966/// ::= 'external'
967bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
968 HasLinkage = false;
969 switch (Lex.getKind()) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000970 default: Res=GlobalValue::ExternalLinkage; return false;
971 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
972 case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break;
Bill Wendling5e721d72010-07-01 21:55:59 +0000973 case lltok::kw_linker_private_weak:
974 Res = GlobalValue::LinkerPrivateWeakLinkage;
975 break;
Bill Wendling55ae5152010-08-20 22:05:50 +0000976 case lltok::kw_linker_private_weak_def_auto:
977 Res = GlobalValue::LinkerPrivateWeakDefAutoLinkage;
978 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000979 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
980 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
981 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
982 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
983 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +0000984 case lltok::kw_available_externally:
985 Res = GlobalValue::AvailableExternallyLinkage;
986 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000987 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
988 case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break;
989 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
990 case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break;
991 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
992 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000993 }
994 Lex.Lex();
995 HasLinkage = true;
996 return false;
997}
998
999/// ParseOptionalVisibility
1000/// ::= /*empty*/
1001/// ::= 'default'
1002/// ::= 'hidden'
1003/// ::= 'protected'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001004///
Chris Lattnerdf986172009-01-02 07:01:27 +00001005bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1006 switch (Lex.getKind()) {
1007 default: Res = GlobalValue::DefaultVisibility; return false;
1008 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1009 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1010 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1011 }
1012 Lex.Lex();
1013 return false;
1014}
1015
1016/// ParseOptionalCallingConv
1017/// ::= /*empty*/
1018/// ::= 'ccc'
1019/// ::= 'fastcc'
1020/// ::= 'coldcc'
1021/// ::= 'x86_stdcallcc'
1022/// ::= 'x86_fastcallcc'
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001023/// ::= 'x86_thiscallcc'
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001024/// ::= 'arm_apcscc'
1025/// ::= 'arm_aapcscc'
1026/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001027/// ::= 'msp430_intrcc'
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001028/// ::= 'ptx_kernel'
1029/// ::= 'ptx_device'
Chris Lattnerdf986172009-01-02 07:01:27 +00001030/// ::= 'cc' UINT
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001031///
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001032bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001033 switch (Lex.getKind()) {
1034 default: CC = CallingConv::C; return false;
1035 case lltok::kw_ccc: CC = CallingConv::C; break;
1036 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1037 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1038 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1039 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001040 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001041 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1042 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1043 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001044 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001045 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1046 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001047 case lltok::kw_cc: {
1048 unsigned ArbitraryCC;
1049 Lex.Lex();
1050 if (ParseUInt32(ArbitraryCC)) {
1051 return true;
1052 } else
1053 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1054 return false;
1055 }
1056 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001057 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001058
Chris Lattnerdf986172009-01-02 07:01:27 +00001059 Lex.Lex();
1060 return false;
1061}
1062
Chris Lattnerb8c46862009-12-30 05:31:19 +00001063/// ParseInstructionMetadata
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001064/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman9d072f52010-08-24 02:05:17 +00001065bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1066 PerFunctionState *PFS) {
Chris Lattnerb8c46862009-12-30 05:31:19 +00001067 do {
1068 if (Lex.getKind() != lltok::MetadataVar)
1069 return TokError("expected metadata after comma");
Devang Patel0475c912009-09-29 00:01:14 +00001070
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001071 std::string Name = Lex.getStrVal();
Dan Gohman309b3af2010-08-24 02:24:03 +00001072 unsigned MDK = M->getMDKindID(Name.c_str());
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001073 Lex.Lex();
Chris Lattner52e20312009-10-19 05:31:10 +00001074
Chris Lattner442ffa12009-12-29 21:53:55 +00001075 MDNode *Node;
Chris Lattner449c3102010-04-01 05:14:45 +00001076 SMLoc Loc = Lex.getLoc();
Dan Gohman309b3af2010-08-24 02:24:03 +00001077
1078 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattnere434d272009-12-30 04:56:59 +00001079 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001080
Dan Gohman68261142010-08-24 14:35:45 +00001081 // This code is similar to that of ParseMetadataValue, however it needs to
1082 // have special-case code for a forward reference; see the comments on
1083 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1084 // at the top level here.
Dan Gohman309b3af2010-08-24 02:24:03 +00001085 if (Lex.getKind() == lltok::lbrace) {
1086 ValID ID;
1087 if (ParseMetadataListValue(ID, PFS))
1088 return true;
1089 assert(ID.Kind == ValID::t_MDNode);
1090 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner449c3102010-04-01 05:14:45 +00001091 } else {
Nick Lewyckyc6877b42010-09-30 21:04:13 +00001092 unsigned NodeID = 0;
Dan Gohman309b3af2010-08-24 02:24:03 +00001093 if (ParseMDNodeID(Node, NodeID))
1094 return true;
1095 if (Node) {
1096 // If we got the node, add it to the instruction.
1097 Inst->setMetadata(MDK, Node);
1098 } else {
1099 MDRef R = { Loc, MDK, NodeID };
1100 // Otherwise, remember that this should be resolved later.
1101 ForwardRefInstMetadata[Inst].push_back(R);
1102 }
Chris Lattner449c3102010-04-01 05:14:45 +00001103 }
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001104
1105 // If this is the end of the list, we're done.
Chris Lattnerb8c46862009-12-30 05:31:19 +00001106 } while (EatIfPresent(lltok::comma));
1107 return false;
Devang Patelf633a062009-09-17 23:04:48 +00001108}
1109
Chris Lattnerdf986172009-01-02 07:01:27 +00001110/// ParseOptionalAlignment
1111/// ::= /* empty */
1112/// ::= 'align' 4
1113bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1114 Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001115 if (!EatIfPresent(lltok::kw_align))
1116 return false;
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001117 LocTy AlignLoc = Lex.getLoc();
1118 if (ParseUInt32(Alignment)) return true;
1119 if (!isPowerOf2_32(Alignment))
1120 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmane16829b2010-07-30 21:07:05 +00001121 if (Alignment > Value::MaximumAlignment)
Dan Gohman138aa2a2010-07-28 20:12:04 +00001122 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001123 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001124}
1125
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001126/// ParseOptionalCommaAlign
1127/// ::=
1128/// ::= ',' align 4
1129///
1130/// This returns with AteExtraComma set to true if it ate an excess comma at the
1131/// end.
1132bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1133 bool &AteExtraComma) {
1134 AteExtraComma = false;
1135 while (EatIfPresent(lltok::comma)) {
1136 // Metadata at the end is an early exit.
Chris Lattner1d928312009-12-30 05:02:06 +00001137 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001138 AteExtraComma = true;
1139 return false;
1140 }
1141
Chris Lattner093eed12010-04-23 00:50:50 +00001142 if (Lex.getKind() != lltok::kw_align)
1143 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sandsbf9fc532010-10-21 16:07:10 +00001144
Chris Lattner093eed12010-04-23 00:50:50 +00001145 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001146 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001147
Devang Patelf633a062009-09-17 23:04:48 +00001148 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001149}
1150
Eli Friedman47f35132011-07-25 23:16:38 +00001151/// ParseScopeAndOrdering
1152/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1153/// else: ::=
1154///
1155/// This sets Scope and Ordering to the parsed values.
1156bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1157 AtomicOrdering &Ordering) {
1158 if (!isAtomic)
1159 return false;
1160
1161 Scope = CrossThread;
1162 if (EatIfPresent(lltok::kw_singlethread))
1163 Scope = SingleThread;
1164 switch (Lex.getKind()) {
1165 default: return TokError("Expected ordering on atomic instruction");
1166 case lltok::kw_unordered: Ordering = Unordered; break;
1167 case lltok::kw_monotonic: Ordering = Monotonic; break;
1168 case lltok::kw_acquire: Ordering = Acquire; break;
1169 case lltok::kw_release: Ordering = Release; break;
1170 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1171 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1172 }
1173 Lex.Lex();
1174 return false;
1175}
1176
Charles Davis1e063d12010-02-12 00:31:15 +00001177/// ParseOptionalStackAlignment
1178/// ::= /* empty */
1179/// ::= 'alignstack' '(' 4 ')'
1180bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1181 Alignment = 0;
1182 if (!EatIfPresent(lltok::kw_alignstack))
1183 return false;
1184 LocTy ParenLoc = Lex.getLoc();
1185 if (!EatIfPresent(lltok::lparen))
1186 return Error(ParenLoc, "expected '('");
1187 LocTy AlignLoc = Lex.getLoc();
1188 if (ParseUInt32(Alignment)) return true;
1189 ParenLoc = Lex.getLoc();
1190 if (!EatIfPresent(lltok::rparen))
1191 return Error(ParenLoc, "expected ')'");
1192 if (!isPowerOf2_32(Alignment))
1193 return Error(AlignLoc, "stack alignment is not a power of two");
1194 return false;
1195}
Devang Patelf633a062009-09-17 23:04:48 +00001196
Chris Lattner628c13a2009-12-30 05:14:00 +00001197/// ParseIndexList - This parses the index list for an insert/extractvalue
1198/// instruction. This sets AteExtraComma in the case where we eat an extra
1199/// comma at the end of the line and find that it is followed by metadata.
1200/// Clients that don't allow metadata can call the version of this function that
1201/// only takes one argument.
1202///
Chris Lattnerdf986172009-01-02 07:01:27 +00001203/// ParseIndexList
1204/// ::= (',' uint32)+
Chris Lattner628c13a2009-12-30 05:14:00 +00001205///
1206bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1207 bool &AteExtraComma) {
1208 AteExtraComma = false;
1209
Chris Lattnerdf986172009-01-02 07:01:27 +00001210 if (Lex.getKind() != lltok::comma)
1211 return TokError("expected ',' as start of index list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001212
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001213 while (EatIfPresent(lltok::comma)) {
Chris Lattner628c13a2009-12-30 05:14:00 +00001214 if (Lex.getKind() == lltok::MetadataVar) {
1215 AteExtraComma = true;
1216 return false;
1217 }
Nick Lewycky28815c42010-09-29 23:32:20 +00001218 unsigned Idx = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001219 if (ParseUInt32(Idx)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001220 Indices.push_back(Idx);
1221 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001222
Chris Lattnerdf986172009-01-02 07:01:27 +00001223 return false;
1224}
1225
1226//===----------------------------------------------------------------------===//
1227// Type Parsing.
1228//===----------------------------------------------------------------------===//
1229
Chris Lattner1afcace2011-07-09 17:41:24 +00001230/// ParseType - Parse a type.
1231bool LLParser::ParseType(Type *&Result, bool AllowVoid) {
1232 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001233 switch (Lex.getKind()) {
1234 default:
1235 return TokError("expected type");
1236 case lltok::Type:
Chris Lattner1afcace2011-07-09 17:41:24 +00001237 // Type ::= 'float' | 'void' (etc)
Chris Lattnerdf986172009-01-02 07:01:27 +00001238 Result = Lex.getTyVal();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001239 Lex.Lex();
Chris Lattnerdf986172009-01-02 07:01:27 +00001240 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001241 case lltok::lbrace:
Chris Lattner1afcace2011-07-09 17:41:24 +00001242 // Type ::= StructType
1243 if (ParseAnonStructType(Result, false))
Chris Lattnerdf986172009-01-02 07:01:27 +00001244 return true;
1245 break;
1246 case lltok::lsquare:
Chris Lattner1afcace2011-07-09 17:41:24 +00001247 // Type ::= '[' ... ']'
Chris Lattnerdf986172009-01-02 07:01:27 +00001248 Lex.Lex(); // eat the lsquare.
1249 if (ParseArrayVectorType(Result, false))
1250 return true;
1251 break;
1252 case lltok::less: // Either vector or packed struct.
Chris Lattner1afcace2011-07-09 17:41:24 +00001253 // Type ::= '<' ... '>'
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001254 Lex.Lex();
1255 if (Lex.getKind() == lltok::lbrace) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001256 if (ParseAnonStructType(Result, true) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001257 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerdf986172009-01-02 07:01:27 +00001258 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001259 } else if (ParseArrayVectorType(Result, true))
1260 return true;
1261 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001262 case lltok::LocalVar: {
1263 // Type ::= %foo
1264 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
1265
1266 // If the type hasn't been defined yet, create a forward definition and
1267 // remember where that forward def'n was seen (in case it never is defined).
1268 if (Entry.first == 0) {
Chris Lattner3ebb6492011-08-12 18:06:37 +00001269 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattner1afcace2011-07-09 17:41:24 +00001270 Entry.second = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001271 }
Chris Lattner1afcace2011-07-09 17:41:24 +00001272 Result = Entry.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001273 Lex.Lex();
1274 break;
Chris Lattner1afcace2011-07-09 17:41:24 +00001275 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001276
Chris Lattner1afcace2011-07-09 17:41:24 +00001277 case lltok::LocalVarID: {
1278 // Type ::= %4
1279 if (Lex.getUIntVal() >= NumberedTypes.size())
1280 NumberedTypes.resize(Lex.getUIntVal()+1);
1281 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
1282
1283 // If the type hasn't been defined yet, create a forward definition and
1284 // remember where that forward def'n was seen (in case it never is defined).
1285 if (Entry.first == 0) {
Chris Lattner3ebb6492011-08-12 18:06:37 +00001286 Entry.first = StructType::create(Context);
Chris Lattner1afcace2011-07-09 17:41:24 +00001287 Entry.second = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001288 }
Chris Lattner1afcace2011-07-09 17:41:24 +00001289 Result = Entry.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001290 Lex.Lex();
1291 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001292 }
1293 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001294
1295 // Parse the type suffixes.
Chris Lattnerdf986172009-01-02 07:01:27 +00001296 while (1) {
1297 switch (Lex.getKind()) {
1298 // End of type.
Chris Lattner1afcace2011-07-09 17:41:24 +00001299 default:
1300 if (!AllowVoid && Result->isVoidTy())
1301 return Error(TypeLoc, "void type only allowed for function results");
1302 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001303
Chris Lattner1afcace2011-07-09 17:41:24 +00001304 // Type ::= Type '*'
Chris Lattnerdf986172009-01-02 07:01:27 +00001305 case lltok::star:
Chris Lattner1afcace2011-07-09 17:41:24 +00001306 if (Result->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001307 return TokError("basic block pointers are invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001308 if (Result->isVoidTy())
1309 return TokError("pointers to void are invalid - use i8* instead");
1310 if (!PointerType::isValidElementType(Result))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001311 return TokError("pointer to this type is invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001312 Result = PointerType::getUnqual(Result);
Chris Lattnerdf986172009-01-02 07:01:27 +00001313 Lex.Lex();
1314 break;
1315
Chris Lattner1afcace2011-07-09 17:41:24 +00001316 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerdf986172009-01-02 07:01:27 +00001317 case lltok::kw_addrspace: {
Chris Lattner1afcace2011-07-09 17:41:24 +00001318 if (Result->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001319 return TokError("basic block pointers are invalid");
Chris Lattner1afcace2011-07-09 17:41:24 +00001320 if (Result->isVoidTy())
Dan Gohmanb9070d32009-02-09 17:41:21 +00001321 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattner1afcace2011-07-09 17:41:24 +00001322 if (!PointerType::isValidElementType(Result))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001323 return TokError("pointer to this type is invalid");
Chris Lattnerdf986172009-01-02 07:01:27 +00001324 unsigned AddrSpace;
1325 if (ParseOptionalAddrSpace(AddrSpace) ||
1326 ParseToken(lltok::star, "expected '*' in address space"))
1327 return true;
1328
Chris Lattner1afcace2011-07-09 17:41:24 +00001329 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerdf986172009-01-02 07:01:27 +00001330 break;
1331 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001332
Chris Lattnerdf986172009-01-02 07:01:27 +00001333 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1334 case lltok::lparen:
1335 if (ParseFunctionType(Result))
1336 return true;
1337 break;
1338 }
1339 }
1340}
1341
1342/// ParseParameterList
1343/// ::= '(' ')'
1344/// ::= '(' Arg (',' Arg)* ')'
1345/// Arg
1346/// ::= Type OptionalAttributes Value OptionalAttributes
1347bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1348 PerFunctionState &PFS) {
1349 if (ParseToken(lltok::lparen, "expected '(' in call"))
1350 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001351
Chris Lattnerdf986172009-01-02 07:01:27 +00001352 while (Lex.getKind() != lltok::rparen) {
1353 // If this isn't the first argument, we need a comma.
1354 if (!ArgList.empty() &&
1355 ParseToken(lltok::comma, "expected ',' in argument list"))
1356 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001357
Chris Lattnerdf986172009-01-02 07:01:27 +00001358 // Parse the argument.
1359 LocTy ArgLoc;
Chris Lattner1afcace2011-07-09 17:41:24 +00001360 Type *ArgTy = 0;
Victor Hernandez19715562009-12-03 23:40:58 +00001361 unsigned ArgAttrs1 = Attribute::None;
1362 unsigned ArgAttrs2 = Attribute::None;
Chris Lattnerdf986172009-01-02 07:01:27 +00001363 Value *V;
Victor Hernandez19715562009-12-03 23:40:58 +00001364 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00001365 return true;
Victor Hernandez19715562009-12-03 23:40:58 +00001366
Chris Lattner287881d2009-12-30 02:11:14 +00001367 // Otherwise, handle normal operands.
Chris Lattnerf3a789d2011-06-17 03:16:47 +00001368 if (ParseOptionalAttrs(ArgAttrs1, 0) || ParseValue(ArgTy, V, PFS))
Chris Lattner287881d2009-12-30 02:11:14 +00001369 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001370 ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2));
1371 }
1372
1373 Lex.Lex(); // Lex the ')'.
1374 return false;
1375}
1376
1377
1378
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001379/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattner1afcace2011-07-09 17:41:24 +00001380/// prototype.
Chris Lattnerdf986172009-01-02 07:01:27 +00001381/// ::= '(' ArgTypeListI ')'
1382/// ArgTypeListI
1383/// ::= /*empty*/
1384/// ::= '...'
1385/// ::= ArgTypeList ',' '...'
1386/// ::= ArgType (',' ArgType)*
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001387///
Chris Lattner1afcace2011-07-09 17:41:24 +00001388bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1389 bool &isVarArg){
Chris Lattnerdf986172009-01-02 07:01:27 +00001390 isVarArg = false;
1391 assert(Lex.getKind() == lltok::lparen);
1392 Lex.Lex(); // eat the (.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001393
Chris Lattnerdf986172009-01-02 07:01:27 +00001394 if (Lex.getKind() == lltok::rparen) {
1395 // empty
1396 } else if (Lex.getKind() == lltok::dotdotdot) {
1397 isVarArg = true;
1398 Lex.Lex();
1399 } else {
1400 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001401 Type *ArgTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00001402 unsigned Attrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00001403 std::string Name;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001404
Chris Lattner1afcace2011-07-09 17:41:24 +00001405 if (ParseType(ArgTy) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001406 ParseOptionalAttrs(Attrs, 0)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001407
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001408 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001409 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001410
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00001411 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001412 Name = Lex.getStrVal();
1413 Lex.Lex();
1414 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001415
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001416 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001417 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001418
Chris Lattnerdf986172009-01-02 07:01:27 +00001419 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001420
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001421 while (EatIfPresent(lltok::comma)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001422 // Handle ... at end of arg list.
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001423 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001424 isVarArg = true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001425 break;
1426 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001427
Chris Lattnerdf986172009-01-02 07:01:27 +00001428 // Otherwise must be an argument type.
1429 TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001430 if (ParseType(ArgTy) || ParseOptionalAttrs(Attrs, 0)) return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001431
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001432 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001433 return Error(TypeLoc, "argument can not have void type");
1434
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00001435 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001436 Name = Lex.getStrVal();
1437 Lex.Lex();
1438 } else {
1439 Name = "";
1440 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001441
Chris Lattner1afcace2011-07-09 17:41:24 +00001442 if (!ArgTy->isFirstClassType())
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001443 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001444
Chris Lattnerdf986172009-01-02 07:01:27 +00001445 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
1446 }
1447 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001448
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001449 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerdf986172009-01-02 07:01:27 +00001450}
Daniel Dunbara279bc32009-09-20 02:20:51 +00001451
Chris Lattnerdf986172009-01-02 07:01:27 +00001452/// ParseFunctionType
1453/// ::= Type ArgumentList OptionalAttrs
Chris Lattner1afcace2011-07-09 17:41:24 +00001454bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001455 assert(Lex.getKind() == lltok::lparen);
1456
Chris Lattnerd77d04c2009-01-05 08:04:33 +00001457 if (!FunctionType::isValidReturnType(Result))
1458 return TokError("invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001459
Chris Lattner1afcace2011-07-09 17:41:24 +00001460 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerdf986172009-01-02 07:01:27 +00001461 bool isVarArg;
Chris Lattner1afcace2011-07-09 17:41:24 +00001462 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerdf986172009-01-02 07:01:27 +00001463 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001464
Chris Lattnerdf986172009-01-02 07:01:27 +00001465 // Reject names on the arguments lists.
1466 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1467 if (!ArgList[i].Name.empty())
1468 return Error(ArgList[i].Loc, "argument name invalid in function type");
Chris Lattnera16546a2011-06-17 17:37:13 +00001469 if (ArgList[i].Attrs != 0)
1470 return Error(ArgList[i].Loc,
1471 "argument attributes invalid in function type");
Chris Lattnerdf986172009-01-02 07:01:27 +00001472 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001473
Jay Foad5fdd6c82011-07-12 14:06:48 +00001474 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerdf986172009-01-02 07:01:27 +00001475 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattner1afcace2011-07-09 17:41:24 +00001476 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001477
Chris Lattner1afcace2011-07-09 17:41:24 +00001478 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerdf986172009-01-02 07:01:27 +00001479 return false;
1480}
1481
Chris Lattner1afcace2011-07-09 17:41:24 +00001482/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1483/// other structs.
1484bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1485 SmallVector<Type*, 8> Elts;
1486 if (ParseStructBody(Elts)) return true;
1487
1488 Result = StructType::get(Context, Elts, Packed);
1489 return false;
1490}
1491
1492/// ParseStructDefinition - Parse a struct in a 'type' definition.
1493bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1494 std::pair<Type*, LocTy> &Entry,
1495 Type *&ResultTy) {
1496 // If the type was already defined, diagnose the redefinition.
1497 if (Entry.first && !Entry.second.isValid())
1498 return Error(TypeLoc, "redefinition of type");
1499
1500 // If we have opaque, just return without filling in the definition for the
1501 // struct. This counts as a definition as far as the .ll file goes.
1502 if (EatIfPresent(lltok::kw_opaque)) {
1503 // This type is being defined, so clear the location to indicate this.
1504 Entry.second = SMLoc();
1505
1506 // If this type number has never been uttered, create it.
1507 if (Entry.first == 0)
Chris Lattner3ebb6492011-08-12 18:06:37 +00001508 Entry.first = StructType::create(Context, Name);
Chris Lattner1afcace2011-07-09 17:41:24 +00001509 ResultTy = Entry.first;
1510 return false;
1511 }
1512
1513 // If the type starts with '<', then it is either a packed struct or a vector.
1514 bool isPacked = EatIfPresent(lltok::less);
1515
1516 // If we don't have a struct, then we have a random type alias, which we
1517 // accept for compatibility with old files. These types are not allowed to be
1518 // forward referenced and not allowed to be recursive.
1519 if (Lex.getKind() != lltok::lbrace) {
1520 if (Entry.first)
1521 return Error(TypeLoc, "forward references to non-struct type");
1522
1523 ResultTy = 0;
1524 if (isPacked)
1525 return ParseArrayVectorType(ResultTy, true);
1526 return ParseType(ResultTy);
1527 }
1528
1529 // This type is being defined, so clear the location to indicate this.
1530 Entry.second = SMLoc();
1531
1532 // If this type number has never been uttered, create it.
1533 if (Entry.first == 0)
Chris Lattner3ebb6492011-08-12 18:06:37 +00001534 Entry.first = StructType::create(Context, Name);
Chris Lattner1afcace2011-07-09 17:41:24 +00001535
1536 StructType *STy = cast<StructType>(Entry.first);
1537
1538 SmallVector<Type*, 8> Body;
1539 if (ParseStructBody(Body) ||
1540 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
1541 return true;
1542
1543 STy->setBody(Body, isPacked);
1544 ResultTy = STy;
1545 return false;
1546}
1547
1548
Chris Lattnerdf986172009-01-02 07:01:27 +00001549/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattner1afcace2011-07-09 17:41:24 +00001550/// StructType
Chris Lattnerdf986172009-01-02 07:01:27 +00001551/// ::= '{' '}'
Chris Lattner1afcace2011-07-09 17:41:24 +00001552/// ::= '{' Type (',' Type)* '}'
Chris Lattnerdf986172009-01-02 07:01:27 +00001553/// ::= '<' '{' '}' '>'
Chris Lattner1afcace2011-07-09 17:41:24 +00001554/// ::= '<' '{' Type (',' Type)* '}' '>'
1555bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001556 assert(Lex.getKind() == lltok::lbrace);
1557 Lex.Lex(); // Consume the '{'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001558
Chris Lattner1afcace2011-07-09 17:41:24 +00001559 // Handle the empty struct.
1560 if (EatIfPresent(lltok::rbrace))
Chris Lattnerdf986172009-01-02 07:01:27 +00001561 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001562
Chris Lattnera9a9e072009-03-09 04:49:14 +00001563 LocTy EltTyLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001564 Type *Ty = 0;
1565 if (ParseType(Ty)) return true;
1566 Body.push_back(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001567
Chris Lattner1afcace2011-07-09 17:41:24 +00001568 if (!StructType::isValidElementType(Ty))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001569 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001570
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001571 while (EatIfPresent(lltok::comma)) {
Chris Lattnera9a9e072009-03-09 04:49:14 +00001572 EltTyLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001573 if (ParseType(Ty)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001574
Chris Lattner1afcace2011-07-09 17:41:24 +00001575 if (!StructType::isValidElementType(Ty))
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001576 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001577
Chris Lattner1afcace2011-07-09 17:41:24 +00001578 Body.push_back(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00001579 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001580
Chris Lattner1afcace2011-07-09 17:41:24 +00001581 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerdf986172009-01-02 07:01:27 +00001582}
1583
1584/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1585/// token has already been consumed.
Chris Lattner1afcace2011-07-09 17:41:24 +00001586/// Type
Chris Lattnerdf986172009-01-02 07:01:27 +00001587/// ::= '[' APSINTVAL 'x' Types ']'
1588/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattner1afcace2011-07-09 17:41:24 +00001589bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001590 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1591 Lex.getAPSIntVal().getBitWidth() > 64)
1592 return TokError("expected number in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001593
Chris Lattnerdf986172009-01-02 07:01:27 +00001594 LocTy SizeLoc = Lex.getLoc();
1595 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001596 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001597
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001598 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1599 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001600
1601 LocTy TypeLoc = Lex.getLoc();
Chris Lattner1afcace2011-07-09 17:41:24 +00001602 Type *EltTy = 0;
1603 if (ParseType(EltTy)) return true;
Chris Lattnera9a9e072009-03-09 04:49:14 +00001604
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001605 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
1606 "expected end of sequential type"))
1607 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001608
Chris Lattnerdf986172009-01-02 07:01:27 +00001609 if (isVector) {
Chris Lattner452e2622009-02-28 18:12:41 +00001610 if (Size == 0)
1611 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerdf986172009-01-02 07:01:27 +00001612 if ((unsigned)Size != Size)
1613 return Error(SizeLoc, "size too large for vector");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001614 if (!VectorType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001615 return Error(TypeLoc, "vector element type must be fp or integer");
Owen Andersondebcb012009-07-29 22:17:13 +00001616 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerdf986172009-01-02 07:01:27 +00001617 } else {
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001618 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001619 return Error(TypeLoc, "invalid array element type");
Chris Lattner1afcace2011-07-09 17:41:24 +00001620 Result = ArrayType::get(EltTy, Size);
Chris Lattnerdf986172009-01-02 07:01:27 +00001621 }
1622 return false;
1623}
1624
1625//===----------------------------------------------------------------------===//
1626// Function Semantic Analysis.
1627//===----------------------------------------------------------------------===//
1628
Chris Lattner09d9ef42009-10-28 03:39:23 +00001629LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
1630 int functionNumber)
1631 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001632
1633 // Insert unnamed arguments into the NumberedVals list.
1634 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1635 AI != E; ++AI)
1636 if (!AI->hasName())
1637 NumberedVals.push_back(AI);
1638}
1639
1640LLParser::PerFunctionState::~PerFunctionState() {
1641 // If there were any forward referenced non-basicblock values, delete them.
1642 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1643 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1644 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001645 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001646 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001647 delete I->second.first;
1648 I->second.first = 0;
1649 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001650
Chris Lattnerdf986172009-01-02 07:01:27 +00001651 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1652 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1653 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001654 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001655 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001656 delete I->second.first;
1657 I->second.first = 0;
1658 }
1659}
1660
Chris Lattner09d9ef42009-10-28 03:39:23 +00001661bool LLParser::PerFunctionState::FinishFunction() {
1662 // Check to see if someone took the address of labels in this block.
1663 if (!P.ForwardRefBlockAddresses.empty()) {
1664 ValID FunctionID;
1665 if (!F.getName().empty()) {
1666 FunctionID.Kind = ValID::t_GlobalName;
1667 FunctionID.StrVal = F.getName();
1668 } else {
1669 FunctionID.Kind = ValID::t_GlobalID;
1670 FunctionID.UIntVal = FunctionNumber;
1671 }
1672
1673 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
1674 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
1675 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
1676 // Resolve all these references.
1677 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
1678 return true;
1679
1680 P.ForwardRefBlockAddresses.erase(FRBAI);
1681 }
1682 }
1683
Chris Lattnerdf986172009-01-02 07:01:27 +00001684 if (!ForwardRefVals.empty())
1685 return P.Error(ForwardRefVals.begin()->second.second,
1686 "use of undefined value '%" + ForwardRefVals.begin()->first +
1687 "'");
1688 if (!ForwardRefValIDs.empty())
1689 return P.Error(ForwardRefValIDs.begin()->second.second,
1690 "use of undefined value '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001691 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001692 return false;
1693}
1694
1695
1696/// GetVal - Get a value with the specified name or ID, creating a
1697/// forward reference record if needed. This can return null if the value
1698/// exists but does not have the right type.
1699Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001700 Type *Ty, LocTy Loc) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001701 // Look this name up in the normal function symbol table.
1702 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001703
Chris Lattnerdf986172009-01-02 07:01:27 +00001704 // If this is a forward reference for the value, see if we already created a
1705 // forward ref record.
1706 if (Val == 0) {
1707 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1708 I = ForwardRefVals.find(Name);
1709 if (I != ForwardRefVals.end())
1710 Val = I->second.first;
1711 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001712
Chris Lattnerdf986172009-01-02 07:01:27 +00001713 // If we have the value in the symbol table or fwd-ref table, return it.
1714 if (Val) {
1715 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001716 if (Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001717 P.Error(Loc, "'%" + Name + "' is not a basic block");
1718 else
1719 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001720 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001721 return 0;
1722 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001723
Chris Lattnerdf986172009-01-02 07:01:27 +00001724 // Don't make placeholders with invalid type.
Chris Lattner1afcace2011-07-09 17:41:24 +00001725 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001726 P.Error(Loc, "invalid use of a non-first-class type");
1727 return 0;
1728 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001729
Chris Lattnerdf986172009-01-02 07:01:27 +00001730 // Otherwise, create a new forward reference for this value and remember it.
1731 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001732 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001733 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001734 else
1735 FwdVal = new Argument(Ty, Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001736
Chris Lattnerdf986172009-01-02 07:01:27 +00001737 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1738 return FwdVal;
1739}
1740
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001741Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerdf986172009-01-02 07:01:27 +00001742 LocTy Loc) {
1743 // Look this name up in the normal function symbol table.
1744 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001745
Chris Lattnerdf986172009-01-02 07:01:27 +00001746 // If this is a forward reference for the value, see if we already created a
1747 // forward ref record.
1748 if (Val == 0) {
1749 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1750 I = ForwardRefValIDs.find(ID);
1751 if (I != ForwardRefValIDs.end())
1752 Val = I->second.first;
1753 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001754
Chris Lattnerdf986172009-01-02 07:01:27 +00001755 // If we have the value in the symbol table or fwd-ref table, return it.
1756 if (Val) {
1757 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001758 if (Ty->isLabelTy())
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001759 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerdf986172009-01-02 07:01:27 +00001760 else
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001761 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001762 getTypeString(Val->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001763 return 0;
1764 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001765
Chris Lattner1afcace2011-07-09 17:41:24 +00001766 if (!Ty->isFirstClassType() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001767 P.Error(Loc, "invalid use of a non-first-class type");
1768 return 0;
1769 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001770
Chris Lattnerdf986172009-01-02 07:01:27 +00001771 // Otherwise, create a new forward reference for this value and remember it.
1772 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001773 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001774 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001775 else
1776 FwdVal = new Argument(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001777
Chris Lattnerdf986172009-01-02 07:01:27 +00001778 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1779 return FwdVal;
1780}
1781
1782/// SetInstName - After an instruction is parsed and inserted into its
1783/// basic block, this installs its name.
1784bool LLParser::PerFunctionState::SetInstName(int NameID,
1785 const std::string &NameStr,
1786 LocTy NameLoc, Instruction *Inst) {
1787 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001788 if (Inst->getType()->isVoidTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001789 if (NameID != -1 || !NameStr.empty())
1790 return P.Error(NameLoc, "instructions returning void cannot have a name");
1791 return false;
1792 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001793
Chris Lattnerdf986172009-01-02 07:01:27 +00001794 // If this was a numbered instruction, verify that the instruction is the
1795 // expected value and resolve any forward references.
1796 if (NameStr.empty()) {
1797 // If neither a name nor an ID was specified, just use the next ID.
1798 if (NameID == -1)
1799 NameID = NumberedVals.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001800
Chris Lattnerdf986172009-01-02 07:01:27 +00001801 if (unsigned(NameID) != NumberedVals.size())
1802 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001803 Twine(NumberedVals.size()) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001804
Chris Lattnerdf986172009-01-02 07:01:27 +00001805 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
1806 ForwardRefValIDs.find(NameID);
1807 if (FI != ForwardRefValIDs.end()) {
1808 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00001809 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001810 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001811 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2b4f28e2009-09-02 15:02:57 +00001812 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001813 ForwardRefValIDs.erase(FI);
1814 }
1815
1816 NumberedVals.push_back(Inst);
1817 return false;
1818 }
1819
1820 // Otherwise, the instruction had a name. Resolve forward refs and set it.
1821 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1822 FI = ForwardRefVals.find(NameStr);
1823 if (FI != ForwardRefVals.end()) {
1824 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00001825 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001826 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001827 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes531552a2009-09-02 14:22:03 +00001828 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001829 ForwardRefVals.erase(FI);
1830 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001831
Chris Lattnerdf986172009-01-02 07:01:27 +00001832 // Set the name on the instruction.
1833 Inst->setName(NameStr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001834
Benjamin Krameraf812352010-10-16 11:28:23 +00001835 if (Inst->getName() != NameStr)
Daniel Dunbara279bc32009-09-20 02:20:51 +00001836 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001837 NameStr + "'");
1838 return false;
1839}
1840
1841/// GetBB - Get a basic block with the specified name or ID, creating a
1842/// forward reference record if needed.
1843BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
1844 LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001845 return cast_or_null<BasicBlock>(GetVal(Name,
1846 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00001847}
1848
1849BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001850 return cast_or_null<BasicBlock>(GetVal(ID,
1851 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00001852}
1853
1854/// DefineBB - Define the specified basic block, which is either named or
1855/// unnamed. If there is an error, this returns null otherwise it returns
1856/// the block being defined.
1857BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
1858 LocTy Loc) {
1859 BasicBlock *BB;
1860 if (Name.empty())
1861 BB = GetBB(NumberedVals.size(), Loc);
1862 else
1863 BB = GetBB(Name, Loc);
1864 if (BB == 0) return 0; // Already diagnosed error.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001865
Chris Lattnerdf986172009-01-02 07:01:27 +00001866 // Move the block to the end of the function. Forward ref'd blocks are
1867 // inserted wherever they happen to be referenced.
1868 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001869
Chris Lattnerdf986172009-01-02 07:01:27 +00001870 // Remove the block from forward ref sets.
1871 if (Name.empty()) {
1872 ForwardRefValIDs.erase(NumberedVals.size());
1873 NumberedVals.push_back(BB);
1874 } else {
1875 // BB forward references are already in the function symbol table.
1876 ForwardRefVals.erase(Name);
1877 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001878
Chris Lattnerdf986172009-01-02 07:01:27 +00001879 return BB;
1880}
1881
1882//===----------------------------------------------------------------------===//
1883// Constants.
1884//===----------------------------------------------------------------------===//
1885
1886/// ParseValID - Parse an abstract value that doesn't necessarily have a
1887/// type implied. For example, if we parse "4" we don't know what integer type
1888/// it has. The value will later be combined with its type and checked for
Victor Hernandez24e64df2010-01-10 07:14:18 +00001889/// sanity. PFS is used to convert function-local operands of metadata (since
1890/// metadata operands are not just parsed here but also converted to values).
1891/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezbf170d42010-01-05 22:22:14 +00001892bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001893 ID.Loc = Lex.getLoc();
1894 switch (Lex.getKind()) {
1895 default: return TokError("expected value token");
1896 case lltok::GlobalID: // @42
1897 ID.UIntVal = Lex.getUIntVal();
1898 ID.Kind = ValID::t_GlobalID;
1899 break;
1900 case lltok::GlobalVar: // @foo
1901 ID.StrVal = Lex.getStrVal();
1902 ID.Kind = ValID::t_GlobalName;
1903 break;
1904 case lltok::LocalVarID: // %42
1905 ID.UIntVal = Lex.getUIntVal();
1906 ID.Kind = ValID::t_LocalID;
1907 break;
1908 case lltok::LocalVar: // %foo
Chris Lattnerdf986172009-01-02 07:01:27 +00001909 ID.StrVal = Lex.getStrVal();
1910 ID.Kind = ValID::t_LocalName;
1911 break;
Dan Gohman83448032010-07-14 18:26:50 +00001912 case lltok::exclaim: // !42, !{...}, or !"foo"
1913 return ParseMetadataValue(ID, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00001914 case lltok::APSInt:
Daniel Dunbara279bc32009-09-20 02:20:51 +00001915 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00001916 ID.Kind = ValID::t_APSInt;
1917 break;
1918 case lltok::APFloat:
1919 ID.APFloatVal = Lex.getAPFloatVal();
1920 ID.Kind = ValID::t_APFloat;
1921 break;
1922 case lltok::kw_true:
Owen Anderson5defacc2009-07-31 17:39:07 +00001923 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001924 ID.Kind = ValID::t_Constant;
1925 break;
1926 case lltok::kw_false:
Owen Anderson5defacc2009-07-31 17:39:07 +00001927 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001928 ID.Kind = ValID::t_Constant;
1929 break;
1930 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
1931 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
1932 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001933
Chris Lattnerdf986172009-01-02 07:01:27 +00001934 case lltok::lbrace: {
1935 // ValID ::= '{' ConstVector '}'
1936 Lex.Lex();
1937 SmallVector<Constant*, 16> Elts;
1938 if (ParseGlobalValueVector(Elts) ||
1939 ParseToken(lltok::rbrace, "expected end of struct constant"))
1940 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001941
Chris Lattner1afcace2011-07-09 17:41:24 +00001942 ID.ConstantStructElts = new Constant*[Elts.size()];
1943 ID.UIntVal = Elts.size();
1944 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
1945 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerdf986172009-01-02 07:01:27 +00001946 return false;
1947 }
1948 case lltok::less: {
1949 // ValID ::= '<' ConstVector '>' --> Vector.
1950 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
1951 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001952 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001953
Chris Lattnerdf986172009-01-02 07:01:27 +00001954 SmallVector<Constant*, 16> Elts;
1955 LocTy FirstEltLoc = Lex.getLoc();
1956 if (ParseGlobalValueVector(Elts) ||
1957 (isPackedStruct &&
1958 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
1959 ParseToken(lltok::greater, "expected end of constant"))
1960 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001961
Chris Lattnerdf986172009-01-02 07:01:27 +00001962 if (isPackedStruct) {
Chris Lattner1afcace2011-07-09 17:41:24 +00001963 ID.ConstantStructElts = new Constant*[Elts.size()];
1964 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
1965 ID.UIntVal = Elts.size();
1966 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerdf986172009-01-02 07:01:27 +00001967 return false;
1968 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001969
Chris Lattnerdf986172009-01-02 07:01:27 +00001970 if (Elts.empty())
1971 return Error(ID.Loc, "constant vector must not be empty");
1972
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001973 if (!Elts[0]->getType()->isIntegerTy() &&
1974 !Elts[0]->getType()->isFloatingPointTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001975 return Error(FirstEltLoc,
1976 "vector elements must have integer or floating point type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001977
Chris Lattnerdf986172009-01-02 07:01:27 +00001978 // Verify that all the vector elements have the same type.
1979 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
1980 if (Elts[i]->getType() != Elts[0]->getType())
1981 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001982 "vector element #" + Twine(i) +
Chris Lattner0cd0d882011-06-18 21:18:23 +00001983 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001984
Chris Lattner2ca5c862011-02-15 00:14:00 +00001985 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00001986 ID.Kind = ValID::t_Constant;
1987 return false;
1988 }
1989 case lltok::lsquare: { // Array Constant
1990 Lex.Lex();
1991 SmallVector<Constant*, 16> Elts;
1992 LocTy FirstEltLoc = Lex.getLoc();
1993 if (ParseGlobalValueVector(Elts) ||
1994 ParseToken(lltok::rsquare, "expected end of array constant"))
1995 return true;
1996
1997 // Handle empty element.
1998 if (Elts.empty()) {
1999 // Use undef instead of an array because it's inconvenient to determine
2000 // the element type at this point, there being no elements to examine.
Chris Lattner081b5052009-01-05 07:52:51 +00002001 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerdf986172009-01-02 07:01:27 +00002002 return false;
2003 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002004
Chris Lattnerdf986172009-01-02 07:01:27 +00002005 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002006 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002007 getTypeString(Elts[0]->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002008
Owen Andersondebcb012009-07-29 22:17:13 +00002009 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002010
Chris Lattnerdf986172009-01-02 07:01:27 +00002011 // Verify all elements are correct type!
Chris Lattner6d6b3cc2009-01-02 08:49:06 +00002012 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002013 if (Elts[i]->getType() != Elts[0]->getType())
2014 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002015 "array element #" + Twine(i) +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002016 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00002017 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002018
Jay Foad26701082011-06-22 09:24:39 +00002019 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00002020 ID.Kind = ValID::t_Constant;
2021 return false;
2022 }
2023 case lltok::kw_c: // c "foo"
2024 Lex.Lex();
Owen Anderson1d0be152009-08-13 21:58:54 +00002025 ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false);
Chris Lattnerdf986172009-01-02 07:01:27 +00002026 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2027 ID.Kind = ValID::t_Constant;
2028 return false;
2029
2030 case lltok::kw_asm: {
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002031 // ValID ::= 'asm' SideEffect? AlignStack? STRINGCONSTANT ',' STRINGCONSTANT
2032 bool HasSideEffect, AlignStack;
Chris Lattnerdf986172009-01-02 07:01:27 +00002033 Lex.Lex();
2034 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002035 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002036 ParseStringConstant(ID.StrVal) ||
2037 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002038 ParseToken(lltok::StringConstant, "expected constraint string"))
2039 return true;
2040 ID.StrVal2 = Lex.getStrVal();
Daniel Dunbarf0bb41c2009-11-07 23:51:55 +00002041 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002042 ID.Kind = ValID::t_InlineAsm;
2043 return false;
2044 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002045
Chris Lattner09d9ef42009-10-28 03:39:23 +00002046 case lltok::kw_blockaddress: {
2047 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2048 Lex.Lex();
2049
2050 ValID Fn, Label;
2051 LocTy FnLoc, LabelLoc;
2052
2053 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2054 ParseValID(Fn) ||
2055 ParseToken(lltok::comma, "expected comma in block address expression")||
2056 ParseValID(Label) ||
2057 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2058 return true;
Chris Lattnercdfc9402009-11-01 01:27:45 +00002059
Chris Lattner09d9ef42009-10-28 03:39:23 +00002060 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2061 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattnercdfc9402009-11-01 01:27:45 +00002062 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner09d9ef42009-10-28 03:39:23 +00002063 return Error(Label.Loc, "expected basic block name in blockaddress");
2064
2065 // Make a global variable as a placeholder for this reference.
2066 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2067 false, GlobalValue::InternalLinkage,
2068 0, "");
2069 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2070 ID.ConstantVal = FwdRef;
2071 ID.Kind = ValID::t_Constant;
2072 return false;
2073 }
2074
Chris Lattnerdf986172009-01-02 07:01:27 +00002075 case lltok::kw_trunc:
2076 case lltok::kw_zext:
2077 case lltok::kw_sext:
2078 case lltok::kw_fptrunc:
2079 case lltok::kw_fpext:
2080 case lltok::kw_bitcast:
2081 case lltok::kw_uitofp:
2082 case lltok::kw_sitofp:
2083 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002084 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00002085 case lltok::kw_inttoptr:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002086 case lltok::kw_ptrtoint: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002087 unsigned Opc = Lex.getUIntVal();
Chris Lattner1afcace2011-07-09 17:41:24 +00002088 Type *DestTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002089 Constant *SrcVal;
2090 Lex.Lex();
2091 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2092 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman24b108b2009-06-15 21:52:11 +00002093 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002094 ParseType(DestTy) ||
2095 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2096 return true;
2097 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2098 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002099 getTypeString(SrcVal->getType()) + "' to '" +
2100 getTypeString(DestTy) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002101 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Andersonfba933c2009-07-01 23:57:11 +00002102 SrcVal, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00002103 ID.Kind = ValID::t_Constant;
2104 return false;
2105 }
2106 case lltok::kw_extractvalue: {
2107 Lex.Lex();
2108 Constant *Val;
2109 SmallVector<unsigned, 4> Indices;
2110 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2111 ParseGlobalTypeAndValue(Val) ||
2112 ParseIndexList(Indices) ||
2113 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2114 return true;
Devang Patele8bc45a2009-11-03 19:06:07 +00002115
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002116 if (!Val->getType()->isAggregateType())
2117 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002118 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002119 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002120 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerdf986172009-01-02 07:01:27 +00002121 ID.Kind = ValID::t_Constant;
2122 return false;
2123 }
2124 case lltok::kw_insertvalue: {
2125 Lex.Lex();
2126 Constant *Val0, *Val1;
2127 SmallVector<unsigned, 4> Indices;
2128 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2129 ParseGlobalTypeAndValue(Val0) ||
2130 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2131 ParseGlobalTypeAndValue(Val1) ||
2132 ParseIndexList(Indices) ||
2133 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2134 return true;
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002135 if (!Val0->getType()->isAggregateType())
2136 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002137 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002138 return Error(ID.Loc, "invalid indices for insertvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00002139 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerdf986172009-01-02 07:01:27 +00002140 ID.Kind = ValID::t_Constant;
2141 return false;
2142 }
2143 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002144 case lltok::kw_fcmp: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002145 unsigned PredVal, Opc = Lex.getUIntVal();
2146 Constant *Val0, *Val1;
2147 Lex.Lex();
2148 if (ParseCmpPredicate(PredVal, Opc) ||
2149 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2150 ParseGlobalTypeAndValue(Val0) ||
2151 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2152 ParseGlobalTypeAndValue(Val1) ||
2153 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2154 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002155
Chris Lattnerdf986172009-01-02 07:01:27 +00002156 if (Val0->getType() != Val1->getType())
2157 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002158
Chris Lattnerdf986172009-01-02 07:01:27 +00002159 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002160
Chris Lattnerdf986172009-01-02 07:01:27 +00002161 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002162 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002163 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002164 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002165 } else {
2166 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002167 if (!Val0->getType()->isIntOrIntVectorTy() &&
Duncan Sands1df98592010-02-16 11:11:14 +00002168 !Val0->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002169 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002170 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002171 }
2172 ID.Kind = ValID::t_Constant;
2173 return false;
2174 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002175
Chris Lattnerdf986172009-01-02 07:01:27 +00002176 // Binary Operators.
2177 case lltok::kw_add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002178 case lltok::kw_fadd:
Chris Lattnerdf986172009-01-02 07:01:27 +00002179 case lltok::kw_sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002180 case lltok::kw_fsub:
Chris Lattnerdf986172009-01-02 07:01:27 +00002181 case lltok::kw_mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002182 case lltok::kw_fmul:
Chris Lattnerdf986172009-01-02 07:01:27 +00002183 case lltok::kw_udiv:
2184 case lltok::kw_sdiv:
2185 case lltok::kw_fdiv:
2186 case lltok::kw_urem:
2187 case lltok::kw_srem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002188 case lltok::kw_frem:
2189 case lltok::kw_shl:
2190 case lltok::kw_lshr:
2191 case lltok::kw_ashr: {
Dan Gohman59858cf2009-07-27 16:11:46 +00002192 bool NUW = false;
2193 bool NSW = false;
2194 bool Exact = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002195 unsigned Opc = Lex.getUIntVal();
2196 Constant *Val0, *Val1;
2197 Lex.Lex();
Dan Gohman59858cf2009-07-27 16:11:46 +00002198 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnerf067d582011-02-07 16:40:21 +00002199 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2200 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002201 if (EatIfPresent(lltok::kw_nuw))
2202 NUW = true;
2203 if (EatIfPresent(lltok::kw_nsw)) {
2204 NSW = true;
2205 if (EatIfPresent(lltok::kw_nuw))
2206 NUW = true;
2207 }
Chris Lattnerf067d582011-02-07 16:40:21 +00002208 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2209 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002210 if (EatIfPresent(lltok::kw_exact))
2211 Exact = true;
2212 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002213 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2214 ParseGlobalTypeAndValue(Val0) ||
2215 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2216 ParseGlobalTypeAndValue(Val1) ||
2217 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2218 return true;
2219 if (Val0->getType() != Val1->getType())
2220 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002221 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002222 if (NUW)
2223 return Error(ModifierLoc, "nuw only applies to integer operations");
2224 if (NSW)
2225 return Error(ModifierLoc, "nsw only applies to integer operations");
2226 }
Dan Gohman1eaac532010-05-03 22:44:19 +00002227 // Check that the type is valid for the operator.
2228 switch (Opc) {
2229 case Instruction::Add:
2230 case Instruction::Sub:
2231 case Instruction::Mul:
2232 case Instruction::UDiv:
2233 case Instruction::SDiv:
2234 case Instruction::URem:
2235 case Instruction::SRem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002236 case Instruction::Shl:
2237 case Instruction::AShr:
2238 case Instruction::LShr:
Dan Gohman1eaac532010-05-03 22:44:19 +00002239 if (!Val0->getType()->isIntOrIntVectorTy())
2240 return Error(ID.Loc, "constexpr requires integer operands");
2241 break;
2242 case Instruction::FAdd:
2243 case Instruction::FSub:
2244 case Instruction::FMul:
2245 case Instruction::FDiv:
2246 case Instruction::FRem:
2247 if (!Val0->getType()->isFPOrFPVectorTy())
2248 return Error(ID.Loc, "constexpr requires fp operands");
2249 break;
2250 default: llvm_unreachable("Unknown binary operator!");
2251 }
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002252 unsigned Flags = 0;
2253 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2254 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00002255 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002256 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman59858cf2009-07-27 16:11:46 +00002257 ID.ConstantVal = C;
Chris Lattnerdf986172009-01-02 07:01:27 +00002258 ID.Kind = ValID::t_Constant;
2259 return false;
2260 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002261
Chris Lattnerdf986172009-01-02 07:01:27 +00002262 // Logical Operations
Chris Lattnerdf986172009-01-02 07:01:27 +00002263 case lltok::kw_and:
2264 case lltok::kw_or:
2265 case lltok::kw_xor: {
2266 unsigned Opc = Lex.getUIntVal();
2267 Constant *Val0, *Val1;
2268 Lex.Lex();
2269 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2270 ParseGlobalTypeAndValue(Val0) ||
2271 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2272 ParseGlobalTypeAndValue(Val1) ||
2273 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2274 return true;
2275 if (Val0->getType() != Val1->getType())
2276 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002277 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002278 return Error(ID.Loc,
2279 "constexpr requires integer or integer vector operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002280 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002281 ID.Kind = ValID::t_Constant;
2282 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002283 }
2284
Chris Lattnerdf986172009-01-02 07:01:27 +00002285 case lltok::kw_getelementptr:
2286 case lltok::kw_shufflevector:
2287 case lltok::kw_insertelement:
2288 case lltok::kw_extractelement:
2289 case lltok::kw_select: {
2290 unsigned Opc = Lex.getUIntVal();
2291 SmallVector<Constant*, 16> Elts;
Dan Gohmandd8004d2009-07-27 21:53:46 +00002292 bool InBounds = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002293 Lex.Lex();
Dan Gohmandd8004d2009-07-27 21:53:46 +00002294 if (Opc == Instruction::GetElementPtr)
Dan Gohmandcb40a32009-07-29 15:58:36 +00002295 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002296 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2297 ParseGlobalValueVector(Elts) ||
2298 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2299 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002300
Chris Lattnerdf986172009-01-02 07:01:27 +00002301 if (Opc == Instruction::GetElementPtr) {
Duncan Sands1df98592010-02-16 11:11:14 +00002302 if (Elts.size() == 0 || !Elts[0]->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002303 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002304
Jay Foaddab3d292011-07-21 14:31:17 +00002305 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
Jay Foada9203102011-07-25 09:48:08 +00002306 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00002307 return Error(ID.Loc, "invalid indices for getelementptr");
Jay Foad4b5e2072011-07-21 15:15:37 +00002308 ID.ConstantVal = ConstantExpr::getGetElementPtr(Elts[0], Indices,
2309 InBounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002310 } else if (Opc == Instruction::Select) {
2311 if (Elts.size() != 3)
2312 return Error(ID.Loc, "expected three operands to select");
2313 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2314 Elts[2]))
2315 return Error(ID.Loc, Reason);
Owen Andersonbaf3c402009-07-29 18:55:55 +00002316 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002317 } else if (Opc == Instruction::ShuffleVector) {
2318 if (Elts.size() != 3)
2319 return Error(ID.Loc, "expected three operands to shufflevector");
2320 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2321 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Andersonfba933c2009-07-01 23:57:11 +00002322 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002323 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002324 } else if (Opc == Instruction::ExtractElement) {
2325 if (Elts.size() != 2)
2326 return Error(ID.Loc, "expected two operands to extractelement");
2327 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2328 return Error(ID.Loc, "invalid extractelement operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002329 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002330 } else {
2331 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2332 if (Elts.size() != 3)
2333 return Error(ID.Loc, "expected three operands to insertelement");
2334 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2335 return Error(ID.Loc, "invalid insertelement operands");
Owen Andersonfba933c2009-07-01 23:57:11 +00002336 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002337 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002338 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002339
Chris Lattnerdf986172009-01-02 07:01:27 +00002340 ID.Kind = ValID::t_Constant;
2341 return false;
2342 }
2343 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002344
Chris Lattnerdf986172009-01-02 07:01:27 +00002345 Lex.Lex();
2346 return false;
2347}
2348
2349/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002350bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Victor Hernandez92f238d2010-01-11 22:31:58 +00002351 C = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002352 ValID ID;
Victor Hernandez92f238d2010-01-11 22:31:58 +00002353 Value *V = NULL;
2354 bool Parsed = ParseValID(ID) ||
2355 ConvertValIDToValue(Ty, ID, V, NULL);
2356 if (V && !(C = dyn_cast<Constant>(V)))
2357 return Error(ID.Loc, "global values must be constants");
2358 return Parsed;
Chris Lattnerdf986172009-01-02 07:01:27 +00002359}
2360
Victor Hernandez92f238d2010-01-11 22:31:58 +00002361bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002362 Type *Ty = 0;
2363 return ParseType(Ty) ||
2364 ParseGlobalValue(Ty, V);
Victor Hernandez92f238d2010-01-11 22:31:58 +00002365}
2366
2367/// ParseGlobalValueVector
2368/// ::= /*empty*/
2369/// ::= TypeAndValue (',' TypeAndValue)*
2370bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2371 // Empty list.
2372 if (Lex.getKind() == lltok::rbrace ||
2373 Lex.getKind() == lltok::rsquare ||
2374 Lex.getKind() == lltok::greater ||
2375 Lex.getKind() == lltok::rparen)
2376 return false;
2377
2378 Constant *C;
2379 if (ParseGlobalTypeAndValue(C)) return true;
2380 Elts.push_back(C);
2381
2382 while (EatIfPresent(lltok::comma)) {
2383 if (ParseGlobalTypeAndValue(C)) return true;
2384 Elts.push_back(C);
2385 }
2386
2387 return false;
2388}
2389
Dan Gohman309b3af2010-08-24 02:24:03 +00002390bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2391 assert(Lex.getKind() == lltok::lbrace);
2392 Lex.Lex();
2393
2394 SmallVector<Value*, 16> Elts;
2395 if (ParseMDNodeVector(Elts, PFS) ||
2396 ParseToken(lltok::rbrace, "expected end of metadata node"))
2397 return true;
2398
Jay Foadec9186b2011-04-21 19:59:31 +00002399 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohman309b3af2010-08-24 02:24:03 +00002400 ID.Kind = ValID::t_MDNode;
2401 return false;
2402}
2403
Dan Gohman83448032010-07-14 18:26:50 +00002404/// ParseMetadataValue
2405/// ::= !42
2406/// ::= !{...}
2407/// ::= !"string"
2408bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2409 assert(Lex.getKind() == lltok::exclaim);
2410 Lex.Lex();
2411
2412 // MDNode:
2413 // !{ ... }
Dan Gohman309b3af2010-08-24 02:24:03 +00002414 if (Lex.getKind() == lltok::lbrace)
2415 return ParseMetadataListValue(ID, PFS);
Dan Gohman83448032010-07-14 18:26:50 +00002416
2417 // Standalone metadata reference
2418 // !42
2419 if (Lex.getKind() == lltok::APSInt) {
2420 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2421 ID.Kind = ValID::t_MDNode;
2422 return false;
2423 }
2424
2425 // MDString:
2426 // ::= '!' STRINGCONSTANT
2427 if (ParseMDString(ID.MDStringVal)) return true;
2428 ID.Kind = ValID::t_MDString;
2429 return false;
2430}
2431
Victor Hernandez92f238d2010-01-11 22:31:58 +00002432
2433//===----------------------------------------------------------------------===//
2434// Function Parsing.
2435//===----------------------------------------------------------------------===//
2436
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002437bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez92f238d2010-01-11 22:31:58 +00002438 PerFunctionState *PFS) {
Duncan Sands1df98592010-02-16 11:11:14 +00002439 if (Ty->isFunctionTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002440 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002441
Chris Lattnerdf986172009-01-02 07:01:27 +00002442 switch (ID.Kind) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002443 default: llvm_unreachable("Unknown ValID!");
Chris Lattnerdf986172009-01-02 07:01:27 +00002444 case ValID::t_LocalID:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002445 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2446 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
2447 return (V == 0);
Chris Lattnerdf986172009-01-02 07:01:27 +00002448 case ValID::t_LocalName:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002449 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2450 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
2451 return (V == 0);
2452 case ValID::t_InlineAsm: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002453 PointerType *PTy = dyn_cast<PointerType>(Ty);
2454 FunctionType *FTy =
Victor Hernandez92f238d2010-01-11 22:31:58 +00002455 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2456 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2457 return Error(ID.Loc, "invalid type for inline asm constraint string");
2458 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
2459 return false;
2460 }
2461 case ValID::t_MDNode:
2462 if (!Ty->isMetadataTy())
2463 return Error(ID.Loc, "metadata value must have metadata type");
2464 V = ID.MDNodeVal;
2465 return false;
2466 case ValID::t_MDString:
2467 if (!Ty->isMetadataTy())
2468 return Error(ID.Loc, "metadata value must have metadata type");
2469 V = ID.MDStringVal;
2470 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002471 case ValID::t_GlobalName:
2472 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2473 return V == 0;
2474 case ValID::t_GlobalID:
2475 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2476 return V == 0;
2477 case ValID::t_APSInt:
Duncan Sands1df98592010-02-16 11:11:14 +00002478 if (!Ty->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002479 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad40f8f622010-12-07 08:25:19 +00002480 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersoneed707b2009-07-24 23:12:02 +00002481 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002482 return false;
2483 case ValID::t_APFloat:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002484 if (!Ty->isFloatingPointTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002485 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2486 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002487
Chris Lattnerdf986172009-01-02 07:01:27 +00002488 // The lexer has no type info, so builds all float and double FP constants
2489 // as double. Fix this here. Long double does not need this.
2490 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002491 Ty->isFloatTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002492 bool Ignored;
2493 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2494 &Ignored);
2495 }
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002496 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002497
Chris Lattner959873d2009-01-05 18:24:23 +00002498 if (V->getType() != Ty)
2499 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00002500 getTypeString(Ty) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002501
Chris Lattnerdf986172009-01-02 07:01:27 +00002502 return false;
2503 case ValID::t_Null:
Duncan Sands1df98592010-02-16 11:11:14 +00002504 if (!Ty->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002505 return Error(ID.Loc, "null must be a pointer type");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002506 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerdf986172009-01-02 07:01:27 +00002507 return false;
2508 case ValID::t_Undef:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002509 // FIXME: LabelTy should not be a first-class type.
Chris Lattner1afcace2011-07-09 17:41:24 +00002510 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002511 return Error(ID.Loc, "invalid type for undef constant");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002512 V = UndefValue::get(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002513 return false;
Chris Lattner081b5052009-01-05 07:52:51 +00002514 case ValID::t_EmptyArray:
Duncan Sands1df98592010-02-16 11:11:14 +00002515 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner081b5052009-01-05 07:52:51 +00002516 return Error(ID.Loc, "invalid empty array initializer");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002517 V = UndefValue::get(Ty);
Chris Lattner081b5052009-01-05 07:52:51 +00002518 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002519 case ValID::t_Zero:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002520 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002521 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002522 return Error(ID.Loc, "invalid type for null constant");
Owen Andersona7235ea2009-07-31 20:28:14 +00002523 V = Constant::getNullValue(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002524 return false;
2525 case ValID::t_Constant:
Chris Lattner61c70e92010-08-28 04:09:24 +00002526 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerdf986172009-01-02 07:01:27 +00002527 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002528
Chris Lattnerdf986172009-01-02 07:01:27 +00002529 V = ID.ConstantVal;
2530 return false;
Chris Lattner1afcace2011-07-09 17:41:24 +00002531 case ValID::t_ConstantStruct:
2532 case ValID::t_PackedConstantStruct:
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002533 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002534 if (ST->getNumElements() != ID.UIntVal)
2535 return Error(ID.Loc,
2536 "initializer with struct type has wrong # elements");
2537 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
2538 return Error(ID.Loc, "packed'ness of initializer and type don't match");
2539
2540 // Verify that the elements are compatible with the structtype.
2541 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
2542 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
2543 return Error(ID.Loc, "element " + Twine(i) +
2544 " of struct initializer doesn't match struct element type");
2545
Frits van Bommel39b5abf2011-07-18 12:00:32 +00002546 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
2547 ID.UIntVal));
Chris Lattner1afcace2011-07-09 17:41:24 +00002548 } else
2549 return Error(ID.Loc, "constant expression type mismatch");
2550 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002551 }
2552}
Daniel Dunbara279bc32009-09-20 02:20:51 +00002553
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002554bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002555 V = 0;
2556 ValID ID;
Chris Lattner1afcace2011-07-09 17:41:24 +00002557 return ParseValID(ID, PFS) ||
2558 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002559}
2560
Chris Lattner1afcace2011-07-09 17:41:24 +00002561bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
2562 Type *Ty = 0;
2563 return ParseType(Ty) ||
2564 ParseValue(Ty, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002565}
2566
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002567bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2568 PerFunctionState &PFS) {
2569 Value *V;
2570 Loc = Lex.getLoc();
2571 if (ParseTypeAndValue(V, PFS)) return true;
2572 if (!isa<BasicBlock>(V))
2573 return Error(Loc, "expected a basic block");
2574 BB = cast<BasicBlock>(V);
2575 return false;
2576}
2577
2578
Chris Lattnerdf986172009-01-02 07:01:27 +00002579/// FunctionHeader
2580/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindolabea46262011-01-08 16:42:36 +00002581/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Chris Lattnerdf986172009-01-02 07:01:27 +00002582/// OptionalAlign OptGC
2583bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2584 // Parse the linkage.
2585 LocTy LinkageLoc = Lex.getLoc();
2586 unsigned Linkage;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002587
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002588 unsigned Visibility, RetAttrs;
2589 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00002590 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002591 LocTy RetTypeLoc = Lex.getLoc();
2592 if (ParseOptionalLinkage(Linkage) ||
2593 ParseOptionalVisibility(Visibility) ||
2594 ParseOptionalCallingConv(CC) ||
2595 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00002596 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerdf986172009-01-02 07:01:27 +00002597 return true;
2598
2599 // Verify that the linkage is ok.
2600 switch ((GlobalValue::LinkageTypes)Linkage) {
2601 case GlobalValue::ExternalLinkage:
2602 break; // always ok.
2603 case GlobalValue::DLLImportLinkage:
Duncan Sands5f4ee1f2009-03-11 08:08:06 +00002604 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002605 if (isDefine)
2606 return Error(LinkageLoc, "invalid linkage for function definition");
2607 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +00002608 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +00002609 case GlobalValue::LinkerPrivateLinkage:
Bill Wendling5e721d72010-07-01 21:55:59 +00002610 case GlobalValue::LinkerPrivateWeakLinkage:
Bill Wendling55ae5152010-08-20 22:05:50 +00002611 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002612 case GlobalValue::InternalLinkage:
Nick Lewycky55f64db2009-04-13 07:02:02 +00002613 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002614 case GlobalValue::LinkOnceAnyLinkage:
2615 case GlobalValue::LinkOnceODRLinkage:
2616 case GlobalValue::WeakAnyLinkage:
2617 case GlobalValue::WeakODRLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002618 case GlobalValue::DLLExportLinkage:
2619 if (!isDefine)
2620 return Error(LinkageLoc, "invalid linkage for function declaration");
2621 break;
2622 case GlobalValue::AppendingLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +00002623 case GlobalValue::CommonLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002624 return Error(LinkageLoc, "invalid function linkage type");
2625 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002626
Chris Lattner1afcace2011-07-09 17:41:24 +00002627 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerdf986172009-01-02 07:01:27 +00002628 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002629
Chris Lattnerdf986172009-01-02 07:01:27 +00002630 LocTy NameLoc = Lex.getLoc();
Chris Lattnerf570e622009-02-18 21:48:13 +00002631
2632 std::string FunctionName;
2633 if (Lex.getKind() == lltok::GlobalVar) {
2634 FunctionName = Lex.getStrVal();
2635 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
2636 unsigned NameID = Lex.getUIntVal();
2637
2638 if (NameID != NumberedVals.size())
2639 return TokError("function expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002640 Twine(NumberedVals.size()) + "'");
Chris Lattnerf570e622009-02-18 21:48:13 +00002641 } else {
2642 return TokError("expected function name");
2643 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002644
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002645 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002646
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002647 if (Lex.getKind() != lltok::lparen)
Chris Lattnerdf986172009-01-02 07:01:27 +00002648 return TokError("expected '(' in function argument list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002649
Chris Lattner1afcace2011-07-09 17:41:24 +00002650 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerdf986172009-01-02 07:01:27 +00002651 bool isVarArg;
Chris Lattnerdf986172009-01-02 07:01:27 +00002652 unsigned FuncAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00002653 std::string Section;
Chris Lattnerdf986172009-01-02 07:01:27 +00002654 unsigned Alignment;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002655 std::string GC;
Rafael Espindola3971df52011-01-25 19:09:56 +00002656 bool UnnamedAddr;
2657 LocTy UnnamedAddrLoc;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002658
Chris Lattner1afcace2011-07-09 17:41:24 +00002659 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola3971df52011-01-25 19:09:56 +00002660 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
2661 &UnnamedAddrLoc) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002662 ParseOptionalAttrs(FuncAttrs, 2) ||
2663 (EatIfPresent(lltok::kw_section) &&
2664 ParseStringConstant(Section)) ||
2665 ParseOptionalAlignment(Alignment) ||
2666 (EatIfPresent(lltok::kw_gc) &&
2667 ParseStringConstant(GC)))
2668 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00002669
2670 // If the alignment was parsed as an attribute, move to the alignment field.
2671 if (FuncAttrs & Attribute::Alignment) {
2672 Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs);
2673 FuncAttrs &= ~Attribute::Alignment;
2674 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002675
Chris Lattnerdf986172009-01-02 07:01:27 +00002676 // Okay, if we got here, the function is syntactically valid. Convert types
2677 // and do semantic checks.
Jay Foad5fdd6c82011-07-12 14:06:48 +00002678 std::vector<Type*> ParamTypeList;
Chris Lattnerdf986172009-01-02 07:01:27 +00002679 SmallVector<AttributeWithIndex, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002680
Chris Lattnerdf986172009-01-02 07:01:27 +00002681 if (RetAttrs != Attribute::None)
2682 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002683
Chris Lattnerdf986172009-01-02 07:01:27 +00002684 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattner1afcace2011-07-09 17:41:24 +00002685 ParamTypeList.push_back(ArgList[i].Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002686 if (ArgList[i].Attrs != Attribute::None)
2687 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
2688 }
2689
2690 if (FuncAttrs != Attribute::None)
2691 Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs));
2692
2693 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002694
Benjamin Kramerf0127052010-01-05 13:12:22 +00002695 if (PAL.paramHasAttr(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002696 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2697
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002698 FunctionType *FT =
Owen Andersondebcb012009-07-29 22:17:13 +00002699 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002700 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerdf986172009-01-02 07:01:27 +00002701
2702 Fn = 0;
2703 if (!FunctionName.empty()) {
2704 // If this was a definition of a forward reference, remove the definition
2705 // from the forward reference table and fill in the forward ref.
2706 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2707 ForwardRefVals.find(FunctionName);
2708 if (FRVI != ForwardRefVals.end()) {
2709 Fn = M->getFunction(FunctionName);
Chris Lattnerf1cfb952010-04-20 04:49:11 +00002710 if (Fn->getType() != PFT)
2711 return Error(FRVI->second.second, "invalid forward reference to "
2712 "function '" + FunctionName + "' with wrong type!");
2713
Chris Lattnerdf986172009-01-02 07:01:27 +00002714 ForwardRefVals.erase(FRVI);
2715 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattnerd5890992011-06-17 07:06:44 +00002716 // Reject redefinitions.
2717 return Error(NameLoc, "invalid redefinition of function '" +
2718 FunctionName + "'");
Chris Lattner1d871c52009-10-25 23:22:50 +00002719 } else if (M->getNamedValue(FunctionName)) {
2720 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002721 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002722
Dan Gohman41905542009-08-29 23:37:49 +00002723 } else {
Chris Lattnerdf986172009-01-02 07:01:27 +00002724 // If this is a definition of a forward referenced function, make sure the
2725 // types agree.
2726 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
2727 = ForwardRefValIDs.find(NumberedVals.size());
2728 if (I != ForwardRefValIDs.end()) {
2729 Fn = cast<Function>(I->second.first);
2730 if (Fn->getType() != PFT)
2731 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002732 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerdf986172009-01-02 07:01:27 +00002733 ForwardRefValIDs.erase(I);
2734 }
2735 }
2736
2737 if (Fn == 0)
2738 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2739 else // Move the forward-reference to the correct spot in the module.
2740 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2741
2742 if (FunctionName.empty())
2743 NumberedVals.push_back(Fn);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002744
Chris Lattnerdf986172009-01-02 07:01:27 +00002745 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
2746 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
2747 Fn->setCallingConv(CC);
2748 Fn->setAttributes(PAL);
Rafael Espindolabea46262011-01-08 16:42:36 +00002749 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerdf986172009-01-02 07:01:27 +00002750 Fn->setAlignment(Alignment);
2751 Fn->setSection(Section);
2752 if (!GC.empty()) Fn->setGC(GC.c_str());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002753
Chris Lattnerdf986172009-01-02 07:01:27 +00002754 // Add all of the arguments we parsed to the function.
2755 Function::arg_iterator ArgIt = Fn->arg_begin();
2756 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
2757 // If the argument has a name, insert it into the argument symbol table.
2758 if (ArgList[i].Name.empty()) continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002759
Chris Lattnerdf986172009-01-02 07:01:27 +00002760 // Set the name, if it conflicted, it will be auto-renamed.
2761 ArgIt->setName(ArgList[i].Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002762
Benjamin Krameraf812352010-10-16 11:28:23 +00002763 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerdf986172009-01-02 07:01:27 +00002764 return Error(ArgList[i].Loc, "redefinition of argument '%" +
2765 ArgList[i].Name + "'");
2766 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002767
Chris Lattnerdf986172009-01-02 07:01:27 +00002768 return false;
2769}
2770
2771
2772/// ParseFunctionBody
2773/// ::= '{' BasicBlock+ '}'
Chris Lattnerdf986172009-01-02 07:01:27 +00002774///
2775bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner6b7c89e2011-06-17 06:42:57 +00002776 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerdf986172009-01-02 07:01:27 +00002777 return TokError("expected '{' in function body");
2778 Lex.Lex(); // eat the {.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002779
Chris Lattner09d9ef42009-10-28 03:39:23 +00002780 int FunctionNumber = -1;
2781 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
2782
2783 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002784
Chris Lattner2fdf8db2010-01-09 19:20:07 +00002785 // We need at least one basic block.
Chris Lattner6b7c89e2011-06-17 06:42:57 +00002786 if (Lex.getKind() == lltok::rbrace)
Chris Lattner2fdf8db2010-01-09 19:20:07 +00002787 return TokError("function body requires at least one basic block");
2788
Chris Lattner6b7c89e2011-06-17 06:42:57 +00002789 while (Lex.getKind() != lltok::rbrace)
Chris Lattnerdf986172009-01-02 07:01:27 +00002790 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002791
Chris Lattnerdf986172009-01-02 07:01:27 +00002792 // Eat the }.
2793 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002794
Chris Lattnerdf986172009-01-02 07:01:27 +00002795 // Verify function is ok.
Chris Lattner09d9ef42009-10-28 03:39:23 +00002796 return PFS.FinishFunction();
Chris Lattnerdf986172009-01-02 07:01:27 +00002797}
2798
2799/// ParseBasicBlock
2800/// ::= LabelStr? Instruction*
2801bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
2802 // If this basic block starts out with a name, remember it.
2803 std::string Name;
2804 LocTy NameLoc = Lex.getLoc();
2805 if (Lex.getKind() == lltok::LabelStr) {
2806 Name = Lex.getStrVal();
2807 Lex.Lex();
2808 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002809
Chris Lattnerdf986172009-01-02 07:01:27 +00002810 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2811 if (BB == 0) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002812
Chris Lattnerdf986172009-01-02 07:01:27 +00002813 std::string NameStr;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002814
Chris Lattnerdf986172009-01-02 07:01:27 +00002815 // Parse the instructions in this block until we get a terminator.
2816 Instruction *Inst;
Chris Lattner1340dd32009-12-30 05:48:36 +00002817 SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +00002818 do {
2819 // This instruction may have three possibilities for a name: a) none
2820 // specified, b) name specified "%foo =", c) number specified: "%4 =".
2821 LocTy NameLoc = Lex.getLoc();
2822 int NameID = -1;
2823 NameStr = "";
Daniel Dunbara279bc32009-09-20 02:20:51 +00002824
Chris Lattnerdf986172009-01-02 07:01:27 +00002825 if (Lex.getKind() == lltok::LocalVarID) {
2826 NameID = Lex.getUIntVal();
2827 Lex.Lex();
2828 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
2829 return true;
Chris Lattner7a1b9bd2011-06-17 06:36:20 +00002830 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002831 NameStr = Lex.getStrVal();
2832 Lex.Lex();
2833 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2834 return true;
2835 }
Devang Patelf633a062009-09-17 23:04:48 +00002836
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002837 switch (ParseInstruction(Inst, BB, PFS)) {
2838 default: assert(0 && "Unknown ParseInstruction result!");
2839 case InstError: return true;
2840 case InstNormal:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002841 BB->getInstList().push_back(Inst);
2842
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002843 // With a normal result, we check to see if the instruction is followed by
2844 // a comma and metadata.
2845 if (EatIfPresent(lltok::comma))
Dan Gohman9d072f52010-08-24 02:05:17 +00002846 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002847 return true;
2848 break;
2849 case InstExtraComma:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002850 BB->getInstList().push_back(Inst);
2851
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002852 // If the instruction parser ate an extra comma at the end of it, it
2853 // *must* be followed by metadata.
Dan Gohman9d072f52010-08-24 02:05:17 +00002854 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002855 return true;
2856 break;
2857 }
Devang Patelf633a062009-09-17 23:04:48 +00002858
Chris Lattnerdf986172009-01-02 07:01:27 +00002859 // Set the name on the instruction.
2860 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
2861 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002862
Chris Lattnerdf986172009-01-02 07:01:27 +00002863 return false;
2864}
2865
2866//===----------------------------------------------------------------------===//
2867// Instruction Parsing.
2868//===----------------------------------------------------------------------===//
2869
2870/// ParseInstruction - Parse one of the many different instructions.
2871///
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002872int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
2873 PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002874 lltok::Kind Token = Lex.getKind();
2875 if (Token == lltok::Eof)
2876 return TokError("found end of file when expecting more instructions");
2877 LocTy Loc = Lex.getLoc();
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002878 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002879 Lex.Lex(); // Eat the keyword.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002880
Chris Lattnerdf986172009-01-02 07:01:27 +00002881 switch (Token) {
2882 default: return Error(Loc, "expected instruction opcode");
2883 // Terminator Instructions.
Owen Anderson1d0be152009-08-13 21:58:54 +00002884 case lltok::kw_unwind: Inst = new UnwindInst(Context); return false;
2885 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002886 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
2887 case lltok::kw_br: return ParseBr(Inst, PFS);
2888 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +00002889 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002890 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingdccc03b2011-07-31 06:30:59 +00002891 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002892 // Binary Operators.
2893 case lltok::kw_add:
2894 case lltok::kw_sub:
Chris Lattnerf067d582011-02-07 16:40:21 +00002895 case lltok::kw_mul:
2896 case lltok::kw_shl: {
Chris Lattnerf067d582011-02-07 16:40:21 +00002897 bool NUW = EatIfPresent(lltok::kw_nuw);
2898 bool NSW = EatIfPresent(lltok::kw_nsw);
2899 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
2900
2901 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
2902
2903 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
2904 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
2905 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00002906 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002907 case lltok::kw_fadd:
2908 case lltok::kw_fsub:
2909 case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
2910
Chris Lattner35bda892011-02-06 21:44:57 +00002911 case lltok::kw_sdiv:
Chris Lattnerf067d582011-02-07 16:40:21 +00002912 case lltok::kw_udiv:
2913 case lltok::kw_lshr:
2914 case lltok::kw_ashr: {
2915 bool Exact = EatIfPresent(lltok::kw_exact);
2916
2917 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
2918 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
2919 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00002920 }
2921
Chris Lattnerdf986172009-01-02 07:01:27 +00002922 case lltok::kw_urem:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002923 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnere914b592009-01-05 08:24:46 +00002924 case lltok::kw_fdiv:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002925 case lltok::kw_frem: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
Chris Lattnerdf986172009-01-02 07:01:27 +00002926 case lltok::kw_and:
2927 case lltok::kw_or:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002928 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002929 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002930 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002931 // Casts.
2932 case lltok::kw_trunc:
2933 case lltok::kw_zext:
2934 case lltok::kw_sext:
2935 case lltok::kw_fptrunc:
2936 case lltok::kw_fpext:
2937 case lltok::kw_bitcast:
2938 case lltok::kw_uitofp:
2939 case lltok::kw_sitofp:
2940 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002941 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00002942 case lltok::kw_inttoptr:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002943 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002944 // Other.
2945 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +00002946 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002947 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
2948 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
2949 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
2950 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlinge6e88262011-08-12 20:24:12 +00002951 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002952 case lltok::kw_call: return ParseCall(Inst, PFS, false);
2953 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
2954 // Memory.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00002955 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Eli Friedmanf03bb262011-08-12 22:50:01 +00002956 case lltok::kw_load: return ParseLoad(Inst, PFS, false);
2957 case lltok::kw_store: return ParseStore(Inst, PFS, false);
2958 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
2959 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedman47f35132011-07-25 23:16:38 +00002960 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002961 case lltok::kw_volatile:
Eli Friedmanf03bb262011-08-12 22:50:01 +00002962 // For compatibility; canonical location is after load
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002963 if (EatIfPresent(lltok::kw_load))
Eli Friedmanf03bb262011-08-12 22:50:01 +00002964 return ParseLoad(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002965 else if (EatIfPresent(lltok::kw_store))
Eli Friedmanf03bb262011-08-12 22:50:01 +00002966 return ParseStore(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002967 else
Chris Lattnerdf986172009-01-02 07:01:27 +00002968 return TokError("expected 'load' or 'store'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002969 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
2970 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
2971 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
2972 }
2973}
2974
2975/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
2976bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002977 if (Opc == Instruction::FCmp) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002978 switch (Lex.getKind()) {
2979 default: TokError("expected fcmp predicate (e.g. 'oeq')");
2980 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
2981 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
2982 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
2983 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
2984 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
2985 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
2986 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
2987 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
2988 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
2989 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
2990 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
2991 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
2992 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
2993 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
2994 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
2995 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
2996 }
2997 } else {
2998 switch (Lex.getKind()) {
2999 default: TokError("expected icmp predicate (e.g. 'eq')");
3000 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3001 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3002 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3003 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3004 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3005 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3006 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3007 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3008 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3009 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3010 }
3011 }
3012 Lex.Lex();
3013 return false;
3014}
3015
3016//===----------------------------------------------------------------------===//
3017// Terminator Instructions.
3018//===----------------------------------------------------------------------===//
3019
3020/// ParseRet - Parse a return instruction.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003021/// ::= 'ret' void (',' !dbg, !1)*
3022/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner437544f2011-06-17 06:49:41 +00003023bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattner1afcace2011-07-09 17:41:24 +00003024 PerFunctionState &PFS) {
3025 SMLoc TypeLoc = Lex.getLoc();
3026 Type *Ty = 0;
Chris Lattnera9a9e072009-03-09 04:49:14 +00003027 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003028
Chris Lattner1afcace2011-07-09 17:41:24 +00003029 Type *ResType = PFS.getFunction().getReturnType();
3030
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00003031 if (Ty->isVoidTy()) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003032 if (!ResType->isVoidTy())
3033 return Error(TypeLoc, "value doesn't match function result type '" +
3034 getTypeString(ResType) + "'");
3035
Owen Anderson1d0be152009-08-13 21:58:54 +00003036 Inst = ReturnInst::Create(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00003037 return false;
3038 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003039
Chris Lattnerdf986172009-01-02 07:01:27 +00003040 Value *RV;
3041 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003042
Chris Lattner1afcace2011-07-09 17:41:24 +00003043 if (ResType != RV->getType())
3044 return Error(TypeLoc, "value doesn't match function result type '" +
3045 getTypeString(ResType) + "'");
3046
Owen Anderson1d0be152009-08-13 21:58:54 +00003047 Inst = ReturnInst::Create(Context, RV);
Chris Lattner437544f2011-06-17 06:49:41 +00003048 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003049}
3050
3051
3052/// ParseBr
3053/// ::= 'br' TypeAndValue
3054/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3055bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3056 LocTy Loc, Loc2;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003057 Value *Op0;
3058 BasicBlock *Op1, *Op2;
Chris Lattnerdf986172009-01-02 07:01:27 +00003059 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003060
Chris Lattnerdf986172009-01-02 07:01:27 +00003061 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3062 Inst = BranchInst::Create(BB);
3063 return false;
3064 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003065
Owen Anderson1d0be152009-08-13 21:58:54 +00003066 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerdf986172009-01-02 07:01:27 +00003067 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003068
Chris Lattnerdf986172009-01-02 07:01:27 +00003069 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003070 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003071 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003072 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003073 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003074
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003075 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerdf986172009-01-02 07:01:27 +00003076 return false;
3077}
3078
3079/// ParseSwitch
3080/// Instruction
3081/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3082/// JumpTable
3083/// ::= (TypeAndValue ',' TypeAndValue)*
3084bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3085 LocTy CondLoc, BBLoc;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003086 Value *Cond;
3087 BasicBlock *DefaultBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003088 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3089 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003090 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003091 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3092 return true;
3093
Duncan Sands1df98592010-02-16 11:11:14 +00003094 if (!Cond->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003095 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003096
Chris Lattnerdf986172009-01-02 07:01:27 +00003097 // Parse the jump table pairs.
3098 SmallPtrSet<Value*, 32> SeenCases;
3099 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3100 while (Lex.getKind() != lltok::rsquare) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003101 Value *Constant;
3102 BasicBlock *DestBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003103
Chris Lattnerdf986172009-01-02 07:01:27 +00003104 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3105 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003106 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003107 return true;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003108
Chris Lattnerdf986172009-01-02 07:01:27 +00003109 if (!SeenCases.insert(Constant))
3110 return Error(CondLoc, "duplicate case value in switch");
3111 if (!isa<ConstantInt>(Constant))
3112 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003113
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003114 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerdf986172009-01-02 07:01:27 +00003115 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003116
Chris Lattnerdf986172009-01-02 07:01:27 +00003117 Lex.Lex(); // Eat the ']'.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003118
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003119 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003120 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3121 SI->addCase(Table[i].first, Table[i].second);
3122 Inst = SI;
3123 return false;
3124}
3125
Chris Lattnerab21db72009-10-28 00:19:10 +00003126/// ParseIndirectBr
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003127/// Instruction
Chris Lattnerab21db72009-10-28 00:19:10 +00003128/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3129bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003130 LocTy AddrLoc;
3131 Value *Address;
3132 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerab21db72009-10-28 00:19:10 +00003133 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3134 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003135 return true;
3136
Duncan Sands1df98592010-02-16 11:11:14 +00003137 if (!Address->getType()->isPointerTy())
Chris Lattnerab21db72009-10-28 00:19:10 +00003138 return Error(AddrLoc, "indirectbr address must have pointer type");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003139
3140 // Parse the destination list.
3141 SmallVector<BasicBlock*, 16> DestList;
3142
3143 if (Lex.getKind() != lltok::rsquare) {
3144 BasicBlock *DestBB;
3145 if (ParseTypeAndBasicBlock(DestBB, PFS))
3146 return true;
3147 DestList.push_back(DestBB);
3148
3149 while (EatIfPresent(lltok::comma)) {
3150 if (ParseTypeAndBasicBlock(DestBB, PFS))
3151 return true;
3152 DestList.push_back(DestBB);
3153 }
3154 }
3155
3156 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3157 return true;
3158
Chris Lattnerab21db72009-10-28 00:19:10 +00003159 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003160 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3161 IBI->addDestination(DestList[i]);
3162 Inst = IBI;
3163 return false;
3164}
3165
3166
Chris Lattnerdf986172009-01-02 07:01:27 +00003167/// ParseInvoke
3168/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3169/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3170bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3171 LocTy CallLoc = Lex.getLoc();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003172 unsigned RetAttrs, FnAttrs;
3173 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00003174 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003175 LocTy RetTypeLoc;
3176 ValID CalleeID;
3177 SmallVector<ParamInfo, 16> ArgList;
3178
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003179 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003180 if (ParseOptionalCallingConv(CC) ||
3181 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003182 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003183 ParseValID(CalleeID) ||
3184 ParseParameterList(ArgList, PFS) ||
3185 ParseOptionalAttrs(FnAttrs, 2) ||
3186 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003187 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003188 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003189 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003190 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003191
Chris Lattnerdf986172009-01-02 07:01:27 +00003192 // If RetType is a non-function pointer type, then this is the short syntax
3193 // for the call, which means that RetType is just the return type. Infer the
3194 // rest of the function argument types from the arguments that are present.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003195 PointerType *PFTy = 0;
3196 FunctionType *Ty = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003197 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3198 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3199 // Pull out the types of all of the arguments...
Jay Foad5fdd6c82011-07-12 14:06:48 +00003200 std::vector<Type*> ParamTypes;
Chris Lattnerdf986172009-01-02 07:01:27 +00003201 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3202 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003203
Chris Lattnerdf986172009-01-02 07:01:27 +00003204 if (!FunctionType::isValidReturnType(RetType))
3205 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003206
Owen Andersondebcb012009-07-29 22:17:13 +00003207 Ty = FunctionType::get(RetType, ParamTypes, false);
3208 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003209 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003210
Chris Lattnerdf986172009-01-02 07:01:27 +00003211 // Look up the callee.
3212 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003213 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003214
Chris Lattnerdf986172009-01-02 07:01:27 +00003215 // Set up the Attributes for the function.
3216 SmallVector<AttributeWithIndex, 8> Attrs;
3217 if (RetAttrs != Attribute::None)
3218 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003219
Chris Lattnerdf986172009-01-02 07:01:27 +00003220 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003221
Chris Lattnerdf986172009-01-02 07:01:27 +00003222 // Loop through FunctionType's arguments and ensure they are specified
3223 // correctly. Also, gather any parameter attributes.
3224 FunctionType::param_iterator I = Ty->param_begin();
3225 FunctionType::param_iterator E = Ty->param_end();
3226 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003227 Type *ExpectedTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003228 if (I != E) {
3229 ExpectedTy = *I++;
3230 } else if (!Ty->isVarArg()) {
3231 return Error(ArgList[i].Loc, "too many arguments specified");
3232 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003233
Chris Lattnerdf986172009-01-02 07:01:27 +00003234 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3235 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003236 getTypeString(ExpectedTy) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003237 Args.push_back(ArgList[i].V);
3238 if (ArgList[i].Attrs != Attribute::None)
3239 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3240 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003241
Chris Lattnerdf986172009-01-02 07:01:27 +00003242 if (I != E)
3243 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003244
Chris Lattnerdf986172009-01-02 07:01:27 +00003245 if (FnAttrs != Attribute::None)
3246 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003247
Chris Lattnerdf986172009-01-02 07:01:27 +00003248 // Finish off the Attributes and check them
3249 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003250
Jay Foada3efbb12011-07-15 08:37:34 +00003251 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
Chris Lattnerdf986172009-01-02 07:01:27 +00003252 II->setCallingConv(CC);
3253 II->setAttributes(PAL);
3254 Inst = II;
3255 return false;
3256}
3257
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003258/// ParseResume
3259/// ::= 'resume' TypeAndValue
3260bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
3261 Value *Exn; LocTy ExnLoc;
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003262 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
3263 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003264
Bill Wendlingdccc03b2011-07-31 06:30:59 +00003265 ResumeInst *RI = ResumeInst::Create(Exn);
3266 Inst = RI;
3267 return false;
3268}
Chris Lattnerdf986172009-01-02 07:01:27 +00003269
3270//===----------------------------------------------------------------------===//
3271// Binary Operators.
3272//===----------------------------------------------------------------------===//
3273
3274/// ParseArithmetic
Chris Lattnere914b592009-01-05 08:24:46 +00003275/// ::= ArithmeticOps TypeAndValue ',' Value
3276///
3277/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3278/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerdf986172009-01-02 07:01:27 +00003279bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnere914b592009-01-05 08:24:46 +00003280 unsigned Opc, unsigned OperandType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003281 LocTy Loc; Value *LHS, *RHS;
3282 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3283 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3284 ParseValue(LHS->getType(), RHS, PFS))
3285 return true;
3286
Chris Lattnere914b592009-01-05 08:24:46 +00003287 bool Valid;
3288 switch (OperandType) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003289 default: llvm_unreachable("Unknown operand type!");
Chris Lattnere914b592009-01-05 08:24:46 +00003290 case 0: // int or FP.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003291 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3292 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnere914b592009-01-05 08:24:46 +00003293 break;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003294 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3295 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnere914b592009-01-05 08:24:46 +00003296 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003297
Chris Lattnere914b592009-01-05 08:24:46 +00003298 if (!Valid)
3299 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003300
Chris Lattnerdf986172009-01-02 07:01:27 +00003301 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3302 return false;
3303}
3304
3305/// ParseLogical
3306/// ::= ArithmeticOps TypeAndValue ',' Value {
3307bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3308 unsigned Opc) {
3309 LocTy Loc; Value *LHS, *RHS;
3310 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3311 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3312 ParseValue(LHS->getType(), RHS, PFS))
3313 return true;
3314
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003315 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003316 return Error(Loc,"instruction requires integer or integer vector operands");
3317
3318 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3319 return false;
3320}
3321
3322
3323/// ParseCompare
3324/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3325/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerdf986172009-01-02 07:01:27 +00003326bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3327 unsigned Opc) {
3328 // Parse the integer/fp comparison predicate.
3329 LocTy Loc;
3330 unsigned Pred;
3331 Value *LHS, *RHS;
3332 if (ParseCmpPredicate(Pred, Opc) ||
3333 ParseTypeAndValue(LHS, Loc, PFS) ||
3334 ParseToken(lltok::comma, "expected ',' after compare value") ||
3335 ParseValue(LHS->getType(), RHS, PFS))
3336 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003337
Chris Lattnerdf986172009-01-02 07:01:27 +00003338 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003339 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003340 return Error(Loc, "fcmp requires floating point operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003341 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003342 } else {
3343 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003344 if (!LHS->getType()->isIntOrIntVectorTy() &&
Duncan Sands1df98592010-02-16 11:11:14 +00003345 !LHS->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003346 return Error(Loc, "icmp requires integer operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003347 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003348 }
3349 return false;
3350}
3351
3352//===----------------------------------------------------------------------===//
3353// Other Instructions.
3354//===----------------------------------------------------------------------===//
3355
3356
3357/// ParseCast
3358/// ::= CastOpc TypeAndValue 'to' Type
3359bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3360 unsigned Opc) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003361 LocTy Loc;
3362 Value *Op;
3363 Type *DestTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003364 if (ParseTypeAndValue(Op, Loc, PFS) ||
3365 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3366 ParseType(DestTy))
3367 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003368
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003369 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3370 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00003371 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003372 getTypeString(Op->getType()) + "' to '" +
3373 getTypeString(DestTy) + "'");
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003374 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003375 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3376 return false;
3377}
3378
3379/// ParseSelect
3380/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3381bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3382 LocTy Loc;
3383 Value *Op0, *Op1, *Op2;
3384 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3385 ParseToken(lltok::comma, "expected ',' after select condition") ||
3386 ParseTypeAndValue(Op1, PFS) ||
3387 ParseToken(lltok::comma, "expected ',' after select value") ||
3388 ParseTypeAndValue(Op2, PFS))
3389 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003390
Chris Lattnerdf986172009-01-02 07:01:27 +00003391 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3392 return Error(Loc, Reason);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003393
Chris Lattnerdf986172009-01-02 07:01:27 +00003394 Inst = SelectInst::Create(Op0, Op1, Op2);
3395 return false;
3396}
3397
Chris Lattner0088a5c2009-01-05 08:18:44 +00003398/// ParseVA_Arg
3399/// ::= 'va_arg' TypeAndValue ',' Type
3400bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003401 Value *Op;
Chris Lattner1afcace2011-07-09 17:41:24 +00003402 Type *EltTy = 0;
Chris Lattner0088a5c2009-01-05 08:18:44 +00003403 LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003404 if (ParseTypeAndValue(Op, PFS) ||
3405 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattner0088a5c2009-01-05 08:18:44 +00003406 ParseType(EltTy, TypeLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003407 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003408
Chris Lattner0088a5c2009-01-05 08:18:44 +00003409 if (!EltTy->isFirstClassType())
3410 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003411
3412 Inst = new VAArgInst(Op, EltTy);
3413 return false;
3414}
3415
3416/// ParseExtractElement
3417/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3418bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3419 LocTy Loc;
3420 Value *Op0, *Op1;
3421 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3422 ParseToken(lltok::comma, "expected ',' after extract value") ||
3423 ParseTypeAndValue(Op1, PFS))
3424 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003425
Chris Lattnerdf986172009-01-02 07:01:27 +00003426 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3427 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003428
Eric Christophera3500da2009-07-25 02:28:41 +00003429 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003430 return false;
3431}
3432
3433/// ParseInsertElement
3434/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3435bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3436 LocTy Loc;
3437 Value *Op0, *Op1, *Op2;
3438 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3439 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3440 ParseTypeAndValue(Op1, PFS) ||
3441 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3442 ParseTypeAndValue(Op2, PFS))
3443 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003444
Chris Lattnerdf986172009-01-02 07:01:27 +00003445 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopher0aaf4e92009-07-23 01:01:32 +00003446 return Error(Loc, "invalid insertelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003447
Chris Lattnerdf986172009-01-02 07:01:27 +00003448 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3449 return false;
3450}
3451
3452/// ParseShuffleVector
3453/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3454bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3455 LocTy Loc;
3456 Value *Op0, *Op1, *Op2;
3457 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3458 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3459 ParseTypeAndValue(Op1, PFS) ||
3460 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3461 ParseTypeAndValue(Op2, PFS))
3462 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003463
Chris Lattnerdf986172009-01-02 07:01:27 +00003464 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
3465 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003466
Chris Lattnerdf986172009-01-02 07:01:27 +00003467 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3468 return false;
3469}
3470
3471/// ParsePHI
Chris Lattnerc6e20092009-10-18 05:27:44 +00003472/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003473int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner1afcace2011-07-09 17:41:24 +00003474 Type *Ty = 0; LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003475 Value *Op0, *Op1;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003476
Chris Lattner1afcace2011-07-09 17:41:24 +00003477 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003478 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3479 ParseValue(Ty, Op0, PFS) ||
3480 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003481 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003482 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3483 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003484
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003485 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003486 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3487 while (1) {
3488 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003489
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003490 if (!EatIfPresent(lltok::comma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003491 break;
3492
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003493 if (Lex.getKind() == lltok::MetadataVar) {
3494 AteExtraComma = true;
Devang Patela43d46f2009-10-16 18:45:49 +00003495 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003496 }
Devang Patela43d46f2009-10-16 18:45:49 +00003497
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003498 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003499 ParseValue(Ty, Op0, PFS) ||
3500 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003501 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003502 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3503 return true;
3504 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003505
Chris Lattnerdf986172009-01-02 07:01:27 +00003506 if (!Ty->isFirstClassType())
3507 return Error(TypeLoc, "phi node must have first class type");
3508
Jay Foad3ecfc862011-03-30 11:28:46 +00003509 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003510 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3511 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3512 Inst = PN;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003513 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003514}
3515
Bill Wendlinge6e88262011-08-12 20:24:12 +00003516/// ParseLandingPad
3517/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3518/// Clause
3519/// ::= 'catch' TypeAndValue
3520/// ::= 'filter'
3521/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3522bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
3523 Type *Ty = 0; LocTy TyLoc;
3524 Value *PersFn; LocTy PersFnLoc;
Bill Wendlinge6e88262011-08-12 20:24:12 +00003525
3526 if (ParseType(Ty, TyLoc) ||
3527 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3528 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3529 return true;
3530
3531 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3532 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3533
3534 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3535 LandingPadInst::ClauseType CT;
3536 if (EatIfPresent(lltok::kw_catch))
3537 CT = LandingPadInst::Catch;
3538 else if (EatIfPresent(lltok::kw_filter))
3539 CT = LandingPadInst::Filter;
3540 else
3541 return TokError("expected 'catch' or 'filter' clause type");
3542
3543 Value *V; LocTy VLoc;
3544 if (ParseTypeAndValue(V, VLoc, PFS)) {
3545 delete LP;
3546 return true;
3547 }
3548
Bill Wendling746c8822011-08-12 20:52:25 +00003549 // A 'catch' type expects a non-array constant. A filter clause expects an
3550 // array constant.
3551 if (CT == LandingPadInst::Catch) {
3552 if (isa<ArrayType>(V->getType()))
3553 Error(VLoc, "'catch' clause has an invalid type");
3554 } else {
3555 if (!isa<ArrayType>(V->getType()))
3556 Error(VLoc, "'filter' clause has an invalid type");
3557 }
3558
Bill Wendlinge6e88262011-08-12 20:24:12 +00003559 LP->addClause(V);
3560 }
3561
3562 Inst = LP;
3563 return false;
3564}
3565
Chris Lattnerdf986172009-01-02 07:01:27 +00003566/// ParseCall
3567/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3568/// ParameterList OptionalAttrs
3569bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3570 bool isTail) {
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003571 unsigned RetAttrs, FnAttrs;
3572 CallingConv::ID CC;
Chris Lattner1afcace2011-07-09 17:41:24 +00003573 Type *RetType = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003574 LocTy RetTypeLoc;
3575 ValID CalleeID;
3576 SmallVector<ParamInfo, 16> ArgList;
3577 LocTy CallLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003578
Chris Lattnerdf986172009-01-02 07:01:27 +00003579 if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3580 ParseOptionalCallingConv(CC) ||
3581 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003582 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003583 ParseValID(CalleeID) ||
3584 ParseParameterList(ArgList, PFS) ||
3585 ParseOptionalAttrs(FnAttrs, 2))
3586 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003587
Chris Lattnerdf986172009-01-02 07:01:27 +00003588 // If RetType is a non-function pointer type, then this is the short syntax
3589 // for the call, which means that RetType is just the return type. Infer the
3590 // rest of the function argument types from the arguments that are present.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003591 PointerType *PFTy = 0;
3592 FunctionType *Ty = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003593 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3594 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3595 // Pull out the types of all of the arguments...
Jay Foad5fdd6c82011-07-12 14:06:48 +00003596 std::vector<Type*> ParamTypes;
Eli Friedman83b4a972010-07-24 23:06:59 +00003597 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3598 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003599
Chris Lattnerdf986172009-01-02 07:01:27 +00003600 if (!FunctionType::isValidReturnType(RetType))
3601 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003602
Owen Andersondebcb012009-07-29 22:17:13 +00003603 Ty = FunctionType::get(RetType, ParamTypes, false);
3604 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003605 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003606
Chris Lattnerdf986172009-01-02 07:01:27 +00003607 // Look up the callee.
3608 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003609 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003610
Chris Lattnerdf986172009-01-02 07:01:27 +00003611 // Set up the Attributes for the function.
3612 SmallVector<AttributeWithIndex, 8> Attrs;
3613 if (RetAttrs != Attribute::None)
3614 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003615
Chris Lattnerdf986172009-01-02 07:01:27 +00003616 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003617
Chris Lattnerdf986172009-01-02 07:01:27 +00003618 // Loop through FunctionType's arguments and ensure they are specified
3619 // correctly. Also, gather any parameter attributes.
3620 FunctionType::param_iterator I = Ty->param_begin();
3621 FunctionType::param_iterator E = Ty->param_end();
3622 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003623 Type *ExpectedTy = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00003624 if (I != E) {
3625 ExpectedTy = *I++;
3626 } else if (!Ty->isVarArg()) {
3627 return Error(ArgList[i].Loc, "too many arguments specified");
3628 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003629
Chris Lattnerdf986172009-01-02 07:01:27 +00003630 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3631 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0cd0d882011-06-18 21:18:23 +00003632 getTypeString(ExpectedTy) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003633 Args.push_back(ArgList[i].V);
3634 if (ArgList[i].Attrs != Attribute::None)
3635 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3636 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003637
Chris Lattnerdf986172009-01-02 07:01:27 +00003638 if (I != E)
3639 return Error(CallLoc, "not enough parameters specified for call");
3640
3641 if (FnAttrs != Attribute::None)
3642 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
3643
3644 // Finish off the Attributes and check them
3645 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003646
Jay Foada3efbb12011-07-15 08:37:34 +00003647 CallInst *CI = CallInst::Create(Callee, Args);
Chris Lattnerdf986172009-01-02 07:01:27 +00003648 CI->setTailCall(isTail);
3649 CI->setCallingConv(CC);
3650 CI->setAttributes(PAL);
3651 Inst = CI;
3652 return false;
3653}
3654
3655//===----------------------------------------------------------------------===//
3656// Memory Instructions.
3657//===----------------------------------------------------------------------===//
3658
3659/// ParseAlloc
Devang Patelf633a062009-09-17 23:04:48 +00003660/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
Chris Lattnerf3a789d2011-06-17 03:16:47 +00003661int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003662 Value *Size = 0;
Chris Lattnereeb4a842009-07-02 23:08:13 +00003663 LocTy SizeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003664 unsigned Alignment = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +00003665 Type *Ty = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003666 if (ParseType(Ty)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003667
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003668 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003669 if (EatIfPresent(lltok::comma)) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003670 if (Lex.getKind() == lltok::kw_align) {
3671 if (ParseOptionalAlignment(Alignment)) return true;
3672 } else if (Lex.getKind() == lltok::MetadataVar) {
3673 AteExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003674 } else {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003675 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3676 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3677 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003678 }
3679 }
3680
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003681 if (Size && !Size->getType()->isIntegerTy())
3682 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003683
Chris Lattnerf3a789d2011-06-17 03:16:47 +00003684 Inst = new AllocaInst(Ty, Size, Alignment);
3685 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003686}
3687
3688/// ParseLoad
Eli Friedmanf03bb262011-08-12 22:50:01 +00003689/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
3690/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
3691/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
3692/// Compatibility:
3693/// ::= 'volatile' 'load' TypeAndValue (',' 'align' i32)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003694int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS,
Eli Friedmanf03bb262011-08-12 22:50:01 +00003695 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003696 Value *Val; LocTy Loc;
Devang Patelf633a062009-09-17 23:04:48 +00003697 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003698 bool AteExtraComma = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003699 bool isAtomic = false;
Eli Friedman21006d42011-08-09 23:02:53 +00003700 AtomicOrdering Ordering = NotAtomic;
3701 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003702
3703 if (Lex.getKind() == lltok::kw_atomic) {
3704 if (isVolatile)
3705 return TokError("mixing atomic with old volatile placement");
3706 isAtomic = true;
3707 Lex.Lex();
3708 }
3709
3710 if (Lex.getKind() == lltok::kw_volatile) {
3711 if (isVolatile)
3712 return TokError("duplicate volatile before and after store");
3713 isVolatile = true;
3714 Lex.Lex();
3715 }
3716
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003717 if (ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman21006d42011-08-09 23:02:53 +00003718 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003719 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3720 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003721
Duncan Sands1df98592010-02-16 11:11:14 +00003722 if (!Val->getType()->isPointerTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003723 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3724 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman21006d42011-08-09 23:02:53 +00003725 if (isAtomic && !Alignment)
3726 return Error(Loc, "atomic load must have explicit non-zero alignment");
3727 if (Ordering == Release || Ordering == AcquireRelease)
3728 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003729
Eli Friedman21006d42011-08-09 23:02:53 +00003730 Inst = new LoadInst(Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003731 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003732}
3733
3734/// ParseStore
Eli Friedmanf03bb262011-08-12 22:50:01 +00003735
3736/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
3737/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman21006d42011-08-09 23:02:53 +00003738/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Eli Friedmanf03bb262011-08-12 22:50:01 +00003739/// Compatibility:
3740/// ::= 'volatile' 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003741int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS,
Eli Friedmanf03bb262011-08-12 22:50:01 +00003742 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003743 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelf633a062009-09-17 23:04:48 +00003744 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003745 bool AteExtraComma = false;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003746 bool isAtomic = false;
Eli Friedman21006d42011-08-09 23:02:53 +00003747 AtomicOrdering Ordering = NotAtomic;
3748 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003749
3750 if (Lex.getKind() == lltok::kw_atomic) {
3751 if (isVolatile)
3752 return TokError("mixing atomic with old volatile placement");
3753 isAtomic = true;
3754 Lex.Lex();
3755 }
3756
3757 if (Lex.getKind() == lltok::kw_volatile) {
3758 if (isVolatile)
3759 return TokError("duplicate volatile before and after store");
3760 isVolatile = true;
3761 Lex.Lex();
3762 }
3763
Chris Lattnerdf986172009-01-02 07:01:27 +00003764 if (ParseTypeAndValue(Val, Loc, PFS) ||
3765 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003766 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman21006d42011-08-09 23:02:53 +00003767 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003768 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003769 return true;
Devang Patelf633a062009-09-17 23:04:48 +00003770
Duncan Sands1df98592010-02-16 11:11:14 +00003771 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003772 return Error(PtrLoc, "store operand must be a pointer");
3773 if (!Val->getType()->isFirstClassType())
3774 return Error(Loc, "store operand must be a first class value");
3775 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3776 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman21006d42011-08-09 23:02:53 +00003777 if (isAtomic && !Alignment)
3778 return Error(Loc, "atomic store must have explicit non-zero alignment");
3779 if (Ordering == Acquire || Ordering == AcquireRelease)
3780 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003781
Eli Friedman21006d42011-08-09 23:02:53 +00003782 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003783 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003784}
3785
Eli Friedmanff030482011-07-28 21:48:00 +00003786/// ParseCmpXchg
Eli Friedmanf03bb262011-08-12 22:50:01 +00003787/// ::= 'cmpxchg' 'volatile'? TypeAndValue ',' TypeAndValue ',' TypeAndValue
3788/// 'singlethread'? AtomicOrdering
3789int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanff030482011-07-28 21:48:00 +00003790 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
3791 bool AteExtraComma = false;
3792 AtomicOrdering Ordering = NotAtomic;
3793 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003794 bool isVolatile = false;
3795
3796 if (EatIfPresent(lltok::kw_volatile))
3797 isVolatile = true;
3798
Eli Friedmanff030482011-07-28 21:48:00 +00003799 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
3800 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
3801 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
3802 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
3803 ParseTypeAndValue(New, NewLoc, PFS) ||
3804 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
3805 return true;
3806
3807 if (Ordering == Unordered)
3808 return TokError("cmpxchg cannot be unordered");
3809 if (!Ptr->getType()->isPointerTy())
3810 return Error(PtrLoc, "cmpxchg operand must be a pointer");
3811 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
3812 return Error(CmpLoc, "compare value and pointer type do not match");
3813 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
3814 return Error(NewLoc, "new value and pointer type do not match");
3815 if (!New->getType()->isIntegerTy())
3816 return Error(NewLoc, "cmpxchg operand must be an integer");
3817 unsigned Size = New->getType()->getPrimitiveSizeInBits();
3818 if (Size < 8 || (Size & (Size - 1)))
3819 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
3820 " integer");
3821
3822 AtomicCmpXchgInst *CXI =
3823 new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, Scope);
3824 CXI->setVolatile(isVolatile);
3825 Inst = CXI;
3826 return AteExtraComma ? InstExtraComma : InstNormal;
3827}
3828
3829/// ParseAtomicRMW
Eli Friedmanf03bb262011-08-12 22:50:01 +00003830/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
3831/// 'singlethread'? AtomicOrdering
3832int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanff030482011-07-28 21:48:00 +00003833 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
3834 bool AteExtraComma = false;
3835 AtomicOrdering Ordering = NotAtomic;
3836 SynchronizationScope Scope = CrossThread;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003837 bool isVolatile = false;
Eli Friedmanff030482011-07-28 21:48:00 +00003838 AtomicRMWInst::BinOp Operation;
Eli Friedmanf03bb262011-08-12 22:50:01 +00003839
3840 if (EatIfPresent(lltok::kw_volatile))
3841 isVolatile = true;
3842
Eli Friedmanff030482011-07-28 21:48:00 +00003843 switch (Lex.getKind()) {
3844 default: return TokError("expected binary operation in atomicrmw");
3845 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
3846 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
3847 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
3848 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
3849 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
3850 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
3851 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
3852 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
3853 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
3854 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
3855 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
3856 }
3857 Lex.Lex(); // Eat the operation.
3858
3859 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
3860 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
3861 ParseTypeAndValue(Val, ValLoc, PFS) ||
3862 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
3863 return true;
3864
3865 if (Ordering == Unordered)
3866 return TokError("atomicrmw cannot be unordered");
3867 if (!Ptr->getType()->isPointerTy())
3868 return Error(PtrLoc, "atomicrmw operand must be a pointer");
3869 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3870 return Error(ValLoc, "atomicrmw value and pointer type do not match");
3871 if (!Val->getType()->isIntegerTy())
3872 return Error(ValLoc, "atomicrmw operand must be an integer");
3873 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
3874 if (Size < 8 || (Size & (Size - 1)))
3875 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
3876 " integer");
3877
3878 AtomicRMWInst *RMWI =
3879 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
3880 RMWI->setVolatile(isVolatile);
3881 Inst = RMWI;
3882 return AteExtraComma ? InstExtraComma : InstNormal;
3883}
3884
Eli Friedman47f35132011-07-25 23:16:38 +00003885/// ParseFence
3886/// ::= 'fence' 'singlethread'? AtomicOrdering
3887int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
3888 AtomicOrdering Ordering = NotAtomic;
3889 SynchronizationScope Scope = CrossThread;
3890 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
3891 return true;
3892
3893 if (Ordering == Unordered)
3894 return TokError("fence cannot be unordered");
3895 if (Ordering == Monotonic)
3896 return TokError("fence cannot be monotonic");
3897
3898 Inst = new FenceInst(Context, Ordering, Scope);
3899 return InstNormal;
3900}
3901
Chris Lattnerdf986172009-01-02 07:01:27 +00003902/// ParseGetElementPtr
Dan Gohmandd8004d2009-07-27 21:53:46 +00003903/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003904int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003905 Value *Ptr, *Val; LocTy Loc, EltLoc;
Dan Gohmandd8004d2009-07-27 21:53:46 +00003906
Dan Gohmandcb40a32009-07-29 15:58:36 +00003907 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohmandd8004d2009-07-27 21:53:46 +00003908
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003909 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003910
Duncan Sands1df98592010-02-16 11:11:14 +00003911 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003912 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003913
Chris Lattnerdf986172009-01-02 07:01:27 +00003914 SmallVector<Value*, 16> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003915 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003916 while (EatIfPresent(lltok::comma)) {
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003917 if (Lex.getKind() == lltok::MetadataVar) {
3918 AteExtraComma = true;
Devang Patel6225d642009-10-13 18:49:55 +00003919 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003920 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003921 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Duncan Sands1df98592010-02-16 11:11:14 +00003922 if (!Val->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003923 return Error(EltLoc, "getelementptr index must be an integer");
3924 Indices.push_back(Val);
3925 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003926
Jay Foada9203102011-07-25 09:48:08 +00003927 if (!GetElementPtrInst::getIndexedType(Ptr->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00003928 return Error(Loc, "invalid getelementptr indices");
Jay Foada9203102011-07-25 09:48:08 +00003929 Inst = GetElementPtrInst::Create(Ptr, Indices);
Dan Gohmandd8004d2009-07-27 21:53:46 +00003930 if (InBounds)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00003931 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003932 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003933}
3934
3935/// ParseExtractValue
3936/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003937int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003938 Value *Val; LocTy Loc;
3939 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003940 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003941 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003942 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003943 return true;
3944
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003945 if (!Val->getType()->isAggregateType())
3946 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003947
Jay Foadfc6d3a42011-07-13 10:26:04 +00003948 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00003949 return Error(Loc, "invalid indices for extractvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00003950 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003951 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003952}
3953
3954/// ParseInsertValue
3955/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003956int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003957 Value *Val0, *Val1; LocTy Loc0, Loc1;
3958 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003959 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003960 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
3961 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
3962 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003963 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003964 return true;
Chris Lattner628c13a2009-12-30 05:14:00 +00003965
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003966 if (!Val0->getType()->isAggregateType())
3967 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003968
Jay Foadfc6d3a42011-07-13 10:26:04 +00003969 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices))
Chris Lattnerdf986172009-01-02 07:01:27 +00003970 return Error(Loc0, "invalid indices for insertvalue");
Jay Foadfc6d3a42011-07-13 10:26:04 +00003971 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003972 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003973}
Nick Lewycky21cc4462009-04-04 07:22:01 +00003974
3975//===----------------------------------------------------------------------===//
3976// Embedded metadata.
3977//===----------------------------------------------------------------------===//
3978
3979/// ParseMDNodeVector
Nick Lewyckycb337992009-05-10 20:57:05 +00003980/// ::= Element (',' Element)*
3981/// Element
3982/// ::= 'null' | TypeAndValue
Victor Hernandezbf170d42010-01-05 22:22:14 +00003983bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandez24e64df2010-01-10 07:14:18 +00003984 PerFunctionState *PFS) {
Dan Gohmanac809752010-07-13 19:33:27 +00003985 // Check for an empty list.
3986 if (Lex.getKind() == lltok::rbrace)
3987 return false;
3988
Nick Lewycky21cc4462009-04-04 07:22:01 +00003989 do {
Chris Lattnera7352392009-12-30 04:42:57 +00003990 // Null is a special case since it is typeless.
3991 if (EatIfPresent(lltok::kw_null)) {
3992 Elts.push_back(0);
3993 continue;
Nick Lewyckycb337992009-05-10 20:57:05 +00003994 }
Chris Lattnera7352392009-12-30 04:42:57 +00003995
3996 Value *V = 0;
Chris Lattner1afcace2011-07-09 17:41:24 +00003997 if (ParseTypeAndValue(V, PFS)) return true;
Nick Lewyckycb337992009-05-10 20:57:05 +00003998 Elts.push_back(V);
Nick Lewycky21cc4462009-04-04 07:22:01 +00003999 } while (EatIfPresent(lltok::comma));
4000
4001 return false;
4002}