blob: 0260a7f63951c29af859c930cf4ca8cbc6eb4c1b [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;
John McCall3a3465b2011-06-15 20:36:13 +0000988 case lltok::kw_nonlazybind: Attrs |= Attribute::NonLazyBind; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000989
Charles Davis1e063d12010-02-12 00:31:15 +0000990 case lltok::kw_alignstack: {
991 unsigned Alignment;
992 if (ParseOptionalStackAlignment(Alignment))
993 return true;
994 Attrs |= Attribute::constructStackAlignmentFromInt(Alignment);
995 continue;
996 }
997
Chris Lattnerdf986172009-01-02 07:01:27 +0000998 case lltok::kw_align: {
999 unsigned Alignment;
1000 if (ParseOptionalAlignment(Alignment))
1001 return true;
1002 Attrs |= Attribute::constructAlignmentFromInt(Alignment);
1003 continue;
1004 }
Charles Davis1e063d12010-02-12 00:31:15 +00001005
Chris Lattnerdf986172009-01-02 07:01:27 +00001006 }
1007 Lex.Lex();
1008 }
1009}
1010
1011/// ParseOptionalLinkage
1012/// ::= /*empty*/
Rafael Espindolabb46f522009-01-15 20:18:42 +00001013/// ::= 'private'
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001014/// ::= 'linker_private'
Bill Wendling5e721d72010-07-01 21:55:59 +00001015/// ::= 'linker_private_weak'
Bill Wendling55ae5152010-08-20 22:05:50 +00001016/// ::= 'linker_private_weak_def_auto'
Chris Lattnerdf986172009-01-02 07:01:27 +00001017/// ::= 'internal'
1018/// ::= 'weak'
Duncan Sands667d4b82009-03-07 15:45:40 +00001019/// ::= 'weak_odr'
Chris Lattnerdf986172009-01-02 07:01:27 +00001020/// ::= 'linkonce'
Duncan Sands667d4b82009-03-07 15:45:40 +00001021/// ::= 'linkonce_odr'
Bill Wendling5e721d72010-07-01 21:55:59 +00001022/// ::= 'available_externally'
Chris Lattnerdf986172009-01-02 07:01:27 +00001023/// ::= 'appending'
1024/// ::= 'dllexport'
1025/// ::= 'common'
1026/// ::= 'dllimport'
1027/// ::= 'extern_weak'
1028/// ::= 'external'
1029bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1030 HasLinkage = false;
1031 switch (Lex.getKind()) {
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001032 default: Res=GlobalValue::ExternalLinkage; return false;
1033 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
1034 case lltok::kw_linker_private: Res = GlobalValue::LinkerPrivateLinkage; break;
Bill Wendling5e721d72010-07-01 21:55:59 +00001035 case lltok::kw_linker_private_weak:
1036 Res = GlobalValue::LinkerPrivateWeakLinkage;
1037 break;
Bill Wendling55ae5152010-08-20 22:05:50 +00001038 case lltok::kw_linker_private_weak_def_auto:
1039 Res = GlobalValue::LinkerPrivateWeakDefAutoLinkage;
1040 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001041 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1042 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1043 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1044 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1045 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner266c7bb2009-04-13 05:44:34 +00001046 case lltok::kw_available_externally:
1047 Res = GlobalValue::AvailableExternallyLinkage;
1048 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +00001049 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
1050 case lltok::kw_dllexport: Res = GlobalValue::DLLExportLinkage; break;
1051 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
1052 case lltok::kw_dllimport: Res = GlobalValue::DLLImportLinkage; break;
1053 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1054 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001055 }
1056 Lex.Lex();
1057 HasLinkage = true;
1058 return false;
1059}
1060
1061/// ParseOptionalVisibility
1062/// ::= /*empty*/
1063/// ::= 'default'
1064/// ::= 'hidden'
1065/// ::= 'protected'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001066///
Chris Lattnerdf986172009-01-02 07:01:27 +00001067bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1068 switch (Lex.getKind()) {
1069 default: Res = GlobalValue::DefaultVisibility; return false;
1070 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1071 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1072 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1073 }
1074 Lex.Lex();
1075 return false;
1076}
1077
1078/// ParseOptionalCallingConv
1079/// ::= /*empty*/
1080/// ::= 'ccc'
1081/// ::= 'fastcc'
1082/// ::= 'coldcc'
1083/// ::= 'x86_stdcallcc'
1084/// ::= 'x86_fastcallcc'
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001085/// ::= 'x86_thiscallcc'
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001086/// ::= 'arm_apcscc'
1087/// ::= 'arm_aapcscc'
1088/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001089/// ::= 'msp430_intrcc'
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001090/// ::= 'ptx_kernel'
1091/// ::= 'ptx_device'
Chris Lattnerdf986172009-01-02 07:01:27 +00001092/// ::= 'cc' UINT
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001093///
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001094bool LLParser::ParseOptionalCallingConv(CallingConv::ID &CC) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001095 switch (Lex.getKind()) {
1096 default: CC = CallingConv::C; return false;
1097 case lltok::kw_ccc: CC = CallingConv::C; break;
1098 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1099 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1100 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1101 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001102 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Anton Korobeynikov385f5a92009-06-16 18:50:49 +00001103 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1104 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1105 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov211a14e2009-12-07 02:27:35 +00001106 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001107 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1108 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001109 case lltok::kw_cc: {
1110 unsigned ArbitraryCC;
1111 Lex.Lex();
1112 if (ParseUInt32(ArbitraryCC)) {
1113 return true;
1114 } else
1115 CC = static_cast<CallingConv::ID>(ArbitraryCC);
1116 return false;
1117 }
1118 break;
Chris Lattnerdf986172009-01-02 07:01:27 +00001119 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001120
Chris Lattnerdf986172009-01-02 07:01:27 +00001121 Lex.Lex();
1122 return false;
1123}
1124
Chris Lattnerb8c46862009-12-30 05:31:19 +00001125/// ParseInstructionMetadata
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001126/// ::= !dbg !42 (',' !dbg !57)*
Dan Gohman9d072f52010-08-24 02:05:17 +00001127bool LLParser::ParseInstructionMetadata(Instruction *Inst,
1128 PerFunctionState *PFS) {
Chris Lattnerb8c46862009-12-30 05:31:19 +00001129 do {
1130 if (Lex.getKind() != lltok::MetadataVar)
1131 return TokError("expected metadata after comma");
Devang Patel0475c912009-09-29 00:01:14 +00001132
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001133 std::string Name = Lex.getStrVal();
Dan Gohman309b3af2010-08-24 02:24:03 +00001134 unsigned MDK = M->getMDKindID(Name.c_str());
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001135 Lex.Lex();
Chris Lattner52e20312009-10-19 05:31:10 +00001136
Chris Lattner442ffa12009-12-29 21:53:55 +00001137 MDNode *Node;
Chris Lattner449c3102010-04-01 05:14:45 +00001138 SMLoc Loc = Lex.getLoc();
Dan Gohman309b3af2010-08-24 02:24:03 +00001139
1140 if (ParseToken(lltok::exclaim, "expected '!' here"))
Chris Lattnere434d272009-12-30 04:56:59 +00001141 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001142
Dan Gohman68261142010-08-24 14:35:45 +00001143 // This code is similar to that of ParseMetadataValue, however it needs to
1144 // have special-case code for a forward reference; see the comments on
1145 // ForwardRefInstMetadata for details. Also, MDStrings are not supported
1146 // at the top level here.
Dan Gohman309b3af2010-08-24 02:24:03 +00001147 if (Lex.getKind() == lltok::lbrace) {
1148 ValID ID;
1149 if (ParseMetadataListValue(ID, PFS))
1150 return true;
1151 assert(ID.Kind == ValID::t_MDNode);
1152 Inst->setMetadata(MDK, ID.MDNodeVal);
Chris Lattner449c3102010-04-01 05:14:45 +00001153 } else {
Nick Lewyckyc6877b42010-09-30 21:04:13 +00001154 unsigned NodeID = 0;
Dan Gohman309b3af2010-08-24 02:24:03 +00001155 if (ParseMDNodeID(Node, NodeID))
1156 return true;
1157 if (Node) {
1158 // If we got the node, add it to the instruction.
1159 Inst->setMetadata(MDK, Node);
1160 } else {
1161 MDRef R = { Loc, MDK, NodeID };
1162 // Otherwise, remember that this should be resolved later.
1163 ForwardRefInstMetadata[Inst].push_back(R);
1164 }
Chris Lattner449c3102010-04-01 05:14:45 +00001165 }
Chris Lattner3f3a0f62009-12-29 21:25:40 +00001166
1167 // If this is the end of the list, we're done.
Chris Lattnerb8c46862009-12-30 05:31:19 +00001168 } while (EatIfPresent(lltok::comma));
1169 return false;
Devang Patelf633a062009-09-17 23:04:48 +00001170}
1171
Chris Lattnerdf986172009-01-02 07:01:27 +00001172/// ParseOptionalAlignment
1173/// ::= /* empty */
1174/// ::= 'align' 4
1175bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1176 Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001177 if (!EatIfPresent(lltok::kw_align))
1178 return false;
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001179 LocTy AlignLoc = Lex.getLoc();
1180 if (ParseUInt32(Alignment)) return true;
1181 if (!isPowerOf2_32(Alignment))
1182 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmane16829b2010-07-30 21:07:05 +00001183 if (Alignment > Value::MaximumAlignment)
Dan Gohman138aa2a2010-07-28 20:12:04 +00001184 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattner3fbb3ab2009-01-05 07:46:05 +00001185 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001186}
1187
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001188/// ParseOptionalCommaAlign
1189/// ::=
1190/// ::= ',' align 4
1191///
1192/// This returns with AteExtraComma set to true if it ate an excess comma at the
1193/// end.
1194bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1195 bool &AteExtraComma) {
1196 AteExtraComma = false;
1197 while (EatIfPresent(lltok::comma)) {
1198 // Metadata at the end is an early exit.
Chris Lattner1d928312009-12-30 05:02:06 +00001199 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001200 AteExtraComma = true;
1201 return false;
1202 }
1203
Chris Lattner093eed12010-04-23 00:50:50 +00001204 if (Lex.getKind() != lltok::kw_align)
1205 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sandsbf9fc532010-10-21 16:07:10 +00001206
Chris Lattner093eed12010-04-23 00:50:50 +00001207 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00001208 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001209
Devang Patelf633a062009-09-17 23:04:48 +00001210 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001211}
1212
Charles Davis1e063d12010-02-12 00:31:15 +00001213/// ParseOptionalStackAlignment
1214/// ::= /* empty */
1215/// ::= 'alignstack' '(' 4 ')'
1216bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1217 Alignment = 0;
1218 if (!EatIfPresent(lltok::kw_alignstack))
1219 return false;
1220 LocTy ParenLoc = Lex.getLoc();
1221 if (!EatIfPresent(lltok::lparen))
1222 return Error(ParenLoc, "expected '('");
1223 LocTy AlignLoc = Lex.getLoc();
1224 if (ParseUInt32(Alignment)) return true;
1225 ParenLoc = Lex.getLoc();
1226 if (!EatIfPresent(lltok::rparen))
1227 return Error(ParenLoc, "expected ')'");
1228 if (!isPowerOf2_32(Alignment))
1229 return Error(AlignLoc, "stack alignment is not a power of two");
1230 return false;
1231}
Devang Patelf633a062009-09-17 23:04:48 +00001232
Chris Lattner628c13a2009-12-30 05:14:00 +00001233/// ParseIndexList - This parses the index list for an insert/extractvalue
1234/// instruction. This sets AteExtraComma in the case where we eat an extra
1235/// comma at the end of the line and find that it is followed by metadata.
1236/// Clients that don't allow metadata can call the version of this function that
1237/// only takes one argument.
1238///
Chris Lattnerdf986172009-01-02 07:01:27 +00001239/// ParseIndexList
1240/// ::= (',' uint32)+
Chris Lattner628c13a2009-12-30 05:14:00 +00001241///
1242bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1243 bool &AteExtraComma) {
1244 AteExtraComma = false;
1245
Chris Lattnerdf986172009-01-02 07:01:27 +00001246 if (Lex.getKind() != lltok::comma)
1247 return TokError("expected ',' as start of index list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001248
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001249 while (EatIfPresent(lltok::comma)) {
Chris Lattner628c13a2009-12-30 05:14:00 +00001250 if (Lex.getKind() == lltok::MetadataVar) {
1251 AteExtraComma = true;
1252 return false;
1253 }
Nick Lewycky28815c42010-09-29 23:32:20 +00001254 unsigned Idx = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001255 if (ParseUInt32(Idx)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001256 Indices.push_back(Idx);
1257 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001258
Chris Lattnerdf986172009-01-02 07:01:27 +00001259 return false;
1260}
1261
1262//===----------------------------------------------------------------------===//
1263// Type Parsing.
1264//===----------------------------------------------------------------------===//
1265
1266/// ParseType - Parse and resolve a full type.
Chris Lattnera9a9e072009-03-09 04:49:14 +00001267bool LLParser::ParseType(PATypeHolder &Result, bool AllowVoid) {
1268 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001269 if (ParseTypeRec(Result)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001270
Chris Lattnerdf986172009-01-02 07:01:27 +00001271 // Verify no unresolved uprefs.
1272 if (!UpRefs.empty())
1273 return Error(UpRefs.back().Loc, "invalid unresolved type up reference");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001274
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001275 if (!AllowVoid && Result.get()->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001276 return Error(TypeLoc, "void type only allowed for function results");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001277
Chris Lattnerdf986172009-01-02 07:01:27 +00001278 return false;
1279}
1280
1281/// HandleUpRefs - Every time we finish a new layer of types, this function is
1282/// called. It loops through the UpRefs vector, which is a list of the
1283/// currently active types. For each type, if the up-reference is contained in
1284/// the newly completed type, we decrement the level count. When the level
1285/// count reaches zero, the up-referenced type is the type that is passed in:
1286/// thus we can complete the cycle.
1287///
1288PATypeHolder LLParser::HandleUpRefs(const Type *ty) {
1289 // If Ty isn't abstract, or if there are no up-references in it, then there is
1290 // nothing to resolve here.
1291 if (!ty->isAbstract() || UpRefs.empty()) return ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001292
Chris Lattnerdf986172009-01-02 07:01:27 +00001293 PATypeHolder Ty(ty);
1294#if 0
David Greene0e28d762009-12-23 23:38:28 +00001295 dbgs() << "Type '" << Ty->getDescription()
Chris Lattnerdf986172009-01-02 07:01:27 +00001296 << "' newly formed. Resolving upreferences.\n"
1297 << UpRefs.size() << " upreferences active!\n";
1298#endif
Daniel Dunbara279bc32009-09-20 02:20:51 +00001299
Chris Lattnerdf986172009-01-02 07:01:27 +00001300 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
1301 // to zero), we resolve them all together before we resolve them to Ty. At
1302 // the end of the loop, if there is anything to resolve to Ty, it will be in
1303 // this variable.
1304 OpaqueType *TypeToResolve = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001305
Chris Lattnerdf986172009-01-02 07:01:27 +00001306 for (unsigned i = 0; i != UpRefs.size(); ++i) {
1307 // Determine if 'Ty' directly contains this up-references 'LastContainedTy'.
1308 bool ContainsType =
1309 std::find(Ty->subtype_begin(), Ty->subtype_end(),
1310 UpRefs[i].LastContainedTy) != Ty->subtype_end();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001311
Chris Lattnerdf986172009-01-02 07:01:27 +00001312#if 0
David Greene0e28d762009-12-23 23:38:28 +00001313 dbgs() << " UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattnerdf986172009-01-02 07:01:27 +00001314 << UpRefs[i].LastContainedTy->getDescription() << ") = "
1315 << (ContainsType ? "true" : "false")
1316 << " level=" << UpRefs[i].NestingLevel << "\n";
1317#endif
1318 if (!ContainsType)
1319 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001320
Chris Lattnerdf986172009-01-02 07:01:27 +00001321 // Decrement level of upreference
1322 unsigned Level = --UpRefs[i].NestingLevel;
1323 UpRefs[i].LastContainedTy = Ty;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001324
Chris Lattnerdf986172009-01-02 07:01:27 +00001325 // If the Up-reference has a non-zero level, it shouldn't be resolved yet.
1326 if (Level != 0)
1327 continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001328
Chris Lattnerdf986172009-01-02 07:01:27 +00001329#if 0
David Greene0e28d762009-12-23 23:38:28 +00001330 dbgs() << " * Resolving upreference for " << UpRefs[i].UpRefTy << "\n";
Chris Lattnerdf986172009-01-02 07:01:27 +00001331#endif
1332 if (!TypeToResolve)
1333 TypeToResolve = UpRefs[i].UpRefTy;
1334 else
1335 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
1336 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list.
1337 --i; // Do not skip the next element.
1338 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001339
Chris Lattnerdf986172009-01-02 07:01:27 +00001340 if (TypeToResolve)
1341 TypeToResolve->refineAbstractTypeTo(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001342
Chris Lattnerdf986172009-01-02 07:01:27 +00001343 return Ty;
1344}
1345
1346
1347/// ParseTypeRec - The recursive function used to process the internal
1348/// implementation details of types.
1349bool LLParser::ParseTypeRec(PATypeHolder &Result) {
1350 switch (Lex.getKind()) {
1351 default:
1352 return TokError("expected type");
1353 case lltok::Type:
1354 // TypeRec ::= 'float' | 'void' (etc)
1355 Result = Lex.getTyVal();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001356 Lex.Lex();
Chris Lattnerdf986172009-01-02 07:01:27 +00001357 break;
1358 case lltok::kw_opaque:
1359 // TypeRec ::= 'opaque'
Owen Anderson0e275dc2009-08-13 23:27:32 +00001360 Result = OpaqueType::get(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001361 Lex.Lex();
1362 break;
1363 case lltok::lbrace:
1364 // TypeRec ::= '{' ... '}'
1365 if (ParseStructType(Result, false))
1366 return true;
1367 break;
1368 case lltok::lsquare:
1369 // TypeRec ::= '[' ... ']'
1370 Lex.Lex(); // eat the lsquare.
1371 if (ParseArrayVectorType(Result, false))
1372 return true;
1373 break;
1374 case lltok::less: // Either vector or packed struct.
1375 // TypeRec ::= '<' ... '>'
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001376 Lex.Lex();
1377 if (Lex.getKind() == lltok::lbrace) {
1378 if (ParseStructType(Result, true) ||
1379 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerdf986172009-01-02 07:01:27 +00001380 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001381 } else if (ParseArrayVectorType(Result, true))
1382 return true;
1383 break;
1384 case lltok::LocalVar:
1385 case lltok::StringConstant: // FIXME: REMOVE IN LLVM 3.0
1386 // TypeRec ::= %foo
1387 if (const Type *T = M->getTypeByName(Lex.getStrVal())) {
1388 Result = T;
1389 } else {
Owen Anderson0e275dc2009-08-13 23:27:32 +00001390 Result = OpaqueType::get(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001391 ForwardRefTypes.insert(std::make_pair(Lex.getStrVal(),
1392 std::make_pair(Result,
1393 Lex.getLoc())));
1394 M->addTypeName(Lex.getStrVal(), Result.get());
1395 }
1396 Lex.Lex();
1397 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001398
Chris Lattnerdf986172009-01-02 07:01:27 +00001399 case lltok::LocalVarID:
1400 // TypeRec ::= %4
1401 if (Lex.getUIntVal() < NumberedTypes.size())
1402 Result = NumberedTypes[Lex.getUIntVal()];
1403 else {
1404 std::map<unsigned, std::pair<PATypeHolder, LocTy> >::iterator
1405 I = ForwardRefTypeIDs.find(Lex.getUIntVal());
1406 if (I != ForwardRefTypeIDs.end())
1407 Result = I->second.first;
1408 else {
Owen Anderson0e275dc2009-08-13 23:27:32 +00001409 Result = OpaqueType::get(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00001410 ForwardRefTypeIDs.insert(std::make_pair(Lex.getUIntVal(),
1411 std::make_pair(Result,
1412 Lex.getLoc())));
1413 }
1414 }
1415 Lex.Lex();
1416 break;
1417 case lltok::backslash: {
1418 // TypeRec ::= '\' 4
Chris Lattnerdf986172009-01-02 07:01:27 +00001419 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001420 unsigned Val;
1421 if (ParseUInt32(Val)) return true;
Owen Anderson0e275dc2009-08-13 23:27:32 +00001422 OpaqueType *OT = OpaqueType::get(Context); //Use temporary placeholder.
Chris Lattnerdf986172009-01-02 07:01:27 +00001423 UpRefs.push_back(UpRefRecord(Lex.getLoc(), Val, OT));
1424 Result = OT;
1425 break;
1426 }
1427 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001428
1429 // Parse the type suffixes.
Chris Lattnerdf986172009-01-02 07:01:27 +00001430 while (1) {
1431 switch (Lex.getKind()) {
1432 // End of type.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001433 default: return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00001434
1435 // TypeRec ::= TypeRec '*'
1436 case lltok::star:
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001437 if (Result.get()->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001438 return TokError("basic block pointers are invalid");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001439 if (Result.get()->isVoidTy())
Dan Gohmanb9070d32009-02-09 17:41:21 +00001440 return TokError("pointers to void are invalid; use i8* instead");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001441 if (!PointerType::isValidElementType(Result.get()))
1442 return TokError("pointer to this type is invalid");
Owen Andersondebcb012009-07-29 22:17:13 +00001443 Result = HandleUpRefs(PointerType::getUnqual(Result.get()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001444 Lex.Lex();
1445 break;
1446
1447 // TypeRec ::= TypeRec 'addrspace' '(' uint32 ')' '*'
1448 case lltok::kw_addrspace: {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001449 if (Result.get()->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001450 return TokError("basic block pointers are invalid");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001451 if (Result.get()->isVoidTy())
Dan Gohmanb9070d32009-02-09 17:41:21 +00001452 return TokError("pointers to void are invalid; use i8* instead");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001453 if (!PointerType::isValidElementType(Result.get()))
1454 return TokError("pointer to this type is invalid");
Chris Lattnerdf986172009-01-02 07:01:27 +00001455 unsigned AddrSpace;
1456 if (ParseOptionalAddrSpace(AddrSpace) ||
1457 ParseToken(lltok::star, "expected '*' in address space"))
1458 return true;
1459
Owen Andersondebcb012009-07-29 22:17:13 +00001460 Result = HandleUpRefs(PointerType::get(Result.get(), AddrSpace));
Chris Lattnerdf986172009-01-02 07:01:27 +00001461 break;
1462 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001463
Chris Lattnerdf986172009-01-02 07:01:27 +00001464 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1465 case lltok::lparen:
1466 if (ParseFunctionType(Result))
1467 return true;
1468 break;
1469 }
1470 }
1471}
1472
1473/// ParseParameterList
1474/// ::= '(' ')'
1475/// ::= '(' Arg (',' Arg)* ')'
1476/// Arg
1477/// ::= Type OptionalAttributes Value OptionalAttributes
1478bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
1479 PerFunctionState &PFS) {
1480 if (ParseToken(lltok::lparen, "expected '(' in call"))
1481 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001482
Chris Lattnerdf986172009-01-02 07:01:27 +00001483 while (Lex.getKind() != lltok::rparen) {
1484 // If this isn't the first argument, we need a comma.
1485 if (!ArgList.empty() &&
1486 ParseToken(lltok::comma, "expected ',' in argument list"))
1487 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001488
Chris Lattnerdf986172009-01-02 07:01:27 +00001489 // Parse the argument.
1490 LocTy ArgLoc;
Owen Anderson1d0be152009-08-13 21:58:54 +00001491 PATypeHolder ArgTy(Type::getVoidTy(Context));
Victor Hernandez19715562009-12-03 23:40:58 +00001492 unsigned ArgAttrs1 = Attribute::None;
1493 unsigned ArgAttrs2 = Attribute::None;
Chris Lattnerdf986172009-01-02 07:01:27 +00001494 Value *V;
Victor Hernandez19715562009-12-03 23:40:58 +00001495 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00001496 return true;
Victor Hernandez19715562009-12-03 23:40:58 +00001497
Chris Lattner287881d2009-12-30 02:11:14 +00001498 // Otherwise, handle normal operands.
1499 if (ParseOptionalAttrs(ArgAttrs1, 0) ||
1500 ParseValue(ArgTy, V, PFS) ||
1501 // FIXME: Should not allow attributes after the argument, remove this
1502 // in LLVM 3.0.
1503 ParseOptionalAttrs(ArgAttrs2, 3))
1504 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001505 ArgList.push_back(ParamInfo(ArgLoc, V, ArgAttrs1|ArgAttrs2));
1506 }
1507
1508 Lex.Lex(); // Lex the ')'.
1509 return false;
1510}
1511
1512
1513
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001514/// ParseArgumentList - Parse the argument list for a function type or function
1515/// prototype. If 'inType' is true then we are parsing a FunctionType.
Chris Lattnerdf986172009-01-02 07:01:27 +00001516/// ::= '(' ArgTypeListI ')'
1517/// ArgTypeListI
1518/// ::= /*empty*/
1519/// ::= '...'
1520/// ::= ArgTypeList ',' '...'
1521/// ::= ArgType (',' ArgType)*
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001522///
Chris Lattnerdf986172009-01-02 07:01:27 +00001523bool LLParser::ParseArgumentList(std::vector<ArgInfo> &ArgList,
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001524 bool &isVarArg, bool inType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001525 isVarArg = false;
1526 assert(Lex.getKind() == lltok::lparen);
1527 Lex.Lex(); // eat the (.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001528
Chris Lattnerdf986172009-01-02 07:01:27 +00001529 if (Lex.getKind() == lltok::rparen) {
1530 // empty
1531 } else if (Lex.getKind() == lltok::dotdotdot) {
1532 isVarArg = true;
1533 Lex.Lex();
1534 } else {
1535 LocTy TypeLoc = Lex.getLoc();
Owen Anderson1d0be152009-08-13 21:58:54 +00001536 PATypeHolder ArgTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00001537 unsigned Attrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00001538 std::string Name;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001539
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001540 // If we're parsing a type, use ParseTypeRec, because we allow recursive
1541 // types (such as a function returning a pointer to itself). If parsing a
1542 // function prototype, we require fully resolved types.
1543 if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001544 ParseOptionalAttrs(Attrs, 0)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001545
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001546 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001547 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001548
Chris Lattnerdf986172009-01-02 07:01:27 +00001549 if (Lex.getKind() == lltok::LocalVar ||
1550 Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1551 Name = Lex.getStrVal();
1552 Lex.Lex();
1553 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001554
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001555 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001556 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001557
Chris Lattnerdf986172009-01-02 07:01:27 +00001558 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001559
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001560 while (EatIfPresent(lltok::comma)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001561 // Handle ... at end of arg list.
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001562 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001563 isVarArg = true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001564 break;
1565 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001566
Chris Lattnerdf986172009-01-02 07:01:27 +00001567 // Otherwise must be an argument type.
1568 TypeLoc = Lex.getLoc();
Chris Lattnera9a9e072009-03-09 04:49:14 +00001569 if ((inType ? ParseTypeRec(ArgTy) : ParseType(ArgTy)) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001570 ParseOptionalAttrs(Attrs, 0)) return true;
1571
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001572 if (ArgTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001573 return Error(TypeLoc, "argument can not have void type");
1574
Chris Lattnerdf986172009-01-02 07:01:27 +00001575 if (Lex.getKind() == lltok::LocalVar ||
1576 Lex.getKind() == lltok::StringConstant) { // FIXME: REMOVE IN LLVM 3.0
1577 Name = Lex.getStrVal();
1578 Lex.Lex();
1579 } else {
1580 Name = "";
1581 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001582
Duncan Sands47c51882010-02-16 14:50:09 +00001583 if (!ArgTy->isFirstClassType() && !ArgTy->isOpaqueTy())
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001584 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001585
Chris Lattnerdf986172009-01-02 07:01:27 +00001586 ArgList.push_back(ArgInfo(TypeLoc, ArgTy, Attrs, Name));
1587 }
1588 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001589
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001590 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerdf986172009-01-02 07:01:27 +00001591}
Daniel Dunbara279bc32009-09-20 02:20:51 +00001592
Chris Lattnerdf986172009-01-02 07:01:27 +00001593/// ParseFunctionType
1594/// ::= Type ArgumentList OptionalAttrs
1595bool LLParser::ParseFunctionType(PATypeHolder &Result) {
1596 assert(Lex.getKind() == lltok::lparen);
1597
Chris Lattnerd77d04c2009-01-05 08:04:33 +00001598 if (!FunctionType::isValidReturnType(Result))
1599 return TokError("invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001600
Chris Lattnerdf986172009-01-02 07:01:27 +00001601 std::vector<ArgInfo> ArgList;
1602 bool isVarArg;
1603 unsigned Attrs;
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00001604 if (ParseArgumentList(ArgList, isVarArg, true) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00001605 // FIXME: Allow, but ignore attributes on function types!
1606 // FIXME: Remove in LLVM 3.0
1607 ParseOptionalAttrs(Attrs, 2))
1608 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001609
Chris Lattnerdf986172009-01-02 07:01:27 +00001610 // Reject names on the arguments lists.
1611 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1612 if (!ArgList[i].Name.empty())
1613 return Error(ArgList[i].Loc, "argument name invalid in function type");
1614 if (!ArgList[i].Attrs != 0) {
1615 // Allow but ignore attributes on function types; this permits
1616 // auto-upgrade.
1617 // FIXME: REJECT ATTRIBUTES ON FUNCTION TYPES in LLVM 3.0
1618 }
1619 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001620
Chris Lattnerdf986172009-01-02 07:01:27 +00001621 std::vector<const Type*> ArgListTy;
1622 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
1623 ArgListTy.push_back(ArgList[i].Type);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001624
Owen Andersondebcb012009-07-29 22:17:13 +00001625 Result = HandleUpRefs(FunctionType::get(Result.get(),
Owen Andersonfba933c2009-07-01 23:57:11 +00001626 ArgListTy, isVarArg));
Chris Lattnerdf986172009-01-02 07:01:27 +00001627 return false;
1628}
1629
1630/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
1631/// TypeRec
1632/// ::= '{' '}'
1633/// ::= '{' TypeRec (',' TypeRec)* '}'
1634/// ::= '<' '{' '}' '>'
1635/// ::= '<' '{' TypeRec (',' TypeRec)* '}' '>'
1636bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {
1637 assert(Lex.getKind() == lltok::lbrace);
1638 Lex.Lex(); // Consume the '{'
Daniel Dunbara279bc32009-09-20 02:20:51 +00001639
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001640 if (EatIfPresent(lltok::rbrace)) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +00001641 Result = StructType::get(Context, Packed);
Chris Lattnerdf986172009-01-02 07:01:27 +00001642 return false;
1643 }
1644
1645 std::vector<PATypeHolder> ParamsList;
Chris Lattnera9a9e072009-03-09 04:49:14 +00001646 LocTy EltTyLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001647 if (ParseTypeRec(Result)) return true;
1648 ParamsList.push_back(Result);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001649
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001650 if (Result->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001651 return Error(EltTyLoc, "struct element can not have void type");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001652 if (!StructType::isValidElementType(Result))
1653 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001654
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001655 while (EatIfPresent(lltok::comma)) {
Chris Lattnera9a9e072009-03-09 04:49:14 +00001656 EltTyLoc = Lex.getLoc();
Chris Lattnerdf986172009-01-02 07:01:27 +00001657 if (ParseTypeRec(Result)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001658
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001659 if (Result->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001660 return Error(EltTyLoc, "struct element can not have void type");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001661 if (!StructType::isValidElementType(Result))
1662 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001663
Chris Lattnerdf986172009-01-02 07:01:27 +00001664 ParamsList.push_back(Result);
1665 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001666
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001667 if (ParseToken(lltok::rbrace, "expected '}' at end of struct"))
1668 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001669
Chris Lattnerdf986172009-01-02 07:01:27 +00001670 std::vector<const Type*> ParamsListTy;
1671 for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
1672 ParamsListTy.push_back(ParamsList[i].get());
Owen Andersond7f2a6c2009-08-05 23:16:16 +00001673 Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed));
Chris Lattnerdf986172009-01-02 07:01:27 +00001674 return false;
1675}
1676
1677/// ParseArrayVectorType - Parse an array or vector type, assuming the first
1678/// token has already been consumed.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001679/// TypeRec
Chris Lattnerdf986172009-01-02 07:01:27 +00001680/// ::= '[' APSINTVAL 'x' Types ']'
1681/// ::= '<' APSINTVAL 'x' Types '>'
1682bool LLParser::ParseArrayVectorType(PATypeHolder &Result, bool isVector) {
1683 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
1684 Lex.getAPSIntVal().getBitWidth() > 64)
1685 return TokError("expected number in address space");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001686
Chris Lattnerdf986172009-01-02 07:01:27 +00001687 LocTy SizeLoc = Lex.getLoc();
1688 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001689 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001690
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001691 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
1692 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00001693
1694 LocTy TypeLoc = Lex.getLoc();
Owen Anderson1d0be152009-08-13 21:58:54 +00001695 PATypeHolder EltTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00001696 if (ParseTypeRec(EltTy)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001697
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001698 if (EltTy->isVoidTy())
Chris Lattnera9a9e072009-03-09 04:49:14 +00001699 return Error(TypeLoc, "array and vector element type cannot be void");
1700
Chris Lattner3ed88ef2009-01-02 08:05:26 +00001701 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
1702 "expected end of sequential type"))
1703 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001704
Chris Lattnerdf986172009-01-02 07:01:27 +00001705 if (isVector) {
Chris Lattner452e2622009-02-28 18:12:41 +00001706 if (Size == 0)
1707 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerdf986172009-01-02 07:01:27 +00001708 if ((unsigned)Size != Size)
1709 return Error(SizeLoc, "size too large for vector");
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001710 if (!VectorType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001711 return Error(TypeLoc, "vector element type must be fp or integer");
Owen Andersondebcb012009-07-29 22:17:13 +00001712 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerdf986172009-01-02 07:01:27 +00001713 } else {
Nick Lewyckya5f54a02009-06-07 07:26:46 +00001714 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerdf986172009-01-02 07:01:27 +00001715 return Error(TypeLoc, "invalid array element type");
Owen Andersondebcb012009-07-29 22:17:13 +00001716 Result = HandleUpRefs(ArrayType::get(EltTy, Size));
Chris Lattnerdf986172009-01-02 07:01:27 +00001717 }
1718 return false;
1719}
1720
1721//===----------------------------------------------------------------------===//
1722// Function Semantic Analysis.
1723//===----------------------------------------------------------------------===//
1724
Chris Lattner09d9ef42009-10-28 03:39:23 +00001725LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
1726 int functionNumber)
1727 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001728
1729 // Insert unnamed arguments into the NumberedVals list.
1730 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
1731 AI != E; ++AI)
1732 if (!AI->hasName())
1733 NumberedVals.push_back(AI);
1734}
1735
1736LLParser::PerFunctionState::~PerFunctionState() {
1737 // If there were any forward referenced non-basicblock values, delete them.
1738 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
1739 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
1740 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001741 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001742 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001743 delete I->second.first;
1744 I->second.first = 0;
1745 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001746
Chris Lattnerdf986172009-01-02 07:01:27 +00001747 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1748 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
1749 if (!isa<BasicBlock>(I->second.first)) {
Owen Andersonb43eae72009-07-02 17:04:01 +00001750 I->second.first->replaceAllUsesWith(
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001751 UndefValue::get(I->second.first->getType()));
Chris Lattnerdf986172009-01-02 07:01:27 +00001752 delete I->second.first;
1753 I->second.first = 0;
1754 }
1755}
1756
Chris Lattner09d9ef42009-10-28 03:39:23 +00001757bool LLParser::PerFunctionState::FinishFunction() {
1758 // Check to see if someone took the address of labels in this block.
1759 if (!P.ForwardRefBlockAddresses.empty()) {
1760 ValID FunctionID;
1761 if (!F.getName().empty()) {
1762 FunctionID.Kind = ValID::t_GlobalName;
1763 FunctionID.StrVal = F.getName();
1764 } else {
1765 FunctionID.Kind = ValID::t_GlobalID;
1766 FunctionID.UIntVal = FunctionNumber;
1767 }
1768
1769 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >::iterator
1770 FRBAI = P.ForwardRefBlockAddresses.find(FunctionID);
1771 if (FRBAI != P.ForwardRefBlockAddresses.end()) {
1772 // Resolve all these references.
1773 if (P.ResolveForwardRefBlockAddresses(&F, FRBAI->second, this))
1774 return true;
1775
1776 P.ForwardRefBlockAddresses.erase(FRBAI);
1777 }
1778 }
1779
Chris Lattnerdf986172009-01-02 07:01:27 +00001780 if (!ForwardRefVals.empty())
1781 return P.Error(ForwardRefVals.begin()->second.second,
1782 "use of undefined value '%" + ForwardRefVals.begin()->first +
1783 "'");
1784 if (!ForwardRefValIDs.empty())
1785 return P.Error(ForwardRefValIDs.begin()->second.second,
1786 "use of undefined value '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001787 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00001788 return false;
1789}
1790
1791
1792/// GetVal - Get a value with the specified name or ID, creating a
1793/// forward reference record if needed. This can return null if the value
1794/// exists but does not have the right type.
1795Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
1796 const Type *Ty, LocTy Loc) {
1797 // Look this name up in the normal function symbol table.
1798 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001799
Chris Lattnerdf986172009-01-02 07:01:27 +00001800 // If this is a forward reference for the value, see if we already created a
1801 // forward ref record.
1802 if (Val == 0) {
1803 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1804 I = ForwardRefVals.find(Name);
1805 if (I != ForwardRefVals.end())
1806 Val = I->second.first;
1807 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001808
Chris Lattnerdf986172009-01-02 07:01:27 +00001809 // If we have the value in the symbol table or fwd-ref table, return it.
1810 if (Val) {
1811 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001812 if (Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00001813 P.Error(Loc, "'%" + Name + "' is not a basic block");
1814 else
1815 P.Error(Loc, "'%" + Name + "' defined with type '" +
1816 Val->getType()->getDescription() + "'");
1817 return 0;
1818 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001819
Chris Lattnerdf986172009-01-02 07:01:27 +00001820 // Don't make placeholders with invalid type.
Duncan Sands47c51882010-02-16 14:50:09 +00001821 if (!Ty->isFirstClassType() && !Ty->isOpaqueTy() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001822 P.Error(Loc, "invalid use of a non-first-class type");
1823 return 0;
1824 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001825
Chris Lattnerdf986172009-01-02 07:01:27 +00001826 // Otherwise, create a new forward reference for this value and remember it.
1827 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001828 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001829 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001830 else
1831 FwdVal = new Argument(Ty, Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001832
Chris Lattnerdf986172009-01-02 07:01:27 +00001833 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1834 return FwdVal;
1835}
1836
1837Value *LLParser::PerFunctionState::GetVal(unsigned ID, const Type *Ty,
1838 LocTy Loc) {
1839 // Look this name up in the normal function symbol table.
1840 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001841
Chris Lattnerdf986172009-01-02 07:01:27 +00001842 // If this is a forward reference for the value, see if we already created a
1843 // forward ref record.
1844 if (Val == 0) {
1845 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
1846 I = ForwardRefValIDs.find(ID);
1847 if (I != ForwardRefValIDs.end())
1848 Val = I->second.first;
1849 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001850
Chris Lattnerdf986172009-01-02 07:01:27 +00001851 // If we have the value in the symbol table or fwd-ref table, return it.
1852 if (Val) {
1853 if (Val->getType() == Ty) return Val;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001854 if (Ty->isLabelTy())
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001855 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerdf986172009-01-02 07:01:27 +00001856 else
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001857 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001858 Val->getType()->getDescription() + "'");
1859 return 0;
1860 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001861
Duncan Sands47c51882010-02-16 14:50:09 +00001862 if (!Ty->isFirstClassType() && !Ty->isOpaqueTy() && !Ty->isLabelTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001863 P.Error(Loc, "invalid use of a non-first-class type");
1864 return 0;
1865 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001866
Chris Lattnerdf986172009-01-02 07:01:27 +00001867 // Otherwise, create a new forward reference for this value and remember it.
1868 Value *FwdVal;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001869 if (Ty->isLabelTy())
Owen Anderson1d0be152009-08-13 21:58:54 +00001870 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerdf986172009-01-02 07:01:27 +00001871 else
1872 FwdVal = new Argument(Ty);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001873
Chris Lattnerdf986172009-01-02 07:01:27 +00001874 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1875 return FwdVal;
1876}
1877
1878/// SetInstName - After an instruction is parsed and inserted into its
1879/// basic block, this installs its name.
1880bool LLParser::PerFunctionState::SetInstName(int NameID,
1881 const std::string &NameStr,
1882 LocTy NameLoc, Instruction *Inst) {
1883 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001884 if (Inst->getType()->isVoidTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001885 if (NameID != -1 || !NameStr.empty())
1886 return P.Error(NameLoc, "instructions returning void cannot have a name");
1887 return false;
1888 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001889
Chris Lattnerdf986172009-01-02 07:01:27 +00001890 // If this was a numbered instruction, verify that the instruction is the
1891 // expected value and resolve any forward references.
1892 if (NameStr.empty()) {
1893 // If neither a name nor an ID was specified, just use the next ID.
1894 if (NameID == -1)
1895 NameID = NumberedVals.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001896
Chris Lattnerdf986172009-01-02 07:01:27 +00001897 if (unsigned(NameID) != NumberedVals.size())
1898 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00001899 Twine(NumberedVals.size()) + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00001900
Chris Lattnerdf986172009-01-02 07:01:27 +00001901 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
1902 ForwardRefValIDs.find(NameID);
1903 if (FI != ForwardRefValIDs.end()) {
1904 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00001905 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001906 FI->second.first->getType()->getDescription() + "'");
1907 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2b4f28e2009-09-02 15:02:57 +00001908 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001909 ForwardRefValIDs.erase(FI);
1910 }
1911
1912 NumberedVals.push_back(Inst);
1913 return false;
1914 }
1915
1916 // Otherwise, the instruction had a name. Resolve forward refs and set it.
1917 std::map<std::string, std::pair<Value*, LocTy> >::iterator
1918 FI = ForwardRefVals.find(NameStr);
1919 if (FI != ForwardRefVals.end()) {
1920 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00001921 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001922 FI->second.first->getType()->getDescription() + "'");
1923 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes531552a2009-09-02 14:22:03 +00001924 delete FI->second.first;
Chris Lattnerdf986172009-01-02 07:01:27 +00001925 ForwardRefVals.erase(FI);
1926 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001927
Chris Lattnerdf986172009-01-02 07:01:27 +00001928 // Set the name on the instruction.
1929 Inst->setName(NameStr);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001930
Benjamin Krameraf812352010-10-16 11:28:23 +00001931 if (Inst->getName() != NameStr)
Daniel Dunbara279bc32009-09-20 02:20:51 +00001932 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerdf986172009-01-02 07:01:27 +00001933 NameStr + "'");
1934 return false;
1935}
1936
1937/// GetBB - Get a basic block with the specified name or ID, creating a
1938/// forward reference record if needed.
1939BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
1940 LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001941 return cast_or_null<BasicBlock>(GetVal(Name,
1942 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00001943}
1944
1945BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson1d0be152009-08-13 21:58:54 +00001946 return cast_or_null<BasicBlock>(GetVal(ID,
1947 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerdf986172009-01-02 07:01:27 +00001948}
1949
1950/// DefineBB - Define the specified basic block, which is either named or
1951/// unnamed. If there is an error, this returns null otherwise it returns
1952/// the block being defined.
1953BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
1954 LocTy Loc) {
1955 BasicBlock *BB;
1956 if (Name.empty())
1957 BB = GetBB(NumberedVals.size(), Loc);
1958 else
1959 BB = GetBB(Name, Loc);
1960 if (BB == 0) return 0; // Already diagnosed error.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001961
Chris Lattnerdf986172009-01-02 07:01:27 +00001962 // Move the block to the end of the function. Forward ref'd blocks are
1963 // inserted wherever they happen to be referenced.
1964 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001965
Chris Lattnerdf986172009-01-02 07:01:27 +00001966 // Remove the block from forward ref sets.
1967 if (Name.empty()) {
1968 ForwardRefValIDs.erase(NumberedVals.size());
1969 NumberedVals.push_back(BB);
1970 } else {
1971 // BB forward references are already in the function symbol table.
1972 ForwardRefVals.erase(Name);
1973 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001974
Chris Lattnerdf986172009-01-02 07:01:27 +00001975 return BB;
1976}
1977
1978//===----------------------------------------------------------------------===//
1979// Constants.
1980//===----------------------------------------------------------------------===//
1981
1982/// ParseValID - Parse an abstract value that doesn't necessarily have a
1983/// type implied. For example, if we parse "4" we don't know what integer type
1984/// it has. The value will later be combined with its type and checked for
Victor Hernandez24e64df2010-01-10 07:14:18 +00001985/// sanity. PFS is used to convert function-local operands of metadata (since
1986/// metadata operands are not just parsed here but also converted to values).
1987/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezbf170d42010-01-05 22:22:14 +00001988bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00001989 ID.Loc = Lex.getLoc();
1990 switch (Lex.getKind()) {
1991 default: return TokError("expected value token");
1992 case lltok::GlobalID: // @42
1993 ID.UIntVal = Lex.getUIntVal();
1994 ID.Kind = ValID::t_GlobalID;
1995 break;
1996 case lltok::GlobalVar: // @foo
1997 ID.StrVal = Lex.getStrVal();
1998 ID.Kind = ValID::t_GlobalName;
1999 break;
2000 case lltok::LocalVarID: // %42
2001 ID.UIntVal = Lex.getUIntVal();
2002 ID.Kind = ValID::t_LocalID;
2003 break;
2004 case lltok::LocalVar: // %foo
2005 case lltok::StringConstant: // "foo" - FIXME: REMOVE IN LLVM 3.0
2006 ID.StrVal = Lex.getStrVal();
2007 ID.Kind = ValID::t_LocalName;
2008 break;
Dan Gohman83448032010-07-14 18:26:50 +00002009 case lltok::exclaim: // !42, !{...}, or !"foo"
2010 return ParseMetadataValue(ID, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002011 case lltok::APSInt:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002012 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002013 ID.Kind = ValID::t_APSInt;
2014 break;
2015 case lltok::APFloat:
2016 ID.APFloatVal = Lex.getAPFloatVal();
2017 ID.Kind = ValID::t_APFloat;
2018 break;
2019 case lltok::kw_true:
Owen Anderson5defacc2009-07-31 17:39:07 +00002020 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002021 ID.Kind = ValID::t_Constant;
2022 break;
2023 case lltok::kw_false:
Owen Anderson5defacc2009-07-31 17:39:07 +00002024 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00002025 ID.Kind = ValID::t_Constant;
2026 break;
2027 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2028 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2029 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002030
Chris Lattnerdf986172009-01-02 07:01:27 +00002031 case lltok::lbrace: {
2032 // ValID ::= '{' ConstVector '}'
2033 Lex.Lex();
2034 SmallVector<Constant*, 16> Elts;
2035 if (ParseGlobalValueVector(Elts) ||
2036 ParseToken(lltok::rbrace, "expected end of struct constant"))
2037 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002038
Owen Andersond7f2a6c2009-08-05 23:16:16 +00002039 ID.ConstantVal = ConstantStruct::get(Context, Elts.data(),
2040 Elts.size(), false);
Chris Lattnerdf986172009-01-02 07:01:27 +00002041 ID.Kind = ValID::t_Constant;
2042 return false;
2043 }
2044 case lltok::less: {
2045 // ValID ::= '<' ConstVector '>' --> Vector.
2046 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2047 Lex.Lex();
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002048 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002049
Chris Lattnerdf986172009-01-02 07:01:27 +00002050 SmallVector<Constant*, 16> Elts;
2051 LocTy FirstEltLoc = Lex.getLoc();
2052 if (ParseGlobalValueVector(Elts) ||
2053 (isPackedStruct &&
2054 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2055 ParseToken(lltok::greater, "expected end of constant"))
2056 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002057
Chris Lattnerdf986172009-01-02 07:01:27 +00002058 if (isPackedStruct) {
Owen Andersonfba933c2009-07-01 23:57:11 +00002059 ID.ConstantVal =
Owen Andersond7f2a6c2009-08-05 23:16:16 +00002060 ConstantStruct::get(Context, Elts.data(), Elts.size(), true);
Chris Lattnerdf986172009-01-02 07:01:27 +00002061 ID.Kind = ValID::t_Constant;
2062 return false;
2063 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002064
Chris Lattnerdf986172009-01-02 07:01:27 +00002065 if (Elts.empty())
2066 return Error(ID.Loc, "constant vector must not be empty");
2067
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002068 if (!Elts[0]->getType()->isIntegerTy() &&
2069 !Elts[0]->getType()->isFloatingPointTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002070 return Error(FirstEltLoc,
2071 "vector elements must have integer or floating point type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002072
Chris Lattnerdf986172009-01-02 07:01:27 +00002073 // Verify that all the vector elements have the same type.
2074 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2075 if (Elts[i]->getType() != Elts[0]->getType())
2076 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002077 "vector element #" + Twine(i) +
Chris Lattnerdf986172009-01-02 07:01:27 +00002078 " is not of type '" + Elts[0]->getType()->getDescription());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002079
Chris Lattner2ca5c862011-02-15 00:14:00 +00002080 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerdf986172009-01-02 07:01:27 +00002081 ID.Kind = ValID::t_Constant;
2082 return false;
2083 }
2084 case lltok::lsquare: { // Array Constant
2085 Lex.Lex();
2086 SmallVector<Constant*, 16> Elts;
2087 LocTy FirstEltLoc = Lex.getLoc();
2088 if (ParseGlobalValueVector(Elts) ||
2089 ParseToken(lltok::rsquare, "expected end of array constant"))
2090 return true;
2091
2092 // Handle empty element.
2093 if (Elts.empty()) {
2094 // Use undef instead of an array because it's inconvenient to determine
2095 // the element type at this point, there being no elements to examine.
Chris Lattner081b5052009-01-05 07:52:51 +00002096 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerdf986172009-01-02 07:01:27 +00002097 return false;
2098 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002099
Chris Lattnerdf986172009-01-02 07:01:27 +00002100 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002101 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattnerdf986172009-01-02 07:01:27 +00002102 Elts[0]->getType()->getDescription());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002103
Owen Andersondebcb012009-07-29 22:17:13 +00002104 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002105
Chris Lattnerdf986172009-01-02 07:01:27 +00002106 // Verify all elements are correct type!
Chris Lattner6d6b3cc2009-01-02 08:49:06 +00002107 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002108 if (Elts[i]->getType() != Elts[0]->getType())
2109 return Error(FirstEltLoc,
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002110 "array element #" + Twine(i) +
Chris Lattnerdf986172009-01-02 07:01:27 +00002111 " is not of type '" +Elts[0]->getType()->getDescription());
2112 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002113
Owen Anderson1fd70962009-07-28 18:32:17 +00002114 ID.ConstantVal = ConstantArray::get(ATy, Elts.data(), Elts.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00002115 ID.Kind = ValID::t_Constant;
2116 return false;
2117 }
2118 case lltok::kw_c: // c "foo"
2119 Lex.Lex();
Owen Anderson1d0be152009-08-13 21:58:54 +00002120 ID.ConstantVal = ConstantArray::get(Context, Lex.getStrVal(), false);
Chris Lattnerdf986172009-01-02 07:01:27 +00002121 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2122 ID.Kind = ValID::t_Constant;
2123 return false;
2124
2125 case lltok::kw_asm: {
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002126 // ValID ::= 'asm' SideEffect? AlignStack? STRINGCONSTANT ',' STRINGCONSTANT
2127 bool HasSideEffect, AlignStack;
Chris Lattnerdf986172009-01-02 07:01:27 +00002128 Lex.Lex();
2129 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +00002130 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002131 ParseStringConstant(ID.StrVal) ||
2132 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002133 ParseToken(lltok::StringConstant, "expected constraint string"))
2134 return true;
2135 ID.StrVal2 = Lex.getStrVal();
Daniel Dunbarf0bb41c2009-11-07 23:51:55 +00002136 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002137 ID.Kind = ValID::t_InlineAsm;
2138 return false;
2139 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002140
Chris Lattner09d9ef42009-10-28 03:39:23 +00002141 case lltok::kw_blockaddress: {
2142 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2143 Lex.Lex();
2144
2145 ValID Fn, Label;
2146 LocTy FnLoc, LabelLoc;
2147
2148 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2149 ParseValID(Fn) ||
2150 ParseToken(lltok::comma, "expected comma in block address expression")||
2151 ParseValID(Label) ||
2152 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2153 return true;
Chris Lattnercdfc9402009-11-01 01:27:45 +00002154
Chris Lattner09d9ef42009-10-28 03:39:23 +00002155 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2156 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattnercdfc9402009-11-01 01:27:45 +00002157 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner09d9ef42009-10-28 03:39:23 +00002158 return Error(Label.Loc, "expected basic block name in blockaddress");
2159
2160 // Make a global variable as a placeholder for this reference.
2161 GlobalVariable *FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context),
2162 false, GlobalValue::InternalLinkage,
2163 0, "");
2164 ForwardRefBlockAddresses[Fn].push_back(std::make_pair(Label, FwdRef));
2165 ID.ConstantVal = FwdRef;
2166 ID.Kind = ValID::t_Constant;
2167 return false;
2168 }
2169
Chris Lattnerdf986172009-01-02 07:01:27 +00002170 case lltok::kw_trunc:
2171 case lltok::kw_zext:
2172 case lltok::kw_sext:
2173 case lltok::kw_fptrunc:
2174 case lltok::kw_fpext:
2175 case lltok::kw_bitcast:
2176 case lltok::kw_uitofp:
2177 case lltok::kw_sitofp:
2178 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002179 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00002180 case lltok::kw_inttoptr:
Daniel Dunbara279bc32009-09-20 02:20:51 +00002181 case lltok::kw_ptrtoint: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002182 unsigned Opc = Lex.getUIntVal();
Owen Anderson1d0be152009-08-13 21:58:54 +00002183 PATypeHolder DestTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00002184 Constant *SrcVal;
2185 Lex.Lex();
2186 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2187 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman24b108b2009-06-15 21:52:11 +00002188 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002189 ParseType(DestTy) ||
2190 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2191 return true;
2192 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2193 return Error(ID.Loc, "invalid cast opcode for cast from '" +
2194 SrcVal->getType()->getDescription() + "' to '" +
2195 DestTy->getDescription() + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002196 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Andersonfba933c2009-07-01 23:57:11 +00002197 SrcVal, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00002198 ID.Kind = ValID::t_Constant;
2199 return false;
2200 }
2201 case lltok::kw_extractvalue: {
2202 Lex.Lex();
2203 Constant *Val;
2204 SmallVector<unsigned, 4> Indices;
2205 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2206 ParseGlobalTypeAndValue(Val) ||
2207 ParseIndexList(Indices) ||
2208 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2209 return true;
Devang Patele8bc45a2009-11-03 19:06:07 +00002210
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002211 if (!Val->getType()->isAggregateType())
2212 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00002213 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
2214 Indices.end()))
2215 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foade3e51c02009-05-21 09:52:38 +00002216 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002217 ConstantExpr::getExtractValue(Val, Indices.data(), Indices.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00002218 ID.Kind = ValID::t_Constant;
2219 return false;
2220 }
2221 case lltok::kw_insertvalue: {
2222 Lex.Lex();
2223 Constant *Val0, *Val1;
2224 SmallVector<unsigned, 4> Indices;
2225 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2226 ParseGlobalTypeAndValue(Val0) ||
2227 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2228 ParseGlobalTypeAndValue(Val1) ||
2229 ParseIndexList(Indices) ||
2230 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2231 return true;
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002232 if (!Val0->getType()->isAggregateType())
2233 return Error(ID.Loc, "insertvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00002234 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
2235 Indices.end()))
2236 return Error(ID.Loc, "invalid indices for insertvalue");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002237 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1,
Owen Andersonfba933c2009-07-01 23:57:11 +00002238 Indices.data(), Indices.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00002239 ID.Kind = ValID::t_Constant;
2240 return false;
2241 }
2242 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002243 case lltok::kw_fcmp: {
Chris Lattnerdf986172009-01-02 07:01:27 +00002244 unsigned PredVal, Opc = Lex.getUIntVal();
2245 Constant *Val0, *Val1;
2246 Lex.Lex();
2247 if (ParseCmpPredicate(PredVal, Opc) ||
2248 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2249 ParseGlobalTypeAndValue(Val0) ||
2250 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2251 ParseGlobalTypeAndValue(Val1) ||
2252 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2253 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002254
Chris Lattnerdf986172009-01-02 07:01:27 +00002255 if (Val0->getType() != Val1->getType())
2256 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002257
Chris Lattnerdf986172009-01-02 07:01:27 +00002258 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002259
Chris Lattnerdf986172009-01-02 07:01:27 +00002260 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002261 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002262 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002263 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00002264 } else {
2265 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002266 if (!Val0->getType()->isIntOrIntVectorTy() &&
Duncan Sands1df98592010-02-16 11:11:14 +00002267 !Val0->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002268 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002269 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002270 }
2271 ID.Kind = ValID::t_Constant;
2272 return false;
2273 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002274
Chris Lattnerdf986172009-01-02 07:01:27 +00002275 // Binary Operators.
2276 case lltok::kw_add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002277 case lltok::kw_fadd:
Chris Lattnerdf986172009-01-02 07:01:27 +00002278 case lltok::kw_sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002279 case lltok::kw_fsub:
Chris Lattnerdf986172009-01-02 07:01:27 +00002280 case lltok::kw_mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002281 case lltok::kw_fmul:
Chris Lattnerdf986172009-01-02 07:01:27 +00002282 case lltok::kw_udiv:
2283 case lltok::kw_sdiv:
2284 case lltok::kw_fdiv:
2285 case lltok::kw_urem:
2286 case lltok::kw_srem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002287 case lltok::kw_frem:
2288 case lltok::kw_shl:
2289 case lltok::kw_lshr:
2290 case lltok::kw_ashr: {
Dan Gohman59858cf2009-07-27 16:11:46 +00002291 bool NUW = false;
2292 bool NSW = false;
2293 bool Exact = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002294 unsigned Opc = Lex.getUIntVal();
2295 Constant *Val0, *Val1;
2296 Lex.Lex();
Dan Gohman59858cf2009-07-27 16:11:46 +00002297 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnerf067d582011-02-07 16:40:21 +00002298 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2299 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002300 if (EatIfPresent(lltok::kw_nuw))
2301 NUW = true;
2302 if (EatIfPresent(lltok::kw_nsw)) {
2303 NSW = true;
2304 if (EatIfPresent(lltok::kw_nuw))
2305 NUW = true;
2306 }
Chris Lattnerf067d582011-02-07 16:40:21 +00002307 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2308 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002309 if (EatIfPresent(lltok::kw_exact))
2310 Exact = true;
2311 }
Chris Lattnerdf986172009-01-02 07:01:27 +00002312 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2313 ParseGlobalTypeAndValue(Val0) ||
2314 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2315 ParseGlobalTypeAndValue(Val1) ||
2316 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2317 return true;
2318 if (Val0->getType() != Val1->getType())
2319 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002320 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman59858cf2009-07-27 16:11:46 +00002321 if (NUW)
2322 return Error(ModifierLoc, "nuw only applies to integer operations");
2323 if (NSW)
2324 return Error(ModifierLoc, "nsw only applies to integer operations");
2325 }
Dan Gohman1eaac532010-05-03 22:44:19 +00002326 // Check that the type is valid for the operator.
2327 switch (Opc) {
2328 case Instruction::Add:
2329 case Instruction::Sub:
2330 case Instruction::Mul:
2331 case Instruction::UDiv:
2332 case Instruction::SDiv:
2333 case Instruction::URem:
2334 case Instruction::SRem:
Chris Lattnerf067d582011-02-07 16:40:21 +00002335 case Instruction::Shl:
2336 case Instruction::AShr:
2337 case Instruction::LShr:
Dan Gohman1eaac532010-05-03 22:44:19 +00002338 if (!Val0->getType()->isIntOrIntVectorTy())
2339 return Error(ID.Loc, "constexpr requires integer operands");
2340 break;
2341 case Instruction::FAdd:
2342 case Instruction::FSub:
2343 case Instruction::FMul:
2344 case Instruction::FDiv:
2345 case Instruction::FRem:
2346 if (!Val0->getType()->isFPOrFPVectorTy())
2347 return Error(ID.Loc, "constexpr requires fp operands");
2348 break;
2349 default: llvm_unreachable("Unknown binary operator!");
2350 }
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002351 unsigned Flags = 0;
2352 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2353 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35bda892011-02-06 21:44:57 +00002354 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002355 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman59858cf2009-07-27 16:11:46 +00002356 ID.ConstantVal = C;
Chris Lattnerdf986172009-01-02 07:01:27 +00002357 ID.Kind = ValID::t_Constant;
2358 return false;
2359 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002360
Chris Lattnerdf986172009-01-02 07:01:27 +00002361 // Logical Operations
Chris Lattnerdf986172009-01-02 07:01:27 +00002362 case lltok::kw_and:
2363 case lltok::kw_or:
2364 case lltok::kw_xor: {
2365 unsigned Opc = Lex.getUIntVal();
2366 Constant *Val0, *Val1;
2367 Lex.Lex();
2368 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2369 ParseGlobalTypeAndValue(Val0) ||
2370 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2371 ParseGlobalTypeAndValue(Val1) ||
2372 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2373 return true;
2374 if (Val0->getType() != Val1->getType())
2375 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002376 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002377 return Error(ID.Loc,
2378 "constexpr requires integer or integer vector operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002379 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002380 ID.Kind = ValID::t_Constant;
2381 return false;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002382 }
2383
Chris Lattnerdf986172009-01-02 07:01:27 +00002384 case lltok::kw_getelementptr:
2385 case lltok::kw_shufflevector:
2386 case lltok::kw_insertelement:
2387 case lltok::kw_extractelement:
2388 case lltok::kw_select: {
2389 unsigned Opc = Lex.getUIntVal();
2390 SmallVector<Constant*, 16> Elts;
Dan Gohmandd8004d2009-07-27 21:53:46 +00002391 bool InBounds = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002392 Lex.Lex();
Dan Gohmandd8004d2009-07-27 21:53:46 +00002393 if (Opc == Instruction::GetElementPtr)
Dan Gohmandcb40a32009-07-29 15:58:36 +00002394 InBounds = EatIfPresent(lltok::kw_inbounds);
Chris Lattnerdf986172009-01-02 07:01:27 +00002395 if (ParseToken(lltok::lparen, "expected '(' in constantexpr") ||
2396 ParseGlobalValueVector(Elts) ||
2397 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2398 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002399
Chris Lattnerdf986172009-01-02 07:01:27 +00002400 if (Opc == Instruction::GetElementPtr) {
Duncan Sands1df98592010-02-16 11:11:14 +00002401 if (Elts.size() == 0 || !Elts[0]->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002402 return Error(ID.Loc, "getelementptr requires pointer operand");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002403
Chris Lattnerdf986172009-01-02 07:01:27 +00002404 if (!GetElementPtrInst::getIndexedType(Elts[0]->getType(),
Eli Friedman4e9bac32009-07-24 21:56:17 +00002405 (Value**)(Elts.data() + 1),
2406 Elts.size() - 1))
Chris Lattnerdf986172009-01-02 07:01:27 +00002407 return Error(ID.Loc, "invalid indices for getelementptr");
Dan Gohmanf8dbee72009-09-07 23:54:19 +00002408 ID.ConstantVal = InBounds ?
2409 ConstantExpr::getInBoundsGetElementPtr(Elts[0],
2410 Elts.data() + 1,
2411 Elts.size() - 1) :
2412 ConstantExpr::getGetElementPtr(Elts[0],
2413 Elts.data() + 1, Elts.size() - 1);
Chris Lattnerdf986172009-01-02 07:01:27 +00002414 } else if (Opc == Instruction::Select) {
2415 if (Elts.size() != 3)
2416 return Error(ID.Loc, "expected three operands to select");
2417 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2418 Elts[2]))
2419 return Error(ID.Loc, Reason);
Owen Andersonbaf3c402009-07-29 18:55:55 +00002420 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002421 } else if (Opc == Instruction::ShuffleVector) {
2422 if (Elts.size() != 3)
2423 return Error(ID.Loc, "expected three operands to shufflevector");
2424 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2425 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Andersonfba933c2009-07-01 23:57:11 +00002426 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002427 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002428 } else if (Opc == Instruction::ExtractElement) {
2429 if (Elts.size() != 2)
2430 return Error(ID.Loc, "expected two operands to extractelement");
2431 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2432 return Error(ID.Loc, "invalid extractelement operands");
Owen Andersonbaf3c402009-07-29 18:55:55 +00002433 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002434 } else {
2435 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2436 if (Elts.size() != 3)
2437 return Error(ID.Loc, "expected three operands to insertelement");
2438 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2439 return Error(ID.Loc, "invalid insertelement operands");
Owen Andersonfba933c2009-07-01 23:57:11 +00002440 ID.ConstantVal =
Owen Andersonbaf3c402009-07-29 18:55:55 +00002441 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerdf986172009-01-02 07:01:27 +00002442 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002443
Chris Lattnerdf986172009-01-02 07:01:27 +00002444 ID.Kind = ValID::t_Constant;
2445 return false;
2446 }
2447 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002448
Chris Lattnerdf986172009-01-02 07:01:27 +00002449 Lex.Lex();
2450 return false;
2451}
2452
2453/// ParseGlobalValue - Parse a global value with the specified type.
Victor Hernandez92f238d2010-01-11 22:31:58 +00002454bool LLParser::ParseGlobalValue(const Type *Ty, Constant *&C) {
2455 C = 0;
Chris Lattnerdf986172009-01-02 07:01:27 +00002456 ValID ID;
Victor Hernandez92f238d2010-01-11 22:31:58 +00002457 Value *V = NULL;
2458 bool Parsed = ParseValID(ID) ||
2459 ConvertValIDToValue(Ty, ID, V, NULL);
2460 if (V && !(C = dyn_cast<Constant>(V)))
2461 return Error(ID.Loc, "global values must be constants");
2462 return Parsed;
Chris Lattnerdf986172009-01-02 07:01:27 +00002463}
2464
Victor Hernandez92f238d2010-01-11 22:31:58 +00002465bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
2466 PATypeHolder Type(Type::getVoidTy(Context));
2467 return ParseType(Type) ||
2468 ParseGlobalValue(Type, V);
2469}
2470
2471/// ParseGlobalValueVector
2472/// ::= /*empty*/
2473/// ::= TypeAndValue (',' TypeAndValue)*
2474bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts) {
2475 // Empty list.
2476 if (Lex.getKind() == lltok::rbrace ||
2477 Lex.getKind() == lltok::rsquare ||
2478 Lex.getKind() == lltok::greater ||
2479 Lex.getKind() == lltok::rparen)
2480 return false;
2481
2482 Constant *C;
2483 if (ParseGlobalTypeAndValue(C)) return true;
2484 Elts.push_back(C);
2485
2486 while (EatIfPresent(lltok::comma)) {
2487 if (ParseGlobalTypeAndValue(C)) return true;
2488 Elts.push_back(C);
2489 }
2490
2491 return false;
2492}
2493
Dan Gohman309b3af2010-08-24 02:24:03 +00002494bool LLParser::ParseMetadataListValue(ValID &ID, PerFunctionState *PFS) {
2495 assert(Lex.getKind() == lltok::lbrace);
2496 Lex.Lex();
2497
2498 SmallVector<Value*, 16> Elts;
2499 if (ParseMDNodeVector(Elts, PFS) ||
2500 ParseToken(lltok::rbrace, "expected end of metadata node"))
2501 return true;
2502
Jay Foadec9186b2011-04-21 19:59:31 +00002503 ID.MDNodeVal = MDNode::get(Context, Elts);
Dan Gohman309b3af2010-08-24 02:24:03 +00002504 ID.Kind = ValID::t_MDNode;
2505 return false;
2506}
2507
Dan Gohman83448032010-07-14 18:26:50 +00002508/// ParseMetadataValue
2509/// ::= !42
2510/// ::= !{...}
2511/// ::= !"string"
2512bool LLParser::ParseMetadataValue(ValID &ID, PerFunctionState *PFS) {
2513 assert(Lex.getKind() == lltok::exclaim);
2514 Lex.Lex();
2515
2516 // MDNode:
2517 // !{ ... }
Dan Gohman309b3af2010-08-24 02:24:03 +00002518 if (Lex.getKind() == lltok::lbrace)
2519 return ParseMetadataListValue(ID, PFS);
Dan Gohman83448032010-07-14 18:26:50 +00002520
2521 // Standalone metadata reference
2522 // !42
2523 if (Lex.getKind() == lltok::APSInt) {
2524 if (ParseMDNodeID(ID.MDNodeVal)) return true;
2525 ID.Kind = ValID::t_MDNode;
2526 return false;
2527 }
2528
2529 // MDString:
2530 // ::= '!' STRINGCONSTANT
2531 if (ParseMDString(ID.MDStringVal)) return true;
2532 ID.Kind = ValID::t_MDString;
2533 return false;
2534}
2535
Victor Hernandez92f238d2010-01-11 22:31:58 +00002536
2537//===----------------------------------------------------------------------===//
2538// Function Parsing.
2539//===----------------------------------------------------------------------===//
2540
2541bool LLParser::ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
2542 PerFunctionState *PFS) {
Duncan Sands1df98592010-02-16 11:11:14 +00002543 if (Ty->isFunctionTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002544 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002545
Chris Lattnerdf986172009-01-02 07:01:27 +00002546 switch (ID.Kind) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00002547 default: llvm_unreachable("Unknown ValID!");
Chris Lattnerdf986172009-01-02 07:01:27 +00002548 case ValID::t_LocalID:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002549 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2550 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
2551 return (V == 0);
Chris Lattnerdf986172009-01-02 07:01:27 +00002552 case ValID::t_LocalName:
Victor Hernandez92f238d2010-01-11 22:31:58 +00002553 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
2554 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
2555 return (V == 0);
2556 case ValID::t_InlineAsm: {
2557 const PointerType *PTy = dyn_cast<PointerType>(Ty);
2558 const FunctionType *FTy =
2559 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
2560 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
2561 return Error(ID.Loc, "invalid type for inline asm constraint string");
2562 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1, ID.UIntVal>>1);
2563 return false;
2564 }
2565 case ValID::t_MDNode:
2566 if (!Ty->isMetadataTy())
2567 return Error(ID.Loc, "metadata value must have metadata type");
2568 V = ID.MDNodeVal;
2569 return false;
2570 case ValID::t_MDString:
2571 if (!Ty->isMetadataTy())
2572 return Error(ID.Loc, "metadata value must have metadata type");
2573 V = ID.MDStringVal;
2574 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002575 case ValID::t_GlobalName:
2576 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
2577 return V == 0;
2578 case ValID::t_GlobalID:
2579 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
2580 return V == 0;
2581 case ValID::t_APSInt:
Duncan Sands1df98592010-02-16 11:11:14 +00002582 if (!Ty->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002583 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad40f8f622010-12-07 08:25:19 +00002584 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersoneed707b2009-07-24 23:12:02 +00002585 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00002586 return false;
2587 case ValID::t_APFloat:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00002588 if (!Ty->isFloatingPointTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00002589 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
2590 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002591
Chris Lattnerdf986172009-01-02 07:01:27 +00002592 // The lexer has no type info, so builds all float and double FP constants
2593 // as double. Fix this here. Long double does not need this.
2594 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002595 Ty->isFloatTy()) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002596 bool Ignored;
2597 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
2598 &Ignored);
2599 }
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002600 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002601
Chris Lattner959873d2009-01-05 18:24:23 +00002602 if (V->getType() != Ty)
2603 return Error(ID.Loc, "floating point constant does not have type '" +
2604 Ty->getDescription() + "'");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002605
Chris Lattnerdf986172009-01-02 07:01:27 +00002606 return false;
2607 case ValID::t_Null:
Duncan Sands1df98592010-02-16 11:11:14 +00002608 if (!Ty->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002609 return Error(ID.Loc, "null must be a pointer type");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002610 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerdf986172009-01-02 07:01:27 +00002611 return false;
2612 case ValID::t_Undef:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002613 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002614 if ((!Ty->isFirstClassType() || Ty->isLabelTy()) &&
Duncan Sands47c51882010-02-16 14:50:09 +00002615 !Ty->isOpaqueTy())
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002616 return Error(ID.Loc, "invalid type for undef constant");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002617 V = UndefValue::get(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002618 return false;
Chris Lattner081b5052009-01-05 07:52:51 +00002619 case ValID::t_EmptyArray:
Duncan Sands1df98592010-02-16 11:11:14 +00002620 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner081b5052009-01-05 07:52:51 +00002621 return Error(ID.Loc, "invalid empty array initializer");
Owen Anderson9e9a0d52009-07-30 23:03:37 +00002622 V = UndefValue::get(Ty);
Chris Lattner081b5052009-01-05 07:52:51 +00002623 return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002624 case ValID::t_Zero:
Chris Lattnere67c1aa2009-01-05 08:13:38 +00002625 // FIXME: LabelTy should not be a first-class type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002626 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002627 return Error(ID.Loc, "invalid type for null constant");
Owen Andersona7235ea2009-07-31 20:28:14 +00002628 V = Constant::getNullValue(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00002629 return false;
2630 case ValID::t_Constant:
Chris Lattner61c70e92010-08-28 04:09:24 +00002631 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerdf986172009-01-02 07:01:27 +00002632 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattnerfdfeb692010-02-12 20:49:41 +00002633
Chris Lattnerdf986172009-01-02 07:01:27 +00002634 V = ID.ConstantVal;
2635 return false;
2636 }
2637}
Daniel Dunbara279bc32009-09-20 02:20:51 +00002638
Chris Lattnerdf986172009-01-02 07:01:27 +00002639bool LLParser::ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS) {
2640 V = 0;
2641 ValID ID;
Victor Hernandezbf170d42010-01-05 22:22:14 +00002642 return ParseValID(ID, &PFS) ||
Victor Hernandez92f238d2010-01-11 22:31:58 +00002643 ConvertValIDToValue(Ty, ID, V, &PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002644}
2645
2646bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00002647 PATypeHolder T(Type::getVoidTy(Context));
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002648 return ParseType(T) ||
2649 ParseValue(T, V, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00002650}
2651
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002652bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
2653 PerFunctionState &PFS) {
2654 Value *V;
2655 Loc = Lex.getLoc();
2656 if (ParseTypeAndValue(V, PFS)) return true;
2657 if (!isa<BasicBlock>(V))
2658 return Error(Loc, "expected a basic block");
2659 BB = cast<BasicBlock>(V);
2660 return false;
2661}
2662
2663
Chris Lattnerdf986172009-01-02 07:01:27 +00002664/// FunctionHeader
2665/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindolabea46262011-01-08 16:42:36 +00002666/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Chris Lattnerdf986172009-01-02 07:01:27 +00002667/// OptionalAlign OptGC
2668bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
2669 // Parse the linkage.
2670 LocTy LinkageLoc = Lex.getLoc();
2671 unsigned Linkage;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002672
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002673 unsigned Visibility, RetAttrs;
2674 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00002675 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00002676 LocTy RetTypeLoc = Lex.getLoc();
2677 if (ParseOptionalLinkage(Linkage) ||
2678 ParseOptionalVisibility(Visibility) ||
2679 ParseOptionalCallingConv(CC) ||
2680 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00002681 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerdf986172009-01-02 07:01:27 +00002682 return true;
2683
2684 // Verify that the linkage is ok.
2685 switch ((GlobalValue::LinkageTypes)Linkage) {
2686 case GlobalValue::ExternalLinkage:
2687 break; // always ok.
2688 case GlobalValue::DLLImportLinkage:
Duncan Sands5f4ee1f2009-03-11 08:08:06 +00002689 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002690 if (isDefine)
2691 return Error(LinkageLoc, "invalid linkage for function definition");
2692 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +00002693 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +00002694 case GlobalValue::LinkerPrivateLinkage:
Bill Wendling5e721d72010-07-01 21:55:59 +00002695 case GlobalValue::LinkerPrivateWeakLinkage:
Bill Wendling55ae5152010-08-20 22:05:50 +00002696 case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002697 case GlobalValue::InternalLinkage:
Nick Lewycky55f64db2009-04-13 07:02:02 +00002698 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +00002699 case GlobalValue::LinkOnceAnyLinkage:
2700 case GlobalValue::LinkOnceODRLinkage:
2701 case GlobalValue::WeakAnyLinkage:
2702 case GlobalValue::WeakODRLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002703 case GlobalValue::DLLExportLinkage:
2704 if (!isDefine)
2705 return Error(LinkageLoc, "invalid linkage for function declaration");
2706 break;
2707 case GlobalValue::AppendingLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +00002708 case GlobalValue::CommonLinkage:
Chris Lattnerdf986172009-01-02 07:01:27 +00002709 return Error(LinkageLoc, "invalid function linkage type");
2710 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002711
Chris Lattner99bb3152009-01-05 08:00:30 +00002712 if (!FunctionType::isValidReturnType(RetType) ||
Duncan Sands47c51882010-02-16 14:50:09 +00002713 RetType->isOpaqueTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00002714 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002715
Chris Lattnerdf986172009-01-02 07:01:27 +00002716 LocTy NameLoc = Lex.getLoc();
Chris Lattnerf570e622009-02-18 21:48:13 +00002717
2718 std::string FunctionName;
2719 if (Lex.getKind() == lltok::GlobalVar) {
2720 FunctionName = Lex.getStrVal();
2721 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
2722 unsigned NameID = Lex.getUIntVal();
2723
2724 if (NameID != NumberedVals.size())
2725 return TokError("function expected to be numbered '%" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002726 Twine(NumberedVals.size()) + "'");
Chris Lattnerf570e622009-02-18 21:48:13 +00002727 } else {
2728 return TokError("expected function name");
2729 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002730
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002731 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002732
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002733 if (Lex.getKind() != lltok::lparen)
Chris Lattnerdf986172009-01-02 07:01:27 +00002734 return TokError("expected '(' in function argument list");
Daniel Dunbara279bc32009-09-20 02:20:51 +00002735
Chris Lattnerdf986172009-01-02 07:01:27 +00002736 std::vector<ArgInfo> ArgList;
2737 bool isVarArg;
Chris Lattnerdf986172009-01-02 07:01:27 +00002738 unsigned FuncAttrs;
Chris Lattnerdf986172009-01-02 07:01:27 +00002739 std::string Section;
Chris Lattnerdf986172009-01-02 07:01:27 +00002740 unsigned Alignment;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002741 std::string GC;
Rafael Espindola3971df52011-01-25 19:09:56 +00002742 bool UnnamedAddr;
2743 LocTy UnnamedAddrLoc;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002744
Chris Lattnerdfd19dd2009-01-05 18:34:07 +00002745 if (ParseArgumentList(ArgList, isVarArg, false) ||
Rafael Espindola3971df52011-01-25 19:09:56 +00002746 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
2747 &UnnamedAddrLoc) ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00002748 ParseOptionalAttrs(FuncAttrs, 2) ||
2749 (EatIfPresent(lltok::kw_section) &&
2750 ParseStringConstant(Section)) ||
2751 ParseOptionalAlignment(Alignment) ||
2752 (EatIfPresent(lltok::kw_gc) &&
2753 ParseStringConstant(GC)))
2754 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00002755
2756 // If the alignment was parsed as an attribute, move to the alignment field.
2757 if (FuncAttrs & Attribute::Alignment) {
2758 Alignment = Attribute::getAlignmentFromAttrs(FuncAttrs);
2759 FuncAttrs &= ~Attribute::Alignment;
2760 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002761
Chris Lattnerdf986172009-01-02 07:01:27 +00002762 // Okay, if we got here, the function is syntactically valid. Convert types
2763 // and do semantic checks.
2764 std::vector<const Type*> ParamTypeList;
2765 SmallVector<AttributeWithIndex, 8> Attrs;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002766 // FIXME : In 3.0, stop accepting zext, sext and inreg as optional function
Chris Lattnerdf986172009-01-02 07:01:27 +00002767 // attributes.
2768 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
2769 if (FuncAttrs & ObsoleteFuncAttrs) {
2770 RetAttrs |= FuncAttrs & ObsoleteFuncAttrs;
2771 FuncAttrs &= ~ObsoleteFuncAttrs;
2772 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002773
Chris Lattnerdf986172009-01-02 07:01:27 +00002774 if (RetAttrs != Attribute::None)
2775 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002776
Chris Lattnerdf986172009-01-02 07:01:27 +00002777 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
2778 ParamTypeList.push_back(ArgList[i].Type);
2779 if (ArgList[i].Attrs != Attribute::None)
2780 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
2781 }
2782
2783 if (FuncAttrs != Attribute::None)
2784 Attrs.push_back(AttributeWithIndex::get(~0, FuncAttrs));
2785
2786 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002787
Benjamin Kramerf0127052010-01-05 13:12:22 +00002788 if (PAL.paramHasAttr(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbara279bc32009-09-20 02:20:51 +00002789 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
2790
Owen Andersonfba933c2009-07-01 23:57:11 +00002791 const FunctionType *FT =
Owen Andersondebcb012009-07-29 22:17:13 +00002792 FunctionType::get(RetType, ParamTypeList, isVarArg);
2793 const PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerdf986172009-01-02 07:01:27 +00002794
2795 Fn = 0;
2796 if (!FunctionName.empty()) {
2797 // If this was a definition of a forward reference, remove the definition
2798 // from the forward reference table and fill in the forward ref.
2799 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
2800 ForwardRefVals.find(FunctionName);
2801 if (FRVI != ForwardRefVals.end()) {
2802 Fn = M->getFunction(FunctionName);
Chris Lattnerf1cfb952010-04-20 04:49:11 +00002803 if (Fn->getType() != PFT)
2804 return Error(FRVI->second.second, "invalid forward reference to "
2805 "function '" + FunctionName + "' with wrong type!");
2806
Chris Lattnerdf986172009-01-02 07:01:27 +00002807 ForwardRefVals.erase(FRVI);
2808 } else if ((Fn = M->getFunction(FunctionName))) {
2809 // If this function already exists in the symbol table, then it is
2810 // multiply defined. We accept a few cases for old backwards compat.
2811 // FIXME: Remove this stuff for LLVM 3.0.
2812 if (Fn->getType() != PFT || Fn->getAttributes() != PAL ||
2813 (!Fn->isDeclaration() && isDefine)) {
2814 // If the redefinition has different type or different attributes,
2815 // reject it. If both have bodies, reject it.
2816 return Error(NameLoc, "invalid redefinition of function '" +
2817 FunctionName + "'");
2818 } else if (Fn->isDeclaration()) {
2819 // Make sure to strip off any argument names so we can't get conflicts.
2820 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2821 AI != AE; ++AI)
2822 AI->setName("");
2823 }
Chris Lattner1d871c52009-10-25 23:22:50 +00002824 } else if (M->getNamedValue(FunctionName)) {
2825 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerdf986172009-01-02 07:01:27 +00002826 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002827
Dan Gohman41905542009-08-29 23:37:49 +00002828 } else {
Chris Lattnerdf986172009-01-02 07:01:27 +00002829 // If this is a definition of a forward referenced function, make sure the
2830 // types agree.
2831 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
2832 = ForwardRefValIDs.find(NumberedVals.size());
2833 if (I != ForwardRefValIDs.end()) {
2834 Fn = cast<Function>(I->second.first);
2835 if (Fn->getType() != PFT)
2836 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerd1e17032010-09-27 17:42:11 +00002837 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerdf986172009-01-02 07:01:27 +00002838 ForwardRefValIDs.erase(I);
2839 }
2840 }
2841
2842 if (Fn == 0)
2843 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
2844 else // Move the forward-reference to the correct spot in the module.
2845 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
2846
2847 if (FunctionName.empty())
2848 NumberedVals.push_back(Fn);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002849
Chris Lattnerdf986172009-01-02 07:01:27 +00002850 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
2851 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
2852 Fn->setCallingConv(CC);
2853 Fn->setAttributes(PAL);
Rafael Espindolabea46262011-01-08 16:42:36 +00002854 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerdf986172009-01-02 07:01:27 +00002855 Fn->setAlignment(Alignment);
2856 Fn->setSection(Section);
2857 if (!GC.empty()) Fn->setGC(GC.c_str());
Daniel Dunbara279bc32009-09-20 02:20:51 +00002858
Chris Lattnerdf986172009-01-02 07:01:27 +00002859 // Add all of the arguments we parsed to the function.
2860 Function::arg_iterator ArgIt = Fn->arg_begin();
2861 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
Chris Lattner5bda3792009-11-26 22:48:23 +00002862 // If we run out of arguments in the Function prototype, exit early.
2863 // FIXME: REMOVE THIS IN LLVM 3.0, this is just for the mismatch case above.
2864 if (ArgIt == Fn->arg_end()) break;
2865
Chris Lattnerdf986172009-01-02 07:01:27 +00002866 // If the argument has a name, insert it into the argument symbol table.
2867 if (ArgList[i].Name.empty()) continue;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002868
Chris Lattnerdf986172009-01-02 07:01:27 +00002869 // Set the name, if it conflicted, it will be auto-renamed.
2870 ArgIt->setName(ArgList[i].Name);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002871
Benjamin Krameraf812352010-10-16 11:28:23 +00002872 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerdf986172009-01-02 07:01:27 +00002873 return Error(ArgList[i].Loc, "redefinition of argument '%" +
2874 ArgList[i].Name + "'");
2875 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002876
Chris Lattnerdf986172009-01-02 07:01:27 +00002877 return false;
2878}
2879
2880
2881/// ParseFunctionBody
2882/// ::= '{' BasicBlock+ '}'
2883/// ::= 'begin' BasicBlock+ 'end' // FIXME: remove in LLVM 3.0
2884///
2885bool LLParser::ParseFunctionBody(Function &Fn) {
2886 if (Lex.getKind() != lltok::lbrace && Lex.getKind() != lltok::kw_begin)
2887 return TokError("expected '{' in function body");
2888 Lex.Lex(); // eat the {.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002889
Chris Lattner09d9ef42009-10-28 03:39:23 +00002890 int FunctionNumber = -1;
2891 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
2892
2893 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbara279bc32009-09-20 02:20:51 +00002894
Chris Lattner2fdf8db2010-01-09 19:20:07 +00002895 // We need at least one basic block.
2896 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_end)
2897 return TokError("function body requires at least one basic block");
2898
Chris Lattnerdf986172009-01-02 07:01:27 +00002899 while (Lex.getKind() != lltok::rbrace && Lex.getKind() != lltok::kw_end)
2900 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002901
Chris Lattnerdf986172009-01-02 07:01:27 +00002902 // Eat the }.
2903 Lex.Lex();
Daniel Dunbara279bc32009-09-20 02:20:51 +00002904
Chris Lattnerdf986172009-01-02 07:01:27 +00002905 // Verify function is ok.
Chris Lattner09d9ef42009-10-28 03:39:23 +00002906 return PFS.FinishFunction();
Chris Lattnerdf986172009-01-02 07:01:27 +00002907}
2908
2909/// ParseBasicBlock
2910/// ::= LabelStr? Instruction*
2911bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
2912 // If this basic block starts out with a name, remember it.
2913 std::string Name;
2914 LocTy NameLoc = Lex.getLoc();
2915 if (Lex.getKind() == lltok::LabelStr) {
2916 Name = Lex.getStrVal();
2917 Lex.Lex();
2918 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00002919
Chris Lattnerdf986172009-01-02 07:01:27 +00002920 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
2921 if (BB == 0) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002922
Chris Lattnerdf986172009-01-02 07:01:27 +00002923 std::string NameStr;
Daniel Dunbara279bc32009-09-20 02:20:51 +00002924
Chris Lattnerdf986172009-01-02 07:01:27 +00002925 // Parse the instructions in this block until we get a terminator.
2926 Instruction *Inst;
Chris Lattner1340dd32009-12-30 05:48:36 +00002927 SmallVector<std::pair<unsigned, MDNode *>, 4> MetadataOnInst;
Chris Lattnerdf986172009-01-02 07:01:27 +00002928 do {
2929 // This instruction may have three possibilities for a name: a) none
2930 // specified, b) name specified "%foo =", c) number specified: "%4 =".
2931 LocTy NameLoc = Lex.getLoc();
2932 int NameID = -1;
2933 NameStr = "";
Daniel Dunbara279bc32009-09-20 02:20:51 +00002934
Chris Lattnerdf986172009-01-02 07:01:27 +00002935 if (Lex.getKind() == lltok::LocalVarID) {
2936 NameID = Lex.getUIntVal();
2937 Lex.Lex();
2938 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
2939 return true;
2940 } else if (Lex.getKind() == lltok::LocalVar ||
2941 // FIXME: REMOVE IN LLVM 3.0
2942 Lex.getKind() == lltok::StringConstant) {
2943 NameStr = Lex.getStrVal();
2944 Lex.Lex();
2945 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
2946 return true;
2947 }
Devang Patelf633a062009-09-17 23:04:48 +00002948
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002949 switch (ParseInstruction(Inst, BB, PFS)) {
2950 default: assert(0 && "Unknown ParseInstruction result!");
2951 case InstError: return true;
2952 case InstNormal:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002953 BB->getInstList().push_back(Inst);
2954
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002955 // With a normal result, we check to see if the instruction is followed by
2956 // a comma and metadata.
2957 if (EatIfPresent(lltok::comma))
Dan Gohman9d072f52010-08-24 02:05:17 +00002958 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002959 return true;
2960 break;
2961 case InstExtraComma:
Chris Lattner4ba9d9b2010-04-07 04:08:57 +00002962 BB->getInstList().push_back(Inst);
2963
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002964 // If the instruction parser ate an extra comma at the end of it, it
2965 // *must* be followed by metadata.
Dan Gohman9d072f52010-08-24 02:05:17 +00002966 if (ParseInstructionMetadata(Inst, &PFS))
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002967 return true;
2968 break;
2969 }
Devang Patelf633a062009-09-17 23:04:48 +00002970
Chris Lattnerdf986172009-01-02 07:01:27 +00002971 // Set the name on the instruction.
2972 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
2973 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbara279bc32009-09-20 02:20:51 +00002974
Chris Lattnerdf986172009-01-02 07:01:27 +00002975 return false;
2976}
2977
2978//===----------------------------------------------------------------------===//
2979// Instruction Parsing.
2980//===----------------------------------------------------------------------===//
2981
2982/// ParseInstruction - Parse one of the many different instructions.
2983///
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00002984int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
2985 PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00002986 lltok::Kind Token = Lex.getKind();
2987 if (Token == lltok::Eof)
2988 return TokError("found end of file when expecting more instructions");
2989 LocTy Loc = Lex.getLoc();
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00002990 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerdf986172009-01-02 07:01:27 +00002991 Lex.Lex(); // Eat the keyword.
Daniel Dunbara279bc32009-09-20 02:20:51 +00002992
Chris Lattnerdf986172009-01-02 07:01:27 +00002993 switch (Token) {
2994 default: return Error(Loc, "expected instruction opcode");
2995 // Terminator Instructions.
Owen Anderson1d0be152009-08-13 21:58:54 +00002996 case lltok::kw_unwind: Inst = new UnwindInst(Context); return false;
2997 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerdf986172009-01-02 07:01:27 +00002998 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
2999 case lltok::kw_br: return ParseBr(Inst, PFS);
3000 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerab21db72009-10-28 00:19:10 +00003001 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003002 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
3003 // Binary Operators.
3004 case lltok::kw_add:
3005 case lltok::kw_sub:
Chris Lattnerf067d582011-02-07 16:40:21 +00003006 case lltok::kw_mul:
3007 case lltok::kw_shl: {
Chris Lattnerf067d582011-02-07 16:40:21 +00003008 bool NUW = EatIfPresent(lltok::kw_nuw);
3009 bool NSW = EatIfPresent(lltok::kw_nsw);
3010 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
3011
3012 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3013
3014 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
3015 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
3016 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003017 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00003018 case lltok::kw_fadd:
3019 case lltok::kw_fsub:
3020 case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
3021
Chris Lattner35bda892011-02-06 21:44:57 +00003022 case lltok::kw_sdiv:
Chris Lattnerf067d582011-02-07 16:40:21 +00003023 case lltok::kw_udiv:
3024 case lltok::kw_lshr:
3025 case lltok::kw_ashr: {
3026 bool Exact = EatIfPresent(lltok::kw_exact);
3027
3028 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
3029 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
3030 return false;
Dan Gohman59858cf2009-07-27 16:11:46 +00003031 }
3032
Chris Lattnerdf986172009-01-02 07:01:27 +00003033 case lltok::kw_urem:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003034 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnere914b592009-01-05 08:24:46 +00003035 case lltok::kw_fdiv:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003036 case lltok::kw_frem: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
Chris Lattnerdf986172009-01-02 07:01:27 +00003037 case lltok::kw_and:
3038 case lltok::kw_or:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003039 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003040 case lltok::kw_icmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003041 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003042 // Casts.
3043 case lltok::kw_trunc:
3044 case lltok::kw_zext:
3045 case lltok::kw_sext:
3046 case lltok::kw_fptrunc:
3047 case lltok::kw_fpext:
3048 case lltok::kw_bitcast:
3049 case lltok::kw_uitofp:
3050 case lltok::kw_sitofp:
3051 case lltok::kw_fptoui:
Daniel Dunbara279bc32009-09-20 02:20:51 +00003052 case lltok::kw_fptosi:
Chris Lattnerdf986172009-01-02 07:01:27 +00003053 case lltok::kw_inttoptr:
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003054 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerdf986172009-01-02 07:01:27 +00003055 // Other.
3056 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattner0088a5c2009-01-05 08:18:44 +00003057 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003058 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
3059 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
3060 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
3061 case lltok::kw_phi: return ParsePHI(Inst, PFS);
3062 case lltok::kw_call: return ParseCall(Inst, PFS, false);
3063 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
3064 // Memory.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003065 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
3066 case lltok::kw_malloc: return ParseAlloc(Inst, PFS, BB, false);
Victor Hernandez66284e02009-10-24 04:23:03 +00003067 case lltok::kw_free: return ParseFree(Inst, PFS, BB);
Chris Lattnerdf986172009-01-02 07:01:27 +00003068 case lltok::kw_load: return ParseLoad(Inst, PFS, false);
3069 case lltok::kw_store: return ParseStore(Inst, PFS, false);
3070 case lltok::kw_volatile:
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003071 if (EatIfPresent(lltok::kw_load))
Chris Lattnerdf986172009-01-02 07:01:27 +00003072 return ParseLoad(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003073 else if (EatIfPresent(lltok::kw_store))
Chris Lattnerdf986172009-01-02 07:01:27 +00003074 return ParseStore(Inst, PFS, true);
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003075 else
Chris Lattnerdf986172009-01-02 07:01:27 +00003076 return TokError("expected 'load' or 'store'");
Chris Lattnerdf986172009-01-02 07:01:27 +00003077 case lltok::kw_getresult: return ParseGetResult(Inst, PFS);
3078 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
3079 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
3080 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
3081 }
3082}
3083
3084/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
3085bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003086 if (Opc == Instruction::FCmp) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003087 switch (Lex.getKind()) {
3088 default: TokError("expected fcmp predicate (e.g. 'oeq')");
3089 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
3090 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
3091 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
3092 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
3093 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
3094 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
3095 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
3096 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
3097 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
3098 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
3099 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
3100 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
3101 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
3102 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
3103 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
3104 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
3105 }
3106 } else {
3107 switch (Lex.getKind()) {
3108 default: TokError("expected icmp predicate (e.g. 'eq')");
3109 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
3110 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
3111 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
3112 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
3113 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
3114 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
3115 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
3116 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
3117 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
3118 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
3119 }
3120 }
3121 Lex.Lex();
3122 return false;
3123}
3124
3125//===----------------------------------------------------------------------===//
3126// Terminator Instructions.
3127//===----------------------------------------------------------------------===//
3128
3129/// ParseRet - Parse a return instruction.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003130/// ::= 'ret' void (',' !dbg, !1)*
3131/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
3132/// ::= 'ret' TypeAndValue (',' TypeAndValue)+ (',' !dbg, !1)*
Devang Patelf633a062009-09-17 23:04:48 +00003133/// [[obsolete: LLVM 3.0]]
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003134int LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
3135 PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003136 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnera9a9e072009-03-09 04:49:14 +00003137 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003138
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00003139 if (Ty->isVoidTy()) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003140 Inst = ReturnInst::Create(Context);
Chris Lattnerdf986172009-01-02 07:01:27 +00003141 return false;
3142 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003143
Chris Lattnerdf986172009-01-02 07:01:27 +00003144 Value *RV;
3145 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003146
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003147 bool ExtraComma = false;
Devang Patelf633a062009-09-17 23:04:48 +00003148 if (EatIfPresent(lltok::comma)) {
Devang Patel0475c912009-09-29 00:01:14 +00003149 // Parse optional custom metadata, e.g. !dbg
Chris Lattner1d928312009-12-30 05:02:06 +00003150 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003151 ExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003152 } else {
3153 // The normal case is one return value.
Chris Lattner3f3a0f62009-12-29 21:25:40 +00003154 // FIXME: LLVM 3.0 remove MRV support for 'ret i32 1, i32 2', requiring
3155 // use of 'ret {i32,i32} {i32 1, i32 2}'
Devang Patelf633a062009-09-17 23:04:48 +00003156 SmallVector<Value*, 8> RVs;
3157 RVs.push_back(RV);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003158
Devang Patelf633a062009-09-17 23:04:48 +00003159 do {
Devang Patel0475c912009-09-29 00:01:14 +00003160 // If optional custom metadata, e.g. !dbg is seen then this is the
3161 // end of MRV.
Chris Lattner1d928312009-12-30 05:02:06 +00003162 if (Lex.getKind() == lltok::MetadataVar)
Daniel Dunbara279bc32009-09-20 02:20:51 +00003163 break;
3164 if (ParseTypeAndValue(RV, PFS)) return true;
3165 RVs.push_back(RV);
Devang Patelf633a062009-09-17 23:04:48 +00003166 } while (EatIfPresent(lltok::comma));
3167
3168 RV = UndefValue::get(PFS.getFunction().getReturnType());
3169 for (unsigned i = 0, e = RVs.size(); i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00003170 Instruction *I = InsertValueInst::Create(RV, RVs[i], i, "mrv");
3171 BB->getInstList().push_back(I);
3172 RV = I;
Devang Patelf633a062009-09-17 23:04:48 +00003173 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003174 }
3175 }
Devang Patelf633a062009-09-17 23:04:48 +00003176
Owen Anderson1d0be152009-08-13 21:58:54 +00003177 Inst = ReturnInst::Create(Context, RV);
Chris Lattnerf1bc7ce2009-12-30 05:23:43 +00003178 return ExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003179}
3180
3181
3182/// ParseBr
3183/// ::= 'br' TypeAndValue
3184/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3185bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
3186 LocTy Loc, Loc2;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003187 Value *Op0;
3188 BasicBlock *Op1, *Op2;
Chris Lattnerdf986172009-01-02 07:01:27 +00003189 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003190
Chris Lattnerdf986172009-01-02 07:01:27 +00003191 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
3192 Inst = BranchInst::Create(BB);
3193 return false;
3194 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003195
Owen Anderson1d0be152009-08-13 21:58:54 +00003196 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerdf986172009-01-02 07:01:27 +00003197 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003198
Chris Lattnerdf986172009-01-02 07:01:27 +00003199 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003200 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003201 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003202 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003203 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003204
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003205 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerdf986172009-01-02 07:01:27 +00003206 return false;
3207}
3208
3209/// ParseSwitch
3210/// Instruction
3211/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
3212/// JumpTable
3213/// ::= (TypeAndValue ',' TypeAndValue)*
3214bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
3215 LocTy CondLoc, BBLoc;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003216 Value *Cond;
3217 BasicBlock *DefaultBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003218 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
3219 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003220 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003221 ParseToken(lltok::lsquare, "expected '[' with switch table"))
3222 return true;
3223
Duncan Sands1df98592010-02-16 11:11:14 +00003224 if (!Cond->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003225 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003226
Chris Lattnerdf986172009-01-02 07:01:27 +00003227 // Parse the jump table pairs.
3228 SmallPtrSet<Value*, 32> SeenCases;
3229 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
3230 while (Lex.getKind() != lltok::rsquare) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003231 Value *Constant;
3232 BasicBlock *DestBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003233
Chris Lattnerdf986172009-01-02 07:01:27 +00003234 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
3235 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003236 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003237 return true;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003238
Chris Lattnerdf986172009-01-02 07:01:27 +00003239 if (!SeenCases.insert(Constant))
3240 return Error(CondLoc, "duplicate case value in switch");
3241 if (!isa<ConstantInt>(Constant))
3242 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003243
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003244 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerdf986172009-01-02 07:01:27 +00003245 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003246
Chris Lattnerdf986172009-01-02 07:01:27 +00003247 Lex.Lex(); // Eat the ']'.
Daniel Dunbara279bc32009-09-20 02:20:51 +00003248
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003249 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003250 for (unsigned i = 0, e = Table.size(); i != e; ++i)
3251 SI->addCase(Table[i].first, Table[i].second);
3252 Inst = SI;
3253 return false;
3254}
3255
Chris Lattnerab21db72009-10-28 00:19:10 +00003256/// ParseIndirectBr
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003257/// Instruction
Chris Lattnerab21db72009-10-28 00:19:10 +00003258/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
3259bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003260 LocTy AddrLoc;
3261 Value *Address;
3262 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerab21db72009-10-28 00:19:10 +00003263 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
3264 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003265 return true;
3266
Duncan Sands1df98592010-02-16 11:11:14 +00003267 if (!Address->getType()->isPointerTy())
Chris Lattnerab21db72009-10-28 00:19:10 +00003268 return Error(AddrLoc, "indirectbr address must have pointer type");
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003269
3270 // Parse the destination list.
3271 SmallVector<BasicBlock*, 16> DestList;
3272
3273 if (Lex.getKind() != lltok::rsquare) {
3274 BasicBlock *DestBB;
3275 if (ParseTypeAndBasicBlock(DestBB, PFS))
3276 return true;
3277 DestList.push_back(DestBB);
3278
3279 while (EatIfPresent(lltok::comma)) {
3280 if (ParseTypeAndBasicBlock(DestBB, PFS))
3281 return true;
3282 DestList.push_back(DestBB);
3283 }
3284 }
3285
3286 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
3287 return true;
3288
Chris Lattnerab21db72009-10-28 00:19:10 +00003289 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003290 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
3291 IBI->addDestination(DestList[i]);
3292 Inst = IBI;
3293 return false;
3294}
3295
3296
Chris Lattnerdf986172009-01-02 07:01:27 +00003297/// ParseInvoke
3298/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
3299/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
3300bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
3301 LocTy CallLoc = Lex.getLoc();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003302 unsigned RetAttrs, FnAttrs;
3303 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00003304 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003305 LocTy RetTypeLoc;
3306 ValID CalleeID;
3307 SmallVector<ParamInfo, 16> ArgList;
3308
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003309 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerdf986172009-01-02 07:01:27 +00003310 if (ParseOptionalCallingConv(CC) ||
3311 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003312 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003313 ParseValID(CalleeID) ||
3314 ParseParameterList(ArgList, PFS) ||
3315 ParseOptionalAttrs(FnAttrs, 2) ||
3316 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003317 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003318 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003319 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerdf986172009-01-02 07:01:27 +00003320 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003321
Chris Lattnerdf986172009-01-02 07:01:27 +00003322 // If RetType is a non-function pointer type, then this is the short syntax
3323 // for the call, which means that RetType is just the return type. Infer the
3324 // rest of the function argument types from the arguments that are present.
3325 const PointerType *PFTy = 0;
3326 const FunctionType *Ty = 0;
3327 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3328 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3329 // Pull out the types of all of the arguments...
3330 std::vector<const Type*> ParamTypes;
3331 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3332 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003333
Chris Lattnerdf986172009-01-02 07:01:27 +00003334 if (!FunctionType::isValidReturnType(RetType))
3335 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003336
Owen Andersondebcb012009-07-29 22:17:13 +00003337 Ty = FunctionType::get(RetType, ParamTypes, false);
3338 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003339 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003340
Chris Lattnerdf986172009-01-02 07:01:27 +00003341 // Look up the callee.
3342 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003343 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003344
Chris Lattnerdf986172009-01-02 07:01:27 +00003345 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3346 // function attributes.
3347 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3348 if (FnAttrs & ObsoleteFuncAttrs) {
3349 RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3350 FnAttrs &= ~ObsoleteFuncAttrs;
3351 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003352
Chris Lattnerdf986172009-01-02 07:01:27 +00003353 // Set up the Attributes for the function.
3354 SmallVector<AttributeWithIndex, 8> Attrs;
3355 if (RetAttrs != Attribute::None)
3356 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003357
Chris Lattnerdf986172009-01-02 07:01:27 +00003358 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003359
Chris Lattnerdf986172009-01-02 07:01:27 +00003360 // Loop through FunctionType's arguments and ensure they are specified
3361 // correctly. Also, gather any parameter attributes.
3362 FunctionType::param_iterator I = Ty->param_begin();
3363 FunctionType::param_iterator E = Ty->param_end();
3364 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3365 const Type *ExpectedTy = 0;
3366 if (I != E) {
3367 ExpectedTy = *I++;
3368 } else if (!Ty->isVarArg()) {
3369 return Error(ArgList[i].Loc, "too many arguments specified");
3370 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003371
Chris Lattnerdf986172009-01-02 07:01:27 +00003372 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3373 return Error(ArgList[i].Loc, "argument is not of expected type '" +
3374 ExpectedTy->getDescription() + "'");
3375 Args.push_back(ArgList[i].V);
3376 if (ArgList[i].Attrs != Attribute::None)
3377 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3378 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003379
Chris Lattnerdf986172009-01-02 07:01:27 +00003380 if (I != E)
3381 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003382
Chris Lattnerdf986172009-01-02 07:01:27 +00003383 if (FnAttrs != Attribute::None)
3384 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003385
Chris Lattnerdf986172009-01-02 07:01:27 +00003386 // Finish off the Attributes and check them
3387 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003388
Chris Lattnerf9be95f2009-10-27 19:13:16 +00003389 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB,
Chris Lattnerdf986172009-01-02 07:01:27 +00003390 Args.begin(), Args.end());
3391 II->setCallingConv(CC);
3392 II->setAttributes(PAL);
3393 Inst = II;
3394 return false;
3395}
3396
3397
3398
3399//===----------------------------------------------------------------------===//
3400// Binary Operators.
3401//===----------------------------------------------------------------------===//
3402
3403/// ParseArithmetic
Chris Lattnere914b592009-01-05 08:24:46 +00003404/// ::= ArithmeticOps TypeAndValue ',' Value
3405///
3406/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
3407/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerdf986172009-01-02 07:01:27 +00003408bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnere914b592009-01-05 08:24:46 +00003409 unsigned Opc, unsigned OperandType) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003410 LocTy Loc; Value *LHS, *RHS;
3411 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3412 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
3413 ParseValue(LHS->getType(), RHS, PFS))
3414 return true;
3415
Chris Lattnere914b592009-01-05 08:24:46 +00003416 bool Valid;
3417 switch (OperandType) {
Torok Edwinc23197a2009-07-14 16:55:14 +00003418 default: llvm_unreachable("Unknown operand type!");
Chris Lattnere914b592009-01-05 08:24:46 +00003419 case 0: // int or FP.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003420 Valid = LHS->getType()->isIntOrIntVectorTy() ||
3421 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnere914b592009-01-05 08:24:46 +00003422 break;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003423 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
3424 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnere914b592009-01-05 08:24:46 +00003425 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003426
Chris Lattnere914b592009-01-05 08:24:46 +00003427 if (!Valid)
3428 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003429
Chris Lattnerdf986172009-01-02 07:01:27 +00003430 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3431 return false;
3432}
3433
3434/// ParseLogical
3435/// ::= ArithmeticOps TypeAndValue ',' Value {
3436bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
3437 unsigned Opc) {
3438 LocTy Loc; Value *LHS, *RHS;
3439 if (ParseTypeAndValue(LHS, Loc, PFS) ||
3440 ParseToken(lltok::comma, "expected ',' in logical operation") ||
3441 ParseValue(LHS->getType(), RHS, PFS))
3442 return true;
3443
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003444 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003445 return Error(Loc,"instruction requires integer or integer vector operands");
3446
3447 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
3448 return false;
3449}
3450
3451
3452/// ParseCompare
3453/// ::= 'icmp' IPredicates TypeAndValue ',' Value
3454/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerdf986172009-01-02 07:01:27 +00003455bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
3456 unsigned Opc) {
3457 // Parse the integer/fp comparison predicate.
3458 LocTy Loc;
3459 unsigned Pred;
3460 Value *LHS, *RHS;
3461 if (ParseCmpPredicate(Pred, Opc) ||
3462 ParseTypeAndValue(LHS, Loc, PFS) ||
3463 ParseToken(lltok::comma, "expected ',' after compare value") ||
3464 ParseValue(LHS->getType(), RHS, PFS))
3465 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003466
Chris Lattnerdf986172009-01-02 07:01:27 +00003467 if (Opc == Instruction::FCmp) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003468 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003469 return Error(Loc, "fcmp requires floating point operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003470 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00003471 } else {
3472 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00003473 if (!LHS->getType()->isIntOrIntVectorTy() &&
Duncan Sands1df98592010-02-16 11:11:14 +00003474 !LHS->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003475 return Error(Loc, "icmp requires integer operands");
Dan Gohman1c8a23c2009-08-25 23:17:54 +00003476 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerdf986172009-01-02 07:01:27 +00003477 }
3478 return false;
3479}
3480
3481//===----------------------------------------------------------------------===//
3482// Other Instructions.
3483//===----------------------------------------------------------------------===//
3484
3485
3486/// ParseCast
3487/// ::= CastOpc TypeAndValue 'to' Type
3488bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
3489 unsigned Opc) {
3490 LocTy Loc; Value *Op;
Owen Anderson1d0be152009-08-13 21:58:54 +00003491 PATypeHolder DestTy(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003492 if (ParseTypeAndValue(Op, Loc, PFS) ||
3493 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
3494 ParseType(DestTy))
3495 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003496
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003497 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
3498 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerdf986172009-01-02 07:01:27 +00003499 return Error(Loc, "invalid cast opcode for cast from '" +
3500 Op->getType()->getDescription() + "' to '" +
3501 DestTy->getDescription() + "'");
Chris Lattnerf6f0bdf2009-03-01 00:53:13 +00003502 }
Chris Lattnerdf986172009-01-02 07:01:27 +00003503 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
3504 return false;
3505}
3506
3507/// ParseSelect
3508/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3509bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
3510 LocTy Loc;
3511 Value *Op0, *Op1, *Op2;
3512 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3513 ParseToken(lltok::comma, "expected ',' after select condition") ||
3514 ParseTypeAndValue(Op1, PFS) ||
3515 ParseToken(lltok::comma, "expected ',' after select value") ||
3516 ParseTypeAndValue(Op2, PFS))
3517 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003518
Chris Lattnerdf986172009-01-02 07:01:27 +00003519 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
3520 return Error(Loc, Reason);
Daniel Dunbara279bc32009-09-20 02:20:51 +00003521
Chris Lattnerdf986172009-01-02 07:01:27 +00003522 Inst = SelectInst::Create(Op0, Op1, Op2);
3523 return false;
3524}
3525
Chris Lattner0088a5c2009-01-05 08:18:44 +00003526/// ParseVA_Arg
3527/// ::= 'va_arg' TypeAndValue ',' Type
3528bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003529 Value *Op;
Owen Anderson1d0be152009-08-13 21:58:54 +00003530 PATypeHolder EltTy(Type::getVoidTy(Context));
Chris Lattner0088a5c2009-01-05 08:18:44 +00003531 LocTy TypeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003532 if (ParseTypeAndValue(Op, PFS) ||
3533 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattner0088a5c2009-01-05 08:18:44 +00003534 ParseType(EltTy, TypeLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003535 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003536
Chris Lattner0088a5c2009-01-05 08:18:44 +00003537 if (!EltTy->isFirstClassType())
3538 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003539
3540 Inst = new VAArgInst(Op, EltTy);
3541 return false;
3542}
3543
3544/// ParseExtractElement
3545/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
3546bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
3547 LocTy Loc;
3548 Value *Op0, *Op1;
3549 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3550 ParseToken(lltok::comma, "expected ',' after extract value") ||
3551 ParseTypeAndValue(Op1, PFS))
3552 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003553
Chris Lattnerdf986172009-01-02 07:01:27 +00003554 if (!ExtractElementInst::isValidOperands(Op0, Op1))
3555 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003556
Eric Christophera3500da2009-07-25 02:28:41 +00003557 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerdf986172009-01-02 07:01:27 +00003558 return false;
3559}
3560
3561/// ParseInsertElement
3562/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3563bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
3564 LocTy Loc;
3565 Value *Op0, *Op1, *Op2;
3566 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3567 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3568 ParseTypeAndValue(Op1, PFS) ||
3569 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
3570 ParseTypeAndValue(Op2, PFS))
3571 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003572
Chris Lattnerdf986172009-01-02 07:01:27 +00003573 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopher0aaf4e92009-07-23 01:01:32 +00003574 return Error(Loc, "invalid insertelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003575
Chris Lattnerdf986172009-01-02 07:01:27 +00003576 Inst = InsertElementInst::Create(Op0, Op1, Op2);
3577 return false;
3578}
3579
3580/// ParseShuffleVector
3581/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
3582bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
3583 LocTy Loc;
3584 Value *Op0, *Op1, *Op2;
3585 if (ParseTypeAndValue(Op0, Loc, PFS) ||
3586 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
3587 ParseTypeAndValue(Op1, PFS) ||
3588 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
3589 ParseTypeAndValue(Op2, PFS))
3590 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003591
Chris Lattnerdf986172009-01-02 07:01:27 +00003592 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
3593 return Error(Loc, "invalid extractelement operands");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003594
Chris Lattnerdf986172009-01-02 07:01:27 +00003595 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
3596 return false;
3597}
3598
3599/// ParsePHI
Chris Lattnerc6e20092009-10-18 05:27:44 +00003600/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003601int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003602 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003603 Value *Op0, *Op1;
3604 LocTy TypeLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003605
Chris Lattnerdf986172009-01-02 07:01:27 +00003606 if (ParseType(Ty) ||
3607 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
3608 ParseValue(Ty, Op0, PFS) ||
3609 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003610 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003611 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3612 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003613
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003614 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003615 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
3616 while (1) {
3617 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003618
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003619 if (!EatIfPresent(lltok::comma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003620 break;
3621
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003622 if (Lex.getKind() == lltok::MetadataVar) {
3623 AteExtraComma = true;
Devang Patela43d46f2009-10-16 18:45:49 +00003624 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003625 }
Devang Patela43d46f2009-10-16 18:45:49 +00003626
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003627 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003628 ParseValue(Ty, Op0, PFS) ||
3629 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson1d0be152009-08-13 21:58:54 +00003630 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003631 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
3632 return true;
3633 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003634
Chris Lattnerdf986172009-01-02 07:01:27 +00003635 if (!Ty->isFirstClassType())
3636 return Error(TypeLoc, "phi node must have first class type");
3637
Jay Foad3ecfc862011-03-30 11:28:46 +00003638 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerdf986172009-01-02 07:01:27 +00003639 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
3640 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
3641 Inst = PN;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003642 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003643}
3644
3645/// ParseCall
3646/// ::= 'tail'? 'call' OptionalCallingConv OptionalAttrs Type Value
3647/// ParameterList OptionalAttrs
3648bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
3649 bool isTail) {
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00003650 unsigned RetAttrs, FnAttrs;
3651 CallingConv::ID CC;
Owen Anderson1d0be152009-08-13 21:58:54 +00003652 PATypeHolder RetType(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003653 LocTy RetTypeLoc;
3654 ValID CalleeID;
3655 SmallVector<ParamInfo, 16> ArgList;
3656 LocTy CallLoc = Lex.getLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00003657
Chris Lattnerdf986172009-01-02 07:01:27 +00003658 if ((isTail && ParseToken(lltok::kw_call, "expected 'tail call'")) ||
3659 ParseOptionalCallingConv(CC) ||
3660 ParseOptionalAttrs(RetAttrs, 1) ||
Chris Lattnera9a9e072009-03-09 04:49:14 +00003661 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003662 ParseValID(CalleeID) ||
3663 ParseParameterList(ArgList, PFS) ||
3664 ParseOptionalAttrs(FnAttrs, 2))
3665 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003666
Chris Lattnerdf986172009-01-02 07:01:27 +00003667 // If RetType is a non-function pointer type, then this is the short syntax
3668 // for the call, which means that RetType is just the return type. Infer the
3669 // rest of the function argument types from the arguments that are present.
3670 const PointerType *PFTy = 0;
3671 const FunctionType *Ty = 0;
3672 if (!(PFTy = dyn_cast<PointerType>(RetType)) ||
3673 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
3674 // Pull out the types of all of the arguments...
3675 std::vector<const Type*> ParamTypes;
Eli Friedman83b4a972010-07-24 23:06:59 +00003676 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3677 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003678
Chris Lattnerdf986172009-01-02 07:01:27 +00003679 if (!FunctionType::isValidReturnType(RetType))
3680 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003681
Owen Andersondebcb012009-07-29 22:17:13 +00003682 Ty = FunctionType::get(RetType, ParamTypes, false);
3683 PFTy = PointerType::getUnqual(Ty);
Chris Lattnerdf986172009-01-02 07:01:27 +00003684 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003685
Chris Lattnerdf986172009-01-02 07:01:27 +00003686 // Look up the callee.
3687 Value *Callee;
Victor Hernandez92f238d2010-01-11 22:31:58 +00003688 if (ConvertValIDToValue(PFTy, CalleeID, Callee, &PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003689
Chris Lattnerdf986172009-01-02 07:01:27 +00003690 // FIXME: In LLVM 3.0, stop accepting zext, sext and inreg as optional
3691 // function attributes.
3692 unsigned ObsoleteFuncAttrs = Attribute::ZExt|Attribute::SExt|Attribute::InReg;
3693 if (FnAttrs & ObsoleteFuncAttrs) {
3694 RetAttrs |= FnAttrs & ObsoleteFuncAttrs;
3695 FnAttrs &= ~ObsoleteFuncAttrs;
3696 }
3697
3698 // Set up the Attributes for the function.
3699 SmallVector<AttributeWithIndex, 8> Attrs;
3700 if (RetAttrs != Attribute::None)
3701 Attrs.push_back(AttributeWithIndex::get(0, RetAttrs));
Daniel Dunbara279bc32009-09-20 02:20:51 +00003702
Chris Lattnerdf986172009-01-02 07:01:27 +00003703 SmallVector<Value*, 8> Args;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003704
Chris Lattnerdf986172009-01-02 07:01:27 +00003705 // Loop through FunctionType's arguments and ensure they are specified
3706 // correctly. Also, gather any parameter attributes.
3707 FunctionType::param_iterator I = Ty->param_begin();
3708 FunctionType::param_iterator E = Ty->param_end();
3709 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3710 const Type *ExpectedTy = 0;
3711 if (I != E) {
3712 ExpectedTy = *I++;
3713 } else if (!Ty->isVarArg()) {
3714 return Error(ArgList[i].Loc, "too many arguments specified");
3715 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003716
Chris Lattnerdf986172009-01-02 07:01:27 +00003717 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
3718 return Error(ArgList[i].Loc, "argument is not of expected type '" +
3719 ExpectedTy->getDescription() + "'");
3720 Args.push_back(ArgList[i].V);
3721 if (ArgList[i].Attrs != Attribute::None)
3722 Attrs.push_back(AttributeWithIndex::get(i+1, ArgList[i].Attrs));
3723 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003724
Chris Lattnerdf986172009-01-02 07:01:27 +00003725 if (I != E)
3726 return Error(CallLoc, "not enough parameters specified for call");
3727
3728 if (FnAttrs != Attribute::None)
3729 Attrs.push_back(AttributeWithIndex::get(~0, FnAttrs));
3730
3731 // Finish off the Attributes and check them
3732 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
Daniel Dunbara279bc32009-09-20 02:20:51 +00003733
Chris Lattnerdf986172009-01-02 07:01:27 +00003734 CallInst *CI = CallInst::Create(Callee, Args.begin(), Args.end());
3735 CI->setTailCall(isTail);
3736 CI->setCallingConv(CC);
3737 CI->setAttributes(PAL);
3738 Inst = CI;
3739 return false;
3740}
3741
3742//===----------------------------------------------------------------------===//
3743// Memory Instructions.
3744//===----------------------------------------------------------------------===//
3745
3746/// ParseAlloc
Devang Patelf633a062009-09-17 23:04:48 +00003747/// ::= 'malloc' Type (',' TypeAndValue)? (',' OptionalInfo)?
3748/// ::= 'alloca' Type (',' TypeAndValue)? (',' OptionalInfo)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003749int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS,
3750 BasicBlock* BB, bool isAlloca) {
Owen Anderson1d0be152009-08-13 21:58:54 +00003751 PATypeHolder Ty(Type::getVoidTy(Context));
Chris Lattnerdf986172009-01-02 07:01:27 +00003752 Value *Size = 0;
Chris Lattnereeb4a842009-07-02 23:08:13 +00003753 LocTy SizeLoc;
Chris Lattnerdf986172009-01-02 07:01:27 +00003754 unsigned Alignment = 0;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003755 if (ParseType(Ty)) return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003756
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003757 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003758 if (EatIfPresent(lltok::comma)) {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003759 if (Lex.getKind() == lltok::kw_align) {
3760 if (ParseOptionalAlignment(Alignment)) return true;
3761 } else if (Lex.getKind() == lltok::MetadataVar) {
3762 AteExtraComma = true;
Devang Patelf633a062009-09-17 23:04:48 +00003763 } else {
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003764 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
3765 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3766 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003767 }
3768 }
3769
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003770 if (Size && !Size->getType()->isIntegerTy())
3771 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003772
Victor Hernandez68afa542009-10-21 19:11:40 +00003773 if (isAlloca) {
Owen Anderson50dead02009-07-15 23:53:25 +00003774 Inst = new AllocaInst(Ty, Size, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003775 return AteExtraComma ? InstExtraComma : InstNormal;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +00003776 }
Victor Hernandez68afa542009-10-21 19:11:40 +00003777
3778 // Autoupgrade old malloc instruction to malloc call.
3779 // FIXME: Remove in LLVM 3.0.
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003780 if (Size && !Size->getType()->isIntegerTy(32))
3781 return Error(SizeLoc, "element count must be i32");
Victor Hernandez68afa542009-10-21 19:11:40 +00003782 const Type *IntPtrTy = Type::getInt32Ty(Context);
Victor Hernandez9d0b7042009-11-07 00:16:28 +00003783 Constant *AllocSize = ConstantExpr::getSizeOf(Ty);
3784 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, IntPtrTy);
Victor Hernandez68afa542009-10-21 19:11:40 +00003785 if (!MallocF)
3786 // Prototype malloc as "void *(int32)".
3787 // This function is renamed as "malloc" in ValidateEndOfModule().
Victor Hernandez336ea062009-10-23 00:59:10 +00003788 MallocF = cast<Function>(
3789 M->getOrInsertFunction("", Type::getInt8PtrTy(Context), IntPtrTy, NULL));
Victor Hernandez9d0b7042009-11-07 00:16:28 +00003790 Inst = CallInst::CreateMalloc(BB, IntPtrTy, Ty, AllocSize, Size, MallocF);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003791return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003792}
3793
3794/// ParseFree
3795/// ::= 'free' TypeAndValue
Victor Hernandez66284e02009-10-24 04:23:03 +00003796bool LLParser::ParseFree(Instruction *&Inst, PerFunctionState &PFS,
3797 BasicBlock* BB) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003798 Value *Val; LocTy Loc;
3799 if (ParseTypeAndValue(Val, Loc, PFS)) return true;
Duncan Sands1df98592010-02-16 11:11:14 +00003800 if (!Val->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003801 return Error(Loc, "operand to free must be a pointer");
Victor Hernandez66284e02009-10-24 04:23:03 +00003802 Inst = CallInst::CreateFree(Val, BB);
Chris Lattnerdf986172009-01-02 07:01:27 +00003803 return false;
3804}
3805
3806/// ParseLoad
Devang Patelf633a062009-09-17 23:04:48 +00003807/// ::= 'volatile'? 'load' TypeAndValue (',' OptionalInfo)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003808int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS,
3809 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003810 Value *Val; LocTy Loc;
Devang Patelf633a062009-09-17 23:04:48 +00003811 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003812 bool AteExtraComma = false;
3813 if (ParseTypeAndValue(Val, Loc, PFS) ||
3814 ParseOptionalCommaAlign(Alignment, AteExtraComma))
3815 return true;
Chris Lattnerdf986172009-01-02 07:01:27 +00003816
Duncan Sands1df98592010-02-16 11:11:14 +00003817 if (!Val->getType()->isPointerTy() ||
Chris Lattnerdf986172009-01-02 07:01:27 +00003818 !cast<PointerType>(Val->getType())->getElementType()->isFirstClassType())
3819 return Error(Loc, "load operand must be a pointer to a first class type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003820
Chris Lattnerdf986172009-01-02 07:01:27 +00003821 Inst = new LoadInst(Val, "", isVolatile, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003822 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003823}
3824
3825/// ParseStore
Dan Gohmana119de82009-06-14 23:30:43 +00003826/// ::= 'volatile'? 'store' TypeAndValue ',' TypeAndValue (',' 'align' i32)?
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003827int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS,
3828 bool isVolatile) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003829 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelf633a062009-09-17 23:04:48 +00003830 unsigned Alignment = 0;
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003831 bool AteExtraComma = false;
Chris Lattnerdf986172009-01-02 07:01:27 +00003832 if (ParseTypeAndValue(Val, Loc, PFS) ||
3833 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003834 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
3835 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003836 return true;
Devang Patelf633a062009-09-17 23:04:48 +00003837
Duncan Sands1df98592010-02-16 11:11:14 +00003838 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003839 return Error(PtrLoc, "store operand must be a pointer");
3840 if (!Val->getType()->isFirstClassType())
3841 return Error(Loc, "store operand must be a first class value");
3842 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
3843 return Error(Loc, "stored value and pointer type do not match");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003844
Chris Lattnerdf986172009-01-02 07:01:27 +00003845 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment);
Chris Lattnerc3a6c5c2009-12-30 05:44:30 +00003846 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003847}
3848
3849/// ParseGetResult
Dan Gohmana119de82009-06-14 23:30:43 +00003850/// ::= 'getresult' TypeAndValue ',' i32
Chris Lattnerdf986172009-01-02 07:01:27 +00003851/// FIXME: Remove support for getresult in LLVM 3.0
3852bool LLParser::ParseGetResult(Instruction *&Inst, PerFunctionState &PFS) {
3853 Value *Val; LocTy ValLoc, EltLoc;
3854 unsigned Element;
3855 if (ParseTypeAndValue(Val, ValLoc, PFS) ||
3856 ParseToken(lltok::comma, "expected ',' after getresult operand") ||
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003857 ParseUInt32(Element, EltLoc))
Chris Lattnerdf986172009-01-02 07:01:27 +00003858 return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003859
Duncan Sands1df98592010-02-16 11:11:14 +00003860 if (!Val->getType()->isStructTy() && !Val->getType()->isArrayTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003861 return Error(ValLoc, "getresult inst requires an aggregate operand");
3862 if (!ExtractValueInst::getIndexedType(Val->getType(), Element))
3863 return Error(EltLoc, "invalid getresult index for value");
3864 Inst = ExtractValueInst::Create(Val, Element);
3865 return false;
3866}
3867
3868/// ParseGetElementPtr
Dan Gohmandd8004d2009-07-27 21:53:46 +00003869/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003870int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003871 Value *Ptr, *Val; LocTy Loc, EltLoc;
Dan Gohmandd8004d2009-07-27 21:53:46 +00003872
Dan Gohmandcb40a32009-07-29 15:58:36 +00003873 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohmandd8004d2009-07-27 21:53:46 +00003874
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003875 if (ParseTypeAndValue(Ptr, Loc, PFS)) return true;
Daniel Dunbara279bc32009-09-20 02:20:51 +00003876
Duncan Sands1df98592010-02-16 11:11:14 +00003877 if (!Ptr->getType()->isPointerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003878 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003879
Chris Lattnerdf986172009-01-02 07:01:27 +00003880 SmallVector<Value*, 16> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003881 bool AteExtraComma = false;
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003882 while (EatIfPresent(lltok::comma)) {
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003883 if (Lex.getKind() == lltok::MetadataVar) {
3884 AteExtraComma = true;
Devang Patel6225d642009-10-13 18:49:55 +00003885 break;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003886 }
Chris Lattner3ed88ef2009-01-02 08:05:26 +00003887 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Duncan Sands1df98592010-02-16 11:11:14 +00003888 if (!Val->getType()->isIntegerTy())
Chris Lattnerdf986172009-01-02 07:01:27 +00003889 return Error(EltLoc, "getelementptr index must be an integer");
3890 Indices.push_back(Val);
3891 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00003892
Chris Lattnerdf986172009-01-02 07:01:27 +00003893 if (!GetElementPtrInst::getIndexedType(Ptr->getType(),
3894 Indices.begin(), Indices.end()))
3895 return Error(Loc, "invalid getelementptr indices");
3896 Inst = GetElementPtrInst::Create(Ptr, Indices.begin(), Indices.end());
Dan Gohmandd8004d2009-07-27 21:53:46 +00003897 if (InBounds)
Dan Gohmanf8dbee72009-09-07 23:54:19 +00003898 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003899 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003900}
3901
3902/// ParseExtractValue
3903/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003904int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003905 Value *Val; LocTy Loc;
3906 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003907 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003908 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003909 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003910 return true;
3911
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003912 if (!Val->getType()->isAggregateType())
3913 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerdf986172009-01-02 07:01:27 +00003914
3915 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices.begin(),
3916 Indices.end()))
3917 return Error(Loc, "invalid indices for extractvalue");
3918 Inst = ExtractValueInst::Create(Val, Indices.begin(), Indices.end());
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003919 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003920}
3921
3922/// ParseInsertValue
3923/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003924int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerdf986172009-01-02 07:01:27 +00003925 Value *Val0, *Val1; LocTy Loc0, Loc1;
3926 SmallVector<unsigned, 4> Indices;
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003927 bool AteExtraComma;
Chris Lattnerdf986172009-01-02 07:01:27 +00003928 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
3929 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
3930 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003931 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerdf986172009-01-02 07:01:27 +00003932 return true;
Chris Lattner628c13a2009-12-30 05:14:00 +00003933
Chris Lattnerfdfeb692010-02-12 20:49:41 +00003934 if (!Val0->getType()->isAggregateType())
3935 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbara279bc32009-09-20 02:20:51 +00003936
Chris Lattnerdf986172009-01-02 07:01:27 +00003937 if (!ExtractValueInst::getIndexedType(Val0->getType(), Indices.begin(),
3938 Indices.end()))
3939 return Error(Loc0, "invalid indices for insertvalue");
3940 Inst = InsertValueInst::Create(Val0, Val1, Indices.begin(), Indices.end());
Chris Lattnera7d7f2c2009-12-30 05:27:33 +00003941 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerdf986172009-01-02 07:01:27 +00003942}
Nick Lewycky21cc4462009-04-04 07:22:01 +00003943
3944//===----------------------------------------------------------------------===//
3945// Embedded metadata.
3946//===----------------------------------------------------------------------===//
3947
3948/// ParseMDNodeVector
Nick Lewyckycb337992009-05-10 20:57:05 +00003949/// ::= Element (',' Element)*
3950/// Element
3951/// ::= 'null' | TypeAndValue
Victor Hernandezbf170d42010-01-05 22:22:14 +00003952bool LLParser::ParseMDNodeVector(SmallVectorImpl<Value*> &Elts,
Victor Hernandez24e64df2010-01-10 07:14:18 +00003953 PerFunctionState *PFS) {
Dan Gohmanac809752010-07-13 19:33:27 +00003954 // Check for an empty list.
3955 if (Lex.getKind() == lltok::rbrace)
3956 return false;
3957
Nick Lewycky21cc4462009-04-04 07:22:01 +00003958 do {
Chris Lattnera7352392009-12-30 04:42:57 +00003959 // Null is a special case since it is typeless.
3960 if (EatIfPresent(lltok::kw_null)) {
3961 Elts.push_back(0);
3962 continue;
Nick Lewyckycb337992009-05-10 20:57:05 +00003963 }
Chris Lattnera7352392009-12-30 04:42:57 +00003964
3965 Value *V = 0;
3966 PATypeHolder Ty(Type::getVoidTy(Context));
3967 ValID ID;
Victor Hernandezbf170d42010-01-05 22:22:14 +00003968 if (ParseType(Ty) || ParseValID(ID, PFS) ||
Victor Hernandez92f238d2010-01-11 22:31:58 +00003969 ConvertValIDToValue(Ty, ID, V, PFS))
Chris Lattnera7352392009-12-30 04:42:57 +00003970 return true;
3971
Nick Lewyckycb337992009-05-10 20:57:05 +00003972 Elts.push_back(V);
Nick Lewycky21cc4462009-04-04 07:22:01 +00003973 } while (EatIfPresent(lltok::comma));
3974
3975 return false;
3976}