blob: 81e0747266f10df4a990d7c04bdca007b5f5bfa9 [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 Lattner3ed88ef2009-01-02 08:05:26 +000029/// Run: module ::= toplevelentity*
Chris Lattnerad7d1e22009-01-04 20:44:11 +000030bool LLParser::Run() {
Chris Lattner3ed88ef2009-01-02 08:05:26 +000031 // Prime the lexer.
32 Lex.Lex();
33
Chris Lattnerad7d1e22009-01-04 20:44:11 +000034 return ParseTopLevelEntities() ||
35 ValidateEndOfModule();
Chris Lattnerdf986172009-01-02 07:01:27 +000036}
37
38/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
39/// module.
40bool LLParser::ValidateEndOfModule() {
Chris Lattner449c3102010-04-01 05:14:45 +000041 // Handle any instruction metadata forward references.
42 if (!ForwardRefInstMetadata.empty()) {
43 for (DenseMap<Instruction*, std::vector<MDRef> >::iterator
44 I = ForwardRefInstMetadata.begin(), E = ForwardRefInstMetadata.end();
45 I != E; ++I) {
46 Instruction *Inst = I->first;
47 const std::vector<MDRef> &MDList = I->second;
48
49 for (unsigned i = 0, e = MDList.size(); i != e; ++i) {
50 unsigned SlotNo = MDList[i].MDSlot;
51
52 if (SlotNo >= NumberedMetadata.size() || NumberedMetadata[SlotNo] == 0)
53 return Error(MDList[i].Loc, "use of undefined metadata '!" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +000054 Twine(SlotNo) + "'");
Chris Lattner449c3102010-04-01 05:14:45 +000055 Inst->setMetadata(MDList[i].MDKind, NumberedMetadata[SlotNo]);
56 }
57 }
58 ForwardRefInstMetadata.clear();
59 }
60
61
Victor Hernandez68afa542009-10-21 19:11:40 +000062 // Update auto-upgraded malloc calls to "malloc".
Chris Lattnercf4d2f12009-10-18 05:09:15 +000063 // FIXME: Remove in LLVM 3.0.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +000064 if (MallocF) {
65 MallocF->setName("malloc");
66 // If setName() does not set the name to "malloc", then there is already a
67 // declaration of "malloc". In that case, iterate over all calls to MallocF
68 // and get them to call the declared "malloc" instead.
69 if (MallocF->getName() != "malloc") {
Chris Lattner09d9ef42009-10-28 03:39:23 +000070 Constant *RealMallocF = M->getFunction("malloc");
Victor Hernandez68afa542009-10-21 19:11:40 +000071 if (RealMallocF->getType() != MallocF->getType())
72 RealMallocF = ConstantExpr::getBitCast(RealMallocF, MallocF->getType());
73 MallocF->replaceAllUsesWith(RealMallocF);
Victor Hernandez13ad5aa2009-10-17 00:00:19 +000074 MallocF->eraseFromParent();
75 MallocF = NULL;
76 }
77 }
Chris Lattner09d9ef42009-10-28 03:39:23 +000078
79
80 // If there are entries in ForwardRefBlockAddresses at this point, they are
81 // references after the function was defined. Resolve those now.
82 while (!ForwardRefBlockAddresses.empty()) {
83 // Okay, we are referencing an already-parsed function, resolve them now.
84 Function *TheFn = 0;
85 const ValID &Fn = ForwardRefBlockAddresses.begin()->first;
86 if (Fn.Kind == ValID::t_GlobalName)
87 TheFn = M->getFunction(Fn.StrVal);
88 else if (Fn.UIntVal < NumberedVals.size())
89 TheFn = dyn_cast<Function>(NumberedVals[Fn.UIntVal]);
90
91 if (TheFn == 0)
92 return Error(Fn.Loc, "unknown function referenced by blockaddress");
93
94 // Resolve all these references.
95 if (ResolveForwardRefBlockAddresses(TheFn,
96 ForwardRefBlockAddresses.begin()->second,
97 0))
98 return true;
99
100 ForwardRefBlockAddresses.erase(ForwardRefBlockAddresses.begin());
101 }
102
103
Chris Lattnerdf986172009-01-02 07:01:27 +0000104 if (!ForwardRefTypes.empty())
105 return Error(ForwardRefTypes.begin()->second.second,
106 "use of undefined type named '" +
107 ForwardRefTypes.begin()->first + "'");
108 if (!ForwardRefTypeIDs.empty())
109 return Error(ForwardRefTypeIDs.begin()->second.second,
110 "use of undefined type '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000111 Twine(ForwardRefTypeIDs.begin()->first) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000112
Chris Lattnerdf986172009-01-02 07:01:27 +0000113 if (!ForwardRefVals.empty())
114 return Error(ForwardRefVals.begin()->second.second,
115 "use of undefined value '@" + ForwardRefVals.begin()->first +
116 "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000117
Chris Lattnerdf986172009-01-02 07:01:27 +0000118 if (!ForwardRefValIDs.empty())
119 return Error(ForwardRefValIDs.begin()->second.second,
120 "use of undefined value '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000121 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000122
Devang Patel1c7eea62009-07-08 19:23:54 +0000123 if (!ForwardRefMDNodes.empty())
124 return Error(ForwardRefMDNodes.begin()->second.second,
125 "use of undefined metadata '!" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000126 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000127
Devang Patel1c7eea62009-07-08 19:23:54 +0000128
Chris Lattnerdf986172009-01-02 07:01:27 +0000129 // Look for intrinsic functions and CallInst that need to be upgraded
130 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
131 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbara279bc32009-09-20 02:20:51 +0000132
Devang Patele4b27562009-08-28 23:24:31 +0000133 // Check debug info intrinsics.
134 CheckDebugInfoIntrinsics(M);
Chris Lattnerdf986172009-01-02 07:01:27 +0000135 return false;
136}
137
Chris Lattner09d9ef42009-10-28 03:39:23 +0000138bool LLParser::ResolveForwardRefBlockAddresses(Function *TheFn,
139 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
140 PerFunctionState *PFS) {
141 // Loop over all the references, resolving them.
142 for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
143 BasicBlock *Res;
Chris Lattnercdfc9402009-11-01 01:27:45 +0000144 if (PFS) {
Chris Lattner09d9ef42009-10-28 03:39:23 +0000145 if (Refs[i].first.Kind == ValID::t_LocalName)
146 Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
Chris Lattnercdfc9402009-11-01 01:27:45 +0000147 else
Chris Lattner09d9ef42009-10-28 03:39:23 +0000148 Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
149 } else if (Refs[i].first.Kind == ValID::t_LocalID) {
150 return Error(Refs[i].first.Loc,
Chris Lattneree7644d2009-11-02 18:28:45 +0000151 "cannot take address of numeric label after the function is defined");
Chris Lattner09d9ef42009-10-28 03:39:23 +0000152 } else {
153 Res = dyn_cast_or_null<BasicBlock>(
154 TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
155 }
156
Chris Lattnercdfc9402009-11-01 01:27:45 +0000157 if (Res == 0)
Chris Lattner09d9ef42009-10-28 03:39:23 +0000158 return Error(Refs[i].first.Loc,
159 "referenced value is not a basic block");
160
161 // Get the BlockAddress for this and update references to use it.
162 BlockAddress *BA = BlockAddress::get(TheFn, Res);
163 Refs[i].second->replaceAllUsesWith(BA);
164 Refs[i].second->eraseFromParent();
165 }
166 return false;
167}
168
169
Chris Lattnerdf986172009-01-02 07:01:27 +0000170//===----------------------------------------------------------------------===//
171// Top-Level Entities
172//===----------------------------------------------------------------------===//
173
174bool LLParser::ParseTopLevelEntities() {
Chris Lattnerdf986172009-01-02 07:01:27 +0000175 while (1) {
176 switch (Lex.getKind()) {
177 default: return TokError("expected top-level entity");
178 case lltok::Eof: return false;
179 //case lltok::kw_define:
180 case lltok::kw_declare: if (ParseDeclare()) return true; break;
181 case lltok::kw_define: if (ParseDefine()) return true; break;
182 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
183 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
184 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
185 case lltok::kw_type: if (ParseUnnamedType()) return true; break;
Dan Gohman3845e502009-08-12 23:32:33 +0000186 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000187 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
188 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman3845e502009-08-12 23:32:33 +0000189 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000190 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
Chris Lattnere434d272009-12-30 04:56:59 +0000191 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Chris Lattner1d928312009-12-30 05:02:06 +0000192 case lltok::MetadataVar: if (ParseNamedMetadata()) return true; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000193
194 // The Global variable production with no name can have many different
195 // optional leading prefixes, the production is:
196 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000197 // OptionalAddrSpace OptionalUnNammedAddr
198 // ('constant'|'global') ...
Bill Wendling5e721d72010-07-01 21:55:59 +0000199 case lltok::kw_private: // OptionalLinkage
200 case lltok::kw_linker_private: // OptionalLinkage
201 case lltok::kw_linker_private_weak: // OptionalLinkage
Bill Wendling55ae5152010-08-20 22:05:50 +0000202 case lltok::kw_linker_private_weak_def_auto: // OptionalLinkage
Bill Wendling5e721d72010-07-01 21:55:59 +0000203 case lltok::kw_internal: // OptionalLinkage
204 case lltok::kw_weak: // OptionalLinkage
205 case lltok::kw_weak_odr: // OptionalLinkage
206 case lltok::kw_linkonce: // OptionalLinkage
207 case lltok::kw_linkonce_odr: // OptionalLinkage
208 case lltok::kw_appending: // OptionalLinkage
209 case lltok::kw_dllexport: // OptionalLinkage
210 case lltok::kw_common: // OptionalLinkage
211 case lltok::kw_dllimport: // OptionalLinkage
212 case lltok::kw_extern_weak: // OptionalLinkage
213 case lltok::kw_external: { // OptionalLinkage
Chris Lattnerdf986172009-01-02 07:01:27 +0000214 unsigned Linkage, Visibility;
215 if (ParseOptionalLinkage(Linkage) ||
216 ParseOptionalVisibility(Visibility) ||
Chris Lattnereeb4a842009-07-02 23:08:13 +0000217 ParseGlobal("", SMLoc(), Linkage, true, Visibility))
Chris Lattnerdf986172009-01-02 07:01:27 +0000218 return true;
219 break;
220 }
221 case lltok::kw_default: // OptionalVisibility
222 case lltok::kw_hidden: // OptionalVisibility
223 case lltok::kw_protected: { // OptionalVisibility
224 unsigned Visibility;
225 if (ParseOptionalVisibility(Visibility) ||
Chris Lattnereeb4a842009-07-02 23:08:13 +0000226 ParseGlobal("", SMLoc(), 0, false, Visibility))
Chris Lattnerdf986172009-01-02 07:01:27 +0000227 return true;
228 break;
229 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000230
Chris Lattnerdf986172009-01-02 07:01:27 +0000231 case lltok::kw_thread_local: // OptionalThreadLocal
232 case lltok::kw_addrspace: // OptionalAddrSpace
233 case lltok::kw_constant: // GlobalType
234 case lltok::kw_global: // GlobalType
Chris Lattnereeb4a842009-07-02 23:08:13 +0000235 if (ParseGlobal("", SMLoc(), 0, false, 0)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +0000236 break;
237 }
238 }
239}
240
241
242/// toplevelentity
243/// ::= 'module' 'asm' STRINGCONSTANT
244bool LLParser::ParseModuleAsm() {
245 assert(Lex.getKind() == lltok::kw_module);
246 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000247
248 std::string AsmStr;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000249 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
250 ParseStringConstant(AsmStr)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000251
Rafael Espindola38c4e532011-03-02 04:14:42 +0000252 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000253 return false;
254}
255
256/// toplevelentity
257/// ::= 'target' 'triple' '=' STRINGCONSTANT
258/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
259bool LLParser::ParseTargetDefinition() {
260 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000261 std::string Str;
Chris Lattnerdf986172009-01-02 07:01:27 +0000262 switch (Lex.Lex()) {
263 default: return TokError("unknown target property");
264 case lltok::kw_triple:
265 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000266 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
267 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000268 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000269 M->setTargetTriple(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000270 return false;
271 case lltok::kw_datalayout:
272 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000273 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
274 ParseStringConstant(Str))
Chris Lattnerdf986172009-01-02 07:01:27 +0000275 return true;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000276 M->setDataLayout(Str);
Chris Lattnerdf986172009-01-02 07:01:27 +0000277 return false;
278 }
279}
280
281/// toplevelentity
282/// ::= 'deplibs' '=' '[' ']'
283/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
284bool LLParser::ParseDepLibs() {
285 assert(Lex.getKind() == lltok::kw_deplibs);
Chris Lattnerdf986172009-01-02 07:01:27 +0000286 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000287 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
288 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
289 return true;
290
291 if (EatIfPresent(lltok::rsquare))
292 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000293
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000294 std::string Str;
295 if (ParseStringConstant(Str)) return true;
296 M->addLibrary(Str);
297
298 while (EatIfPresent(lltok::comma)) {
299 if (ParseStringConstant(Str)) return true;
300 M->addLibrary(Str);
301 }
302
303 return ParseToken(lltok::rsquare, "expected ']' at end of list");
Chris Lattnerdf986172009-01-02 07:01:27 +0000304}
305
Dan Gohman3845e502009-08-12 23:32:33 +0000306/// ParseUnnamedType:
Chris Lattnerdf986172009-01-02 07:01:27 +0000307/// ::= 'type' type
Dan Gohman3845e502009-08-12 23:32:33 +0000308/// ::= LocalVarID '=' 'type' type
Chris Lattnerdf986172009-01-02 07:01:27 +0000309bool LLParser::ParseUnnamedType() {
Dan Gohman3845e502009-08-12 23:32:33 +0000310 unsigned TypeID = NumberedTypes.size();
311
312 // Handle the LocalVarID form.
313 if (Lex.getKind() == lltok::LocalVarID) {
314 if (Lex.getUIntVal() != TypeID)
315 return Error(Lex.getLoc(), "type expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000316 Twine(TypeID) + "'");
Dan Gohman3845e502009-08-12 23:32:33 +0000317 Lex.Lex(); // eat LocalVarID;
318
319 if (ParseToken(lltok::equal, "expected '=' after name"))
320 return true;
321 }
322
Chris Lattnerdf986172009-01-02 07:01:27 +0000323 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerf7240de2010-04-10 18:01:25 +0000324 if (ParseToken(lltok::kw_type, "expected 'type' after '='")) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +0000325
Owen Anderson1d0be152009-08-13 21:58:54 +0000326 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +0000327 if (ParseType(Ty)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000328
Chris Lattnerdf986172009-01-02 07:01:27 +0000329 // See if this type was previously referenced.
330 std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator
331 FI = ForwardRefTypeIDs.find(TypeID);
332 if (FI != ForwardRefTypeIDs.end()) {
Chris Lattnerc38daba2009-01-05 18:19:46 +0000333 if (FI->second.first.get() == Ty)
334 return Error(TypeLoc, "self referential type is invalid");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000335
Chris Lattnerdf986172009-01-02 07:01:27 +0000336 cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty);
337 Ty = FI->second.first.get();
338 ForwardRefTypeIDs.erase(FI);
339 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000340
Chris Lattnerdf986172009-01-02 07:01:27 +0000341 NumberedTypes.push_back(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000342
Chris Lattnerdf986172009-01-02 07:01:27 +0000343 return false;
344}
345
346/// toplevelentity
347/// ::= LocalVar '=' 'type' type
348bool LLParser::ParseNamedType() {
349 std::string Name = Lex.getStrVal();
350 LocTy NameLoc = Lex.getLoc();
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000351 Lex.Lex(); // eat LocalVar.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000352
Owen Anderson1d0be152009-08-13 21:58:54 +0000353 PATypeHolder Ty(Type::getVoidTy(Context));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000354
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000355 if (ParseToken(lltok::equal, "expected '=' after name") ||
356 ParseToken(lltok::kw_type, "expected 'type' after name") ||
357 ParseType(Ty))
358 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000359
Chris Lattnerdf986172009-01-02 07:01:27 +0000360 // Set the type name, checking for conflicts as we do so.
361 bool AlreadyExists = M->addTypeName(Name, Ty);
362 if (!AlreadyExists) return false;
363
364 // See if this type is a forward reference. We need to eagerly resolve
365 // types to allow recursive type redefinitions below.
366 std::map<std::string, std::pair<PATypeHolder, LocTy> >::iterator
367 FI = ForwardRefTypes.find(Name);
368 if (FI != ForwardRefTypes.end()) {
Chris Lattnerc38daba2009-01-05 18:19:46 +0000369 if (FI->second.first.get() == Ty)
370 return Error(NameLoc, "self referential type is invalid");
371
Chris Lattnerdf986172009-01-02 07:01:27 +0000372 cast<DerivedType>(FI->second.first.get())->refineAbstractTypeTo(Ty);
373 Ty = FI->second.first.get();
374 ForwardRefTypes.erase(FI);
375 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000376
Chris Lattnerdf986172009-01-02 07:01:27 +0000377 // Inserting a name that is already defined, get the existing name.
378 const Type *Existing = M->getTypeByName(Name);
379 assert(Existing && "Conflict but no matching type?!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000380
Chris Lattnerdf986172009-01-02 07:01:27 +0000381 // Otherwise, this is an attempt to redefine a type. That's okay if
382 // the redefinition is identical to the original.
383 // FIXME: REMOVE REDEFINITIONS IN LLVM 3.0
384 if (Existing == Ty) return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000385
Chris Lattnerdf986172009-01-02 07:01:27 +0000386 // Any other kind of (non-equivalent) redefinition is an error.
387 return Error(NameLoc, "redefinition of type named '" + Name + "' of type '" +
388 Ty->getDescription() + "'");
389}
390
391
392/// toplevelentity
393/// ::= 'declare' FunctionHeader
394bool LLParser::ParseDeclare() {
395 assert(Lex.getKind() == lltok::kw_declare);
396 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000397
Chris Lattnerdf986172009-01-02 07:01:27 +0000398 Function *F;
399 return ParseFunctionHeader(F, false);
400}
401
402/// toplevelentity
403/// ::= 'define' FunctionHeader '{' ...
404bool LLParser::ParseDefine() {
405 assert(Lex.getKind() == lltok::kw_define);
406 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000407
Chris Lattnerdf986172009-01-02 07:01:27 +0000408 Function *F;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000409 return ParseFunctionHeader(F, true) ||
410 ParseFunctionBody(*F);
Chris Lattnerdf986172009-01-02 07:01:27 +0000411}
412
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000413/// ParseGlobalType
414/// ::= 'constant'
415/// ::= 'global'
Chris Lattnerdf986172009-01-02 07:01:27 +0000416bool LLParser::ParseGlobalType(bool &IsConstant) {
417 if (Lex.getKind() == lltok::kw_constant)
418 IsConstant = true;
419 else if (Lex.getKind() == lltok::kw_global)
420 IsConstant = false;
Duncan Sands35b51072009-02-10 16:24:55 +0000421 else {
422 IsConstant = false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000423 return TokError("expected 'global' or 'constant'");
Duncan Sands35b51072009-02-10 16:24:55 +0000424 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000425 Lex.Lex();
426 return false;
427}
428
Dan Gohman3845e502009-08-12 23:32:33 +0000429/// ParseUnnamedGlobal:
430/// OptionalVisibility ALIAS ...
431/// OptionalLinkage OptionalVisibility ... -> global variable
432/// GlobalID '=' OptionalVisibility ALIAS ...
433/// GlobalID '=' OptionalLinkage OptionalVisibility ... -> global variable
434bool LLParser::ParseUnnamedGlobal() {
435 unsigned VarID = NumberedVals.size();
436 std::string Name;
437 LocTy NameLoc = Lex.getLoc();
438
439 // Handle the GlobalID form.
440 if (Lex.getKind() == lltok::GlobalID) {
441 if (Lex.getUIntVal() != VarID)
442 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000443 Twine(VarID) + "'");
Dan Gohman3845e502009-08-12 23:32:33 +0000444 Lex.Lex(); // eat GlobalID;
445
446 if (ParseToken(lltok::equal, "expected '=' after name"))
447 return true;
448 }
449
450 bool HasLinkage;
451 unsigned Linkage, Visibility;
452 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
453 ParseOptionalVisibility(Visibility))
454 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000455
Dan Gohman3845e502009-08-12 23:32:33 +0000456 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
457 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
458 return ParseAlias(Name, NameLoc, Visibility);
459}
460
Chris Lattnerdf986172009-01-02 07:01:27 +0000461/// ParseNamedGlobal:
462/// GlobalVar '=' OptionalVisibility ALIAS ...
463/// GlobalVar '=' OptionalLinkage OptionalVisibility ... -> global variable
464bool LLParser::ParseNamedGlobal() {
465 assert(Lex.getKind() == lltok::GlobalVar);
466 LocTy NameLoc = Lex.getLoc();
467 std::string Name = Lex.getStrVal();
468 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000469
Chris Lattnerdf986172009-01-02 07:01:27 +0000470 bool HasLinkage;
471 unsigned Linkage, Visibility;
472 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
473 ParseOptionalLinkage(Linkage, HasLinkage) ||
474 ParseOptionalVisibility(Visibility))
475 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000476
Chris Lattnerdf986172009-01-02 07:01:27 +0000477 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
478 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility);
479 return ParseAlias(Name, NameLoc, Visibility);
480}
481
Devang Patel256be962009-07-20 19:00:08 +0000482// MDString:
483// ::= '!' STRINGCONSTANT
Chris Lattner442ffa12009-12-29 21:53:55 +0000484bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000485 std::string Str;
486 if (ParseStringConstant(Str)) return true;
Chris Lattner442ffa12009-12-29 21:53:55 +0000487 Result = MDString::get(Context, Str);
Devang Patel256be962009-07-20 19:00:08 +0000488 return false;
489}
490
491// MDNode:
492// ::= '!' MDNodeNumber
Chris Lattner449c3102010-04-01 05:14:45 +0000493//
494/// This version of ParseMDNodeID returns the slot number and null in the case
495/// of a forward reference.
496bool LLParser::ParseMDNodeID(MDNode *&Result, unsigned &SlotNo) {
497 // !{ ..., !42, ... }
498 if (ParseUInt32(SlotNo)) return true;
499
500 // Check existing MDNode.
501 if (SlotNo < NumberedMetadata.size() && NumberedMetadata[SlotNo] != 0)
502 Result = NumberedMetadata[SlotNo];
503 else
504 Result = 0;
505 return false;
506}
507
Chris Lattner4a72efc2009-12-30 04:15:23 +0000508bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel256be962009-07-20 19:00:08 +0000509 // !{ ..., !42, ... }
510 unsigned MID = 0;
Chris Lattner449c3102010-04-01 05:14:45 +0000511 if (ParseMDNodeID(Result, MID)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000512
Chris Lattner449c3102010-04-01 05:14:45 +0000513 // If not a forward reference, just return it now.
514 if (Result) return false;
Devang Patel256be962009-07-20 19:00:08 +0000515
Chris Lattner449c3102010-04-01 05:14:45 +0000516 // Otherwise, create MDNode forward reference.
Jay Foadec9186b2011-04-21 19:59:31 +0000517 MDNode *FwdNode = MDNode::getTemporary(Context, ArrayRef<Value*>());
Devang Patel256be962009-07-20 19:00:08 +0000518 ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc());
Chris Lattner0834e6a2009-12-30 04:51:58 +0000519
520 if (NumberedMetadata.size() <= MID)
521 NumberedMetadata.resize(MID+1);
522 NumberedMetadata[MID] = FwdNode;
Chris Lattner442ffa12009-12-29 21:53:55 +0000523 Result = FwdNode;
Devang Patel256be962009-07-20 19:00:08 +0000524 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000525}
Devang Patel256be962009-07-20 19:00:08 +0000526
Chris Lattner84d03b12009-12-29 22:35:39 +0000527/// ParseNamedMetadata:
Devang Pateleff2ab62009-07-29 00:34:02 +0000528/// !foo = !{ !1, !2 }
529bool LLParser::ParseNamedMetadata() {
Chris Lattner1d928312009-12-30 05:02:06 +0000530 assert(Lex.getKind() == lltok::MetadataVar);
Devang Pateleff2ab62009-07-29 00:34:02 +0000531 std::string Name = Lex.getStrVal();
Chris Lattner1d928312009-12-30 05:02:06 +0000532 Lex.Lex();
Devang Pateleff2ab62009-07-29 00:34:02 +0000533
Chris Lattner84d03b12009-12-29 22:35:39 +0000534 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattnere434d272009-12-30 04:56:59 +0000535 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner84d03b12009-12-29 22:35:39 +0000536 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Pateleff2ab62009-07-29 00:34:02 +0000537 return true;
538
Dan Gohman17aa92c2010-07-21 23:38:33 +0000539 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000540 if (Lex.getKind() != lltok::rbrace)
541 do {
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000542 if (ParseToken(lltok::exclaim, "Expected '!' here"))
543 return true;
Chris Lattner442ffa12009-12-29 21:53:55 +0000544
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000545 MDNode *N = 0;
546 if (ParseMDNodeID(N)) return true;
Dan Gohman17aa92c2010-07-21 23:38:33 +0000547 NMD->addOperand(N);
Dan Gohman9dc8ae12010-07-13 19:42:44 +0000548 } while (EatIfPresent(lltok::comma));
Devang Pateleff2ab62009-07-29 00:34:02 +0000549
550 if (ParseToken(lltok::rbrace, "expected end of metadata node"))
551 return true;
552
Devang Pateleff2ab62009-07-29 00:34:02 +0000553 return false;
554}
555
Devang Patel923078c2009-07-01 19:21:12 +0000556/// ParseStandaloneMetadata:
Daniel Dunbara279bc32009-09-20 02:20:51 +0000557/// !42 = !{...}
Devang Patel923078c2009-07-01 19:21:12 +0000558bool LLParser::ParseStandaloneMetadata() {
Chris Lattnere434d272009-12-30 04:56:59 +0000559 assert(Lex.getKind() == lltok::exclaim);
Devang Patel923078c2009-07-01 19:21:12 +0000560 Lex.Lex();
561 unsigned MetadataID = 0;
Devang Patel923078c2009-07-01 19:21:12 +0000562
563 LocTy TyLoc;
Owen Anderson1d0be152009-08-13 21:58:54 +0000564 PATypeHolder Ty(Type::getVoidTy(Context));
Devang Patel104cf9e2009-07-23 01:07:34 +0000565 SmallVector<Value *, 16> Elts;
Chris Lattner3f5132a2009-12-29 22:40:21 +0000566 if (ParseUInt32(MetadataID) ||
567 ParseToken(lltok::equal, "expected '=' here") ||
568 ParseType(Ty, TyLoc) ||
Chris Lattnere434d272009-12-30 04:56:59 +0000569 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000570 ParseToken(lltok::lbrace, "Expected '{' here") ||
Victor Hernandez24e64df2010-01-10 07:14:18 +0000571 ParseMDNodeVector(Elts, NULL) ||
Chris Lattner3f5132a2009-12-29 22:40:21 +0000572 ParseToken(lltok::rbrace, "expected end of metadata node"))
Devang Patel104cf9e2009-07-23 01:07:34 +0000573 return true;
574
Jay Foadec9186b2011-04-21 19:59:31 +0000575 MDNode *Init = MDNode::get(Context, Elts);
Chris Lattner0834e6a2009-12-30 04:51:58 +0000576
577 // See if this was forward referenced, if so, handle it.
Chris Lattnere80250e2009-12-29 21:43:58 +0000578 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> >::iterator
Devang Patel1c7eea62009-07-08 19:23:54 +0000579 FI = ForwardRefMDNodes.find(MetadataID);
580 if (FI != ForwardRefMDNodes.end()) {
Dan Gohman489b29b2010-08-20 22:02:26 +0000581 MDNode *Temp = FI->second.first;
582 Temp->replaceAllUsesWith(Init);
583 MDNode::deleteTemporary(Temp);
Devang Patel1c7eea62009-07-08 19:23:54 +0000584 ForwardRefMDNodes.erase(FI);
Chris Lattner0834e6a2009-12-30 04:51:58 +0000585
586 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
587 } else {
588 if (MetadataID >= NumberedMetadata.size())
589 NumberedMetadata.resize(MetadataID+1);
590
591 if (NumberedMetadata[MetadataID] != 0)
592 return TokError("Metadata id is already used");
593 NumberedMetadata[MetadataID] = Init;
Devang Patel1c7eea62009-07-08 19:23:54 +0000594 }
595
Devang Patel923078c2009-07-01 19:21:12 +0000596 return false;
597}
598
Chris Lattnerdf986172009-01-02 07:01:27 +0000599/// ParseAlias:
600/// ::= GlobalVar '=' OptionalVisibility 'alias' OptionalLinkage Aliasee
601/// Aliasee
Chris Lattner040f7582009-04-25 21:26:00 +0000602/// ::= TypeAndValue
603/// ::= 'bitcast' '(' TypeAndValue 'to' Type ')'
Dan Gohmandd8004d2009-07-27 21:53:46 +0000604/// ::= 'getelementptr' 'inbounds'? '(' ... ')'
Chris Lattnerdf986172009-01-02 07:01:27 +0000605///
606/// Everything through visibility has already been parsed.
607///
608bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
609 unsigned Visibility) {
610 assert(Lex.getKind() == lltok::kw_alias);
611 Lex.Lex();
612 unsigned Linkage;
613 LocTy LinkageLoc = Lex.getLoc();
614 if (ParseOptionalLinkage(Linkage))
615 return true;
616
617 if (Linkage != GlobalValue::ExternalLinkage &&
Duncan Sands667d4b82009-03-07 15:45:40 +0000618 Linkage != GlobalValue::WeakAnyLinkage &&
619 Linkage != GlobalValue::WeakODRLinkage &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000620 Linkage != GlobalValue::InternalLinkage &&
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000621 Linkage != GlobalValue::PrivateLinkage &&
Bill Wendling5e721d72010-07-01 21:55:59 +0000622 Linkage != GlobalValue::LinkerPrivateLinkage &&
Bill Wendling55ae5152010-08-20 22:05:50 +0000623 Linkage != GlobalValue::LinkerPrivateWeakLinkage &&
624 Linkage != GlobalValue::LinkerPrivateWeakDefAutoLinkage)
Chris Lattnerdf986172009-01-02 07:01:27 +0000625 return Error(LinkageLoc, "invalid linkage type for alias");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000626
Chris Lattnerdf986172009-01-02 07:01:27 +0000627 Constant *Aliasee;
628 LocTy AliaseeLoc = Lex.getLoc();
Chris Lattner040f7582009-04-25 21:26:00 +0000629 if (Lex.getKind() != lltok::kw_bitcast &&
630 Lex.getKind() != lltok::kw_getelementptr) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000631 if (ParseGlobalTypeAndValue(Aliasee)) return true;
632 } else {
633 // The bitcast dest type is not present, it is implied by the dest type.
634 ValID ID;
635 if (ParseValID(ID)) return true;
636 if (ID.Kind != ValID::t_Constant)
637 return Error(AliaseeLoc, "invalid aliasee");
638 Aliasee = ID.ConstantVal;
639 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000640
Duncan Sands1df98592010-02-16 11:11:14 +0000641 if (!Aliasee->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +0000642 return Error(AliaseeLoc, "alias must have pointer type");
643
644 // Okay, create the alias but do not insert it into the module yet.
645 GlobalAlias* GA = new GlobalAlias(Aliasee->getType(),
646 (GlobalValue::LinkageTypes)Linkage, Name,
647 Aliasee);
648 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000649
Chris Lattnerdf986172009-01-02 07:01:27 +0000650 // See if this value already exists in the symbol table. If so, it is either
651 // a redefinition or a definition of a forward reference.
Chris Lattner1d871c52009-10-25 23:22:50 +0000652 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000653 // See if this was a redefinition. If so, there is no entry in
654 // ForwardRefVals.
655 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
656 I = ForwardRefVals.find(Name);
657 if (I == ForwardRefVals.end())
658 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
659
660 // Otherwise, this was a definition of forward ref. Verify that types
661 // agree.
662 if (Val->getType() != GA->getType())
663 return Error(NameLoc,
664 "forward reference and definition of alias have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000665
Chris Lattnerdf986172009-01-02 07:01:27 +0000666 // If they agree, just RAUW the old value with the alias and remove the
667 // forward ref info.
668 Val->replaceAllUsesWith(GA);
669 Val->eraseFromParent();
670 ForwardRefVals.erase(I);
671 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000672
Chris Lattnerdf986172009-01-02 07:01:27 +0000673 // Insert into the module, we know its name won't collide now.
674 M->getAliasList().push_back(GA);
Benjamin Krameraf812352010-10-16 11:28:23 +0000675 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000676
Chris Lattnerdf986172009-01-02 07:01:27 +0000677 return false;
678}
679
680/// ParseGlobal
681/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000682/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000683/// ::= OptionalLinkage OptionalVisibility OptionalThreadLocal
Rafael Espindolabea46262011-01-08 16:42:36 +0000684/// OptionalAddrSpace OptionalUnNammedAddr GlobalType Type Const
Chris Lattnerdf986172009-01-02 07:01:27 +0000685///
686/// Everything through visibility has been parsed already.
687///
688bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
689 unsigned Linkage, bool HasLinkage,
690 unsigned Visibility) {
691 unsigned AddrSpace;
Rafael Espindolabea46262011-01-08 16:42:36 +0000692 bool ThreadLocal, IsConstant, UnnamedAddr;
Rafael Espindolad72479c2011-01-13 01:30:30 +0000693 LocTy UnnamedAddrLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +0000694 LocTy TyLoc;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000695
Owen Anderson1d0be152009-08-13 21:58:54 +0000696 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +0000697 if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
698 ParseOptionalAddrSpace(AddrSpace) ||
Rafael Espindolad72479c2011-01-13 01:30:30 +0000699 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
700 &UnnamedAddrLoc) ||
Chris Lattnerdf986172009-01-02 07:01:27 +0000701 ParseGlobalType(IsConstant) ||
702 ParseType(Ty, TyLoc))
703 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000704
Chris Lattnerdf986172009-01-02 07:01:27 +0000705 // If the linkage is specified and is external, then no initializer is
706 // present.
707 Constant *Init = 0;
708 if (!HasLinkage || (Linkage != GlobalValue::DLLImportLinkage &&
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000709 Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerdf986172009-01-02 07:01:27 +0000710 Linkage != GlobalValue::ExternalLinkage)) {
711 if (ParseGlobalValue(Ty, Init))
712 return true;
713 }
714
Duncan Sands1df98592010-02-16 11:11:14 +0000715 if (Ty->isFunctionTy() || Ty->isLabelTy())
Chris Lattner4a2f1122009-02-08 20:00:15 +0000716 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000717
Chris Lattnerdf986172009-01-02 07:01:27 +0000718 GlobalVariable *GV = 0;
719
720 // See if the global was forward referenced, if so, use the global.
Chris Lattner91dad872009-02-02 07:24:28 +0000721 if (!Name.empty()) {
Chris Lattner1d871c52009-10-25 23:22:50 +0000722 if (GlobalValue *GVal = M->getNamedValue(Name)) {
723 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
724 return Error(NameLoc, "redefinition of global '@" + Name + "'");
725 GV = cast<GlobalVariable>(GVal);
726 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000727 } else {
728 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
729 I = ForwardRefValIDs.find(NumberedVals.size());
730 if (I != ForwardRefValIDs.end()) {
731 GV = cast<GlobalVariable>(I->second.first);
732 ForwardRefValIDs.erase(I);
733 }
734 }
735
736 if (GV == 0) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000737 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0,
Owen Andersone9b11b42009-07-08 19:03:57 +0000738 Name, 0, false, AddrSpace);
Chris Lattnerdf986172009-01-02 07:01:27 +0000739 } else {
740 if (GV->getType()->getElementType() != Ty)
741 return Error(TyLoc,
742 "forward reference and definition of global have different types");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000743
Chris Lattnerdf986172009-01-02 07:01:27 +0000744 // Move the forward-reference to the correct spot in the module.
745 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
746 }
747
748 if (Name.empty())
749 NumberedVals.push_back(GV);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000750
Chris Lattnerdf986172009-01-02 07:01:27 +0000751 // Set the parsed properties on the global.
752 if (Init)
753 GV->setInitializer(Init);
754 GV->setConstant(IsConstant);
755 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
756 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
757 GV->setThreadLocal(ThreadLocal);
Rafael Espindolabea46262011-01-08 16:42:36 +0000758 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000759
Chris Lattnerdf986172009-01-02 07:01:27 +0000760 // Parse attributes on the global.
761 while (Lex.getKind() == lltok::comma) {
762 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000763
Chris Lattnerdf986172009-01-02 07:01:27 +0000764 if (Lex.getKind() == lltok::kw_section) {
765 Lex.Lex();
766 GV->setSection(Lex.getStrVal());
767 if (ParseToken(lltok::StringConstant, "expected global section string"))
768 return true;
769 } else if (Lex.getKind() == lltok::kw_align) {
770 unsigned Alignment;
771 if (ParseOptionalAlignment(Alignment)) return true;
772 GV->setAlignment(Alignment);
773 } else {
774 TokError("unknown global variable property!");
775 }
776 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000777
Chris Lattnerdf986172009-01-02 07:01:27 +0000778 return false;
779}
780
781
782//===----------------------------------------------------------------------===//
783// GlobalValue Reference/Resolution Routines.
784//===----------------------------------------------------------------------===//
785
786/// GetGlobalVal - Get a value with the specified name or ID, creating a
787/// forward reference record if needed. This can return null if the value
788/// exists but does not have the right type.
789GlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty,
790 LocTy Loc) {
791 const PointerType *PTy = dyn_cast<PointerType>(Ty);
792 if (PTy == 0) {
793 Error(Loc, "global variable reference must have pointer type");
794 return 0;
795 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000796
Chris Lattnerdf986172009-01-02 07:01:27 +0000797 // Look this name up in the normal function symbol table.
798 GlobalValue *Val =
799 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000800
Chris Lattnerdf986172009-01-02 07:01:27 +0000801 // If this is a forward reference for the value, see if we already created a
802 // forward ref record.
803 if (Val == 0) {
804 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
805 I = ForwardRefVals.find(Name);
806 if (I != ForwardRefVals.end())
807 Val = I->second.first;
808 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000809
Chris Lattnerdf986172009-01-02 07:01:27 +0000810 // If we have the value in the symbol table or fwd-ref table, return it.
811 if (Val) {
812 if (Val->getType() == Ty) return Val;
813 Error(Loc, "'@" + Name + "' defined with type '" +
814 Val->getType()->getDescription() + "'");
815 return 0;
816 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000817
Chris Lattnerdf986172009-01-02 07:01:27 +0000818 // Otherwise, create a new forward reference for this value and remember it.
819 GlobalValue *FwdVal;
Chris Lattner1e407c32009-01-08 19:05:36 +0000820 if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) {
821 // Function types can return opaque but functions can't.
Duncan Sands47c51882010-02-16 14:50:09 +0000822 if (FT->getReturnType()->isOpaqueTy()) {
Chris Lattner1e407c32009-01-08 19:05:36 +0000823 Error(Loc, "function may not return opaque type");
824 return 0;
825 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000826
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000827 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattner1e407c32009-01-08 19:05:36 +0000828 } else {
Owen Andersone9b11b42009-07-08 19:03:57 +0000829 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
830 GlobalValue::ExternalWeakLinkage, 0, Name);
Chris Lattner1e407c32009-01-08 19:05:36 +0000831 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000832
Chris Lattnerdf986172009-01-02 07:01:27 +0000833 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
834 return FwdVal;
835}
836
837GlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) {
838 const PointerType *PTy = dyn_cast<PointerType>(Ty);
839 if (PTy == 0) {
840 Error(Loc, "global variable reference must have pointer type");
841 return 0;
842 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000843
Chris Lattnerdf986172009-01-02 07:01:27 +0000844 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000845
Chris Lattnerdf986172009-01-02 07:01:27 +0000846 // If this is a forward reference for the value, see if we already created a
847 // forward ref record.
848 if (Val == 0) {
849 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
850 I = ForwardRefValIDs.find(ID);
851 if (I != ForwardRefValIDs.end())
852 Val = I->second.first;
853 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000854
Chris Lattnerdf986172009-01-02 07:01:27 +0000855 // If we have the value in the symbol table or fwd-ref table, return it.
856 if (Val) {
857 if (Val->getType() == Ty) return Val;
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000858 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +0000859 Val->getType()->getDescription() + "'");
860 return 0;
861 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000862
Chris Lattnerdf986172009-01-02 07:01:27 +0000863 // Otherwise, create a new forward reference for this value and remember it.
864 GlobalValue *FwdVal;
Chris Lattner830703b2009-01-05 18:27:50 +0000865 if (const FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType())) {
866 // Function types can return opaque but functions can't.
Duncan Sands47c51882010-02-16 14:50:09 +0000867 if (FT->getReturnType()->isOpaqueTy()) {
Chris Lattner0d8484f2009-01-05 18:56:52 +0000868 Error(Loc, "function may not return opaque type");
Chris Lattner830703b2009-01-05 18:27:50 +0000869 return 0;
870 }
Duncan Sands5f4ee1f2009-03-11 08:08:06 +0000871 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattner830703b2009-01-05 18:27:50 +0000872 } else {
Owen Andersone9b11b42009-07-08 19:03:57 +0000873 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
874 GlobalValue::ExternalWeakLinkage, 0, "");
Chris Lattner830703b2009-01-05 18:27:50 +0000875 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000876
Chris Lattnerdf986172009-01-02 07:01:27 +0000877 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
878 return FwdVal;
879}
880
881
882//===----------------------------------------------------------------------===//
883// Helper Routines.
884//===----------------------------------------------------------------------===//
885
886/// ParseToken - If the current token has the specified kind, eat it and return
887/// success. Otherwise, emit the specified error and return failure.
888bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
889 if (Lex.getKind() != T)
890 return TokError(ErrMsg);
891 Lex.Lex();
892 return false;
893}
894
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000895/// ParseStringConstant
896/// ::= StringConstant
897bool LLParser::ParseStringConstant(std::string &Result) {
898 if (Lex.getKind() != lltok::StringConstant)
899 return TokError("expected string constant");
900 Result = Lex.getStrVal();
901 Lex.Lex();
902 return false;
903}
904
905/// ParseUInt32
906/// ::= uint32
907bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000908 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
909 return TokError("expected integer");
910 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
911 if (Val64 != unsigned(Val64))
912 return TokError("expected 32-bit integer (too large)");
913 Val = Val64;
914 Lex.Lex();
915 return false;
916}
917
918
919/// ParseOptionalAddrSpace
920/// := /*empty*/
921/// := 'addrspace' '(' uint32 ')'
922bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
923 AddrSpace = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000924 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerdf986172009-01-02 07:01:27 +0000925 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +0000926 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +0000927 ParseUInt32(AddrSpace) ||
Chris Lattnerdf986172009-01-02 07:01:27 +0000928 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000929}
Chris Lattnerdf986172009-01-02 07:01:27 +0000930
931/// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind
932/// indicates what kind of attribute list this is: 0: function arg, 1: result,
933/// 2: function attr.
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000934/// 3: function arg after value: FIXME: REMOVE IN LLVM 3.0
Chris Lattnerdf986172009-01-02 07:01:27 +0000935bool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) {
936 Attrs = Attribute::None;
937 LocTy AttrLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000938
Chris Lattnerdf986172009-01-02 07:01:27 +0000939 while (1) {
940 switch (Lex.getKind()) {
941 case lltok::kw_sext:
942 case lltok::kw_zext:
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000943 // Treat these as signext/zeroext if they occur in the argument list after
944 // the value, as in "call i8 @foo(i8 10 sext)". If they occur before the
945 // value, as in "call i8 @foo(i8 sext (" then it is part of a constant
946 // expr.
Chris Lattnerdf986172009-01-02 07:01:27 +0000947 // FIXME: REMOVE THIS IN LLVM 3.0
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000948 if (AttrKind == 3) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000949 if (Lex.getKind() == lltok::kw_sext)
950 Attrs |= Attribute::SExt;
951 else
952 Attrs |= Attribute::ZExt;
953 break;
954 }
955 // FALL THROUGH.
956 default: // End of attributes.
957 if (AttrKind != 2 && (Attrs & Attribute::FunctionOnly))
958 return Error(AttrLoc, "invalid use of function-only attribute");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000959
Chris Lattnerad9ad7c2009-03-25 06:36:36 +0000960 if (AttrKind != 0 && AttrKind != 3 && (Attrs & Attribute::ParameterOnly))
Chris Lattnerdf986172009-01-02 07:01:27 +0000961 return Error(AttrLoc, "invalid use of parameter-only attribute");
Daniel Dunbara279bc32009-09-20 02:20:51 +0000962
Chris Lattnerdf986172009-01-02 07:01:27 +0000963 return false;
Devang Patel578efa92009-06-05 21:57:13 +0000964 case lltok::kw_zeroext: Attrs |= Attribute::ZExt; break;
965 case lltok::kw_signext: Attrs |= Attribute::SExt; break;
966 case lltok::kw_inreg: Attrs |= Attribute::InReg; break;
967 case lltok::kw_sret: Attrs |= Attribute::StructRet; break;
968 case lltok::kw_noalias: Attrs |= Attribute::NoAlias; break;
969 case lltok::kw_nocapture: Attrs |= Attribute::NoCapture; break;
970 case lltok::kw_byval: Attrs |= Attribute::ByVal; break;
971 case lltok::kw_nest: Attrs |= Attribute::Nest; break;
Chris Lattnerdf986172009-01-02 07:01:27 +0000972
Devang Patel578efa92009-06-05 21:57:13 +0000973 case lltok::kw_noreturn: Attrs |= Attribute::NoReturn; break;
974 case lltok::kw_nounwind: Attrs |= Attribute::NoUnwind; break;
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000975 case lltok::kw_uwtable: Attrs |= Attribute::UWTable; break;
Devang Patel578efa92009-06-05 21:57:13 +0000976 case lltok::kw_noinline: Attrs |= Attribute::NoInline; break;
977 case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break;
978 case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break;
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000979 case lltok::kw_inlinehint: Attrs |= Attribute::InlineHint; break;
Devang Patel578efa92009-06-05 21:57:13 +0000980 case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break;
981 case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break;
982 case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break;
983 case lltok::kw_sspreq: Attrs |= Attribute::StackProtectReq; break;
984 case lltok::kw_noredzone: Attrs |= Attribute::NoRedZone; break;
985 case lltok::kw_noimplicitfloat: Attrs |= Attribute::NoImplicitFloat; break;
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000986 case lltok::kw_naked: Attrs |= Attribute::Naked; break;
Charles Davis970bfcc2010-10-25 15:37:09 +0000987 case lltok::kw_hotpatch: Attrs |= Attribute::Hotpatch; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000988
Charles Davis1e063d12010-02-12 00:31:15 +0000989 case lltok::kw_alignstack: {
990 unsigned Alignment;
991 if (ParseOptionalStackAlignment(Alignment))
992 return true;
993 Attrs |= Attribute::constructStackAlignmentFromInt(Alignment);
994 continue;
995 }
996
Chris Lattnerdf986172009-01-02 07:01:27 +0000997 case lltok::kw_align: {
998 unsigned Alignment;
999 if (ParseOptionalAlignment(Alignment))
1000 return true;
1001 Attrs |= Attribute::constructAlignmentFromInt(Alignment);
1002 continue;
1003 }
Charles Davis1e063d12010-02-12 00:31:15 +00001004
Chris Lattnerdf986172009-01-02 07:01:27 +00001005 }
1006 Lex.Lex();
1007 }
1008}
1009
1010/// ParseOptionalLinkage
1011/// ::= /*empty*/
Rafael Espindolabb46f522009-01-15 20:18:42 +00001012/// ::= 'private'
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001013/// ::= 'linker_private'
Bill Wendling5e721d72010-07-01 21:55:59 +00001014/// ::= 'linker_private_weak'
Bill Wendling55ae5152010-08-20 22:05:50 +00001015/// ::= 'linker_private_weak_def_auto'
Chris Lattnerdf986172009-01-02 07:01:27 +00001016/// ::= 'internal'
1017/// ::= 'weak'
Duncan Sands667d4b82009-03-07 15:45:40 +00001018/// ::= 'weak_odr'
Chris Lattnerdf986172009-01-02 07:01:27 +00001019/// ::= 'linkonce'
Duncan Sands667d4b82009-03-07 15:45:40 +00001020/// ::= 'linkonce_odr'
Bill Wendling5e721d72010-07-01 21:55:59 +00001021/// ::= 'available_externally'
Chris Lattnerdf986172009-01-02 07:01:27 +00001022/// ::= 'appending'
1023/// ::= 'dllexport'
1024/// ::= 'common'
1025/// ::= 'dllimport'
1026/// ::= 'extern_weak'
1027/// ::= 'external'
1028bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1029 HasLinkage = false;
1030 switch (Lex.getKind()) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001031 default: Res=GlobalValue::ExternalLinkage; return false;
1032 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
1033 case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001034 case lltok::kw_linker_private_weak:
1035 Res = GlobalValue::LinkerPrivateWeakLinkage;
1036 break;
Bill Wendling55ae5152010-08-20 22:05:50 +00001037 case lltok::kw_linker_private_weak_def_auto:
1038 Res = GlobalValue::LinkerPrivateWeakDefAutoLinkage;
1039 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001040 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1041 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1042 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1043 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1044 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001045 case lltok::kw_available_externally:
1046 Res = GlobalValue::AvailableExternallyLinkage;
1047 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001048 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
1049 case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break;
1050 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
1051 case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break;
1052 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1053 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001054 }
1055 Lex.Lex();
1056 HasLinkage = true;
1057 return false;
1058}
1059
1060/// ParseOptionalVisibility
1061/// ::= /*empty*/
1062/// ::= 'default'
1063/// ::= 'hidden'
1064/// ::= 'protected'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001065///
Chris Lattnerdf986172009-01-02 07:01:27 +00001066bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1067 switch (Lex.getKind()) {
1068 default: Res = GlobalValue::DefaultVisibility; return false;
1069 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1070 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1071 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1072 }
1073 Lex.Lex();
1074 return false;
1075}
1076
1077/// ParseOptionalCallingConv
1078/// ::= /*empty*/
1079/// ::= 'ccc'
1080/// ::= 'fastcc'
1081/// ::= 'coldcc'
1082/// ::= 'x86_stdcallcc'
1083/// ::= 'x86_fastcallcc'
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001084/// ::= 'x86_thiscallcc'
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001085/// ::= 'arm_apcscc'
1086/// ::= 'arm_aapcscc'
1087/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001088/// ::= 'msp430_intrcc'
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001089/// ::= 'ptx_kernel'
1090/// ::= 'ptx_device'
Chris Lattnerdf986172009-01-02 07:01:27 +00001091/// ::= 'cc' UINT
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001092///
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001093bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001094 switch (Lex.getKind()) {
1095 default: CC = CallingConv::C; return false;
1096 case lltok::kw_ccc: CC = CallingConv::C; break;
1097 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1098 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1099 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1100 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001101 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001102 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1103 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1104 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001105 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001106 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1107 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001108 case lltok::kw_cc: {
1109 unsigned ArbitraryCC;
1110 Lex.Lex();
1111 if (ParseUInt32(ArbitraryCC)) {
1112 return true;
1113 } else
1114 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1115 return false;
1116 }
1117 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001118 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001119
Chris Lattnerdf986172009-01-02 07:01:27 +00001120 Lex.Lex();
1121 return false;
1122}
1123
Chris Lattnerb8c46862009-12-30 05:31:19 +00001124/// ParseInstructionMetadata
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001125/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman9d072f52010-08-24 02:05:17 +00001126bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1127 PerFunctionState *PFS) {
Chris Lattnerb8c46862009-12-30 05:31:19 +00001128 do {
1129 if (Lex.getKind() != lltok::MetadataVar)
1130 return TokError("expected metadata after comma");
Devang Patel0475c912009-09-29 00:01:14 +00001131
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001132 std::string Name = Lex.getStrVal();
Dan Gohman309b3af2010-08-24 02:24:03 +00001133 unsigned MDK = M->getMDKindID(Name.c_str());
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001134 Lex.Lex();
Chris Lattner52e20312009-10-19 05:31:10 +00001135
Chris Lattner442ffa12009-12-29 21:53:55 +00001136 MDNode *Node;
Chris Lattner449c3102010-04-01 05:14:45 +00001137 SMLoc Loc = Lex.getLoc();
Dan Gohman309b3af2010-08-24 02:24:03 +00001138
1139 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattnere434d272009-12-30 04:56:59 +00001140 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001141
Dan Gohman68261142010-08-24 14:35:45 +00001142 // This code is similar to that of ParseMetadataValue, however it needs to
1143 // have special-case code for a forward reference; see the comments on
1144 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1145 // at the top level here.
Dan Gohman309b3af2010-08-24 02:24:03 +00001146 if (Lex.getKind() == lltok::lbrace) {
1147 ValID ID;
1148 if (ParseMetadataListValue(ID, PFS))
1149 return true;
1150 assert(ID.Kind == ValID::t_MDNode);
1151 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner449c3102010-04-01 05:14:45 +00001152 } else {
Nick Lewyckyc6877b42010-09-30 21:04:13 +00001153 unsigned NodeID = 0;
Dan Gohman309b3af2010-08-24 02:24:03 +00001154 if (ParseMDNodeID(Node, NodeID))
1155 return true;
1156 if (Node) {
1157 // If we got the node, add it to the instruction.
1158 Inst->setMetadata(MDK, Node);
1159 } else {
1160 MDRef R = { Loc, MDK, NodeID };
1161 // Otherwise, remember that this should be resolved later.
1162 ForwardRefInstMetadata[Inst].push_back(R);
1163 }
Chris Lattner449c3102010-04-01 05:14:45 +00001164 }
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001165
1166 // If this is the end of the list, we're done.
Chris Lattnerb8c46862009-12-30 05:31:19 +00001167 } while (EatIfPresent(lltok::comma));
1168 return false;
Devang Patelf633a062009-09-17 23:04:48 +00001169}
1170
Chris Lattnerdf986172009-01-02 07:01:27 +00001171/// ParseOptionalAlignment
1172/// ::= /* empty */
1173/// ::= 'align' 4
1174bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1175 Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001176 if (!EatIfPresent(lltok::kw_align))
1177 return false;
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001178 LocTy AlignLoc = Lex.getLoc();
1179 if (ParseUInt32(Alignment)) return true;
1180 if (!isPowerOf2_32(Alignment))
1181 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmane16829b2010-07-30 21:07:05 +00001182 if (Alignment > Value::MaximumAlignment)
Dan Gohman138aa2a2010-07-28 20:12:04 +00001183 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001184 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001185}
1186
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001187/// ParseOptionalCommaAlign
1188/// ::=
1189/// ::= ',' align 4
1190///
1191/// This returns with AteExtraComma set to true if it ate an excess comma at the
1192/// end.
1193bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1194 bool &AteExtraComma) {
1195 AteExtraComma = false;
1196 while (EatIfPresent(lltok::comma)) {
1197 // Metadata at the end is an early exit.
Chris Lattner1d928312009-12-30 05:02:06 +00001198 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001199 AteExtraComma = true;
1200 return false;
1201 }
1202
Chris Lattner093eed12010-04-23 00:50:50 +00001203 if (Lex.getKind() != lltok::kw_align)
1204 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sandsbf9fc532010-10-21 16:07:10 +00001205
Chris Lattner093eed12010-04-23 00:50:50 +00001206 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001207 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001208
Devang Patelf633a062009-09-17 23:04:48 +00001209 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001210}
1211
Charles Davis1e063d12010-02-12 00:31:15 +00001212/// ParseOptionalStackAlignment
1213/// ::= /* empty */
1214/// ::= 'alignstack' '(' 4 ')'
1215bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1216 Alignment = 0;
1217 if (!EatIfPresent(lltok::kw_alignstack))
1218 return false;
1219 LocTy ParenLoc = Lex.getLoc();
1220 if (!EatIfPresent(lltok::lparen))
1221 return Error(ParenLoc, "expected '('");
1222 LocTy AlignLoc = Lex.getLoc();
1223 if (ParseUInt32(Alignment)) return true;
1224 ParenLoc = Lex.getLoc();
1225 if (!EatIfPresent(lltok::rparen))
1226 return Error(ParenLoc, "expected ')'");
1227 if (!isPowerOf2_32(Alignment))
1228 return Error(AlignLoc, "stack alignment is not a power of two");
1229 return false;
1230}
Devang Patelf633a062009-09-17 23:04:48 +00001231
Chris Lattner628c13a2009-12-30 05:14:00 +00001232/// ParseIndexList - This parses the index list for an insert/extractvalue
1233/// instruction. This sets AteExtraComma in the case where we eat an extra
1234/// comma at the end of the line and find that it is followed by metadata.
1235/// Clients that don't allow metadata can call the version of this function that
1236/// only takes one argument.
1237///
Chris Lattnerdf986172009-01-02 07:01:27 +00001238/// ParseIndexList
1239/// ::= (',' uint32)+
Chris Lattner628c13a2009-12-30 05:14:00 +00001240///
1241bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1242 bool &AteExtraComma) {
1243 AteExtraComma = false;
1244
Chris Lattnerdf986172009-01-02 07:01:27 +00001245 if (Lex.getKind() != lltok::comma)
1246 return TokError("expected ',' as start of index list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001247
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001248 while (EatIfPresent(lltok::comma)) {
Chris Lattner628c13a2009-12-30 05:14:00 +00001249 if (Lex.getKind() == lltok::MetadataVar) {
1250 AteExtraComma = true;
1251 return false;
1252 }
Nick Lewycky28815c42010-09-29 23:32:20 +00001253 unsigned Idx = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001254 if (ParseUInt32(Idx)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001255 Indices.push_back(Idx);
1256 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001257
Chris Lattnerdf986172009-01-02 07:01:27 +00001258 return false;
1259}
1260
1261//===----------------------------------------------------------------------===//
1262// Type Parsing.
1263//===----------------------------------------------------------------------===//
1264
1265/// ParseType - Parse and resolve a full type.
Chris Lattnera9a9e072009-03-09 04:49:14 +00001266bool LLParser::ParseType(PATypeHolder &Result, bool AllowVoid) {
1267 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001268 if (ParseTypeRec(Result)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001269
Chris Lattnerdf986172009-01-02 07:01:27 +00001270 // Verify no unresolved uprefs.
1271 if (!UpRefs.empty())
1272 return Error(UpRefs.back().Loc, "invalid unresolved type up reference");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001273
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001274 if (!AllowVoid && Result.get()->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001275 return Error(TypeLoc, "void type only allowed for function results");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001276
Chris Lattnerdf986172009-01-02 07:01:27 +00001277 return false;
1278}
1279
1280/// HandleUpRefs - Every time we finish a new layer of types, this function is
1281/// called. It loops through the UpRefs vector, which is a list of the
1282/// currently active types. For each type, if the up-reference is contained in
1283/// the newly completed type, we decrement the level count. When the level
1284/// count reaches zero, the up-referenced type is the type that is passed in:
1285/// thus we can complete the cycle.
1286///
1287PATypeHolder LLParser::HandleUpRefs(const Type *ty) {
1288 // If Ty isn't abstract, or if there are no up-references in it, then there is
1289 // nothing to resolve here.
1290 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001291
Chris Lattnerdf986172009-01-02 07:01:27 +00001292 PATypeHolder Ty(ty);
1293#if 0
David Greene0e28d762009-12-23 23:38:28 +00001294 dbgs() << "Type '" << Ty->getDescription()
Chris Lattnerdf986172009-01-02 07:01:27 +00001295 << "' newly formed. Resolving upreferences.\n"
1296 << UpRefs.size() << " upreferences active!\n";
1297#endif
Daniel Dunbara279bc32009-09-20 02:20:51 +00001298
Chris Lattnerdf986172009-01-02 07:01:27 +00001299 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1300 // to zero), we resolve them all together before we resolve them to Ty. At
1301 // the end of the loop, if there is anything to resolve to Ty, it will be in
1302 // this variable.
1303 OpaqueType *TypeToResolve = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001304
Chris Lattnerdf986172009-01-02 07:01:27 +00001305 for (unsigned i = 0; i != UpRefs.size(); ++i) {
1306 // Determine if 'Ty' directly contains this up-references 'LastContainedTy'.
1307 bool ContainsType =
1308 std::find(Ty->subtype_begin(), Ty->subtype_end(),
1309 UpRefs[i].LastContainedTy) != Ty->subtype_end();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001310
Chris Lattnerdf986172009-01-02 07:01:27 +00001311#if 0
David Greene0e28d762009-12-23 23:38:28 +00001312 dbgs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattnerdf986172009-01-02 07:01:27 +00001313 << UpRefs[i].LastContainedTy->getDescription() << ") = "
1314 << (ContainsType ? "true" : "false")
1315 << " level=" << UpRefs[i].NestingLevel << "\n";
1316#endif
1317 if (!ContainsType)
1318 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001319
Chris Lattnerdf986172009-01-02 07:01:27 +00001320 // Decrement level of upreference
1321 unsigned Level = --UpRefs[i].NestingLevel;
1322 UpRefs[i].LastContainedTy = Ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001323
Chris Lattnerdf986172009-01-02 07:01:27 +00001324 // If the Up-reference has a non-zero level, it shouldn't be resolved yet.
1325 if (Level != 0)
1326 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001327
Chris Lattnerdf986172009-01-02 07:01:27 +00001328#if 0
David Greene0e28d762009-12-23 23:38:28 +00001329 dbgs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n";
Chris Lattnerdf986172009-01-02 07:01:27 +00001330#endif
1331 if (!TypeToResolve)
1332 TypeToResolve = UpRefs[i].UpRefTy;
1333 else
1334 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1335 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list.
1336 --i; // Do not skip the next element.
1337 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001338
Chris Lattnerdf986172009-01-02 07:01:27 +00001339 if (TypeToResolve)
1340 TypeToResolve->refineAbstractTypeTo(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001341
Chris Lattnerdf986172009-01-02 07:01:27 +00001342 return Ty;
1343}
1344
1345
1346/// ParseTypeRec - The recursive function used to process the internal
1347/// implementation details of types.
1348bool LLParser::ParseTypeRec(PATypeHolder &Result) {
1349 switch (Lex.getKind()) {
1350 default:
1351 return TokError("expected type");
1352 case lltok::Type:
1353 // TypeRec ::= 'float' | 'void' (etc)
1354 Result = Lex.getTyVal();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001355 Lex.Lex();
Chris Lattnerdf986172009-01-02 07:01:27 +00001356 break;
1357 case lltok::kw_opaque:
1358 // TypeRec ::= 'opaque'
Owen Anderson0e275dc2009-08-13 23:27:32 +00001359 Result = OpaqueType::get(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001360 Lex.Lex();
1361 break;
1362 case lltok::lbrace:
1363 // TypeRec ::= '{' ... '}'
1364 if (ParseStructType(Result, false))
1365 return true;
1366 break;
1367 case lltok::lsquare:
1368 // TypeRec ::= '[' ... ']'
1369 Lex.Lex(); // eat the lsquare.
1370 if (ParseArrayVectorType(Result, false))
1371 return true;
1372 break;
1373 case lltok::less: // Either vector or packed struct.
1374 // TypeRec ::= '<' ... '>'
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001375 Lex.Lex();
1376 if (Lex.getKind() == lltok::lbrace) {
1377 if (ParseStructType(Result, true) ||
1378 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerdf986172009-01-02 07:01:27 +00001379 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001380 } else if (ParseArrayVectorType(Result, true))
1381 return true;
1382 break;
1383 case lltok::LocalVar:
1384 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
1385 // TypeRec ::= %foo
1386 if (const Type *T = M->getTypeByName(Lex.getStrVal())) {
1387 Result = T;
1388 } else {
Owen Anderson0e275dc2009-08-13 23:27:32 +00001389 Result = OpaqueType::get(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001390 ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(),
1391 std::make_pair(Result,
1392 Lex.getLoc())));
1393 M->addTypeName(Lex.getStrVal(), Result.get());
1394 }
1395 Lex.Lex();
1396 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001397
Chris Lattnerdf986172009-01-02 07:01:27 +00001398 case lltok::LocalVarID:
1399 // TypeRec ::= %4
1400 if (Lex.getUIntVal() < NumberedTypes.size())
1401 Result = NumberedTypes[Lex.getUIntVal()];
1402 else {
1403 std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator
1404 I = ForwardRefTypeIDs.find(Lex.getUIntVal());
1405 if (I != ForwardRefTypeIDs.end())
1406 Result = I->second.first;
1407 else {
Owen Anderson0e275dc2009-08-13 23:27:32 +00001408 Result = OpaqueType::get(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001409 ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(),
1410 std::make_pair(Result,
1411 Lex.getLoc())));
1412 }
1413 }
1414 Lex.Lex();
1415 break;
1416 case lltok::backslash: {
1417 // TypeRec ::= '\' 4
Chris Lattnerdf986172009-01-02 07:01:27 +00001418 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001419 unsigned Val;
1420 if (ParseUInt32(Val)) return true;
Owen Anderson0e275dc2009-08-13 23:27:32 +00001421 OpaqueType *OT = OpaqueType::get(Context); //Use temporary placeholder.
Chris Lattnerdf986172009-01-02 07:01:27 +00001422 UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT));
1423 Result = OT;
1424 break;
1425 }
1426 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001427
1428 // Parse the type suffixes.
Chris Lattnerdf986172009-01-02 07:01:27 +00001429 while (1) {
1430 switch (Lex.getKind()) {
1431 // End of type.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001432 default: return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001433
1434 // TypeRec ::= TypeRec '*'
1435 case lltok::star:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001436 if (Result.get()->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001437 return TokError("basic block pointers are invalid");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001438 if (Result.get()->isVoidTy())
Dan Gohmanb9070d32009-02-09 17:41:21 +00001439 return TokError("pointers to void are invalid; use i8* instead");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001440 if (!PointerType::isValidElementType(Result.get()))
1441 return TokError("pointer to this type is invalid");
Owen Andersondebcb012009-07-29 22:17:13 +00001442 Result = HandleUpRefs(PointerType::getUnqual(Result.get()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001443 Lex.Lex();
1444 break;
1445
1446 // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*'
1447 case lltok::kw_addrspace: {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001448 if (Result.get()->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001449 return TokError("basic block pointers are invalid");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001450 if (Result.get()->isVoidTy())
Dan Gohmanb9070d32009-02-09 17:41:21 +00001451 return TokError("pointers to void are invalid; use i8* instead");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001452 if (!PointerType::isValidElementType(Result.get()))
1453 return TokError("pointer to this type is invalid");
Chris Lattnerdf986172009-01-02 07:01:27 +00001454 unsigned AddrSpace;
1455 if (ParseOptionalAddrSpace(AddrSpace) ||
1456 ParseToken(lltok::star, "expected '*' in address space"))
1457 return true;
1458
Owen Andersondebcb012009-07-29 22:17:13 +00001459 Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace));
Chris Lattnerdf986172009-01-02 07:01:27 +00001460 break;
1461 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001462
Chris Lattnerdf986172009-01-02 07:01:27 +00001463 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1464 case lltok::lparen:
1465 if (ParseFunctionType(Result))
1466 return true;
1467 break;
1468 }
1469 }
1470}
1471
1472/// ParseParameterList
1473/// ::= '(' ')'
1474/// ::= '(' Arg (',' Arg)* ')'
1475/// Arg
1476/// ::= Type OptionalAttributes Value OptionalAttributes
1477bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1478 PerFunctionState &PFS) {
1479 if (ParseToken(lltok::lparen, "expected '(' in call"))
1480 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001481
Chris Lattnerdf986172009-01-02 07:01:27 +00001482 while (Lex.getKind() != lltok::rparen) {
1483 // If this isn't the first argument, we need a comma.
1484 if (!ArgList.empty() &&
1485 ParseToken(lltok::comma, "expected ',' in argument list"))
1486 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001487
Chris Lattnerdf986172009-01-02 07:01:27 +00001488 // Parse the argument.
1489 LocTy ArgLoc;
Owen Anderson1d0be152009-08-13 21:58:54 +00001490 PATypeHolder ArgTy(Type::getVoidTy(Context));
Victor Hernandez19715562009-12-03 23:40:58 +00001491 unsigned ArgAttrs1 = Attribute::None;
1492 unsigned ArgAttrs2 = Attribute::None;
Chris Lattnerdf986172009-01-02 07:01:27 +00001493 Value *V;
Victor Hernandez19715562009-12-03 23:40:58 +00001494 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00001495 return true;
Victor Hernandez19715562009-12-03 23:40:58 +00001496
Chris Lattner287881d2009-12-30 02:11:14 +00001497 // Otherwise, handle normal operands.
1498 if (ParseOptionalAttrs(ArgAttrs1, 0) ||
1499 ParseValue(ArgTy, V, PFS) ||
1500 // FIXME: Should not allow attributes after the argument, remove this
1501 // in LLVM 3.0.
1502 ParseOptionalAttrs(ArgAttrs2, 3))
1503 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001504 ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2));
1505 }
1506
1507 Lex.Lex(); // Lex the ')'.
1508 return false;
1509}
1510
1511
1512
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001513/// ParseArgumentList - Parse the argument list for a function type or function
1514/// prototype. If 'inType' is true then we are parsing a FunctionType.
Chris Lattnerdf986172009-01-02 07:01:27 +00001515/// ::= '(' ArgTypeListI ')'
1516/// ArgTypeListI
1517/// ::= /*empty*/
1518/// ::= '...'
1519/// ::= ArgTypeList ',' '...'
1520/// ::= ArgType (',' ArgType)*
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001521///
Chris Lattnerdf986172009-01-02 07:01:27 +00001522bool LLParser::ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001523 bool &isVarArg, bool inType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001524 isVarArg = false;
1525 assert(Lex.getKind() == lltok::lparen);
1526 Lex.Lex(); // eat the (.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001527
Chris Lattnerdf986172009-01-02 07:01:27 +00001528 if (Lex.getKind() == lltok::rparen) {
1529 // empty
1530 } else if (Lex.getKind() == lltok::dotdotdot) {
1531 isVarArg = true;
1532 Lex.Lex();
1533 } else {
1534 LocTy TypeLoc = Lex.getLoc();
Owen Anderson1d0be152009-08-13 21:58:54 +00001535 PATypeHolder ArgTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00001536 unsigned Attrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00001537 std::string Name;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001538
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001539 // If we're parsing a type, use ParseTypeRec, because we allow recursive
1540 // types (such as a function returning a pointer to itself). If parsing a
1541 // function prototype, we require fully resolved types.
1542 if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001543 ParseOptionalAttrs(Attrs, 0)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001544
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001545 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001546 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001547
Chris Lattnerdf986172009-01-02 07:01:27 +00001548 if (Lex.getKind() == lltok::LocalVar ||
1549 Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1550 Name = Lex.getStrVal();
1551 Lex.Lex();
1552 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001553
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001554 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001555 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001556
Chris Lattnerdf986172009-01-02 07:01:27 +00001557 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001558
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001559 while (EatIfPresent(lltok::comma)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001560 // Handle ... at end of arg list.
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001561 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001562 isVarArg = true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001563 break;
1564 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001565
Chris Lattnerdf986172009-01-02 07:01:27 +00001566 // Otherwise must be an argument type.
1567 TypeLoc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +00001568 if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001569 ParseOptionalAttrs(Attrs, 0)) return true;
1570
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001571 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001572 return Error(TypeLoc, "argument can not have void type");
1573
Chris Lattnerdf986172009-01-02 07:01:27 +00001574 if (Lex.getKind() == lltok::LocalVar ||
1575 Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1576 Name = Lex.getStrVal();
1577 Lex.Lex();
1578 } else {
1579 Name = "";
1580 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001581
Duncan Sands47c51882010-02-16 14:50:09 +00001582 if (!ArgTy->isFirstClassType() && !ArgTy->isOpaqueTy())
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001583 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001584
Chris Lattnerdf986172009-01-02 07:01:27 +00001585 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
1586 }
1587 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001588
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001589 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerdf986172009-01-02 07:01:27 +00001590}
Daniel Dunbara279bc32009-09-20 02:20:51 +00001591
Chris Lattnerdf986172009-01-02 07:01:27 +00001592/// ParseFunctionType
1593/// ::= Type ArgumentList OptionalAttrs
1594bool LLParser::ParseFunctionType(PATypeHolder &Result) {
1595 assert(Lex.getKind() == lltok::lparen);
1596
Chris Lattnerd77d04c2009-01-05 08:04:33 +00001597 if (!FunctionType::isValidReturnType(Result))
1598 return TokError("invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001599
Chris Lattnerdf986172009-01-02 07:01:27 +00001600 std::vector<ArgInfo> ArgList;
1601 bool isVarArg;
1602 unsigned Attrs;
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001603 if (ParseArgumentList(ArgList, isVarArg, true) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00001604 // FIXME: Allow, but ignore attributes on function types!
1605 // FIXME: Remove in LLVM 3.0
1606 ParseOptionalAttrs(Attrs, 2))
1607 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001608
Chris Lattnerdf986172009-01-02 07:01:27 +00001609 // Reject names on the arguments lists.
1610 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1611 if (!ArgList[i].Name.empty())
1612 return Error(ArgList[i].Loc, "argument name invalid in function type");
1613 if (!ArgList[i].Attrs != 0) {
1614 // Allow but ignore attributes on function types; this permits
1615 // auto-upgrade.
1616 // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0
1617 }
1618 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001619
Chris Lattnerdf986172009-01-02 07:01:27 +00001620 std::vector<const Type*> ArgListTy;
1621 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
1622 ArgListTy.push_back(ArgList[i].Type);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001623
Owen Andersondebcb012009-07-29 22:17:13 +00001624 Result = HandleUpRefs(FunctionType::get(Result.get(),
Owen Andersonfba933c2009-07-01 23:57:11 +00001625 ArgListTy, isVarArg));
Chris Lattnerdf986172009-01-02 07:01:27 +00001626 return false;
1627}
1628
1629/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
1630/// TypeRec
1631/// ::= '{' '}'
1632/// ::= '{' TypeRec (',' TypeRec)* '}'
1633/// ::= '<' '{' '}' '>'
1634/// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>'
1635bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {
1636 assert(Lex.getKind() == lltok::lbrace);
1637 Lex.Lex(); // Consume the '{'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001638
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001639 if (EatIfPresent(lltok::rbrace)) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +00001640 Result = StructType::get(Context, Packed);
Chris Lattnerdf986172009-01-02 07:01:27 +00001641 return false;
1642 }
1643
1644 std::vector<PATypeHolder> ParamsList;
Chris Lattnera9a9e072009-03-09 04:49:14 +00001645 LocTy EltTyLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001646 if (ParseTypeRec(Result)) return true;
1647 ParamsList.push_back(Result);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001648
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001649 if (Result->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001650 return Error(EltTyLoc, "struct element can not have void type");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001651 if (!StructType::isValidElementType(Result))
1652 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001653
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001654 while (EatIfPresent(lltok::comma)) {
Chris Lattnera9a9e072009-03-09 04:49:14 +00001655 EltTyLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001656 if (ParseTypeRec(Result)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001657
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001658 if (Result->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001659 return Error(EltTyLoc, "struct element can not have void type");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001660 if (!StructType::isValidElementType(Result))
1661 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001662
Chris Lattnerdf986172009-01-02 07:01:27 +00001663 ParamsList.push_back(Result);
1664 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001665
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001666 if (ParseToken(lltok::rbrace, "expected '}' at end of struct"))
1667 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001668
Chris Lattnerdf986172009-01-02 07:01:27 +00001669 std::vector<const Type*> ParamsListTy;
1670 for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
1671 ParamsListTy.push_back(ParamsList[i].get());
Owen Andersond7f2a6c2009-08-05 23:16:16 +00001672 Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed));
Chris Lattnerdf986172009-01-02 07:01:27 +00001673 return false;
1674}
1675
1676/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1677/// token has already been consumed.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001678/// TypeRec
Chris Lattnerdf986172009-01-02 07:01:27 +00001679/// ::= '[' APSINTVAL 'x' Types ']'
1680/// ::= '<' APSINTVAL 'x' Types '>'
1681bool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) {
1682 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1683 Lex.getAPSIntVal().getBitWidth() > 64)
1684 return TokError("expected number in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001685
Chris Lattnerdf986172009-01-02 07:01:27 +00001686 LocTy SizeLoc = Lex.getLoc();
1687 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001688 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001689
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001690 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1691 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001692
1693 LocTy TypeLoc = Lex.getLoc();
Owen Anderson1d0be152009-08-13 21:58:54 +00001694 PATypeHolder EltTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00001695 if (ParseTypeRec(EltTy)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001696
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001697 if (EltTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001698 return Error(TypeLoc, "array and vector element type cannot be void");
1699
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001700 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
1701 "expected end of sequential type"))
1702 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001703
Chris Lattnerdf986172009-01-02 07:01:27 +00001704 if (isVector) {
Chris Lattner452e2622009-02-28 18:12:41 +00001705 if (Size == 0)
1706 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerdf986172009-01-02 07:01:27 +00001707 if ((unsigned)Size != Size)
1708 return Error(SizeLoc, "size too large for vector");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001709 if (!VectorType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001710 return Error(TypeLoc, "vector element type must be fp or integer");
Owen Andersondebcb012009-07-29 22:17:13 +00001711 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerdf986172009-01-02 07:01:27 +00001712 } else {
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001713 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001714 return Error(TypeLoc, "invalid array element type");
Owen Andersondebcb012009-07-29 22:17:13 +00001715 Result = HandleUpRefs(ArrayType::get(EltTy, Size));
Chris Lattnerdf986172009-01-02 07:01:27 +00001716 }
1717 return false;
1718}
1719
1720//===----------------------------------------------------------------------===//
1721// Function Semantic Analysis.
1722//===----------------------------------------------------------------------===//
1723
Chris Lattner09d9ef42009-10-28 03:39:23 +00001724LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
1725 int functionNumber)
1726 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001727
1728 // Insert unnamed arguments into the NumberedVals list.
1729 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1730 AI != E; ++AI)
1731 if (!AI->hasName())
1732 NumberedVals.push_back(AI);
1733}
1734
1735LLParser::PerFunctionState::~PerFunctionState() {
1736 // If there were any forward referenced non-basicblock values, delete them.
1737 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1738 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1739 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001740 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001741 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001742 delete I->second.first;
1743 I->second.first = 0;
1744 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001745
Chris Lattnerdf986172009-01-02 07:01:27 +00001746 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1747 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1748 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001749 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001750 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001751 delete I->second.first;
1752 I->second.first = 0;
1753 }
1754}
1755
Chris Lattner09d9ef42009-10-28 03:39:23 +00001756bool LLParser::PerFunctionState::FinishFunction() {
1757 // Check to see if someone took the address of labels in this block.
1758 if (!P.ForwardRefBlockAddresses.empty()) {
1759 ValID FunctionID;
1760 if (!F.getName().empty()) {
1761 FunctionID.Kind = ValID::t_GlobalName;
1762 FunctionID.StrVal = F.getName();
1763 } else {
1764 FunctionID.Kind = ValID::t_GlobalID;
1765 FunctionID.UIntVal = FunctionNumber;
1766 }
1767
1768 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
1769 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
1770 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
1771 // Resolve all these references.
1772 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
1773 return true;
1774
1775 P.ForwardRefBlockAddresses.erase(FRBAI);
1776 }
1777 }
1778
Chris Lattnerdf986172009-01-02 07:01:27 +00001779 if (!ForwardRefVals.empty())
1780 return P.Error(ForwardRefVals.begin()->second.second,
1781 "use of undefined value '%" + ForwardRefVals.begin()->first +
1782 "'");
1783 if (!ForwardRefValIDs.empty())
1784 return P.Error(ForwardRefValIDs.begin()->second.second,
1785 "use of undefined value '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001786 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001787 return false;
1788}
1789
1790
1791/// GetVal - Get a value with the specified name or ID, creating a
1792/// forward reference record if needed. This can return null if the value
1793/// exists but does not have the right type.
1794Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
1795 const Type *Ty, LocTy Loc) {
1796 // Look this name up in the normal function symbol table.
1797 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001798
Chris Lattnerdf986172009-01-02 07:01:27 +00001799 // If this is a forward reference for the value, see if we already created a
1800 // forward ref record.
1801 if (Val == 0) {
1802 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1803 I = ForwardRefVals.find(Name);
1804 if (I != ForwardRefVals.end())
1805 Val = I->second.first;
1806 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001807
Chris Lattnerdf986172009-01-02 07:01:27 +00001808 // If we have the value in the symbol table or fwd-ref table, return it.
1809 if (Val) {
1810 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001811 if (Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001812 P.Error(Loc, "'%" + Name + "' is not a basic block");
1813 else
1814 P.Error(Loc, "'%" + Name + "' defined with type '" +
1815 Val->getType()->getDescription() + "'");
1816 return 0;
1817 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001818
Chris Lattnerdf986172009-01-02 07:01:27 +00001819 // Don't make placeholders with invalid type.
Duncan Sands47c51882010-02-16 14:50:09 +00001820 if (!Ty->isFirstClassType() && !Ty->isOpaqueTy() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001821 P.Error(Loc, "invalid use of a non-first-class type");
1822 return 0;
1823 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001824
Chris Lattnerdf986172009-01-02 07:01:27 +00001825 // Otherwise, create a new forward reference for this value and remember it.
1826 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001827 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001828 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001829 else
1830 FwdVal = new Argument(Ty, Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001831
Chris Lattnerdf986172009-01-02 07:01:27 +00001832 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1833 return FwdVal;
1834}
1835
1836Value *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty,
1837 LocTy Loc) {
1838 // Look this name up in the normal function symbol table.
1839 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001840
Chris Lattnerdf986172009-01-02 07:01:27 +00001841 // If this is a forward reference for the value, see if we already created a
1842 // forward ref record.
1843 if (Val == 0) {
1844 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1845 I = ForwardRefValIDs.find(ID);
1846 if (I != ForwardRefValIDs.end())
1847 Val = I->second.first;
1848 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001849
Chris Lattnerdf986172009-01-02 07:01:27 +00001850 // If we have the value in the symbol table or fwd-ref table, return it.
1851 if (Val) {
1852 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001853 if (Ty->isLabelTy())
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001854 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerdf986172009-01-02 07:01:27 +00001855 else
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001856 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001857 Val->getType()->getDescription() + "'");
1858 return 0;
1859 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001860
Duncan Sands47c51882010-02-16 14:50:09 +00001861 if (!Ty->isFirstClassType() && !Ty->isOpaqueTy() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001862 P.Error(Loc, "invalid use of a non-first-class type");
1863 return 0;
1864 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001865
Chris Lattnerdf986172009-01-02 07:01:27 +00001866 // Otherwise, create a new forward reference for this value and remember it.
1867 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001868 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001869 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001870 else
1871 FwdVal = new Argument(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001872
Chris Lattnerdf986172009-01-02 07:01:27 +00001873 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1874 return FwdVal;
1875}
1876
1877/// SetInstName - After an instruction is parsed and inserted into its
1878/// basic block, this installs its name.
1879bool LLParser::PerFunctionState::SetInstName(int NameID,
1880 const std::string &NameStr,
1881 LocTy NameLoc, Instruction *Inst) {
1882 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001883 if (Inst->getType()->isVoidTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001884 if (NameID != -1 || !NameStr.empty())
1885 return P.Error(NameLoc, "instructions returning void cannot have a name");
1886 return false;
1887 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001888
Chris Lattnerdf986172009-01-02 07:01:27 +00001889 // If this was a numbered instruction, verify that the instruction is the
1890 // expected value and resolve any forward references.
1891 if (NameStr.empty()) {
1892 // If neither a name nor an ID was specified, just use the next ID.
1893 if (NameID == -1)
1894 NameID = NumberedVals.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001895
Chris Lattnerdf986172009-01-02 07:01:27 +00001896 if (unsigned(NameID) != NumberedVals.size())
1897 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001898 Twine(NumberedVals.size()) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001899
Chris Lattnerdf986172009-01-02 07:01:27 +00001900 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
1901 ForwardRefValIDs.find(NameID);
1902 if (FI != ForwardRefValIDs.end()) {
1903 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00001904 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001905 FI->second.first->getType()->getDescription() + "'");
1906 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2b4f28e2009-09-02 15:02:57 +00001907 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001908 ForwardRefValIDs.erase(FI);
1909 }
1910
1911 NumberedVals.push_back(Inst);
1912 return false;
1913 }
1914
1915 // Otherwise, the instruction had a name. Resolve forward refs and set it.
1916 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1917 FI = ForwardRefVals.find(NameStr);
1918 if (FI != ForwardRefVals.end()) {
1919 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00001920 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001921 FI->second.first->getType()->getDescription() + "'");
1922 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes531552a2009-09-02 14:22:03 +00001923 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001924 ForwardRefVals.erase(FI);
1925 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001926
Chris Lattnerdf986172009-01-02 07:01:27 +00001927 // Set the name on the instruction.
1928 Inst->setName(NameStr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001929
Benjamin Krameraf812352010-10-16 11:28:23 +00001930 if (Inst->getName() != NameStr)
Daniel Dunbara279bc32009-09-20 02:20:51 +00001931 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001932 NameStr + "'");
1933 return false;
1934}
1935
1936/// GetBB - Get a basic block with the specified name or ID, creating a
1937/// forward reference record if needed.
1938BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
1939 LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001940 return cast_or_null<BasicBlock>(GetVal(Name,
1941 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00001942}
1943
1944BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001945 return cast_or_null<BasicBlock>(GetVal(ID,
1946 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00001947}
1948
1949/// DefineBB - Define the specified basic block, which is either named or
1950/// unnamed. If there is an error, this returns null otherwise it returns
1951/// the block being defined.
1952BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
1953 LocTy Loc) {
1954 BasicBlock *BB;
1955 if (Name.empty())
1956 BB = GetBB(NumberedVals.size(), Loc);
1957 else
1958 BB = GetBB(Name, Loc);
1959 if (BB == 0) return 0; // Already diagnosed error.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001960
Chris Lattnerdf986172009-01-02 07:01:27 +00001961 // Move the block to the end of the function. Forward ref'd blocks are
1962 // inserted wherever they happen to be referenced.
1963 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001964
Chris Lattnerdf986172009-01-02 07:01:27 +00001965 // Remove the block from forward ref sets.
1966 if (Name.empty()) {
1967 ForwardRefValIDs.erase(NumberedVals.size());
1968 NumberedVals.push_back(BB);
1969 } else {
1970 // BB forward references are already in the function symbol table.
1971 ForwardRefVals.erase(Name);
1972 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001973
Chris Lattnerdf986172009-01-02 07:01:27 +00001974 return BB;
1975}
1976
1977//===----------------------------------------------------------------------===//
1978// Constants.
1979//===----------------------------------------------------------------------===//
1980
1981/// ParseValID - Parse an abstract value that doesn't necessarily have a
1982/// type implied. For example, if we parse "4" we don't know what integer type
1983/// it has. The value will later be combined with its type and checked for
Victor Hernandez24e64df2010-01-10 07:14:18 +00001984/// sanity. PFS is used to convert function-local operands of metadata (since
1985/// metadata operands are not just parsed here but also converted to values).
1986/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezbf170d42010-01-05 22:22:14 +00001987bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001988 ID.Loc = Lex.getLoc();
1989 switch (Lex.getKind()) {
1990 default: return TokError("expected value token");
1991 case lltok::GlobalID: // @42
1992 ID.UIntVal = Lex.getUIntVal();
1993 ID.Kind = ValID::t_GlobalID;
1994 break;
1995 case lltok::GlobalVar: // @foo
1996 ID.StrVal = Lex.getStrVal();
1997 ID.Kind = ValID::t_GlobalName;
1998 break;
1999 case lltok::LocalVarID: // %42
2000 ID.UIntVal = Lex.getUIntVal();
2001 ID.Kind = ValID::t_LocalID;
2002 break;
2003 case lltok::LocalVar: // %foo
2004 case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0
2005 ID.StrVal = Lex.getStrVal();
2006 ID.Kind = ValID::t_LocalName;
2007 break;
Dan Gohman83448032010-07-14 18:26:50 +00002008 case lltok::exclaim: // !42, !{...}, or !"foo"
2009 return ParseMetadataValue(ID, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002010 case lltok::APSInt:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002011 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002012 ID.Kind = ValID::t_APSInt;
2013 break;
2014 case lltok::APFloat:
2015 ID.APFloatVal = Lex.getAPFloatVal();
2016 ID.Kind = ValID::t_APFloat;
2017 break;
2018 case lltok::kw_true:
Owen Anderson5defacc2009-07-31 17:39:07 +00002019 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002020 ID.Kind = ValID::t_Constant;
2021 break;
2022 case lltok::kw_false:
Owen Anderson5defacc2009-07-31 17:39:07 +00002023 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002024 ID.Kind = ValID::t_Constant;
2025 break;
2026 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2027 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2028 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002029
Chris Lattnerdf986172009-01-02 07:01:27 +00002030 case lltok::lbrace: {
2031 // ValID ::= '{' ConstVector '}'
2032 Lex.Lex();
2033 SmallVector<Constant*, 16> Elts;
2034 if (ParseGlobalValueVector(Elts) ||
2035 ParseToken(lltok::rbrace, "expected end of struct constant"))
2036 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002037
Owen Andersond7f2a6c2009-08-05 23:16:16 +00002038 ID.ConstantVal = ConstantStruct::get(Context, Elts.data(),
2039 Elts.size(), false);
Chris Lattnerdf986172009-01-02 07:01:27 +00002040 ID.Kind = ValID::t_Constant;
2041 return false;
2042 }
2043 case lltok::less: {
2044 // ValID ::= '<' ConstVector '>' --> Vector.
2045 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2046 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002047 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002048
Chris Lattnerdf986172009-01-02 07:01:27 +00002049 SmallVector<Constant*, 16> Elts;
2050 LocTy FirstEltLoc = Lex.getLoc();
2051 if (ParseGlobalValueVector(Elts) ||
2052 (isPackedStruct &&
2053 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2054 ParseToken(lltok::greater, "expected end of constant"))
2055 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002056
Chris Lattnerdf986172009-01-02 07:01:27 +00002057 if (isPackedStruct) {
Owen Andersonfba933c2009-07-01 23:57:11 +00002058 ID.ConstantVal =
Owen Andersond7f2a6c2009-08-05 23:16:16 +00002059 ConstantStruct::get(Context, Elts.data(), Elts.size(), true);
Chris Lattnerdf986172009-01-02 07:01:27 +00002060 ID.Kind = ValID::t_Constant;
2061 return false;
2062 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002063
Chris Lattnerdf986172009-01-02 07:01:27 +00002064 if (Elts.empty())
2065 return Error(ID.Loc, "constant vector must not be empty");
2066
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002067 if (!Elts[0]->getType()->isIntegerTy() &&
2068 !Elts[0]->getType()->isFloatingPointTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002069 return Error(FirstEltLoc,
2070 "vector elements must have integer or floating point type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002071
Chris Lattnerdf986172009-01-02 07:01:27 +00002072 // Verify that all the vector elements have the same type.
2073 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2074 if (Elts[i]->getType() != Elts[0]->getType())
2075 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002076 "vector element #" + Twine(i) +
Chris Lattnerdf986172009-01-02 07:01:27 +00002077 " is not of type '" + Elts[0]->getType()->getDescription());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002078
Chris Lattner2ca5c862011-02-15 00:14:00 +00002079 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00002080 ID.Kind = ValID::t_Constant;
2081 return false;
2082 }
2083 case lltok::lsquare: { // Array Constant
2084 Lex.Lex();
2085 SmallVector<Constant*, 16> Elts;
2086 LocTy FirstEltLoc = Lex.getLoc();
2087 if (ParseGlobalValueVector(Elts) ||
2088 ParseToken(lltok::rsquare, "expected end of array constant"))
2089 return true;
2090
2091 // Handle empty element.
2092 if (Elts.empty()) {
2093 // Use undef instead of an array because it's inconvenient to determine
2094 // the element type at this point, there being no elements to examine.
Chris Lattner081b5052009-01-05 07:52:51 +00002095 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerdf986172009-01-02 07:01:27 +00002096 return false;
2097 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002098
Chris Lattnerdf986172009-01-02 07:01:27 +00002099 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002100 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattnerdf986172009-01-02 07:01:27 +00002101 Elts[0]->getType()->getDescription());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002102
Owen Andersondebcb012009-07-29 22:17:13 +00002103 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002104
Chris Lattnerdf986172009-01-02 07:01:27 +00002105 // Verify all elements are correct type!
Chris Lattner6d6b3cc2009-01-02 08:49:06 +00002106 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002107 if (Elts[i]->getType() != Elts[0]->getType())
2108 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002109 "array element #" + Twine(i) +
Chris Lattnerdf986172009-01-02 07:01:27 +00002110 " is not of type '" +Elts[0]->getType()->getDescription());
2111 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002112
Owen Anderson1fd70962009-07-28 18:32:17 +00002113 ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00002114 ID.Kind = ValID::t_Constant;
2115 return false;
2116 }
2117 case lltok::kw_c: // c "foo"
2118 Lex.Lex();
Owen Anderson1d0be152009-08-13 21:58:54 +00002119 ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false);
Chris Lattnerdf986172009-01-02 07:01:27 +00002120 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2121 ID.Kind = ValID::t_Constant;
2122 return false;
2123
2124 case lltok::kw_asm: {
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002125 // ValID ::= 'asm' SideEffect? AlignStack? STRINGCONSTANT ',' STRINGCONSTANT
2126 bool HasSideEffect, AlignStack;
Chris Lattnerdf986172009-01-02 07:01:27 +00002127 Lex.Lex();
2128 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002129 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002130 ParseStringConstant(ID.StrVal) ||
2131 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002132 ParseToken(lltok::StringConstant, "expected constraint string"))
2133 return true;
2134 ID.StrVal2 = Lex.getStrVal();
Daniel Dunbarf0bb41c2009-11-07 23:51:55 +00002135 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002136 ID.Kind = ValID::t_InlineAsm;
2137 return false;
2138 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002139
Chris Lattner09d9ef42009-10-28 03:39:23 +00002140 case lltok::kw_blockaddress: {
2141 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2142 Lex.Lex();
2143
2144 ValID Fn, Label;
2145 LocTy FnLoc, LabelLoc;
2146
2147 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2148 ParseValID(Fn) ||
2149 ParseToken(lltok::comma, "expected comma in block address expression")||
2150 ParseValID(Label) ||
2151 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2152 return true;
Chris Lattnercdfc9402009-11-01 01:27:45 +00002153
Chris Lattner09d9ef42009-10-28 03:39:23 +00002154 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2155 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattnercdfc9402009-11-01 01:27:45 +00002156 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner09d9ef42009-10-28 03:39:23 +00002157 return Error(Label.Loc, "expected basic block name in blockaddress");
2158
2159 // Make a global variable as a placeholder for this reference.
2160 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2161 false, GlobalValue::InternalLinkage,
2162 0, "");
2163 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2164 ID.ConstantVal = FwdRef;
2165 ID.Kind = ValID::t_Constant;
2166 return false;
2167 }
2168
Chris Lattnerdf986172009-01-02 07:01:27 +00002169 case lltok::kw_trunc:
2170 case lltok::kw_zext:
2171 case lltok::kw_sext:
2172 case lltok::kw_fptrunc:
2173 case lltok::kw_fpext:
2174 case lltok::kw_bitcast:
2175 case lltok::kw_uitofp:
2176 case lltok::kw_sitofp:
2177 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002178 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00002179 case lltok::kw_inttoptr:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002180 case lltok::kw_ptrtoint: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002181 unsigned Opc = Lex.getUIntVal();
Owen Anderson1d0be152009-08-13 21:58:54 +00002182 PATypeHolder DestTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00002183 Constant *SrcVal;
2184 Lex.Lex();
2185 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2186 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman24b108b2009-06-15 21:52:11 +00002187 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002188 ParseType(DestTy) ||
2189 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2190 return true;
2191 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2192 return Error(ID.Loc, "invalid cast opcode for cast from '" +
2193 SrcVal->getType()->getDescription() + "' to '" +
2194 DestTy->getDescription() + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002195 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Andersonfba933c2009-07-01 23:57:11 +00002196 SrcVal, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00002197 ID.Kind = ValID::t_Constant;
2198 return false;
2199 }
2200 case lltok::kw_extractvalue: {
2201 Lex.Lex();
2202 Constant *Val;
2203 SmallVector<unsigned, 4> Indices;
2204 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2205 ParseGlobalTypeAndValue(Val) ||
2206 ParseIndexList(Indices) ||
2207 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2208 return true;
Devang Patele8bc45a2009-11-03 19:06:07 +00002209
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002210 if (!Val->getType()->isAggregateType())
2211 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00002212 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
2213 Indices.end()))
2214 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foade3e51c02009-05-21 09:52:38 +00002215 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002216 ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00002217 ID.Kind = ValID::t_Constant;
2218 return false;
2219 }
2220 case lltok::kw_insertvalue: {
2221 Lex.Lex();
2222 Constant *Val0, *Val1;
2223 SmallVector<unsigned, 4> Indices;
2224 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2225 ParseGlobalTypeAndValue(Val0) ||
2226 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2227 ParseGlobalTypeAndValue(Val1) ||
2228 ParseIndexList(Indices) ||
2229 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2230 return true;
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002231 if (!Val0->getType()->isAggregateType())
2232 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00002233 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
2234 Indices.end()))
2235 return Error(ID.Loc, "invalid indices for insertvalue");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002236 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1,
Owen Andersonfba933c2009-07-01 23:57:11 +00002237 Indices.data(), Indices.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00002238 ID.Kind = ValID::t_Constant;
2239 return false;
2240 }
2241 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002242 case lltok::kw_fcmp: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002243 unsigned PredVal, Opc = Lex.getUIntVal();
2244 Constant *Val0, *Val1;
2245 Lex.Lex();
2246 if (ParseCmpPredicate(PredVal, Opc) ||
2247 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2248 ParseGlobalTypeAndValue(Val0) ||
2249 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2250 ParseGlobalTypeAndValue(Val1) ||
2251 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2252 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002253
Chris Lattnerdf986172009-01-02 07:01:27 +00002254 if (Val0->getType() != Val1->getType())
2255 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002256
Chris Lattnerdf986172009-01-02 07:01:27 +00002257 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002258
Chris Lattnerdf986172009-01-02 07:01:27 +00002259 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002260 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002261 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002262 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002263 } else {
2264 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002265 if (!Val0->getType()->isIntOrIntVectorTy() &&
Duncan Sands1df98592010-02-16 11:11:14 +00002266 !Val0->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002267 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002268 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002269 }
2270 ID.Kind = ValID::t_Constant;
2271 return false;
2272 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002273
Chris Lattnerdf986172009-01-02 07:01:27 +00002274 // Binary Operators.
2275 case lltok::kw_add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002276 case lltok::kw_fadd:
Chris Lattnerdf986172009-01-02 07:01:27 +00002277 case lltok::kw_sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002278 case lltok::kw_fsub:
Chris Lattnerdf986172009-01-02 07:01:27 +00002279 case lltok::kw_mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002280 case lltok::kw_fmul:
Chris Lattnerdf986172009-01-02 07:01:27 +00002281 case lltok::kw_udiv:
2282 case lltok::kw_sdiv:
2283 case lltok::kw_fdiv:
2284 case lltok::kw_urem:
2285 case lltok::kw_srem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002286 case lltok::kw_frem:
2287 case lltok::kw_shl:
2288 case lltok::kw_lshr:
2289 case lltok::kw_ashr: {
Dan Gohman59858cf2009-07-27 16:11:46 +00002290 bool NUW = false;
2291 bool NSW = false;
2292 bool Exact = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002293 unsigned Opc = Lex.getUIntVal();
2294 Constant *Val0, *Val1;
2295 Lex.Lex();
Dan Gohman59858cf2009-07-27 16:11:46 +00002296 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnerf067d582011-02-07 16:40:21 +00002297 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2298 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002299 if (EatIfPresent(lltok::kw_nuw))
2300 NUW = true;
2301 if (EatIfPresent(lltok::kw_nsw)) {
2302 NSW = true;
2303 if (EatIfPresent(lltok::kw_nuw))
2304 NUW = true;
2305 }
Chris Lattnerf067d582011-02-07 16:40:21 +00002306 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2307 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002308 if (EatIfPresent(lltok::kw_exact))
2309 Exact = true;
2310 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002311 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2312 ParseGlobalTypeAndValue(Val0) ||
2313 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2314 ParseGlobalTypeAndValue(Val1) ||
2315 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2316 return true;
2317 if (Val0->getType() != Val1->getType())
2318 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002319 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002320 if (NUW)
2321 return Error(ModifierLoc, "nuw only applies to integer operations");
2322 if (NSW)
2323 return Error(ModifierLoc, "nsw only applies to integer operations");
2324 }
Dan Gohman1eaac532010-05-03 22:44:19 +00002325 // Check that the type is valid for the operator.
2326 switch (Opc) {
2327 case Instruction::Add:
2328 case Instruction::Sub:
2329 case Instruction::Mul:
2330 case Instruction::UDiv:
2331 case Instruction::SDiv:
2332 case Instruction::URem:
2333 case Instruction::SRem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002334 case Instruction::Shl:
2335 case Instruction::AShr:
2336 case Instruction::LShr:
Dan Gohman1eaac532010-05-03 22:44:19 +00002337 if (!Val0->getType()->isIntOrIntVectorTy())
2338 return Error(ID.Loc, "constexpr requires integer operands");
2339 break;
2340 case Instruction::FAdd:
2341 case Instruction::FSub:
2342 case Instruction::FMul:
2343 case Instruction::FDiv:
2344 case Instruction::FRem:
2345 if (!Val0->getType()->isFPOrFPVectorTy())
2346 return Error(ID.Loc, "constexpr requires fp operands");
2347 break;
2348 default: llvm_unreachable("Unknown binary operator!");
2349 }
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002350 unsigned Flags = 0;
2351 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2352 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00002353 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002354 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman59858cf2009-07-27 16:11:46 +00002355 ID.ConstantVal = C;
Chris Lattnerdf986172009-01-02 07:01:27 +00002356 ID.Kind = ValID::t_Constant;
2357 return false;
2358 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002359
Chris Lattnerdf986172009-01-02 07:01:27 +00002360 // Logical Operations
Chris Lattnerdf986172009-01-02 07:01:27 +00002361 case lltok::kw_and:
2362 case lltok::kw_or:
2363 case lltok::kw_xor: {
2364 unsigned Opc = Lex.getUIntVal();
2365 Constant *Val0, *Val1;
2366 Lex.Lex();
2367 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2368 ParseGlobalTypeAndValue(Val0) ||
2369 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2370 ParseGlobalTypeAndValue(Val1) ||
2371 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2372 return true;
2373 if (Val0->getType() != Val1->getType())
2374 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002375 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002376 return Error(ID.Loc,
2377 "constexpr requires integer or integer vector operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002378 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002379 ID.Kind = ValID::t_Constant;
2380 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002381 }
2382
Chris Lattnerdf986172009-01-02 07:01:27 +00002383 case lltok::kw_getelementptr:
2384 case lltok::kw_shufflevector:
2385 case lltok::kw_insertelement:
2386 case lltok::kw_extractelement:
2387 case lltok::kw_select: {
2388 unsigned Opc = Lex.getUIntVal();
2389 SmallVector<Constant*, 16> Elts;
Dan Gohmandd8004d2009-07-27 21:53:46 +00002390 bool InBounds = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002391 Lex.Lex();
Dan Gohmandd8004d2009-07-27 21:53:46 +00002392 if (Opc == Instruction::GetElementPtr)
Dan Gohmandcb40a32009-07-29 15:58:36 +00002393 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002394 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2395 ParseGlobalValueVector(Elts) ||
2396 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2397 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002398
Chris Lattnerdf986172009-01-02 07:01:27 +00002399 if (Opc == Instruction::GetElementPtr) {
Duncan Sands1df98592010-02-16 11:11:14 +00002400 if (Elts.size() == 0 || !Elts[0]->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002401 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002402
Chris Lattnerdf986172009-01-02 07:01:27 +00002403 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(),
Eli Friedman4e9bac32009-07-24 21:56:17 +00002404 (Value**)(Elts.data() + 1),
2405 Elts.size() - 1))
Chris Lattnerdf986172009-01-02 07:01:27 +00002406 return Error(ID.Loc, "invalid indices for getelementptr");
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002407 ID.ConstantVal = InBounds ?
2408 ConstantExpr::getInBoundsGetElementPtr(Elts[0],
2409 Elts.data() + 1,
2410 Elts.size() - 1) :
2411 ConstantExpr::getGetElementPtr(Elts[0],
2412 Elts.data() + 1, Elts.size() - 1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002413 } else if (Opc == Instruction::Select) {
2414 if (Elts.size() != 3)
2415 return Error(ID.Loc, "expected three operands to select");
2416 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2417 Elts[2]))
2418 return Error(ID.Loc, Reason);
Owen Andersonbaf3c402009-07-29 18:55:55 +00002419 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002420 } else if (Opc == Instruction::ShuffleVector) {
2421 if (Elts.size() != 3)
2422 return Error(ID.Loc, "expected three operands to shufflevector");
2423 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2424 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Andersonfba933c2009-07-01 23:57:11 +00002425 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002426 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002427 } else if (Opc == Instruction::ExtractElement) {
2428 if (Elts.size() != 2)
2429 return Error(ID.Loc, "expected two operands to extractelement");
2430 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2431 return Error(ID.Loc, "invalid extractelement operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002432 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002433 } else {
2434 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2435 if (Elts.size() != 3)
2436 return Error(ID.Loc, "expected three operands to insertelement");
2437 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2438 return Error(ID.Loc, "invalid insertelement operands");
Owen Andersonfba933c2009-07-01 23:57:11 +00002439 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002440 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002441 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002442
Chris Lattnerdf986172009-01-02 07:01:27 +00002443 ID.Kind = ValID::t_Constant;
2444 return false;
2445 }
2446 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002447
Chris Lattnerdf986172009-01-02 07:01:27 +00002448 Lex.Lex();
2449 return false;
2450}
2451
2452/// ParseGlobalValue - Parse a global value with the specified type.
Victor Hernandez92f238d2010-01-11 22:31:58 +00002453bool LLParser::ParseGlobalValue(const Type *Ty, Constant *&C) {
2454 C = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002455 ValID ID;
Victor Hernandez92f238d2010-01-11 22:31:58 +00002456 Value *V = NULL;
2457 bool Parsed = ParseValID(ID) ||
2458 ConvertValIDToValue(Ty, ID, V, NULL);
2459 if (V && !(C = dyn_cast<Constant>(V)))
2460 return Error(ID.Loc, "global values must be constants");
2461 return Parsed;
Chris Lattnerdf986172009-01-02 07:01:27 +00002462}
2463
Victor Hernandez92f238d2010-01-11 22:31:58 +00002464bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
2465 PATypeHolder Type(Type::getVoidTy(Context));
2466 return ParseType(Type) ||
2467 ParseGlobalValue(Type, V);
2468}
2469
2470/// ParseGlobalValueVector
2471/// ::= /*empty*/
2472/// ::= TypeAndValue (',' TypeAndValue)*
2473bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2474 // Empty list.
2475 if (Lex.getKind() == lltok::rbrace ||
2476 Lex.getKind() == lltok::rsquare ||
2477 Lex.getKind() == lltok::greater ||
2478 Lex.getKind() == lltok::rparen)
2479 return false;
2480
2481 Constant *C;
2482 if (ParseGlobalTypeAndValue(C)) return true;
2483 Elts.push_back(C);
2484
2485 while (EatIfPresent(lltok::comma)) {
2486 if (ParseGlobalTypeAndValue(C)) return true;
2487 Elts.push_back(C);
2488 }
2489
2490 return false;
2491}
2492
Dan Gohman309b3af2010-08-24 02:24:03 +00002493bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2494 assert(Lex.getKind() == lltok::lbrace);
2495 Lex.Lex();
2496
2497 SmallVector<Value*, 16> Elts;
2498 if (ParseMDNodeVector(Elts, PFS) ||
2499 ParseToken(lltok::rbrace, "expected end of metadata node"))
2500 return true;
2501
Jay Foadec9186b2011-04-21 19:59:31 +00002502 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohman309b3af2010-08-24 02:24:03 +00002503 ID.Kind = ValID::t_MDNode;
2504 return false;
2505}
2506
Dan Gohman83448032010-07-14 18:26:50 +00002507/// ParseMetadataValue
2508/// ::= !42
2509/// ::= !{...}
2510/// ::= !"string"
2511bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2512 assert(Lex.getKind() == lltok::exclaim);
2513 Lex.Lex();
2514
2515 // MDNode:
2516 // !{ ... }
Dan Gohman309b3af2010-08-24 02:24:03 +00002517 if (Lex.getKind() == lltok::lbrace)
2518 return ParseMetadataListValue(ID, PFS);
Dan Gohman83448032010-07-14 18:26:50 +00002519
2520 // Standalone metadata reference
2521 // !42
2522 if (Lex.getKind() == lltok::APSInt) {
2523 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2524 ID.Kind = ValID::t_MDNode;
2525 return false;
2526 }
2527
2528 // MDString:
2529 // ::= '!' STRINGCONSTANT
2530 if (ParseMDString(ID.MDStringVal)) return true;
2531 ID.Kind = ValID::t_MDString;
2532 return false;
2533}
2534
Victor Hernandez92f238d2010-01-11 22:31:58 +00002535
2536//===----------------------------------------------------------------------===//
2537// Function Parsing.
2538//===----------------------------------------------------------------------===//
2539
2540bool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
2541 PerFunctionState *PFS) {
Duncan Sands1df98592010-02-16 11:11:14 +00002542 if (Ty->isFunctionTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002543 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002544
Chris Lattnerdf986172009-01-02 07:01:27 +00002545 switch (ID.Kind) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002546 default: llvm_unreachable("Unknown ValID!");
Chris Lattnerdf986172009-01-02 07:01:27 +00002547 case ValID::t_LocalID:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002548 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2549 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
2550 return (V == 0);
Chris Lattnerdf986172009-01-02 07:01:27 +00002551 case ValID::t_LocalName:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002552 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2553 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
2554 return (V == 0);
2555 case ValID::t_InlineAsm: {
2556 const PointerType *PTy = dyn_cast<PointerType>(Ty);
2557 const FunctionType *FTy =
2558 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2559 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2560 return Error(ID.Loc, "invalid type for inline asm constraint string");
2561 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
2562 return false;
2563 }
2564 case ValID::t_MDNode:
2565 if (!Ty->isMetadataTy())
2566 return Error(ID.Loc, "metadata value must have metadata type");
2567 V = ID.MDNodeVal;
2568 return false;
2569 case ValID::t_MDString:
2570 if (!Ty->isMetadataTy())
2571 return Error(ID.Loc, "metadata value must have metadata type");
2572 V = ID.MDStringVal;
2573 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002574 case ValID::t_GlobalName:
2575 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2576 return V == 0;
2577 case ValID::t_GlobalID:
2578 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2579 return V == 0;
2580 case ValID::t_APSInt:
Duncan Sands1df98592010-02-16 11:11:14 +00002581 if (!Ty->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002582 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad40f8f622010-12-07 08:25:19 +00002583 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersoneed707b2009-07-24 23:12:02 +00002584 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002585 return false;
2586 case ValID::t_APFloat:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002587 if (!Ty->isFloatingPointTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002588 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2589 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002590
Chris Lattnerdf986172009-01-02 07:01:27 +00002591 // The lexer has no type info, so builds all float and double FP constants
2592 // as double. Fix this here. Long double does not need this.
2593 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002594 Ty->isFloatTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002595 bool Ignored;
2596 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2597 &Ignored);
2598 }
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002599 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002600
Chris Lattner959873d2009-01-05 18:24:23 +00002601 if (V->getType() != Ty)
2602 return Error(ID.Loc, "floating point constant does not have type '" +
2603 Ty->getDescription() + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002604
Chris Lattnerdf986172009-01-02 07:01:27 +00002605 return false;
2606 case ValID::t_Null:
Duncan Sands1df98592010-02-16 11:11:14 +00002607 if (!Ty->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002608 return Error(ID.Loc, "null must be a pointer type");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002609 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerdf986172009-01-02 07:01:27 +00002610 return false;
2611 case ValID::t_Undef:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002612 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002613 if ((!Ty->isFirstClassType() || Ty->isLabelTy()) &&
Duncan Sands47c51882010-02-16 14:50:09 +00002614 !Ty->isOpaqueTy())
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002615 return Error(ID.Loc, "invalid type for undef constant");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002616 V = UndefValue::get(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002617 return false;
Chris Lattner081b5052009-01-05 07:52:51 +00002618 case ValID::t_EmptyArray:
Duncan Sands1df98592010-02-16 11:11:14 +00002619 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner081b5052009-01-05 07:52:51 +00002620 return Error(ID.Loc, "invalid empty array initializer");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002621 V = UndefValue::get(Ty);
Chris Lattner081b5052009-01-05 07:52:51 +00002622 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002623 case ValID::t_Zero:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002624 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002625 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002626 return Error(ID.Loc, "invalid type for null constant");
Owen Andersona7235ea2009-07-31 20:28:14 +00002627 V = Constant::getNullValue(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002628 return false;
2629 case ValID::t_Constant:
Chris Lattner61c70e92010-08-28 04:09:24 +00002630 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerdf986172009-01-02 07:01:27 +00002631 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002632
Chris Lattnerdf986172009-01-02 07:01:27 +00002633 V = ID.ConstantVal;
2634 return false;
2635 }
2636}
Daniel Dunbara279bc32009-09-20 02:20:51 +00002637
Chris Lattnerdf986172009-01-02 07:01:27 +00002638bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
2639 V = 0;
2640 ValID ID;
Victor Hernandezbf170d42010-01-05 22:22:14 +00002641 return ParseValID(ID, &PFS) ||
Victor Hernandez92f238d2010-01-11 22:31:58 +00002642 ConvertValIDToValue(Ty, ID, V, &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002643}
2644
2645bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002646 PATypeHolder T(Type::getVoidTy(Context));
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002647 return ParseType(T) ||
2648 ParseValue(T, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002649}
2650
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002651bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2652 PerFunctionState &PFS) {
2653 Value *V;
2654 Loc = Lex.getLoc();
2655 if (ParseTypeAndValue(V, PFS)) return true;
2656 if (!isa<BasicBlock>(V))
2657 return Error(Loc, "expected a basic block");
2658 BB = cast<BasicBlock>(V);
2659 return false;
2660}
2661
2662
Chris Lattnerdf986172009-01-02 07:01:27 +00002663/// FunctionHeader
2664/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindolabea46262011-01-08 16:42:36 +00002665/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Chris Lattnerdf986172009-01-02 07:01:27 +00002666/// OptionalAlign OptGC
2667bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2668 // Parse the linkage.
2669 LocTy LinkageLoc = Lex.getLoc();
2670 unsigned Linkage;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002671
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002672 unsigned Visibility, RetAttrs;
2673 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00002674 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00002675 LocTy RetTypeLoc = Lex.getLoc();
2676 if (ParseOptionalLinkage(Linkage) ||
2677 ParseOptionalVisibility(Visibility) ||
2678 ParseOptionalCallingConv(CC) ||
2679 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00002680 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerdf986172009-01-02 07:01:27 +00002681 return true;
2682
2683 // Verify that the linkage is ok.
2684 switch ((GlobalValue::LinkageTypes)Linkage) {
2685 case GlobalValue::ExternalLinkage:
2686 break; // always ok.
2687 case GlobalValue::DLLImportLinkage:
Duncan Sands5f4ee1f2009-03-11 08:08:06 +00002688 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002689 if (isDefine)
2690 return Error(LinkageLoc, "invalid linkage for function definition");
2691 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +00002692 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +00002693 case GlobalValue::LinkerPrivateLinkage:
Bill Wendling5e721d72010-07-01 21:55:59 +00002694 case GlobalValue::LinkerPrivateWeakLinkage:
Bill Wendling55ae5152010-08-20 22:05:50 +00002695 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002696 case GlobalValue::InternalLinkage:
Nick Lewycky55f64db2009-04-13 07:02:02 +00002697 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002698 case GlobalValue::LinkOnceAnyLinkage:
2699 case GlobalValue::LinkOnceODRLinkage:
2700 case GlobalValue::WeakAnyLinkage:
2701 case GlobalValue::WeakODRLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002702 case GlobalValue::DLLExportLinkage:
2703 if (!isDefine)
2704 return Error(LinkageLoc, "invalid linkage for function declaration");
2705 break;
2706 case GlobalValue::AppendingLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +00002707 case GlobalValue::CommonLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002708 return Error(LinkageLoc, "invalid function linkage type");
2709 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002710
Chris Lattner99bb3152009-01-05 08:00:30 +00002711 if (!FunctionType::isValidReturnType(RetType) ||
Duncan Sands47c51882010-02-16 14:50:09 +00002712 RetType->isOpaqueTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002713 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002714
Chris Lattnerdf986172009-01-02 07:01:27 +00002715 LocTy NameLoc = Lex.getLoc();
Chris Lattnerf570e622009-02-18 21:48:13 +00002716
2717 std::string FunctionName;
2718 if (Lex.getKind() == lltok::GlobalVar) {
2719 FunctionName = Lex.getStrVal();
2720 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
2721 unsigned NameID = Lex.getUIntVal();
2722
2723 if (NameID != NumberedVals.size())
2724 return TokError("function expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002725 Twine(NumberedVals.size()) + "'");
Chris Lattnerf570e622009-02-18 21:48:13 +00002726 } else {
2727 return TokError("expected function name");
2728 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002729
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002730 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002731
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002732 if (Lex.getKind() != lltok::lparen)
Chris Lattnerdf986172009-01-02 07:01:27 +00002733 return TokError("expected '(' in function argument list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002734
Chris Lattnerdf986172009-01-02 07:01:27 +00002735 std::vector<ArgInfo> ArgList;
2736 bool isVarArg;
Chris Lattnerdf986172009-01-02 07:01:27 +00002737 unsigned FuncAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00002738 std::string Section;
Chris Lattnerdf986172009-01-02 07:01:27 +00002739 unsigned Alignment;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002740 std::string GC;
Rafael Espindola3971df52011-01-25 19:09:56 +00002741 bool UnnamedAddr;
2742 LocTy UnnamedAddrLoc;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002743
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00002744 if (ParseArgumentList(ArgList, isVarArg, false) ||
Rafael Espindola3971df52011-01-25 19:09:56 +00002745 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
2746 &UnnamedAddrLoc) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002747 ParseOptionalAttrs(FuncAttrs, 2) ||
2748 (EatIfPresent(lltok::kw_section) &&
2749 ParseStringConstant(Section)) ||
2750 ParseOptionalAlignment(Alignment) ||
2751 (EatIfPresent(lltok::kw_gc) &&
2752 ParseStringConstant(GC)))
2753 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00002754
2755 // If the alignment was parsed as an attribute, move to the alignment field.
2756 if (FuncAttrs & Attribute::Alignment) {
2757 Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs);
2758 FuncAttrs &= ~Attribute::Alignment;
2759 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002760
Chris Lattnerdf986172009-01-02 07:01:27 +00002761 // Okay, if we got here, the function is syntactically valid. Convert types
2762 // and do semantic checks.
2763 std::vector<const Type*> ParamTypeList;
2764 SmallVector<AttributeWithIndex, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002765 // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
Chris Lattnerdf986172009-01-02 07:01:27 +00002766 // attributes.
2767 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
2768 if (FuncAttrs & ObsoleteFuncAttrs) {
2769 RetAttrs |= FuncAttrs & ObsoleteFuncAttrs;
2770 FuncAttrs &= ~ObsoleteFuncAttrs;
2771 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002772
Chris Lattnerdf986172009-01-02 07:01:27 +00002773 if (RetAttrs != Attribute::None)
2774 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002775
Chris Lattnerdf986172009-01-02 07:01:27 +00002776 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
2777 ParamTypeList.push_back(ArgList[i].Type);
2778 if (ArgList[i].Attrs != Attribute::None)
2779 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
2780 }
2781
2782 if (FuncAttrs != Attribute::None)
2783 Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs));
2784
2785 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002786
Benjamin Kramerf0127052010-01-05 13:12:22 +00002787 if (PAL.paramHasAttr(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002788 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2789
Owen Andersonfba933c2009-07-01 23:57:11 +00002790 const FunctionType *FT =
Owen Andersondebcb012009-07-29 22:17:13 +00002791 FunctionType::get(RetType, ParamTypeList, isVarArg);
2792 const PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerdf986172009-01-02 07:01:27 +00002793
2794 Fn = 0;
2795 if (!FunctionName.empty()) {
2796 // If this was a definition of a forward reference, remove the definition
2797 // from the forward reference table and fill in the forward ref.
2798 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2799 ForwardRefVals.find(FunctionName);
2800 if (FRVI != ForwardRefVals.end()) {
2801 Fn = M->getFunction(FunctionName);
Chris Lattnerf1cfb952010-04-20 04:49:11 +00002802 if (Fn->getType() != PFT)
2803 return Error(FRVI->second.second, "invalid forward reference to "
2804 "function '" + FunctionName + "' with wrong type!");
2805
Chris Lattnerdf986172009-01-02 07:01:27 +00002806 ForwardRefVals.erase(FRVI);
2807 } else if ((Fn = M->getFunction(FunctionName))) {
2808 // If this function already exists in the symbol table, then it is
2809 // multiply defined. We accept a few cases for old backwards compat.
2810 // FIXME: Remove this stuff for LLVM 3.0.
2811 if (Fn->getType() != PFT || Fn->getAttributes() != PAL ||
2812 (!Fn->isDeclaration() && isDefine)) {
2813 // If the redefinition has different type or different attributes,
2814 // reject it. If both have bodies, reject it.
2815 return Error(NameLoc, "invalid redefinition of function '" +
2816 FunctionName + "'");
2817 } else if (Fn->isDeclaration()) {
2818 // Make sure to strip off any argument names so we can't get conflicts.
2819 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2820 AI != AE; ++AI)
2821 AI->setName("");
2822 }
Chris Lattner1d871c52009-10-25 23:22:50 +00002823 } else if (M->getNamedValue(FunctionName)) {
2824 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002825 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002826
Dan Gohman41905542009-08-29 23:37:49 +00002827 } else {
Chris Lattnerdf986172009-01-02 07:01:27 +00002828 // If this is a definition of a forward referenced function, make sure the
2829 // types agree.
2830 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
2831 = ForwardRefValIDs.find(NumberedVals.size());
2832 if (I != ForwardRefValIDs.end()) {
2833 Fn = cast<Function>(I->second.first);
2834 if (Fn->getType() != PFT)
2835 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002836 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerdf986172009-01-02 07:01:27 +00002837 ForwardRefValIDs.erase(I);
2838 }
2839 }
2840
2841 if (Fn == 0)
2842 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2843 else // Move the forward-reference to the correct spot in the module.
2844 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2845
2846 if (FunctionName.empty())
2847 NumberedVals.push_back(Fn);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002848
Chris Lattnerdf986172009-01-02 07:01:27 +00002849 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
2850 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
2851 Fn->setCallingConv(CC);
2852 Fn->setAttributes(PAL);
Rafael Espindolabea46262011-01-08 16:42:36 +00002853 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerdf986172009-01-02 07:01:27 +00002854 Fn->setAlignment(Alignment);
2855 Fn->setSection(Section);
2856 if (!GC.empty()) Fn->setGC(GC.c_str());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002857
Chris Lattnerdf986172009-01-02 07:01:27 +00002858 // Add all of the arguments we parsed to the function.
2859 Function::arg_iterator ArgIt = Fn->arg_begin();
2860 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
Chris Lattner5bda3792009-11-26 22:48:23 +00002861 // If we run out of arguments in the Function prototype, exit early.
2862 // FIXME: REMOVE THIS IN LLVM 3.0, this is just for the mismatch case above.
2863 if (ArgIt == Fn->arg_end()) break;
2864
Chris Lattnerdf986172009-01-02 07:01:27 +00002865 // If the argument has a name, insert it into the argument symbol table.
2866 if (ArgList[i].Name.empty()) continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002867
Chris Lattnerdf986172009-01-02 07:01:27 +00002868 // Set the name, if it conflicted, it will be auto-renamed.
2869 ArgIt->setName(ArgList[i].Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002870
Benjamin Krameraf812352010-10-16 11:28:23 +00002871 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerdf986172009-01-02 07:01:27 +00002872 return Error(ArgList[i].Loc, "redefinition of argument '%" +
2873 ArgList[i].Name + "'");
2874 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002875
Chris Lattnerdf986172009-01-02 07:01:27 +00002876 return false;
2877}
2878
2879
2880/// ParseFunctionBody
2881/// ::= '{' BasicBlock+ '}'
2882/// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0
2883///
2884bool LLParser::ParseFunctionBody(Function &Fn) {
2885 if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin)
2886 return TokError("expected '{' in function body");
2887 Lex.Lex(); // eat the {.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002888
Chris Lattner09d9ef42009-10-28 03:39:23 +00002889 int FunctionNumber = -1;
2890 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
2891
2892 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002893
Chris Lattner2fdf8db2010-01-09 19:20:07 +00002894 // We need at least one basic block.
2895 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_end)
2896 return TokError("function body requires at least one basic block");
2897
Chris Lattnerdf986172009-01-02 07:01:27 +00002898 while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end)
2899 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002900
Chris Lattnerdf986172009-01-02 07:01:27 +00002901 // Eat the }.
2902 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002903
Chris Lattnerdf986172009-01-02 07:01:27 +00002904 // Verify function is ok.
Chris Lattner09d9ef42009-10-28 03:39:23 +00002905 return PFS.FinishFunction();
Chris Lattnerdf986172009-01-02 07:01:27 +00002906}
2907
2908/// ParseBasicBlock
2909/// ::= LabelStr? Instruction*
2910bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
2911 // If this basic block starts out with a name, remember it.
2912 std::string Name;
2913 LocTy NameLoc = Lex.getLoc();
2914 if (Lex.getKind() == lltok::LabelStr) {
2915 Name = Lex.getStrVal();
2916 Lex.Lex();
2917 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002918
Chris Lattnerdf986172009-01-02 07:01:27 +00002919 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2920 if (BB == 0) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002921
Chris Lattnerdf986172009-01-02 07:01:27 +00002922 std::string NameStr;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002923
Chris Lattnerdf986172009-01-02 07:01:27 +00002924 // Parse the instructions in this block until we get a terminator.
2925 Instruction *Inst;
Chris Lattner1340dd32009-12-30 05:48:36 +00002926 SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +00002927 do {
2928 // This instruction may have three possibilities for a name: a) none
2929 // specified, b) name specified "%foo =", c) number specified: "%4 =".
2930 LocTy NameLoc = Lex.getLoc();
2931 int NameID = -1;
2932 NameStr = "";
Daniel Dunbara279bc32009-09-20 02:20:51 +00002933
Chris Lattnerdf986172009-01-02 07:01:27 +00002934 if (Lex.getKind() == lltok::LocalVarID) {
2935 NameID = Lex.getUIntVal();
2936 Lex.Lex();
2937 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
2938 return true;
2939 } else if (Lex.getKind() == lltok::LocalVar ||
2940 // FIXME: REMOVE IN LLVM 3.0
2941 Lex.getKind() == lltok::StringConstant) {
2942 NameStr = Lex.getStrVal();
2943 Lex.Lex();
2944 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2945 return true;
2946 }
Devang Patelf633a062009-09-17 23:04:48 +00002947
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002948 switch (ParseInstruction(Inst, BB, PFS)) {
2949 default: assert(0 && "Unknown ParseInstruction result!");
2950 case InstError: return true;
2951 case InstNormal:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002952 BB->getInstList().push_back(Inst);
2953
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002954 // With a normal result, we check to see if the instruction is followed by
2955 // a comma and metadata.
2956 if (EatIfPresent(lltok::comma))
Dan Gohman9d072f52010-08-24 02:05:17 +00002957 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002958 return true;
2959 break;
2960 case InstExtraComma:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002961 BB->getInstList().push_back(Inst);
2962
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002963 // If the instruction parser ate an extra comma at the end of it, it
2964 // *must* be followed by metadata.
Dan Gohman9d072f52010-08-24 02:05:17 +00002965 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002966 return true;
2967 break;
2968 }
Devang Patelf633a062009-09-17 23:04:48 +00002969
Chris Lattnerdf986172009-01-02 07:01:27 +00002970 // Set the name on the instruction.
2971 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
2972 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002973
Chris Lattnerdf986172009-01-02 07:01:27 +00002974 return false;
2975}
2976
2977//===----------------------------------------------------------------------===//
2978// Instruction Parsing.
2979//===----------------------------------------------------------------------===//
2980
2981/// ParseInstruction - Parse one of the many different instructions.
2982///
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002983int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
2984 PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002985 lltok::Kind Token = Lex.getKind();
2986 if (Token == lltok::Eof)
2987 return TokError("found end of file when expecting more instructions");
2988 LocTy Loc = Lex.getLoc();
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002989 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002990 Lex.Lex(); // Eat the keyword.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002991
Chris Lattnerdf986172009-01-02 07:01:27 +00002992 switch (Token) {
2993 default: return Error(Loc, "expected instruction opcode");
2994 // Terminator Instructions.
Owen Anderson1d0be152009-08-13 21:58:54 +00002995 case lltok::kw_unwind: Inst = new UnwindInst(Context); return false;
2996 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002997 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
2998 case lltok::kw_br: return ParseBr(Inst, PFS);
2999 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +00003000 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003001 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
3002 // Binary Operators.
3003 case lltok::kw_add:
3004 case lltok::kw_sub:
Chris Lattnerf067d582011-02-07 16:40:21 +00003005 case lltok::kw_mul:
3006 case lltok::kw_shl: {
Chris Lattnerf067d582011-02-07 16:40:21 +00003007 bool NUW = EatIfPresent(lltok::kw_nuw);
3008 bool NSW = EatIfPresent(lltok::kw_nsw);
3009 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
3010
3011 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3012
3013 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3014 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3015 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003016 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00003017 case lltok::kw_fadd:
3018 case lltok::kw_fsub:
3019 case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
3020
Chris Lattner35bda892011-02-06 21:44:57 +00003021 case lltok::kw_sdiv:
Chris Lattnerf067d582011-02-07 16:40:21 +00003022 case lltok::kw_udiv:
3023 case lltok::kw_lshr:
3024 case lltok::kw_ashr: {
3025 bool Exact = EatIfPresent(lltok::kw_exact);
3026
3027 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3028 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3029 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003030 }
3031
Chris Lattnerdf986172009-01-02 07:01:27 +00003032 case lltok::kw_urem:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003033 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnere914b592009-01-05 08:24:46 +00003034 case lltok::kw_fdiv:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003035 case lltok::kw_frem: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
Chris Lattnerdf986172009-01-02 07:01:27 +00003036 case lltok::kw_and:
3037 case lltok::kw_or:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003038 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003039 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003040 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003041 // Casts.
3042 case lltok::kw_trunc:
3043 case lltok::kw_zext:
3044 case lltok::kw_sext:
3045 case lltok::kw_fptrunc:
3046 case lltok::kw_fpext:
3047 case lltok::kw_bitcast:
3048 case lltok::kw_uitofp:
3049 case lltok::kw_sitofp:
3050 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00003051 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00003052 case lltok::kw_inttoptr:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003053 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003054 // Other.
3055 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +00003056 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003057 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3058 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3059 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3060 case lltok::kw_phi: return ParsePHI(Inst, PFS);
3061 case lltok::kw_call: return ParseCall(Inst, PFS, false);
3062 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
3063 // Memory.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003064 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
3065 case lltok::kw_malloc: return ParseAlloc(Inst, PFS, BB, false);
Victor Hernandez66284e02009-10-24 04:23:03 +00003066 case lltok::kw_free: return ParseFree(Inst, PFS, BB);
Chris Lattnerdf986172009-01-02 07:01:27 +00003067 case lltok::kw_load: return ParseLoad(Inst, PFS, false);
3068 case lltok::kw_store: return ParseStore(Inst, PFS, false);
3069 case lltok::kw_volatile:
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003070 if (EatIfPresent(lltok::kw_load))
Chris Lattnerdf986172009-01-02 07:01:27 +00003071 return ParseLoad(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003072 else if (EatIfPresent(lltok::kw_store))
Chris Lattnerdf986172009-01-02 07:01:27 +00003073 return ParseStore(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003074 else
Chris Lattnerdf986172009-01-02 07:01:27 +00003075 return TokError("expected 'load' or 'store'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003076 case lltok::kw_getresult: return ParseGetResult(Inst, PFS);
3077 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3078 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3079 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3080 }
3081}
3082
3083/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3084bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003085 if (Opc == Instruction::FCmp) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003086 switch (Lex.getKind()) {
3087 default: TokError("expected fcmp predicate (e.g. 'oeq')");
3088 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3089 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3090 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3091 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3092 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3093 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3094 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3095 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3096 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3097 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3098 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3099 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3100 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3101 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3102 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3103 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3104 }
3105 } else {
3106 switch (Lex.getKind()) {
3107 default: TokError("expected icmp predicate (e.g. 'eq')");
3108 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3109 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3110 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3111 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3112 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3113 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3114 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3115 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3116 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3117 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3118 }
3119 }
3120 Lex.Lex();
3121 return false;
3122}
3123
3124//===----------------------------------------------------------------------===//
3125// Terminator Instructions.
3126//===----------------------------------------------------------------------===//
3127
3128/// ParseRet - Parse a return instruction.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003129/// ::= 'ret' void (',' !dbg, !1)*
3130/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
3131/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ (',' !dbg, !1)*
Devang Patelf633a062009-09-17 23:04:48 +00003132/// [[obsolete: LLVM 3.0]]
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003133int LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
3134 PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003135 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnera9a9e072009-03-09 04:49:14 +00003136 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003137
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00003138 if (Ty->isVoidTy()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003139 Inst = ReturnInst::Create(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00003140 return false;
3141 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003142
Chris Lattnerdf986172009-01-02 07:01:27 +00003143 Value *RV;
3144 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003145
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003146 bool ExtraComma = false;
Devang Patelf633a062009-09-17 23:04:48 +00003147 if (EatIfPresent(lltok::comma)) {
Devang Patel0475c912009-09-29 00:01:14 +00003148 // Parse optional custom metadata, e.g. !dbg
Chris Lattner1d928312009-12-30 05:02:06 +00003149 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003150 ExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003151 } else {
3152 // The normal case is one return value.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003153 // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring
3154 // use of 'ret {i32,i32} {i32 1, i32 2}'
Devang Patelf633a062009-09-17 23:04:48 +00003155 SmallVector<Value*, 8> RVs;
3156 RVs.push_back(RV);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003157
Devang Patelf633a062009-09-17 23:04:48 +00003158 do {
Devang Patel0475c912009-09-29 00:01:14 +00003159 // If optional custom metadata, e.g. !dbg is seen then this is the
3160 // end of MRV.
Chris Lattner1d928312009-12-30 05:02:06 +00003161 if (Lex.getKind() == lltok::MetadataVar)
Daniel Dunbara279bc32009-09-20 02:20:51 +00003162 break;
3163 if (ParseTypeAndValue(RV, PFS)) return true;
3164 RVs.push_back(RV);
Devang Patelf633a062009-09-17 23:04:48 +00003165 } while (EatIfPresent(lltok::comma));
3166
3167 RV = UndefValue::get(PFS.getFunction().getReturnType());
3168 for (unsigned i = 0, e = RVs.size(); i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00003169 Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv");
3170 BB->getInstList().push_back(I);
3171 RV = I;
Devang Patelf633a062009-09-17 23:04:48 +00003172 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003173 }
3174 }
Devang Patelf633a062009-09-17 23:04:48 +00003175
Owen Anderson1d0be152009-08-13 21:58:54 +00003176 Inst = ReturnInst::Create(Context, RV);
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003177 return ExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003178}
3179
3180
3181/// ParseBr
3182/// ::= 'br' TypeAndValue
3183/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3184bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3185 LocTy Loc, Loc2;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003186 Value *Op0;
3187 BasicBlock *Op1, *Op2;
Chris Lattnerdf986172009-01-02 07:01:27 +00003188 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003189
Chris Lattnerdf986172009-01-02 07:01:27 +00003190 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3191 Inst = BranchInst::Create(BB);
3192 return false;
3193 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003194
Owen Anderson1d0be152009-08-13 21:58:54 +00003195 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerdf986172009-01-02 07:01:27 +00003196 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003197
Chris Lattnerdf986172009-01-02 07:01:27 +00003198 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003199 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003200 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003201 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003202 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003203
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003204 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerdf986172009-01-02 07:01:27 +00003205 return false;
3206}
3207
3208/// ParseSwitch
3209/// Instruction
3210/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3211/// JumpTable
3212/// ::= (TypeAndValue ',' TypeAndValue)*
3213bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3214 LocTy CondLoc, BBLoc;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003215 Value *Cond;
3216 BasicBlock *DefaultBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003217 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3218 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003219 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003220 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3221 return true;
3222
Duncan Sands1df98592010-02-16 11:11:14 +00003223 if (!Cond->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003224 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003225
Chris Lattnerdf986172009-01-02 07:01:27 +00003226 // Parse the jump table pairs.
3227 SmallPtrSet<Value*, 32> SeenCases;
3228 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3229 while (Lex.getKind() != lltok::rsquare) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003230 Value *Constant;
3231 BasicBlock *DestBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003232
Chris Lattnerdf986172009-01-02 07:01:27 +00003233 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3234 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003235 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003236 return true;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003237
Chris Lattnerdf986172009-01-02 07:01:27 +00003238 if (!SeenCases.insert(Constant))
3239 return Error(CondLoc, "duplicate case value in switch");
3240 if (!isa<ConstantInt>(Constant))
3241 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003242
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003243 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerdf986172009-01-02 07:01:27 +00003244 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003245
Chris Lattnerdf986172009-01-02 07:01:27 +00003246 Lex.Lex(); // Eat the ']'.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003247
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003248 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003249 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3250 SI->addCase(Table[i].first, Table[i].second);
3251 Inst = SI;
3252 return false;
3253}
3254
Chris Lattnerab21db72009-10-28 00:19:10 +00003255/// ParseIndirectBr
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003256/// Instruction
Chris Lattnerab21db72009-10-28 00:19:10 +00003257/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3258bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003259 LocTy AddrLoc;
3260 Value *Address;
3261 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerab21db72009-10-28 00:19:10 +00003262 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3263 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003264 return true;
3265
Duncan Sands1df98592010-02-16 11:11:14 +00003266 if (!Address->getType()->isPointerTy())
Chris Lattnerab21db72009-10-28 00:19:10 +00003267 return Error(AddrLoc, "indirectbr address must have pointer type");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003268
3269 // Parse the destination list.
3270 SmallVector<BasicBlock*, 16> DestList;
3271
3272 if (Lex.getKind() != lltok::rsquare) {
3273 BasicBlock *DestBB;
3274 if (ParseTypeAndBasicBlock(DestBB, PFS))
3275 return true;
3276 DestList.push_back(DestBB);
3277
3278 while (EatIfPresent(lltok::comma)) {
3279 if (ParseTypeAndBasicBlock(DestBB, PFS))
3280 return true;
3281 DestList.push_back(DestBB);
3282 }
3283 }
3284
3285 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3286 return true;
3287
Chris Lattnerab21db72009-10-28 00:19:10 +00003288 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003289 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3290 IBI->addDestination(DestList[i]);
3291 Inst = IBI;
3292 return false;
3293}
3294
3295
Chris Lattnerdf986172009-01-02 07:01:27 +00003296/// ParseInvoke
3297/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3298/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3299bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3300 LocTy CallLoc = Lex.getLoc();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003301 unsigned RetAttrs, FnAttrs;
3302 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00003303 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003304 LocTy RetTypeLoc;
3305 ValID CalleeID;
3306 SmallVector<ParamInfo, 16> ArgList;
3307
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003308 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003309 if (ParseOptionalCallingConv(CC) ||
3310 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003311 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003312 ParseValID(CalleeID) ||
3313 ParseParameterList(ArgList, PFS) ||
3314 ParseOptionalAttrs(FnAttrs, 2) ||
3315 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003316 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003317 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003318 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003319 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003320
Chris Lattnerdf986172009-01-02 07:01:27 +00003321 // If RetType is a non-function pointer type, then this is the short syntax
3322 // for the call, which means that RetType is just the return type. Infer the
3323 // rest of the function argument types from the arguments that are present.
3324 const PointerType *PFTy = 0;
3325 const FunctionType *Ty = 0;
3326 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3327 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3328 // Pull out the types of all of the arguments...
3329 std::vector<const Type*> ParamTypes;
3330 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3331 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003332
Chris Lattnerdf986172009-01-02 07:01:27 +00003333 if (!FunctionType::isValidReturnType(RetType))
3334 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003335
Owen Andersondebcb012009-07-29 22:17:13 +00003336 Ty = FunctionType::get(RetType, ParamTypes, false);
3337 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003338 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003339
Chris Lattnerdf986172009-01-02 07:01:27 +00003340 // Look up the callee.
3341 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003342 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003343
Chris Lattnerdf986172009-01-02 07:01:27 +00003344 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3345 // function attributes.
3346 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3347 if (FnAttrs & ObsoleteFuncAttrs) {
3348 RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3349 FnAttrs &= ~ObsoleteFuncAttrs;
3350 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003351
Chris Lattnerdf986172009-01-02 07:01:27 +00003352 // Set up the Attributes for the function.
3353 SmallVector<AttributeWithIndex, 8> Attrs;
3354 if (RetAttrs != Attribute::None)
3355 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003356
Chris Lattnerdf986172009-01-02 07:01:27 +00003357 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003358
Chris Lattnerdf986172009-01-02 07:01:27 +00003359 // Loop through FunctionType's arguments and ensure they are specified
3360 // correctly. Also, gather any parameter attributes.
3361 FunctionType::param_iterator I = Ty->param_begin();
3362 FunctionType::param_iterator E = Ty->param_end();
3363 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3364 const Type *ExpectedTy = 0;
3365 if (I != E) {
3366 ExpectedTy = *I++;
3367 } else if (!Ty->isVarArg()) {
3368 return Error(ArgList[i].Loc, "too many arguments specified");
3369 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003370
Chris Lattnerdf986172009-01-02 07:01:27 +00003371 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3372 return Error(ArgList[i].Loc, "argument is not of expected type '" +
3373 ExpectedTy->getDescription() + "'");
3374 Args.push_back(ArgList[i].V);
3375 if (ArgList[i].Attrs != Attribute::None)
3376 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3377 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003378
Chris Lattnerdf986172009-01-02 07:01:27 +00003379 if (I != E)
3380 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003381
Chris Lattnerdf986172009-01-02 07:01:27 +00003382 if (FnAttrs != Attribute::None)
3383 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003384
Chris Lattnerdf986172009-01-02 07:01:27 +00003385 // Finish off the Attributes and check them
3386 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003387
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003388 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB,
Chris Lattnerdf986172009-01-02 07:01:27 +00003389 Args.begin(), Args.end());
3390 II->setCallingConv(CC);
3391 II->setAttributes(PAL);
3392 Inst = II;
3393 return false;
3394}
3395
3396
3397
3398//===----------------------------------------------------------------------===//
3399// Binary Operators.
3400//===----------------------------------------------------------------------===//
3401
3402/// ParseArithmetic
Chris Lattnere914b592009-01-05 08:24:46 +00003403/// ::= ArithmeticOps TypeAndValue ',' Value
3404///
3405/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3406/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerdf986172009-01-02 07:01:27 +00003407bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnere914b592009-01-05 08:24:46 +00003408 unsigned Opc, unsigned OperandType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003409 LocTy Loc; Value *LHS, *RHS;
3410 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3411 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3412 ParseValue(LHS->getType(), RHS, PFS))
3413 return true;
3414
Chris Lattnere914b592009-01-05 08:24:46 +00003415 bool Valid;
3416 switch (OperandType) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003417 default: llvm_unreachable("Unknown operand type!");
Chris Lattnere914b592009-01-05 08:24:46 +00003418 case 0: // int or FP.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003419 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3420 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnere914b592009-01-05 08:24:46 +00003421 break;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003422 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3423 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnere914b592009-01-05 08:24:46 +00003424 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003425
Chris Lattnere914b592009-01-05 08:24:46 +00003426 if (!Valid)
3427 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003428
Chris Lattnerdf986172009-01-02 07:01:27 +00003429 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3430 return false;
3431}
3432
3433/// ParseLogical
3434/// ::= ArithmeticOps TypeAndValue ',' Value {
3435bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3436 unsigned Opc) {
3437 LocTy Loc; Value *LHS, *RHS;
3438 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3439 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3440 ParseValue(LHS->getType(), RHS, PFS))
3441 return true;
3442
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003443 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003444 return Error(Loc,"instruction requires integer or integer vector operands");
3445
3446 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3447 return false;
3448}
3449
3450
3451/// ParseCompare
3452/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3453/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerdf986172009-01-02 07:01:27 +00003454bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3455 unsigned Opc) {
3456 // Parse the integer/fp comparison predicate.
3457 LocTy Loc;
3458 unsigned Pred;
3459 Value *LHS, *RHS;
3460 if (ParseCmpPredicate(Pred, Opc) ||
3461 ParseTypeAndValue(LHS, Loc, PFS) ||
3462 ParseToken(lltok::comma, "expected ',' after compare value") ||
3463 ParseValue(LHS->getType(), RHS, PFS))
3464 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003465
Chris Lattnerdf986172009-01-02 07:01:27 +00003466 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003467 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003468 return Error(Loc, "fcmp requires floating point operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003469 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003470 } else {
3471 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003472 if (!LHS->getType()->isIntOrIntVectorTy() &&
Duncan Sands1df98592010-02-16 11:11:14 +00003473 !LHS->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003474 return Error(Loc, "icmp requires integer operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003475 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003476 }
3477 return false;
3478}
3479
3480//===----------------------------------------------------------------------===//
3481// Other Instructions.
3482//===----------------------------------------------------------------------===//
3483
3484
3485/// ParseCast
3486/// ::= CastOpc TypeAndValue 'to' Type
3487bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3488 unsigned Opc) {
3489 LocTy Loc; Value *Op;
Owen Anderson1d0be152009-08-13 21:58:54 +00003490 PATypeHolder DestTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003491 if (ParseTypeAndValue(Op, Loc, PFS) ||
3492 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3493 ParseType(DestTy))
3494 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003495
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003496 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3497 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00003498 return Error(Loc, "invalid cast opcode for cast from '" +
3499 Op->getType()->getDescription() + "' to '" +
3500 DestTy->getDescription() + "'");
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003501 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003502 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3503 return false;
3504}
3505
3506/// ParseSelect
3507/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3508bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3509 LocTy Loc;
3510 Value *Op0, *Op1, *Op2;
3511 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3512 ParseToken(lltok::comma, "expected ',' after select condition") ||
3513 ParseTypeAndValue(Op1, PFS) ||
3514 ParseToken(lltok::comma, "expected ',' after select value") ||
3515 ParseTypeAndValue(Op2, PFS))
3516 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003517
Chris Lattnerdf986172009-01-02 07:01:27 +00003518 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3519 return Error(Loc, Reason);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003520
Chris Lattnerdf986172009-01-02 07:01:27 +00003521 Inst = SelectInst::Create(Op0, Op1, Op2);
3522 return false;
3523}
3524
Chris Lattner0088a5c2009-01-05 08:18:44 +00003525/// ParseVA_Arg
3526/// ::= 'va_arg' TypeAndValue ',' Type
3527bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003528 Value *Op;
Owen Anderson1d0be152009-08-13 21:58:54 +00003529 PATypeHolder EltTy(Type::getVoidTy(Context));
Chris Lattner0088a5c2009-01-05 08:18:44 +00003530 LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003531 if (ParseTypeAndValue(Op, PFS) ||
3532 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattner0088a5c2009-01-05 08:18:44 +00003533 ParseType(EltTy, TypeLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003534 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003535
Chris Lattner0088a5c2009-01-05 08:18:44 +00003536 if (!EltTy->isFirstClassType())
3537 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003538
3539 Inst = new VAArgInst(Op, EltTy);
3540 return false;
3541}
3542
3543/// ParseExtractElement
3544/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3545bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3546 LocTy Loc;
3547 Value *Op0, *Op1;
3548 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3549 ParseToken(lltok::comma, "expected ',' after extract value") ||
3550 ParseTypeAndValue(Op1, PFS))
3551 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003552
Chris Lattnerdf986172009-01-02 07:01:27 +00003553 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3554 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003555
Eric Christophera3500da2009-07-25 02:28:41 +00003556 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003557 return false;
3558}
3559
3560/// ParseInsertElement
3561/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3562bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3563 LocTy Loc;
3564 Value *Op0, *Op1, *Op2;
3565 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3566 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3567 ParseTypeAndValue(Op1, PFS) ||
3568 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3569 ParseTypeAndValue(Op2, PFS))
3570 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003571
Chris Lattnerdf986172009-01-02 07:01:27 +00003572 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopher0aaf4e92009-07-23 01:01:32 +00003573 return Error(Loc, "invalid insertelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003574
Chris Lattnerdf986172009-01-02 07:01:27 +00003575 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3576 return false;
3577}
3578
3579/// ParseShuffleVector
3580/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3581bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3582 LocTy Loc;
3583 Value *Op0, *Op1, *Op2;
3584 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3585 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3586 ParseTypeAndValue(Op1, PFS) ||
3587 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3588 ParseTypeAndValue(Op2, PFS))
3589 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003590
Chris Lattnerdf986172009-01-02 07:01:27 +00003591 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
3592 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003593
Chris Lattnerdf986172009-01-02 07:01:27 +00003594 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3595 return false;
3596}
3597
3598/// ParsePHI
Chris Lattnerc6e20092009-10-18 05:27:44 +00003599/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003600int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003601 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003602 Value *Op0, *Op1;
3603 LocTy TypeLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003604
Chris Lattnerdf986172009-01-02 07:01:27 +00003605 if (ParseType(Ty) ||
3606 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3607 ParseValue(Ty, Op0, PFS) ||
3608 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003609 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003610 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3611 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003612
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003613 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003614 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3615 while (1) {
3616 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003617
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003618 if (!EatIfPresent(lltok::comma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003619 break;
3620
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003621 if (Lex.getKind() == lltok::MetadataVar) {
3622 AteExtraComma = true;
Devang Patela43d46f2009-10-16 18:45:49 +00003623 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003624 }
Devang Patela43d46f2009-10-16 18:45:49 +00003625
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003626 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003627 ParseValue(Ty, Op0, PFS) ||
3628 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003629 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003630 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3631 return true;
3632 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003633
Chris Lattnerdf986172009-01-02 07:01:27 +00003634 if (!Ty->isFirstClassType())
3635 return Error(TypeLoc, "phi node must have first class type");
3636
Jay Foad3ecfc862011-03-30 11:28:46 +00003637 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003638 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3639 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3640 Inst = PN;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003641 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003642}
3643
3644/// ParseCall
3645/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3646/// ParameterList OptionalAttrs
3647bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3648 bool isTail) {
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003649 unsigned RetAttrs, FnAttrs;
3650 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00003651 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003652 LocTy RetTypeLoc;
3653 ValID CalleeID;
3654 SmallVector<ParamInfo, 16> ArgList;
3655 LocTy CallLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003656
Chris Lattnerdf986172009-01-02 07:01:27 +00003657 if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3658 ParseOptionalCallingConv(CC) ||
3659 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003660 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003661 ParseValID(CalleeID) ||
3662 ParseParameterList(ArgList, PFS) ||
3663 ParseOptionalAttrs(FnAttrs, 2))
3664 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003665
Chris Lattnerdf986172009-01-02 07:01:27 +00003666 // If RetType is a non-function pointer type, then this is the short syntax
3667 // for the call, which means that RetType is just the return type. Infer the
3668 // rest of the function argument types from the arguments that are present.
3669 const PointerType *PFTy = 0;
3670 const FunctionType *Ty = 0;
3671 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3672 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3673 // Pull out the types of all of the arguments...
3674 std::vector<const Type*> ParamTypes;
Eli Friedman83b4a972010-07-24 23:06:59 +00003675 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3676 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003677
Chris Lattnerdf986172009-01-02 07:01:27 +00003678 if (!FunctionType::isValidReturnType(RetType))
3679 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003680
Owen Andersondebcb012009-07-29 22:17:13 +00003681 Ty = FunctionType::get(RetType, ParamTypes, false);
3682 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003683 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003684
Chris Lattnerdf986172009-01-02 07:01:27 +00003685 // Look up the callee.
3686 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003687 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003688
Chris Lattnerdf986172009-01-02 07:01:27 +00003689 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3690 // function attributes.
3691 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3692 if (FnAttrs & ObsoleteFuncAttrs) {
3693 RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3694 FnAttrs &= ~ObsoleteFuncAttrs;
3695 }
3696
3697 // Set up the Attributes for the function.
3698 SmallVector<AttributeWithIndex, 8> Attrs;
3699 if (RetAttrs != Attribute::None)
3700 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003701
Chris Lattnerdf986172009-01-02 07:01:27 +00003702 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003703
Chris Lattnerdf986172009-01-02 07:01:27 +00003704 // Loop through FunctionType's arguments and ensure they are specified
3705 // correctly. Also, gather any parameter attributes.
3706 FunctionType::param_iterator I = Ty->param_begin();
3707 FunctionType::param_iterator E = Ty->param_end();
3708 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3709 const Type *ExpectedTy = 0;
3710 if (I != E) {
3711 ExpectedTy = *I++;
3712 } else if (!Ty->isVarArg()) {
3713 return Error(ArgList[i].Loc, "too many arguments specified");
3714 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003715
Chris Lattnerdf986172009-01-02 07:01:27 +00003716 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3717 return Error(ArgList[i].Loc, "argument is not of expected type '" +
3718 ExpectedTy->getDescription() + "'");
3719 Args.push_back(ArgList[i].V);
3720 if (ArgList[i].Attrs != Attribute::None)
3721 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3722 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003723
Chris Lattnerdf986172009-01-02 07:01:27 +00003724 if (I != E)
3725 return Error(CallLoc, "not enough parameters specified for call");
3726
3727 if (FnAttrs != Attribute::None)
3728 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
3729
3730 // Finish off the Attributes and check them
3731 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003732
Chris Lattnerdf986172009-01-02 07:01:27 +00003733 CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end());
3734 CI->setTailCall(isTail);
3735 CI->setCallingConv(CC);
3736 CI->setAttributes(PAL);
3737 Inst = CI;
3738 return false;
3739}
3740
3741//===----------------------------------------------------------------------===//
3742// Memory Instructions.
3743//===----------------------------------------------------------------------===//
3744
3745/// ParseAlloc
Devang Patelf633a062009-09-17 23:04:48 +00003746/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
3747/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003748int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3749 BasicBlock* BB, bool isAlloca) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003750 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003751 Value *Size = 0;
Chris Lattnereeb4a842009-07-02 23:08:13 +00003752 LocTy SizeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003753 unsigned Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003754 if (ParseType(Ty)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003755
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003756 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003757 if (EatIfPresent(lltok::comma)) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003758 if (Lex.getKind() == lltok::kw_align) {
3759 if (ParseOptionalAlignment(Alignment)) return true;
3760 } else if (Lex.getKind() == lltok::MetadataVar) {
3761 AteExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003762 } else {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003763 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3764 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3765 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003766 }
3767 }
3768
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003769 if (Size && !Size->getType()->isIntegerTy())
3770 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003771
Victor Hernandez68afa542009-10-21 19:11:40 +00003772 if (isAlloca) {
Owen Anderson50dead02009-07-15 23:53:25 +00003773 Inst = new AllocaInst(Ty, Size, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003774 return AteExtraComma ? InstExtraComma : InstNormal;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003775 }
Victor Hernandez68afa542009-10-21 19:11:40 +00003776
3777 // Autoupgrade old malloc instruction to malloc call.
3778 // FIXME: Remove in LLVM 3.0.
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003779 if (Size && !Size->getType()->isIntegerTy(32))
3780 return Error(SizeLoc, "element count must be i32");
Victor Hernandez68afa542009-10-21 19:11:40 +00003781 const Type *IntPtrTy = Type::getInt32Ty(Context);
Victor Hernandez9d0b7042009-11-07 00:16:28 +00003782 Constant *AllocSize = ConstantExpr::getSizeOf(Ty);
3783 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy);
Victor Hernandez68afa542009-10-21 19:11:40 +00003784 if (!MallocF)
3785 // Prototype malloc as "void *(int32)".
3786 // This function is renamed as "malloc" in ValidateEndOfModule().
Victor Hernandez336ea062009-10-23 00:59:10 +00003787 MallocF = cast<Function>(
3788 M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
Victor Hernandez9d0b7042009-11-07 00:16:28 +00003789 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003790return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003791}
3792
3793/// ParseFree
3794/// ::= 'free' TypeAndValue
Victor Hernandez66284e02009-10-24 04:23:03 +00003795bool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS,
3796 BasicBlock* BB) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003797 Value *Val; LocTy Loc;
3798 if (ParseTypeAndValue(Val, Loc, PFS)) return true;
Duncan Sands1df98592010-02-16 11:11:14 +00003799 if (!Val->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003800 return Error(Loc, "operand to free must be a pointer");
Victor Hernandez66284e02009-10-24 04:23:03 +00003801 Inst = CallInst::CreateFree(Val, BB);
Chris Lattnerdf986172009-01-02 07:01:27 +00003802 return false;
3803}
3804
3805/// ParseLoad
Devang Patelf633a062009-09-17 23:04:48 +00003806/// ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003807int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS,
3808 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003809 Value *Val; LocTy Loc;
Devang Patelf633a062009-09-17 23:04:48 +00003810 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003811 bool AteExtraComma = false;
3812 if (ParseTypeAndValue(Val, Loc, PFS) ||
3813 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3814 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003815
Duncan Sands1df98592010-02-16 11:11:14 +00003816 if (!Val->getType()->isPointerTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003817 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3818 return Error(Loc, "load operand must be a pointer to a first class type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003819
Chris Lattnerdf986172009-01-02 07:01:27 +00003820 Inst = new LoadInst(Val, "", isVolatile, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003821 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003822}
3823
3824/// ParseStore
Dan Gohmana119de82009-06-14 23:30:43 +00003825/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003826int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS,
3827 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003828 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelf633a062009-09-17 23:04:48 +00003829 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003830 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003831 if (ParseTypeAndValue(Val, Loc, PFS) ||
3832 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003833 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
3834 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003835 return true;
Devang Patelf633a062009-09-17 23:04:48 +00003836
Duncan Sands1df98592010-02-16 11:11:14 +00003837 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003838 return Error(PtrLoc, "store operand must be a pointer");
3839 if (!Val->getType()->isFirstClassType())
3840 return Error(Loc, "store operand must be a first class value");
3841 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3842 return Error(Loc, "stored value and pointer type do not match");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003843
Chris Lattnerdf986172009-01-02 07:01:27 +00003844 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003845 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003846}
3847
3848/// ParseGetResult
Dan Gohmana119de82009-06-14 23:30:43 +00003849/// ::= 'getresult' TypeAndValue ',' i32
Chris Lattnerdf986172009-01-02 07:01:27 +00003850/// FIXME: Remove support for getresult in LLVM 3.0
3851bool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) {
3852 Value *Val; LocTy ValLoc, EltLoc;
3853 unsigned Element;
3854 if (ParseTypeAndValue(Val, ValLoc, PFS) ||
3855 ParseToken(lltok::comma, "expected ',' after getresult operand") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003856 ParseUInt32(Element, EltLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003857 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003858
Duncan Sands1df98592010-02-16 11:11:14 +00003859 if (!Val->getType()->isStructTy() && !Val->getType()->isArrayTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003860 return Error(ValLoc, "getresult inst requires an aggregate operand");
3861 if (!ExtractValueInst::getIndexedType(Val->getType(), Element))
3862 return Error(EltLoc, "invalid getresult index for value");
3863 Inst = ExtractValueInst::Create(Val, Element);
3864 return false;
3865}
3866
3867/// ParseGetElementPtr
Dan Gohmandd8004d2009-07-27 21:53:46 +00003868/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003869int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003870 Value *Ptr, *Val; LocTy Loc, EltLoc;
Dan Gohmandd8004d2009-07-27 21:53:46 +00003871
Dan Gohmandcb40a32009-07-29 15:58:36 +00003872 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohmandd8004d2009-07-27 21:53:46 +00003873
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003874 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003875
Duncan Sands1df98592010-02-16 11:11:14 +00003876 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003877 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003878
Chris Lattnerdf986172009-01-02 07:01:27 +00003879 SmallVector<Value*, 16> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003880 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003881 while (EatIfPresent(lltok::comma)) {
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003882 if (Lex.getKind() == lltok::MetadataVar) {
3883 AteExtraComma = true;
Devang Patel6225d642009-10-13 18:49:55 +00003884 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003885 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003886 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Duncan Sands1df98592010-02-16 11:11:14 +00003887 if (!Val->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003888 return Error(EltLoc, "getelementptr index must be an integer");
3889 Indices.push_back(Val);
3890 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003891
Chris Lattnerdf986172009-01-02 07:01:27 +00003892 if (!GetElementPtrInst::getIndexedType(Ptr->getType(),
3893 Indices.begin(), Indices.end()))
3894 return Error(Loc, "invalid getelementptr indices");
3895 Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end());
Dan Gohmandd8004d2009-07-27 21:53:46 +00003896 if (InBounds)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00003897 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003898 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003899}
3900
3901/// ParseExtractValue
3902/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003903int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003904 Value *Val; LocTy Loc;
3905 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003906 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003907 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003908 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003909 return true;
3910
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003911 if (!Val->getType()->isAggregateType())
3912 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003913
3914 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
3915 Indices.end()))
3916 return Error(Loc, "invalid indices for extractvalue");
3917 Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end());
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003918 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003919}
3920
3921/// ParseInsertValue
3922/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003923int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003924 Value *Val0, *Val1; LocTy Loc0, Loc1;
3925 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003926 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003927 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
3928 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
3929 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003930 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003931 return true;
Chris Lattner628c13a2009-12-30 05:14:00 +00003932
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003933 if (!Val0->getType()->isAggregateType())
3934 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003935
Chris Lattnerdf986172009-01-02 07:01:27 +00003936 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
3937 Indices.end()))
3938 return Error(Loc0, "invalid indices for insertvalue");
3939 Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end());
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003940 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003941}
Nick Lewycky21cc4462009-04-04 07:22:01 +00003942
3943//===----------------------------------------------------------------------===//
3944// Embedded metadata.
3945//===----------------------------------------------------------------------===//
3946
3947/// ParseMDNodeVector
Nick Lewyckycb337992009-05-10 20:57:05 +00003948/// ::= Element (',' Element)*
3949/// Element
3950/// ::= 'null' | TypeAndValue
Victor Hernandezbf170d42010-01-05 22:22:14 +00003951bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandez24e64df2010-01-10 07:14:18 +00003952 PerFunctionState *PFS) {
Dan Gohmanac809752010-07-13 19:33:27 +00003953 // Check for an empty list.
3954 if (Lex.getKind() == lltok::rbrace)
3955 return false;
3956
Nick Lewycky21cc4462009-04-04 07:22:01 +00003957 do {
Chris Lattnera7352392009-12-30 04:42:57 +00003958 // Null is a special case since it is typeless.
3959 if (EatIfPresent(lltok::kw_null)) {
3960 Elts.push_back(0);
3961 continue;
Nick Lewyckycb337992009-05-10 20:57:05 +00003962 }
Chris Lattnera7352392009-12-30 04:42:57 +00003963
3964 Value *V = 0;
3965 PATypeHolder Ty(Type::getVoidTy(Context));
3966 ValID ID;
Victor Hernandezbf170d42010-01-05 22:22:14 +00003967 if (ParseType(Ty) || ParseValID(ID, PFS) ||
Victor Hernandez92f238d2010-01-11 22:31:58 +00003968 ConvertValIDToValue(Ty, ID, V, PFS))
Chris Lattnera7352392009-12-30 04:42:57 +00003969 return true;
3970
Nick Lewyckycb337992009-05-10 20:57:05 +00003971 Elts.push_back(V);
Nick Lewycky21cc4462009-04-04 07:22:01 +00003972 } while (EatIfPresent(lltok::comma));
3973
3974 return false;
3975}